From 6ea34ea46cee3571eedeb374d5e9685358312573 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Thu, 20 Sep 2012 04:10:34 +0000 Subject: [PATCH] - added cell hint text entry when placing cell markers (supports \n and \t for text formatting) --- source/glest_game/game/chat_manager.cpp | 58 ++++++++++++++----- source/glest_game/game/chat_manager.h | 16 ++++- source/glest_game/game/game.cpp | 54 ++++++++++++++--- source/glest_game/game/game.h | 7 ++- source/glest_game/graphics/renderer.cpp | 41 +++++++++++-- source/glest_game/graphics/renderer.h | 2 +- source/glest_game/network/network_interface.h | 2 + 7 files changed, 149 insertions(+), 31 deletions(-) diff --git a/source/glest_game/game/chat_manager.cpp b/source/glest_game/game/chat_manager.cpp index 6ea15299..5663ccbd 100644 --- a/source/glest_game/game/chat_manager.cpp +++ b/source/glest_game/game/chat_manager.cpp @@ -47,6 +47,8 @@ ChatManager::ChatManager() { font=CoreData::getInstance().getConsoleFont(); font3D=CoreData::getInstance().getConsoleFont3D(); inMenu=false; + customCB = NULL; + this->maxCustomTextLength = maxTextLenght; } void ChatManager::init(Console* console, int thisTeamIndex, const bool inMenu, string manualPlayerNameOverride) { @@ -69,13 +71,18 @@ void ChatManager::keyUp(SDL_KeyboardEvent key) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); try { - if(editEnabled) { + if(editEnabled == true) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] key = [%c] [%d]\n",__FILE__,__FUNCTION__,__LINE__,key.keysym.sym,key.keysym.sym); if(isKeyPressed(SDLK_ESCAPE,key,false) == true) { text.clear(); textCharLength.clear(); editEnabled= false; + + if(customCB != NULL) { + customCB->processInputText(text,true); + customCB = NULL; + } } } } @@ -127,24 +134,34 @@ void ChatManager::keyDown(SDL_KeyboardEvent key) { GameNetworkInterface *gameNetworkInterface= NetworkManager::getInstance().getGameNetworkInterface(); if(text.empty() == false) { - string playerName = gameNetworkInterface->getHumanPlayerName(); - int playerIndex = gameNetworkInterface->getHumanPlayerIndex(); - if(this->manualPlayerNameOverride != "") { - console->addLine(text,false,this->manualPlayerNameOverride,Vec3f(1.f, 1.f, 1.f),teamMode); - } - else { - console->addLine(text,false,playerIndex,Vec3f(1.f, 1.f, 1.f),teamMode); - } + if(customCB == NULL) { + string playerName = gameNetworkInterface->getHumanPlayerName(); + int playerIndex = gameNetworkInterface->getHumanPlayerIndex(); - gameNetworkInterface->sendTextMessage("*"+text, teamMode? thisTeamIndex: -1, false, ""); -// if(inMenu == false) { -// editEnabled= false; -// } + if(this->manualPlayerNameOverride != "") { + console->addLine(text,false,this->manualPlayerNameOverride,Vec3f(1.f, 1.f, 1.f),teamMode); + } + else { + console->addLine(text,false,playerIndex,Vec3f(1.f, 1.f, 1.f),teamMode); + } + + gameNetworkInterface->sendTextMessage("*"+text, teamMode? thisTeamIndex: -1, false, ""); + // if(inMenu == false) { + // editEnabled= false; + // } + } } else { editEnabled= false; } + + if(customCB != NULL) { + customCB->processInputText(text,false); + editEnabled= false; + customCB = NULL; + } + text.clear(); textCharLength.clear(); } @@ -309,7 +326,8 @@ void ChatManager::keyDown(SDL_KeyboardEvent key) { void ChatManager::keyPress(SDL_KeyboardEvent c) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] key = [%c] [%d]\n",__FILE__,__FUNCTION__,__LINE__,c.keysym.sym,c.keysym.sym); - if(editEnabled && text.size() < maxTextLenght) { + int maxTextLenAllowed = (customCB != NULL ? this->maxCustomTextLength : maxTextLenght); + if(editEnabled && text.size() < maxTextLenAllowed) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] key = [%c] [%d]\n",__FILE__,__FUNCTION__,__LINE__,c.keysym.sym,c.keysym.sym); //space is the first meaningful code wchar_t key = extractKeyPressedUnicode(c); @@ -318,10 +336,17 @@ void ChatManager::keyPress(SDL_KeyboardEvent c) { } } -void ChatManager::switchOnEdit() { +void ChatManager::switchOnEdit(CustomInputCallbackInterface *customCB,int maxCustomTextLength) { editEnabled= true; text.clear(); textCharLength.clear(); + this->customCB = customCB; + if(maxCustomTextLength > 0) { + this->maxCustomTextLength = maxCustomTextLength; + } + else { + this->maxCustomTextLength = maxTextLenght; + } } void ChatManager::deleteText(int deleteCount,bool addToAutoCompleteBuffer) { @@ -402,7 +427,8 @@ void ChatManager::updateAutoCompleteBuffer() { } void ChatManager::addText(string text) { - if(editEnabled && text.size() + this->text.size() < maxTextLenght) { + int maxTextLenAllowed = (customCB != NULL ? this->maxCustomTextLength : maxTextLenght); + if(editEnabled && text.size() + this->text.size() < maxTextLenAllowed) { this->text += text; } } diff --git a/source/glest_game/game/chat_manager.h b/source/glest_game/game/chat_manager.h index 9908558f..30c99095 100644 --- a/source/glest_game/game/chat_manager.h +++ b/source/glest_game/game/chat_manager.h @@ -32,6 +32,14 @@ namespace Glest{ namespace Game{ class Console; +// +// This interface describes the methods a callback object must implement +// +class CustomInputCallbackInterface { +public: + virtual void processInputText(string text, bool cancelled) = 0; +}; + // ===================================================== // class ChatManager // ===================================================== @@ -57,6 +65,9 @@ private: string lastAutoCompleteSearchText; vector autoCompleteTextList; + CustomInputCallbackInterface *customCB; + int maxCustomTextLength; + void appendText(const wchar_t *addText, bool validateChars=true,bool addToAutoCompleteBuffer=true); void deleteText(int deleteCount,bool addToAutoCompleteBuffer=true); void updateAutoCompleteBuffer(); @@ -85,13 +96,14 @@ public: void setFont(Font2D *font) {this->font= font;} void setFont3D(Font3D *font) {this->font3D= font;} void addText(string text); - void switchOnEdit(); - + void switchOnEdit(CustomInputCallbackInterface *customCB=NULL,int maxCustomTextLength=-1); bool getDisableTeamMode() const { return disableTeamMode; } void setDisableTeamMode(bool value); void setAutoCompleteTextList(vector list) { autoCompleteTextList = list; } + + bool isInCustomInputMode() const { return customCB != NULL; }; }; }}//end namespace diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index c7bdb4ea..4784f64e 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -121,6 +121,8 @@ Game::Game() : ProgramState(NULL) { keyboardSetupPopupMenuIndex = -1; isMarkCellEnabled = false; + isMarkCellTextEnabled = false; + markCellTexture = NULL; isUnMarkCellEnabled = false; unmarkCellTexture = NULL; @@ -176,6 +178,8 @@ void Game::resetMembers() { keyboardSetupPopupMenuIndex = -1; isMarkCellEnabled = false; + isMarkCellTextEnabled = false; + markCellTexture = NULL; isUnMarkCellEnabled = false; unmarkCellTexture = NULL; @@ -2349,14 +2353,17 @@ void Game::mouseDownLeft(int x, int y) { Vec2i targetPos(vertex.x,vertex.z); MarkedCell mc(targetPos,world.getThisFaction(),"placeholder for note"); - mapMarkedCellList[surfaceCellPos] = mc; - GameNetworkInterface *gameNetworkInterface= NetworkManager::getInstance().getGameNetworkInterface(); - gameNetworkInterface->sendMarkCellMessage(mc.getTargetPos(),mc.getFaction()->getIndex(),mc.getNote()); + //GameNetworkInterface *gameNetworkInterface= NetworkManager::getInstance().getGameNetworkInterface(); + //gameNetworkInterface->sendMarkCellMessage(mc.getTargetPos(),mc.getFaction()->getIndex(),mc.getNote()); //printf("#1 ADDED in marked list pos [%s] markedCells.size() = %lu\n",surfaceCellPos.getString().c_str(),mapMarkedCellList.size()); isMarkCellEnabled = false; + cellMarkedData = mc; + cellMarkedPos = surfaceCellPos; + isMarkCellTextEnabled = true; + chatManager.switchOnEdit(this,500); Renderer &renderer= Renderer::getInstance(); //renderer.updateMarkedCellScreenPosQuadCache(surfaceCellPos); @@ -2426,14 +2433,19 @@ void Game::mouseDownLeft(int x, int y) { Vec2i surfaceCellPos = map->toSurfCoords(targetPos); MarkedCell mc(targetPos,world.getThisFaction(),"placeholder for note"); - mapMarkedCellList[surfaceCellPos] = mc; + cellMarkedData = mc; + //mapMarkedCellList[surfaceCellPos] = mc; - GameNetworkInterface *gameNetworkInterface= NetworkManager::getInstance().getGameNetworkInterface(); - gameNetworkInterface->sendMarkCellMessage(mc.getTargetPos(),mc.getFaction()->getIndex(),mc.getNote()); + //GameNetworkInterface *gameNetworkInterface= NetworkManager::getInstance().getGameNetworkInterface(); + //gameNetworkInterface->sendMarkCellMessage(mc.getTargetPos(),mc.getFaction()->getIndex(),mc.getNote()); //printf("#2 ADDED in marked list pos [%s] markedCells.size() = %lu\n",surfaceCellPos.getString().c_str(),mapMarkedCellList.size()); isMarkCellEnabled = false; + cellMarkedData = mc; + cellMarkedPos = surfaceCellPos; + isMarkCellTextEnabled = true; + chatManager.switchOnEdit(this,500); //renderer.updateMarkedCellScreenPosQuadCache(surfaceCellPos); renderer.forceQuadCacheUpdate(); @@ -2805,8 +2817,9 @@ void Game::mouseMove(int x, int y, const MouseState *ms) { lastMousePos.x = mouseX; lastMousePos.y = mouseY; - Renderer::getInstance().computePosition(Vec2i(mouseX, mouseY), mouseCellPos); + Renderer &renderer= Renderer::getInstance(); + renderer.computePosition(Vec2i(mouseX, mouseY), mouseCellPos); } catch(const exception &ex) { char szBuf[4096]=""; @@ -2860,6 +2873,32 @@ void Game::eventMouseWheel(int x, int y, int zDelta) { } } +void Game::processInputText(string text, bool cancelled) { + isMarkCellTextEnabled = false; + + if(cancelled == false) { + //printf("Note [%s]\n",text.c_str()); + + if(text.find("\\n") != text.npos) { + replaceAll(text, "\\n", "\n"); + } + if(text.find("\\t") != text.npos) { + replaceAll(text, "\\t", "\t"); + } + + cellMarkedData.setNote(text); + //MarkedCell mc(targetPos,world.getThisFaction(),"placeholder for note"); + //mapMarkedCellList[surfaceCellPos] = mc; + mapMarkedCellList[cellMarkedPos] = cellMarkedData; + + GameNetworkInterface *gameNetworkInterface= NetworkManager::getInstance().getGameNetworkInterface(); + gameNetworkInterface->sendMarkCellMessage(cellMarkedData.getTargetPos(),cellMarkedData.getFaction()->getIndex(),cellMarkedData.getNote()); + + Renderer &renderer= Renderer::getInstance(); + renderer.forceQuadCacheUpdate(); + } +} + void Game::keyDown(SDL_KeyboardEvent key) { if(this->masterserverMode == true) { return; @@ -3598,6 +3637,7 @@ void Game::render2d() { } renderer.renderVisibleMarkedCells(); + renderer.renderVisibleMarkedCells(true,lastMousePos.x,lastMousePos.y); //selection renderer.renderSelectionQuad(); diff --git a/source/glest_game/game/game.h b/source/glest_game/game/game.h index 25dd1534..9c21c618 100644 --- a/source/glest_game/game/game.h +++ b/source/glest_game/game/game.h @@ -57,7 +57,7 @@ enum LoadGameItem { // // Main game class // ===================================================== -class Game: public ProgramState, public FileCRCPreCacheThreadCallbackInterface { +class Game: public ProgramState, public FileCRCPreCacheThreadCallbackInterface, public CustomInputCallbackInterface { public: static const float highlightTime; enum Speed{ @@ -157,6 +157,10 @@ private: ProgramState *currentUIState; bool isMarkCellEnabled; + Vec2i cellMarkedPos; + MarkedCell cellMarkedData; + bool isMarkCellTextEnabled; + Texture2D *markCellTexture; bool isUnMarkCellEnabled; Texture2D *unmarkCellTexture; @@ -326,6 +330,7 @@ private: void updateNetworkUnMarkedCells(); void updateNetworkHighligtedCells(); + virtual void processInputText(string text, bool cancelled); }; }}//end namespace diff --git a/source/glest_game/graphics/renderer.cpp b/source/glest_game/graphics/renderer.cpp index 304c417d..ec224a7b 100644 --- a/source/glest_game/graphics/renderer.cpp +++ b/source/glest_game/graphics/renderer.cpp @@ -2189,7 +2189,10 @@ void Renderer::renderChatManager(const ChatManager *chatManager) { if(chatManager->getEditEnabled()) { string text=""; - if(chatManager->getInMenu()) { + if(chatManager->isInCustomInputMode() == true) { + text += lang.get("CellHint"); + } + else if(chatManager->getInMenu()) { text += lang.get("Chat"); } else if(chatManager->getTeamMode()) { @@ -5992,7 +5995,11 @@ void Renderer::renderMarkedCellsOnMinimap() { } } } -void Renderer::renderVisibleMarkedCells() { +void Renderer::renderVisibleMarkedCells(bool renderTextHint,int x, int y) { + if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + // Draw marked cells std::map markedCells = game->getMapMarkedCellList(); if(markedCells.empty() == false) { @@ -6004,8 +6011,34 @@ void Renderer::renderVisibleMarkedCells() { std::pair bmVisible = posInCellQuadCache( map->toSurfCoords(bm.getTargetPos())); if(bmVisible.first == true) { - const Texture2D *texture= game->getMarkCellTexture(); - renderTextureQuad(bmVisible.second.x,bmVisible.second.y+10,32,32,texture,0.8f); + if(renderTextHint == true) { + if(bm.getNote() != "") { + bool validPosObjWorld= x > bmVisible.second.x && + y > bmVisible.second.y && + x < bmVisible.second.x + 32 && + y < bmVisible.second.y + 32; + + if(validPosObjWorld) { + //printf("Checking for hint text render mouse [%d,%d] marker pos [%d,%d] validPosObjWorld = %d, hint [%s]\n",x,y,bm.getTargetPos().x,bm.getTargetPos().y,validPosObjWorld,bm.getNote().c_str()); + + //Lang &lang= Lang::getInstance(); + Vec4f fontColor = Vec4f(1.0f, 1.0f, 1.0f, 0.25f); + + if(renderText3DEnabled == true) { + renderTextShadow3D(bm.getNote(), CoreData::getInstance().getConsoleFont3D(), fontColor, + bmVisible.second.x, bmVisible.second.y); + } + else { + renderTextShadow(bm.getNote(), CoreData::getInstance().getConsoleFont(), fontColor, + bmVisible.second.x, bmVisible.second.y); + } + } + } + } + else { + const Texture2D *texture= game->getMarkCellTexture(); + renderTextureQuad(bmVisible.second.x,bmVisible.second.y+10,32,32,texture,0.8f); + } } } } diff --git a/source/glest_game/graphics/renderer.h b/source/glest_game/graphics/renderer.h index 2baf7e7a..337d6c97 100644 --- a/source/glest_game/graphics/renderer.h +++ b/source/glest_game/graphics/renderer.h @@ -594,7 +594,7 @@ public: Vec3f getMarkedCellScreenPosQuadCache(Vec2i pos); void updateMarkedCellScreenPosQuadCache(Vec2i pos); void forceQuadCacheUpdate(); - void renderVisibleMarkedCells(); + void renderVisibleMarkedCells(bool renderTextHint=false,int x=-1, int y=-1); void renderMarkedCellsOnMinimap(); void renderHighlightedCellsOnMinimap(); diff --git a/source/glest_game/network/network_interface.h b/source/glest_game/network/network_interface.h index 871616a8..b191bcc9 100644 --- a/source/glest_game/network/network_interface.h +++ b/source/glest_game/network/network_interface.h @@ -120,6 +120,8 @@ public: void decrementAliveCount() { this->aliveCount--; } int getAliveCount() const { return aliveCount; } void setAliveCount(int value) { this->aliveCount = value; } + + void setNote(string value) { note = value; } }; class UnMarkedCell {