From 5904844e96da821c276fe3409f6c173e71ef7dc2 Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Wed, 5 Sep 2012 16:56:41 +0100 Subject: Add way of uniquely identifying tools, preserve active menu and tools when rebuilding menus (fixes #170) diff --git a/src/game/DecorationTool.h b/src/game/DecorationTool.h index 39c070b..679c854 100644 --- a/src/game/DecorationTool.h +++ b/src/game/DecorationTool.h @@ -16,8 +16,8 @@ public: unsigned char Blue; unsigned char Alpha; - DecorationTool(ToolType decoMode_, string name, string description, int r, int g, int b): - Tool(0, name, description, r, g, b), + DecorationTool(ToolType decoMode_, string name, string description, int r, int g, int b, std::string identifier): + Tool(0, name, description, r, g, b, identifier), decoMode(decoMode_), Red(0), Green(0), diff --git a/src/game/GameModel.cpp b/src/game/GameModel.cpp index 2870d0d..e083251 100644 --- a/src/game/GameModel.cpp +++ b/src/game/GameModel.cpp @@ -14,6 +14,7 @@ #include "game/DecorationTool.h" #include "GameModelException.h" #include "QuickOptions.h" +#include "Format.h" GameModel::GameModel(): sim(NULL), @@ -27,12 +28,13 @@ GameModel::GameModel(): placeSave(NULL), colour(255, 0, 0, 255), toolStrength(1.0f), - activeColourPreset(-1) + activeColourPreset(-1), + activeMenu(NULL) { sim = new Simulation(); ren = new Renderer(ui::Engine::Ref().g, sim); - memset(activeTools, 0, sizeof(activeTools)); + std::fill(activeTools, activeTools+3, (Tool*)NULL); //Load config into renderer try @@ -176,6 +178,18 @@ void GameModel::BuildQuickOptionMenu() void GameModel::BuildMenus() { + char lastMenu = 0; + if(activeMenu) + lastMenu = activeMenu->GetIcon(); + + std::string activeToolIdentifiers[3]; + if(activeTools[0]) + activeToolIdentifiers[0] = activeTools[0]->GetIdentifier(); + if(activeTools[1]) + activeToolIdentifiers[1] = activeTools[1]->GetIdentifier(); + if(activeTools[2]) + activeToolIdentifiers[2] = activeTools[2]->GetIdentifier(); + //Empty current menus for(std::vector::iterator iter = menuList.begin(), end = menuList.end(); iter != end; ++iter) { @@ -205,19 +219,19 @@ void GameModel::BuildMenus() Tool * tempTool; if(i == PT_LIGH) { - tempTool = new Element_LIGH_Tool(i, sim->elements[i].Name, sim->elements[i].Description, PIXR(sim->elements[i].Colour), PIXG(sim->elements[i].Colour), PIXB(sim->elements[i].Colour), sim->elements[i].IconGenerator); + tempTool = new Element_LIGH_Tool(i, sim->elements[i].Name, sim->elements[i].Description, PIXR(sim->elements[i].Colour), PIXG(sim->elements[i].Colour), PIXB(sim->elements[i].Colour), sim->elements[i].Identifier, sim->elements[i].IconGenerator); } else if(i == PT_TESC) { - tempTool = new Element_TESC_Tool(i, sim->elements[i].Name, sim->elements[i].Description, PIXR(sim->elements[i].Colour), PIXG(sim->elements[i].Colour), PIXB(sim->elements[i].Colour), sim->elements[i].IconGenerator); + tempTool = new Element_TESC_Tool(i, sim->elements[i].Name, sim->elements[i].Description, PIXR(sim->elements[i].Colour), PIXG(sim->elements[i].Colour), PIXB(sim->elements[i].Colour), sim->elements[i].Identifier, sim->elements[i].IconGenerator); } else if(i == PT_STKM || i == PT_FIGH || i == PT_STKM2) { - tempTool = new PlopTool(i, sim->elements[i].Name, sim->elements[i].Description, PIXR(sim->elements[i].Colour), PIXG(sim->elements[i].Colour), PIXB(sim->elements[i].Colour), sim->elements[i].IconGenerator); + tempTool = new PlopTool(i, sim->elements[i].Name, sim->elements[i].Description, PIXR(sim->elements[i].Colour), PIXG(sim->elements[i].Colour), PIXB(sim->elements[i].Colour), sim->elements[i].Identifier, sim->elements[i].IconGenerator); } else { - tempTool = new ElementTool(i, sim->elements[i].Name, sim->elements[i].Description, PIXR(sim->elements[i].Colour), PIXG(sim->elements[i].Colour), PIXB(sim->elements[i].Colour), sim->elements[i].IconGenerator); + tempTool = new ElementTool(i, sim->elements[i].Name, sim->elements[i].Description, PIXR(sim->elements[i].Colour), PIXG(sim->elements[i].Colour), PIXB(sim->elements[i].Colour), sim->elements[i].Identifier, sim->elements[i].IconGenerator); } if(sim->elements[i].MenuSection < 12 && sim->elements[i].MenuVisible) @@ -235,14 +249,14 @@ void GameModel::BuildMenus() //Build menu for GOL types for(int i = 0; i < NGOL; i++) { - Tool * tempTool = new GolTool(i, sim->gmenu[i].name, std::string(sim->gmenu[i].description), PIXR(sim->gmenu[i].colour), PIXG(sim->gmenu[i].colour), PIXB(sim->gmenu[i].colour)); + Tool * tempTool = new GolTool(i, sim->gmenu[i].name, std::string(sim->gmenu[i].description), PIXR(sim->gmenu[i].colour), PIXG(sim->gmenu[i].colour), PIXB(sim->gmenu[i].colour), "DEFAULT_PT_LIFE_"+std::string(sim->gmenu[i].name)); menuList[SC_LIFE]->AddTool(tempTool); } //Build other menus from wall data for(int i = 0; i < UI_WALLCOUNT; i++) { - Tool * tempTool = new WallTool(i, "", std::string(sim->wtypes[i].descs), PIXR(sim->wtypes[i].colour), PIXG(sim->wtypes[i].colour), PIXB(sim->wtypes[i].colour), sim->wtypes[i].textureGen); + Tool * tempTool = new WallTool(i, "", std::string(sim->wtypes[i].descs), PIXR(sim->wtypes[i].colour), PIXG(sim->wtypes[i].colour), PIXB(sim->wtypes[i].colour), "DEFAULT_WL_"+format::NumberToString(i), sim->wtypes[i].textureGen); menuList[SC_WALL]->AddTool(tempTool); //sim->wtypes[i] } @@ -251,24 +265,24 @@ void GameModel::BuildMenus() menuList[SC_TOOL]->AddTool(new SampleTool(this)); menuList[SC_TOOL]->AddTool(new SignTool()); menuList[SC_TOOL]->AddTool(new PropertyTool()); - menuList[SC_TOOL]->AddTool(new WindTool(0, "WIND", "Create air movement", 64, 64, 64)); + menuList[SC_TOOL]->AddTool(new WindTool(0, "WIND", "Create air movement", 64, 64, 64, "DEFAULT_UI_WIND")); //Build menu for simtools for(int i = 0; i < sim->tools.size(); i++) { Tool * tempTool; - tempTool = new Tool(i, sim->tools[i]->Name, sim->tools[i]->Description, PIXR(sim->tools[i]->Colour), PIXG(sim->tools[i]->Colour), PIXB(sim->tools[i]->Colour)); + tempTool = new Tool(i, sim->tools[i]->Name, sim->tools[i]->Description, PIXR(sim->tools[i]->Colour), PIXG(sim->tools[i]->Colour), PIXB(sim->tools[i]->Colour), sim->tools[i]->Identifier); menuList[SC_TOOL]->AddTool(tempTool); } //Add decoration tools to menu - menuList[SC_DECO]->AddTool(new DecorationTool(DecorationTool::BlendAdd, "ADD", "Colour blending: Add", 0, 0, 0)); - menuList[SC_DECO]->AddTool(new DecorationTool(DecorationTool::BlendRemove, "SUB", "Colour blending: Subtract", 0, 0, 0)); - menuList[SC_DECO]->AddTool(new DecorationTool(DecorationTool::BlendMultiply, "MUL", "Colour blending: Multiply", 0, 0, 0)); - menuList[SC_DECO]->AddTool(new DecorationTool(DecorationTool::BlendDivide, "DIV", "Colour blending: Divide" , 0, 0, 0)); - menuList[SC_DECO]->AddTool(new DecorationTool(DecorationTool::BlendSmudge, "SMDG", "Smudge colour", 0, 0, 0)); - menuList[SC_DECO]->AddTool(new DecorationTool(DecorationTool::BlendSet, "SET", "Set colour (No blending)", 0, 0, 0)); - menuList[SC_DECO]->AddTool(new DecorationTool(DecorationTool::Remove, "CLR", "Clear any set decoration", 0, 0, 0)); + menuList[SC_DECO]->AddTool(new DecorationTool(DecorationTool::BlendAdd, "ADD", "Colour blending: Add", 0, 0, 0, "DEFAULT_DECOR_ADD")); + menuList[SC_DECO]->AddTool(new DecorationTool(DecorationTool::BlendRemove, "SUB", "Colour blending: Subtract", 0, 0, 0, "DEFAULT_DECOR_SUB")); + menuList[SC_DECO]->AddTool(new DecorationTool(DecorationTool::BlendMultiply, "MUL", "Colour blending: Multiply", 0, 0, 0, "DEFAULT_DECOR_MUL")); + menuList[SC_DECO]->AddTool(new DecorationTool(DecorationTool::BlendDivide, "DIV", "Colour blending: Divide" , 0, 0, 0, "DEFAULT_DECOR_DIV")); + menuList[SC_DECO]->AddTool(new DecorationTool(DecorationTool::BlendSmudge, "SMDG", "Smudge colour", 0, 0, 0, "DEFAULT_DECOR_SMDG")); + menuList[SC_DECO]->AddTool(new DecorationTool(DecorationTool::BlendSet, "SET", "Set colour (No blending)", 0, 0, 0, "DEFAULT_DECOR_SET")); + menuList[SC_DECO]->AddTool(new DecorationTool(DecorationTool::Remove, "CLR", "Clear any set decoration", 0, 0, 0, "DEFAULT_DECOR_CLR")); //Set default brush palette brushList.push_back(new EllipseBrush(ui::Point(4, 4))); @@ -276,14 +290,35 @@ void GameModel::BuildMenus() brushList.push_back(new TriangleBrush(ui::Point(4, 4))); //Set default tools - activeTools[0] = menuList[SC_POWDERS]->GetToolList()[0]; - activeTools[1] = menuList[SC_SPECIAL]->GetToolList()[0]; - activeTools[2] = menuList[SC_TOOL]->GetToolList()[0]; + activeTools[0] = GetToolFromIdentifier("DEFAULT_PT_DUST");//menuList[SC_POWDERS]->GetToolList()[0]; + activeTools[1] = GetToolFromIdentifier("DEFAULT_UI_SAMPLE");//menuList[SC_SPECIAL]->GetToolList()[0]; + activeTools[2] = GetToolFromIdentifier("DEFAULT_PT_NONE");//menuList[SC_TOOL]->GetToolList()[0]; + + if(activeToolIdentifiers[0].length()) + activeTools[0] = GetToolFromIdentifier(activeToolIdentifiers[0]); + if(activeToolIdentifiers[1].length()) + activeTools[1] = GetToolFromIdentifier(activeToolIdentifiers[1]); + if(activeToolIdentifiers[2].length()) + activeTools[2] = GetToolFromIdentifier(activeToolIdentifiers[2]); + lastTool = activeTools[0]; //Set default menu activeMenu = menuList[SC_POWDERS]; - toolList = menuList[SC_POWDERS]->GetToolList(); + + if(lastMenu) + { + for(std::vector::iterator iter = menuList.begin(), end = menuList.end(); iter != end; ++iter) + { + if((*iter)->GetIcon() == lastMenu) + activeMenu = *iter; + } + } + + if(activeMenu) + toolList = activeMenu->GetToolList(); + else + toolList = std::vector(); notifyMenuListChanged(); notifyToolListChanged(); @@ -291,6 +326,20 @@ void GameModel::BuildMenus() notifyLastToolChanged(); } +Tool * GameModel::GetToolFromIdentifier(std::string identifier) +{ + for(std::vector::iterator iter = menuList.begin(), end = menuList.end(); iter != end; ++iter) + { + std::vector menuTools = (*iter)->GetToolList(); + for(std::vector::iterator titer = menuTools.begin(), tend = menuTools.end(); titer != tend; ++titer) + { + if(identifier == (*titer)->GetIdentifier()) + return *titer; + } + } + return NULL; +} + std::deque GameModel::GetHistory() { return history; diff --git a/src/game/GameModel.h b/src/game/GameModel.h index a46bfd3..577af9a 100644 --- a/src/game/GameModel.h +++ b/src/game/GameModel.h @@ -98,6 +98,8 @@ public: GameModel(); ~GameModel(); + Tool * GetToolFromIdentifier(std::string identifier); + void SetActiveColourPreset(int preset); int GetActiveColourPreset(); diff --git a/src/game/Tool.cpp b/src/game/Tool.cpp index 639808d..7fa8e15 100644 --- a/src/game/Tool.cpp +++ b/src/game/Tool.cpp @@ -13,7 +13,7 @@ using namespace std; -Tool::Tool(int id, string name, string description, int r, int g, int b, VideoBuffer * (*textureGen)(int, int, int)): +Tool::Tool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int)): toolID(id), toolName(name), toolDescription(description), @@ -22,7 +22,8 @@ Tool::Tool(int id, string name, string description, int r, int g, int b, VideoBu colBlue(b), textureGen(textureGen), strength(1.0f), - resolution(1) + resolution(1), + identifier(identifier) { } VideoBuffer * Tool::GetTexture(int width, int height) @@ -37,6 +38,7 @@ void Tool::SetTextureGen(VideoBuffer * (*textureGen)(int, int, int)) { this->textureGen = textureGen; } +std::string Tool::GetIdentifier() { return identifier; } string Tool::GetName() { return toolName; } string Tool::GetDescription() { return toolDescription; } Tool::~Tool() {} @@ -52,8 +54,8 @@ void Tool::DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Po } void Tool::DrawFill(Simulation * sim, Brush * brush, ui::Point position) {}; -ElementTool::ElementTool(int id, string name, string description, int r, int g, int b, VideoBuffer * (*textureGen)(int, int, int)): - Tool(id, name, description, r, g, b, textureGen) +ElementTool::ElementTool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int)): + Tool(id, name, description, r, g, b, identifier, textureGen) { } ElementTool::~ElementTool() {} @@ -71,8 +73,8 @@ void ElementTool::DrawFill(Simulation * sim, Brush * brush, ui::Point position) } -WallTool::WallTool(int id, string name, string description, int r, int g, int b, VideoBuffer * (*textureGen)(int, int, int)): -Tool(id, name, description, r, g, b, textureGen) +WallTool::WallTool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int)): +Tool(id, name, description, r, g, b, identifier, textureGen) { resolution = CELL; } @@ -112,8 +114,8 @@ void WallTool::DrawFill(Simulation * sim, Brush * brush, ui::Point position) { } -GolTool::GolTool(int id, string name, string description, int r, int g, int b, VideoBuffer * (*textureGen)(int, int, int)): - Tool(id, name, description, r, g, b, textureGen) +GolTool::GolTool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int)): + Tool(id, name, description, r, g, b, identifier, textureGen) { } GolTool::~GolTool() {} @@ -130,8 +132,8 @@ void GolTool::DrawFill(Simulation * sim, Brush * brush, ui::Point position) { sim->FloodParts(position.X, position.Y, PT_LIFE|(toolID<<8), -1, -1, 0); } -WindTool::WindTool(int id, string name, string description, int r, int g, int b, VideoBuffer * (*textureGen)(int, int, int)): - Tool(id, name, description, r, g, b, textureGen) +WindTool::WindTool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int)): + Tool(id, name, description, r, g, b, identifier, textureGen) { } WindTool::~WindTool() {} @@ -187,8 +189,8 @@ void Element_LIGH_Tool::Draw(Simulation * sim, Brush * brush, ui::Point position } } -Element_TESC_Tool::Element_TESC_Tool(int id, string name, string description, int r, int g, int b, VideoBuffer * (*textureGen)(int, int, int)): - ElementTool(id, name, description, r, g, b, textureGen) +Element_TESC_Tool::Element_TESC_Tool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int)): + ElementTool(id, name, description, r, g, b, identifier, textureGen) { } void Element_TESC_Tool::Draw(Simulation * sim, Brush * brush, ui::Point position){ diff --git a/src/game/Tool.h b/src/game/Tool.h index 2540a22..22898c7 100644 --- a/src/game/Tool.h +++ b/src/game/Tool.h @@ -27,11 +27,13 @@ protected: string toolDescription; float strength; int resolution; + std::string identifier; public: - Tool(int id, string name, string description, int r, int g, int b, VideoBuffer * (*textureGen)(int, int, int) = NULL); + Tool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int) = NULL); int GetToolID() { return toolID; } string GetName(); string GetDescription(); + std::string GetIdentifier(); int GetResolution() { return resolution; } void SetStrength(float value) { strength = value; } float GetStrength() { return strength; } @@ -50,7 +52,7 @@ class SignTool: public Tool { public: SignTool(): - Tool(0, "SIGN", "Sign. Click a sign to edit or anywhere else to create a new one", 0, 0, 0, SignTool::GetIcon) + Tool(0, "SIGN", "Sign. Click a sign to edit or anywhere else to create a new one", 0, 0, 0, "DEFAULT_UI_SIGN", SignTool::GetIcon) { } static VideoBuffer * GetIcon(int toolID, int width, int height); @@ -69,7 +71,7 @@ class SampleTool: public Tool GameModel * gameModel; public: SampleTool(GameModel * model): - Tool(0, "SMPL", "Sample an element on the screen", 0, 0, 0, SampleTool::GetIcon), + Tool(0, "SMPL", "Sample an element on the screen", 0, 0, 0, "DEFAULT_UI_SAMPLE", SampleTool::GetIcon), gameModel(model) { } @@ -86,7 +88,7 @@ class PropertyTool: public Tool { public: PropertyTool(): - Tool(0, "PROP", "Property Edit. Click to alter the properties of elements in the field", 0xfe, 0xa9, 0x00, NULL) + Tool(0, "PROP", "Property Edit. Click to alter the properties of elements in the field", 0xfe, 0xa9, 0x00, "DEFAULT_UI_PROPERTY", NULL) { } virtual ~PropertyTool() {} @@ -101,8 +103,8 @@ class Element_LIGH_Tool: public Tool { int nextUse; public: - Element_LIGH_Tool(int id, string name, string description, int r, int g, int b, VideoBuffer * (*textureGen)(int, int, int) = NULL): - Tool(id, name, description, r, g, b), + Element_LIGH_Tool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int) = NULL): + Tool(id, name, description, r, g, b, identifier, textureGen), nextUse(0) { } @@ -118,7 +120,7 @@ public: class ElementTool: public Tool { public: - ElementTool(int id, string name, string description, int r, int g, int b, VideoBuffer * (*textureGen)(int, int, int) = NULL); + ElementTool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int) = NULL); virtual ~ElementTool(); virtual void Draw(Simulation * sim, Brush * brush, ui::Point position); virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging = false); @@ -129,7 +131,7 @@ public: class Element_TESC_Tool: public ElementTool { public: - Element_TESC_Tool(int id, string name, string description, int r, int g, int b, VideoBuffer * (*textureGen)(int, int, int) = NULL); + Element_TESC_Tool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int) = NULL); virtual ~Element_TESC_Tool() {} virtual void Draw(Simulation * sim, Brush * brush, ui::Point position); virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging = false); @@ -140,8 +142,8 @@ public: class PlopTool: public ElementTool { public: - PlopTool(int id, string name, string description, int r, int g, int b, VideoBuffer * (*textureGen)(int, int, int) = NULL): - ElementTool(id, name, description, r, g, b) + PlopTool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int) = NULL): + ElementTool(id, name, description, r, g, b, identifier, textureGen) { } virtual ~PlopTool() {} @@ -155,7 +157,7 @@ public: class WallTool: public Tool { public: - WallTool(int id, string name, string description, int r, int g, int b, VideoBuffer * (*textureGen)(int, int, int) = NULL); + WallTool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int) = NULL); virtual ~WallTool(); virtual void Draw(Simulation * sim, Brush * brush, ui::Point position); virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging = false); @@ -166,7 +168,7 @@ public: class GolTool: public Tool { public: - GolTool(int id, string name, string description, int r, int g, int b, VideoBuffer * (*textureGen)(int, int, int) = NULL); + GolTool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int) = NULL); virtual ~GolTool(); virtual void Draw(Simulation * sim, Brush * brush, ui::Point position); virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging = false); @@ -177,7 +179,7 @@ public: class WindTool: public Tool { public: - WindTool(int id, string name, string description, int r, int g, int b, VideoBuffer * (*textureGen)(int, int, int) = NULL); + WindTool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int) = NULL); virtual ~WindTool(); virtual void Draw(Simulation * sim, Brush * brush, ui::Point position); virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging = false); -- cgit v0.9.2-21-gd62e