• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

milligram


Commit MetaInfo

Revisão23811455b2b715fe64e9f8981c4550f0171c9bbf (tree)
Hora2011-03-25 17:04:20
Autorberu <berupon@gmai...>
Commiterberu

Mensagem de Log

basic event system implemented

Mudança Sumário

Diff

--- a/common/callback.hpp
+++ /dev/null
@@ -1,1038 +0,0 @@
1-#ifndef UTIL_CALLBACK_HPP
2-#define UTIL_CALLBACK_HPP
3-
4-#define UTIL_GET_CALLBACK_FACTORY_BIND_FREE(freeFuncPtr) \
5- (util::GetCallbackFactory(freeFuncPtr).Bind<freeFuncPtr>())
6-#define BIND_FREE_CB UTIL_GET_CALLBACK_FACTORY_BIND_FREE
7-
8-#define UTIL_GET_CALLBACK_FACTORY_BIND_MEMBER(memFuncPtr, instancePtr) \
9- (util::GetCallbackFactory(memFuncPtr).Bind<memFuncPtr>(instancePtr))
10-#define BIND_MEM_CB UTIL_GET_CALLBACK_FACTORY_BIND_MEMBER
11-
12-namespace util {
13-
14-template<typename FuncSignature>
15-class Callback;
16-
17-struct NullCallback {};
18-
19-// 0 parameter version
20-
21-template<typename R>
22-class Callback<R ()>
23-{
24-public:
25- static const int Arity = 0;
26- typedef R ReturnType;
27-
28- Callback() : func(0), obj(0) {}
29- Callback(NullCallback) : func(0), obj(0) {}
30- Callback(const Callback& rhs) : func(rhs.func), obj(rhs.obj) {}
31- ~Callback() {}
32-
33- Callback& operator=(NullCallback)
34- { obj = 0; func = 0; return *this; }
35- Callback& operator=(const Callback& rhs)
36- { obj = rhs.obj; func = rhs.func; return *this; }
37-
38- inline R operator()() const
39- {
40- return (*func)(obj);
41- }
42-
43-private:
44- typedef const void* Callback::*SafeBoolType;
45-public:
46- inline operator SafeBoolType() const
47- { return func != 0 ? &Callback::obj : 0; }
48- inline bool operator!() const
49- { return func == 0; }
50-
51-private:
52- typedef R (*FuncType)(const void*);
53- Callback(FuncType f, const void* o) : func(f), obj(o) {}
54-
55-private:
56- FuncType func;
57- const void* obj;
58-
59- template<typename FR>
60- friend class FreeCallbackFactory0;
61- template<typename FR, class FT>
62- friend class MemberCallbackFactory0;
63- template<typename FR, class FT>
64- friend class ConstMemberCallbackFactory0;
65-};
66-
67-template<typename R>
68-void operator==(const Callback<R ()>&,
69- const Callback<R ()>&);
70-template<typename R>
71-void operator!=(const Callback<R ()>&,
72- const Callback<R ()>&);
73-
74-template<typename R>
75-class FreeCallbackFactory0
76-{
77-private:
78- template<R (*Func)()>
79- static R Wrapper(const void*)
80- {
81- return (*Func)();
82- }
83-
84-public:
85- template<R (*Func)()>
86- inline static Callback<R ()> Bind()
87- {
88- return Callback<R ()>
89- (&FreeCallbackFactory0::Wrapper<Func>, 0);
90- }
91-};
92-
93-template<typename R>
94-inline FreeCallbackFactory0<R>
95-GetCallbackFactory(R (*)())
96-{
97- return FreeCallbackFactory0<R>();
98-}
99-
100-template<typename R, class T>
101-class MemberCallbackFactory0
102-{
103-private:
104- template<R (T::*Func)()>
105- static R Wrapper(const void* o)
106- {
107- T* obj = const_cast<T*>(static_cast<const T*>(o));
108- return (obj->*Func)();
109- }
110-
111-public:
112- template<R (T::*Func)()>
113- inline static Callback<R ()> Bind(T* o)
114- {
115- return Callback<R ()>
116- (&MemberCallbackFactory0::Wrapper<Func>,
117- static_cast<const void*>(o));
118- }
119-};
120-
121-template<typename R, class T>
122-inline MemberCallbackFactory0<R, T>
123-GetCallbackFactory(R (T::*)())
124-{
125- return MemberCallbackFactory0<R, T>();
126-}
127-
128-template<typename R, class T>
129-class ConstMemberCallbackFactory0
130-{
131-private:
132- template<R (T::*Func)() const>
133- static R Wrapper(const void* o)
134- {
135- const T* obj = static_cast<const T*>(o);
136- return (obj->*Func)();
137- }
138-
139-public:
140- template<R (T::*Func)() const>
141- inline static Callback<R ()> Bind(const T* o)
142- {
143- return Callback<R ()>
144- (&ConstMemberCallbackFactory0::Wrapper<Func>,
145- static_cast<const void*>(o));
146- }
147-};
148-
149-template<typename R, class T>
150-inline ConstMemberCallbackFactory0<R, T>
151-GetCallbackFactory(R (T::*)() const)
152-{
153- return ConstMemberCallbackFactory0<R, T>();
154-}
155-
156-// 1 parameter version
157-
158-template<typename R, typename P1>
159-class Callback<R (P1)>
160-{
161-public:
162- static const int Arity = 1;
163- typedef R ReturnType;
164- typedef P1 Param1Type;
165-
166- Callback() : func(0), obj(0) {}
167- Callback(NullCallback) : func(0), obj(0) {}
168- Callback(const Callback& rhs) : func(rhs.func), obj(rhs.obj) {}
169- ~Callback() {}
170-
171- Callback& operator=(NullCallback)
172- { obj = 0; func = 0; return *this; }
173- Callback& operator=(const Callback& rhs)
174- { obj = rhs.obj; func = rhs.func; return *this; }
175-
176- inline R operator()(P1 a1) const
177- {
178- return (*func)(obj, a1);
179- }
180-
181-private:
182- typedef const void* Callback::*SafeBoolType;
183-public:
184- inline operator SafeBoolType() const
185- { return func != 0 ? &Callback::obj : 0; }
186- inline bool operator!() const
187- { return func == 0; }
188-
189-private:
190- typedef R (*FuncType)(const void*, P1);
191- Callback(FuncType f, const void* o) : func(f), obj(o) {}
192-
193-private:
194- FuncType func;
195- const void* obj;
196-
197- template<typename FR, typename FP1>
198- friend class FreeCallbackFactory1;
199- template<typename FR, class FT, typename FP1>
200- friend class MemberCallbackFactory1;
201- template<typename FR, class FT, typename FP1>
202- friend class ConstMemberCallbackFactory1;
203-};
204-
205-template<typename R, typename P1>
206-void operator==(const Callback<R (P1)>&,
207- const Callback<R (P1)>&);
208-template<typename R, typename P1>
209-void operator!=(const Callback<R (P1)>&,
210- const Callback<R (P1)>&);
211-
212-template<typename R, typename P1>
213-class FreeCallbackFactory1
214-{
215-private:
216- template<R (*Func)(P1)>
217- static R Wrapper(const void*, P1 a1)
218- {
219- return (*Func)(a1);
220- }
221-
222-public:
223- template<R (*Func)(P1)>
224- inline static Callback<R (P1)> Bind()
225- {
226- return Callback<R (P1)>
227- (&FreeCallbackFactory1::Wrapper<Func>, 0);
228- }
229-};
230-
231-template<typename R, typename P1>
232-inline FreeCallbackFactory1<R, P1>
233-GetCallbackFactory(R (*)(P1))
234-{
235- return FreeCallbackFactory1<R, P1>();
236-}
237-
238-template<typename R, class T, typename P1>
239-class MemberCallbackFactory1
240-{
241-private:
242- template<R (T::*Func)(P1)>
243- static R Wrapper(const void* o, P1 a1)
244- {
245- T* obj = const_cast<T*>(static_cast<const T*>(o));
246- return (obj->*Func)(a1);
247- }
248-
249-public:
250- template<R (T::*Func)(P1)>
251- inline static Callback<R (P1)> Bind(T* o)
252- {
253- return Callback<R (P1)>
254- (&MemberCallbackFactory1::Wrapper<Func>,
255- static_cast<const void*>(o));
256- }
257-};
258-
259-template<typename R, class T, typename P1>
260-inline MemberCallbackFactory1<R, T, P1>
261-GetCallbackFactory(R (T::*)(P1))
262-{
263- return MemberCallbackFactory1<R, T, P1>();
264-}
265-
266-template<typename R, class T, typename P1>
267-class ConstMemberCallbackFactory1
268-{
269-private:
270- template<R (T::*Func)(P1) const>
271- static R Wrapper(const void* o, P1 a1)
272- {
273- const T* obj = static_cast<const T*>(o);
274- return (obj->*Func)(a1);
275- }
276-
277-public:
278- template<R (T::*Func)(P1) const>
279- inline static Callback<R (P1)> Bind(const T* o)
280- {
281- return Callback<R (P1)>
282- (&ConstMemberCallbackFactory1::Wrapper<Func>,
283- static_cast<const void*>(o));
284- }
285-};
286-
287-template<typename R, class T, typename P1>
288-inline ConstMemberCallbackFactory1<R, T, P1>
289-GetCallbackFactory(R (T::*)(P1) const)
290-{
291- return ConstMemberCallbackFactory1<R, T, P1>();
292-}
293-
294-// 2 parameter version
295-
296-template<typename R, typename P1, typename P2>
297-class Callback<R (P1, P2)>
298-{
299-public:
300- static const int Arity = 2;
301- typedef R ReturnType;
302- typedef P1 Param1Type;
303- typedef P2 Param2Type;
304-
305- Callback() : func(0), obj(0) {}
306- Callback(NullCallback) : func(0), obj(0) {}
307- Callback(const Callback& rhs) : func(rhs.func), obj(rhs.obj) {}
308- ~Callback() {}
309-
310- Callback& operator=(NullCallback)
311- { obj = 0; func = 0; return *this; }
312- Callback& operator=(const Callback& rhs)
313- { obj = rhs.obj; func = rhs.func; return *this; }
314-
315- inline R operator()(P1 a1, P2 a2) const
316- {
317- return (*func)(obj, a1, a2);
318- }
319-
320-private:
321- typedef const void* Callback::*SafeBoolType;
322-public:
323- inline operator SafeBoolType() const
324- { return func != 0 ? &Callback::obj : 0; }
325- inline bool operator!() const
326- { return func == 0; }
327-
328-private:
329- typedef R (*FuncType)(const void*, P1, P2);
330- Callback(FuncType f, const void* o) : func(f), obj(o) {}
331-
332-private:
333- FuncType func;
334- const void* obj;
335-
336- template<typename FR, typename FP1, typename FP2>
337- friend class FreeCallbackFactory2;
338- template<typename FR, class FT, typename FP1, typename FP2>
339- friend class MemberCallbackFactory2;
340- template<typename FR, class FT, typename FP1, typename FP2>
341- friend class ConstMemberCallbackFactory2;
342-};
343-
344-template<typename R, typename P1, typename P2>
345-void operator==(const Callback<R (P1, P2)>&,
346- const Callback<R (P1, P2)>&);
347-template<typename R, typename P1, typename P2>
348-void operator!=(const Callback<R (P1, P2)>&,
349- const Callback<R (P1, P2)>&);
350-
351-template<typename R, typename P1, typename P2>
352-class FreeCallbackFactory2
353-{
354-private:
355- template<R (*Func)(P1, P2)>
356- static R Wrapper(const void*, P1 a1, P2 a2)
357- {
358- return (*Func)(a1, a2);
359- }
360-
361-public:
362- template<R (*Func)(P1, P2)>
363- inline static Callback<R (P1, P2)> Bind()
364- {
365- return Callback<R (P1, P2)>
366- (&FreeCallbackFactory2::Wrapper<Func>, 0);
367- }
368-};
369-
370-template<typename R, typename P1, typename P2>
371-inline FreeCallbackFactory2<R, P1, P2>
372-GetCallbackFactory(R (*)(P1, P2))
373-{
374- return FreeCallbackFactory2<R, P1, P2>();
375-}
376-
377-template<typename R, class T, typename P1, typename P2>
378-class MemberCallbackFactory2
379-{
380-private:
381- template<R (T::*Func)(P1, P2)>
382- static R Wrapper(const void* o, P1 a1, P2 a2)
383- {
384- T* obj = const_cast<T*>(static_cast<const T*>(o));
385- return (obj->*Func)(a1, a2);
386- }
387-
388-public:
389- template<R (T::*Func)(P1, P2)>
390- inline static Callback<R (P1, P2)> Bind(T* o)
391- {
392- return Callback<R (P1, P2)>
393- (&MemberCallbackFactory2::Wrapper<Func>,
394- static_cast<const void*>(o));
395- }
396-};
397-
398-template<typename R, class T, typename P1, typename P2>
399-inline MemberCallbackFactory2<R, T, P1, P2>
400-GetCallbackFactory(R (T::*)(P1, P2))
401-{
402- return MemberCallbackFactory2<R, T, P1, P2>();
403-}
404-
405-template<typename R, class T, typename P1, typename P2>
406-class ConstMemberCallbackFactory2
407-{
408-private:
409- template<R (T::*Func)(P1, P2) const>
410- static R Wrapper(const void* o, P1 a1, P2 a2)
411- {
412- const T* obj = static_cast<const T*>(o);
413- return (obj->*Func)(a1, a2);
414- }
415-
416-public:
417- template<R (T::*Func)(P1, P2) const>
418- inline static Callback<R (P1, P2)> Bind(const T* o)
419- {
420- return Callback<R (P1, P2)>
421- (&ConstMemberCallbackFactory2::Wrapper<Func>,
422- static_cast<const void*>(o));
423- }
424-};
425-
426-template<typename R, class T, typename P1, typename P2>
427-inline ConstMemberCallbackFactory2<R, T, P1, P2>
428-GetCallbackFactory(R (T::*)(P1, P2) const)
429-{
430- return ConstMemberCallbackFactory2<R, T, P1, P2>();
431-}
432-
433-// 3 parameter version
434-
435-template<typename R, typename P1, typename P2, typename P3>
436-class Callback<R (P1, P2, P3)>
437-{
438-public:
439- static const int Arity = 3;
440- typedef R ReturnType;
441- typedef P1 Param1Type;
442- typedef P2 Param2Type;
443- typedef P3 Param3Type;
444-
445- Callback() : func(0), obj(0) {}
446- Callback(NullCallback) : func(0), obj(0) {}
447- Callback(const Callback& rhs) : func(rhs.func), obj(rhs.obj) {}
448- ~Callback() {}
449-
450- Callback& operator=(NullCallback)
451- { obj = 0; func = 0; return *this; }
452- Callback& operator=(const Callback& rhs)
453- { obj = rhs.obj; func = rhs.func; return *this; }
454-
455- inline R operator()(P1 a1, P2 a2, P3 a3) const
456- {
457- return (*func)(obj, a1, a2, a3);
458- }
459-
460-private:
461- typedef const void* Callback::*SafeBoolType;
462-public:
463- inline operator SafeBoolType() const
464- { return func != 0 ? &Callback::obj : 0; }
465- inline bool operator!() const
466- { return func == 0; }
467-
468-private:
469- typedef R (*FuncType)(const void*, P1, P2, P3);
470- Callback(FuncType f, const void* o) : func(f), obj(o) {}
471-
472-private:
473- FuncType func;
474- const void* obj;
475-
476- template<typename FR, typename FP1, typename FP2, typename FP3>
477- friend class FreeCallbackFactory3;
478- template<typename FR, class FT, typename FP1, typename FP2, typename FP3>
479- friend class MemberCallbackFactory3;
480- template<typename FR, class FT, typename FP1, typename FP2, typename FP3>
481- friend class ConstMemberCallbackFactory3;
482-};
483-
484-template<typename R, typename P1, typename P2, typename P3>
485-void operator==(const Callback<R (P1, P2, P3)>&,
486- const Callback<R (P1, P2, P3)>&);
487-template<typename R, typename P1, typename P2, typename P3>
488-void operator!=(const Callback<R (P1, P2, P3)>&,
489- const Callback<R (P1, P2, P3)>&);
490-
491-template<typename R, typename P1, typename P2, typename P3>
492-class FreeCallbackFactory3
493-{
494-private:
495- template<R (*Func)(P1, P2, P3)>
496- static R Wrapper(const void*, P1 a1, P2 a2, P3 a3)
497- {
498- return (*Func)(a1, a2, a3);
499- }
500-
501-public:
502- template<R (*Func)(P1, P2, P3)>
503- inline static Callback<R (P1, P2, P3)> Bind()
504- {
505- return Callback<R (P1, P2, P3)>
506- (&FreeCallbackFactory3::Wrapper<Func>, 0);
507- }
508-};
509-
510-template<typename R, typename P1, typename P2, typename P3>
511-inline FreeCallbackFactory3<R, P1, P2, P3>
512-GetCallbackFactory(R (*)(P1, P2, P3))
513-{
514- return FreeCallbackFactory3<R, P1, P2, P3>();
515-}
516-
517-template<typename R, class T, typename P1, typename P2, typename P3>
518-class MemberCallbackFactory3
519-{
520-private:
521- template<R (T::*Func)(P1, P2, P3)>
522- static R Wrapper(const void* o, P1 a1, P2 a2, P3 a3)
523- {
524- T* obj = const_cast<T*>(static_cast<const T*>(o));
525- return (obj->*Func)(a1, a2, a3);
526- }
527-
528-public:
529- template<R (T::*Func)(P1, P2, P3)>
530- inline static Callback<R (P1, P2, P3)> Bind(T* o)
531- {
532- return Callback<R (P1, P2, P3)>
533- (&MemberCallbackFactory3::Wrapper<Func>,
534- static_cast<const void*>(o));
535- }
536-};
537-
538-template<typename R, class T, typename P1, typename P2, typename P3>
539-inline MemberCallbackFactory3<R, T, P1, P2, P3>
540-GetCallbackFactory(R (T::*)(P1, P2, P3))
541-{
542- return MemberCallbackFactory3<R, T, P1, P2, P3>();
543-}
544-
545-template<typename R, class T, typename P1, typename P2, typename P3>
546-class ConstMemberCallbackFactory3
547-{
548-private:
549- template<R (T::*Func)(P1, P2, P3) const>
550- static R Wrapper(const void* o, P1 a1, P2 a2, P3 a3)
551- {
552- const T* obj = static_cast<const T*>(o);
553- return (obj->*Func)(a1, a2, a3);
554- }
555-
556-public:
557- template<R (T::*Func)(P1, P2, P3) const>
558- inline static Callback<R (P1, P2, P3)> Bind(const T* o)
559- {
560- return Callback<R (P1, P2, P3)>
561- (&ConstMemberCallbackFactory3::Wrapper<Func>,
562- static_cast<const void*>(o));
563- }
564-};
565-
566-template<typename R, class T, typename P1, typename P2, typename P3>
567-inline ConstMemberCallbackFactory3<R, T, P1, P2, P3>
568-GetCallbackFactory(R (T::*)(P1, P2, P3) const)
569-{
570- return ConstMemberCallbackFactory3<R, T, P1, P2, P3>();
571-}
572-
573-// 4 parameter version
574-
575-template<typename R, typename P1, typename P2, typename P3,
576- typename P4>
577-class Callback<R (P1, P2, P3, P4)>
578-{
579-public:
580- static const int Arity = 4;
581- typedef R ReturnType;
582- typedef P1 Param1Type;
583- typedef P2 Param2Type;
584- typedef P3 Param3Type;
585- typedef P4 Param4Type;
586-
587- Callback() : func(0), obj(0) {}
588- Callback(NullCallback) : func(0), obj(0) {}
589- Callback(const Callback& rhs) : func(rhs.func), obj(rhs.obj) {}
590- ~Callback() {}
591-
592- Callback& operator=(NullCallback)
593- { obj = 0; func = 0; return *this; }
594- Callback& operator=(const Callback& rhs)
595- { obj = rhs.obj; func = rhs.func; return *this; }
596-
597- inline R operator()(P1 a1, P2 a2, P3 a3, P4 a4) const
598- {
599- return (*func)(obj, a1, a2, a3, a4);
600- }
601-
602-private:
603- typedef const void* Callback::*SafeBoolType;
604-public:
605- inline operator SafeBoolType() const
606- { return func != 0 ? &Callback::obj : 0; }
607- inline bool operator!() const
608- { return func == 0; }
609-
610-private:
611- typedef R (*FuncType)(const void*, P1, P2, P3, P4);
612- Callback(FuncType f, const void* o) : func(f), obj(o) {}
613-
614-private:
615- FuncType func;
616- const void* obj;
617-
618- template<typename FR, typename FP1, typename FP2, typename FP3,
619- typename FP4>
620- friend class FreeCallbackFactory4;
621- template<typename FR, class FT, typename FP1, typename FP2, typename FP3,
622- typename FP4>
623- friend class MemberCallbackFactory4;
624- template<typename FR, class FT, typename FP1, typename FP2, typename FP3,
625- typename FP4>
626- friend class ConstMemberCallbackFactory4;
627-};
628-
629-template<typename R, typename P1, typename P2, typename P3,
630- typename P4>
631-void operator==(const Callback<R (P1, P2, P3, P4)>&,
632- const Callback<R (P1, P2, P3, P4)>&);
633-template<typename R, typename P1, typename P2, typename P3,
634- typename P4>
635-void operator!=(const Callback<R (P1, P2, P3, P4)>&,
636- const Callback<R (P1, P2, P3, P4)>&);
637-
638-template<typename R, typename P1, typename P2, typename P3,
639- typename P4>
640-class FreeCallbackFactory4
641-{
642-private:
643- template<R (*Func)(P1, P2, P3, P4)>
644- static R Wrapper(const void*, P1 a1, P2 a2, P3 a3, P4 a4)
645- {
646- return (*Func)(a1, a2, a3, a4);
647- }
648-
649-public:
650- template<R (*Func)(P1, P2, P3, P4)>
651- inline static Callback<R (P1, P2, P3, P4)> Bind()
652- {
653- return Callback<R (P1, P2, P3, P4)>
654- (&FreeCallbackFactory4::Wrapper<Func>, 0);
655- }
656-};
657-
658-template<typename R, typename P1, typename P2, typename P3,
659- typename P4>
660-inline FreeCallbackFactory4<R, P1, P2, P3, P4>
661-GetCallbackFactory(R (*)(P1, P2, P3, P4))
662-{
663- return FreeCallbackFactory4<R, P1, P2, P3, P4>();
664-}
665-
666-template<typename R, class T, typename P1, typename P2, typename P3,
667- typename P4>
668-class MemberCallbackFactory4
669-{
670-private:
671- template<R (T::*Func)(P1, P2, P3, P4)>
672- static R Wrapper(const void* o, P1 a1, P2 a2, P3 a3, P4 a4)
673- {
674- T* obj = const_cast<T*>(static_cast<const T*>(o));
675- return (obj->*Func)(a1, a2, a3, a4);
676- }
677-
678-public:
679- template<R (T::*Func)(P1, P2, P3, P4)>
680- inline static Callback<R (P1, P2, P3, P4)> Bind(T* o)
681- {
682- return Callback<R (P1, P2, P3, P4)>
683- (&MemberCallbackFactory4::Wrapper<Func>,
684- static_cast<const void*>(o));
685- }
686-};
687-
688-template<typename R, class T, typename P1, typename P2, typename P3,
689- typename P4>
690-inline MemberCallbackFactory4<R, T, P1, P2, P3, P4>
691-GetCallbackFactory(R (T::*)(P1, P2, P3, P4))
692-{
693- return MemberCallbackFactory4<R, T, P1, P2, P3, P4>();
694-}
695-
696-template<typename R, class T, typename P1, typename P2, typename P3,
697- typename P4>
698-class ConstMemberCallbackFactory4
699-{
700-private:
701- template<R (T::*Func)(P1, P2, P3, P4) const>
702- static R Wrapper(const void* o, P1 a1, P2 a2, P3 a3, P4 a4)
703- {
704- const T* obj = static_cast<const T*>(o);
705- return (obj->*Func)(a1, a2, a3, a4);
706- }
707-
708-public:
709- template<R (T::*Func)(P1, P2, P3, P4) const>
710- inline static Callback<R (P1, P2, P3, P4)> Bind(const T* o)
711- {
712- return Callback<R (P1, P2, P3, P4)>
713- (&ConstMemberCallbackFactory4::Wrapper<Func>,
714- static_cast<const void*>(o));
715- }
716-};
717-
718-template<typename R, class T, typename P1, typename P2, typename P3,
719- typename P4>
720-inline ConstMemberCallbackFactory4<R, T, P1, P2, P3, P4>
721-GetCallbackFactory(R (T::*)(P1, P2, P3, P4) const)
722-{
723- return ConstMemberCallbackFactory4<R, T, P1, P2, P3, P4>();
724-}
725-
726-// 5 parameter version
727-
728-template<typename R, typename P1, typename P2, typename P3,
729- typename P4, typename P5>
730-class Callback<R (P1, P2, P3, P4, P5)>
731-{
732-public:
733- static const int Arity = 5;
734- typedef R ReturnType;
735- typedef P1 Param1Type;
736- typedef P2 Param2Type;
737- typedef P3 Param3Type;
738- typedef P4 Param4Type;
739- typedef P5 Param5Type;
740-
741- Callback() : func(0), obj(0) {}
742- Callback(NullCallback) : func(0), obj(0) {}
743- Callback(const Callback& rhs) : func(rhs.func), obj(rhs.obj) {}
744- ~Callback() {}
745-
746- Callback& operator=(NullCallback)
747- { obj = 0; func = 0; return *this; }
748- Callback& operator=(const Callback& rhs)
749- { obj = rhs.obj; func = rhs.func; return *this; }
750-
751- inline R operator()(P1 a1, P2 a2, P3 a3, P4 a4, P5 a5) const
752- {
753- return (*func)(obj, a1, a2, a3, a4, a5);
754- }
755-
756-private:
757- typedef const void* Callback::*SafeBoolType;
758-public:
759- inline operator SafeBoolType() const
760- { return func != 0 ? &Callback::obj : 0; }
761- inline bool operator!() const
762- { return func == 0; }
763-
764-private:
765- typedef R (*FuncType)(const void*, P1, P2, P3, P4, P5);
766- Callback(FuncType f, const void* o) : func(f), obj(o) {}
767-
768-private:
769- FuncType func;
770- const void* obj;
771-
772- template<typename FR, typename FP1, typename FP2, typename FP3,
773- typename FP4, typename FP5>
774- friend class FreeCallbackFactory5;
775- template<typename FR, class FT, typename FP1, typename FP2, typename FP3,
776- typename FP4, typename FP5>
777- friend class MemberCallbackFactory5;
778- template<typename FR, class FT, typename FP1, typename FP2, typename FP3,
779- typename FP4, typename FP5>
780- friend class ConstMemberCallbackFactory5;
781-};
782-
783-template<typename R, typename P1, typename P2, typename P3,
784- typename P4, typename P5>
785-void operator==(const Callback<R (P1, P2, P3, P4, P5)>&,
786- const Callback<R (P1, P2, P3, P4, P5)>&);
787-template<typename R, typename P1, typename P2, typename P3,
788- typename P4, typename P5>
789-void operator!=(const Callback<R (P1, P2, P3, P4, P5)>&,
790- const Callback<R (P1, P2, P3, P4, P5)>&);
791-
792-template<typename R, typename P1, typename P2, typename P3,
793- typename P4, typename P5>
794-class FreeCallbackFactory5
795-{
796-private:
797- template<R (*Func)(P1, P2, P3, P4, P5)>
798- static R Wrapper(const void*, P1 a1, P2 a2, P3 a3, P4 a4, P5 a5)
799- {
800- return (*Func)(a1, a2, a3, a4, a5);
801- }
802-
803-public:
804- template<R (*Func)(P1, P2, P3, P4, P5)>
805- inline static Callback<R (P1, P2, P3, P4, P5)> Bind()
806- {
807- return Callback<R (P1, P2, P3, P4, P5)>
808- (&FreeCallbackFactory5::Wrapper<Func>, 0);
809- }
810-};
811-
812-template<typename R, typename P1, typename P2, typename P3,
813- typename P4, typename P5>
814-inline FreeCallbackFactory5<R, P1, P2, P3, P4, P5>
815-GetCallbackFactory(R (*)(P1, P2, P3, P4, P5))
816-{
817- return FreeCallbackFactory5<R, P1, P2, P3, P4, P5>();
818-}
819-
820-template<typename R, class T, typename P1, typename P2, typename P3,
821- typename P4, typename P5>
822-class MemberCallbackFactory5
823-{
824-private:
825- template<R (T::*Func)(P1, P2, P3, P4, P5)>
826- static R Wrapper(const void* o, P1 a1, P2 a2, P3 a3, P4 a4, P5 a5)
827- {
828- T* obj = const_cast<T*>(static_cast<const T*>(o));
829- return (obj->*Func)(a1, a2, a3, a4, a5);
830- }
831-
832-public:
833- template<R (T::*Func)(P1, P2, P3, P4, P5)>
834- inline static Callback<R (P1, P2, P3, P4, P5)> Bind(T* o)
835- {
836- return Callback<R (P1, P2, P3, P4, P5)>
837- (&MemberCallbackFactory5::Wrapper<Func>,
838- static_cast<const void*>(o));
839- }
840-};
841-
842-template<typename R, class T, typename P1, typename P2, typename P3,
843- typename P4, typename P5>
844-inline MemberCallbackFactory5<R, T, P1, P2, P3, P4, P5>
845-GetCallbackFactory(R (T::*)(P1, P2, P3, P4, P5))
846-{
847- return MemberCallbackFactory5<R, T, P1, P2, P3, P4, P5>();
848-}
849-
850-template<typename R, class T, typename P1, typename P2, typename P3,
851- typename P4, typename P5>
852-class ConstMemberCallbackFactory5
853-{
854-private:
855- template<R (T::*Func)(P1, P2, P3, P4, P5) const>
856- static R Wrapper(const void* o, P1 a1, P2 a2, P3 a3, P4 a4, P5 a5)
857- {
858- const T* obj = static_cast<const T*>(o);
859- return (obj->*Func)(a1, a2, a3, a4, a5);
860- }
861-
862-public:
863- template<R (T::*Func)(P1, P2, P3, P4, P5) const>
864- inline static Callback<R (P1, P2, P3, P4, P5)> Bind(const T* o)
865- {
866- return Callback<R (P1, P2, P3, P4, P5)>
867- (&ConstMemberCallbackFactory5::Wrapper<Func>,
868- static_cast<const void*>(o));
869- }
870-};
871-
872-template<typename R, class T, typename P1, typename P2, typename P3,
873- typename P4, typename P5>
874-inline ConstMemberCallbackFactory5<R, T, P1, P2, P3, P4, P5>
875-GetCallbackFactory(R (T::*)(P1, P2, P3, P4, P5) const)
876-{
877- return ConstMemberCallbackFactory5<R, T, P1, P2, P3, P4, P5>();
878-}
879-
880-// 6 parameter version
881-
882-template<typename R, typename P1, typename P2, typename P3,
883- typename P4, typename P5, typename P6>
884-class Callback<R (P1, P2, P3, P4, P5, P6)>
885-{
886-public:
887- static const int Arity = 6;
888- typedef R ReturnType;
889- typedef P1 Param1Type;
890- typedef P2 Param2Type;
891- typedef P3 Param3Type;
892- typedef P4 Param4Type;
893- typedef P5 Param5Type;
894- typedef P6 Param6Type;
895-
896- Callback() : func(0), obj(0) {}
897- Callback(NullCallback) : func(0), obj(0) {}
898- Callback(const Callback& rhs) : func(rhs.func), obj(rhs.obj) {}
899- ~Callback() {}
900-
901- Callback& operator=(NullCallback)
902- { obj = 0; func = 0; return *this; }
903- Callback& operator=(const Callback& rhs)
904- { obj = rhs.obj; func = rhs.func; return *this; }
905-
906- inline R operator()(P1 a1, P2 a2, P3 a3, P4 a4, P5 a5, P6 a6) const
907- {
908- return (*func)(obj, a1, a2, a3, a4, a5, a6);
909- }
910-
911-private:
912- typedef const void* Callback::*SafeBoolType;
913-public:
914- inline operator SafeBoolType() const
915- { return func != 0 ? &Callback::obj : 0; }
916- inline bool operator!() const
917- { return func == 0; }
918-
919-private:
920- typedef R (*FuncType)(const void*, P1, P2, P3, P4, P5, P6);
921- Callback(FuncType f, const void* o) : func(f), obj(o) {}
922-
923-private:
924- FuncType func;
925- const void* obj;
926-
927- template<typename FR, typename FP1, typename FP2, typename FP3,
928- typename FP4, typename FP5, typename FP6>
929- friend class FreeCallbackFactory6;
930- template<typename FR, class FT, typename FP1, typename FP2, typename FP3,
931- typename FP4, typename FP5, typename FP6>
932- friend class MemberCallbackFactory6;
933- template<typename FR, class FT, typename FP1, typename FP2, typename FP3,
934- typename FP4, typename FP5, typename FP6>
935- friend class ConstMemberCallbackFactory6;
936-};
937-
938-template<typename R, typename P1, typename P2, typename P3,
939- typename P4, typename P5, typename P6>
940-void operator==(const Callback<R (P1, P2, P3, P4, P5, P6)>&,
941- const Callback<R (P1, P2, P3, P4, P5, P6)>&);
942-template<typename R, typename P1, typename P2, typename P3,
943- typename P4, typename P5, typename P6>
944-void operator!=(const Callback<R (P1, P2, P3, P4, P5, P6)>&,
945- const Callback<R (P1, P2, P3, P4, P5, P6)>&);
946-
947-template<typename R, typename P1, typename P2, typename P3,
948- typename P4, typename P5, typename P6>
949-class FreeCallbackFactory6
950-{
951-private:
952- template<R (*Func)(P1, P2, P3, P4, P5, P6)>
953- static R Wrapper(const void*, P1 a1, P2 a2, P3 a3, P4 a4, P5 a5, P6 a6)
954- {
955- return (*Func)(a1, a2, a3, a4, a5, a6);
956- }
957-
958-public:
959- template<R (*Func)(P1, P2, P3, P4, P5, P6)>
960- inline static Callback<R (P1, P2, P3, P4, P5, P6)> Bind()
961- {
962- return Callback<R (P1, P2, P3, P4, P5, P6)>
963- (&FreeCallbackFactory6::Wrapper<Func>, 0);
964- }
965-};
966-
967-template<typename R, typename P1, typename P2, typename P3,
968- typename P4, typename P5, typename P6>
969-inline FreeCallbackFactory6<R, P1, P2, P3, P4, P5, P6>
970-GetCallbackFactory(R (*)(P1, P2, P3, P4, P5, P6))
971-{
972- return FreeCallbackFactory6<R, P1, P2, P3, P4, P5, P6>();
973-}
974-
975-template<typename R, class T, typename P1, typename P2, typename P3,
976- typename P4, typename P5, typename P6>
977-class MemberCallbackFactory6
978-{
979-private:
980- template<R (T::*Func)(P1, P2, P3, P4, P5, P6)>
981- static R Wrapper(const void* o, P1 a1, P2 a2, P3 a3, P4 a4, P5 a5, P6 a6)
982- {
983- T* obj = const_cast<T*>(static_cast<const T*>(o));
984- return (obj->*Func)(a1, a2, a3, a4, a5, a6);
985- }
986-
987-public:
988- template<R (T::*Func)(P1, P2, P3, P4, P5, P6)>
989- inline static Callback<R (P1, P2, P3, P4, P5, P6)> Bind(T* o)
990- {
991- return Callback<R (P1, P2, P3, P4, P5, P6)>
992- (&MemberCallbackFactory6::Wrapper<Func>,
993- static_cast<const void*>(o));
994- }
995-};
996-
997-template<typename R, class T, typename P1, typename P2, typename P3,
998- typename P4, typename P5, typename P6>
999-inline MemberCallbackFactory6<R, T, P1, P2, P3, P4, P5, P6>
1000-GetCallbackFactory(R (T::*)(P1, P2, P3, P4, P5, P6))
1001-{
1002- return MemberCallbackFactory6<R, T, P1, P2, P3, P4, P5, P6>();
1003-}
1004-
1005-template<typename R, class T, typename P1, typename P2, typename P3,
1006- typename P4, typename P5, typename P6>
1007-class ConstMemberCallbackFactory6
1008-{
1009-private:
1010- template<R (T::*Func)(P1, P2, P3, P4, P5, P6) const>
1011- static R Wrapper(const void* o, P1 a1, P2 a2, P3 a3, P4 a4, P5 a5, P6 a6)
1012- {
1013- const T* obj = static_cast<const T*>(o);
1014- return (obj->*Func)(a1, a2, a3, a4, a5, a6);
1015- }
1016-
1017-public:
1018- template<R (T::*Func)(P1, P2, P3, P4, P5, P6) const>
1019- inline static Callback<R (P1, P2, P3, P4, P5, P6)> Bind(const T* o)
1020- {
1021- return Callback<R (P1, P2, P3, P4, P5, P6)>
1022- (&ConstMemberCallbackFactory6::Wrapper<Func>,
1023- static_cast<const void*>(o));
1024- }
1025-};
1026-
1027-template<typename R, class T, typename P1, typename P2, typename P3,
1028- typename P4, typename P5, typename P6>
1029-inline ConstMemberCallbackFactory6<R, T, P1, P2, P3, P4, P5, P6>
1030-GetCallbackFactory(R (T::*)(P1, P2, P3, P4, P5, P6) const)
1031-{
1032- return ConstMemberCallbackFactory6<R, T, P1, P2, P3, P4, P5, P6>();
1033-}
1034-
1035-}
1036-
1037-#endif
1038-
--- a/main.cpp
+++ b/main.cpp
@@ -196,21 +196,21 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
196196 case WM_LBUTTONDOWN:
197197 case WM_MBUTTONDOWN:
198198 case WM_RBUTTONDOWN:
199- ::SetCapture(hWnd);
199+// ::SetCapture(hWnd);
200200 OnMouseDown(hWnd, wParam, lParam);
201201 break;
202202 case WM_LBUTTONUP:
203203 case WM_MBUTTONUP:
204204 case WM_RBUTTONUP:
205- if (::GetCapture() == hWnd) {
206- ::ReleaseCapture();
205+// if (::GetCapture() == hWnd) {
206+// ::ReleaseCapture();
207207 OnMouseUp(hWnd, wParam, lParam);
208- }
208+// }
209209 break;
210210 case WM_MOUSEMOVE:
211- if (::GetCapture() == hWnd) {
211+// if (::GetCapture() == hWnd) {
212212 OnMouseMove(hWnd, wParam, lParam);
213- }
213+// }
214214 break;
215215 default:
216216 return DefWindowProc(hWnd, message, wParam, lParam);
--- a/main_plus.cpp
+++ b/main_plus.cpp
@@ -52,7 +52,7 @@ HDC hMemDC;
5252 MG::Renderer24 renderer;
5353
5454 MG::Container mg(4, 8);
55-MG::Button button1(0);
55+MG::Button button1(4);
5656 MG::Button button2(0);
5757 MG::Element pic1(0);
5858 MG::Element pic2(0);
@@ -94,6 +94,26 @@ bool ReadImage(const char* filename, MG::Bitmap& bmp, std::vector<uint8_t>& buff
9494
9595 } // anonymous namespace
9696
97+void OnButton1MouseDown(void* param, const MG::Element::Event& e)
98+{
99+ OutputDebugString(L"MouseDown\n");
100+}
101+
102+void OnButton1MouseUp(void* param, const MG::Element::Event& e)
103+{
104+ OutputDebugString(L"MouseUp\n");
105+}
106+
107+void OnButton1MouseMove(void* param, const MG::Element::Event& e)
108+{
109+ OutputDebugString(L"MouseMove\n");
110+}
111+
112+void OnButton1Click(void* param, const MG::Element::Event& e)
113+{
114+ OutputDebugString(L"Click\n");
115+}
116+
97117 void OnCreate(HWND hWnd, WPARAM wParam, LPARAM lParam)
98118 {
99119 BITMAPINFO* pBMI = (BITMAPINFO*) &bmiBuff[0];
@@ -127,15 +147,15 @@ void OnCreate(HWND hWnd, WPARAM wParam, LPARAM lParam)
127147 rect.y = 0;
128148 rect.w = width;
129149 rect.h = height;
130- mg.rect = rect;
150+ mg.rect_ = rect;
131151 MG::Color col;
132152 col.r = 50;
133153 col.g = 200;
134154 col.b = 150;
135155
136156 mainbg.color = col;
137- mg.pBG = &mainbg;
138- mg.needsToDraw = true;
157+ mg.pBG_ = &mainbg;
158+ mg.needsToDraw_ = true;
139159
140160 col.g = 100;
141161 buttonBG.color = col;
@@ -146,18 +166,18 @@ void OnCreate(HWND hWnd, WPARAM wParam, LPARAM lParam)
146166 rect.y = 100;
147167 rect.w = 100;
148168 rect.h = 50;
149- button1.rect = rect;
150- button1.pBG = &buttonBG;
151- button1.pBGPressed = &buttonBGPressed;
169+ button1.rect_ = rect;
170+ button1.pBG_ = &buttonBG;
171+ button1.pBGPressed_ = &buttonBGPressed;
152172 mg.AddChild(button1);
153173
154174 rect.x = 300;
155175 rect.y = 100;
156176 rect.w = 100;
157177 rect.h = 50;
158- button2.rect = rect;
159- button2.pBG = &buttonBG;
160- button2.pBGPressed = &buttonBGPressed;
178+ button2.rect_ = rect;
179+ button2.pBG_ = &buttonBG;
180+ button2.pBGPressed_ = &buttonBGPressed;
161181 mg.AddChild(button2);
162182
163183 if (!ReadImage("test24.bmp", bmp24Content, image24Buffer)) {
@@ -169,7 +189,7 @@ void OnCreate(HWND hWnd, WPARAM wParam, LPARAM lParam)
169189
170190 rect.x = 100;
171191 rect.y = 300;
172- pic1.rect = rect;
192+ pic1.rect_ = rect;
173193 MG::Rectangle irec;
174194 irec.x = 510;
175195 irec.y = 340;
@@ -177,23 +197,23 @@ void OnCreate(HWND hWnd, WPARAM wParam, LPARAM lParam)
177197 irec.h = 400;
178198 picbg1.image.rect = irec;
179199 picbg1.image.pBitmap = &bmp24Content;
180- pic1.pBG = &picbg1;
181- pic1.needsToDraw = true;
200+ pic1.pBG_ = &picbg1;
201+ pic1.needsToDraw_ = true;
182202 mg.AddChild(pic1);
183203
184204 rect.x = 300;
185205 rect.y = 200;
186206 rect.w = 336;
187207 rect.h = 300;
188- pic2.rect = rect;
208+ pic2.rect_ = rect;
189209 irec.x = 0;
190210 irec.y = 0;
191211 irec.w = 336;
192212 irec.h = 24;
193213 picbg2.image.rect = irec;
194214 picbg2.image.pBitmap = &bmp32Content;
195- pic2.pBG = &picbg2;
196- pic2.needsToDraw = true;
215+ pic2.pBG_ = &picbg2;
216+ pic2.needsToDraw_ = true;
197217 mg.AddChild(pic2);
198218
199219 HDC hWndDC = ::GetDC(hWnd);
@@ -202,6 +222,12 @@ void OnCreate(HWND hWnd, WPARAM wParam, LPARAM lParam)
202222 ::ReleaseDC(hWnd, hWndDC);
203223 ::SelectObject(hMemDC, hBMP);
204224
225+ button1.AddEventListener(MG::Element::EventType_MouseDown, OnButton1MouseDown);
226+ button1.AddEventListener(MG::Element::EventType_MouseUp, OnButton1MouseUp);
227+// button1.AddEventListener(MG::Element::EventType_MouseMove, OnButton1MouseMove);
228+ button1.AddEventListener(MG::Button::EventType_Click, OnButton1Click);
229+
230+
205231 mg.Draw(0, 0, renderer);
206232 }
207233
--- a/mg.vcproj
+++ b/mg.vcproj
@@ -113,8 +113,9 @@
113113 />
114114 <Tool
115115 Name="VCCLCompilerTool"
116- Optimization="2"
116+ Optimization="1"
117117 EnableIntrinsicFunctions="true"
118+ FavorSizeOrSpeed="2"
118119 AdditionalIncludeDirectories="./;./common/"
119120 PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_CRT_SECURE_NO_WARNINGS"
120121 RuntimeLibrary="2"
@@ -313,10 +314,6 @@
313314 >
314315 </File>
315316 <File
316- RelativePath=".\common\callback.hpp"
317- >
318- </File>
319- <File
320317 RelativePath=".\common\optional.h"
321318 >
322319 </File>
--- a/mg/button.cpp
+++ b/mg/button.cpp
@@ -7,19 +7,18 @@ namespace MG {
77 Button::Button(size_t nMaxEvents)
88 :
99 Element(nMaxEvents),
10- pBGPressed(0)
10+ pBGPressed_(0)
1111 {
12- needsToDraw = true;
13- bClickable_ = true;
12+ needsToDraw_ = true;
1413 }
1514
1615 void Button::Draw(int16_t offsetX, int16_t offsetY, IRenderer& renderer)
1716 {
1817 if (isPressed_) {
19- IBackground* backup = pBG;
20- pBG = pBGPressed;
18+ IBackground* backup = pBG_;
19+ pBG_ = pBGPressed_;
2120 __super::Draw(offsetX, offsetY, renderer);
22- pBG = backup;
21+ pBG_ = backup;
2322 }else {
2423 __super::Draw(offsetX, offsetY, renderer);
2524 }
@@ -27,26 +26,38 @@ void Button::Draw(int16_t offsetX, int16_t offsetY, IRenderer& renderer)
2726
2827 bool Button::HitTest(int16_t x, int16_t y) const
2928 {
30- return true;
29+ return __super::HitTest(x, y);
3130 }
3231
3332 void Button::OnMouseDown(int16_t x, int16_t y)
3433 {
35- needsToDraw = true;
34+ needsToDraw_ = true;
3635 isPressed_ = true;
36+ s_pCapture_ = this;
37+ __super::OnMouseDown(x, y);
3738 }
3839
3940 void Button::OnMouseUp(int16_t x, int16_t y)
4041 {
42+ __super::OnMouseUp(x, y);
4143 if (isPressed_) {
42- needsToDraw = true;
44+ needsToDraw_ = true;
4345 isPressed_ = false;
46+ s_pCapture_ = 0;
47+ if (HitTest(x, y)) {
48+ OnClick(x, y);
49+ }
4450 }
4551 }
4652
47-void Button::OnMouseMove(int16_t x, int16_t y)
53+void Button::OnClick(int16_t x, int16_t y)
4854 {
49- ;
55+ Event e;
56+ e.target = this;
57+ e.type = EventType_Click;
58+ e.x = x;
59+ e.y = y;
60+ fireEventListener(e);
5061 }
5162
5263 } // anonymous MG
--- a/mg/button.h
+++ b/mg/button.h
@@ -16,12 +16,12 @@ public:
1616 bool HitTest(int16_t x, int16_t y) const;
1717 void OnMouseDown(int16_t x, int16_t y);
1818 void OnMouseUp(int16_t x, int16_t y);
19- void OnMouseMove(int16_t x, int16_t y);
19+ void OnClick(int16_t x, int16_t y);
2020
21- IBackground* pBGPressed;
21+ IBackground* pBGPressed_;
2222
2323 enum EventType {
24- EventType_Clicked = __super::EventType_End,
24+ EventType_Click = __super::EventType_End,
2525
2626 EventType_End,
2727 };
--- a/mg/container.cpp
+++ b/mg/container.cpp
@@ -13,11 +13,10 @@ namespace MG {
1313
1414 Container::Container(size_t nMaxEvents, size_t nMaxChildren)
1515 :
16- Element(nMaxEvents),
17- clickedElementIndex_(0)
16+ Element(nMaxEvents)
1817 {
19- vElements_.buff_ = &s_elementPtrs[s_nElementEntries];
20- vElements_.maxLength_ = nMaxChildren;
18+ elements_.buff_ = &s_elementPtrs[s_nElementEntries];
19+ elements_.maxLength_ = nMaxChildren;
2120 s_nElementEntries += nMaxChildren;
2221 assert(s_nElementEntries < countof(s_elementPtrs));
2322 }
@@ -28,15 +27,15 @@ Container::~Container()
2827
2928 void Container::AddChild(Element& e)
3029 {
31- vElements_.push_back(&e);
30+ elements_.push_back(&e);
3231 e.SetParent(this);
3332 }
3433
3534 void Container::RemoveChild(Element& e)
3635 {
37- for (size_t i=0; i<vElements_.length_; ++i) {
38- if (vElements_[i] == &e) {
39- vElements_[i] = 0;
36+ for (size_t i=0; i<elements_.length_; ++i) {
37+ if (elements_[i] == &e) {
38+ elements_[i] = 0;
4039 e.SetParent(0);
4140 break;
4241 }
@@ -45,18 +44,18 @@ void Container::RemoveChild(Element& e)
4544
4645 void Container::Draw(int16_t offsetX, int16_t offsetY, IRenderer& renderer)
4746 {
48- if (needsToDraw) {
47+ if (needsToDraw_) {
4948 __super::Draw(offsetX, offsetY, renderer);
50- needsToDraw = false;
49+ needsToDraw_ = false;
5150 }
5251
53- int16_t x = rect.x + offsetX;
54- int16_t y = rect.y + offsetY;
55- for (size_t i=0; i<vElements_.length_; ++i) {
56- Element* e = vElements_[i];
57- if (e && e->needsToDraw) {
52+ int16_t x = rect_.x + offsetX;
53+ int16_t y = rect_.y + offsetY;
54+ for (size_t i=0; i<elements_.length_; ++i) {
55+ Element* e = elements_[i];
56+ if (e && e->needsToDraw_) {
5857 e->Draw(x, y, renderer);
59- e->needsToDraw = false;
58+ e->needsToDraw_ = false;
6059 }
6160 }
6261 }
@@ -66,46 +65,66 @@ bool Container::HitTest(int16_t x, int16_t y) const
6665 return true;
6766 }
6867
69-void Container::OnMouseDown(int16_t x, int16_t y)
68+static
69+Element* hitTestElements(varray<Element*>& elements, int16_t x, int16_t y)
7070 {
71- int16_t ox = x - rect.x;
72- int16_t oy = y - rect.y;
73- for (size_t i=0; i<vElements_.length_; ++i) {
74- Element* e = vElements_[i];
71+ const size_t len = elements.length_;
72+ for (size_t i=0; i<len; ++i) {
73+ Element* e = elements[i];
7574 if (e) {
76- if (1
77- && e->IsClickable()
78- && e->rect.Contains(ox, oy)
79- && e->HitTest(ox, oy)
80- ) {
81- clickedElementIndex_ = i;
82- e->OnMouseDown(ox, oy);
83- return;
75+ if (e->HitTest(x, y)) {
76+ return e;
8477 }
8578 }
8679 }
80+ return 0;
81+}
82+
83+void Container::OnMouseDown(int16_t x, int16_t y)
84+{
85+ int16_t ox = x - rect_.x;
86+ int16_t oy = y - rect_.y;
87+
88+ if (s_pCapture_) {
89+ s_pCapture_->OnMouseDown(ox, oy);
90+ }else {
91+ Element* e = hitTestElements(elements_, x, y);
92+ if (e) {
93+ e->OnMouseDown(ox, oy);
94+ return;
95+ }
96+ }
8797 }
8898
8999 void Container::OnMouseUp(int16_t x, int16_t y)
90100 {
91- int16_t ox = x - rect.x;
92- int16_t oy = y - rect.y;
93- Element* e = vElements_[clickedElementIndex_];
94- if (e) {
95- e->OnMouseUp(ox, oy);
101+ int16_t ox = x - rect_.x;
102+ int16_t oy = y - rect_.y;
103+ if (s_pCapture_) {
104+ s_pCapture_->OnMouseUp(ox, oy);
105+ }else {
106+ Element* e = hitTestElements(elements_, x, y);
107+ if (e) {
108+ e->OnMouseUp(ox, oy);
109+ return;
110+ }
96111 }
97112 }
98113
99114 void Container::OnMouseMove(int16_t x, int16_t y)
100115 {
101- int16_t ox = x - rect.x;
102- int16_t oy = y - rect.y;
103- Element* e = vElements_[clickedElementIndex_];
104- if (e) {
105- e->OnMouseMove(ox, oy);
116+ int16_t ox = x - rect_.x;
117+ int16_t oy = y - rect_.y;
118+ if (s_pCapture_) {
119+ s_pCapture_->OnMouseMove(ox, oy);
120+ }else {
121+ Element* e = hitTestElements(elements_, x, y);
122+ if (e) {
123+ e->OnMouseMove(ox, oy);
124+ return;
125+ }
106126 }
107127 }
108128
109-
110129 } // namespace MG
111130
--- a/mg/container.h
+++ b/mg/container.h
@@ -23,8 +23,7 @@ public:
2323 virtual void OnMouseMove(int16_t x, int16_t y);
2424
2525 protected:
26- size_t clickedElementIndex_;
27- varray<Element*> vElements_;
26+ varray<Element*> elements_;
2827
2928 };
3029
--- a/mg/element.cpp
+++ b/mg/element.cpp
@@ -12,6 +12,9 @@ MG::Element::EventListenerEntry s_eventEntries[128];
1212
1313 namespace MG {
1414
15+// static
16+Element* Element::s_pCapture_ = 0;
17+
1518 Element::Element(size_t nMaxEvents)
1619 {
1720 eventEntries_.buff_ = &s_eventEntries[s_nEventEntries];
@@ -20,23 +23,90 @@ Element::Element(size_t nMaxEvents)
2023 assert(s_nEventEntries < countof(s_eventEntries));
2124
2225 pParent_ = 0;
23- needsToDraw = false;
24- pBG = 0;
25- bClickable_ = false;
26+ needsToDraw_ = false;
27+ pBG_ = 0;
28+ pEventListenerParam_ = 0;
2629 }
2730
2831 // virtual
2932 void Element::Draw(int16_t offsetX, int16_t offsetY, IRenderer& renderer)
3033 {
31- Rectangle r = rect;
34+ Rectangle r = rect_;
3235 r.x += offsetX;
3336 r.y += offsetY;
3437
35- if (pBG) {
36- pBG->Draw(r, renderer);
38+ if (pBG_) {
39+ pBG_->Draw(r, renderer);
3740 }
3841 }
3942
43+// virtual
44+bool Element::HitTest(int16_t x, int16_t y) const
45+{
46+ return rect_.Contains(x, y);
47+}
4048
41-} // namespace MG
49+void Element::AddEventListener(int type, const EventListener& listener)
50+{
51+ EventListenerEntry entry;
52+ entry.type = type;
53+ entry.listener = listener;
54+ eventEntries_.push_back(entry);
55+}
56+
57+void Element::RemoveEventListener(int type, const EventListener& listener)
58+{
59+ for (size_t i=0; i<eventEntries_.length_; ++i) {
60+ EventListenerEntry& entry = eventEntries_[i];
61+ if (entry.type == type && entry.listener == listener) {
62+ entry.type = 0;
63+ entry.listener = 0;
64+ break;
65+ }
66+ }
67+}
68+
69+void Element::fireEventListener(const Event& e)
70+{
71+ for (size_t i=0; i<eventEntries_.length_; ++i) {
72+ EventListenerEntry& entry = eventEntries_[i];
73+ if (entry.type == e.type) {
74+ entry.listener(pEventListenerParam_, e);
75+ }
76+ }
77+}
78+
79+// virtual
80+void Element::OnMouseDown(int16_t x, int16_t y)
81+{
82+ Event e;
83+ e.target = this;
84+ e.type = EventType_MouseDown;
85+ e.x = x;
86+ e.y = y;
87+ fireEventListener(e);
88+}
89+
90+// virtual
91+void Element::OnMouseUp(int16_t x, int16_t y)
92+{
93+ Event e;
94+ e.target = this;
95+ e.type = EventType_MouseUp;
96+ e.x = x;
97+ e.y = y;
98+ fireEventListener(e);
99+}
42100
101+// virtual
102+void Element::OnMouseMove(int16_t x, int16_t y)
103+{
104+ Event e;
105+ e.target = this;
106+ e.type = EventType_MouseMove;
107+ e.x = x;
108+ e.y = y;
109+ fireEventListener(e);
110+}
111+
112+} // namespace MG
--- a/mg/element.h
+++ b/mg/element.h
@@ -2,7 +2,6 @@
22
33 #include "renderer.h"
44 #include "optional.h"
5-#include "callback.hpp"
65 #include "varray.h"
76
87 namespace MG {
@@ -15,23 +14,20 @@ public:
1514 Element(size_t nMaxEvents);
1615 virtual ~Element() {}
1716
18- bool IsClickable() const {
19- return bClickable_;
20- }
21-
2217 virtual void Draw(int16_t offsetX, int16_t offsetY, IRenderer& renderer);
23- virtual bool HitTest(int16_t x, int16_t y) const { return false; }
24- virtual void OnMouseDown(int16_t x, int16_t y) {}
25- virtual void OnMouseUp(int16_t x, int16_t y) {}
26- virtual void OnMouseMove(int16_t x, int16_t y) {}
18+ virtual bool HitTest(int16_t x, int16_t y) const;
19+
20+ virtual void OnMouseDown(int16_t x, int16_t y);
21+ virtual void OnMouseUp(int16_t x, int16_t y);
22+ virtual void OnMouseMove(int16_t x, int16_t y);
2723
28- Rectangle rect; // positioning, size
29- bool needsToDraw;
24+ Rectangle rect_; // positioning, size
25+ bool needsToDraw_;
3026
3127 struct IBackground {
3228 virtual void Draw(const Rectangle& rect, IRenderer& renderer) = 0;
3329 };
34- IBackground* pBG;
30+ IBackground* pBG_;
3531
3632 Element* GetParent() const { return pParent_; }
3733 void SetParent(Element* pParent) { pParent_ = pParent; }
@@ -51,19 +47,20 @@ public:
5147 int16_t x;
5248 int16_t y;
5349 };
54- typedef util::Callback<void (const Event&)> EventListener;
50+ typedef void (*EventListener)(void* param, const Event& e);
5551 struct EventListenerEntry {
5652 int type;
5753 EventListener listener;
5854 };
55+ void* pEventListenerParam_;
5956 void AddEventListener(int type, const EventListener& listener);
6057 void RemoveEventListener(int type, const EventListener& listener);
61-
58+ static Element* s_pCapture_;
6259 protected:
63- bool bClickable_;
6460 Element* pParent_;
6561
6662 varray<EventListenerEntry> eventEntries_;
63+ void fireEventListener(const Event& e);
6764 };
6865
6966 } // namespace MG
--- a/mg/rectangle.cpp
+++ b/mg/rectangle.cpp
@@ -18,7 +18,7 @@ Rectangle Rectangle::Offset(const Rectangle& r, int16_t x, int16_t y)
1818 return ret;
1919 }
2020
21-bool Rectangle::Contains(int16_t x, int16_t y)
21+bool Rectangle::Contains(int16_t x, int16_t y) const
2222 {
2323 return
2424 1
@@ -29,7 +29,7 @@ bool Rectangle::Contains(int16_t x, int16_t y)
2929 ;
3030 }
3131
32-bool Rectangle::Contains(const Rectangle& r)
32+bool Rectangle::Contains(const Rectangle& r) const
3333 {
3434 return true;
3535 }
--- a/mg/rectangle.h
+++ b/mg/rectangle.h
@@ -19,8 +19,8 @@ struct Rectangle
1919 void Offset(int16_t x, int16_t y);
2020 static Rectangle Offset(const Rectangle& r, int16_t x, int16_t y);
2121
22- bool Contains(int16_t x, int16_t y);
23- bool Contains(const Rectangle& r);
22+ bool Contains(int16_t x, int16_t y) const;
23+ bool Contains(const Rectangle& r) const;
2424
2525 void Intersect(const Rectangle& r);
2626 static Rectangle Intersect(const Rectangle& r1, const Rectangle& r2);