forked from bobsayshilol/engine-sim
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththrottle_display.cpp
More file actions
131 lines (98 loc) · 4.04 KB
/
throttle_display.cpp
File metadata and controls
131 lines (98 loc) · 4.04 KB
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
#include "../include/throttle_display.h"
#include "../include/geometry_generator.h"
#include "../include/engine_sim_application.h"
ThrottleDisplay::ThrottleDisplay() {
m_engine = nullptr;
}
ThrottleDisplay::~ThrottleDisplay() {
/* void */
}
void ThrottleDisplay::initialize(EngineSimApplication *app) {
UiElement::initialize(app);
}
void ThrottleDisplay::destroy() {
UiElement::destroy();
}
void ThrottleDisplay::update(float dt) {
UiElement::update(dt);
}
void ThrottleDisplay::render() {
UiElement::render();
drawFrame(m_bounds, 1.0, ysMath::Constants::One, m_app->getBackgroundColor());
const Bounds bounds = m_bounds.inset(10.0f);
const Bounds title = bounds.verticalSplit(1.0f, 0.9f);
drawCenteredText("THROTTLE", title.inset(10.0f), 24.0f);
const Bounds mainDrawArea = bounds.verticalSplit(0.0f, 0.9f);
renderThrottle(mainDrawArea);
}
void ThrottleDisplay::renderThrottle(const Bounds &bounds) {
GeometryGenerator *gen = m_app->getGeometryGenerator();
const float width = pixelsToUnits(bounds.width());
const float height = pixelsToUnits(bounds.height());
const float size = std::fmin(width, height);
const Point origin = getRenderPoint(bounds.getPosition(Bounds::center));
const float carbBore = size * 0.4f;
const float carbHeight = size * 0.5f;
const float plateWidth = carbBore * 0.8f;
GeometryGenerator::Line2dParameters params;
params.lineWidth = size * 0.01f;
GeometryGenerator::Circle2dParameters circleParams;
circleParams.radius = params.lineWidth / 2.0f;
circleParams.maxEdgeLength = m_app->pixelsToUnits(5.0f);
GeometryGenerator::GeometryIndices main, pivot, pivotShadow;
gen->startShape();
params.x0 = origin.x + carbBore / 2.0f;
params.y0 = origin.y - carbHeight / 2.0f;
params.x1 = origin.x + carbBore / 2.0f;
params.y1 = origin.y + carbHeight / 2.0f;
gen->generateLine2d(params);
circleParams.center_x = params.x1;
circleParams.center_y = params.y1;
gen->generateCircle2d(circleParams);
circleParams.center_x = params.x1;
circleParams.center_y = params.y0;
gen->generateCircle2d(circleParams);
params.x0 = origin.x - carbBore / 2.0f;
params.x1 = origin.x - carbBore / 2.0f;
gen->generateLine2d(params);
circleParams.center_x = params.x1;
circleParams.center_y = params.y1;
gen->generateCircle2d(circleParams);
circleParams.center_x = params.x1;
circleParams.center_y = params.y0;
gen->generateCircle2d(circleParams);
// Draw throttle plate
const float throttleAngle = (float)m_engine->getThrottlePlateAngle();
const float cos_theta = std::cosf(throttleAngle);
const float sin_theta = std::sinf(throttleAngle);
params.y0 = origin.y - sin_theta * plateWidth / 2.0f;
params.x0 = origin.x - cos_theta * plateWidth / 2.0f;
params.y1 = origin.y + sin_theta * plateWidth / 2.0f;
params.x1 = origin.x + cos_theta * plateWidth / 2.0f;
gen->generateLine2d(params);
circleParams.center_x = params.x0;
circleParams.center_y = params.y0;
gen->generateCircle2d(circleParams);
circleParams.center_x = params.x1;
circleParams.center_y = params.y1;
gen->generateCircle2d(circleParams);
gen->endShape(&main);
gen->startShape();
circleParams.center_x = origin.x;
circleParams.center_y = origin.y;
circleParams.radius = size * 0.01f;
gen->generateCircle2d(circleParams);
gen->endShape(&pivot);
gen->startShape();
circleParams.center_x = origin.x;
circleParams.center_y = origin.y;
circleParams.radius = 2 * size * 0.01f;
gen->generateCircle2d(circleParams);
gen->endShape(&pivotShadow);
m_app->getShaders()->SetBaseColor(m_app->getWhite());
m_app->drawGenerated(main, 0x11, m_app->getShaders()->GetUiFlags());
m_app->getShaders()->SetBaseColor(m_app->getBackgroundColor());
m_app->drawGenerated(pivotShadow, 0x11, m_app->getShaders()->GetUiFlags());
m_app->getShaders()->SetBaseColor(m_app->getWhite());
m_app->drawGenerated(pivot, 0x11, m_app->getShaders()->GetUiFlags());
}