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