-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartUI.cpp
104 lines (92 loc) · 3.34 KB
/
StartUI.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
#include "Game.h"
StartUI::StartUI(sf::RenderWindow& win) : window(win) {
loadResources(
"./access/others/start_textures_atlas.png",
"./access/others/start_anchors.txt",
"./access/others/button/button.png",
"./access/others/button/button_hover.png",
"./access/others/button/button_pressed.png"
);
}
void StartUI::loadResources(const std::string& textureFile, const std::string& anchorsFile,
const std::string& buttonFile1, const std::string& buttonFile2, const std::string& buttonFile3) {
// 加载纹理
if (!texture.loadFromFile(textureFile)) {
std::cerr << "Failed to load texture file: " << textureFile << std::endl;
return;
}
sprite.setTexture(texture);
// 加载动画帧锚框
std::ifstream anchorInput(anchorsFile);
if (!anchorInput.is_open()) {
std::cerr << "Failed to open rects file: " << anchorsFile << std::endl;
return;
}
int x1, y1, width, height, index = 1;
char delimiter;
anchors.resize(91);
while (anchorInput >> index >> delimiter >> delimiter >> x1 >> delimiter >> y1 >> delimiter >> width >> delimiter >> height >> delimiter) {
if (index >= anchors.size()) {
anchors.resize(index + 1);
}
anchors[index] = sf::IntRect(x1, y1, width, height);
}
anchorInput.close();
// 加载按钮纹理
buttonTextures[0].loadFromFile(buttonFile1);
buttonTextures[1].loadFromFile(buttonFile2);
buttonTextures[2].loadFromFile(buttonFile3);
// 初始化按钮
startButton.setSize(sf::Vector2f(650, 140));
startButton.setTexture(&buttonTextures[0]); // 默认状态
startButton.setPosition(450, 593);
}
bool StartUI::update(float deltaTime) {
// 动画轮播
if (frameClock.getElapsedTime().asSeconds() > frameDuration) {
currentFrame++;
if (mouseClicked) {
if (currentFrame < 65) currentFrame = 67;
if (currentFrame >= anchors.size()) currentFrame = 76;
}
else if (currentFrame >= 65) currentFrame = 50;
sprite.setTextureRect(anchors[currentFrame]);
frameClock.restart();
}
// 检测鼠标点击
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
mouseClicked = true;
}
// 按钮状态更新
sf::Vector2i mousePos = sf::Mouse::getPosition(window);
sf::FloatRect buttonBounds = startButton.getGlobalBounds();
if (buttonBounds.contains((float)mousePos.x, (float)mousePos.y)) {
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
startButton.setTexture(&buttonTextures[2]); // 按下状态
} else {
startButton.setTexture(&buttonTextures[1]); // hover状态
}
} else {
startButton.setTexture(&buttonTextures[0]); // 默认状态
}
// 检测按钮点击完成
if (mouseClicked && sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
if (buttonBounds.contains((float)mousePos.x, (float)mousePos.y)) {
// 点击完成,切换到角色选择状态
mouseClicked = false;
currentFrame = 0;
// 触发状态变更逻辑
return true;
}
}
return false;
}
void StartUI::render() {
window.clear();
sprite.setScale(2.f, 2.f);
window.draw(sprite);
if (mouseClicked) {
window.draw(startButton);
}
window.display();
}