-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue_bounded.cpp
83 lines (72 loc) · 1.43 KB
/
value_bounded.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* value_bounded.cpp
*
* Created on: Apr 2, 2018
* Author: MisterCavespider
*/
#include "value_bounded.h"
BoundedValue::BoundedValue(float value, float min, float step, float max)
{
if(min < max) // In case someone does something stupid
{
this->min = min;
this->max = max;
}
else
{
this->min = max;
this->max = min;
}
this->step = step;
this->value = value;
}
BoundedValue::BoundedValue(float min, float step, float max) : BoundedValue(min,min,step,max) {}
BoundedValue &BoundedValue::operator+=(float value)
{
this->value += value;
forceBounds();
return *this;
}
BoundedValue &BoundedValue::operator+=(int value)
{
this->value += step * value;
forceBounds();
return *this;
}
BoundedValue &BoundedValue::operator-=(float value)
{
this->value -= value;
forceBounds();
return *this;
}
BoundedValue &BoundedValue::operator-=(int value)
{
this->value -= step * value;
forceBounds();
return *this;
}
uint8_t BoundedValue::identicator() {return 0x01;}
float BoundedValue::getMinimum() {return min;}
float BoundedValue::getStep() {return step;}
float BoundedValue::getMaximum() {return max;}
float BoundedValue::getValue() {return value;}
bool BoundedValue::withinBounds()
{
if(value < min) return false;
if(value > max) return false;
return true;
}
void BoundedValue::forceBounds()
{
if(value < min)
{
value = min;
return;
}
if(value > max)
{
value = max;
return;
}
}
BoundedValue::~BoundedValue() {}