2010-04-11 03:25:06 +02:00
|
|
|
// ==============================================================
|
|
|
|
// This file is part of Glest (www.glest.org)
|
|
|
|
//
|
2011-12-14 08:40:48 +01:00
|
|
|
// Copyright (C) 2001-2008 Martiño Figueroa
|
2010-04-11 03:25:06 +02:00
|
|
|
//
|
2010-12-24 03:10:31 +01:00
|
|
|
// You can redistribute this code and/or modify it under
|
|
|
|
// the terms of the GNU General Public License as published
|
|
|
|
// by the Free Software Foundation; either version 2 of the
|
2010-04-11 03:25:06 +02:00
|
|
|
// License, or (at your option) any later version
|
|
|
|
// ==============================================================
|
|
|
|
|
|
|
|
#include "console.h"
|
|
|
|
|
|
|
|
#include "lang.h"
|
|
|
|
#include "config.h"
|
|
|
|
#include "program.h"
|
|
|
|
#include "game_constants.h"
|
|
|
|
#include "sound_renderer.h"
|
|
|
|
#include "core_data.h"
|
|
|
|
#include <stdexcept>
|
2010-10-23 06:00:39 +02:00
|
|
|
#include "network_manager.h"
|
2017-02-04 21:33:18 +01:00
|
|
|
#include "gen_uuid.h"
|
2010-04-11 03:25:06 +02:00
|
|
|
#include "leak_dumper.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace Glest{ namespace Game{
|
|
|
|
|
|
|
|
// =====================================================
|
|
|
|
// class Console
|
|
|
|
// =====================================================
|
|
|
|
|
2010-10-22 09:28:55 +02:00
|
|
|
Console::Console() {
|
2017-02-04 21:33:18 +01:00
|
|
|
registerGraphicComponent("Console", "Generic-Console");
|
|
|
|
this->fontCallbackName = getInstanceName() + "_" + getNewUUD();
|
|
|
|
CoreData::getInstance().registerFontChangedCallback(this->getFontCallbackName(), this);
|
|
|
|
|
2010-10-22 09:28:55 +02:00
|
|
|
maxLines = Config::getInstance().getInt("ConsoleMaxLines");
|
|
|
|
maxStoredLines = Config::getInstance().getInt("ConsoleMaxLinesStored");
|
|
|
|
timeout = Config::getInstance().getInt("ConsoleTimeout");
|
|
|
|
timeElapsed = 0.0f;
|
2010-12-27 01:59:57 +01:00
|
|
|
xPos=20;
|
|
|
|
yPos=20;
|
2012-09-09 16:12:35 +02:00
|
|
|
lineHeight=Config::getInstance().getInt("FontConsoleBaseSize","18")+2;
|
2017-01-30 20:20:01 +01:00
|
|
|
setFont(CoreData::getInstance().getConsoleFont());
|
|
|
|
setFont3D(CoreData::getInstance().getConsoleFont3D());
|
|
|
|
|
2011-01-01 23:08:34 +01:00
|
|
|
stringToHighlight="";
|
2012-09-09 16:12:35 +02:00
|
|
|
onlyChatMessagesInStoredLines=true;
|
2010-04-11 03:25:06 +02:00
|
|
|
}
|
|
|
|
|
2017-02-04 21:33:18 +01:00
|
|
|
string Console::getNewUUD() {
|
|
|
|
char uuid_str[38];
|
|
|
|
get_uuid_string(uuid_str,sizeof(uuid_str));
|
|
|
|
return string(uuid_str);
|
|
|
|
}
|
|
|
|
|
2017-01-30 20:20:01 +01:00
|
|
|
Console::~Console() {
|
2017-02-04 21:33:18 +01:00
|
|
|
CoreData::getInstance().unRegisterFontChangedCallback(this->getFontCallbackName());
|
2017-01-30 20:20:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Console::setFont(Font2D *font) {
|
|
|
|
this->font = font;
|
|
|
|
if (this->font != NULL) {
|
|
|
|
this->font2DUniqueId = font->getFontUniqueId();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this->font2DUniqueId = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Console::setFont3D(Font3D *font) {
|
|
|
|
this->font3D = font;
|
|
|
|
if (this->font3D != NULL) {
|
|
|
|
this->font3DUniqueId = font->getFontUniqueId();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this->font3DUniqueId = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-10 04:09:36 +02:00
|
|
|
void Console::registerGraphicComponent(const std::string &containerName, const std::string &objName) {
|
2017-01-30 20:20:01 +01:00
|
|
|
this->instanceName = objName;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Console::FontChangedCallback(std::string fontUniqueId, Font *font) {
|
|
|
|
if (fontUniqueId != "") {
|
|
|
|
if (fontUniqueId == this->font2DUniqueId) {
|
|
|
|
if (font != NULL) {
|
|
|
|
this->font = (Font2D *)font;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this->font = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (fontUniqueId == this->font3DUniqueId) {
|
|
|
|
if (font != NULL) {
|
|
|
|
this->font3D = (Font3D *)font;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this->font3D = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-04 00:38:43 +01:00
|
|
|
void Console::resetFonts() {
|
2017-01-30 20:20:01 +01:00
|
|
|
setFont(CoreData::getInstance().getConsoleFont());
|
|
|
|
setFont3D(CoreData::getInstance().getConsoleFont3D());
|
2011-11-04 00:38:43 +01:00
|
|
|
}
|
|
|
|
|
2012-03-20 16:01:18 +01:00
|
|
|
void Console::addStdMessage(const string &s,bool clearOtherLines) {
|
|
|
|
if(clearOtherLines == true) {
|
2013-10-29 07:13:38 +01:00
|
|
|
addLineOnly(Lang::getInstance().getString(s));
|
2012-03-20 16:01:18 +01:00
|
|
|
}
|
|
|
|
else {
|
2013-10-29 07:13:38 +01:00
|
|
|
addLine(Lang::getInstance().getString(s));
|
2012-03-20 16:01:18 +01:00
|
|
|
}
|
2010-04-11 03:25:06 +02:00
|
|
|
}
|
|
|
|
|
2017-10-10 04:09:36 +02:00
|
|
|
void Console::addStdMessage(const string &s,const string &failText, bool clearOtherLines) {
|
2012-11-10 20:39:55 +01:00
|
|
|
if(clearOtherLines == true) {
|
2013-10-29 07:13:38 +01:00
|
|
|
addLineOnly(Lang::getInstance().getString(s) + failText);
|
2012-11-10 20:39:55 +01:00
|
|
|
}
|
|
|
|
else {
|
2013-10-29 07:13:38 +01:00
|
|
|
addLine(Lang::getInstance().getString(s) + failText);
|
2012-11-10 20:39:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-20 16:01:18 +01:00
|
|
|
void Console::addStdScenarioMessage(const string &s,bool clearOtherLines) {
|
|
|
|
if(clearOtherLines == true) {
|
|
|
|
addLineOnly(Lang::getInstance().getScenarioString(s));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
addLine(Lang::getInstance().getScenarioString(s));
|
|
|
|
}
|
2011-04-09 08:20:43 +02:00
|
|
|
}
|
|
|
|
|
2017-10-10 04:09:36 +02:00
|
|
|
void Console::addLineOnly(const string &line) {
|
2012-03-20 16:01:18 +01:00
|
|
|
addLine(line,false,-1,Vec3f(1.f, 1.f, 1.f),false,true);
|
|
|
|
}
|
|
|
|
|
2017-10-10 04:09:36 +02:00
|
|
|
void Console::addLine(const string &line, bool playSound, int playerIndex, Vec3f textColor, bool teamMode,bool clearOtherLines) {
|
2010-10-22 09:28:55 +02:00
|
|
|
try {
|
2011-03-26 22:38:26 +01:00
|
|
|
if(playSound == true) {
|
|
|
|
SoundRenderer::getInstance().playFx(CoreData::getInstance().getClickSoundA());
|
|
|
|
}
|
|
|
|
ConsoleLineInfo info;
|
|
|
|
info.text = line;
|
|
|
|
info.timeStamp = timeElapsed;
|
|
|
|
info.PlayerIndex = playerIndex;
|
|
|
|
info.originalPlayerName = "";
|
|
|
|
info.color = textColor;
|
2011-10-03 02:36:28 +02:00
|
|
|
info.teamMode = teamMode;
|
2010-10-23 06:00:39 +02:00
|
|
|
if(playerIndex >= 0) {
|
|
|
|
GameNetworkInterface *gameNetworkInterface= NetworkManager::getInstance().getGameNetworkInterface();
|
|
|
|
if(gameNetworkInterface != NULL) {
|
2011-03-26 22:38:26 +01:00
|
|
|
info.originalPlayerName = gameNetworkInterface->getGameSettings()->getNetworkPlayerNameByPlayerIndex(playerIndex);
|
|
|
|
//for(int i = 0; i < GameConstants::maxPlayers; ++i) {
|
|
|
|
// printf("i = %d, playerName = [%s]\n",i,gameNetworkInterface->getGameSettings()->getNetworkPlayerName(i).c_str());
|
|
|
|
//}
|
2010-10-23 06:00:39 +02:00
|
|
|
}
|
|
|
|
}
|
2011-03-26 22:38:26 +01:00
|
|
|
//printf("info.PlayerIndex = %d, line [%s]\n",info.PlayerIndex,info.originalPlayerName.c_str());
|
|
|
|
|
2012-03-20 16:01:18 +01:00
|
|
|
if(clearOtherLines == true) {
|
|
|
|
lines.clear();
|
|
|
|
storedLines.clear();
|
|
|
|
}
|
2011-03-26 22:38:26 +01:00
|
|
|
lines.insert(lines.begin(), info);
|
2013-11-19 07:14:06 +01:00
|
|
|
if((int)lines.size() > maxLines) {
|
2011-03-26 22:38:26 +01:00
|
|
|
lines.pop_back();
|
|
|
|
}
|
2012-09-09 16:12:35 +02:00
|
|
|
if(onlyChatMessagesInStoredLines==false || info.PlayerIndex!=-1) {
|
|
|
|
storedLines.insert(storedLines.begin(), info);
|
2013-11-19 07:14:06 +01:00
|
|
|
if((int)storedLines.size() > maxStoredLines) {
|
2012-09-09 16:12:35 +02:00
|
|
|
storedLines.pop_back();
|
|
|
|
}
|
2011-03-26 22:38:26 +01:00
|
|
|
}
|
2010-04-11 03:25:06 +02:00
|
|
|
}
|
|
|
|
catch(const exception &ex) {
|
2012-10-19 03:31:20 +02:00
|
|
|
char szBuf[8096]="";
|
|
|
|
snprintf(szBuf,8096,"In [%s::%s %d] error [%s]\n",__FILE__,__FUNCTION__,__LINE__,ex.what());
|
2010-10-06 22:22:06 +02:00
|
|
|
SystemFlags::OutputDebug(SystemFlags::debugError,szBuf);
|
2012-04-14 23:21:09 +02:00
|
|
|
throw megaglest_runtime_error(szBuf);
|
2010-04-11 03:25:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-10 04:09:36 +02:00
|
|
|
void Console::addLine(const string &line, bool playSound, const string &playerName, Vec3f textColor, bool teamMode) {
|
2010-12-24 03:10:31 +01:00
|
|
|
try {
|
|
|
|
if(playSound == true) {
|
|
|
|
SoundRenderer::getInstance().playFx(CoreData::getInstance().getClickSoundA());
|
|
|
|
}
|
|
|
|
ConsoleLineInfo info;
|
|
|
|
info.text = line;
|
|
|
|
info.timeStamp = timeElapsed;
|
|
|
|
info.PlayerIndex = -1;
|
|
|
|
info.originalPlayerName = "";
|
2011-03-26 18:17:34 +01:00
|
|
|
info.color = textColor;
|
2011-10-03 02:36:28 +02:00
|
|
|
info.teamMode = teamMode;
|
2010-12-24 03:10:31 +01:00
|
|
|
if(playerName != "") {
|
|
|
|
info.originalPlayerName = playerName;
|
|
|
|
}
|
|
|
|
//printf("info.PlayerIndex = %d, line [%s]\n",info.PlayerIndex,info.originalPlayerName.c_str());
|
|
|
|
|
|
|
|
lines.insert(lines.begin(), info);
|
2013-11-19 07:14:06 +01:00
|
|
|
if((int)lines.size() > maxLines) {
|
2010-12-24 03:10:31 +01:00
|
|
|
lines.pop_back();
|
|
|
|
}
|
|
|
|
storedLines.insert(storedLines.begin(), info);
|
2013-11-19 07:14:06 +01:00
|
|
|
if((int)storedLines.size() > maxStoredLines) {
|
2010-12-24 03:10:31 +01:00
|
|
|
storedLines.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(const exception &ex) {
|
2012-10-19 03:31:20 +02:00
|
|
|
char szBuf[8096]="";
|
|
|
|
snprintf(szBuf,8096,"In [%s::%s %d] error [%s]\n",__FILE__,__FUNCTION__,__LINE__,ex.what());
|
2010-12-24 03:10:31 +01:00
|
|
|
SystemFlags::OutputDebug(SystemFlags::debugError,szBuf);
|
2012-04-14 23:21:09 +02:00
|
|
|
throw megaglest_runtime_error(szBuf);
|
2010-12-24 03:10:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-22 09:28:55 +02:00
|
|
|
void Console::clearStoredLines() {
|
|
|
|
while(storedLines.empty() == false) {
|
2010-04-14 22:49:14 +02:00
|
|
|
storedLines.pop_back();
|
2010-12-24 03:10:31 +01:00
|
|
|
}
|
2010-04-14 22:49:14 +02:00
|
|
|
}
|
|
|
|
|
2010-10-22 09:28:55 +02:00
|
|
|
void Console::update() {
|
|
|
|
timeElapsed += 1.f / GameConstants::updateFps;
|
2010-12-24 03:10:31 +01:00
|
|
|
|
2010-10-22 09:28:55 +02:00
|
|
|
if(lines.empty() == false) {
|
2010-10-23 06:00:39 +02:00
|
|
|
if(lines.back().timeStamp < (timeElapsed - timeout)) {
|
2010-04-11 03:25:06 +02:00
|
|
|
lines.pop_back();
|
|
|
|
}
|
2010-12-24 03:10:31 +01:00
|
|
|
}
|
2010-04-11 03:25:06 +02:00
|
|
|
}
|
|
|
|
|
2010-10-22 09:28:55 +02:00
|
|
|
bool Console::isEmpty() {
|
2010-12-24 03:10:31 +01:00
|
|
|
return lines.empty();
|
2010-04-11 03:25:06 +02:00
|
|
|
}
|
2010-10-23 06:00:39 +02:00
|
|
|
|
2017-10-10 08:09:52 +02:00
|
|
|
//string Console::getLine(int i) const {
|
|
|
|
// if(i < 0 || i >= (int)lines.size())
|
|
|
|
// throw megaglest_runtime_error("i >= Lines.size()");
|
|
|
|
// return lines[i].text;
|
|
|
|
//}
|
|
|
|
|
|
|
|
//string Console::getStoredLine(int i) const {
|
|
|
|
// if(i < 0 || i >= (int)storedLines.size())
|
|
|
|
// throw megaglest_runtime_error("i >= storedLines.size()");
|
|
|
|
// return storedLines[i].text;
|
|
|
|
//}
|
2010-10-23 06:00:39 +02:00
|
|
|
|
|
|
|
ConsoleLineInfo Console::getLineItem(int i) const {
|
2013-11-19 07:14:06 +01:00
|
|
|
if(i < 0 || i >= (int)lines.size())
|
2012-04-14 23:21:09 +02:00
|
|
|
throw megaglest_runtime_error("i >= Lines.size()");
|
2010-10-23 06:00:39 +02:00
|
|
|
return lines[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
ConsoleLineInfo Console::getStoredLineItem(int i) const {
|
2013-11-19 07:14:06 +01:00
|
|
|
if(i < 0 || i >= (int)storedLines.size())
|
2012-04-14 23:21:09 +02:00
|
|
|
throw megaglest_runtime_error("i >= storedLines.size()");
|
2010-10-23 06:00:39 +02:00
|
|
|
return storedLines[i];
|
|
|
|
}
|
|
|
|
|
2010-04-11 03:25:06 +02:00
|
|
|
|
|
|
|
}}//end namespace
|