00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "event.h"
00023 #include "event_handler.h"
00024
00025
00026 event::event ()
00027 {
00028 Action = ACTION_NOTHING;
00029 Registered = false;
00030 Paused = false;
00031 Repeat = -1;
00032 Script = NULL;
00033 PyFunc = NULL;
00034 Args = NULL;
00035 List = NULL;
00036 Id = "";
00037 }
00038
00039
00040 event::~event ()
00041 {
00042
00043 if (Registered) event_handler::remove_event (this);
00044
00045
00046 if (List) List->remove_event (this);
00047
00048 clear ();
00049 }
00050
00051
00052 void event::clear ()
00053 {
00054 switch (Action)
00055 {
00056
00057 case ACTION_SCRIPT:
00058 {
00059 delete Script;
00060 Py_XDECREF (Args);
00061 Args = NULL;
00062 Script = NULL;
00063
00064 break;
00065 }
00066
00067
00068 case ACTION_PYFUNC:
00069 {
00070 delete PyFunc;
00071 PyFunc = NULL;
00072
00073 break;
00074 }
00075
00076 default: break;
00077 }
00078
00079 Action = ACTION_NOTHING;
00080 }
00081
00082
00083 void event::set_script (std::string filename, PyObject * args)
00084 {
00085
00086 clear ();
00087
00088 if (filename == "") return;
00089
00090 Py_XINCREF (args);
00091 Args = args;
00092
00093 u_int16 argssize = args == NULL ? 1 : PyTuple_Size (args) + 1;
00094 PyObject *theargs = PyTuple_New (argssize);
00095
00096
00097
00098 PyTuple_SetItem (theargs, 0, python::pass_instance (this, "event"));
00099 for (u_int16 i = 1; i < argssize; i++)
00100 {
00101 PyObject *intref = PyTuple_GetItem (args, i - 1);
00102 Py_INCREF (intref);
00103 PyTuple_SetItem (theargs, i, intref);
00104 }
00105
00106 Script = new py_object;
00107 Script->create_instance (EVENTS_DIR + filename, filename, theargs);
00108 Py_DECREF (theargs);
00109
00110 Action = ACTION_SCRIPT;
00111 }
00112
00113
00114 void event::set_callback (PyObject *callback, PyObject *args)
00115 {
00116
00117 clear ();
00118
00119
00120 PyFunc = new py_callback (callback, args);
00121
00122
00123 Action = ACTION_PYFUNC;
00124 }
00125
00126
00127 void event::set_callback (const Functor0 & callback)
00128 {
00129
00130 clear ();
00131
00132 Callback = callback;
00133 Action = ACTION_CPPFUNC;
00134 }
00135
00136
00137 void event::put_state (ogzstream & file) const
00138 {
00139 Type >> file;
00140
00141 Repeat >> file;
00142 Paused >> file;
00143 Action >> file;
00144
00145 switch (Action)
00146 {
00147
00148 case ACTION_SCRIPT:
00149 {
00150 Script->class_name () >> file;
00151
00152 if (Args)
00153 {
00154 true >> file;
00155 python::put_tuple (Args, file);
00156 }
00157 else false >> file;
00158
00159 return;
00160 }
00161
00162
00163 case ACTION_PYFUNC:
00164 {
00165 PyFunc->put_state (file);
00166 return;
00167 }
00168
00169 default: return;
00170 }
00171 }
00172
00173
00174 bool event::get_state (igzstream & file)
00175 {
00176
00177
00178
00179 Repeat << file;
00180 Paused << file;
00181 Action << file;
00182
00183 switch (Action)
00184 {
00185
00186 case ACTION_SCRIPT:
00187 {
00188 std::string name;
00189 bool has_args;
00190 PyObject * args = NULL;
00191
00192 name << file;
00193 has_args << file;
00194
00195 if (has_args) args = python::get_tuple (file);
00196
00197 set_script (name, args);
00198 Py_XDECREF (args);
00199
00200 return true;
00201 }
00202
00203
00204 case ACTION_PYFUNC:
00205 {
00206 PyFunc = new py_callback;
00207 return PyFunc->get_state (file);
00208 }
00209
00210 default: return true;
00211 }
00212
00213 return false;
00214 }
00215
00216
00217 void event::set_list (event_list *l)
00218 {
00219 List = l;
00220 }
00221
00222
00223 void event::pause ()
00224 {
00225 event_handler::remove_event (this);
00226 Paused = true;
00227 }
00228
00229
00230 void event::resume ()
00231 {
00232 event_handler::register_event (this);
00233 Paused = false;
00234 }
00235
00236
00237 s_int32 event::do_repeat ()
00238 {
00239 if (Repeat > 0) Repeat--;
00240
00241 return Repeat;
00242 }