Skip to content

Commit 08d9e55

Browse files
committed
Added Style set methods and moved shadow sync.
1 parent b5258b5 commit 08d9e55

2 files changed

Lines changed: 124 additions & 36 deletions

File tree

v3/src/gameobjects/text/TextStyle.js

Lines changed: 121 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ var TextStyle = new Class({
2525

2626
function TextStyle (text, style)
2727
{
28-
// Needed?
29-
this.text = text;
28+
this.parent = text;
3029

3130
this.font = propertyMap.font[1];
3231
this.backgroundColor = propertyMap.backgroundColor[1];
@@ -48,6 +47,39 @@ var TextStyle = new Class({
4847
}
4948
},
5049

50+
syncFont: function (canvas, context)
51+
{
52+
context.font = this.font;
53+
context.textBaseline = 'alphabetic';
54+
55+
context.fillStyle = this.fill;
56+
context.strokeStyle = this.stroke;
57+
58+
context.lineWidth = this.strokeThickness;
59+
context.lineCap = 'round';
60+
context.lineJoin = 'round';
61+
},
62+
63+
syncShadow: function (canvas, context, visible)
64+
{
65+
var style = this.style;
66+
67+
if (visible)
68+
{
69+
context.shadowOffsetX = style.shadowOffsetX;
70+
context.shadowOffsetY = style.shadowOffsetY;
71+
context.shadowColor = style.shadowColor;
72+
context.shadowBlur = style.shadowBlur;
73+
}
74+
else
75+
{
76+
context.shadowOffsetX = 0;
77+
context.shadowOffsetY = 0;
78+
context.shadowColor = 0;
79+
context.shadowBlur = 0;
80+
}
81+
},
82+
5183
setStyle: function (style)
5284
{
5385
for (var key in propertyMap)
@@ -58,26 +90,102 @@ var TextStyle = new Class({
5890
return this;
5991
},
6092

61-
syncToCanvas: function (canvas, context)
93+
setFont: function (font)
6294
{
63-
context.font = this.font;
64-
context.textBaseline = 'alphabetic';
6595

66-
context.fillStyle = this.fill;
67-
context.strokeStyle = this.stroke;
96+
this.font = font;
6897

69-
context.lineWidth = this.strokeThickness;
70-
context.lineCap = 'round';
71-
context.lineJoin = 'round';
98+
this.parent.updateText();
99+
100+
return this;
72101
},
73102

74-
setFont: function (font)
103+
setBackgroundColor: function (color)
75104
{
76-
this.font = font;
105+
this.backgroundColor = color;
77106

78-
// Tell Text it changed
107+
this.parent.updateText();
108+
109+
return this;
79110
},
80111

112+
setFill: function (color)
113+
{
114+
this.fill = color;
115+
116+
this.parent.updateText();
117+
118+
return this;
119+
},
120+
121+
setStroke: function (color, thickness)
122+
{
123+
if (color === undefined)
124+
{
125+
// Reset the stroke to zero (disabling it)
126+
this.strokeThickness = 0;
127+
}
128+
else
129+
{
130+
if (thickness === undefined) { thickness = this.strokeThickness; }
131+
132+
this.stroke = color;
133+
this.strokeThickness = thickness;
134+
}
135+
136+
this.parent.updateText();
137+
138+
return this;
139+
},
140+
141+
setShadow: function (x, y, color, blur, shadowStroke, shadowFill)
142+
{
143+
if (x === undefined) { x = 0; }
144+
if (y === undefined) { y = 0; }
145+
if (color === undefined) { color = '#000'; }
146+
if (blur === undefined) { blur = 0; }
147+
if (shadowStroke === undefined) { shadowStroke = false; }
148+
if (shadowFill === undefined) { shadowFill = false; }
149+
150+
this.shadowOffsetX = x;
151+
this.shadowOffsetY = y;
152+
this.shadowColor = color;
153+
this.shadowBlur = blur;
154+
this.shadowStroke = shadowStroke;
155+
this.shadowFill = shadowFill;
156+
157+
this.parent.updateText();
158+
159+
return this;
160+
},
161+
162+
setAlign: function (align)
163+
{
164+
if (align === undefined) { align = 'left'; }
165+
166+
this.align = align;
167+
168+
this.parent.updateText();
169+
170+
return this;
171+
},
172+
173+
setMaxLines: function (max)
174+
{
175+
if (max === undefined) { max = 0; }
176+
177+
this.maxLines = max;
178+
179+
this.parent.updateText();
180+
181+
return this;
182+
},
183+
184+
destroy: function ()
185+
{
186+
this.parent = undefined;
187+
}
188+
81189
});
82190

83191
module.exports = TextStyle;

v3/src/gameobjects/text/static/Text.js

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -153,42 +153,22 @@ var Text = new Class({
153153

154154
if (style.strokeThickness)
155155
{
156-
this.updateShadow(style.shadowStroke);
156+
this.style.syncShadow(style.shadowStroke);
157157

158158
context.strokeText(lines[i], linePositionX, linePositionY);
159159
}
160160

161161
if (style.fill)
162162
{
163-
this.updateShadow(style.shadowFill);
163+
this.style.syncShadow(style.shadowFill);
164164

165165
context.fillText(lines[i], linePositionX, linePositionY);
166166
}
167167
}
168-
},
168+
}
169169

170170
// Add style callback so we can chain filter effects
171171

172-
updateShadow: function (visible)
173-
{
174-
var context = this.context;
175-
var style = this.style;
176-
177-
if (visible)
178-
{
179-
context.shadowOffsetX = style.shadowOffsetX;
180-
context.shadowOffsetY = style.shadowOffsetY;
181-
context.shadowColor = style.shadowColor;
182-
context.shadowBlur = style.shadowBlur;
183-
}
184-
else
185-
{
186-
context.shadowOffsetX = 0;
187-
context.shadowOffsetY = 0;
188-
context.shadowColor = 0;
189-
context.shadowBlur = 0;
190-
}
191-
}
192172

193173
});
194174

0 commit comments

Comments
 (0)