GL Studio C++ Runtime API
input_handler.h
Go to the documentation of this file.
1 /*! \file
2  \brief The input handler interface
3 
4  \par Copyright Information
5 
6  Copyright (c) 2016 by The DiSTI Corporation.<br>
7  11301 Corporate Blvd; Suite 100<br>
8  Orlando, Florida 32817<br>
9  USA<br>
10  <br>
11  All rights reserved.<br>
12 
13  This Software contains proprietary trade secrets of DiSTI and may not be
14 reproduced, in whole or part, in any form, or by any means of electronic,
15 mechanical, or otherwise, without the written permission of DiSTI. Said
16 permission may be derived through the purchase of applicable DiSTI product
17 licenses which detail the distribution rights of this content and any
18 Derivative Works based on this or other copyrighted DiSTI Software.
19 
20  NO WARRANTY. THE SOFTWARE IS PROVIDED "AS-IS," WITHOUT WARRANTY OF ANY KIND,
21 AND ANY USE OF THIS SOFTWARE PRODUCT IS AT YOUR OWN RISK. TO THE MAXIMUM EXTENT
22 PERMITTED BY APPLICABLE LAW, DISTI AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES
23 AND CONDITIONS, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
24 IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY AND/OR FITNESS FOR A
25 PARTICULAR PURPOSE, TITLE, AND NON-INFRINGEMENT, WITH REGARD TO THE SOFTWARE.
26 
27  LIMITATION OF LIABILITY. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW,
28 IN NO EVENT SHALL DISTI OR ITS SUPPLIERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
29 INDIRECT, OR CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION,
30 DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS
31 INFORMATION, OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR
32 INABILITY TO USE THE SOFTWARE, EVEN IF DISTI HAS BEEN ADVISED OF THE POSSIBILITY
33 OF SUCH DAMAGES. DISTI'S ENTIRE LIABILITY AND YOUR EXCLUSIVE REMEDY SHALL NOT
34 EXCEED FIVE DOLLARS (US$5.00).
35 
36  The aforementioned terms and restrictions are governed by the laws of the
37 State of Florida and the United States of America.
38 
39 */
40 #ifndef INCLUDED_INPUT_HANDLER_H
41 #define INCLUDED_INPUT_HANDLER_H
42 
43 #include "disti_assert.h"
44 #include "gls_cpp_lang_support.h"
45 #include "weak_reference.h"
46 
47 namespace disti
48 {
49 class DisplayObject;
50 class DisplayEvent;
51 class KeyboardEvent;
52 class MouseEvent;
53 
54 /** Class used to call a callback method that takes a KeyboardEvent */
56 {
57 public:
58  /** call the callback with the provided display event */
59  virtual void Call( KeyboardEvent* ev ) = 0;
60 
61  /** returns true if the callback is valid to call */
62  virtual bool IsValid() const = 0;
63 
64  virtual ~KeyboardCallbackBase() {}
65 };
66 
67 /** Interface for creating a callback from a class method. The class must implement WeakReferenceable.
68  * Don't use this directly. Use CreateInputHandlerCallback instead
69  */
70 template<class T>
72 {
73 public:
74  typedef void ( T::*Callback )( KeyboardEvent* ev );
75 
76  KeyboardMethodCallback( Callback method, T* object )
77  : _method( method )
78  , _object( object )
79  {
81  GLS_VERIFY( NULL != method );
82  GLS_VERIFY( NULL != object );
83  }
84 
85  void Call( KeyboardEvent* ev )
86  {
87  if( IsValid() )
88  {
89  ( _object.Get()->*( _method ) )( ev );
90  }
91  }
92 
93  virtual bool IsValid() const
94  {
95  return !_object.IsNull();
96  }
97 
98 protected:
99  Callback _method;
100  WeakRef<T> _object;
101 };
102 
103 /** helper method to create a KeyboardCallbackBase from a class method
104  * \param method a class method pointer
105  * \param obj the object to call the method on
106  */
107 template<class Class>
109  const typename KeyboardMethodCallback<Class>::Callback method,
110  Class* const obj )
111 {
112  return new KeyboardMethodCallback<Class>( method, obj );
113 }
114 
115 /** Class used to call a callback method that takes a MouseEvent */
117 {
118 public:
119  /** call the callback with the provided display event */
120  virtual void Call( MouseEvent* ev ) = 0;
121 
122  /** returns true if the callback is valid to call */
123  virtual bool IsValid() const = 0;
124 
125  virtual ~MouseCallbackBase() {}
126 };
127 
128 /** Interface for creating a callback from a class method. The class must implement WeakReferenceable.
129  * Don't use this directly. Use CreateInputHandlerCallback instead
130  */
131 template<class T>
133 {
134 public:
135  typedef void ( T::*Callback )( MouseEvent* ev );
136 
137  MouseMethodCallback( Callback method, T* object )
138  : _method( method )
139  , _object( object )
140  {
142  GLS_VERIFY( NULL != method );
143  GLS_VERIFY( NULL != object );
144  }
145 
146  void Call( MouseEvent* ev )
147  {
148  if( IsValid() )
149  {
150  ( _object.Get()->*( _method ) )( ev );
151  }
152  }
153 
154  virtual bool IsValid() const
155  {
156  return !_object.IsNull();
157  }
158 
159 protected:
160  Callback _method;
161  WeakRef<T> _object;
162 };
163 
164 /** helper method to create a MouseCallbackBase from a class method
165  * \param method a class method pointer
166  * \param obj the object to call the method on
167  */
168 template<class Class>
170  const typename MouseMethodCallback<Class>::Callback method,
171  Class* const obj )
172 {
173  return new MouseMethodCallback<Class>( method, obj );
174 }
175 
176 /** Base class implemented by all input handlers */
178 {
179 public:
180  /// Type for unique identifiers
181  typedef unsigned int ID;
182 
183  /** Handle the event
184  * \param ev the event to handle
185  * \return the object that handled the event, or NULL if no object handled the event
186  */
187  virtual DisplayObject* HandleInput( DisplayEvent* ev ) = 0;
188 
189  /** Register a global keyboard handler.
190  * \param callback the callback to call when a keyboard event occurs. The input handler owns the callback and will delete it.
191  * \return unique ID. Used to unregister.
192  */
193  virtual ID RegisterGlobalKeyboardHandler( KeyboardCallbackBase* callback ) = 0;
194 
195  /** Register a global mouse handler.
196  * \param callback the callback to call when a mouse event occurs. The input handler owns the callback and will delete it.
197  * \return unique ID. Used to unregister.
198  */
199  virtual ID RegisterGlobalMouseHandler( MouseCallbackBase* callback ) = 0;
200 
201  /** Unregister a global keyboard handler.
202  * \param id the unique identifier returned from RegisterGlobalKeyboardHandler.
203  */
204  virtual void UnregisterGlobalKeyboardHandler( ID id ) = 0;
205 
206  /** Unregister a global mouse handler.
207  * \param id the unique identifier returned from RegisterGlobalMouseHandler.
208  */
209  virtual void UnregisterGlobalMouseHandler( ID id ) = 0;
210 
211  /** destructor */
212  virtual ~InputHandler() {}
213 };
214 
215 } // namespace disti
216 
217 #endif
virtual DisplayObject * HandleInput(DisplayEvent *ev)=0
virtual void UnregisterGlobalKeyboardHandler(ID id)=0
void Call(MouseEvent *ev)
Definition: input_handler.h:146
virtual ID RegisterGlobalMouseHandler(MouseCallbackBase *callback)=0
KeyboardCallbackBase * CreateInputHandlerCallback(const typename KeyboardMethodCallback< Class >::Callback method, Class *const obj)
Definition: input_handler.h:108
Definition: events.h:272
Definition: input_handler.h:132
virtual void Call(MouseEvent *ev)=0
Definition: display.h:98
virtual bool IsValid() const =0
unsigned int ID
Type for unique identifiers.
Definition: input_handler.h:181
virtual void UnregisterGlobalMouseHandler(ID id)=0
virtual ID RegisterGlobalKeyboardHandler(KeyboardCallbackBase *callback)=0
virtual void Call(KeyboardEvent *ev)=0
virtual bool IsValid() const
Definition: input_handler.h:93
Definition: weak_reference.h:64
void Call(KeyboardEvent *ev)
Definition: input_handler.h:85
#define GLS_VERIFY(exp)
Definition: disti_assert.h:155
Definition: input_handler.h:71
Definition: input_handler.h:177
#define DISTI_STATIC_ASSERT_IS_CONVERTIBLE_TO(T, ConvertsTo)
Definition: gls_cpp_lang_support.h:323
Definition: events.h:111
virtual bool IsValid() const
Definition: input_handler.h:154
Definition: weak_reference.h:91
virtual ~InputHandler()
Definition: input_handler.h:212
virtual bool IsValid() const =0
Contains the DistiAssert macro.
Macros and helper code to determine what subset of C++11/14/17 is available.
Definition: bmpimage.h:46
Definition: input_handler.h:116
Definition: events.h:219
weak reference and related classes
Definition: input_handler.h:55