252 lines
8.7 KiB
C++
252 lines
8.7 KiB
C++
#include "MenuManager.h"
|
|
|
|
|
|
namespace ZL {
|
|
|
|
MenuManager::MenuManager(Renderer& iRenderer) :
|
|
renderer(iRenderer)
|
|
{
|
|
}
|
|
|
|
void MenuManager::setupMenu()
|
|
{
|
|
|
|
uiManager.loadFromFile("resources/config/main_menu.json", renderer, CONST_ZIP_FILE);
|
|
|
|
uiSavedRoot = loadUiFromFile("resources/config/ui.json", renderer, CONST_ZIP_FILE);
|
|
|
|
settingsSavedRoot = loadUiFromFile("resources/config/settings.json", renderer, CONST_ZIP_FILE);
|
|
|
|
multiplayerSavedRoot = loadUiFromFile("resources/config/multiplayer_menu.json", renderer, CONST_ZIP_FILE);
|
|
|
|
gameOverSavedRoot = loadUiFromFile("resources/config/game_over.json", renderer, CONST_ZIP_FILE);
|
|
|
|
auto shipSelectionRoot = loadUiFromFile("resources/config/ship_selection_menu.json", renderer, CONST_ZIP_FILE);
|
|
std::function<void()> loadGameplayUI;
|
|
loadGameplayUI = [this]() {
|
|
uiManager.replaceRoot(uiSavedRoot);
|
|
|
|
auto velocityTv = uiManager.findTextView("velocityText");
|
|
if (velocityTv) {
|
|
velocityTv->rect.x = 10.0f;
|
|
velocityTv->rect.y = static_cast<float>(Environment::height) - velocityTv->rect.h - 10.0f;
|
|
}
|
|
else {
|
|
std::cerr << "Failed to find velocityText in UI" << std::endl;
|
|
}
|
|
|
|
uiManager.startAnimationOnNode("backgroundNode", "bgScroll");
|
|
static bool isExitButtonAnimating = false;
|
|
uiManager.setAnimationCallback("settingsButton", "buttonsExit", [this]() {
|
|
std::cerr << "Settings button animation finished -> переход в настройки" << std::endl;
|
|
if (uiManager.pushMenuFromSavedRoot(settingsSavedRoot)) {
|
|
uiManager.setButtonCallback("Opt1", [this](const std::string& n) {
|
|
std::cerr << "Opt1 pressed: " << n << std::endl;
|
|
});
|
|
uiManager.setButtonCallback("Opt2", [this](const std::string& n) {
|
|
std::cerr << "Opt2 pressed: " << n << std::endl;
|
|
});
|
|
uiManager.setButtonCallback("backButton", [this](const std::string& n) {
|
|
uiManager.stopAllAnimations();
|
|
uiManager.popMenu();
|
|
});
|
|
}
|
|
else {
|
|
std::cerr << "Failed to open settings menu after animations" << std::endl;
|
|
}
|
|
});
|
|
|
|
uiManager.setAnimationCallback("exitButton", "bgScroll", [this]() {
|
|
std::cerr << "Exit button bgScroll animation finished" << std::endl;
|
|
g_exitBgAnimating = false;
|
|
});
|
|
|
|
uiManager.setButtonCallback("playButton", [this](const std::string& name) {
|
|
std::cerr << "Play button pressed: " << name << std::endl;
|
|
});
|
|
|
|
uiManager.setButtonCallback("settingsButton", [this](const std::string& name) {
|
|
std::cerr << "Settings button pressed: " << name << std::endl;
|
|
uiManager.startAnimationOnNode("playButton", "buttonsExit");
|
|
uiManager.startAnimationOnNode("settingsButton", "buttonsExit");
|
|
uiManager.startAnimationOnNode("exitButton", "buttonsExit");
|
|
});
|
|
|
|
uiManager.setButtonCallback("exitButton", [this](const std::string& name) {
|
|
std::cerr << "Exit button pressed: " << name << std::endl;
|
|
|
|
if (!g_exitBgAnimating) {
|
|
std::cerr << "start repeat anim bgScroll on exitButton" << std::endl;
|
|
g_exitBgAnimating = true;
|
|
uiManager.startAnimationOnNode("exitButton", "bgScroll");
|
|
}
|
|
else {
|
|
std::cerr << "stop repeat anim bgScroll on exitButton" << std::endl;
|
|
g_exitBgAnimating = false;
|
|
uiManager.stopAnimationOnNode("exitButton", "bgScroll");
|
|
|
|
auto exitButton = uiManager.findButton("exitButton");
|
|
if (exitButton) {
|
|
exitButton->animOffsetX = 0.0f;
|
|
exitButton->animOffsetY = 0.0f;
|
|
exitButton->animScaleX = 1.0f;
|
|
exitButton->animScaleY = 1.0f;
|
|
exitButton->buildMesh();
|
|
}
|
|
}
|
|
});
|
|
|
|
uiManager.setButtonCallback("shootButton", [this](const std::string& name) {
|
|
onFirePressed();
|
|
});
|
|
uiManager.setButtonCallback("shootButton2", [this](const std::string& name) {
|
|
onFirePressed();
|
|
});
|
|
uiManager.setSliderCallback("velocitySlider", [this](const std::string& name, float value) {
|
|
int newVel = roundf(value * 10);
|
|
if (newVel > 2)
|
|
{
|
|
newVel = 2;
|
|
}
|
|
|
|
if (newVel != Environment::shipState.selectedVelocity) {
|
|
onVelocityChanged(newVel);
|
|
}
|
|
});
|
|
};
|
|
|
|
uiManager.setButtonCallback("singleButton", [this, shipSelectionRoot, loadGameplayUI](const std::string& name) {
|
|
std::cerr << "Single button pressed: " << name << " -> open ship selection UI\n";
|
|
if (!shipSelectionRoot) {
|
|
std::cerr << "Failed to load ship selection UI\n";
|
|
return;
|
|
}
|
|
if (uiManager.pushMenuFromSavedRoot(shipSelectionRoot)) {
|
|
uiManager.setButtonCallback("spaceshipButton", [this, loadGameplayUI](const std::string& btnName) {
|
|
std::string nick = uiManager.getTextFieldValue("nicknameInput");
|
|
if (nick.empty()) nick = "Player";
|
|
int shipType = 0;
|
|
uiManager.popMenu();
|
|
loadGameplayUI();
|
|
if (onSingleplayerPressed) onSingleplayerPressed(nick, shipType);
|
|
});
|
|
|
|
uiManager.setButtonCallback("cargoshipButton", [this, loadGameplayUI](const std::string& btnName) {
|
|
std::string nick = uiManager.getTextFieldValue("nicknameInput");
|
|
if (nick.empty()) nick = "Player";
|
|
int shipType = 1;
|
|
uiManager.popMenu();
|
|
loadGameplayUI();
|
|
if (onSingleplayerPressed) onSingleplayerPressed(nick, shipType);
|
|
});
|
|
|
|
uiManager.setButtonCallback("backButton", [this](const std::string& btnName) {
|
|
uiManager.popMenu();
|
|
});
|
|
}
|
|
else {
|
|
std::cerr << "Failed to push ship selection menu\n";
|
|
}
|
|
});
|
|
|
|
uiManager.setButtonCallback("multiplayerButton", [this, shipSelectionRoot, loadGameplayUI](const std::string& name) {
|
|
std::cerr << "Multiplayer button pressed: " << name << " -> open ship selection UI\n";
|
|
if (!shipSelectionRoot) {
|
|
std::cerr << "Failed to load ship selection UI\n";
|
|
return;
|
|
}
|
|
if (uiManager.pushMenuFromSavedRoot(shipSelectionRoot)) {
|
|
uiManager.setButtonCallback("spaceshipButton", [this, loadGameplayUI](const std::string& btnName) {
|
|
std::string nick = uiManager.getTextFieldValue("nicknameInput");
|
|
if (nick.empty()) nick = "Player";
|
|
int shipType = 0;
|
|
uiManager.popMenu();
|
|
loadGameplayUI();
|
|
if (onMultiplayerPressed) onMultiplayerPressed(nick, shipType);
|
|
});
|
|
|
|
uiManager.setButtonCallback("cargoshipButton", [this, loadGameplayUI](const std::string& btnName) {
|
|
std::string nick = uiManager.getTextFieldValue("nicknameInput");
|
|
if (nick.empty()) nick = "Player";
|
|
int shipType = 1;
|
|
uiManager.popMenu();
|
|
loadGameplayUI();
|
|
if (onMultiplayerPressed) onMultiplayerPressed(nick, shipType);
|
|
});
|
|
|
|
uiManager.setButtonCallback("backButton", [this](const std::string& btnName) {
|
|
uiManager.popMenu();
|
|
});
|
|
}
|
|
else {
|
|
std::cerr << "Failed to push ship selection menu\n";
|
|
}
|
|
});
|
|
|
|
uiManager.setButtonCallback("multiplayerButton2", [this](const std::string& name) {
|
|
std::cerr << "Multiplayer button pressed → opening multiplayer menu\n";
|
|
|
|
uiManager.startAnimationOnNode("playButton", "buttonsExit");
|
|
uiManager.startAnimationOnNode("settingsButton", "buttonsExit");
|
|
uiManager.startAnimationOnNode("multiplayerButton", "buttonsExit");
|
|
uiManager.startAnimationOnNode("exitButton", "buttonsExit");
|
|
|
|
if (uiManager.pushMenuFromSavedRoot(multiplayerSavedRoot)) {
|
|
|
|
uiManager.setButtonCallback("connectButton", [this](const std::string& buttonName) {
|
|
std::string serverAddress = uiManager.getTextFieldValue("serverInputField");
|
|
|
|
if (serverAddress.empty()) {
|
|
uiManager.setText("statusText", "Please enter server address");
|
|
return;
|
|
}
|
|
|
|
uiManager.setText("statusText", "Connecting to " + serverAddress + "...");
|
|
std::cerr << "Connecting to server: " << serverAddress << std::endl;
|
|
|
|
});
|
|
|
|
uiManager.setButtonCallback("backButton", [this](const std::string& buttonName) {
|
|
uiManager.popMenu();
|
|
});
|
|
|
|
uiManager.setTextFieldCallback("serverInputField",
|
|
[this](const std::string& fieldName, const std::string& newText) {
|
|
std::cout << "Server input field changed to: " << newText << std::endl;
|
|
});
|
|
|
|
std::cerr << "Multiplayer menu loaded successfully\n";
|
|
}
|
|
else {
|
|
std::cerr << "Failed to load multiplayer menu\n";
|
|
}
|
|
});
|
|
uiManager.setButtonCallback("exitButton", [](const std::string& name) {
|
|
std::cerr << "Exit from main menu pressed: " << name << " -> exiting\n";
|
|
Environment::exitGameLoop = true;
|
|
});
|
|
}
|
|
|
|
void MenuManager::showGameOver()
|
|
{
|
|
if (!uiGameOverShown) {
|
|
if (uiManager.pushMenuFromSavedRoot(gameOverSavedRoot)) {
|
|
uiManager.setButtonCallback("restartButton", [this](const std::string& name) {
|
|
uiGameOverShown = false;
|
|
uiManager.popMenu();
|
|
onRestartPressed();
|
|
});
|
|
|
|
uiManager.setButtonCallback("gameOverExitButton", [this](const std::string& name) {
|
|
Environment::exitGameLoop = true;
|
|
});
|
|
|
|
uiGameOverShown = true;
|
|
}
|
|
else {
|
|
std::cerr << "Failed to load game_over.json\n";
|
|
}
|
|
}
|
|
}
|
|
}
|