SourceInputNode.cpp
Go to the documentation of this file.
1 #include "SourceInputNode.hpp"
2 #include "../Defs.hpp"
3 
4 namespace iv
5 {
6 
8  InputNode( inst ),
9  InputBindingListener( inst ),
10  FrameUpdateClient( inst ),
11  cm( inst, this, "InputSourceNode" ),
12  pressed(),
13  reserved(),
14  refresh_queued( false )
15 {
16  this->cm.inherits( this->InputNode::cm, this->FrameUpdateClient::cm );
17 
18  this->pressed.insert( std::pair( Input::Key::Mouse, 0 ) );
19 }
20 
22 {
23  this->input_deactivate();
24 }
25 
27 {
28  // disconnect node from previous parent
29  if( node->input_getParent() )
30  node->input_getParent()->input_childDisconnect( node );
31 
32  //
33  this->_input_children.push_back( node );
34  node->input_setParent( this );
35 }
36 
38 {
39  this->input_childDisconnect( node );
40 }
41 
43 {
44  for( size_t i = 0; i < this->_input_children.size(); i++ )
45  if( this->_input_children[ i ] == node )
46  {
47  this->_input_children[ i ]->input_setParent( nullptr );
48  this->_input_children.erase( this->_input_children.begin() + i );
49  break;
50  }
51 }
52 
53 void SourceInputNode::input_eachChild( std::function< void( InputNode * ) > const & f )
54 {
55  for( auto it = this->_input_children.rbegin(); it != this->_input_children.rend(); ++it )
56  f( *it );
57 }
58 
60 {
61  if( reserve )
62  {
63  if( key.second >= 0 )
64  this->reserved.insert( key );
65  else
66  this->reserved_whole.insert( key.first );
67  }
68  else
69  {
70  if( key.second >= 0 )
71  this->reserved.erase( key );
72  else
73  this->reserved_whole.erase( key.first );
74  }
75 }
76 
77 void SourceInputNode::on_binding_changed()
78 {
79  this->refresh_queued = true;
80 }
81 
83 {
84  this->refresh_queued = true;
85 }
86 
87 void SourceInputNode::frame_update()
88 {
89  if( this->refresh_queued )
90  {
91  this->refresh_queued = false;
92  this->do_refresh();
93  }
94 }
95 
96 bool SourceInputNode::input( Input const * input )
97 {
98  Input::DeviceKey device_key( input->key(), input->device_id() );
99  this->reserved.erase( device_key );
100 
101  //
102  this->cm.log( SRC_INFO, Defs::Log::Input, input->type(), " ", input->key(), "/", input->device_id() );
103 
104  if( input->type() == Input::Type::Trigger )
105  {
106  return !this->input_trigger_visit( this, device_key );
107  }
108  else
109  {
110  // update list of active keys
111  if( input->type() == Input::Type::Press )
112  {
113  if( this->pressed.count( device_key ) )
114  return false;
115 
116  this->pressed.insert( device_key );
117  }
118  else if( input->type() == Input::Type::Release )
119  {
120  if( !this->pressed.count( device_key ) )
121  return false;
122 
123  this->pressed.erase( device_key );
124  }
125 
126  // visit
127  bool press = input->type() == Input::Type::Press;
128  bool real = true;
129  bool offspace = false;
130 
131  bool cont = this->input_visit( this, device_key, press, real, offspace );
132 
133  return !cont || !real;
134  }
135 }
136 
137 bool SourceInputNode::is_reserved( Input::DeviceKey device_key )
138 {
139  return this->reserved.count( device_key ) || this->reserved_whole.count( device_key.first );
140 }
141 
142 void SourceInputNode::do_refresh()
143 {
144  for( Input::DeviceKey const & device_key : this->pressed )
145  {
146  bool press = true;
147  bool real = false;
148  bool offspace = this->is_reserved( device_key );
149 
150  this->input_visit( this, device_key, press, real, offspace );
151  }
152 }
153 
155 {
156  for( Input::DeviceKey const & device_key : this->pressed )
157  {
158  bool press = false;
159  bool real = false;
160  bool offspace = this->is_reserved( device_key );
161 
162  node->input_visit( nullptr, device_key, press, real, offspace );
163  }
164 }
165 
167 {
168  return this;
169 }
170 
171 }