nomlib
Loading...
Searching...
No Matches
InputStateMapper.hpp
1/******************************************************************************
2
3 nomlib - C++11 cross-platform game engine
4
5Copyright (c) 2013, 2014 Jeffrey Carpenter <i8degrees@gmail.com>
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are met:
10
111. Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
132. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28******************************************************************************/
29#ifndef NOMLIB_SYSTEM_INPUT_MAPPER_INPUT_STATE_MAPPER_HPP
30#define NOMLIB_SYSTEM_INPUT_MAPPER_INPUT_STATE_MAPPER_HPP
31
32#include <map>
33
34#include "export.hpp"
35#include "nomlib/config.hpp"
36#include "nomlib/system/InputMapper/InputActionMapper.hpp"
37
38namespace nom {
39
40// Forward declarations
41struct Event;
42class EventHandler;
43class InputAction;
44
45struct NOM_EXPORT InputActionState
46{
47 bool active;
48 InputActionMapper::ActionMap actions;
49};
50
54class NOM_EXPORT InputStateMapper
55{
56 public:
57 typedef InputStateMapper SelfType;
58 typedef std::pair<std::string, InputActionState> Pair;
59 typedef std::map<std::string, InputActionState> InputStateMap;
60
63
66
70 bool active( const std::string& key ) const;
71
81 bool insert( const std::string& key, const InputActionMapper& map, bool active );
82
86 bool erase(const std::string& key);
87
91 bool activate( const std::string& key );
92
96 bool disable( const std::string& key );
97
99 void disable( void );
100
105 bool activate_only( const std::string& key );
106
108 void clear( void );
109
113 void dump();
114
126 void set_event_handler(EventHandler& evt_handler);
127
135
136 private:
138 void on_event(const Event& ev);
139
142 bool on_key_press(const InputAction& mapping, const Event& ev);
143
146 bool on_mouse_button( const InputAction& mapping, const Event& ev );
147
150 bool on_mouse_wheel( const InputAction& mapping, const Event& ev );
151
154 bool on_joystick_button(const InputAction& mapping, const Event& ev);
155
158 bool on_joystick_axis(const InputAction& mapping, const Event& ev);
159
160 bool on_joystick_hat(const InputAction& mapping, const Event& ev);
161
164 bool
165 on_game_controller_button(const InputAction& mapping, const Event& ev);
166
169 bool
170 on_game_controller_axis(const InputAction& mapping, const Event& ev);
171
172 // Non-owned pointer
173 EventHandler* event_handler_ = nullptr;
174
175 // Stored copy of the watch installed in event_handler_; enables
176 // idempotent installs and precise removal via target_type() matching.
177 event_filter event_watch_ = nullptr;
178
179 InputStateMap states_;
180};
181
182} // namespace nom
183
184#endif // include guard defined
Event handling abstraction.
Base class for mapping an action to an input device.
High-level API for associating names with input actions.
void disable(void)
Disable all input states.
void set_event_handler(EventHandler &evt_handler)
Install the event handler used by the interface.
bool on_joystick_button(const InputAction &mapping, const Event &ev)
Internal event handler for matching a joystick button action to a joystick button event.
void clear(void)
Empty the container of input states.
bool on_game_controller_button(const InputAction &mapping, const Event &ev)
Internal event handler for matching a game controller button action to an equivalent event.
bool insert(const std::string &key, const InputActionMapper &map, bool active)
Add action mappings to an input state.
InputStateMapper(void)
Default constructor.
bool erase(const std::string &key)
Remove a state.
void remove_event_handler()
Detach the event handler watch previously installed via set_event_handler.
bool on_mouse_button(const InputAction &mapping, const Event &ev)
Internal event handler for matching a mouse button action to a mouse button event.
bool on_joystick_axis(const InputAction &mapping, const Event &ev)
Internal event handler for matching a joystick axis action to a joystick axis event.
bool on_mouse_wheel(const InputAction &mapping, const Event &ev)
Internal event handler for matching a mouse wheel action to a mouse wheel event.
bool activate_only(const std::string &key)
Enable only a particular state.
bool on_game_controller_axis(const InputAction &mapping, const Event &ev)
Internal event handler for matching a game controller axis action to an equivalent event.
bool on_key_press(const InputAction &mapping, const Event &ev)
Internal event handler for matching a keyboard action to a keyboard event.
bool activate(const std::string &key)
Activate a state.
bool disable(const std::string &key)
Disable a state.
~InputStateMapper(void)
Destructor.
void dump()
Diagnostic output.
void on_event(const Event &ev)
Internal event handler for triggering mapped action states.
bool active(const std::string &key) const
Obtain active state status.
Event handling types.
Definition Event.hpp:458