I want to create Label & MenuItem in cocos2dx 3.0 with custom font - cocos2d-x

I want to create menu item & Label with title & give color to its title in cocos2dx 3.0
For Label m using
LabelTTF *designInfoLabel;
if(temp == 1){
designInfoLabel = LabelTTF::create("App", "Helvetica", 15, Size(545, 32), TextHAlignment::CENTER);
}
else{
designInfoLabel = LabelTTF::create("App", "Helvetica", 13, Size(545, 32), TextHAlignment::CENTER);
}
designInfoLabel->setPosition(100,100));
this->addChild(designInfoLabel);
but this code has lot of warnings also I want to set colour for text.

LabelTTF and CCMenu are deprecated, please use Label and Menu classes instead:
Label *designInfoLabel;
if(temp == 1){
designInfoLabel = Label::createWithTTF("App", "Helvetica.ttf", 15, Size(545, 32), TextHAlignment::CENTER);
}
else{
designInfoLabel = Label::createWithTTF("App", "Helvetica.ttf", 13, Size(545, 32), TextHAlignment::CENTER);
}
designInfoLabel->setPosition(100,100));
this->addChild(designInfoLabel);
Menu *buttonsMenu = Menu::create(supportMenuItem, nullptr);
buttonsMenu->setPosition(Vec2(0.0, 0.0));
this->addChild(buttonsMenu, 1);
To set color for the label, you can call Label::setTextColor(const Color4B &color); function. For example:
designInfoLabel->setTextColor(Color4B::BLACK);
or set the RGBA directly like:
designInfoLabel->setTextColor(Color4B(0, 0, 0, 0));

Related

How can i add an svg image and a label to a custom shape dynamically mxgraph

`function Domain() {
mxCylinder.call(this);
}
/*
The next lines use an mxCylinder instance to augment the
prototype of the shape ("inheritance") and reset the
constructor to the topmost function of the c'tor chain.
*/
mxUtils.extend(Domain, mxCylinder);
Domain.prototype.redrawPath = function (
path: any,
x: any,
y: any,
w: any,
h: any
) {
var dy = this.extrude * this.scale;
var dx = this.extrude * this.scale;
path.moveTo(0, dy);
path.lineTo(dx, 0);
path.lineTo(w, 0);
path.lineTo(w, h - dy);
path.lineTo(w, h);
path.lineTo(0, h);
path.lineTo(0, dy);
path.lineTo(dx, 0);
path.close();
};
mxCellRenderer.registerShape("Domain", Domain);
export function main(container: any) {
if (!mxClient.isBrowserSupported()) {
mxUtils.error("Browser is not supported!", 200, false);
} else {
mxEvent.disableContextMenu(container);
var graph = new mxGraph(container);
graph.setCellsCloneable(true);
graph.setHtmlLabels(true);
graph.setPanning(true);
graph.setEnabled(false);
graph.centerZoom = false;
new mxRubberband(graph);
configureStylesheet(graph);
var parent = graph.getDefaultParent();
// Adds cells to the model in a single step
graph.getModel().beginUpdate();
try {
var v1 = graph.insertVertex(
parent,
null,
"RAN",
20,
20,
240,
120,
// 'image;image=icons/secure-gdpr-user-svgrepo-com.svg;selectable=0;connectable=0;editable=0;movable=0;fillColor=hsl(36deg 98% 51%)'
//here i'm regestering my shape how can i add an image and a label inside the shape "shape=Domain;startSize=30;fillColor=hsl(36deg 98% 51%);spacingLeft=10;align=left;fontColor=#FFFFFF;fontSize=18;shadow=0;strokeColor=none;whiteSpace=wrap;img;"
);
} finally {
// Updates the display
graph.getModel().endUpdate();
}
}
}
i Used configure style sheet to add the image to the shape but when i do this
// style[mxConstants.STYLE_SHAPE] = "Domain";
the image disappears and i get only the sape
export function configureStylesheet(graph: any) {
var style: any = new Object();
style = mxUtils.clone(style);
// style[mxConstants.STYLE_PERIMETER] = mxPerimeter.RectanglePerimeter;
// style[mxConstants.STYLE_SHAPE] = mxConstants.SHAPE_LABEL;
// style[mxConstants.STYLE_SHAPE] = "Domain";
style[mxConstants.STYLE_SHAPE] = mxConstants.SHAPE_LABEL;
style[mxConstants.STYLE_ALIGN] = mxConstants.ALIGN_CENTER;
style[mxConstants.STYLE_VERTICAL_ALIGN] = mxConstants.ALIGN_TOP;
style[mxConstants.STYLE_IMAGE_ALIGN] = mxConstants.ALIGN_CENTER;
style[mxConstants.STYLE_IMAGE_VERTICAL_ALIGN] = mxConstants.ALIGN_TOP;
style[mxConstants.STYLE_IMAGE] = "icons/secure-gdpr-user-svgrepo-com.svg";
style[mxConstants.STYLE_IMAGE_WIDTH] = "48";
style[mxConstants.STYLE_IMAGE_HEIGHT] = "48";
style[mxConstants.STYLE_SPACING_TOP] = "80";
style[mxConstants.STYLE_SPACING] = "8";
graph.getStylesheet().putCellStyle("img", style);
Here's the approach i'm trying to do but the thing is that the image is overwriting the shape and not able to display the image in my customised shape. i need to have that custom shape taking the label and the image and dynamically for it to be implemented more than once`

Add the same child shape to multiple objects

Ok so I have this vector shape:
my_shape.graphics.lineStyle(1, 0x00FF00, 1);
my_shape.graphics.moveTo(30, 38);
my_shape.graphics.lineTo(7, 38);
my_shape.graphics.curveTo(-1, 38, -1, 36);
my_shape.graphics.lineTo(-1, 3);
my_shape.graphics.curveTo(-1, -1, 3, -1);
my_shape.graphics.lineTo(35, -1);
my_shape.graphics.curveTo(39, -1, 39, 3);
my_shape.graphics.lineTo(39, 30);
Since I couldn't find how can I change color of the existing shape I created 3 of these with only difference in the color,is it possible to change color of the existing shape?
Is it possible to use that same shape in other classes? Or I have to create it again?
if(ability3.Locked == true && Ability.suma >= 5){
ability3.Locked = false;
ability3.addChild(my_shape);
ability4.Locked = false;
ability4.addChild(my_shapea);
ability5.Locked = false;
ability5.addChild(my_shapeb);
}
if(ability6.Locked == true && Ability.suma >= 10){
ability6.Locked = false;
ability6.addChild(my_shapeb);
ability7.Locked = false;
ability7.addChild(my_shapea);
ability8.Locked = false;
ability8.addChild(my_shape);
ability9.Locked = false;
ability9.addChild(my_shape);
}
When this first code (ability 3 to 5) runs all 3 childs show but when it comes to running other if command abilities 6 to 9 get their children but 3 to 5 got their removed..can i use multiple times the same shape?
I hope you understand what I meant xD
Thanks.
Here is an example of copying graphics from one object to another, and changing their color:
var newShape:Shape = new Shape();
newShape.graphics.copyFrom(my_shape.graphics);
var colorTrans:ColorTransform = new ColorTransform();
colorTrans.color = 0xFFFFFF;
newShape.transform.colorTransform = colorTrans;
A display object can only be on a single display list at a time. This means that if I do the following:
container1.addChild(my_shape);
container2.addChild(my_shape);
my_shape will end up on container2
Normally, you would create a class for this particular shape and instantiate it as needed, but judging from your code, I'm not sure that you're using OOP (please correct me if I'm wrong and I'll update my answer).
Having said that, you can always write a function that creates the shape you need using the color of your choosing:
function createShape(color:uint):Shape
{
var shape:Shape = new Shape();
shape.graphics.lineStyle(1, color, 1);
shape.graphics.moveTo(30, 38);
shape.graphics.lineTo(7, 38);
shape.graphics.curveTo(-1, 38, -1, 36);
shape.graphics.lineTo(-1, 3);
shape.graphics.curveTo(-1, -1, 3, -1);
shape.graphics.lineTo(35, -1);
shape.graphics.curveTo(39, -1, 39, 3);
shape.graphics.lineTo(39, 30);
return shape;
}
Now you can call that function whenever you need to create the shape:
if(ability3.Locked == true && Ability.suma >= 5){
ability3.Locked = false;
ability3.addChild(createShape(someColor));
ability4.Locked = false;
ability4.addChild(createShape(someOtherColor));
ability5.Locked = false;
ability5.addChild(createShape(anotherColor));
}
Where someColor, someOtherColor, and anotherColor each represent a different uint (color).

Text on HTML5 Canvas loop

I have code here that displays the days of the week horizontally. How can I make it so that it is displayed in a for loop horizontally but also taking into account the size of the canvas? (the size of the canvas in this case is 600)
canvasObj1 = document.getElementById('myCanvas1');
context = canvasObj1.getContext('2d');
context.fillStyle = "#5D6C7B";
context.font = "12px sans-serif";
context.fillText("Monday", 10, 225);
context.fillText("Tuesday", 70, 225);
context.fillText("Wednesday", 130, 225);
context.fillText("Thursday", 210, 225);
context.fillText("Friday", 275, 225);
context.fillText("Saturday", 330, 225);
context.fillText("Sunday", 400, 225);
Here is one way you can do it:
Online demo
// put week names in an array
var days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday',
'Friday', 'Saturday', 'Sunday'],
gap = 5, // gap between the names
x = 0, // for drawing text
w = 0, // for measuring total width
i = 0; // generic counter
context.fillStyle = "#5D6C7B";
context.font = "12px sans-serif";
// calc total width incl. gaps
for (; i < days.length; i++) {
w += (context.measureText(days[i]).width + gap);
}
// fine adjust width removing last gap and adding a couple of pixels for space
w = w - gap + 2;
// adjust scale
context.scale(canvas.width / w, 1);
// draw the texts
for (i = 0; i < days.length; i++) {
context.fillText(days[i], x, 225);
x += (context.measureText(days[i]).width + gap);
}
Just remember to reset the scale or use it to draw other elements relative to texts. One other thing you can do is to store the calculated positions to another array so you can use that with for example vertical lines and so forth.
Another way, in case there is space for all text, is to calculate the total width of the text only (not using the gap value), then subtract it from canvas width, divide on 6 to get an average gap value which you then use with the draw loop.

how to change the background color of message box in WP8

Actually, windows phone provide default message box with black background. I am already using custom message box using its toolkit. It has background property but I am unable to assign any value. It gives type casting error.
hope so someone will tell the right value, I alreay tried (0,0,0,0) / "grey" / colors.grey but same error
CustomMessageBox msgbox = new CustomMessageBox()
{
Caption = "Memory Race",
Message ="This is custom message box!",
LeftButtonContent = "OK",
Background = "what?"
};
you can set backgroundcolor
Background = new SolidColorBrush(Colors.Green);
Background property is a color brush.
You must do this:
*.Background = new SolidColorBrush(ConvertStringToColor("#0D0D0E"));
private Color ConvertStringToColor(String hex)
{
//remove the # at the front
hex = hex.Replace("#", "");
byte a = 255;
byte r = 255;
byte g = 255;
byte b = 255;
int start = 0;
//handle ARGB strings (8 characters long)
if (hex.Length == 8)
{
a = byte.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
start = 2;
}
//convert RGB characters to bytes
r = byte.Parse(hex.Substring(start, 2), System.Globalization.NumberStyles.HexNumber);
g = byte.Parse(hex.Substring(start + 2, 2), System.Globalization.NumberStyles.HexNumber);
b = byte.Parse(hex.Substring(start + 4, 2), System.Globalization.NumberStyles.HexNumber);
return Color.FromArgb(a, r, g, b);
}
See this link and stylize your CustomMessageBox

If statement to change text colour

for(var temp:int = 0;temp<recipeNum;temp++)
{
if ((temp == 1) || (temp == 2) || (temp == 6) || (temp == 9))
{
textRecipe.textColor = 0x0000FF;
}
else
{
textRecipe.textColor = 0x000000;
}
textRecipe.text += "\n" + recipe[temp];
addChild(textRecipe);
}
The problem with this code all the text on screen is black. I want temp 1,2,6,9 to be blue, any solutions.
If I'm not wrong you're using only one TextField named textRecipe.
Note that, even if you add it recipeNum times to the stage, it's always the same object.
Assigning textColor property, it modifies the color of the entire text, so the last color assigned (probably black) will be the color of the entire text.
Either use more than one TextField or use a TextFormat to assign color to a part of the text:
var myFormat = new TextFormat();
myFormat.color = 0x0000FF;
textRecipe.setTextFormat(myFormat, 5, 8); //sets color blue to chars from 5 to 8
Let me know if you need more help.