InputBindingSystem.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "InputId.hpp"
5 
6 namespace iv
7 {
8 
9 class InputBindingListener;
10 
41 class InputBindingSystem : public System
42 {
43 public:
45 
46  virtual std::string debug_name() const override { return "InputBindingSystem"; }
47  virtual void status( TextDebugView * view ) override;
48 
51 
52  //----------------- called by InputBinding -----------------------------------------------
56  void BindKey( InputId inputId, Input::Key key, int device_index = -1 );
57  void UnbindKey( InputId inputId, Input::Key key, int device_index = -1 );
58 
59  void BindHoverKey( InputId inputId, Input::Key key, int device_index = -1 );
60  void UnbindHoverKey( InputId inputId, Input::Key key, int device_index = -1 );
61 
62  //----------------- called by InputEventClient -------------------------------------------------
66  bool IsBound( InputId inputId, Input::Key key, int device_index );
67 
70  bool IsHoverBound( InputId inputId, Input::Key key, int device_index );
71 
75  template< class Callable >
76  void ForeachHoverBinding( InputId inputId, Callable callable )
77  {
78  auto it = this->hover_binds.find( inputId );
79  if( it == this->hover_binds.end() )
80  return;
81 
82  Binds const & bind = it->second;
83 
84  for( auto const & dev_key : bind.device )
85  callable( dev_key.first, dev_key.second );
86 
87  for( auto const & key : bind.all )
88  callable( key, -1 );
89  }
90 
96  template< class Callable >
97  void ForeachBinding( InputId inputId, Callable callable )
98  {
99  auto it = this->binds.find( inputId );
100  if( it == this->binds.end() )
101  return;
102 
103  Binds const & bind = it->second;
104 
105  for( auto const & dev_key : bind.device )
106  callable( dev_key.first, dev_key.second );
107 
108  for( auto const & key : bind.all )
109  callable( key, -1 );
110  }
111 
112 private:
113  void binding_changed();
114 
115 private:
116  struct Binds
117  {
118  std::unordered_set< Input::Key > all;
119  std::unordered_set< Input::DeviceKey, Input::DeviceKey_Hash > device;
120  };
121 
122  std::unordered_map< InputId, Binds > binds;
123  std::unordered_map< InputId, Binds > hover_binds;
124 
125  volatile_set< InputBindingListener * > listeners;
126 };
127 
128 }