diff mde/input/Input.d @ 132:264028f4115a

Cleaned up mde.imde and a couple of widget functions. New mde.menus module to add default menus. The input singleton is now created in mde.input.Input instead of mde.imde.
author Diggory Hardy <diggory.hardy@gmail.com>
date Fri, 23 Jan 2009 14:59:05 +0000
parents a2ef6b549101
children 4084f07f2c7a
line wrap: on
line diff
--- a/mde/input/Input.d	Wed Jan 21 13:01:40 2009 +0000
+++ b/mde/input/Input.d	Fri Jan 23 14:59:05 2009 +0000
@@ -143,15 +143,17 @@
     /** Adds a callback delegate for key events (both DOWN and UP) with this ID.
     *
     * Delegate receives event status. */
-    void addButtonCallback (inputID id, ButtonCallback dg) {
+    Input addButtonCallback (inputID id, ButtonCallback dg) {
         buttonCallbacks[id] ~= dg;
+        return this;
     }
 
     /** Adds a callback delegate for axis events with this ID.
     *
     * Delegate receives event status (as per what getAxis returns). */
-    void addAxisCallback (inputID id, AxisCallback dg) {
+    Input addAxisCallback (inputID id, AxisCallback dg) {
         axisCallbacks[id] ~= dg;
+        return this;
     }
 
     /** Adds a callback delegate for mouse motion/joystick ball events with this ID.
@@ -162,8 +164,9 @@
     * (A separate callback for mouse screen position changes is not
     * necessary since this will be triggered by the same event - use mouseScreenPos from within the
     * function to get new screen coordinates.) */
-    void addRelMotionCallback (inputID id, RelMotionCallback dg) {
+    Input addRelMotionCallback (inputID id, RelMotionCallback dg) {
         relMotionCallbacks[id] ~= dg;
+        return this;
     }
     
     /** Adds a callback delegate for all mouse clicks & releases.
@@ -175,16 +178,18 @@
     * The point of this over a standard button callback is firstly to avoid mouse configuration for
     * the GUI, and secondly to give the pointer position at the time of the event, not the time the
     * callback gets called. */
-    void addMouseClickCallback (MouseClickCallback dg) {
+    Input addMouseClickCallback (MouseClickCallback dg) {
         mouseClickCallbacks ~= dg;
+        return this;
     }
     
     /** Adds a callback delegate for all mouse motion events.
     *
     * Really just for graphical user interfaces. Use addRelMotionCallback for relative motion (for
     * manipulating 3D views, etc.). */
-    void addMouseMotionCallback (MouseMotionCallback dg) {
+    Input addMouseMotionCallback (MouseMotionCallback dg) {
         mouseMotionCallbacks ~= dg;
+        return this;
     }
     
     /** Sets a callback delegate to recieve key presses as a Utf-8 char[].
@@ -206,21 +211,22 @@
     }
 
     /** Feed an SDL_Event struct (only uses if it's a key, mouse or joystick event).
-    *
-    * Other types of event functions may be added. Returns true if the event was used, false if not
-    * or no config was available. Hmm... doesn't seem very useful, but has practically no cost.
-    * (Due to lack of use of this feature, false is returned even for used events when no config is
-    * available).
-    *
-    * May throw InputClassExceptions (on configuration errors). Catching the exception and continuing should
-    * be fine. */
-    bool opCall (ref SDL_Event event) {
+     *
+     * Other types of event functions may be added. Returns true if the event
+     * was used, false if not or no config was available. Hmm... doesn't seem
+     * very useful, but has practically no cost.
+     *
+     * May throw InputClassExceptions (on configuration errors). Catching the
+     * exception and continuing should be fine. */
+    bool send (ref SDL_Event event) {
         /* Non-config events.
         *
-        * Handle these first so that if no config exists some functionality at least is retained.
+        * Handle these first so that if no config exists some functionality at
+        * least is retained.
         *
         * Coordinates don't need adjusting (they put the top-left most pixel at 0,0).
-        */
+        * 
+        * If no config, exit from this switch. */
         switch (event.type) {
             case SDL_KEYDOWN:
                 if (letterCallback) {
@@ -252,12 +258,9 @@
                 break;
             
             default:
+                if (!config) return false;	// event not used
         }
-        
-        /* No config available, so don't try to access it and segfault.
-        * Don't log a message because this function is called per-event (i.e. frequently).
-        * A message should already have been logged by loadConfig anyway. */
-        if (!config) return false;
+        if (!config) return true;		// event used
         
         switch (event.type) {
             // Keyboard events:
@@ -388,11 +391,19 @@
         Config* c_p = profile in Config.configs;
         if (c_p) config = *c_p;
         else {
+            logger.error ("Config profile \""~profile~"\" not found: input won't work unless a valid profile is loaded!");
             throw new ConfigLoadException;
-            logger.error ("Config profile \""~profile~"\" not found: input won't work unless a valid profile is loaded!");
         }
     }
     
+    /** For now, use as a singleton. (Could be changed later to allow multiple
+     * "players". */
+    static Input singleton () {
+        if (instance is null)
+            instance = new Input();
+        return instance;
+    }
+    
 private:
     // Static constructor for event stream (fills es_*_fcts tables).
     static this () {
@@ -414,6 +425,7 @@
     
     static const CB_EXC = "Callback exception: ";
     
+    static Input instance;
     static Logger logger;
 
     Config config;		// Configuration