From efcefdbf8efa0644917fb70b7f40b369e37161cf Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sun, 25 Jun 2023 10:04:23 +0300 Subject: [PATCH] factored out GUI manager behaviour from GuiAppState --- guiAppState.cpp | 314 +++--------------------------------------------- guiAppState.h | 32 ----- 2 files changed, 17 insertions(+), 329 deletions(-) diff --git a/guiAppState.cpp b/guiAppState.cpp index 3545a15..f32cf31 100755 --- a/guiAppState.cpp +++ b/guiAppState.cpp @@ -7,6 +7,7 @@ #include "util.h" #include "defConfigs.h" #include "gameManager.h" +#include "concreteGuiManager.h" #include "guiAppState.h" using namespace gameBase; @@ -27,52 +28,7 @@ namespace battleship{ GuiAppState::~GuiAppState() {} void GuiAppState::update() { - Vector2 mousePos = getCursorPos(); - - for (Button *b : buttons) { - if (b->isSeparate()) - b->update(); - - bool withinX = mousePos.x > b->getPos().x && mousePos.x < b->getPos().x + b->getSize().x; - bool withinY = mousePos.y > b->getPos().y && mousePos.y < b->getPos().y + b->getSize().y; - - if(withinX && withinY) - b->onMouseOver(); - else - b->onMouseOff(); - } - - for (Listbox *l : listboxes) - l->update(); - - if(currentListbox && - leftMousePressed && - (currentListbox->getScrollingButton()->getPos().x < mousePos.x && - mousePos.x < currentListbox->getPos().x + currentListbox->getSize().x)) - { - currentListbox->scrollToHeight(mousePos.y); - } - - for (Checkbox *c : checkboxes) - c->update(); - - for (Slider *s : sliders) - s->update(); - - if(currentSlider){ - float percentage = (mousePos.x - currentSlider->getPos().x) / currentSlider->getSize().x; - currentSlider->setValue(percentage * currentSlider->getMaxValue()); - } - - for (Textbox *t : textboxes){ - t->update(); - - if(t->isEnabled() && backspacePressed) - t->deleteCharacter(); - } - - for (Tooltip *t : tooltips) - t->update(); + ConcreteGuiManager::getSingleton()->update(); } void GuiAppState::onAttached() { @@ -83,84 +39,15 @@ namespace battleship{ AbstractAppState::onDettached(); } - void GuiAppState::updateCurrentListbox(Button *b, bool &listboxClicked){ - Listbox *pastListbox = currentListbox; - - for(Listbox *l : listboxes){ - if(b == l->getListboxButton() || b == l->getScrollingButton()){ - currentListbox = l; - listboxClicked = true; - - if(pastListbox && currentListbox != pastListbox){ - if(pastListbox->isCloseable()) - pastListbox->close(); - } - else - currentListbox = (l->isOpen() ? l : nullptr); - - break; - } - } - } - - void GuiAppState::updateCurrentSlider(Button *b){ - for(Slider *s : sliders) - if(b == s->getMovableSliderButton()){ - currentSlider = s; - break; - } - } - - void GuiAppState::updateCurrentTextbox(Button *b, bool &textboxClicked){ - Textbox *pastTextbox = currentTextbox; - - for(Textbox *t : textboxes) - if(b == t->getTextboxButton()){ - currentTextbox = t; - textboxClicked = true; - - if(pastTextbox && currentTextbox != pastTextbox) - pastTextbox->disable(); - else - currentTextbox = (t->isEnabled() ? t : nullptr); - - break; - } - } - void GuiAppState::onAction(int bind, bool isPressed) { + ConcreteGuiManager *guiManager = ConcreteGuiManager::getSingleton(); + switch((Bind)bind){ case Bind::LEFT_CLICK: leftMousePressed = isPressed; - currentSlider = nullptr; - if(isPressed){ - bool textboxClicked = false, listboxClicked = false; - - for (int i = 0; i < buttons.size(); i++) { - Vector2 mousePos = getCursorPos(); - Button *b = buttons[i]; - bool withinX = mousePos.x > b->getPos().x && mousePos.x < b->getPos().x + b->getSize().x; - bool withinY = mousePos.y > b->getPos().y && mousePos.y < b->getPos().y + b->getSize().y; - - if (b->isActive() && withinX && withinY){ - b->onClick(); - updateCurrentListbox(b, listboxClicked); - updateCurrentSlider(b); - updateCurrentTextbox(b, textboxClicked); - } - } - - if(!textboxClicked && currentTextbox){ - currentTextbox->disable(); - currentTextbox = nullptr; - } - - if(!listboxClicked && currentListbox && currentListbox->isCloseable()){ - currentListbox->close(); - currentListbox = nullptr; - } - } + if(isPressed) + guiManager->findClickedButton(); break; case Bind::SCROLLING_UP:{ @@ -179,206 +66,39 @@ namespace battleship{ } } - void GuiAppState::updateControlsListbox(int trigger){ - Listbox *controlsListbox = nullptr; - - for(Listbox *l : listboxes) - if(l->isOpen()) - controlsListbox = l; - - if(controlsListbox){ - int selectedOption = controlsListbox->getSelectedOption(), colonId = -1; - wstring line = controlsListbox->getContents()[selectedOption]; - - for(int i = 0; i < line.size() && colonId == -1; i++) - if(line.c_str()[i] == ':') - colonId = i; - - stringstream ss; - ss << hex << trigger; - line = line.substr(0, colonId + 1) + L"0x"/*+ss.str().c_str()*/; - controlsListbox->changeLine(selectedOption, line); - } - } - void GuiAppState::onRawKeyPress(int ch){ - for(Button *b : buttons) + for(Button *b : ConcreteGuiManager::getSingleton()->getButtons()) if(b->getTrigger() == ch){ b->onClick(); break; } } - void GuiAppState::onRawCharPress(u32 codepoint){ - if(currentTextbox) - currentTextbox->type(codepoint); - } + void GuiAppState::onRawCharPress(u32 codepoint){ + Textbox *currentTextbox = getOpenTextbox(); + + if(currentTextbox) + currentTextbox->type(codepoint); + } void GuiAppState::onRawMousePress(int trigger){ } Textbox* GuiAppState::getOpenTextbox() { + vector textboxes = ConcreteGuiManager::getSingleton()->getTextboxes(); + for (int i = 0; i < textboxes.size(); i++) if (textboxes[i]->isEnabled()) return textboxes[i]; } Listbox* GuiAppState::getOpenListbox() { + vector listboxes = ConcreteGuiManager::getSingleton()->getListboxes(); + for (int i = 0; i < listboxes.size(); i++) if (listboxes[i]->isOpen()) return listboxes[i]; } void GuiAppState::onAnalog(int bind, float strength) {} - - void GuiAppState::addButton(Button* b) { - buttons.push_back(b); - } - - void GuiAppState::addListbox(Listbox* l) { - listboxes.push_back(l); - buttons.push_back(l->getListboxButton()); - buttons.push_back(l->getScrollingButton()); - } - - void GuiAppState::addCheckbox(Checkbox* c) { - buttons.push_back(c->getCheckboxButton()); - checkboxes.push_back(c); - } - - void GuiAppState::addSlider(Slider* s) { - buttons.push_back(s->getMovableSliderButton()); - buttons.push_back(s->getStaticSliderButton()); - sliders.push_back(s); - } - - void GuiAppState::addTextbox(Textbox* t) { - buttons.push_back(t->getTextboxButton()); - textboxes.push_back(t); - } - - void GuiAppState::addTooltip(Tooltip* t) { - tooltips.push_back(t); - } - - void GuiAppState::removeButton(Button *b) { - for (int i = 0; i < buttons.size(); i++) { - if (b == buttons[i]) { - delete b; - buttons.erase(buttons.begin() + i); - } - } - } - - void GuiAppState::removeButton(string name) { - for (int i = 0; i < buttons.size(); i++) { - if (name == buttons[i]->getName() && buttons[i]->isSeparate()) { - delete buttons[i]; - buttons.erase(buttons.begin() + i); - } - } - } - - void GuiAppState::removeListbox(Listbox *l) { - for (int i = 0; i < listboxes.size(); i++) { - if (l == listboxes[i]) { - removeButton(l->getListboxButton()); - delete l; - listboxes.erase(listboxes.begin() + i); - } - } - } - - void GuiAppState::removeCheckbox(Checkbox *c) { - for (int i = 0; i < checkboxes.size(); i++) { - if (c == checkboxes[i]) { - removeButton(c->getCheckboxButton()); - delete c; - checkboxes.erase(checkboxes.begin() + i); - } - } - } - - void GuiAppState::removeSlider(Slider *s) { - for (int i = 0; i < sliders.size(); i++) { - if (s == sliders[i]) { - removeButton(s->getMovableSliderButton()); - removeButton(s->getStaticSliderButton()); - delete s; - sliders.erase(sliders.begin() + i); - } - } - } - - void GuiAppState::removeTextbox(Textbox* t) { - for (int i = 0; i < textboxes.size(); i++) { - if (t == textboxes[i]) { - removeButton(t->getTextboxButton()); - delete t; - textboxes.erase(textboxes.begin() + i); - } - } - } - - void GuiAppState::removeTooltip(Tooltip *t) { - for (int i = 0; i < tooltips.size(); i++) { - if (t == tooltips[i]) { - delete t; - tooltips.erase(tooltips.begin() + i); - } - } - } - - void GuiAppState::removeAllButtons(vector exceptions){ - int targetId = 0; - - while(targetId != buttons.size()){ - if(find(exceptions.begin(), exceptions.end(), buttons[targetId]) == exceptions.end()){ - delete buttons[targetId]; - buttons.erase(buttons.begin() + targetId); - } - else - targetId++; - } - } - - void GuiAppState::removeAllListboxes() { - while (listboxes.size() > 0) { - removeButton(listboxes[listboxes.size() - 1]->getListboxButton()); - delete listboxes[listboxes.size() - 1]; - listboxes.pop_back(); - } - } - - void GuiAppState::removeAllCheckboxes() { - while (checkboxes.size() > 0) { - removeButton(checkboxes[checkboxes.size() - 1]->getCheckboxButton()); - delete checkboxes[checkboxes.size() - 1]; - checkboxes.pop_back(); - } - } - - void GuiAppState::removeAllSliders() { - while (sliders.size() > 0) { - removeButton(sliders[sliders.size() - 1]->getMovableSliderButton()); - removeButton(sliders[sliders.size() - 1]->getStaticSliderButton()); - delete sliders[sliders.size() - 1]; - sliders.pop_back(); - } - } - - void GuiAppState::removeAllTextboxes() { - while (textboxes.size() > 0) { - removeButton(textboxes[textboxes.size() - 1]->getTextboxButton()); - delete textboxes[textboxes.size() - 1]; - textboxes.pop_back(); - } - } - - void GuiAppState::removeAllTooltips(){ - while(!tooltips.empty()){ - delete tooltips[tooltips.size()-1]; - tooltips.pop_back(); - } - } } diff --git a/guiAppState.h b/guiAppState.h index 53ab570..191efaf 100755 --- a/guiAppState.h +++ b/guiAppState.h @@ -23,30 +23,8 @@ namespace battleship{ void onAttached(); void onDettached(); void update(); - void addButton(vb01Gui::Button*); - void removeButton(vb01Gui::Button*); - void removeButton(std::string); - void addListbox(vb01Gui::Listbox*); - void removeListbox(vb01Gui::Listbox*); - void addCheckbox(vb01Gui::Checkbox*); - void removeCheckbox(vb01Gui::Checkbox*); - void addTextbox(vb01Gui::Textbox*); - void removeTextbox(vb01Gui::Textbox*); - void addSlider(vb01Gui::Slider*); - void addTooltip(Tooltip*); - void removeTooltip(Tooltip*); - void removeSlider(vb01Gui::Slider*); - void removeAllButtons(std::vector = std::vector{}); - void removeAllListboxes(); - void removeAllCheckboxes(); - void removeAllSliders(); - void removeAllTextboxes(); - void removeAllTooltips(); inline bool isLeftMousePressed(){return leftMousePressed;} private: - void updateCurrentListbox(vb01Gui::Button*, bool&); - void updateCurrentSlider(vb01Gui::Button*); - void updateCurrentTextbox(vb01Gui::Button*, bool&); virtual void onAction(int, bool); virtual void onAnalog(int, float); virtual void onRawKeyPress(int); @@ -54,17 +32,7 @@ namespace battleship{ virtual void onRawMousePress(int); vb01Gui::Textbox* getOpenTextbox(); vb01Gui::Listbox* getOpenListbox(); - void updateControlsListbox(int); - std::vector buttons; - std::vector listboxes; - std::vector checkboxes; - std::vector textboxes; - std::vector sliders; - std::vector tooltips; - vb01Gui::Slider *currentSlider = nullptr; - vb01Gui::Textbox *currentTextbox = nullptr; - vb01Gui::Listbox *currentListbox = nullptr; bool leftMousePressed = false, backspacePressed = false; protected: };