1 // Copyright (c) Charles J. Cliffe
2 // SPDX-License-Identifier: GPL-2.0+
3
4 #include "MeterPanel.h"
5 #include "ColorTheme.h"
6
7
MeterPanel(const std::string & name,float low,float high,float current)8 MeterPanel::MeterPanel(const std::string& name, float low, float high, float current) {
9 this->name = name;
10 this->low = low;
11 this->high = high;
12 this->current = current;
13
14 RGBA4f c1, c2;
15
16 setFill(GLPanel::GLPANEL_FILL_NONE);
17
18 bgPanel.setBorderPx(1);
19 bgPanel.setCoordinateSystem(GLPanel::GLPANEL_Y_UP);
20 bgPanel.setFill(GLPanel::GLPANEL_FILL_GRAD_X);
21
22 levelPanel.setBorderPx(0);
23 levelPanel.setMarginPx(1);
24
25 setPanelLevel(current, levelPanel);
26 levelPanel.setFill(GLPanel::GLPANEL_FILL_GRAD_BAR_X);
27 levelPanel.setBlend(GL_ONE, GL_ONE);
28
29 bgPanel.addChild(&levelPanel);
30
31 setPanelLevel(current, highlightPanel);
32 highlightPanel.setBorderPx(0);
33 highlightPanel.setMarginPx(1);
34 highlightPanel.setFill(GLPanel::GLPANEL_FILL_GRAD_BAR_X);
35 highlightPanel.setBlend(GL_ONE, GL_ONE);
36 highlightPanel.visible = false;
37 c1 = RGBA4f(0.3f,0.3f,0.3f,1.0f);
38 c2 = RGBA4f(0.65f,0.65f,0.65f,1.0f);
39 highlightPanel.setFillColor(c1, c2);
40
41 bgPanel.addChild(&highlightPanel);
42
43 addChild(&bgPanel);
44
45 labelPanel.setSize(1.0f, 0.1f);
46 labelPanel.setPosition(0.0, 1.0);
47 labelPanel.setText(name,GLFont::GLFONT_ALIGN_CENTER, GLFont::GLFONT_ALIGN_CENTER, true);
48 labelPanel.setFill(GLPanel::GLPANEL_FILL_NONE);
49
50 addChild(&labelPanel);
51
52 valuePanel.setSize(1.0f, 0.1f);
53 valuePanel.setPosition(0.0, -1.0);
54
55 setValueLabel(std::to_string(int(current)));
56 valuePanel.setFill(GLPanel::GLPANEL_FILL_NONE);
57
58 addChild(&valuePanel);
59 }
60
61 MeterPanel::~MeterPanel() = default;
62
63
setName(std::string name_in)64 void MeterPanel::setName(std::string name_in) {
65 name = name_in;
66 }
67
getName()68 std::string MeterPanel::getName() {
69 return name;
70 }
71
setRange(float low_in,float high_in)72 void MeterPanel::setRange(float low_in, float high_in) {
73 low = low_in;
74 high = high_in;
75 }
76
getLow() const77 float MeterPanel::getLow() const {
78 return low;
79 }
80
getHigh() const81 float MeterPanel::getHigh() const {
82 return high;
83 }
84
setValue(float value)85 void MeterPanel::setValue(float value) {
86 if (value > high) {
87 value = high;
88 }
89 if (value < low) {
90 value = low;
91 }
92
93 current = value;
94 setValueLabel(std::to_string(int(current)));
95 setPanelLevel(value, levelPanel);
96 }
97
setHighlight(float value)98 void MeterPanel::setHighlight(float value) {
99 if (value > high) {
100 value = high;
101 }
102 if (value < low) {
103 value = low;
104 }
105
106 setPanelLevel(value, highlightPanel);
107 }
108
setHighlightVisible(bool vis)109 void MeterPanel::setHighlightVisible(bool vis) {
110 highlightPanel.visible = vis;
111 }
112
getValue() const113 float MeterPanel::getValue() const {
114 return current;
115 }
116
isMeterHit(CubicVR::vec2 mousePoint)117 bool MeterPanel::isMeterHit(CubicVR::vec2 mousePoint) {
118 CubicVR::vec2 hitResult;
119
120 if (bgPanel.hitTest(mousePoint, hitResult)) {
121 return true;
122 }
123
124 return false;
125 }
126
getMeterHitValue(CubicVR::vec2 mousePoint)127 float MeterPanel::getMeterHitValue(CubicVR::vec2 mousePoint) {
128 CubicVR::vec2 hitResult;
129
130 if (bgPanel.hitTest(mousePoint, hitResult)) {
131 float hitLevel = ((hitResult.y + 1.0) * 0.5);
132
133 if (hitLevel < 0.0f) {
134 hitLevel = 0.0f;
135 }
136 if (hitLevel > 1.0f) {
137 hitLevel = 1.0f;
138 }
139
140 return low + (hitLevel * (high-low));
141 } else {
142 return 0;
143 }
144 }
145
drawPanelContents()146 void MeterPanel::drawPanelContents() {
147 GLint vp[4];
148
149 glGetIntegerv( GL_VIEWPORT, vp);
150
151 double viewHeight = vp[3];
152
153 CubicVR::vec4 t = CubicVR::mat4::vec4_multiply(CubicVR::vec4(0,0.5,0,1), transform);
154 CubicVR::vec4 b = CubicVR::mat4::vec4_multiply(CubicVR::vec4(0,-0.5,0,1), transform);
155
156 double hScale = t.y-b.y;
157
158 viewHeight = viewHeight * hScale;
159
160 double labelHeight = GLFont::getScaledPx(18, GLFont::getScaleFactor());
161 double labelPad = 8.0;
162
163 double pScale = (1.0/viewHeight);
164 RGBA4f c1, c2;
165
166 bgPanel.setSize(1.0f, 1.0 - pScale * (labelHeight + labelPad * 2.0));
167
168 valuePanel.setPosition(0.0, (pScale * (labelHeight / 2.0 + labelPad) ) - 1.0);
169 valuePanel.setSize(1.0, pScale*labelHeight);
170
171 labelPanel.setPosition(0.0, 1.0 - (pScale * (labelHeight / 2.0 + labelPad)));
172 labelPanel.setSize(1.0, pScale*labelHeight);
173
174 c1 = ThemeMgr::mgr.currentTheme->generalBackground;
175 c2 = ThemeMgr::mgr.currentTheme->generalBackground * 0.5;
176 c1.a = 1.0;
177 c2.a = 1.0;
178 bgPanel.setFillColor(c1, c2);
179
180 c1 = ThemeMgr::mgr.currentTheme->meterLevel * 0.5;
181 c2 = ThemeMgr::mgr.currentTheme->meterLevel;
182 c1.a = 1.0;
183 c2.a = 1.0;
184 levelPanel.setFillColor(c1, c2);
185
186 drawChildren();
187 }
188
setValueLabel(std::string label)189 void MeterPanel::setValueLabel(std::string label) {
190 valuePanel.setText(label,
191 GLFont::GLFONT_ALIGN_CENTER,
192 GLFont::GLFONT_ALIGN_CENTER,
193 true);
194
195 }
196
setPanelLevel(float setValue,GLPanel & panel) const197 void MeterPanel::setPanelLevel(float setValue, GLPanel &panel) const {
198 float valueNorm = (setValue - low) / (high - low);
199 panel.setSize(1.0, valueNorm);
200 panel.setPosition(0.0, (-1.0+(valueNorm)));
201 }
202
getChanged() const203 bool MeterPanel::getChanged() const {
204 return changed;
205 }
206
setChanged(bool changed_in)207 void MeterPanel::setChanged(bool changed_in) {
208 changed = changed_in;
209 }
210