core/vgui/vgui_event_condition.h
Go to the documentation of this file.
00001 // This is core/vgui/vgui_event_condition.h
00002 #ifndef vgui_event_condition_h_
00003 #define vgui_event_condition_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \author fsm
00010 // \brief  Represent and recognise simple event conditions.
00011 //
00012 // \verbatim
00013 //  Modifications
00014 //   04-OCT-2002 K.Y.McGaul - Added doxygen style documentation.
00015 //                          - Check for impossible events in new init function.
00016 //                          - key is now always lower case.
00017 //   22-OCT-2002 A.Fitzgibbon & K.Y.McGaul - Added constructor for ascii_char
00018 //                                           event conditions.
00019 //   05-DEC-2002 K.Y.McGaul - Added Awf's comments from mailing list.
00020 // \endverbatim
00021 
00022 #include <vcl_string.h>
00023 #include <vgui/vgui_key.h>
00024 #include <vgui/vgui_button.h>
00025 #include <vgui/vgui_modifier.h>
00026 class vgui_event;
00027 
00028 //: Represent and recognise simple event conditions.
00029 //
00030 //  This makes it easy to change the key/mouse combination that causes
00031 //  one's tableau to do something.
00032 //
00033 //  The key is now always lower case. ascii_char contains the
00034 //  actual character returned by the keyboard.  To construct a
00035 //  vgui_event_condition to detect a SHIFT+b event you can do either:
00036 // \code
00037 //       vgui_event_condition my_ec(vgui_key('b'), vgui_SHIFT);
00038 // \endcode
00039 //  or
00040 // \code
00041 //       vgui_event_condition my_ec(vgui_key('B');
00042 // \endcode
00043 //
00044 //  The point about vgui_event_condition is that it should not be
00045 //  constructed just before you check the event, but placed in a
00046 //  standard place in the object to
00047 //
00048 //  (a) Make it easy to see the list of handled events
00049 //
00050 //  (b) Make it easy to change the keys/mouse gestures to which
00051 //      an action is bound.  For example, if my 3D viewer tableau
00052 //      uses left mouse to rotate in 3D, and I wish to put it into
00053 //      a 2D zoomer tableau which uses left mouse to zoom, I need
00054 //      change one of them. If you don't use vgui_event_condition,
00055 //      you need to edit the code, which means other people can't
00056 //      use left mouse.
00057 //
00058 //  Concrete example:  my_tableau spins a 3D model
00059 //
00060 // \code
00061 //  struct my_tableau
00062 //  {
00063 //    void handle(vgui_event);
00064 //
00065 //    vgui_event_condition c_rotate;     // event which initiates rotating
00066 //    vgui_event_condition c_translate;
00067 //    vgui_event_condition c_scale;
00068 //    vgui_event_condition c_zoom;
00069 //  };
00070 // \endcode
00071 //
00072 //  Note that the event conditions are stored in the object, not hard-coded
00073 //  in the handle routine.  This means they can be changed by client code
00074 //  without modifying my_tableau.cxx
00075 //
00076 //  One might think it would be better to have all tableaux to use unique
00077 //  event codes, and maybe it is for the core tableaux, but we can't have
00078 //  someone use ctrl+shift+3rd button because it's the only one free, when
00079 //  for their application (movie player shuttle action?) it may be the
00080 //  application's most natural action, and should be on left-mouse.
00081 
00082 struct vgui_event_condition
00083 {
00084   enum event_types { null_event, mouse_event, ascii_char_event, key_event };
00085 
00086   bool on;
00087   bool pressed;
00088   vgui_key key;
00089   vgui_key ascii_char;
00090   vgui_button button;
00091   vgui_modifier modifier;
00092   event_types how_checked;
00093 
00094   //: Initialise event condition and check for impossible events.
00095   void init(vgui_key k, vgui_key ascii_char, vgui_button b,
00096             vgui_modifier m, bool is_pressed, bool is_on, event_types use_event);
00097 
00098   //: Constructor - create a default event condition.
00099   // This type of event condition will never be triggered.
00100   vgui_event_condition();
00101 
00102   //: Constructor for a key press event condition (using ascii char).
00103   vgui_event_condition(vgui_key ascii_code, bool is_pressed = true);
00104 
00105   //: Constructor for a key press event condition (using key and modifier).
00106   vgui_event_condition(vgui_key k, vgui_modifier m,  bool is_pressed = true);
00107 
00108   //: Constructor for a mouse button press event condition.
00109   vgui_event_condition(vgui_button b, vgui_modifier m = vgui_MODIFIER_NULL,
00110                        bool is_pressed = true);
00111 
00112   void enable(bool v = true) { on = v; }
00113   void disable(bool v = true) { on = !v; }
00114 
00115   //: E.g. if (c_mouse_spin(e))
00116   bool operator()(vgui_event const &e) const;
00117   bool operator()(vgui_key k, vgui_modifier m) const;
00118   bool operator()(vgui_button b, vgui_modifier m) const;
00119 
00120   //: Text representation such as "shift-middle" or "ctrl-K".
00121   // If field_width is supplied, pad to that width.
00122   vcl_string as_string(int field_width = 0) const;
00123 };
00124 
00125 #endif // vgui_event_condition_h_