2017-07-13 10:26:03 +02:00
|
|
|
/*
|
2023-02-01 16:42:03 +02:00
|
|
|
* CInGameConsole.cpp, part of VCMI engine
|
2017-07-13 10:26:03 +02:00
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CInGameConsole.h"
|
2014-07-13 20:53:37 +03:00
|
|
|
|
|
|
|
#include "../CGameInfo.h"
|
|
|
|
#include "../CMusicHandler.h"
|
|
|
|
#include "../CPlayerInterface.h"
|
2023-04-17 12:06:58 +02:00
|
|
|
#include "../PlayerLocalState.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "../ClientCommandManager.h"
|
2023-03-01 17:20:05 +02:00
|
|
|
#include "../gui/CGuiHandler.h"
|
|
|
|
#include "../render/Colors.h"
|
2014-07-13 20:53:37 +03:00
|
|
|
|
|
|
|
#include "../../CCallback.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "../../lib/CConfigHandler.h"
|
2023-02-12 23:00:56 +02:00
|
|
|
#include "../../lib/TextOperations.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "../../lib/mapObjects/CArmedInstance.h"
|
2012-06-13 16:04:06 +03:00
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
CInGameConsole::CInGameConsole()
|
2023-03-22 23:09:43 +02:00
|
|
|
: CIntObject(KEYBOARD | TIME | TEXTINPUT)
|
|
|
|
, prevEntDisp(-1)
|
|
|
|
{
|
|
|
|
type |= REDRAW_PARENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CInGameConsole::showAll(SDL_Surface * to)
|
2018-04-07 13:34:11 +02:00
|
|
|
{
|
2023-03-22 23:09:43 +02:00
|
|
|
show(to);
|
2018-04-07 13:34:11 +02:00
|
|
|
}
|
|
|
|
|
2014-07-13 18:39:45 +03:00
|
|
|
void CInGameConsole::show(SDL_Surface * to)
|
|
|
|
{
|
2023-04-08 17:53:47 +02:00
|
|
|
if (LOCPLINT->cingconsole != this)
|
|
|
|
return;
|
|
|
|
|
2014-07-13 18:39:45 +03:00
|
|
|
int number = 0;
|
|
|
|
|
|
|
|
boost::unique_lock<boost::mutex> lock(texts_mx);
|
2023-03-22 23:09:43 +02:00
|
|
|
for(auto & text : texts)
|
2014-07-13 18:39:45 +03:00
|
|
|
{
|
2022-12-21 17:02:53 +02:00
|
|
|
Point leftBottomCorner(0, pos.h);
|
2023-03-22 23:09:43 +02:00
|
|
|
Point textPosition(leftBottomCorner.x + 50, leftBottomCorner.y - texts.size() * 20 - 80 + number * 20);
|
2022-12-21 17:02:53 +02:00
|
|
|
|
2023-03-22 23:09:43 +02:00
|
|
|
graphics->fonts[FONT_MEDIUM]->renderTextLeft(to, text.text, Colors::GREEN, textPosition );
|
2014-07-13 18:39:45 +03:00
|
|
|
|
2023-03-22 23:09:43 +02:00
|
|
|
number++;
|
2014-07-13 18:39:45 +03:00
|
|
|
}
|
2023-03-22 23:09:43 +02:00
|
|
|
}
|
2014-07-13 18:39:45 +03:00
|
|
|
|
2023-03-22 23:09:43 +02:00
|
|
|
void CInGameConsole::tick(uint32_t msPassed)
|
|
|
|
{
|
|
|
|
size_t sizeBefore = texts.size();
|
2014-07-13 18:39:45 +03:00
|
|
|
{
|
2023-03-22 23:09:43 +02:00
|
|
|
boost::unique_lock<boost::mutex> lock(texts_mx);
|
|
|
|
|
|
|
|
for(auto & text : texts)
|
|
|
|
text.timeOnScreen += msPassed;
|
|
|
|
|
|
|
|
vstd::erase_if(
|
|
|
|
texts,
|
|
|
|
[&](const auto & value)
|
|
|
|
{
|
|
|
|
return value.timeOnScreen > defaultTimeout;
|
|
|
|
}
|
|
|
|
);
|
2014-07-13 18:39:45 +03:00
|
|
|
}
|
2023-03-22 23:09:43 +02:00
|
|
|
|
|
|
|
if(sizeBefore != texts.size())
|
|
|
|
GH.totalRedraw(); // FIXME: ingame console has no parent widget set
|
2014-07-13 18:39:45 +03:00
|
|
|
}
|
|
|
|
|
2023-03-22 23:09:43 +02:00
|
|
|
void CInGameConsole::print(const std::string & txt)
|
2014-07-13 18:39:45 +03:00
|
|
|
{
|
2023-03-22 23:09:43 +02:00
|
|
|
// boost::unique_lock scope
|
2014-07-13 18:39:45 +03:00
|
|
|
{
|
2023-03-22 23:09:43 +02:00
|
|
|
boost::unique_lock<boost::mutex> lock(texts_mx);
|
|
|
|
int lineLen = conf.go()->ac.outputLineLength;
|
|
|
|
|
|
|
|
if(txt.size() < lineLen)
|
2014-07-13 18:39:45 +03:00
|
|
|
{
|
2023-03-22 23:09:43 +02:00
|
|
|
texts.push_back({txt, 0});
|
2014-07-13 18:39:45 +03:00
|
|
|
}
|
2023-03-22 23:09:43 +02:00
|
|
|
else
|
2014-07-13 18:39:45 +03:00
|
|
|
{
|
2023-03-22 23:09:43 +02:00
|
|
|
assert(lineLen);
|
|
|
|
for(int g = 0; g < txt.size() / lineLen + 1; ++g)
|
2014-07-13 18:39:45 +03:00
|
|
|
{
|
2023-03-22 23:09:43 +02:00
|
|
|
std::string part = txt.substr(g * lineLen, lineLen);
|
|
|
|
if(part.empty())
|
|
|
|
break;
|
|
|
|
|
|
|
|
texts.push_back({part, 0});
|
2014-07-13 18:39:45 +03:00
|
|
|
}
|
|
|
|
}
|
2023-03-22 23:09:43 +02:00
|
|
|
|
|
|
|
while(texts.size() > maxDisplayedTexts)
|
|
|
|
texts.erase(texts.begin());
|
2014-07-13 18:39:45 +03:00
|
|
|
}
|
2023-03-22 23:09:43 +02:00
|
|
|
|
|
|
|
GH.totalRedraw(); // FIXME: ingame console has no parent widget set
|
2014-07-13 18:39:45 +03:00
|
|
|
}
|
|
|
|
|
2023-02-02 18:42:44 +02:00
|
|
|
void CInGameConsole::keyPressed (const SDL_Keycode & key)
|
2014-07-13 18:39:45 +03:00
|
|
|
{
|
2023-04-08 17:53:47 +02:00
|
|
|
if (LOCPLINT->cingconsole != this)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(!captureAllKeys && key != SDLK_TAB)
|
|
|
|
return; //because user is not entering any text
|
2014-07-13 18:39:45 +03:00
|
|
|
|
2023-02-02 18:02:25 +02:00
|
|
|
switch(key)
|
2014-07-13 18:39:45 +03:00
|
|
|
{
|
|
|
|
case SDLK_TAB:
|
|
|
|
case SDLK_ESCAPE:
|
|
|
|
{
|
|
|
|
if(captureAllKeys)
|
|
|
|
{
|
|
|
|
endEnteringText(false);
|
|
|
|
}
|
2023-02-02 18:02:25 +02:00
|
|
|
else if(SDLK_TAB == key)
|
2014-07-13 18:39:45 +03:00
|
|
|
{
|
|
|
|
startEnteringText();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SDLK_RETURN: //enter key
|
|
|
|
{
|
2023-01-22 02:14:28 +02:00
|
|
|
if(!enteredText.empty() && captureAllKeys)
|
2014-07-13 18:39:45 +03:00
|
|
|
{
|
2023-01-22 02:14:28 +02:00
|
|
|
bool anyTextExceptCaret = enteredText.size() > 1;
|
|
|
|
endEnteringText(anyTextExceptCaret);
|
|
|
|
|
|
|
|
if(anyTextExceptCaret)
|
|
|
|
{
|
|
|
|
CCS->soundh->playSound("CHAT");
|
|
|
|
}
|
2014-07-13 18:39:45 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SDLK_BACKSPACE:
|
|
|
|
{
|
|
|
|
if(enteredText.size() > 1)
|
|
|
|
{
|
2023-02-12 23:52:35 +02:00
|
|
|
TextOperations::trimRightUnicode(enteredText,2);
|
2014-07-13 18:39:45 +03:00
|
|
|
enteredText += '_';
|
|
|
|
refreshEnteredText();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SDLK_UP: //up arrow
|
|
|
|
{
|
2023-03-22 23:09:43 +02:00
|
|
|
if(previouslyEntered.empty())
|
2014-07-13 18:39:45 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
if(prevEntDisp == -1)
|
|
|
|
{
|
2020-10-01 10:38:06 +02:00
|
|
|
prevEntDisp = static_cast<int>(previouslyEntered.size() - 1);
|
2014-07-13 18:39:45 +03:00
|
|
|
enteredText = previouslyEntered[prevEntDisp] + "_";
|
|
|
|
refreshEnteredText();
|
|
|
|
}
|
|
|
|
else if( prevEntDisp > 0)
|
|
|
|
{
|
|
|
|
--prevEntDisp;
|
|
|
|
enteredText = previouslyEntered[prevEntDisp] + "_";
|
|
|
|
refreshEnteredText();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SDLK_DOWN: //down arrow
|
|
|
|
{
|
|
|
|
if(prevEntDisp != -1 && prevEntDisp+1 < previouslyEntered.size())
|
|
|
|
{
|
|
|
|
++prevEntDisp;
|
|
|
|
enteredText = previouslyEntered[prevEntDisp] + "_";
|
|
|
|
refreshEnteredText();
|
|
|
|
}
|
|
|
|
else if(prevEntDisp+1 == previouslyEntered.size()) //useful feature
|
|
|
|
{
|
|
|
|
prevEntDisp = -1;
|
|
|
|
enteredText = "_";
|
|
|
|
refreshEnteredText();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-02 20:16:41 +02:00
|
|
|
void CInGameConsole::textInputed(const std::string & inputtedText)
|
2014-07-13 18:39:45 +03:00
|
|
|
{
|
2023-04-08 17:53:47 +02:00
|
|
|
if (LOCPLINT->cingconsole != this)
|
|
|
|
return;
|
|
|
|
|
2023-03-22 23:09:43 +02:00
|
|
|
if(!captureAllKeys || enteredText.empty())
|
2014-07-13 18:39:45 +03:00
|
|
|
return;
|
|
|
|
enteredText.resize(enteredText.size()-1);
|
|
|
|
|
2023-02-02 20:16:41 +02:00
|
|
|
enteredText += inputtedText;
|
2014-07-13 18:39:45 +03:00
|
|
|
enteredText += "_";
|
|
|
|
|
|
|
|
refreshEnteredText();
|
|
|
|
}
|
|
|
|
|
2023-02-02 20:16:41 +02:00
|
|
|
void CInGameConsole::textEdited(const std::string & inputtedText)
|
2014-07-13 18:39:45 +03:00
|
|
|
{
|
|
|
|
//do nothing here
|
|
|
|
}
|
|
|
|
|
|
|
|
void CInGameConsole::startEnteringText()
|
|
|
|
{
|
2022-12-19 22:53:31 +02:00
|
|
|
if (!active)
|
|
|
|
return;
|
|
|
|
|
2022-12-19 22:04:50 +02:00
|
|
|
if (captureAllKeys)
|
|
|
|
return;
|
2022-11-20 11:53:46 +02:00
|
|
|
|
2022-12-12 18:23:41 +02:00
|
|
|
assert(GH.statusbar);
|
2022-12-19 22:53:31 +02:00
|
|
|
assert(currentStatusBar.expired());//effectively, nullptr check
|
2014-07-13 18:39:45 +03:00
|
|
|
|
2022-12-19 22:53:31 +02:00
|
|
|
currentStatusBar = GH.statusbar;
|
|
|
|
|
2022-12-12 18:23:41 +02:00
|
|
|
captureAllKeys = true;
|
2014-07-13 18:39:45 +03:00
|
|
|
enteredText = "_";
|
|
|
|
|
2022-12-12 18:23:41 +02:00
|
|
|
GH.statusbar->setEnteringMode(true);
|
|
|
|
GH.statusbar->setEnteredText(enteredText);
|
2014-07-13 18:39:45 +03:00
|
|
|
}
|
|
|
|
|
2023-01-06 22:01:00 +02:00
|
|
|
void CInGameConsole::endEnteringText(bool processEnteredText)
|
2014-07-13 18:39:45 +03:00
|
|
|
{
|
2022-11-20 11:53:46 +02:00
|
|
|
captureAllKeys = false;
|
2014-07-13 18:39:45 +03:00
|
|
|
prevEntDisp = -1;
|
2023-01-06 22:01:00 +02:00
|
|
|
if(processEnteredText)
|
2014-07-13 18:39:45 +03:00
|
|
|
{
|
|
|
|
std::string txt = enteredText.substr(0, enteredText.size()-1);
|
|
|
|
previouslyEntered.push_back(txt);
|
2023-01-06 22:01:00 +02:00
|
|
|
|
2023-01-06 23:53:10 +02:00
|
|
|
if(txt.at(0) == '/')
|
|
|
|
{
|
|
|
|
//some commands like gosolo don't work when executed from GUI thread
|
2023-01-15 02:09:58 +02:00
|
|
|
auto threadFunction = [=]()
|
|
|
|
{
|
|
|
|
ClientCommandManager commandController;
|
|
|
|
commandController.processCommand(txt.substr(1), true);
|
|
|
|
};
|
|
|
|
|
|
|
|
boost::thread clientCommandThread(threadFunction);
|
2023-01-07 16:42:34 +02:00
|
|
|
clientCommandThread.detach();
|
2023-01-06 23:53:10 +02:00
|
|
|
}
|
2023-01-08 21:30:57 +02:00
|
|
|
else
|
2023-04-17 12:06:58 +02:00
|
|
|
LOCPLINT->cb->sendMessage(txt, LOCPLINT->localState->getCurrentArmy());
|
2014-07-13 18:39:45 +03:00
|
|
|
}
|
2022-11-15 02:20:55 +02:00
|
|
|
enteredText.clear();
|
2022-12-19 22:53:31 +02:00
|
|
|
|
|
|
|
auto statusbar = currentStatusBar.lock();
|
|
|
|
assert(statusbar);
|
|
|
|
|
|
|
|
if (statusbar)
|
|
|
|
statusbar->setEnteringMode(false);
|
|
|
|
|
|
|
|
currentStatusBar.reset();
|
2014-07-13 18:39:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CInGameConsole::refreshEnteredText()
|
|
|
|
{
|
2022-12-19 22:53:31 +02:00
|
|
|
auto statusbar = currentStatusBar.lock();
|
|
|
|
assert(statusbar);
|
|
|
|
|
|
|
|
if (statusbar)
|
|
|
|
statusbar->setEnteredText(enteredText);
|
2014-07-13 18:39:45 +03:00
|
|
|
}
|
|
|
|
|