Bubba-3D  0.9.0
Awesome game engine!
ControlStatus.cpp
1 /*
2  * This file is part of Bubba-3D.
3  *
4  * Bubba-3D is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Bubba-3D is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with Bubba-3D. If not, see http://www.gnu.org/licenses/.
16  */
17 //
18 // Created by simon on 2015-12-20.
19 //
20 
21 #include "ControlStatus.h"
22 #include <map>
23 #include <cmath>
24 
25 const float ControlStatus::NO_MAX = -10000.0f;
26 
28 }
29 
30 ControlStatus::ControlStatus(Activator activator, float value) {
31  this->addButton(activator,value);
32 }
33 
34 void ControlStatus::addButton(Activator activator, float value) {
35  buttons.insert(std::pair<Activator,int>(activator,value));
36  maxValue = NO_MAX;
37 }
38 
39 void ControlStatus::merge(ControlStatus cs) {
40  for(auto it = cs.buttons.begin(); it != cs.buttons.end(); it++)
41  buttons.insert(std::pair<Activator,float>(it->first,it->second));
42  maxValue = NO_MAX;
43 }
44 
45 ControlStatus::Activator ControlStatus::activatorFromJoystickNumber(int n) {
46  if(n >= 8)
47  throw "Argument must be less than 8";
48  const Activator map[8] = {JOYSTICK_0,JOYSTICK_1,JOYSTICK_2,JOYSTICK_3,JOYSTICK_4,JOYSTICK_5,JOYSTICK_6,JOYSTICK_7};
49  return map[n];
50 }
51 
53  if(maxValue == NO_MAX) { //cache max value
54  maxValue = 0.0f;
55  for (auto it = buttons.begin(); it != buttons.end(); it++)
56  if (std::abs(it->second) > std::abs(maxValue)) {
57  maxValue = it->second;
58  maxValueActivator = it->first;
59  }
60  }
61  return maxValue;
62 }
63 
64 ControlStatus::Activator ControlStatus::getMaxActivator(){
65  if(maxValue == NO_MAX)
66  getValue();
67  return maxValueActivator;
68 }
69 
70 float ControlStatus::getValue(Activator activator) {
71  std::map<Activator ,float>::iterator elem = buttons.find(activator);
72  return elem == buttons.end() ? 0.0f : elem->second;
73 }
74 
75 bool ControlStatus::isActive(){
76  float val = getValue();
77  return val != 0.0f;
78 }
79 
80 bool ControlStatus::isActive(Activator activator) {
81  float val = getValue(activator);
82  return val != 0.0f;
83 }
ControlStatus(Activator activator, float value)
The class that contains information about a function at the state of creation.
Definition: ControlStatus.h:59
Activator getMaxActivator()