OSDN Git Service

Changing the design of the event
[liveml/LiveML.git] / src / livemlrunner.h
1 /** * LiveML - LiveML is screen(graphic) controling library using XML.
2  *
3  * LGPL License
4  * Copyright (C) 2010 Nothan
5  * http://github.com/nothan/liveml/
6  * All rights reserved.
7  *
8  *  This library is free software; you can redistribute it and/or
9  *  modify it under the terms of the GNU Library General Public
10  *  License as published by the Free Software Foundation; either
11  *  version 2 of the License, or (at your option) any later version.
12  *
13  *  This library is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *  Library General Public License for more details.
17  *
18  *  You should have received a copy of the GNU Library General Public
19  *  License along with this library; if not, write to the Free
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  * Nothan
23  * private@nothan.xrea.jp
24  *
25  * Tsuioku Denrai
26  * http://tsuioku-denrai.xrea.jp/
27  */
28
29 #ifndef __LIVEMLRUNNER_H__
30 #define __LIVEMLRUNNER_H__
31
32 #include "livemlparser.h"
33 #include "list.hpp"
34 #include "bool_list.hpp"
35
36 #define LML_CMDFUNC(name) LML_CMDRTN name(LMLParameter &param)
37
38 typedef int LML_CMDRTN;
39 enum
40 {
41   LML_CMDRTN_NULL = 0x00,
42   LML_CMDRTN_STOP = 0x01,
43   LML_CMDRTN_NEXT = 0x02,
44   LML_CMDRTN_REACTION = 0x04,
45 };
46
47 class LiveMLRunner;
48
49 struct NumericVariable
50 {
51   variable_size id;
52   fixed_float value;
53 };
54
55 class LMLVariable
56 {
57   fixed_float* getNumeric(variable_size id)
58   {
59 #ifdef DEBUG
60       printf("LMLVariable::getNumeric(%d)\n", id);
61 #endif
62     id = VAR_LOCAL_ID(id);
63     NumericVariable *v = numericList.front();
64
65     while (v)
66     {
67       if (v->id == id)
68       {
69         numericList.chain_front(v);
70         break;
71       }
72       v = (NumericVariable*)((list_item<NumericVariable>*)v)->next;
73     }
74
75     if (v == NULL)
76     {
77 #ifdef DEBUG
78       printf("added variable %d\n", id);
79 #endif
80       v = numericList.push_front();
81     }
82
83     return &v->value;
84   }
85
86 public:
87   list<NumericVariable> numericList;
88
89   void clear(void)
90   {
91     numericList.clear(true);
92   }
93
94   void setFixedFloat(variable_size id, fixed_float v)
95   {
96 #ifdef DEBUG
97     printf("LMLVariable::setFixedFloat(%d) = %d\n", id, v);
98 #endif
99     *getNumeric(id) = v;
100   }
101
102   void setString(variable_size id, const char *v)
103   {
104   }
105
106   fixed_float getFixedFloat(variable_size id)
107   {
108 #ifdef DEBUG
109     printf("LMLVariable::getFixedFloat(%d) = %d\n", id, *getNumeric(id));
110 #endif
111     return *getNumeric(id);
112   }
113
114   const char* getString(variable_size id)
115   {
116     return "";
117   }
118 };
119
120 class ActiveAction
121 {
122 public:
123   ActionParser *action;
124   Tag *tag;
125   // local variable
126   LMLVariable variable;
127
128   void clear(void)
129   {
130     variable.clear();
131   }
132 };
133
134 template <class T>
135 class ActiveStack
136 {
137 public:
138   list<T> list;
139
140   T* current(void)
141   {
142     return list.back();
143   }
144
145   T* add(void)
146   {
147     return list.push_back();
148   }
149
150   void close()
151   {
152     T* aa = current();
153
154     aa->clear();
155     list.remove(aa);
156   }
157
158   void clear(void)
159   {
160     list.clear(true);
161   }
162
163   bool has(void)
164   {
165     return current() != NULL;
166   }
167 };
168
169 class ActiveActionStack : public ActiveStack<ActiveAction>
170 {
171 public:
172   unsigned int waitCounter;
173   ::list<unsigned int> repeatCounter;
174 };
175
176 class ActiveEventStack : public ActiveActionStack
177 {
178 };
179
180 struct LMLObject
181 {
182 public:
183   ActiveActionStack activeActionStack;
184   ActiveEventStack activeEventStack;
185   TagAct *parser;
186   // member variable
187   LMLVariable variable;
188
189   LMLObject(void)
190   {
191     init();
192   }
193
194   ~LMLObject()
195   {
196     release();
197   }
198
199   void init()
200   {
201     parser = NULL;
202     activeActionStack.waitCounter = 0;
203     activeEventStack.waitCounter = 0;
204   }
205
206   void release(void)
207   {
208     variable.clear();
209   }
210
211   void setRepeatAllocator(list_allocator<unsigned int> *alloc)
212   {
213     activeActionStack.repeatCounter.allocator(alloc);
214     activeEventStack.repeatCounter.allocator(alloc);
215   }
216
217   void setVariableAllocator(list_allocator<NumericVariable> *alloc)
218   {
219     variable.numericList.allocator(alloc);
220   }
221
222   bool setAction(ActionParser *action)
223   {
224 #ifdef DEBUG
225     printf("LMLObject::setAction(ActionParser*) begin.\n");
226 #endif
227     if (action == NULL) return false;
228
229     ActiveAction *aa = activeActionStack.add();
230     aa->action = action;
231     aa->variable.numericList.allocator(variable.numericList.get_allocator());
232     aa->tag = action->tag;
233 #ifdef DEBUG
234     printf("LMLObject::setAction(ActionParser*) end.\n");
235 #endif
236     return true;
237   }
238   bool setAction(action_type type)
239   {
240 #ifdef DEBUG
241     printf("LMLObject::setAction(%d) begin.\n", type);
242 #endif
243     ActionParser *action = parser->actionContainer[type];
244     if (action == NULL) return false;
245
246     setAction(action);
247 #ifdef DEBUG
248     printf("LMLObject::setAction(%d) end.\n", type);
249 #endif
250     return true;
251   }
252   bool setActionByName(const char *name)
253   {
254 #ifdef DEBUG
255     printf("LMLObject::setActionByName(%s)\n", name);
256 #endif
257     return setAction(parser->actionContainer[name]);
258   }
259
260   bool setEvent(EventParser *event)
261   {
262 #ifdef DEBUG
263     printf("LMLObject::setEvent(EventParser*) begin.\n");
264 #endif
265     if (event == NULL) return false;
266
267     activeEventStack.clear();
268     ActiveAction *aa = activeEventStack.add();
269     aa->action = (ActionParser*)event;
270     aa->variable.numericList.allocator(variable.numericList.get_allocator());
271     aa->tag = event->tag;
272 #ifdef DEBUG
273     printf("LMLObject::setEvent(EventParser*) end.\n");
274 #endif
275     return true;
276   }
277
278   bool setEvent(event_type type)
279   {
280 #ifdef DEBUG
281     printf("LMLObject::setEvent(%d) begin.\n", type);
282 #endif
283     EventParser *event = parser->eventContainer[type];
284     if (event == NULL) return false;
285
286 #ifdef DEBUG
287     printf("LMLObject::setEvent(%d) end.\n", type);
288 #endif
289     return setEvent(event);
290   }
291 };
292
293 struct LMLSceneObject : public LMLObject
294 {
295 };
296
297 struct LMLActorObject : public LMLObject
298 {
299 };
300
301 class LMLParameter
302 {
303   friend class LiveMLRunner;
304   Parameter *param;
305 public:
306   LiveMLRunner *runner;
307   LMLObject *obj;
308   ActiveActionStack *actionStack;
309
310   LMLParameter(Parameter *param, LiveMLRunner *runner, LMLObject *obj, ActiveActionStack *actionStack)
311   {
312     this->param = param;
313     this->runner = runner;
314     this->obj = obj;
315     this->actionStack = actionStack;
316   }
317
318   LMLVariable* getVariable(variable_size);
319   float getFloat(size_t);
320   int getInteger(size_t);
321   int getFixedFloat(size_t);
322   const char* getText(size_t);
323
324   bool has(size_t);
325   const void* get(size_t);
326   Tag* getTag(size_t);
327   const char* getString(size_t);
328 };
329
330 class LiveMLRunner
331 {
332   struct FuncPtr
333   {
334     LML_CMDRTN (*func)(LMLParameter&);
335     LML_CMDRTN (*closeFunc)(LMLParameter&);
336   };
337
338   LMLSceneObject _scene;
339   list<LMLActorObject> _actorList;
340   LMLActorObject *_currentActor;
341   LMLObject *_currentObject;
342   vector<FuncPtr> _commandFuncs;
343
344   list_allocator<ActiveAction> _actionAllocator;
345   list_allocator<unsigned int> _repeatAllocator;
346   list_allocator<NumericVariable> _numericVariableAllocator;
347
348 public:
349   LiveMLParser *parser;
350   // global variable
351   LMLVariable variable;
352
353   LiveMLRunner(LiveMLParser*);
354   ~LiveMLRunner();
355
356   bool registerCommand(
357     const char*,
358     LML_CMDRTN (*)(LMLParameter&),
359     LML_CMDRTN (*)(LMLParameter&) = NULL
360   );
361
362   void setMaxActors(size_t);
363   LMLSceneObject* setScene(const char*);
364   LMLActorObject* addActor(const char*);
365
366   void setCurrentActor(LMLActorObject *obj) { _currentActor = obj; }
367   LMLActorObject* getCurrentActor(void) { return _currentActor; }
368
369   void setCurrentObject(LMLObject *obj) { _currentObject = obj; }
370   LMLObject* getCurrentObject(void) { return _currentObject; }
371
372   LML_CMDRTN runCommand(LMLParameter&, Tag*);
373   LML_CMDRTN runAction(LMLParameter&);
374   void callEvent(LMLObject&, event_type);
375   void callEvent(event_type);
376   bool runActiveActions(LMLObject&);
377   bool runObject(LMLObject&);
378   bool run(void);
379
380   static fixed_float variableNumericDecoder(variable_size, void*);
381   static const char* variableStringDecoder(variable_size, void*);
382 };
383
384 #endif // __LIVEMLRUNNER_H__