1 /*
2 Copyright (C) 2007, 2010 - Bit-Blot
3 
4 This file is part of Aquaria.
5 
6 Aquaria is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 
15 See the 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 */
21 #ifndef __event__
22 #define __event__
23 
24 #include <stdlib.h>
25 
26 #include <list>
27 
28 
29 class Event
30 {
31 public:
Event()32 	Event() { data = 0; }
~Event()33 	virtual ~Event() {}
34 	virtual void act()=0;
35 	void *data;
36 };
37 
38 
39 template <class T>
40 class FunctionEvent : public Event
41 {
42 public:
FunctionEvent(T * who,void (T::* func)(void))43 	FunctionEvent(T *who,void (T::*func)(void)):
44 		callee(who),callback(func){}
45 
act()46 	void act()
47 	{
48 		(callee->*callback)();
49 	}
50 
51 private:
52 	T *callee;
53     void (T::*callback)(void);
54 };
55 
56 template <class T>
57 class FunctionEventWithData : public Event
58 {
59 public:
FunctionEventWithData(T * who,void (T::* func)(Event *))60 	FunctionEventWithData(T *who,void (T::*func)(Event*)):
61 		callee(who),callback(func){}
62 
act()63 	void act()
64 	{
65 		(callee->*callback)(this);
66 	}
67 
68 private:
69 	T *callee;
70     void (T::*callback)(Event *e);
71 };
72 
73 #define EVENT(x,y) class x : public Event { public: void act(); }; x y;
74 
75 #define MakeFunctionEvent(type,function) addCreatedEvent(new FunctionEvent<type>(this, &type::function))
76 #define MakeFunctionEventPointer(type,function,pointer) addCreatedEvent(new FunctionEvent<type>(pointer, &type::function))
77 #define FuncEvent(type,function) addCreatedEvent(new FunctionEventWithData<type>(this, &type::function))
78 
79 enum EventManage
80 {
81 	EVM_NONE	= 0,
82 	EVM_CLEANUP	= 1,
83 	EVM_MAX
84 };
85 
86 class ActionMapper;
87 
88 class EventPtr
89 {
90 public:
91 	EventPtr();
92 	~EventPtr();
93 	void call();
94 	void set(Event *e, EventManage _evm=EVM_NONE);
95 	const EventPtr& operator= (const EventPtr &e);
96 	void clean();
97 	void setActionMapperCallback(ActionMapper *actionMapperCallback, int actionValue, int stateToCall=0);
98 protected:
99 	Event *e;
100 	unsigned char _evm;  // Keep the structure size down.
evm()101 	inline EventManage evm() const {return EventManage(_evm);}
102 
103 	bool stateToCall;
104 
105 	ActionMapper *actionMapperCallback;
106 	int actionValue;
107 };
108 
109 
110 class Timer
111 {
112 public:
113 	Timer();
114 	void startStopWatch();
115 	void start(float t);
116 	void stop();
117 	bool isDone();
118 	bool isActive();
119 	void update(float dt);
120 	bool updateCheck(float dt);
121 	float getValue();
122 	float getPerc();
123 	EventPtr endEvent, startEvent;
124 protected:
125 	int running;
126 	float timer, time;
127 };
128 
129 class EventPulser
130 {
131 public:
132 	EventPulser();
133 
134 	void setInterval(float t);
135 
136 	void update(float dt);
137 
138 	float interval;
139 	float time;
140 
141 	int times;
142 
143 	int randomVariance;
144 
145 	EventPtr e;
146 };
147 
148 class EventTimer
149 {
150 public:
151 	EventTimer(const EventPtr &p, float time);
152 
153 	void update(float dt);
154 
155 	EventPtr eventPtr;
156 
157 	Timer timer;
158 };
159 
160 class EventQueue
161 {
162 public:
163 	EventQueue();
164 	void addEvent(const EventPtr &eventPtr, float t);
165 	void update(float dt);
166 	void clear();
167 	int getSize();
168 
169 private:
170 	typedef std::list<EventTimer> EventTimers;
171 	EventTimers eventTimers;
172 };
173 
174 #endif
175