InputBindingSystem.cpp
Go to the documentation of this file.
1 #include "InputBindingSystem.hpp"
2 #include "InputBindingQuery.hpp"
4 
5 namespace iv
6 {
7 
9  System( sc ),
10  binds()
11 {
12 }
13 
15 {
16  auto & out = view->out();
17 
18  for( auto & [ input_id, binds ] : this->binds )
19  {
20  out << "InputId::" << StringIO_Write( input_id, view->context() ) << ": ";
21 
22  bool first = true;
23  for( Input::Key const & key : binds.all )
24  {
25  if( !first )
26  out << ", ";
27  first = false;
28 
29  out << "( Key::" << StringIO_Write( key, view->context() ) << ", -1 )";
30  }
31 
32  for( Input::DeviceKey const & device_key : binds.device )
33  {
34  if( !first )
35  out << ", ";
36  first = false;
37 
38  out << "( Key::" << StringIO_Write( device_key.first, view->context() ) << ", "<< device_key.second <<" )";
39  }
40 
41  if( first )
42  out << "<unbound>";
43 
44  out << std::endl;
45  }
46 
47 
48  for( auto & [ input_id, binds ] : this->hover_binds )
49  {
50  out << "InputId::" << StringIO_Write( input_id, view->context() ) << " (hover): ";
51 
52  bool first = true;
53  for( Input::Key const & key : binds.all )
54  {
55  if( !first )
56  out << ", ";
57  first = false;
58 
59  out << "( Key::" << StringIO_Write( key, view->context() ) << ", -1 )";
60  }
61 
62  for( Input::DeviceKey const & device_key : binds.device )
63  {
64  if( !first )
65  out << ", ";
66  first = false;
67 
68  out << "( Key::" << StringIO_Write( device_key.first, view->context() ) << ", "<< device_key.second <<" )";
69  }
70 
71  if( first )
72  out << "<unbound>";
73 
74  out << std::endl;
75  }
76 }
77 
79 {
80  this->listeners.insert( lst );
81 }
82 
84 {
85  this->listeners.erase( lst );
86 }
87 
88 void InputBindingSystem::binding_changed()
89 {
90  this->listeners.foreach(
91  [&]( InputBindingListener * lst )
92  {
93  lst->on_binding_changed();
94  }
95  );
96 }
97 
98 void InputBindingSystem::BindKey( InputId inputId, Input::Key key, int device_index )
99 {
100  if( device_index < 0 )
101  this->binds[ inputId ].all.insert( key );
102  else
103  this->binds[ inputId ].device.insert( std::pair( key, device_index ) );
104 
105  this->binding_changed();
106 }
107 
108 void InputBindingSystem::UnbindKey( InputId inputId, Input::Key key, int device_index )
109 {
110  auto bind = this->binds[ inputId ];
111  if( device_index < 0 )
112  bind.all.erase( key );
113  else
114  bind.device.erase( std::pair( key, device_index ) );
115 
116  if( bind.all.empty() && bind.device.empty() )
117  this->binds.erase( inputId );
118 
119  this->binding_changed();
120 }
121 
122 bool InputBindingSystem::IsBound( InputId inputId, Input::Key key, int device_index )
123 {
124  auto it = this->binds.find( inputId );
125  if( it == this->binds.end() )
126  return false;
127 
128  if( it->second.all.count( key ) )
129  return true;
130 
131  if( it->second.device.count( std::pair( key, device_index ) ) )
132  return true;
133 
134  return false;
135 }
136 
137 
138 void InputBindingSystem::BindHoverKey( InputId inputId, Input::Key key, int device_index )
139 {
140  if( device_index < 0 )
141  this->hover_binds[ inputId ].all.insert( key );
142  else
143  this->hover_binds[ inputId ].device.insert( std::pair( key, device_index ) );
144 
145  this->binding_changed();
146 }
147 
148 void InputBindingSystem::UnbindHoverKey( InputId inputId, Input::Key key, int device_index )
149 {
150  auto bind = this->hover_binds[ inputId ];
151  if( device_index < 0 )
152  bind.all.erase( key );
153  else
154  bind.device.erase( std::pair( key, device_index ) );
155 
156  if( bind.all.empty() && bind.device.empty() )
157  this->hover_binds.erase( inputId );
158 
159  this->binding_changed();
160 }
161 
162 bool InputBindingSystem::IsHoverBound( InputId inputId, Input::Key key, int device_index )
163 {
164  auto it = this->hover_binds.find( inputId );
165  if( it == this->hover_binds.end() )
166  return false;
167 
168  if( it->second.all.count( key ) )
169  return true;
170 
171  if( it->second.device.count( std::pair( key, device_index ) ) )
172  return true;
173 
174  return false;
175 }
176 
177 }