Skip to content

Commit dca9bce

Browse files
authored
update(menus): Add Kinds (#76)
Add support for both center and screen menus.
2 parents a7a03b0 + 5d60ae0 commit dca9bce

File tree

19 files changed

+623
-280
lines changed

19 files changed

+623
-280
lines changed

plugin_files/configs/core.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
"option": false,
4040
"button": "tab"
4141
}
42-
}
42+
},
43+
"kind": "screen",
44+
"available_kinds": ["screen", "center"]
4345
}
4446
}

plugin_files/translations/translation.core.json

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,25 @@
5959
"pl": "Wyjście",
6060
"de": "Beenden"
6161
},
62-
"menu.footer": {
62+
"menu.center.footer": {
63+
"en": "{CYCLE_BUTTON} - Cycle | {USE_BUTTON} - Select | Page {PAGE}/{MAXPAGES}",
64+
"ro": "{CYCLE_BUTTON} - Schimbă | {USE_BUTTON} - Selectează | Pagina {PAGE}/{MAXPAGES}",
65+
"pl": "{CYCLE_BUTTON} - Przeglądaj | {USE_BUTTON} - Wybierz | Strona {PAGE}/{MAXPAGES}",
66+
"de": "{CYCLE_BUTTON} - Durchblättern | {USE_BUTTON} - Auswählen | Seite {PAGE}/{MAXPAGES}"
67+
},
68+
"menu.center.footer.nooption": {
69+
"en": "{CYCLE_BUTTON} - Cycle | {USE_BUTTON} - Select | {EXIT_BUTTON} - Exit | Page {PAGE}/{MAXPAGES}",
70+
"ro": "{CYCLE_BUTTON} - Schimbă | {USE_BUTTON} - Selectează | {EXIT_BUTTON} - Ieșire | Pagina {PAGE}/{MAXPAGES}",
71+
"pl": "{CYCLE_BUTTON} - Przeglądaj | {USE_BUTTON} - Wybierz | {EXIT_BUTTON} - Wyjście | Strona {PAGE}/{MAXPAGES}",
72+
"de": "{CYCLE_BUTTON} - Durchblättern | {USE_BUTTON} - Auswählen | {EXIT_BUTTON} - Beenden | Seite {PAGE}/{MAXPAGES}"
73+
},
74+
"menu.screen.footer": {
6375
"en": "{CYCLE_BUTTON} - Cycle | {USE_BUTTON} - Select\nPage {PAGE}/{MAXPAGES}",
6476
"ro": "{CYCLE_BUTTON} - Schimbă | {USE_BUTTON} - Selectează\nPagina {PAGE}/{MAXPAGES}",
6577
"pl": "{CYCLE_BUTTON} - Przeglądaj | {USE_BUTTON} - Wybierz\nStrona {PAGE}/{MAXPAGES}",
6678
"de": "{CYCLE_BUTTON} - Durchblättern | {USE_BUTTON} - Auswählen\nSeite {PAGE}/{MAXPAGES}"
6779
},
68-
"menu.footer.nooption": {
80+
"menu.screen.footer.nooption": {
6981
"en": "{CYCLE_BUTTON} - Cycle | {USE_BUTTON} - Select\n{EXIT_BUTTON} - Exit | Page {PAGE}/{MAXPAGES}",
7082
"ro": "{CYCLE_BUTTON} - Schimbă | {USE_BUTTON} - Selectează\n{EXIT_BUTTON} - Ieșire | Pagina {PAGE}/{MAXPAGES}",
7183
"pl": "{CYCLE_BUTTON} - Przeglądaj | {USE_BUTTON} - Wybierz\n{EXIT_BUTTON} - Wyjście | Strona {PAGE}/{MAXPAGES}",

src/entrypoint.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,9 @@ void Swiftly::Hook_GameFrame(bool simulating, bool bFirstTick, bool bLastTick)
440440
auto buttonStates = pawn->m_pMovementServices()->m_nButtons().m_pButtonStates();
441441
player->SetButtons(buttonStates[0]);
442442

443-
if (player->HasCenterText())
443+
if(player->menu_renderer->ShouldRenderEachTick())
444+
player->menu_renderer->RenderMenuTick();
445+
else if (player->HasCenterText())
444446
player->RenderCenterText(time);
445447

446448
if(!pawn->m_pObserverServices()) continue;

src/player/player/Player.cpp

Lines changed: 2 additions & 193 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ Player::Player(bool m_isFakeClient, int m_slot, const char* m_name, uint64 m_xui
6767
this->language = g_Config->FetchValue<std::string>("core.language");
6868

6969
centerMessageEvent = g_gameEventManager->CreateEvent("show_survival_respawn_status", true);
70+
menu_renderer = new MenuRenderer(this);
7071

7172
centerMessageEvent->SetUint64("duration", 1);
7273
centerMessageEvent->SetInt("userid", this->slot);
@@ -78,18 +79,7 @@ Player::~Player()
7879
{
7980
this->isFakeClient = true;
8081
g_gameEventManager->FreeEvent(centerMessageEvent);
81-
if(menuTextID != 0) {
82-
g_pVGUI->DeleteScreenText(menuTextID);
83-
}
84-
if(menuPanelID != 0) {
85-
g_pVGUI->DeleteScreenPanel(menuPanelID);
86-
}
87-
if(menuFooterID != 0) {
88-
g_pVGUI->DeleteScreenText(menuFooterID);
89-
}
90-
if(menuPanelExtendID != 0) {
91-
g_pVGUI->DeleteScreenPanel(menuPanelExtendID);
92-
}
82+
if(menu_renderer) delete menu_renderer;
9383
}
9484

9585
CBasePlayerController* Player::GetController()
@@ -375,187 +365,6 @@ void Player::SetButtons(uint64_t new_buttons)
375365
}
376366
}
377367

378-
bool Player::HasMenuShown() { return (this->menu != nullptr); }
379-
Menu* Player::GetMenu() { return this->menu; }
380-
381-
int Player::GetPage() { return this->page; }
382-
void Player::SetPage(int pg)
383-
{
384-
this->page = pg;
385-
this->selected = 0;
386-
this->menu->RegeneratePage(this->slot, this->page, this->selected);
387-
this->RegenerateMenu();
388-
}
389-
int Player::GetSelection() { return this->selected; }
390-
void Player::MoveSelection()
391-
{
392-
if (this->page == 0)
393-
return;
394-
395-
int itemsPerPage = this->menu->GetItemsOnPage(this->page);
396-
++this->selected;
397-
if (itemsPerPage == this->selected)
398-
this->selected = 0;
399-
400-
this->menu->RegeneratePage(this->slot, this->page, this->selected);
401-
this->RegenerateMenu();
402-
}
403-
404-
void Player::RegenerateMenu()
405-
{
406-
auto menuText = g_pVGUI->GetScreenText(menuTextID);
407-
if(!menuText) return;
408-
409-
menuText->SetText(this->menu->GeneratedItems(this->slot, this->page));
410-
411-
auto menuFooter = g_pVGUI->GetScreenText(menuFooterID);
412-
if(!menuFooter) return;
413-
menuFooter->SetText(this->menu->GenerateFooter(this->page));
414-
}
415-
416-
void Player::ShowMenu(std::string menuid)
417-
{
418-
if (this->menu != nullptr)
419-
return;
420-
421-
Menu* m = g_MenuManager->FetchMenu(menuid);
422-
if (m == nullptr)
423-
return;
424-
425-
this->menu = m;
426-
this->page = 1;
427-
this->selected = 0;
428-
429-
this->menu->RegeneratePage(this->slot, this->page, this->selected);
430-
431-
menuTextID = g_pVGUI->RegisterScreenText();
432-
menuPanelID = g_pVGUI->RegisterScreenPanel();
433-
menuPanelExtendID = g_pVGUI->RegisterScreenPanel();
434-
menuFooterID = g_pVGUI->RegisterScreenText();
435-
436-
auto menuText = g_pVGUI->GetScreenText(menuTextID);
437-
auto menuPanel = g_pVGUI->GetScreenPanel(menuPanelID);
438-
auto menuPanelExtend = g_pVGUI->GetScreenPanel(menuPanelExtendID);
439-
auto menuFooter = g_pVGUI->GetScreenText(menuFooterID);
440-
441-
menuFooter->Create(Color(255,255,255,255));
442-
menuFooter->SetupViewForPlayer(this);
443-
444-
menuText->Create(this->menu->GetColor());
445-
menuText->SetupViewForPlayer(this);
446-
RegenerateMenu();
447-
menuText->SetPosition(0.14, 0.68);
448-
449-
menuFooter->SetPosition(0.14, 0.27);
450-
451-
menuPanel->Create(Color(18, 18, 18, 255));
452-
menuPanel->SetupViewForPlayer(this);
453-
menuPanel->SetText("");
454-
menuPanel->SetPosition(0.13, 0.7);
455-
456-
menuPanelExtend->Create(Color(18, 18, 18, 255));
457-
menuPanelExtend->SetupViewForPlayer(this);
458-
menuPanelExtend->SetText("");
459-
menuPanelExtend->SetPosition(0.17, 0.7);
460-
}
461-
462-
void Player::HideMenu()
463-
{
464-
if (this->menu == nullptr)
465-
return;
466-
467-
this->page = 0;
468-
this->selected = 0;
469-
if (this->menu->IsTemporary())
470-
{
471-
std::string menuID = this->menu->GetID();
472-
g_MenuManager->UnregisterMenu(menuID);
473-
}
474-
this->menu = nullptr;
475-
476-
g_pVGUI->DeleteScreenText(menuFooterID);
477-
g_pVGUI->DeleteScreenText(menuTextID);
478-
g_pVGUI->DeleteScreenPanel(menuPanelID);
479-
g_pVGUI->DeleteScreenPanel(menuPanelExtendID);
480-
481-
menuTextID = 0;
482-
menuPanelID = 0;
483-
menuFooterID = 0;
484-
menuPanelExtendID = 0;
485-
}
486-
487-
void Player::PerformMenuAction(std::string button)
488-
{
489-
if (!this->HasMenuShown())
490-
return;
491-
492-
if (button == g_Config->FetchValue<std::string>("core.menu.buttons.scroll"))
493-
{
494-
CCSPlayerController* controller = this->GetPlayerController();
495-
CSingleRecipientFilter filter(this->slot);
496-
if (controller)
497-
controller->EmitSoundFilter(filter, g_Config->FetchValue<std::string>("core.menu.sound.scroll.name"), 1.0, g_Config->FetchValue<double>("core.menu.sound.scroll.volume"));
498-
499-
this->MoveSelection();
500-
this->RegenerateMenu();
501-
}
502-
else if (!g_Config->FetchValue<bool>("core.menu.buttons.exit.option") && button == g_Config->FetchValue<std::string>("core.menu.buttons.exit.button"))
503-
{
504-
CCSPlayerController* controller = this->GetPlayerController();
505-
CSingleRecipientFilter filter(this->slot);
506-
if (controller)
507-
controller->EmitSoundFilter(filter, g_Config->FetchValue<std::string>("core.menu.sound.exit.name"), 1.0, g_Config->FetchValue<double>("core.menu.sound.exit.volume"));
508-
this->HideMenu();
509-
}
510-
else if (button == g_Config->FetchValue<std::string>("core.menu.buttons.use"))
511-
{
512-
std::string cmd = this->GetMenu()->GetCommandFromOption(this->GetPage(), this->GetSelection());
513-
CCSPlayerController* controller = this->GetPlayerController();
514-
CSingleRecipientFilter filter(this->slot);
515-
if (controller && cmd != "menuexit")
516-
controller->EmitSoundFilter(filter, g_Config->FetchValue<std::string>("core.menu.sound.use.name"), 1.0, g_Config->FetchValue<double>("core.menu.sound.use.volume"));
517-
if (cmd == "menunext")
518-
{
519-
this->SetPage(this->GetPage() + 1);
520-
this->RegenerateMenu();
521-
}
522-
else if (cmd == "menuback")
523-
{
524-
this->SetPage(this->GetPage() - 1);
525-
this->RegenerateMenu();
526-
}
527-
else if (g_Config->FetchValue<bool>("core.menu.buttons.exit.option") && cmd == "menuexit")
528-
{
529-
CCSPlayerController* controller = this->GetPlayerController();
530-
CSingleRecipientFilter filter(this->slot);
531-
if (controller)
532-
controller->EmitSoundFilter(filter, g_Config->FetchValue<std::string>("core.menu.sound.exit.name"), 1.0, g_Config->FetchValue<double>("core.menu.sound.exit.volume"));
533-
this->HideMenu();
534-
}
535-
else if (g_MenuManager->FetchMenu(cmd))
536-
{
537-
this->HideMenu();
538-
this->ShowMenu(cmd);
539-
}
540-
else if (starts_with(cmd, "sw_"))
541-
{
542-
CCommand tokenizedArgs;
543-
tokenizedArgs.Tokenize(cmd.c_str());
544-
545-
std::vector<std::string> cmdString = TokenizeCommand(cmd);
546-
cmdString.erase(cmdString.begin());
547-
548-
std::string commandName = replace(tokenizedArgs[0], "sw_", "");
549-
550-
Command* cmd = g_commandsManager->FetchCommand(commandName);
551-
if (cmd)
552-
cmd->Execute(this->slot, cmdString, true, "sw_");
553-
}
554-
else if (cmd != "")
555-
this->PerformCommand(cmd);
556-
}
557-
}
558-
559368
void Player::PerformCommand(std::string command)
560369
{
561370
engine->ClientCommand(this->GetSlot(), command.c_str());

src/player/player/Player.h

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "../../sdk/entity/CCSPlayerPawnBase.h"
99
#include "../../sdk/entity/CBaseViewModel.h"
1010
#include "../../server/menus/Menu.h"
11+
#include "../../server/menus/MenuRenderer.h"
1112

1213
#include <string>
1314
#include <public/mathlib/vector.h>
@@ -82,19 +83,6 @@ class Player
8283

8384
bool HasCenterText();
8485

85-
void ShowMenu(std::string menuid);
86-
void HideMenu();
87-
bool HasMenuShown();
88-
Menu* GetMenu();
89-
void RegenerateMenu();
90-
91-
int GetPage();
92-
void SetPage(int pg);
93-
int GetSelection();
94-
void MoveSelection();
95-
96-
void PerformMenuAction(std::string button);
97-
9886
void PerformCommand(std::string command);
9987

10088
void SetClientConvar(std::string cmd, std::string val);
@@ -112,6 +100,7 @@ class Player
112100
CPlayerBitVec m_selfMutes[64] = {};
113101

114102
std::string language = "";
103+
MenuRenderer* menu_renderer = nullptr;
115104

116105
private:
117106
int slot;
@@ -130,14 +119,6 @@ class Player
130119

131120
bool firstSpawn = true;
132121

133-
Menu* menu = nullptr;
134-
int page = 0;
135-
int selected = 0;
136-
uint64_t menuTextID = 0;
137-
uint64_t menuFooterID = 0;
138-
uint64_t menuPanelID = 0;
139-
uint64_t menuPanelExtendID = 0;
140-
141122
uint64_t buttons = 0;
142123

143124
std::map<std::string, std::any> internalVars;

src/plugins/core/scripting.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,8 @@ class PluginMenus
535535
public:
536536
PluginMenus(std::string m_plugin_name);
537537

538-
void Register(std::string custom_id, std::string title, std::string color, std::vector<std::pair<std::string, std::string>> options);
539-
void RegisterTemporary(std::string custom_id, std::string title, std::string color, std::vector<std::pair<std::string, std::string>> options);
538+
void Register(std::string custom_id, std::string title, std::string color, std::vector<std::pair<std::string, std::string>> options, std::optional<std::string> okind);
539+
void RegisterTemporary(std::string custom_id, std::string title, std::string color, std::vector<std::pair<std::string, std::string>> options, std::optional<std::string> okind);
540540
void Unregister(std::string id);
541541
};
542542

src/plugins/core/scripting/engine/gameevents.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void OnClientKeyStateChange(int playerid, std::string key, bool pressed)
1313
if (!player)
1414
return;
1515

16-
player->PerformMenuAction(key);
16+
player->menu_renderer->PerformMenuAction(key);
1717
}
1818

1919
if (noReturnEvent == nullptr) noReturnEvent = new PluginEvent("core", nullptr, nullptr);

src/plugins/core/scripting/player/player.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,10 @@ void PluginPlayer::HideMenu()
266266
if (!player)
267267
return;
268268

269-
if (!player->HasMenuShown())
269+
if (!player->menu_renderer->HasMenuShown())
270270
return;
271271

272-
player->HideMenu();
272+
player->menu_renderer->HideMenu();
273273
}
274274

275275
bool PluginPlayer::IsFakeClient()
@@ -331,10 +331,10 @@ void PluginPlayer::ShowMenu(std::string menuid)
331331
if (!player)
332332
return;
333333

334-
if (player->HasMenuShown())
334+
if (player->menu_renderer->HasMenuShown())
335335
return;
336336

337-
player->ShowMenu(menuid);
337+
player->menu_renderer->ShowMenu(menuid);
338338
}
339339

340340
void PluginPlayer::SwitchTeam(int team)
@@ -469,12 +469,12 @@ void PluginPlayer::PerformMenuAction(std::string action, int value)
469469
if (!self) return;
470470

471471
if(action == "useOption") {
472-
while(self->GetSelection() != value-1)
473-
self->MoveSelection();
472+
while(self->menu_renderer->GetSelection() != value-1)
473+
self->menu_renderer->MoveSelection();
474474

475-
self->PerformMenuAction(g_Config->FetchValue<std::string>("core.menu.buttons.use"));
475+
self->menu_renderer->PerformMenuAction(g_Config->FetchValue<std::string>("core.menu.buttons.use"));
476476
} else if(action == "scrollToOption") {
477-
while(self->GetSelection() != value-1)
478-
self->MoveSelection();
477+
while(self->menu_renderer->GetSelection() != value-1)
478+
self->menu_renderer->MoveSelection();
479479
}
480480
}

0 commit comments

Comments
 (0)