adonthell-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Adonthell-commits] CVS: adonthell/src event.cc,1.12.2.4,1.12.2.5 event.


From: Kai Sterker <address@hidden>
Subject: [Adonthell-commits] CVS: adonthell/src event.cc,1.12.2.4,1.12.2.5 event.h,1.25.2.4,1.25.2.5 event_handler_base.h,1.1.2.1,1.1.2.2 gamedate.cc,1.1.2.5,1.1.2.6 py_base.i,1.1.2.4,1.1.2.5 py_callback.h,1.3,1.3.2.1 schedule.cc,1.1.2.2,1.1.2.3 time_event.cc,1.1.2.4,1.1.2.5 time_event.h,1.1.2.6,1.1.2.7
Date: Sat, 15 Jun 2002 16:23:26 -0400

Update of /cvsroot/adonthell/adonthell/src
In directory subversions:/tmp/cvs-serv28711

Modified Files:
      Tag: Branch_road_to_0-4
        event.cc event.h event_handler_base.h gamedate.cc py_base.i 
        py_callback.h schedule.cc time_event.cc time_event.h 
Log Message:
ADDED python callbacks as additional 'action' for events


Index: event.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/event.cc,v
retrieving revision 1.12.2.4
retrieving revision 1.12.2.5
diff -C2 -r1.12.2.4 -r1.12.2.5
*** event.cc    14 Jun 2002 08:55:41 -0000      1.12.2.4
--- event.cc    15 Jun 2002 20:23:24 -0000      1.12.2.5
***************
*** 29,33 ****
--- 29,36 ----
      Repeat = -1;
      Shared = false;
+     Script = NULL;
+     PyFunc = NULL;
      Args = NULL;
+     Action = ACTION_NOTHING;
  }
  
***************
*** 41,87 ****
  void event::clear ()
  {
!     Script.clear ();
!     Py_XDECREF (Args);
!     Args = NULL;
  }
  
! // set the script asspciated with the event
  void event::set_script (string filename, PyObject * args = NULL)
  {
!     if (filename == "") clear ();
!     else 
!     {
!         Py_XINCREF (args);
!         Args = args; 
!         Shared = false;
         
!         u_int16 argssize = args == NULL ? 1 : PyTuple_Size (args) + 1; 
!         PyObject *theargs = PyTuple_New (argssize);
          
!         // We can pass_instance directly 'cause PyTuple_SetItem steals a
!         // reference to the result of pass_instance.
!         PyTuple_SetItem (theargs, 0, python::pass_instance (this, "event"));
!         for (u_int16 i = 1; i < argssize; i++)
!         {
!             PyObject *intref = PyTuple_GetItem (args, i - 1);
!             Py_INCREF (intref); 
!             PyTuple_SetItem (theargs, i, intref); 
!         }
!         Script.create_instance (EVENTS_DIR + filename, filename, theargs);
!         Py_DECREF (theargs);
      }
  }
  
! // make the event script a reference to an existing script
! void event::set_shared_script (py_object &script)
  {
      // cleanup
      clear ();
  
!     // 'clone' the given script
      Script = script;
      
      // tell the script not to save any arguments
      Shared = true;
  }
  
--- 44,137 ----
  void event::clear ()
  {
!     switch (Action)
!     {
!         // script attached
!         case ACTION_SCRIPT:
!         {
!             if (!Shared)
!             {
!                 delete Script;
!                 Py_XDECREF (Args);
!                 Args = NULL;
!             }
!             else Script = NULL;
!             
!             break;
!         }
!         
!         // python callback attached
!         case ACTION_PYFUNC:
!         {
!             delete PyFunc;
!             PyFunc = NULL;
!             
!             break;
!         }
!         
!         default: break;
!     }
!     
!     Action = ACTION_NOTHING;
  }
  
! // set a script as event's action
  void event::set_script (string filename, PyObject * args = NULL)
  {
!     // cleanup
!     clear ();
!     
!     if (filename == "") return;
!     
!     Py_XINCREF (args);
!     Args = args; 
!     Shared = false;
         
!     u_int16 argssize = args == NULL ? 1 : PyTuple_Size (args) + 1; 
!     PyObject *theargs = PyTuple_New (argssize);
          
!     // We can pass_instance directly 'cause PyTuple_SetItem steals a
!     // reference to the result of pass_instance.
!     PyTuple_SetItem (theargs, 0, python::pass_instance (this, "event"));
!     for (u_int16 i = 1; i < argssize; i++)
!     {
!         PyObject *intref = PyTuple_GetItem (args, i - 1);
!         Py_INCREF (intref); 
!         PyTuple_SetItem (theargs, i, intref); 
      }
+     
+     Script = new py_object;
+     Script->create_instance (EVENTS_DIR + filename, filename, theargs);
+     Py_DECREF (theargs);
+         
+     Action = ACTION_SCRIPT;
  }
  
! // make the event script a pointer to an existing script
! void event::set_shared_script (py_object * script)
  {
      // cleanup
      clear ();
  
!     // attach the given script
      Script = script;
      
      // tell the script not to save any arguments
      Shared = true;
+ 
+     // tell the event what to do    
+     Action = ACTION_SCRIPT;
+ }
+ 
+ // set a callback as event's action
+ void event::set_callback (PyObject *callback, PyObject *args)
+ {
+     // cleanup
+     clear ();
+     
+     // create the callback
+     PyFunc = new py_callback (callback, args);
+     
+     // tell the event what to do    
+     Action = ACTION_PYFUNC;
  }
  
***************
*** 91,99 ****
      Type >> file;
      Repeat >> file;
      Shared >> file;
      
      if (Shared) return;
      
!     Script.class_name () >> file;
      
      if (Args)
--- 141,153 ----
      Type >> file;
      Repeat >> file;
+     Action >> file;
+     
+     if (Action != ACTION_SCRIPT) return;
+     
      Shared >> file;
      
      if (Shared) return;
      
!     Script->class_name () >> file;
      
      if (Args)
***************
*** 115,118 ****
--- 169,176 ----
      // determine what event subclass to instanciate
      Repeat << file;
+     Action << file;
+     
+     if (Action != ACTION_SCRIPT) return true;
+     
      Shared << file;
      

Index: event.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/event.h,v
retrieving revision 1.25.2.4
retrieving revision 1.25.2.5
diff -C2 -r1.25.2.4 -r1.25.2.5
*** event.h     14 Jun 2002 08:55:41 -0000      1.25.2.4
--- event.h     15 Jun 2002 20:23:24 -0000      1.25.2.5
***************
*** 17,21 ****
   * @author Kai Sterker <address@hidden>
   * 
!  * @brief  Declares the event class.
   * 
   */
--- 17,21 ----
   * @author Kai Sterker <address@hidden>
   * 
!  * @brief  Declares the %event class.
   * 
   */
***************
*** 26,37 ****
  #include "fileops.h" 
  #include "py_object.h"
  
  /**
!  * Directory where event scripts reside.
   */ 
  #define EVENTS_DIR "events."
  
  /**
!  * Available Event types.
   */ 
  enum
--- 26,39 ----
  #include "fileops.h" 
  #include "py_object.h"
+ #include "py_callback.h"
  
  /**
!  * Directory where %event scripts reside.
   */ 
  #define EVENTS_DIR "events."
  
+ #ifndef SWIG
  /**
!  * Available %event types.
   */ 
  enum
***************
*** 45,51 ****
  
  /**
!  * Base class for events. You can create your own event types that can
!  * be handled by the event list and event handler by inheriting them from
   * this class.
   */ 
  class event
--- 47,68 ----
  
  /**
!  * Available 'actions', i.e. what happens when the event occurs
!  */
! enum
! {
!     ACTION_NOTHING  = 0,
!     ACTION_SCRIPT   = 1,
!     ACTION_PYFUNC   = 2
! };
! #endif // SWIG
!     
! /**
!  * Base class for events. You can create your own %event types that can
!  * be handled by the event_list and event_handler by inheriting from
   * this class.
+  *
+  * Events are used to notify when certain things happen during the game.
+  * They may either execute the "run" method of an exclusive python script
+  * or a simple python callback defined elsewhere.
   */ 
  class event
***************
*** 53,57 ****
  public:
      /**
!      * Constructor
       */
      event ();
--- 70,74 ----
  public:
      /**
!      * Constructor. Needs to be called by any derived class!
       */
      event ();
***************
*** 108,114 ****
      
      /**
!      * Execute the associated python script.
       * 
!      * @param evnt The event that triggered the execution.
       */ 
      virtual void execute (const event& evnt) = 0;
--- 125,131 ----
      
      /**
!      * Execute the associated python script or callback.
       * 
!      * @param evnt The %event that triggered the execution.
       */ 
      virtual void execute (const event& evnt) = 0;
***************
*** 117,121 ****
       * Compare two events for equality.
       * 
!      * @param evnt pointer to the event to compare with.
       * @return \e true if the events are equal, \e false otherwise.
       */
--- 134,138 ----
       * Compare two events for equality.
       * 
!      * @param evnt pointer to the %event to compare with.
       * @return \e true if the events are equal, \e false otherwise.
       */
***************
*** 125,129 ****
      
      /** 
!      * Sets the script to be executed whenever the event occurs.
       * 
       * @param filename filename of the script to set.
--- 142,146 ----
      
      /** 
!      * Sets a script to be executed whenever the event occurs.
       * 
       * @param filename filename of the script to set.
***************
*** 133,137 ****
      
      /**
!      * Sets the script to be executed whenever the event occurs.
       * This method allows to specify a script that is already
       * in use elsewhere.
--- 150,154 ----
      
      /**
!      * Sets a script to be executed whenever the event occurs.
       * This method allows to specify a script that is already
       * in use elsewhere.
***************
*** 142,150 ****
       * the original owner has to supply the event with the script.
       *
!      * @param script reference to a script initialized elsewhere.
       */
!     void set_shared_script (py_object & script);
      
      /**
       * @name Loading / Saving
       */
--- 159,179 ----
       * the original owner has to supply the event with the script.
       *
!      * @param script pointer to a script initialized elsewhere.
       */
!     void set_shared_script (py_object * script);
      
      /**
+      * Sets a python function/method to be executed whenever the
+      * event occurs.
+      *
+      * @warning the callback won't be saved with the %event. It
+      * must be restored by the event's owner.
+      *
+      * @param callback The function or method to call.
+      * @param args Additional arguments to pass to the callback.
+      */
+     void set_callback (PyObject *callback, PyObject *args = NULL);
+      
+     /**
       * @name Loading / Saving
       */
***************
*** 183,186 ****
--- 212,220 ----
  
      /**
+      * What happens if the event occurs - see enum above.
+      */
+     u_int8 Action;
+     
+     /**
       * For events that share their script with another class, Shared
       * has to be set <b>true</b>. This prevents the script from getting
***************
*** 200,204 ****
       * whenever the %event gets triggered.
       */
!     py_object Script; 
  
      /**
--- 234,238 ----
       * whenever the %event gets triggered.
       */
!     py_object *Script; 
  
      /**
***************
*** 208,211 ****
--- 242,249 ----
      PyObject *Args;
      
+     /**
+      * Python Callback that may be executed instead of the script.
+      */
+     py_callback *PyFunc;
      //@}
  #endif // SWIG

Index: event_handler_base.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/Attic/event_handler_base.h,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -C2 -r1.1.2.1 -r1.1.2.2
*** event_handler_base.h        15 Jun 2002 09:37:16 -0000      1.1.2.1
--- event_handler_base.h        15 Jun 2002 20:23:24 -0000      1.1.2.2
***************
*** 31,34 ****
--- 31,36 ----
  {
  public:
+     virtual ~event_handler_base () { }
+ 
      /**
       */

Index: gamedate.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/Attic/gamedate.cc,v
retrieving revision 1.1.2.5
retrieving revision 1.1.2.6
diff -C2 -r1.1.2.5 -r1.1.2.6
*** gamedate.cc 14 Jun 2002 08:55:41 -0000      1.1.2.5
--- gamedate.cc 15 Jun 2002 20:23:24 -0000      1.1.2.6
***************
*** 20,23 ****
--- 20,24 ----
   */
  
+ #include <stdlib.h>
  #include "gamedate.h"
  #include "gametime.h"

Index: py_base.i
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/Attic/py_base.i,v
retrieving revision 1.1.2.4
retrieving revision 1.1.2.5
diff -C2 -r1.1.2.4 -r1.1.2.5
*** py_base.i   14 Jun 2002 08:55:41 -0000      1.1.2.4
--- py_base.i   15 Jun 2002 20:23:24 -0000      1.1.2.5
***************
*** 6,9 ****
--- 6,11 ----
  #include "game.h"
  #include "event.h"
+ #include "time_event.h"
+ #include "event_handler.h"
  #include "fileops.h"
  #include "gamedata.h"
***************
*** 20,23 ****
--- 22,27 ----
  
  %include "event.h"
+ %include "time_event.h"
+ %include "event_handler.h"
  %include "game.h"
  %include "fileops.h"

Index: py_callback.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/py_callback.h,v
retrieving revision 1.3
retrieving revision 1.3.2.1
diff -C2 -r1.3 -r1.3.2.1
*** py_callback.h       23 Sep 2001 14:05:45 -0000      1.3
--- py_callback.h       15 Jun 2002 20:23:24 -0000      1.3.2.1
***************
*** 59,64 ****
       * Calls the python function and returns bool.
       * 
       */
- 
      bool callback_func0ret (); 
  
--- 59,64 ----
       * Calls the python function and returns bool.
       * 
+      * @return the result of the callback function.
       */
      bool callback_func0ret (); 
  
***************
*** 66,76 ****
       * Calls the python function with an integer.
       * 
       */ 
!     void callback_func1 (int);
  
  private:
- 
      /**
!      * TThe actual function call
       *
       */
--- 66,76 ----
       * Calls the python function with an integer.
       * 
+      * @param arg Integer argument to pass to the callback. 
       */ 
!     void callback_func1 (int arg);
  
  private:
      /**
!      * The actual function call
       *
       */

Index: schedule.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/Attic/schedule.cc,v
retrieving revision 1.1.2.2
retrieving revision 1.1.2.3
diff -C2 -r1.1.2.2 -r1.1.2.3
*** schedule.cc 14 Jun 2002 08:55:41 -0000      1.1.2.2
--- schedule.cc 15 Jun 2002 20:23:24 -0000      1.1.2.3
***************
*** 79,83 ****
      
      // update the alarm with the new manager script
!     if (Alarm) Alarm->set_shared_script (Manager);
  }
  
--- 79,83 ----
      
      // update the alarm with the new manager script
!     if (Alarm) Alarm->set_shared_script (&Manager);
  }
  
***************
*** 109,114 ****
      // create and register the new alarm
      Alarm = new time_event (time, absolute);
!     Alarm->event::set_repeat (0);
!     Alarm->set_shared_script (Manager);
      event_handler::register_event (Alarm);
  }
--- 109,113 ----
      // create and register the new alarm
      Alarm = new time_event (time, absolute);
!     Alarm->set_shared_script (&Manager);
      event_handler::register_event (Alarm);
  }
***************
*** 166,170 ****
          Alarm = new time_event;
          Alarm->get_state (file);
!         Alarm->set_shared_script (Manager);
          
          // don't forget to register the alarm!
--- 165,169 ----
          Alarm = new time_event;
          Alarm->get_state (file);
!         Alarm->set_shared_script (&Manager);
          
          // don't forget to register the alarm!

Index: time_event.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/Attic/time_event.cc,v
retrieving revision 1.1.2.4
retrieving revision 1.1.2.5
diff -C2 -r1.1.2.4 -r1.1.2.5
*** time_event.cc       14 Jun 2002 08:55:41 -0000      1.1.2.4
--- time_event.cc       15 Jun 2002 20:23:24 -0000      1.1.2.5
***************
*** 20,29 ****
   */
  
- #include "stdlib.h"
  #include "time_event.h"
  #include "gamedate.h"
  
  // create a new time event
! time_event::time_event (const string & time, bool absolute)
  {
      Repeat = 0;
--- 20,28 ----
   */
  
  #include "time_event.h"
  #include "gamedate.h"
  
  // create a new time event
! time_event::time_event (const string & time, bool absolute) : event ()
  {
      Repeat = 0;
***************
*** 45,49 ****
      // nothing needs be passed to the script; it can get the
      // current time from the gametime class if it is needed.
!     Script.run ();
      
      // when the script needs be repeated, do so.
--- 44,63 ----
      // nothing needs be passed to the script; it can get the
      // current time from the gametime class if it is needed.
!     switch (Action)
!     {
!         case ACTION_SCRIPT:
!         {
!             Script->run ();
!             break;
!         }
!         
!         case ACTION_PYFUNC:
!         {
!             PyFunc->callback_func0 ();
!             break;
!         }
!     
!         default: return;
!     }
      
      // when the script needs be repeated, do so.

Index: time_event.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/Attic/time_event.h,v
retrieving revision 1.1.2.6
retrieving revision 1.1.2.7
diff -C2 -r1.1.2.6 -r1.1.2.7
*** time_event.h        14 Jun 2002 08:55:41 -0000      1.1.2.6
--- time_event.h        15 Jun 2002 20:23:24 -0000      1.1.2.7
***************
*** 69,73 ****
       * @param time The "alarm" time in %gametime minutes.
       */
!     time_event (const u_int32 & time)
      {
          Type = TIME_EVENT;
--- 69,73 ----
       * @param time The "alarm" time in %gametime minutes.
       */
!     time_event (const u_int32 & time) : event ()
      {
          Type = TIME_EVENT;




reply via email to

[Prev in Thread] Current Thread [Next in Thread]