-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitch.cpp
More file actions
182 lines (151 loc) · 6.16 KB
/
switch.cpp
File metadata and controls
182 lines (151 loc) · 6.16 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* Switch Component
*
* 可交互的开关组件示例
* 演示鼠标输入、状态管理和UI绘制
*/
#include "runtime_api.h"
#include <cmath>
class SwitchComponent : public BitHCI::IComponent {
private:
// 运行时接口
BitHCI::IRenderer* renderer = nullptr;
BitHCI::IInput* input = nullptr;
BitHCI::IWindow* window = nullptr;
// 开关状态
bool isOn = false;
bool wasMousePressed = false;
// 开关位置和尺寸
// 使用(0,0)作为中心点,native层的视图变换会自动将其映射到屏幕中心
float switchX = -50.0f; // 开关左边界(宽度100,所以中心在0)
float switchY = -25.0f; // 开关上边界(高度50,所以中心在0)
float switchWidth = 100.0f;
float switchHeight = 50.0f;
float knobRadius = 20.0f;
// 动画状态
float knobPosition = 0.0f; // 0.0 = 左侧(关), 1.0 = 右侧(开)
float animationSpeed = 8.0f;
// 颜色定义
float colorOff[4] = {0.3f, 0.3f, 0.3f, 1.0f}; // 关闭状态 - 灰色
float colorOn[4] = {0.2f, 0.8f, 0.3f, 1.0f}; // 打开状态 - 绿色
float colorKnob[4] = {0.95f, 0.95f, 0.95f, 1.0f}; // 旋钮 - 白色
float colorText[4] = {1.0f, 1.0f, 1.0f, 1.0f}; // 文本 - 白色
public:
/**
* 组件初始化
*/
void onInit(BitHCI::IWindow* window,
BitHCI::IRenderer* renderer,
BitHCI::IInput* input,
BitHCI::IResourceManager* resources) override {
this->window = window;
this->renderer = renderer;
this->input = input;
// 设置清屏颜色
renderer->setClearColor(0.15f, 0.15f, 0.2f, 1.0f);
// 开关位置固定在(0,0)中心,native层的视图变换会自动处理
// 无需手动计算窗口中心
}
/**
* 组件更新
*/
void onUpdate(float deltaTime) override {
// 开关位置固定,无需每帧更新
// 视图变换由 native 层自动处理
// 检测鼠标点击
bool isMousePressed = input->isMouseButtonPressed(0); // 左键
if (isMousePressed && !wasMousePressed) {
// 鼠标按下瞬间
float mouseX, mouseY;
input->getMousePosition(mouseX, mouseY);
// 检查是否点击在开关区域内
if (isPointInSwitch(mouseX, mouseY)) {
isOn = !isOn; // 切换状态
}
}
wasMousePressed = isMousePressed;
// 平滑动画
float targetPosition = isOn ? 1.0f : 0.0f;
if (knobPosition < targetPosition) {
knobPosition += animationSpeed * deltaTime;
if (knobPosition > targetPosition) knobPosition = targetPosition;
} else if (knobPosition > targetPosition) {
knobPosition -= animationSpeed * deltaTime;
if (knobPosition < targetPosition) knobPosition = targetPosition;
}
// 键盘控制
if (input->isKeyJustPressed(32)) { // 空格键
isOn = !isOn;
}
// ESC 退出
if (input->isKeyJustPressed(27)) {
window->close();
}
}
/**
* 组件渲染
*/
void onRender() override {
// 获取当前颜色(根据状态插值)
float currentColor[4];
for (int i = 0; i < 4; i++) {
currentColor[i] = colorOff[i] * (1.0f - knobPosition) + colorOn[i] * knobPosition;
}
// 绘制开关背景(圆角矩形效果)
drawRoundedRectangle(switchX, switchY, switchWidth, switchHeight,
switchHeight / 2.0f, currentColor);
// 计算旋钮位置
float knobX = switchX + knobRadius + (switchWidth - 2 * knobRadius) * knobPosition;
float knobY = switchY + switchHeight / 2.0f;
// 绘制旋钮
renderer->drawCircle(knobX, knobY, knobRadius, colorKnob);
// 绘制状态文本(在开关上方)
const char* statusText = isOn ? "ON" : "OFF";
renderer->drawText(statusText, -20.0f, switchY - 50.0f, colorText, 32.0f);
// 绘制说明文字(相对于视图中心)
renderer->drawText("Switch Component Demo", -150, -150, colorText, 20.0f);
renderer->drawText("Click the switch to toggle", -150, -120, colorText, 14.0f);
renderer->drawText("[Space] Toggle [ESC] Quit", -150, -100, colorText, 14.0f);
renderer->drawText("[Right Mouse] Pan View", -150, -80, colorText, 14.0f);
renderer->drawText("[Mouse Wheel] Zoom", -150, -60, colorText, 14.0f);
renderer->drawText("[Home] Reset View", -150, -40, colorText, 14.0f);
// 绘制鼠标提示
float mouseX, mouseY;
input->getMousePosition(mouseX, mouseY);
if (isPointInSwitch(mouseX, mouseY)) {
renderer->drawText("Click!", mouseX + 15, mouseY - 10, colorText, 14.0f);
}
}
/**
* 组件销毁
*/
void onDestroy() override {
// 清理资源
}
private:
/**
* 检查点是否在开关区域内
*/
bool isPointInSwitch(float x, float y) const {
return x >= switchX && x <= switchX + switchWidth &&
y >= switchY && y <= switchY + switchHeight;
}
/**
* 绘制圆角矩形
* 使用多个三角形和圆形组合实现
*/
void drawRoundedRectangle(float x, float y, float w, float h,
float radius, const float* color) {
// 简化版本: 绘制中心矩形 + 两个半圆
// 中心矩形
renderer->drawRectangle(x + radius, y, w - 2 * radius, h, color);
// 左右两个圆形(作为圆角)
renderer->drawCircle(x + radius, y + h / 2, radius, color);
renderer->drawCircle(x + w - radius, y + h / 2, radius, color);
// 上下填充矩形
renderer->drawRectangle(x, y + radius, radius, h - 2 * radius, color);
renderer->drawRectangle(x + w - radius, y + radius, radius, h - 2 * radius, color);
}
};
// 导出组件创建函数
BITHCI_COMPONENT(SwitchComponent)