]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/controller.cpp
Use skylight for nicer lighting
[r2c2.git] / source / libr2c2 / controller.cpp
1 #include <cmath>
2 #include "controller.h"
3
4 using namespace std;
5
6 namespace R2C2 {
7
8 void Controller::Control::set(float v)
9 {
10         if(v<min_value)
11                 v = min_value;
12         else if(v>max_value)
13                 v = max_value;
14         else if(type==BINARY)
15                 value = v ? 1 : 0;
16         else if(type==DISCRETE)
17                 value = min_value+floor((v-min_value)/step)*step;
18         else if(type==CONTINUOUS)
19                 value = v;
20 }
21
22 Controller::Control Controller::Control::binary(const string &n)
23 {
24         Controller::Control tc;
25         tc.name = n;
26         tc.type = BINARY;
27         tc.min_value = 0;
28         tc.max_value = 1;
29         tc.step = 1;
30         tc.value = 0;
31
32         return tc;
33 }
34
35 Controller::Control Controller::Control::discrete(const string &n, float m, float x, float s)
36 {
37         if(x<m)
38                 throw invalid_argument("Controller::Control::discrete");
39
40         Controller::Control tc;
41         tc.name = n;
42         tc.type = DISCRETE;
43         tc.min_value = m;
44         tc.max_value = m+floor((x-m)/s)*s;
45         tc.step = s;
46         tc.value = m;
47
48         return tc;
49 }
50
51 Controller::Control Controller::Control::continuous(const string &n, float m, float x)
52 {
53         if(x<m)
54                 throw invalid_argument("Controller::Control::continuous");
55
56         Controller::Control tc;
57         tc.name = n;
58         tc.type = CONTINUOUS;
59         tc.min_value = m;
60         tc.max_value = x;
61         tc.step = 0;
62         tc.value = m;
63
64         return tc;
65 }
66
67 } // namespace R2C2