-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenu.cpp
More file actions
662 lines (564 loc) · 19.7 KB
/
Menu.cpp
File metadata and controls
662 lines (564 loc) · 19.7 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
#include "Menu.hpp"
#include "Core.hpp"
#include "MySocket.cpp"
#include <iostream>
namespace rpf {
#pragma region ClickableMenu
ClickableMenu::ClickableMenu(RenderManager* _rm, ResourceHolder* _rh) {
this->rm = _rm;
this->rh = _rh;
m_text_index = -1;
}
void ClickableMenu::handleEvent(sf::Event e) {
switch (e.type) {
case sf::Event::KeyPressed:
onKeyDown(e.key.code);
break;
case sf::Event::MouseMoved:
onMouseMove(e.mouseMove);
break;
case sf::Event::MouseButtonPressed:
onMouseClick();
break;
}
}
void ClickableMenu::update() {
for (Text* text : m_clickable_texts) {
text->updateText();
}
}
int ClickableMenu::changeTextIndex(int new_index) {
this->m_text_index = new_index;
return this->m_text_index;
}
void ClickableMenu::onKeyDown(sf::Keyboard::Key keycode) {
switch (keycode) {
case sf::Keyboard::Up:
if (changeTextIndex(m_text_index - 1) < 0)
changeTextIndex(this->getClickableSize() - 1);
break;
case sf::Keyboard::Down:
if (changeTextIndex(m_text_index + 1) > this->getClickableSize() - 1)
changeTextIndex(0);
break;
case sf::Keyboard::Enter:
EnterPressed(m_text_index);
break;
default:
otherKeyDown(keycode);
break;
}
}
void ClickableMenu::onMouseMove(sf::Event::MouseMoveEvent mouse) {
if (rm->view) {
mouse.x += rm->view->getCenter().x - rh->s_width / 2;
mouse.y += rm->view->getCenter().y - rh->s_height / 2;
}
for (auto t : this->getBaseClickable()) {
if (t->isPosIn(sf::Vector2i(mouse.x, mouse.y))) {
changeTextIndex(t->getId());
break;
}
}
}
void ClickableMenu::onMouseClick() {
sf::Vector2i mousepos = sf::Mouse::getPosition(*rm->window);
if (rm->view) {
mousepos.x += rm->view->getCenter().x - rh->s_width / 2;
mousepos.y += rm->view->getCenter().y - rh->s_height / 2;
}
if (this->getBaseClickable()[m_text_index]->isPosIn(mousepos))
EnterPressed(m_text_index);
}
#pragma endregion
#pragma region Text/Textbox
Text::Text(sf::String text, Pos position, sf::Font font) {
this->m_id = -1;//default
this->m_font = font;
this->grap = sf::Text(text, m_font, 30U);//default
this->grap.setOrigin(grap.getLocalBounds().width / 2, grap.getLocalBounds().height / 2);
this->grap.setPosition(position.x, position.y);
this->index = nullptr;
}
void Text::setId(int id) {
this->m_id = id;
}
void Text::setTextIndexPointer(int* text_index) {
this->index = text_index;
}
void Text::setTextSize(unsigned int size) {
this->grap.setCharacterSize(size);
this->grap.setOrigin(grap.getLocalBounds().width / 2, grap.getLocalBounds().height / 2);
this->TextChanged();
}
void Text::setTextColor(sf::Color color) {
this->grap.setFillColor(color);
}
int Text::getId() {
return m_id;
}
void Text::updateText() {
if (this->index) {
if (*this->index == m_id) {
this->setTextColor(sf::Color::Yellow);
this->grap.setScale(1.1f, 1.1f); // 聚焦時放大
}
else {
this->setTextColor(sf::Color::White);
this->grap.setScale(1.0f, 1.0f); // 恢復原大小
}
}
}
bool Text::isPosIn(sf::Vector2i pos) {
sf::Vector2f grapPos = grap.getPosition();
sf::FloatRect grap_rect = grap.getLocalBounds();
pos.x -= grapPos.x + grap_rect.left - grap_rect.width / 2;
pos.y -= grapPos.y + grap_rect.top - grap_rect.height / 2;
return 0 <= pos.x && pos.x <= grap_rect.width &&
0 <= pos.y && pos.y <= grap_rect.height;
}
TextBox::TextBox(Pos position, sf::Font font, unsigned int size, RenderManager* rm) {
this->m_font = font;
this->rm = rm;
// 初始化框
box.setSize(sf::Vector2f(300.f, 50.f));
box.setFillColor(sf::Color::White);
box.setOutlineColor(sf::Color::Black);
box.setOutlineThickness(2.f);
box.setOrigin(box.getSize().x / 2, box.getSize().y / 2); // 設置中心原點
box.setPosition(position.x, position.y);
// 初始化輸入文本
inputText = sf::Text(sf::String(), m_font, size);
inputText.setFillColor(sf::Color::Black);
inputText.setPosition(box.getPosition().x - box.getSize().x / 2 + 10, box.getPosition().y - inputText.getCharacterSize() / 2);
// 初始化占位符文本
placeholderText = sf::Text("", m_font, size);
placeholderText.setFillColor(sf::Color(150, 150, 150));
placeholderText.setPosition(inputText.getPosition());
isFocused = false;
}
void TextBox::updateVisuals() {
if (isFocused) {
box.setOutlineColor(sf::Color::Blue); // 聚焦時外框顏色變藍
box.setOutlineThickness(5.f); // 增加框線厚度
}
else {
box.setOutlineColor(sf::Color::Black); // 失焦時恢復黑色
box.setOutlineThickness(2.f); // 恢復框線厚度
}
}
void TextBox::handleEvent(sf::Event e) {
if (e.type == sf::Event::MouseButtonPressed) {
sf::Vector2i pixelPos = sf::Mouse::getPosition(*rm->window);
sf::Vector2f worldPos = rm->window->mapPixelToCoords(pixelPos);
isFocused = box.getGlobalBounds().contains(worldPos);
updateVisuals();
}
else if (isFocused && e.type == sf::Event::TextEntered) {
if (e.text.unicode == '\b' && !inputText.getString().isEmpty()) {
// Backspace 處理
sf::String text = inputText.getString();
text.erase(text.getSize() - 1);
inputText.setString(text);
}
else if (e.text.unicode >= 32 && e.text.unicode < 128) {
// 限制 ASCII 輸入範圍
inputText.setString(inputText.getString() + e.text.unicode);
}
}
// 占位符顯示
if (inputText.getString().isEmpty()) {
placeholderText.setFillColor(sf::Color(150, 150, 150));
}
else {
placeholderText.setFillColor(sf::Color::Transparent);
}
}
void TextBox::setPlaceholder(const sf::String& text) {
placeholderText.setString(text);
}
sf::String TextBox::getInputText() const {
return inputText.getString();
}
sf::RectangleShape* TextBox::getBox() {
return &box;
}
sf::Text* TextBox::getInputTextGraphic() {
return &inputText;
}
sf::Text* TextBox::getPlaceholderTextGraphic() {
return &placeholderText;
}
#pragma endregion
#pragma region effects
void createGradientBackground(sf::VertexArray& background, sf::Color topColor, sf::Color bottomColor, sf::Vector2f size) {
background.setPrimitiveType(sf::Quads);
background.resize(4);
background[0].position = sf::Vector2f(0, 0);
background[1].position = sf::Vector2f(size.x, 0);
background[2].position = sf::Vector2f(size.x, size.y);
background[3].position = sf::Vector2f(0, size.y);
background[0].color = topColor;
background[1].color = topColor;
background[2].color = bottomColor;
background[3].color = bottomColor;
}
std::vector<sf::CircleShape> stars;
void createStarfield(int numStars, sf::Vector2f size, std::vector<sf::CircleShape>& stars) {
stars.clear();
for (int i = 0; i < numStars; ++i) {
sf::CircleShape star(1.f);
star.setFillColor(sf::Color::White);
star.setPosition(rand() % static_cast<int>(size.x), rand() % static_cast<int>(size.y));
stars.push_back(star);
}
}
void updateStarfield(std::vector<sf::CircleShape>& stars, sf::Vector2f size) {
for (auto& star : stars) {
star.move(0.f, 0.5f); // 星星向下移動
if (star.getPosition().y > size.y) {
star.setPosition(rand() % static_cast<int>(size.x), 0); // 重置到頂部
}
}
}
void drawStarfield(RenderManager* rm, std::vector<sf::CircleShape>& stars) {
for (auto& star : stars) {
rm->addGraphics(&star);
}
}
#pragma endregion
#pragma region MainMenu
MainMenu::MainMenu(RenderManager* rm, ResourceHolder* rh) : ClickableMenu(rm, rh) {
this->rm = rm;
this->rh = rh;
this->initMenu();
}
void MainMenu::initMenu() {
sf::VertexArray* gradient = new sf::VertexArray();
createGradientBackground(*gradient, sf::Color(30, 30, 120), sf::Color(10, 10, 40), sf::Vector2f(rh->s_width, rh->s_height));
rm->addGraphics(gradient);
createStarfield(100, sf::Vector2f(rh->s_width, rh->s_height), stars);
drawStarfield(rm, stars);
sf::RectangleShape* rect = new sf::RectangleShape(sf::Vector2f(rh->s_width, rh->s_height));
rect->setFillColor(sf::Color::Cyan);
rect->setOrigin(rh->s_width / 2, 0);
rect->setPosition(rh->view->getCenter().x, 0);
rm->addGraphics(rect);
sf::String texts[] = { "Play alone", "Play with others", "Quit the game" };
Pos position = Pos(rh->s_width / 2, rh->s_height / 4);
unsigned int text_size;
text_size = static_cast<unsigned int>(30.f * position.x / 500);
for (int i = 0; i < 3; i++) {
Text* tmp = new Text(texts[i], position.AddY(text_size * 2), rh->font);
tmp->setTextSize(30U);
tmp->setId(i);
tmp->setTextIndexPointer(&m_text_index);
m_clickable_texts.push_back(tmp);
}
for (Text* t : m_clickable_texts)
rm->addGraphics(&t->getDrawable());
m_text_index = 0;
}
void MainMenu::update() {
updateStarfield(stars, sf::Vector2f(rh->s_width, rh->s_height));
ClickableMenu::update();
}
void MainMenu::EnterPressed(int index) {
if (index == 0)
Core::CORE->switchMode(Mode::SINGLE_GAME);
else if (index == 1)
Core::CORE->switchMode(Mode::CONNECTION_MENU);
else if (index == 2)
Core::CORE->switchMode(Mode::CLOSED);
}
#pragma endregion
#pragma region GameOverMenu
GameOverMenu::GameOverMenu(RenderManager* rm, ResourceHolder* rh) : ClickableMenu(rm, rh) {
this->rm = rm;
this->rh = rh;
this->initMenu();
}
void GameOverMenu::EnterPressed(int index) {
if (index == 0)
Core::CORE->switchMode(Mode::MAIN_MENU);
else if (index == 1)
Core::CORE->switchMode(Mode::CLOSED);
}
void GameOverMenu::initMenu() {
sf::RectangleShape* rect = new sf::RectangleShape(sf::Vector2f(rh->s_width, rh->s_height));
rect->setFillColor(sf::Color(0x00000088));
rect->setOrigin(rh->s_width / 2, 0);
rect->setPosition(rh->view->getCenter().x, 0);
rm->addGraphics(rect);
sf::String texts[] = { "Back to Menu"/*, L"回到選單"*/, "Quit the game" };
Pos text_position(rh->view->getCenter().x, rh->view->getCenter().y - rh->s_height / 4);
unsigned int text_size;
text_size = static_cast<unsigned int>(30.f * text_position.x / 500);
for (int i = 0; i < 2; i++) {
Text* tmp = new Text(texts[i], text_position.AddY(text_size * 2), rh->font);
tmp->setTextSize(30U);
tmp->setId(i);
tmp->setTextIndexPointer(&m_text_index);
m_clickable_texts.push_back(tmp);
}
for (Text* t : m_clickable_texts)
rm->addGraphics(&t->getDrawable());
m_text_index = 0;
}
#pragma endregion
#pragma region GameOverMulMenu
GameOverMulMenu::GameOverMulMenu(RenderManager* rm, ResourceHolder* rh) : ClickableMenu(rm, rh) {
this->rm = rm;
this->rh = rh;
this->initMenu();
}
void GameOverMulMenu::EnterPressed(int index) {
if (index == 0)
Core::CORE->switchMode(Mode::MAIN_MENU);
else if (index == 1)
Core::CORE->switchMode(Mode::CLOSED);
}
void GameOverMulMenu::initMenu() {
sf::RectangleShape* rect = new sf::RectangleShape(sf::Vector2f(rh->s_width, rh->s_height));
rect->setFillColor(sf::Color(0x00000088));
rect->setOrigin(rh->s_width / 2, 0);
rect->setPosition(rh->view->getCenter().x, 0);
rm->addGraphics(rect);
Pos text_position_0(rh->view->getCenter().x, rh->view->getCenter().y - rh->s_height / 3);
std::vector<std::pair<int, float>>& v = Core::CORE->results;
for (int i = 1; i <= v.size(); i++) {
int id = v[i - 1].first;
float _time = v[i - 1].second;
std::string txt = std::to_string(i) + ".Player" + std::to_string(id) + " " + std::to_string(_time) + "s";
sf::Text* text = new sf::Text(txt, rh->font);
text->setOrigin(text->getLocalBounds().width / 2, text->getLocalBounds().height / 2);
text->setPosition(text_position_0.x, text_position_0.y);
text_position_0.y += 50;
rm->addGraphics(text);
}
sf::String texts[] = { "Back to Menu", "Quit the game" };
Pos text_position(rh->view->getCenter().x, rh->view->getCenter().y + rh->s_height / 5);
unsigned int text_size;
text_size = 30U;
for (int i = 0; i < 2; i++) {
Text* tmp = new Text(texts[i], text_position.AddY(text_size * 2), rh->font);
tmp->setTextSize(30U);
tmp->setId(i);
tmp->setTextIndexPointer(&m_text_index);
m_clickable_texts.push_back(tmp);
}
for (Text* t : m_clickable_texts)
rm->addGraphics(&t->getDrawable());
m_text_index = 0;
}
#pragma endregion
#pragma region ConnectionMenu
ConnectionMenu::ConnectionMenu(RenderManager* rm, ResourceHolder* rh) : ClickableMenu(rm, rh) {
this->rm = rm;
this->rh = rh;
this->initMenu();
}
void ConnectionMenu::initMenu() {
sf::VertexArray* gradient = new sf::VertexArray();
createGradientBackground(*gradient, sf::Color(30, 30, 120), sf::Color(10, 10, 40), sf::Vector2f(rh->s_width, rh->s_height));
rm->addGraphics(gradient);
createStarfield(100, sf::Vector2f(rh->s_width, rh->s_height), stars);
drawStarfield(rm, stars);
sf::RectangleShape* rect = new sf::RectangleShape(sf::Vector2f(rh->s_width, rh->s_height));
rect->setFillColor(sf::Color::Blue);
rect->setOrigin(rh->s_width / 2, 0);
rect->setPosition(rh->view->getCenter().x, 0);
rm->addGraphics(rect);
errorText = sf::Text("", rh->font, 20U);
errorText.setFillColor(sf::Color::Transparent);
rm->addGraphics(&errorText);
Pos position(rh->s_width / 2, rh->s_height / 4);
textBox = new TextBox(position.AddY(50), rh->font, 20U, rm);
textBox->setPlaceholder("Enter IP Address");
textBox->getBox()->setOrigin(textBox->getBox()->getSize().x / 2, textBox->getBox()->getSize().y / 2);
textBox->getInputTextGraphic()->setPosition(position.x - 140, position.y - 15);
rm->addGraphics(textBox->getBox());
rm->addGraphics(textBox->getInputTextGraphic());
rm->addGraphics(textBox->getPlaceholderTextGraphic());
sf::String texts[] = { "Connect", "Back", "Create Game" };
for (int i = 0; i < 3; i++) {
Text* tmp = new Text(texts[i], position.AddY(100), rh->font);
tmp->setTextSize(30U);
tmp->setId(i);
tmp->setTextIndexPointer(&m_text_index);
m_clickable_texts.push_back(tmp);
}
for (Text* t : m_clickable_texts)
rm->addGraphics(&t->getDrawable());
m_text_index = 0;
}
void ConnectionMenu::update() {
updateStarfield(stars, sf::Vector2f(rh->s_width, rh->s_height));
ClickableMenu::update();
}
void ConnectionMenu::EnterPressed(int index) {
if (index == 0) {
sf::String ip = textBox->getInputText();
std::cout << "Connecting to IP: " << ip.toAnsiString() << std::endl;
SocketManager* sock = Core::CORE->sock = new SocketManager();
bool connectionSuccess = sock->connect(ip);
if (connectionSuccess) {
int selfId = 0;//default
std::vector<int> players = { 0 };
RoomMenu* menu = new RoomMenu(rm, rh, players, selfId);
Core::CORE->switchMode(menu);
//std::thread([this, menu]() { handleMsgClient(menu); }).detach();
sock->loopClient(menu);
sock->firstConnect();
}
else {
errorText.setString("Connection failed!");
errorText.setFillColor(sf::Color::Red);
errorText.setPosition(textBox->getBox()->getPosition().x, textBox->getBox()->getPosition().y + 80);
errorText.setPosition(
textBox->getBox()->getPosition().x,
textBox->getBox()->getPosition().y + textBox->getBox()->getSize().y / 2 + 10
);
errorText.setOrigin(errorText.getLocalBounds().left + errorText.getLocalBounds().width / 2,
errorText.getLocalBounds().top + errorText.getLocalBounds().height / 2);
}
}
else if (index == 1) {
Core::CORE->switchMode(Mode::MAIN_MENU);
}
else if (index == 2) {
std::cout << "Creating a new game..." << std::endl;
SocketManager* sock = Core::CORE->sock = new SocketManager();
int selfId = sock->host();
std::vector<int> players = { selfId };
RoomMenu* menu = new RoomMenu(rm, rh, players, selfId);
Core::CORE->switchMode(menu);
sock->loopServer();
}
}
void ConnectionMenu::handleEvent(sf::Event e) {
textBox->handleEvent(e);
ClickableMenu::handleEvent(e);
}
#pragma endregion
#pragma region RoomMenu
RoomMenu::RoomMenu(RenderManager* rm, ResourceHolder* rh, std::vector<int> players, int selfId)
: ClickableMenu(rm, rh), players(players), selfId(selfId) {
rm->clear();
this->m_font = rh->font;
this->roomOwner = (selfId != 0);
this->initMenu();
}
void RoomMenu::initMenu() {
sf::VertexArray* gradient = new sf::VertexArray();
createGradientBackground(*gradient, sf::Color(30, 30, 120), sf::Color(10, 10, 40), sf::Vector2f(rh->s_width, rh->s_height));
rm->addGraphics(gradient);
createStarfield(100, sf::Vector2f(rh->s_width, rh->s_height), stars);
drawStarfield(rm, stars);
renderPlayers();
sf::String texts[] = { "Leave Room", "Start RACE"};
Pos position(rh->s_width / 2, rh->s_height - 200); // 放在底部 //-100
for (int i = 0; i < (roomOwner ? 2 : 1); i++) {
Text* tmp = new Text(texts[i], position.AddY(40), rh->font);
tmp->setTextSize(30U);
tmp->setId(i);
tmp->setTextIndexPointer(&m_text_index);
m_clickable_texts.push_back(tmp);
}
for (Text* t : m_clickable_texts)
rm->addGraphics(&t->getDrawable());
m_text_index = 0;
}
void RoomMenu::renderPlayers() {
Pos position(rh->s_width / 2, 240); // 起始位置
unsigned int textSize = 25U;
for (int playerId : players) {
sf::Text* playerText = new sf::Text();
playerText->setFont(m_font);
playerText->setString(playerId == selfId ? "Player " + std::to_string(playerId) + " (You)" : "Player " + std::to_string(playerId));
playerText->setCharacterSize(textSize);
playerText->setFillColor(playerId == selfId ? sf::Color::Green : sf::Color::White);
playerText->setOrigin(playerText->getLocalBounds().width / 2, playerText->getLocalBounds().height / 2);
playerText->setPosition(position.x, position.y);
position.y += 40; // 每個玩家往下排
playerTexts.push_back({ playerId, playerText });
rm->addGraphics(playerText);
}
}
void RoomMenu::EnterPressed(int index) {
if (index == 0) { // Leave Room
if (Core::CORE->sock) {
Core::CORE->sock->quitRoom();
}
Core::CORE->switchMode(Mode::MAIN_MENU);
/*if (Core::CORE->sock)
free(Core::CORE->sock);*///free causes bugs
}
else if (index == 1) {// Start Game
MySocket* sock = Core::CORE->sock->sock;
int N = sock->clients.size() + 1;
sock->sout << "start " << N << ' ' << sock->server_fd << ' ';
for (auto [_, id] : sock->clients)
sock->sout << id << ' ';
sock->sout << std::endl;
Core::CORE->switchMode(Mode::MULTI_GAME);
Core::CORE->sock->loopServerGaming();
for (auto [_, id] : sock->clients)
Core::GAME->poses[id] = { 0,0 };
Core::GAME->resetLvl();
}
}
void RoomMenu::update() {
updateStarfield(stars, sf::Vector2f(rh->s_width, rh->s_height));
bool repos = 0;
for (auto& i : Core::CORE->leaved)
leaved.push_back(i);
Core::CORE->leaved.clear();
for (auto& i : Core::CORE->joined)
joined.push_back(i);
Core::CORE->joined.clear();
if (!leaved.empty()) {
for (int id : leaved) {
for (auto it = playerTexts.begin(); it != playerTexts.end();) {
if (it->first == id) {
rm->delGraphic(it->second);
free(it->second);
it = playerTexts.erase(it);
}
else
it++;
}
}
leaved.clear();
repos = 1;
}
if (!joined.empty()) {
Pos position(rh->s_width / 2, 240); // 起始位置
unsigned int textSize = 25U;
for (int playerId : joined) {
sf::Text* playerText = new sf::Text();
playerText->setFont(m_font);
playerText->setString(playerId == selfId ? "Player " + std::to_string(playerId) + " (You)" : "Player " + std::to_string(playerId));
playerText->setCharacterSize(textSize);
playerText->setFillColor(playerId == selfId ? sf::Color::Green : sf::Color::White);
playerText->setOrigin(playerText->getLocalBounds().width / 2, playerText->getLocalBounds().height / 2);
playerText->setPosition(position.x, position.y);
position.y += 40; // 每個玩家往下排
playerTexts.push_back({ playerId, playerText });
rm->addGraphics(playerText);
}
joined.clear();
repos = 1;
}
if (repos) {
Pos position(rh->s_width / 2, 240); // 起始位置
for (std::pair<int, sf::Text*> element : playerTexts) {
sf::Text* playerText = element.second;
playerText->setPosition(position.x, position.y);
position.y += 40;
}
}
ClickableMenu::update();
}
#pragma endregion
}