Adonthell  0.4
event.cc
Go to the documentation of this file.
1 /*
2  $Id: event.cc,v 1.23 2003/02/10 20:01:13 ksterker Exp $
3 
4  Copyright (C) 2000/2001/2002/2003 Kai Sterker <kaisterker@linuxgames.com>
5  Part of the Adonthell Project http://adonthell.linuxgames.com
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file event.cc
17  * @author Kai Sterker <kaisterker@linuxgames.com>
18  *
19  * @brief Defines the base event class.
20  */
21 
22 #include "event.h"
23 #include "event_handler.h"
24 
25 // constructor
27 {
28  Action = ACTION_NOTHING;
29  Registered = false;
30  Paused = false;
31  Repeat = -1;
32  Script = NULL;
33  PyFunc = NULL;
34  Args = NULL;
35  List = NULL;
36  Id = "";
37 }
38 
39 // destructor
41 {
42  // automatically remove myself from the event_handler
44 
45  // ... and from the event_list
46  if (List) List->remove_event (this);
47 
48  clear ();
49 }
50 
51 // cleanup
52 void event::clear ()
53 {
54  switch (Action)
55  {
56  // script attached
57  case ACTION_SCRIPT:
58  {
59  delete Script;
60  Py_XDECREF (Args);
61  Args = NULL;
62  Script = NULL;
63 
64  break;
65  }
66 
67  // python callback attached
68  case ACTION_PYFUNC:
69  {
70  delete PyFunc;
71  PyFunc = NULL;
72 
73  break;
74  }
75 
76  default: break;
77  }
78 
79  Action = ACTION_NOTHING;
80 }
81 
82 // set a script as event's action
83 void event::set_script (std::string filename, PyObject * args)
84 {
85  // cleanup
86  clear ();
87 
88  if (filename == "") return;
89 
90  Py_XINCREF (args);
91  Args = args;
92 
93  u_int16 argssize = args == NULL ? 1 : PyTuple_Size (args) + 1;
94  PyObject *theargs = PyTuple_New (argssize);
95 
96  // We can pass_instance directly 'cause PyTuple_SetItem steals a
97  // reference to the result of pass_instance.
98  PyTuple_SetItem (theargs, 0, python::pass_instance (this, "event"));
99  for (u_int16 i = 1; i < argssize; i++)
100  {
101  PyObject *intref = PyTuple_GetItem (args, i - 1);
102  Py_INCREF (intref);
103  PyTuple_SetItem (theargs, i, intref);
104  }
105 
106  Script = new py_object;
107  Script->create_instance (EVENTS_DIR + filename, filename, theargs);
108  Py_DECREF (theargs);
109 
110  Action = ACTION_SCRIPT;
111 }
112 
113 // set a python callback as event's action
114 void event::set_callback (PyObject *callback, PyObject *args)
115 {
116  // cleanup
117  clear ();
118 
119  // create the callback
120  PyFunc = new py_callback (callback, args);
121 
122  // tell the event what to do
123  Action = ACTION_PYFUNC;
124 }
125 
126 // set a C/C++ callback as event's action
127 void event::set_callback (const Functor0 & callback)
128 {
129  // cleanup
130  clear ();
131 
132  Callback = callback;
133  Action = ACTION_CPPFUNC;
134 }
135 
136 // save the state of the script associated with the event
137 void event::put_state (ogzstream & file) const
138 {
139  Type >> file;
140  // Id >> file;
141  Repeat >> file;
142  Paused >> file;
143  Action >> file;
144 
145  switch (Action)
146  {
147  // save script
148  case ACTION_SCRIPT:
149  {
150  Script->class_name () >> file;
151 
152  if (Args)
153  {
154  true >> file;
155  python::put_tuple (Args, file);
156  }
157  else false >> file;
158 
159  return;
160  }
161 
162  // save python callback
163  case ACTION_PYFUNC:
164  {
165  PyFunc->put_state (file);
166  return;
167  }
168 
169  default: return;
170  }
171 }
172 
173 // load the state of the script associated with the event
175 {
176  // Note that »Type« is already read by event_list::load to
177  // determine what event subclass to instanciate
178  // Id << file;
179  Repeat << file;
180  Paused << file;
181  Action << file;
182 
183  switch (Action)
184  {
185  // load script from file
186  case ACTION_SCRIPT:
187  {
188  std::string name;
189  bool has_args;
190  PyObject * args = NULL;
191 
192  name << file;
193  has_args << file;
194 
195  if (has_args) args = python::get_tuple (file);
196 
197  set_script (name, args);
198  Py_XDECREF (args);
199 
200  return true;
201  }
202 
203  // load python callback from file
204  case ACTION_PYFUNC:
205  {
206  PyFunc = new py_callback;
207  return PyFunc->get_state (file);
208  }
209 
210  default: return true;
211  }
212 
213  return false;
214 }
215 
216 // the event_list this event is kept in
218 {
219  List = l;
220 }
221 
222 // disable the event temporarily
224 {
226  Paused = true;
227 }
228 
229 // resume a disabled event
231 {
233  Paused = false;
234 }
235 
236 // repeat an event
238 {
239  if (Repeat > 0) Repeat--;
240 
241  return Repeat;
242 }