-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpeedBar.cpp
150 lines (113 loc) · 3.6 KB
/
SpeedBar.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <cmath>
#include <limits>
#include <sstream>
#include <iostream>
#include <fstream>
#include <istream>
#include "SpeedBar.h"
#include "Region2D.h"
void SpeedBar::MakeSpeedBar(GLfloat x, GLfloat y, GLfloat w, GLfloat h, GLfloat framemin, GLfloat framemax, GLfloat min, GLfloat max, GLchar or)
{
float barlen;
speed = 0;
frameMaxSpeed = framemax;
frameMinSpeed = framemin;
maxSpeed = max;
minSpeed = min;
xpos = x-w/2; //x is centered, xpos is the lower left corner
ypos = y-h/2; //y is centered, ypos is the lower left corner
width = w; // width on screen
height = h; // height on screen
orient = or;
if (orient == 'v')
{
frame.SetNSides(4);
frame.SetRectDims(width, height);
frame.SetRegionCenter(xpos, ypos);
bar.SetNSides(4);
barlen = (speed-frameMinSpeed)*height/(frameMaxSpeed-frameMinSpeed);
barlen = (barlen<0 ? 0 : barlen); //restrict the minimum length to be 0
barlen = (barlen>height ? height : barlen); //restrict the maximum length to be the frame length
bar.SetRectDims(width/2.0f, barlen);
bar.SetRegionCenter(xpos+width/4.0f, ypos);
speedregion.SetNSides(4);
speedregion.SetRectDims(width*1.05, (maxSpeed-minSpeed)*height/(frameMaxSpeed-frameMinSpeed));
speedregion.SetRegionCenter(xpos-width*0.025f, ypos+((minSpeed-frameMinSpeed)*height/(frameMaxSpeed-frameMinSpeed)));
}
else //default is a horizontal bar/frame
{
frame.SetNSides(4);
frame.SetRectDims(width, height);
frame.SetRegionCenter(xpos, ypos);
bar.SetNSides(4);
barlen = (speed-frameMinSpeed)*width/(frameMaxSpeed-frameMinSpeed);
barlen = (barlen<0 ? 0 : barlen); //restrict the minimum length to be 0
barlen = (barlen>width ? width : barlen); //restrict the maximum length to be the frame length
bar.SetRectDims(barlen, height/2.0f);
bar.SetRegionCenter(xpos, ypos+height/4.0f);
speedregion.SetNSides(4);
speedregion.SetRectDims((maxSpeed-minSpeed)*width/(frameMaxSpeed-frameMinSpeed), height*1.05f);
speedregion.SetRegionCenter(xpos+((minSpeed-frameMinSpeed)*width/(frameMaxSpeed-frameMinSpeed)), ypos-height*0.025f);
}
frameColor[0] = 0.9f;
frameColor[1] = 0.9f;
frameColor[2] = 0.9f;
barColor[0] = 0.0f;
barColor[1] = 1.0f;
barColor[2] = 1.0f;
speedregionColor[0] = 0.5f;
speedregionColor[1] = 0.5f;
speedregionColor[2] = 0.5f;
frame.SetRegionColor(frameColor);
bar.SetRegionColor(barColor);
speedregion.SetRegionColor(speedregionColor);
}
void SpeedBar::SetSpeedBounds(GLfloat min, GLfloat max)
{
maxSpeed = max;
minSpeed = min;
}
void SpeedBar::SetFrameBounds(GLfloat min, GLfloat max)
{
frameMaxSpeed = max;
frameMinSpeed = min;
}
void SpeedBar::UpdateSpeed(GLfloat currSpeed)
{
speed = currSpeed;
float barlen;
if (orient == 'v')
{
barlen = (speed-frameMinSpeed)*height/(frameMaxSpeed-frameMinSpeed);
barlen = (barlen<0 ? 0 : barlen); //restrict the minimum length to be 0
barlen = (barlen>height ? height : barlen); //restrict the maximum length to be the frame length
bar.SetRectDims(width/2.0f, barlen);
bar.SetRegionCenter(xpos+width/4.0f, ypos);
}
else //default is a horizontal bar/frame
{
barlen = (speed-frameMinSpeed)*width/(frameMaxSpeed-frameMinSpeed);
barlen = (barlen<0 ? 0 : barlen); //restrict the minimum length to be 0
barlen = (barlen>width ? width : barlen); //restrict the maximum length to be the frame length
bar.SetRectDims(barlen, height/2.0f);
bar.SetRegionCenter(xpos, ypos+height/4.0f);
}
}
void SpeedBar::On()
{
frame.On();
bar.On();
speedregion.On();
}
void SpeedBar::Off()
{
frame.Off();
bar.Off();
speedregion.Off();
}
void SpeedBar::Draw()
{
frame.Draw();
speedregion.Draw();
bar.Draw();
}