InputEvent.cpp
Go to the documentation of this file.
1 #include "InputEvent.hpp"
2 
3 #include "../Defs.hpp"
4 
5 namespace iv
6 {
7 
9  cm( inst, this, "InputEvent", ClientMarker::Status() ),
10  attr_eventEnabled( &this->cm, this, false ),
11  attr_activation( &this->cm, this, Activator() ),
12  attr_hot( &this->cm, this, false ),
13  field_press( inst, this ),
14  field_release( inst, this ),
15  field_duration( inst, this ),
16  inst( inst ),
17  release_hot( false ),
18  duration( false )
19 {
20  this->cm.owns( this->field_press.cm, this->field_release.cm, this->field_duration.cm );
21 
25 }
26 
28 {
29 }
30 
32 {
33  return this->inst;
34 }
35 
37 {
38  static iv::TableId DebugTable = TableId::create( "InputEvent" );
39 
40  auto row = view->Table( DebugTable ).Row( this );
41 
42  row.Column( "attr_inputEnabled", this->attr_eventEnabled.Get() );
43  row.Column( "attr_activation", this->attr_activation.Get() );
44  row.Column( "attr_hot", this->attr_hot.Get() );
45 
46  row.Column( "field_press", this->field_press.Get() )
47  .Hint( "assignment", this->field_press.AssignmentState() )
48  .Hint( "mode", this->field_press.Mode() );
49  row.Column( "field_release", this->field_release.Get() )
50  .Hint( "assignment", this->field_release.AssignmentState() )
51  .Hint( "mode", this->field_release.Mode() );
52  row.Column( "field_duration", this->field_duration.Get() )
53  .Hint( "assignment", this->field_duration.AssignmentState() )
54  .Hint( "mode", this->field_duration.Mode() );
55 }
56 
57 void InputEvent::check_consistency()
58 {
60  {
61  bool disconnected = this->field_press.AssignmentState() == FieldI::Assignment::Unassigned
62  && this->field_release.AssignmentState() == FieldI::Assignment::Unassigned
63  && this->field_duration.AssignmentState() == FieldI::Assignment::Unassigned;
64  if( disconnected )
65  this->cm.log( SRC_INFO, iv::Defs::Log::ConsistencyChecks, "Fields disconnected." );
66  }
67 }
68 
70 {
71  if( this->duration )
72  return;
73  this->duration = true;
74 
75  this->check_consistency();
76 
78  {
79  this->cm.log( SRC_INFO, Defs::Log::InputEvent, "Start duration." );
80  this->PrivField_Owner< SumAgg< int > >::Field_Modify( &this->field_duration, this->field_duration.Get().Insert( 1 ) );
81  }
82 }
83 
85 {
86  if( !this->duration )
87  return;
88  this->duration = false;
89 
90  this->check_consistency();
91 
93  {
94  this->cm.log( SRC_INFO, Defs::Log::InputEvent, "Stop duration." );
95  this->PrivField_Owner< SumAgg< int > >::Field_Modify( &this->field_duration, this->field_duration.Get().Remove( 1 ) );
96  }
97 }
98 
100 {
101  this->check_consistency();
102 
103  if( this->field_press.Mode() == Attribute::ValueMode::ValueModify || this->field_release.Mode() == Attribute::ValueMode::ValueModify )
104  this->cm.log( SRC_INFO, Defs::Log::InputEvent, "Trigger." );
105 
106  // press
107  this->PrivField_Owner< Activator >::Field_Modify( &this->field_press, this->field_press.Get().MakeActivated() );
108 
109  // release
110  this->PrivField_Owner< Activator >::Field_Modify( &this->field_release, this->field_release.Get().MakeActivated() );
111 }
112 
114 {
115  this->check_consistency();
116 
117  if( this->field_press.Mode() == Attribute::ValueMode::ValueModify )
118  this->cm.log( SRC_INFO, Defs::Log::InputEvent, "Press activated." );
119 
120  // press activation
121  this->PrivField_Owner< Activator >::Field_Modify( &this->field_press, this->field_press.Get().MakeActivated() );
122 
123  // release is hot
125  {
126  this->release_hot = true;
127  this->Field_OnChanged( (Field< SumAgg< int > > *)nullptr, false );
128  }
129 }
130 
132 {
133  this->check_consistency();
134 
135  // release activation
136  if( real )
137  {
139  this->cm.log( SRC_INFO, Defs::Log::InputEvent, "Release activated." );
140  this->PrivField_Owner< Activator >::Field_Modify( &this->field_release, this->field_release.Get().MakeActivated() );
141  }
142 
143  // release is no longer hot
144  this->release_hot = false;
145  this->Field_OnChanged( (Field< SumAgg< int > > *)nullptr, false );
146 }
147 
148 void InputEvent::refresh_enabled()
149 {
150  bool enabled = this->field_press.Mode() == Attribute::ValueMode::ValueModify
153 
154  this->Attribute_Set( &this->attr_eventEnabled, enabled );
155 }
156 
157 void InputEvent::Field_OnChanged( Field< Activator > * field, bool real )
158 {
159  // refresh enabled field
160  this->refresh_enabled();
161 
162  // copy activations
163  unsigned char activations;
164  if( field == &this->field_press )
165  activations = this->field_press.Get().CopyActivations( this->press_reference );
166  else if( field == &this->field_release )
167  activations = this->field_release.Get().CopyActivations( this->release_reference );
168 
169  // activate visualization
170  if( real )
171  {
172  Activator current = this->attr_activation.Get();
173  this->Attribute_Set( &this->attr_activation, Activator( current.Value() + activations ) );
174  }
175 }
176 
177 void InputEvent::Field_OnChanged( Field< SumAgg< int > > * field, bool real )
178 {
179  // refresh enabled field
180  this->refresh_enabled();
181 
182  // refresh attr_hot
183  this->Attribute_Set( &this->attr_hot, this->field_duration.Get().Aggregated() > 0 || this->release_hot );
184 }
185 
186 }