diff --git a/client/battle/BattleInterfaceClasses.cpp b/client/battle/BattleInterfaceClasses.cpp index 9413133c9..3df6ac532 100644 --- a/client/battle/BattleInterfaceClasses.cpp +++ b/client/battle/BattleInterfaceClasses.cpp @@ -27,6 +27,7 @@ #include "../gui/Canvas.h" #include "../gui/CCursorHandler.h" #include "../gui/CGuiHandler.h" +#include "../widgets/AdventureMapClasses.h" #include "../widgets/Buttons.h" #include "../widgets/Images.h" #include "../widgets/TextControls.h" @@ -113,10 +114,12 @@ BattleConsole::BattleConsole(const Rect & position) pos.h = position.h; } -BattleConsole::~BattleConsole() +void BattleConsole::deactivate() { if (enteringText) - setEnteringMode(false); + LOCPLINT->cingconsole->endEnteringText(false); + + CIntObject::deactivate(); } void BattleConsole::setEnteringMode(bool on) diff --git a/client/battle/BattleInterfaceClasses.h b/client/battle/BattleInterfaceClasses.h index 9014ee784..4b4df51c0 100644 --- a/client/battle/BattleInterfaceClasses.h +++ b/client/battle/BattleInterfaceClasses.h @@ -59,8 +59,9 @@ private: bool enteringText; public: BattleConsole(const Rect & position); - ~BattleConsole(); + void showAll(SDL_Surface * to) override; + void deactivate() override; bool addText(const std::string &text); //adds text at the last position; returns false if failed (e.g. text longer than 70 characters) void scrollUp(ui32 by = 1); //scrolls console up by 'by' positions diff --git a/client/gui/SDL_Extensions.cpp b/client/gui/SDL_Extensions.cpp index 1a528e062..7eb4d9a8f 100644 --- a/client/gui/SDL_Extensions.cpp +++ b/client/gui/SDL_Extensions.cpp @@ -526,13 +526,10 @@ bool CSDL_Ext::isTransparent( SDL_Surface * srf, int x, int y ) SDL_GetRGBA(SDL_GetPixel(srf, x, y), srf->format, &color.r, &color.g, &color.b, &color.a); - // color is considered transparent here if - // a) image has aplha: less than 50% transparency - // b) no alpha: color is cyan - if (srf->format->Amask) - return color.a < 128; // almost transparent - else - return (color.r == 0 && color.g == 255 && color.b == 255); + bool pixelTransparent = color.a < 128; + bool pixelCyan = (color.r == 0 && color.g == 255 && color.b == 255); + + return pixelTransparent || pixelCyan; } void CSDL_Ext::VflipSurf(SDL_Surface * surf) diff --git a/client/lobby/CSavingScreen.cpp b/client/lobby/CSavingScreen.cpp index 06071c533..ea40bb286 100644 --- a/client/lobby/CSavingScreen.cpp +++ b/client/lobby/CSavingScreen.cpp @@ -66,10 +66,10 @@ void CSavingScreen::changeSelection(std::shared_ptr to) void CSavingScreen::saveGame() { - if(!(tabSel && tabSel->inputName && tabSel->inputName->text.size())) + if(!(tabSel && tabSel->inputName && tabSel->inputName->getText().size())) return; - std::string path = "Saves/" + tabSel->inputName->text; + std::string path = "Saves/" + tabSel->inputName->getText(); auto overWrite = [this, path]() -> void { @@ -82,7 +82,7 @@ void CSavingScreen::saveGame() if(CResourceHandler::get("local")->existsResource(ResourceID(path, EResType::CLIENT_SAVEGAME))) { std::string hlp = CGI->generaltexth->allTexts[493]; //%s exists. Overwrite? - boost::algorithm::replace_first(hlp, "%s", tabSel->inputName->text); + boost::algorithm::replace_first(hlp, "%s", tabSel->inputName->getText()); LOCPLINT->showYesNoDialog(hlp, overWrite, nullptr); } else diff --git a/client/lobby/CSelectionBase.cpp b/client/lobby/CSelectionBase.cpp index 26820dbdc..2edcaf50c 100644 --- a/client/lobby/CSelectionBase.cpp +++ b/client/lobby/CSelectionBase.cpp @@ -321,9 +321,9 @@ CChatBox::CChatBox(const Rect & rect) void CChatBox::keyPressed(const SDL_KeyboardEvent & key) { - if(key.keysym.sym == SDLK_RETURN && key.state == SDL_PRESSED && inputBox->text.size()) + if(key.keysym.sym == SDLK_RETURN && key.state == SDL_PRESSED && inputBox->getText().size()) { - CSH->sendMessage(inputBox->text); + CSH->sendMessage(inputBox->getText()); inputBox->setText(""); } else @@ -333,7 +333,7 @@ void CChatBox::keyPressed(const SDL_KeyboardEvent & key) void CChatBox::addNewMessage(const std::string & text) { CCS->soundh->playSound("CHAT"); - chatHistory->setText(chatHistory->label->text + text + "\n"); + chatHistory->setText(chatHistory->label->getText() + text + "\n"); if(chatHistory->slider) chatHistory->slider->moveToMax(); } diff --git a/client/mainmenu/CMainMenu.cpp b/client/mainmenu/CMainMenu.cpp index 12f1b0639..c7a9fbf02 100644 --- a/client/mainmenu/CMainMenu.cpp +++ b/client/mainmenu/CMainMenu.cpp @@ -438,7 +438,7 @@ void CMultiPlayers::onChange(std::string newText) size_t namesCount = 0; for(auto & elem : inputNames) - if(!elem->text.empty()) + if(!elem->getText().empty()) namesCount++; } @@ -447,8 +447,8 @@ void CMultiPlayers::enterSelectionScreen() std::vector names; for(auto name : inputNames) { - if(name->text.length()) - names.push_back(name->text); + if(name->getText().length()) + names.push_back(name->getText()); } Settings name = settings.write["general"]["playerName"]; @@ -494,7 +494,7 @@ void CSimpleJoinScreen::connectToServer() buttonOk->block(true); CSDL_Ext::stopTextInput(); - boost::thread(&CSimpleJoinScreen::connectThread, this, inputAddress->text, boost::lexical_cast(inputPort->text)); + boost::thread(&CSimpleJoinScreen::connectThread, this, inputAddress->getText(), boost::lexical_cast(inputPort->getText())); } void CSimpleJoinScreen::leaveScreen() @@ -512,7 +512,7 @@ void CSimpleJoinScreen::leaveScreen() void CSimpleJoinScreen::onChange(const std::string & newText) { - buttonOk->block(inputAddress->text.empty() || inputPort->text.empty()); + buttonOk->block(inputAddress->getText().empty() || inputPort->getText().empty()); } void CSimpleJoinScreen::connectThread(const std::string addr, const ui16 port) diff --git a/client/widgets/AdventureMapClasses.cpp b/client/widgets/AdventureMapClasses.cpp index 0f0ed2772..292a6baf1 100644 --- a/client/widgets/AdventureMapClasses.cpp +++ b/client/widgets/AdventureMapClasses.cpp @@ -1126,7 +1126,17 @@ void CInGameConsole::textEdited(const SDL_TextEditingEvent & event) void CInGameConsole::startEnteringText() { + if (!active) + return; + + if (captureAllKeys) + return; + assert(GH.statusbar); + assert(currentStatusBar.expired());//effectively, nullptr check + + currentStatusBar = GH.statusbar; + captureAllKeys = true; enteredText = "_"; @@ -1145,12 +1155,23 @@ void CInGameConsole::endEnteringText(bool printEnteredText) previouslyEntered.push_back(txt); } enteredText.clear(); - GH.statusbar->setEnteringMode(false); + + auto statusbar = currentStatusBar.lock(); + assert(statusbar); + + if (statusbar) + statusbar->setEnteringMode(false); + + currentStatusBar.reset(); } void CInGameConsole::refreshEnteredText() { - GH.statusbar->setEnteredText(enteredText); + auto statusbar = currentStatusBar.lock(); + assert(statusbar); + + if (statusbar) + statusbar->setEnteredText(enteredText); } CAdvMapPanel::CAdvMapPanel(SDL_Surface * bg, Point position) diff --git a/client/widgets/AdventureMapClasses.h b/client/widgets/AdventureMapClasses.h index 9eb592c3c..1c7ef5b50 100644 --- a/client/widgets/AdventureMapClasses.h +++ b/client/widgets/AdventureMapClasses.h @@ -412,6 +412,8 @@ private: int prevEntDisp; //displayed entry from previouslyEntered - if none it's -1 int defaultTimeout; //timeout for new texts (in ms) int maxDisplayedTexts; //hiw many texts can be displayed simultaneously + + std::weak_ptr currentStatusBar; public: std::string enteredText; void show(SDL_Surface * to) override; diff --git a/client/widgets/TextControls.cpp b/client/widgets/TextControls.cpp index d9fb7d9a1..a218beb2d 100644 --- a/client/widgets/TextControls.cpp +++ b/client/widgets/TextControls.cpp @@ -14,7 +14,9 @@ #include "Images.h" #include "../CMessage.h" +#include "../CPlayerInterface.h" #include "../gui/CGuiHandler.h" +#include "../widgets/AdventureMapClasses.h" #include "../../lib/CGeneralTextHandler.h" //for Unicode related stuff @@ -428,14 +430,17 @@ void CGStatusBar::clickLeft(tribool down, bool previousState) { if(!down && onClick) { - onClick(); + if(LOCPLINT && LOCPLINT->cingconsole->active) + LOCPLINT->cingconsole->startEnteringText(); } } -void CGStatusBar::setOnClick(std::function handler) +void CGStatusBar::deactivate() { - onClick = handler; - addUsedEvents(LCLICK); + if (enteringText) + LOCPLINT->cingconsole->endEnteringText(false); + + CIntObject::deactivate(); } Point CGStatusBar::getBorderSize() diff --git a/client/widgets/TextControls.h b/client/widgets/TextControls.h index a9c17f38a..5e9a49aef 100644 --- a/client/widgets/TextControls.h +++ b/client/widgets/TextControls.h @@ -41,11 +41,11 @@ protected: virtual std::string visibleText(); std::shared_ptr background; -public: - std::string text; bool autoRedraw; //whether control will redraw itself on setTxt +public: + std::string getText(); virtual void setAutoRedraw(bool option); virtual void setText(const std::string & Txt); @@ -124,6 +124,14 @@ class CGStatusBar : public CLabel, public std::enable_shared_from_this background_, EFonts Font = FONT_SMALL, ETextAlignment Align = ETextAlignment::CENTER, const SDL_Color & Color = Colors::WHITE); CGStatusBar(int x, int y, std::string name, int maxw = -1); + + //make CLabel API private + using CLabel::getText; + using CLabel::setAutoRedraw; + using CLabel::setText; + using CLabel::setColor; + using CLabel::getWidth; + protected: Point getBorderSize() override; @@ -141,9 +149,8 @@ public: return ret; } - void setOnClick(std::function handler); - void show(SDL_Surface * to) override; + void deactivate() override; // IStatusBar interface void write(const std::string & Text) override; diff --git a/client/windows/CAdvmapInterface.cpp b/client/windows/CAdvmapInterface.cpp index cff2559e1..8242d1882 100644 --- a/client/windows/CAdvmapInterface.cpp +++ b/client/windows/CAdvmapInterface.cpp @@ -716,11 +716,6 @@ CAdvMapInt::CAdvMapInt(): worldViewUnderground->block(!CGI->mh->map->twoLevel); addUsedEvents(MOVE); - - statusbar->setOnClick([&] - { - if(LOCPLINT) LOCPLINT->cingconsole->startEnteringText(); - }); } CAdvMapInt::~CAdvMapInt() @@ -978,6 +973,7 @@ void CAdvMapInt::deactivate() } minimap.deactivate(); terrain.deactivate(); + statusbar->deactivate(); } } @@ -1667,13 +1663,13 @@ void CAdvMapInt::tileHovered(const int3 &mapPos) objRelations = LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, objAtTile->tempOwner); std::string text = curHero() ? objAtTile->getHoverText(curHero()) : objAtTile->getHoverText(LOCPLINT->playerID); boost::replace_all(text,"\n"," "); - statusbar->setText(text); + statusbar->write(text); } else { std::string hlp; CGI->mh->getTerrainDescr(mapPos, hlp, false); - statusbar->setText(hlp); + statusbar->write(hlp); } if(spellBeingCasted) diff --git a/client/windows/CCastleInterface.cpp b/client/windows/CCastleInterface.cpp index a89ab26c6..e4b774795 100644 --- a/client/windows/CCastleInterface.cpp +++ b/client/windows/CCastleInterface.cpp @@ -1019,7 +1019,7 @@ void CCreaInfo::update() else value = boost::lexical_cast(town->creatureGrowth(level)); - if(value != label->text) + if(value != label->getText()) label->setText(value); } } diff --git a/client/windows/CKingdomInterface.cpp b/client/windows/CKingdomInterface.cpp index 14531d9df..d91842b70 100644 --- a/client/windows/CKingdomInterface.cpp +++ b/client/windows/CKingdomInterface.cpp @@ -799,7 +799,7 @@ void CTownItem::updateGarrisons() void CTownItem::update() { std::string incomeVal = boost::lexical_cast(town->dailyIncome()[Res::GOLD]); - if (incomeVal != income->text) + if (incomeVal != income->getText()) income->setText(incomeVal); heroes->update(); diff --git a/client/windows/CSpellWindow.cpp b/client/windows/CSpellWindow.cpp index 93a885f5c..959e2c61b 100644 --- a/client/windows/CSpellWindow.cpp +++ b/client/windows/CSpellWindow.cpp @@ -66,7 +66,7 @@ void CSpellWindow::InteractiveArea::clickRight(tribool down, bool previousState) void CSpellWindow::InteractiveArea::hover(bool on) { if(on) - owner->statusBar->setText(hoverText); + owner->statusBar->write(hoverText); else owner->statusBar->clear(); } @@ -513,7 +513,7 @@ CSpellWindow::SpellArea::SpellArea(SDL_Rect pos, CSpellWindow * owner) cost = std::make_shared(39, 94, FONT_TINY, ETextAlignment::CENTER); for(auto l : {name, level, cost}) - l->autoRedraw = false; + l->setAutoRedraw(false); } CSpellWindow::SpellArea::~SpellArea() = default; @@ -609,7 +609,7 @@ void CSpellWindow::SpellArea::hover(bool on) if(mySpell) { if(on) - owner->statusBar->setText(boost::to_string(boost::format("%s (%s)") % mySpell->name % CGI->generaltexth->allTexts[171+mySpell->level])); + owner->statusBar->write(boost::to_string(boost::format("%s (%s)") % mySpell->name % CGI->generaltexth->allTexts[171+mySpell->level])); else owner->statusBar->clear(); } diff --git a/client/windows/GUIClasses.cpp b/client/windows/GUIClasses.cpp index 91a51c84a..02dc707a3 100644 --- a/client/windows/GUIClasses.cpp +++ b/client/windows/GUIClasses.cpp @@ -579,7 +579,7 @@ void CSystemOptionsWindow::selectGameRes() #endif auto resolutionStr = resolutionToString(resolution.first, resolution.second); - if(gameResLabel->text == resolutionStr) + if(gameResLabel->getText() == resolutionStr) currentResolutionIndex = i; items.push_back(std::move(resolutionStr)); ++i;