Skip to content

Commit f1dd994

Browse files
author
shashachu
committed
- Fixing some bugs with setting color and opacity
- Since Java doesn't have unsigned bytes, we need to & values by 0xff to make sure we don't go negative - Also adding support for newline character in LabelAtlas
1 parent af4d9bd commit f1dd994

File tree

4 files changed

+72
-40
lines changed

4 files changed

+72
-40
lines changed

src/org/cocos2d/actions/interval/TintBy.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ public static TintBy action(float t, int r, int g, int b) {
1717

1818
protected TintBy(float t, int r, int g, int b) {
1919
super(t);
20-
deltaR = r;
21-
deltaG = g;
22-
deltaB = b;
20+
deltaR = r & 0xff;
21+
deltaG = g & 0xff;
22+
deltaB = b & 0xff;
2323
}
2424

2525
@Override

src/org/cocos2d/actions/interval/TintTo.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ public static TintTo action(float t, int r, int g, int b) {
1717

1818
protected TintTo(float t, int r, int g, int b) {
1919
super(t);
20-
toR = r;
21-
toG = g;
22-
toB = b;
20+
toR = r & 0xff;
21+
toG = g & 0xff;
22+
toB = b & 0xff;
2323
}
2424

2525
@Override

src/org/cocos2d/nodes/LabelAtlas.java

Lines changed: 62 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -34,36 +34,68 @@ public void updateAtlasValues() {
3434

3535
String s = string;
3636

37-
for (int i = 0; i < n; i++) {
38-
39-
int a = s.charAt(i) - mapStartChar;
40-
float row = (a % itemsPerRow) * texStepX;
41-
float col = (a / itemsPerRow) * texStepY;
42-
43-
texCoord.bl_x = row; // A - x
44-
texCoord.bl_y = col; // A - y
45-
texCoord.br_x = row + texStepX; // B - x
46-
texCoord.br_y = col; // B - y
47-
texCoord.tl_x = row; // C - x
48-
texCoord.tl_y = col + texStepY; // C - y
49-
texCoord.tr_x = row + texStepX; // D - x
50-
texCoord.tr_y = col + texStepY; // D - y
51-
52-
vertex.bl_x = i * itemWidth; // A - x
53-
vertex.bl_y = 0; // A - y
54-
vertex.bl_z = 0; // A - z
55-
vertex.br_x = i * itemWidth + itemWidth; // B - x
56-
vertex.br_y = 0; // B - y
57-
vertex.br_z = 0; // B - z
58-
vertex.tl_x = i * itemWidth; // C - x
59-
vertex.tl_y = itemHeight; // C - y
60-
vertex.tl_z = 0; // C - z
61-
vertex.tr_x = i * itemWidth + itemWidth; // D - x
62-
vertex.tr_y = itemHeight; // D - y
63-
vertex.tr_z = 0; // D - z
64-
65-
textureAtlas_.updateQuad(texCoord, vertex, i);
37+
int xpos = 0;
38+
int ypos = 0;
39+
int numLines = 1;
40+
int maxLineWidth = 0;
41+
42+
for (int i = 0; i < n; i++)
43+
{
44+
// If we see a newline, reset the x-position back to zero
45+
// and update our y-position. Since OpenGL is y=0 bottom,
46+
// we negate numLines
47+
if (s.charAt(i) == '\n')
48+
{
49+
// Keep track of the longest line
50+
if (xpos > maxLineWidth)
51+
{
52+
maxLineWidth = xpos;
53+
}
54+
xpos = 0;
55+
ypos = -numLines * itemHeight;
56+
numLines++;
57+
}
58+
else
59+
{
60+
int a = s.charAt(i) - mapStartChar;
61+
float row = (a % itemsPerRow) * texStepX;
62+
float col = (a / itemsPerRow) * texStepY;
63+
64+
texCoord.bl_x = row; // A - x
65+
texCoord.bl_y = col; // A - y
66+
texCoord.br_x = row + texStepX; // B - x
67+
texCoord.br_y = col; // B - y
68+
texCoord.tl_x = row; // C - x
69+
texCoord.tl_y = col + texStepY; // C - y
70+
texCoord.tr_x = row + texStepX; // D - x
71+
texCoord.tr_y = col + texStepY; // D - y
72+
73+
vertex.bl_x = xpos * itemWidth; // A - x
74+
vertex.bl_y = ypos; // A - y
75+
vertex.bl_z = 0; // A - z
76+
vertex.br_x = xpos * itemWidth + itemWidth; // B - x
77+
vertex.br_y = ypos; // B - y
78+
vertex.br_z = 0; // B - z
79+
vertex.tl_x = xpos * itemWidth; // C - x
80+
vertex.tl_y = ypos + itemHeight; // C - y
81+
vertex.tl_z = 0; // C - z
82+
vertex.tr_x = xpos * itemWidth + itemWidth; // D - x
83+
vertex.tr_y = ypos + itemHeight; // D - y
84+
vertex.tr_z = 0; // D - z
85+
86+
textureAtlas_.updateQuad(texCoord, vertex, i);
87+
xpos++;
88+
}
6689
}
90+
91+
int numCharsWidth = n;
92+
// If this is multi-line, use the max line width instead of the total string length
93+
if (numLines > 1)
94+
{
95+
numCharsWidth = maxLineWidth;
96+
}
97+
98+
setContentSize(itemWidth * numCharsWidth, itemHeight * numLines);
6799
}
68100

69101
public void setString(String newString) {
@@ -82,7 +114,7 @@ public void draw(GL10 gl) {
82114
gl.glEnable(GL10.GL_TEXTURE_2D);
83115

84116
//fixed bug that can't show text on G1 by zt
85-
//gl.glColor4f(color_.r / 255f, color_.g / 255f, color_.b / 255f, opacity_ / 255f);
117+
gl.glColor4f(color_.r / 255f, color_.g / 255f, color_.b / 255f, opacity_ / 255f);
86118

87119
textureAtlas_.draw(gl, string.length());
88120

src/org/cocos2d/nodes/TextureNode.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ public int getOpacity() {
5050
}
5151

5252
public void setOpacity(int opacity) {
53-
opacity_ = opacity;
53+
opacity_ = opacity & 0xff;
5454
}
5555

5656
public void setColor(CCColor3B color) {
57-
color_.r = color.r;
58-
color_.g = color.g;
59-
color_.b = color.b;
57+
color_.r = color.r & 0xff;
58+
color_.g = color.g & 0xff;
59+
color_.b = color.b & 0xff;
6060
}
6161

6262
public CCColor3B getColor() {

0 commit comments

Comments
 (0)