1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef COMMON_VIRTUAL_KEYBOARD_PARSER_H
24 #define COMMON_VIRTUAL_KEYBOARD_PARSER_H
25 
26 #include "common/scummsys.h"
27 
28 #ifdef ENABLE_VKEYBD
29 
30 #include "common/xmlparser.h"
31 #include "backends/vkeybd/virtual-keyboard.h"
32 
33 /**
34 
35                    ***************************************
36                    ** Virtual Keyboard Pack File Format **
37                    ***************************************
38 
39 The new virtual keyboard for ScummVM is implemented in the same way as a HTML
40 ImageMap. It uses a single bitmap of the entire keyboard layout and then a
41 image map description allows certain areas of the bitmap to be given special
42 actions. Most of these actions will be a virtual key press event, but there
43 will also be special keys that will change the keyboard layout or close the
44 keyboard. The HTML image map description is contained in a larger XML file that
45 can describe all the different modes of the keyboard, and also different
46 keyboard layouts for different screen resolutions.
47 
48                 ********************************************
49                 ** Example keyboard pack description file **
50                 ********************************************
51 
52 <keyboard modes="normal,caps" initial_mode="normal" v_align="bottom" h_align="center">
53   <mode name="normal" resolutions="640x400,320x200">
54     <layout resolution="640x400" bitmap="normal_640x400.bmp" transparent_color="255,0,255">
55       <map>
56         <area shape="poly" coords="65,50,67,48,94,48,96,50,96,77,94,79,67,79,65,77" target="q" />
57         <area shape="poly" coords="105,50,107,48,134,48,136,50,136,77,134,79,107,79,105,77" target="w" />
58         <area shape="poly" coords="146,50,148,48,174,48,176,50,176,77,174,79,148,79,146,77" target="e" />
59         ...
60         <area shape="poly" coords="11,89,12,88,69,88,70,89,70,116,69,117,12,117,11,116" target="caps" />
61       </map>
62     </layout>
63     <layout resolution="320x200" bitmap="normal_320x200.bmp" transparent_color="255,0,255">
64       ...
65     </layout>
66     <event name="a" type="key" code="97" ascii="97" modifiers="" />
67     <event name="b" type="key" code="98" ascii="98" modifiers="" />
68     <event name="c" type="key" code="99" ascii="99" modifiers="" />
69     ...
70     <event name="caps" type="switch_mode" mode="caps" />
71   </mode>
72 
73   <mode name="caps" resolutions="640x400">
74     <layout resolution="640x400" bitmap="caps_640x480.bmp" transparent_color="255,0,255">
75       <map>
76         <area shape="poly" coords="65,50,67,48,94,48,96,50,96,77,94,79,67,79,65,77" target="Q" />
77         ...
78       </map>
79     </layout>
80     <event name="A" type="key" code="97" ascii="65" modifiers="shift" />
81     <event name="B" type="key" code="98" ascii="66" modifiers="shift" />
82     <event name="C" type="key" code="99" ascii="67" modifiers="shift" />
83     ...
84   </mode>
85 </keyboard>
86 
87 *************************
88 ** Description of tags **
89 *************************
90 
91 <keyboard>
92 
93 This is the required, root element of the file format.
94 
95 required attributes:
96  - initial_mode: name of the mode the keyboard will show initially
97 
98 optional attributes:
99  - v_align/h_align: where on the screen should the keyboard appear initially
100    (defaults to bottom/center).
101 
102 child tags:
103  - mode
104 
105 -------------------------------------------------------------------------------
106 
107 <mode>
108 
109 This tag encapsulates a single mode of the keyboard. Within are a number of
110 layouts, which provide the specific implementation at different resolutions.
111 
112 required attributes:
113  - name: the name of the mode
114  - resolutions: list of the different layout resolutions
115 
116 child tags:
117  - layout
118  - event
119 
120 -------------------------------------------------------------------------------
121 
122 <event>
123 
124 These tags describe a particular event that will be triggered by a mouse click
125 on a particular area. The target attribute of each image map area should be the
126 same as an event's name.
127 
128 required attributes:
129  - name: name of the event
130  - type: key | modifier | switch_mode | submit | cancel | clear | delete |
131    move_left | move_right - see VirtualKeyboard::EventType for explanation
132 for key events
133  - code / ascii: describe a key press in ScummVM KeyState format
134 for key and modifier events
135  - modifiers: modifier keystate as comma-separated list of shift, ctrl and/or
136    alt.
137 for switch_mode events
138  - mode: name of the mode that should be switched to
139 -------------------------------------------------------------------------------
140 
141 <layout>
142 
143 These tags encapsulate an implementation of a mode at a particular resolution.
144 
145 required attributes:
146  - resolution: the screen resolution that this layout is designed for
147  - bitmap: filename of the 24-bit bitmap that will be used for this layout
148 
149 optional attributes:
150  - transparent_color: color in r,g,b format that will be used for keycolor
151    transparency (defaults to (255,0,255).
152  - display_font_color: color in r,g,b format that will be used for the text of
153    the keyboard display (defaults to (0,0,0).
154 
155 child nodes:
156  - map: this describes the image map using the same format as html image maps
157 
158 -------------------------------------------------------------------------------
159 
160 <map>
161 
162 These tags describe the image map for a particular layout. It uses the same
163 format as HTML image maps. The only area shapes that are supported are
164 rectangles and polygons. The target attribute of each area should be the name
165 of an event for this mode (see <event> tag). They will usually be generated by
166 an external tool such as GIMP's Image Map plugin, and so will not be written
167 by hand, but for more information on HTML image map format see
168  - http://www.w3schools.com/TAGS/tag_map.asp
169  - http://www.w3schools.com/TAGS/tag_area.asp
170 
171 */
172 
173 namespace Common {
174 
175 /**
176  * Subclass of Common::XMLParser that parses the virtual keyboard pack
177  * description file
178  */
179 class VirtualKeyboardParser : public XMLParser {
180 
181 public:
182 
183 	/**
184 	 * Enum dictating how extensive a parse will be
185 	 */
186 	enum ParseMode {
187 		/**
188 		 * Full parse - when loading keyboard pack for first time
189 		 */
190 		kParseFull,
191 
192 		/**
193 		 * Just check resolutions and reload layouts if needed - following a
194 		 * change in screen size
195 		 */
196 		kParseCheckResolutions
197 	};
198 
199 	VirtualKeyboardParser(VirtualKeyboard *kbd);
setParseMode(ParseMode m)200 	void setParseMode(ParseMode m) {
201 		_parseMode = m;
202 	}
203 
204 protected:
CUSTOM_XML_PARSER(VirtualKeyboardParser)205 	CUSTOM_XML_PARSER(VirtualKeyboardParser) {
206 		XML_KEY(keyboard)
207 			XML_PROP(initial_mode, true)
208 			XML_PROP(v_align, false)
209 			XML_PROP(h_align, false)
210 			XML_KEY(mode)
211 				XML_PROP(name, true)
212 				XML_PROP(resolutions, true)
213 				XML_KEY(layout)
214 					XML_PROP(resolution, true)
215 					XML_PROP(bitmap, true)
216 					XML_PROP(transparent_color, false)
217 					XML_PROP(display_font_color, false)
218 					XML_KEY(map)
219 						XML_KEY(area)
220 							XML_PROP(shape, true)
221 							XML_PROP(coords, true)
222 							XML_PROP(target, true)
223 						KEY_END()
224 					KEY_END()
225 				KEY_END()
226 				XML_KEY(event)
227 					XML_PROP(name, true)
228 					XML_PROP(type, true)
229 					XML_PROP(code, false)
230 					XML_PROP(ascii, false)
231 					XML_PROP(modifiers, false)
232 					XML_PROP(mode, false)
233 				KEY_END()
234 			KEY_END()
235 		KEY_END()
236 	} PARSER_END()
237 
238 protected:
239 	VirtualKeyboard *_keyboard;
240 
241 	/** internal state variables of parser */
242 	ParseMode _parseMode;
243 	VirtualKeyboard::Mode *_mode;
244 	String _initialModeName;
245 	bool _kbdParsed;
246 	bool _layoutParsed;
247 
248 	/** Cleanup internal state before parse */
249 	virtual void cleanup();
250 
251 	/** Parser callback function */
252 	bool parserCallback_keyboard(ParserNode *node);
253 	bool parserCallback_mode(ParserNode *node);
254 	bool parserCallback_event(ParserNode *node);
255 	bool parserCallback_layout(ParserNode *node);
256 	bool parserCallback_map(ParserNode *node);
257 	bool parserCallback_area(ParserNode *node);
258 	virtual bool closedKeyCallback(ParserNode *node);
259 
260 	/** Parse helper functions */
261 	byte parseFlags(const String &flags);
262 	bool parseRect(Rect &rect, const String &coords);
263 	bool parsePolygon(Polygon &poly, const String &coords);
264 	bool parseRectAsPolygon(Polygon &poly, const String &coords);
265 };
266 
267 } // End of namespace GUI
268 
269 #endif // #ifdef ENABLE_VKEYBD
270 
271 #endif // #ifndef COMMON_VIRTUAL_KEYBOARD_PARSER_H
272