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 
53 /** Class used to call a callback method that takes a KeyboardEvent */
55 {
56 public:
57  /** call the callback with the provided display event */
58  virtual void Call( KeyboardEvent* ev ) = 0;
59 
60  /** returns true if the callback is valid to call */
61  virtual bool IsValid() const = 0;
62 
63  virtual ~KeyboardCallbackBase() {}
64 };
65 
66 /** Interface for creating a callback from a class method. The class must implement WeakReferenceable.
67  * Don't use this directly. Use CreateInputHandlerCallback instead
68  */
69 template<class T>
71 {
72 public:
73  typedef void ( T::*Callback )( KeyboardEvent* ev );
74 
75  KeyboardMethodCallback( Callback method, T* object )
76  : _method( method )
77  , _object( object )
78  {
80  GLS_VERIFY( NULL != method );
81  GLS_VERIFY( NULL != object );
82  }
83 
84  void Call( KeyboardEvent* ev )
85  {
86  if( IsValid() )
87  {
88  ( _object.Get()->*( _method ) )( ev );
89  }
90  }
91 
92  virtual bool IsValid() const
93  {
94  return !_object.IsNull();
95  }
96 
97 protected:
98  Callback _method;
99  WeakRef<T> _object;
100 };
101 
102 /** helper method to create a KeyboardCallbackBase from a class method
103  * \param method a class method pointer
104  * \param obj the object to call the method on
105  */
106 template<class Class>
108  const typename KeyboardMethodCallback<Class>::Callback method,
109  Class* const obj )
110 {
111  return new KeyboardMethodCallback<Class>( method, obj );
112 }
113 
114 /** Base class implemented by all input handlers */
116 {
117 public:
118  /// Type for unique identifiers
119  typedef unsigned int ID;
120 
121  /** Handle the event
122  * \param ev the event to handle
123  * \return the object that handled the event, or NULL if no object handled the event
124  */
125  virtual DisplayObject* HandleInput( DisplayEvent* ev ) = 0;
126 
127  /** Register a global keyboard handler.
128  * \param callback the callback to call when a keyboard event occurs. The input handler owns the callback and will delete it.
129  * \return unique ID. Used to unregister.
130  */
131  virtual ID RegisterGlobalKeyboardHandler( KeyboardCallbackBase* callback ) = 0;
132 
133  /** Unregister a global keyboard handler.
134  * \param id the unique identifier returned from RegisterGlobalKeyboardHandler.
135  */
136  virtual void UnregisterGlobalKeyboardHandler( ID id ) = 0;
137 
138  /** destructor */
139  virtual ~InputHandler() {}
140 };
141 
142 } // namespace disti
143 
144 #endif
virtual DisplayObject * HandleInput(DisplayEvent *ev)=0
virtual void UnregisterGlobalKeyboardHandler(ID id)=0
KeyboardCallbackBase * CreateInputHandlerCallback(const typename KeyboardMethodCallback< Class >::Callback method, Class *const obj)
Definition: input_handler.h:107
Definition: events.h:271
Definition: display.h:97
virtual bool IsValid() const =0
unsigned int ID
Type for unique identifiers.
Definition: input_handler.h:119
virtual ID RegisterGlobalKeyboardHandler(KeyboardCallbackBase *callback)=0
virtual void Call(KeyboardEvent *ev)=0
virtual bool IsValid() const
Definition: input_handler.h:92
Definition: weak_reference.h:64
void Call(KeyboardEvent *ev)
Definition: input_handler.h:84
#define GLS_VERIFY(exp)
Definition: disti_assert.h:155
Definition: input_handler.h:70
Definition: input_handler.h:115
#define DISTI_STATIC_ASSERT_IS_CONVERTIBLE_TO(T, ConvertsTo)
Definition: gls_cpp_lang_support.h:323
Definition: events.h:110
Definition: weak_reference.h:91
virtual ~InputHandler()
Definition: input_handler.h:139
Contains the DistiAssert macro.
Macros and helper code to determine what subset of C++11/14/17 is available.
Definition: bmpimage.h:46
weak reference and related classes
Definition: input_handler.h:54