1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
package tsukuba_bunko.peko.scenario.text; |
10 |
|
|
11 |
|
import java.awt.Dimension; |
12 |
|
|
13 |
|
import java.awt.font.LineBreakMeasurer; |
14 |
|
import java.awt.font.TextAttribute; |
15 |
|
import java.awt.font.TextLayout; |
16 |
|
|
17 |
|
import java.text.AttributedString; |
18 |
|
|
19 |
|
import tsukuba_bunko.peko.Logger; |
20 |
|
|
21 |
|
import tsukuba_bunko.peko.canvas.text.Line; |
22 |
|
import tsukuba_bunko.peko.canvas.text.Page; |
23 |
|
|
24 |
|
import tsukuba_bunko.peko.resource.ResourceManager; |
25 |
|
|
26 |
|
import tsukuba_bunko.peko.scenario.Coordinator; |
27 |
|
import tsukuba_bunko.peko.scenario.SceneContext; |
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
public class TextCoordinator extends Coordinator { |
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
0 |
protected boolean _pageConfigured = false; |
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
0 |
protected float _lineSpan = 0f; |
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
0 |
protected Line _lastLine = null; |
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
0 |
protected Dimension _sizeCache = new Dimension(); |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
public TextCoordinator() |
63 |
|
{ |
64 |
0 |
super(); |
65 |
0 |
} |
66 |
|
|
67 |
|
|
68 |
|
public void prepare( SceneContext context, Thread activeThread ) |
69 |
|
{ |
70 |
0 |
super.prepare( context, activeThread ); |
71 |
0 |
_pageConfigured = false; |
72 |
0 |
_lastLine = null; |
73 |
0 |
} |
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
public void begin() |
80 |
|
{ |
81 |
0 |
if( isActiveThread() ) { |
82 |
0 |
_lastLine = null; |
83 |
0 |
getActionControler().setSaveEnabled( true ); |
84 |
|
} |
85 |
0 |
} |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
public void commit() |
91 |
|
{ |
92 |
0 |
if( isActiveThread() ) { |
93 |
0 |
getActionControler().setSaveEnabled( false ); |
94 |
0 |
getCurrentPage().commit(); |
95 |
|
} |
96 |
0 |
} |
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
public void pushText( String text ) |
103 |
|
{ |
104 |
0 |
if( !isActiveThread() ) { |
105 |
0 |
return; |
106 |
|
} |
107 |
|
|
108 |
0 |
Page page = getCurrentPage(); |
109 |
0 |
if( !_pageConfigured ) { |
110 |
0 |
PageConfigurator.configure( page, getSceneContext() ); |
111 |
0 |
_pageConfigured = true; |
112 |
0 |
_lineSpan = getLineSpan(); |
113 |
|
} |
114 |
|
|
115 |
0 |
int length = text.length(); |
116 |
0 |
if( length == 0 ) { |
117 |
0 |
Logger.debug( "[scnario.text] empty string is pecified." ); |
118 |
0 |
return; |
119 |
|
} |
120 |
|
|
121 |
0 |
if( _lastLine != null ) { |
122 |
0 |
text = _lastLine.getText() + text; |
123 |
0 |
length = text.length(); |
124 |
|
} |
125 |
0 |
Logger.debug( text ); |
126 |
|
|
127 |
0 |
AttributedString astring = new AttributedString( text ); |
128 |
0 |
astring.addAttribute( TextAttribute.FONT, page.getDefaultFont() ); |
129 |
0 |
astring.addAttribute( TextAttribute.FOREGROUND, page.getForeground() ); |
130 |
|
|
131 |
0 |
LineBreakMeasurer lbm = new LineBreakMeasurer( astring.getIterator(), page.getFontRenderContext() ); |
132 |
0 |
float maxWidth = page.getMaxLineWidth(); |
133 |
0 |
float lineSpan = _lineSpan; |
134 |
0 |
boolean conflictWait = false; |
135 |
0 |
TextLayout layout = null; |
136 |
0 |
Line line = null; |
137 |
|
|
138 |
0 |
layout = lbm.nextLayout( maxWidth ); |
139 |
0 |
line = new Line(); |
140 |
0 |
line.setTextLayout( layout ); |
141 |
0 |
line.setText( text.substring(0, lbm.getPosition()) ); |
142 |
0 |
line.setForeground( page.getForeground() ); |
143 |
0 |
line.setShadowColor( page.getShadow() ); |
144 |
|
|
145 |
0 |
if( _lastLine == null ) { |
146 |
0 |
if( isActiveThread() ) { |
147 |
0 |
line.setLineSpan( lineSpan ); |
148 |
0 |
if( !page.isAdaptive(line) ) { |
149 |
0 |
page = advancesNewPage(); |
150 |
|
} |
151 |
0 |
page.addLine( line ); |
152 |
0 |
} |
153 |
|
else { |
154 |
0 |
return; |
155 |
|
} |
156 |
|
} |
157 |
|
else { |
158 |
0 |
line.setLineSpan( lineSpan ); |
159 |
0 |
if( isActiveThread() ) { |
160 |
0 |
page.setLine( (page.getLineCount() - 1), line ); |
161 |
0 |
if( _lastLine.getText().equals(line.getText()) ) { |
162 |
0 |
conflictWait = true; |
163 |
0 |
} |
164 |
|
} |
165 |
|
else { |
166 |
0 |
return; |
167 |
|
} |
168 |
|
} |
169 |
0 |
_lastLine = line; |
170 |
|
|
171 |
0 |
int lastLinePosition = 0; |
172 |
0 |
while( (lastLinePosition = lbm.getPosition()) < length ) { |
173 |
0 |
line = new Line(); |
174 |
0 |
line.setTextLayout( lbm.nextLayout(maxWidth) ); |
175 |
0 |
line.setLineSpan( lineSpan ); |
176 |
0 |
line.setText( text.substring(lastLinePosition, lbm.getPosition()) ); |
177 |
0 |
line.setForeground( page.getForeground() ); |
178 |
0 |
line.setShadowColor( page.getShadow() ); |
179 |
0 |
if( isActiveThread() ) { |
180 |
0 |
if( !page.isAdaptive(line) ) { |
181 |
0 |
if( conflictWait ) { |
182 |
0 |
conflictWait = false; |
183 |
0 |
} |
184 |
|
else { |
185 |
0 |
page.updateContents(); |
186 |
0 |
stop(); |
187 |
|
} |
188 |
0 |
page = advancesNewPage(); |
189 |
|
} |
190 |
0 |
if( page != null ) { |
191 |
0 |
page.addLine( line ); |
192 |
0 |
conflictWait = false; |
193 |
0 |
} |
194 |
|
else { |
195 |
0 |
return; |
196 |
|
} |
197 |
0 |
_lastLine = line; |
198 |
0 |
} |
199 |
|
else { |
200 |
0 |
return; |
201 |
|
} |
202 |
|
} |
203 |
0 |
if( isActiveThread() ) { |
204 |
0 |
page.updateContents(); |
205 |
|
} |
206 |
0 |
} |
207 |
|
|
208 |
|
|
209 |
|
|
210 |
|
|
211 |
|
|
212 |
|
public Page getCurrentPage() |
213 |
|
{ |
214 |
0 |
return getCanvasManager().getCurrentPage(); |
215 |
|
} |
216 |
|
|
217 |
|
|
218 |
|
|
219 |
|
|
220 |
|
|
221 |
|
public Page advancesNewPage() |
222 |
|
{ |
223 |
0 |
if( isActiveThread() ) { |
224 |
0 |
return getCanvasManager().advancesNewPage(); |
225 |
|
} |
226 |
|
else { |
227 |
0 |
return null; |
228 |
|
} |
229 |
|
} |
230 |
|
|
231 |
|
|
232 |
|
|
233 |
|
|
234 |
|
protected void stop() |
235 |
|
{ |
236 |
0 |
if( isActiveThread() ) { |
237 |
0 |
getActionControler().stop(); |
238 |
|
} |
239 |
0 |
} |
240 |
|
|
241 |
|
|
242 |
|
|
243 |
|
|
244 |
|
|
245 |
|
protected float getLineSpan() |
246 |
|
{ |
247 |
0 |
SceneContext context = getSceneContext(); |
248 |
0 |
String value = context.getProperty( PropertyIDs.CANVAS_TEXT_LINESPAN ); |
249 |
0 |
if( value != null ) { |
250 |
|
try { |
251 |
0 |
return Float.parseFloat( value ); |
252 |
|
} |
253 |
0 |
catch( Exception e ) { |
254 |
0 |
Logger.warn( MessageIDs.SCN2002W, e ); |
255 |
|
} |
256 |
|
} |
257 |
|
|
258 |
0 |
ResourceManager resources = ResourceManager.getInstance(); |
259 |
0 |
Float lineSpan = (Float)resources.getResource( ResourceIDs.CANVAS_TEXT_LINESPAN ); |
260 |
0 |
if( lineSpan != null ) { |
261 |
0 |
return lineSpan.floatValue(); |
262 |
|
} |
263 |
|
else { |
264 |
0 |
Logger.warn( MessageIDs.SCN2001W, new Object[]{"20.0"} ); |
265 |
0 |
return 20f; |
266 |
|
} |
267 |
|
} |
268 |
|
} |