GL Studio C++ Runtime API
input_handler.h
Go to the documentation of this file.
1/// \file input_handler.h
2/// \brief The input handler interface
3///
4/// \par Copyright Information
5///
6/// Copyright (c) 2016 by The DiSTI Corporation.<br>
7/// 12249 Science Drive; Suite 300<br>
8/// Orlando, Florida 32826<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#ifndef INCLUDED_INPUT_HANDLER_H
40#define INCLUDED_INPUT_HANDLER_H
41
42#include "disti_assert.h"
44#include "weak_reference.h"
45
46namespace disti
47{
48class DisplayObject;
49class DisplayEvent;
50class KeyboardEvent;
51class MouseEvent;
52
53/** Class used to call a callback method that takes a KeyboardEvent */
55{
56public:
57 /// Call the callback with the provided display event.
58 /// \param ev The event to process.
59 virtual void Call( KeyboardEvent* ev ) = 0;
60
61 /// \return 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 */
70template<class T>
72{
73public:
74 typedef void ( T::*Callback )( KeyboardEvent* ev ); ///< Typedef for a function pointer accepting a keyboard event.
75
76 /// Constructor
77 /// \param method Function pointer to call back.
78 /// \param object The object containing the function pointer.
79 KeyboardMethodCallback( Callback method, T* object )
80 : _method( method )
81 , _object( object )
82 {
84 GLS_VERIFY( NULL != method );
85 GLS_VERIFY( NULL != object );
86 }
87
89 {
90 if( IsValid() )
91 {
92 ( _object.Get()->*( _method ) )( ev );
93 }
94 }
95
97 {
98 return !_object.IsNull();
99 }
100
101protected:
102 Callback _method; ///< The function pointer to call back.
103 WeakRef<T> _object; ///< The object containing the function pointer.
104};
105
106/// Helper method to create a KeyboardCallbackBase from a class method.
107/// \param method A class method pointer.
108/// \param obj The object to call the method on.
109/// \return A pointer to a new KeyboardMethodCallback object.
110template<class Class>
112 const typename KeyboardMethodCallback<Class>::Callback method,
113 Class* const obj )
114{
115 return new KeyboardMethodCallback<Class>( method, obj );
116}
117
118/** Class used to call a callback method that takes a MouseEvent */
120{
121public:
122 /// Call the callback with the provided display event.
123 /// \param ev The event to process.
124 virtual void Call( MouseEvent* ev ) = 0;
125
126 /// \return True if the callback is valid to call.
127 virtual bool IsValid() const = 0;
128
129 virtual ~MouseCallbackBase() {}
130};
131
132/** Interface for creating a callback from a class method. The class must implement WeakReferenceable.
133 * Don't use this directly. Use CreateInputHandlerCallback instead
134 */
135template<class T>
137{
138public:
139 typedef void ( T::*Callback )( MouseEvent* ev ); ///< Typedef for a function pointer accepting a mouse event.
140
141 /// Constructor
142 /// \param method Function pointer to call back.
143 /// \param object The object containing the function pointer.
144 MouseMethodCallback( Callback method, T* object )
145 : _method( method )
146 , _object( object )
147 {
149 GLS_VERIFY( NULL != method );
150 GLS_VERIFY( NULL != object );
151 }
152
154 {
155 if( IsValid() )
156 {
157 ( _object.Get()->*( _method ) )( ev );
158 }
159 }
160
162 {
163 return !_object.IsNull();
164 }
165
166protected:
167 Callback _method; ///< The function pointer to call back.
168 WeakRef<T> _object; ///< The object containing the function pointer.
169};
170
171/// Helper method to create a MouseCallbackBase from a class method.
172/// \param method A class method pointer.
173/// \param obj The object to call the method on.
174/// \return A pointer to a new MouseMethodCallback object.
175template<class Class>
177 const typename MouseMethodCallback<Class>::Callback method,
178 Class* const obj )
179{
180 return new MouseMethodCallback<Class>( method, obj );
181}
182
183/** Base class implemented by all input handlers */
185{
186public:
187 /// Type for unique identifiers
188 typedef unsigned int ID;
189
190 /** Handle the event
191 * \param ev the event to handle
192 * \return the object that handled the event, or NULL if no object handled the event
193 */
195
196 /** Register a global keyboard handler.
197 * \param callback the callback to call when a keyboard event occurs. The input handler owns the callback and will delete it.
198 * \return unique ID. Used to unregister.
199 */
201
202 /** Register a global mouse handler.
203 * \param callback the callback to call when a mouse event occurs. The input handler owns the callback and will delete it.
204 * \return unique ID. Used to unregister.
205 */
207
208 /** Unregister a global keyboard handler.
209 * \param id the unique identifier returned from RegisterGlobalKeyboardHandler.
210 */
211 virtual void UnregisterGlobalKeyboardHandler( ID id ) = 0;
212
213 /** Unregister a global mouse handler.
214 * \param id the unique identifier returned from RegisterGlobalMouseHandler.
215 */
216 virtual void UnregisterGlobalMouseHandler( ID id ) = 0;
217
218 /** destructor */
219 virtual ~InputHandler() {}
220};
221
222} // namespace disti
223
224#endif
Definition: events.h:111
Definition: display.h:95
Definition: input_handler.h:185
virtual ID RegisterGlobalKeyboardHandler(KeyboardCallbackBase *callback)=0
virtual ID RegisterGlobalMouseHandler(MouseCallbackBase *callback)=0
unsigned int ID
Type for unique identifiers.
Definition: input_handler.h:188
virtual void UnregisterGlobalKeyboardHandler(ID id)=0
virtual void UnregisterGlobalMouseHandler(ID id)=0
virtual DisplayObject * HandleInput(DisplayEvent *ev)=0
virtual ~InputHandler()
Definition: input_handler.h:219
Definition: input_handler.h:55
virtual void Call(KeyboardEvent *ev)=0
virtual bool IsValid() const =0
Definition: events.h:280
Definition: input_handler.h:72
WeakRef< T > _object
The object containing the function pointer.
Definition: input_handler.h:103
bool IsValid() const override
Definition: input_handler.h:96
KeyboardMethodCallback(Callback method, T *object)
Definition: input_handler.h:79
Callback _method
The function pointer to call back.
Definition: input_handler.h:102
void(T::* Callback)(KeyboardEvent *ev)
Typedef for a function pointer accepting a keyboard event.
Definition: input_handler.h:74
void Call(KeyboardEvent *ev) override
Definition: input_handler.h:88
Definition: input_handler.h:120
virtual void Call(MouseEvent *ev)=0
virtual bool IsValid() const =0
Definition: events.h:223
Definition: input_handler.h:137
void Call(MouseEvent *ev) override
Definition: input_handler.h:153
MouseMethodCallback(Callback method, T *object)
Definition: input_handler.h:144
WeakRef< T > _object
The object containing the function pointer.
Definition: input_handler.h:168
bool IsValid() const override
Definition: input_handler.h:161
Callback _method
The function pointer to call back.
Definition: input_handler.h:167
void(T::* Callback)(MouseEvent *ev)
Typedef for a function pointer accepting a mouse event.
Definition: input_handler.h:139
Definition: weak_reference.h:91
Definition: weak_reference.h:64
Contains the DistiAssert macro.
#define GLS_VERIFY(exp)
Definition: disti_assert.h:169
Macros and helper code to determine what subset of C++11/14/17 is available.
#define DISTI_STATIC_ASSERT_IS_CONVERTIBLE_TO(T, ConvertsTo)
Definition: gls_cpp_lang_support.h:276
#define DISTI_METHOD_OVERRIDE
Macro to wrap the override keyword, removed on compilers that don't support it.
Definition: gls_cpp_lang_support.h:221
Definition: bmpimage.h:46
KeyboardCallbackBase * CreateInputHandlerCallback(const typename KeyboardMethodCallback< Class >::Callback method, Class *const obj)
Definition: input_handler.h:111
Weak reference and related classes.