1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package tsukuba_bunko.peko.scenario.text;
20
21 import java.awt.Color;
22 import java.awt.Dimension;
23 import java.awt.Font;
24 import java.awt.Insets;
25 import java.awt.Point;
26
27 import java.awt.font.TextAttribute;
28
29 import java.util.Map;
30
31 import tsukuba_bunko.peko.Logger;
32
33 import tsukuba_bunko.peko.canvas.text.Page;
34
35 import tsukuba_bunko.peko.resource.ColorManager;
36 import tsukuba_bunko.peko.resource.DimensionDeserializer;
37 import tsukuba_bunko.peko.resource.FontManager;
38 import tsukuba_bunko.peko.resource.InsetsDeserializer;
39 import tsukuba_bunko.peko.resource.ResourceManager;
40 import tsukuba_bunko.peko.resource.PointDeserializer;
41
42 import tsukuba_bunko.peko.scenario.SceneContext;
43
44
45 /***
46 * {@link tsukuba_bunko.peko.canvas.text.Page} をシーンの設定を元にカスタマイズする機能を提供します。
47 * @author $Author: ppoi $
48 * @version $Revision: 1.2 $
49 */
50 public class PageConfigurator {
51
52 /***
53 * <code>PageConfigurator</code> のインスタンスを作成するために getInstance から呼ばれます。
54 */
55 protected PageConfigurator()
56 {
57 super();
58 }
59
60
61 /***
62 * <code>page</code> にシーンの設定を反映させます。
63 * @param page ページ
64 * @param context シーンコンテキスト
65 */
66 public static void configure( Page page, SceneContext context )
67 {
68 ResourceManager resources = ResourceManager.getInstance();
69 FontManager fonts = FontManager.getInstance();
70 ColorManager colors = ColorManager.getInstance();
71
72 boolean set = false;
73
74 String value = context.getProperty( PropertyIDs.CANVAS_TEXT_BACKGROUND_TRANSPARENCY );
75 if( value != null ) {
76 try {
77 page.setTransparency( Float.parseFloat(value) );
78 set = true;
79 }
80 catch( Exception e ) {
81 Logger.warn( MessageIDs.SCN2006W );
82 }
83 }
84 if( !set ) {
85 Float trans = (Float)resources.getResource( ResourceIDs.CANVAS_TEXT_BACKGROUND_TRANSPARENCY );
86 if( trans == null ) {
87 Logger.warn( MessageIDs.SCN2005W, new Object[]{"0.5"} );
88 page.setTransparency( 0.5f );
89 }
90 else {
91 page.setTransparency( trans.floatValue() );
92 }
93 }
94
95 set = false;
96 value = context.getProperty( PropertyIDs.CANVAS_TEXT_BACKGROUND_COLOR );
97 if( value == null ) {
98 Color c = (Color)resources.getResource( ResourceIDs.CANVAS_TEXT_BACKGROUND_COLOR );
99 if( c == null ) {
100 Logger.warn( MessageIDs.SCN2003W, new Object[]{"black"} );
101 c = Color.black;
102 }
103 page.setBackground( c );
104 }
105 else {
106 page.setBackground( colors.getColor(value) );
107 }
108
109 String type = context.getProperty( PropertyIDs.CANVAS_TEXT_VIEWTYPE );
110 if( type != null ) {
111 if( !"window".equals(type) && !"full".equals(type) ) {
112 Logger.warn( MessageIDs.SCN2014W );
113 type = null;
114 }
115 }
116 if( type == null ) {
117 type = (String)resources.getResource( ResourceIDs.CANVAS_TEXT_VIEWTYPE );
118 if( (type == null) || !("window".equals(type) || "full".equals(type)) ) {
119 Logger.warn( MessageIDs.SCN2013W, new Object[]{"full"} );
120 type = "full";
121 }
122 }
123
124 Dimension viewSize = null;
125 if( type.equals("window") ) {
126 value = context.getProperty( PropertyIDs.CANVAS_TEXT_SIZE );
127 if( value != null ) {
128 try {
129 viewSize = DimensionDeserializer.parseDimension( value );
130 }
131 catch( Exception e ) {
132 Logger.warn( MessageIDs.SCN2016W );
133 }
134 }
135 if( viewSize == null ) {
136 viewSize = (Dimension)resources.getResource( ResourceIDs.CANVAS_TEXT_SIZE );
137 if( viewSize == null ) {
138 Logger.warn( MessageIDs.SCN2015W, new Object[]{"600, 180"} );
139 viewSize = new Dimension( 600, 180 );
140 }
141 }
142
143 Point location = null;
144 value = context.getProperty( PropertyIDs.CANVAS_TEXT_LOCATION );
145 if( value != null ) {
146 try {
147 location = PointDeserializer.parsePoint( value );
148 }
149 catch( Exception e ) {
150 Logger.warn( MessageIDs.SCN2018W, e );
151 }
152 }
153 if( location == null ) {
154 location = (Point)resources.getResource( ResourceIDs.CANVAS_TEXT_LOCATION );
155 if( location == null ) {
156 Logger.warn( MessageIDs.SCN2017W, new Object[]{"0, 0"} );
157 location = new Point( 0, 0 );
158 }
159 }
160
161 page.setLocation( location.x, location.y );
162
163 }
164 else {
165 page.setLocation( 0, 0 );
166 viewSize = page.getTextCanvas().getSize();
167 }
168 page.setSize( viewSize );
169
170 Insets padding = null;
171 value = context.getProperty( PropertyIDs.CANVAS_TEXT_PADDING );
172 if( value != null ) {
173 try {
174 padding = InsetsDeserializer.parseInsets( value );
175 }
176 catch( Exception e ) {
177 Logger.warn( MessageIDs.SCN2020W );
178 }
179 }
180 if( padding == null ) {
181 padding = (Insets)resources.getResource( ResourceIDs.CANVAS_TEXT_PADDING );
182 if( padding == null ) {
183 Logger.warn( MessageIDs.SCN2019W, new Object[]{"10,40,10,10"} );
184 padding = new Insets( 10, 40, 10, 10 );
185 }
186 }
187 page.setPadding( padding.top, padding.left, padding.bottom, padding.right );
188
189
190 Map fontAttrs = new java.util.HashMap( 17 );
191 Font baseFont = (Font)resources.getResource( ResourceIDs.CANVAS_TEXT_FONT );
192 if( baseFont == null ) {
193 Logger.warn( MessageIDs.SCN2011W, new Object[]{"Serif, 20"} );
194 fontAttrs = new java.util.HashMap( 17 );
195 fontAttrs.put( TextAttribute.FAMILY, "Serif" );
196 fontAttrs.put( TextAttribute.SIZE, new Float(20f) );
197 fontAttrs.put( TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR );
198 fontAttrs.put( TextAttribute.WEIGHT, TextAttribute.WEIGHT_REGULAR );
199 }
200 else {
201 fontAttrs.putAll( baseFont.getAttributes() );
202 }
203
204 value = context.getProperty( PropertyIDs.CANVAS_TEXT_FONT_FAMILY );
205 if( value != null ) {
206 fontAttrs.put( TextAttribute.FAMILY, value );
207 }
208
209 value = context.getProperty( PropertyIDs.CANVAS_TEXT_FONT_SIZE );
210 if( value != null ) {
211 try {
212 fontAttrs.put( TextAttribute.SIZE, Float.valueOf(value) );
213 }
214 catch( Exception e ) {
215 Logger.warn( MessageIDs.SCN2012W, new Object[]{"size"} );
216 }
217 }
218
219 value = context.getProperty( PropertyIDs.CANVAS_TEXT_FONT_STYLE );
220 if( value != null ) {
221 if( "italic".equals(value) ) {
222 fontAttrs.put( TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE );
223 }
224 else if( "normal".equals(value) ) {
225 fontAttrs.put( TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR );
226 }
227 else {
228 Logger.warn( MessageIDs.SCN2012W, new Object[]{"style"} );
229 }
230 }
231
232 value = context.getProperty( PropertyIDs.CANVAS_TEXT_FONT_WEIGHT );
233 if( value != null ) {
234 if( "bold".equals(value) ) {
235 fontAttrs.put( TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD );
236 }
237 else if( "normal".equals(value) ) {
238 fontAttrs.put( TextAttribute.WEIGHT, TextAttribute.WEIGHT_REGULAR );
239 }
240 }
241
242 page.setDefaultFont( fonts.getFont(fontAttrs) );
243
244
245 value = context.getProperty( PropertyIDs.CANVAS_TEXT_FOREGROUND_COLOR );
246 if( value == null ) {
247 Color c = (Color)resources.getResource( ResourceIDs.CANVAS_TEXT_FOREGROUND_COLOR );
248 if( c == null ) {
249 Logger.warn( MessageIDs.SCN2007W, new Object[]{"white"} );
250 c = Color.white;
251 }
252 page.setForeground( c );
253 }
254 else {
255 page.setForeground( colors.getColor(value) );
256 }
257
258 value = context.getProperty( PropertyIDs.CANVAS_TEXT_FOREGROUND_SHADOW );
259 if( value == null ) {
260 Color c = (Color)resources.getResource( ResourceIDs.CANVAS_TEXT_FOREGROUND_SHADOW );
261 if( c == null ) {
262 Logger.warn( MessageIDs.SCN2007W, new Object[]{"black"} );
263 c = Color.black;
264 }
265 page.setShadow( c );
266 }
267 else {
268 page.setShadow( colors.getColor(value) );
269 }
270
271 page.updateCanvas();
272 }
273 }