From e65e222f2ccbf60a4c224bbe29884ebb1001cfd7 Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Tue, 24 Jul 2012 13:03:28 +0100 Subject: [PATCH] Integer values for prefs, Default decoration colour, clear decoration --- src/client/Client.cpp | 119 ++++++++++++++++++++++++++++++-- src/client/Client.h | 4 ++ src/game/DecorationTool.h | 6 +- src/game/GameModel.cpp | 18 ++++- src/simulation/Simulation.cpp | 58 ++++++++++------ src/simulation/Simulation.h | 4 +- src/simulation/SimulationData.h | 1 + 7 files changed, 176 insertions(+), 34 deletions(-) diff --git a/src/client/Client.cpp b/src/client/Client.cpp index de37a6c62..2e74c48e1 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -1438,6 +1438,34 @@ double Client::GetPrefNumber(std::string property, double defaultValue) return defaultValue; } +int Client::GetPrefInteger(std::string property, int defaultValue) +{ + try + { + std::stringstream defHexInt; + defHexInt << std::hex << defaultValue; + + std::string hexString = GetPrefString(property, defHexInt.str()); + int finalValue = defaultValue; + + std::stringstream hexInt; + hexInt << hexString; + + hexInt >> std::hex >> finalValue; + + return finalValue; + } + catch (json::Exception & e) + { + + } + catch(exception & e) + { + + } + return defaultValue; +} + vector Client::GetPrefStringArray(std::string property) { try @@ -1446,8 +1474,15 @@ vector Client::GetPrefStringArray(std::string property) vector strArray; for(json::Array::iterator iter = value.Begin(); iter != value.End(); ++iter) { - json::String cValue = *iter; - strArray.push_back(cValue.Value()); + try + { + json::String cValue = *iter; + strArray.push_back(cValue.Value()); + } + catch (json::Exception & e) + { + + } } return strArray; } @@ -1466,8 +1501,15 @@ vector Client::GetPrefNumberArray(std::string property) vector strArray; for(json::Array::iterator iter = value.Begin(); iter != value.End(); ++iter) { - json::Number cValue = *iter; - strArray.push_back(cValue.Value()); + try + { + json::Number cValue = *iter; + strArray.push_back(cValue.Value()); + } + catch (json::Exception & e) + { + + } } return strArray; } @@ -1478,6 +1520,40 @@ vector Client::GetPrefNumberArray(std::string property) return vector(); } +vector Client::GetPrefIntegerArray(std::string property) +{ + try + { + json::Array value = GetPref(property); + vector intArray; + for(json::Array::iterator iter = value.Begin(); iter != value.End(); ++iter) + { + try + { + json::String cValue = *iter; + int finalValue = 0; + + std::string hexString = cValue.Value(); + std::stringstream hexInt; + hexInt << std::hex << hexString; + hexInt >> finalValue; + + intArray.push_back(finalValue); + } + catch (json::Exception & e) + { + + } + } + return intArray; + } + catch (json::Exception & e) + { + + } + return vector(); +} + vector Client::GetPrefBoolArray(std::string property) { try @@ -1486,8 +1562,15 @@ vector Client::GetPrefBoolArray(std::string property) vector strArray; for(json::Array::iterator iter = value.Begin(); iter != value.End(); ++iter) { - json::Boolean cValue = *iter; - strArray.push_back(cValue.Value()); + try + { + json::Boolean cValue = *iter; + strArray.push_back(cValue.Value()); + } + catch (json::Exception & e) + { + + } } return strArray; } @@ -1524,6 +1607,14 @@ void Client::SetPref(std::string property, double value) SetPref(property, numberValue); } +void Client::SetPref(std::string property, int value) +{ + std::stringstream hexInt; + hexInt << std::hex << value; + json::UnknownElement intValue = json::String(hexInt.str()); + SetPref(property, intValue); +} + void Client::SetPref(std::string property, vector value) { json::Array newArray; @@ -1557,6 +1648,20 @@ void Client::SetPref(std::string property, vector value) SetPref(property, newArrayValue); } +void Client::SetPref(std::string property, vector value) +{ + json::Array newArray; + for(vector::iterator iter = value.begin(); iter != value.end(); ++iter) + { + std::stringstream hexInt; + hexInt << std::hex << *iter; + + newArray.Insert(json::String(hexInt.str())); + } + json::UnknownElement newArrayValue = newArray; + SetPref(property, newArrayValue); +} + void Client::SetPref(std::string property, bool value) { json::UnknownElement boolValue = json::Boolean(value); @@ -1570,7 +1675,7 @@ json::UnknownElement Client::GetPref(std::string property) json::UnknownElement currentRef = configDocumentCopy; for(vector::iterator iter = pTokens.begin(); iter != pTokens.end(); ++iter) { - currentRef = currentRef[*iter]; + currentRef = ((const json::UnknownElement &)currentRef)[*iter]; } return currentRef; } diff --git a/src/client/Client.h b/src/client/Client.h index cb39f2e8f..7b9439375 100644 --- a/src/client/Client.h +++ b/src/client/Client.h @@ -120,15 +120,19 @@ public: std::string GetPrefString(std::string property, std::string defaultValue); double GetPrefNumber(std::string property, double defaultValue); + int GetPrefInteger(std::string property, int defaultValue); vector GetPrefStringArray(std::string property); vector GetPrefNumberArray(std::string property); + vector GetPrefIntegerArray(std::string property); vector GetPrefBoolArray(std::string property); bool GetPrefBool(std::string property, bool defaultValue); void SetPref(std::string property, std::string value); void SetPref(std::string property, double value); + void SetPref(std::string property, int value); void SetPref(std::string property, vector value); void SetPref(std::string property, vector value); + void SetPref(std::string property, vector value); void SetPref(std::string property, vector value); void SetPref(std::string property, bool value); diff --git a/src/game/DecorationTool.h b/src/game/DecorationTool.h index be79e6bc2..e2e47585d 100644 --- a/src/game/DecorationTool.h +++ b/src/game/DecorationTool.h @@ -7,7 +7,7 @@ class DecorationTool: public Tool { public: - enum ToolType { BlendAdd = DECO_ADD, BlendRemove = DECO_SUBTRACT, BlendMultiply = DECO_MULTIPLY, BlendDivide = DECO_DIVIDE, BlendSet = DECO_DRAW, BlendSmudge = DECO_SMUDGE }; + enum ToolType { BlendAdd = DECO_ADD, BlendRemove = DECO_SUBTRACT, BlendMultiply = DECO_MULTIPLY, BlendDivide = DECO_DIVIDE, BlendSet = DECO_DRAW, BlendSmudge = DECO_SMUDGE, Remove = DECO_CLEAR }; ToolType decoMode; @@ -27,10 +27,10 @@ public: } virtual ~DecorationTool() {} virtual void Draw(Simulation * sim, Brush * brush, ui::Point position){ - sim->ApplyDecorationPoint(position.X, position.Y, 1, 1, Red, Green, Blue, Alpha, decoMode, brush); + sim->ApplyDecorationPoint(position.X, position.Y, Red, Green, Blue, Alpha, decoMode, brush); } virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) { - sim->ApplyDecorationLine(position1.X, position1.Y, position2.X, position2.Y, 1, 1, Red, Green, Blue, Alpha, decoMode, brush); + sim->ApplyDecorationLine(position1.X, position1.Y, position2.X, position2.Y, Red, Green, Blue, Alpha, decoMode, brush); } virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) { sim->ApplyDecorationBox(position1.X, position1.Y, position2.X, position2.Y, Red, Green, Blue, Alpha, decoMode); diff --git a/src/game/GameModel.cpp b/src/game/GameModel.cpp index 29d13ce0c..f3ce1bc01 100644 --- a/src/game/GameModel.cpp +++ b/src/game/GameModel.cpp @@ -20,7 +20,8 @@ GameModel::GameModel(): colourSelector(false), clipboard(NULL), stamp(NULL), - placeSave(NULL) + placeSave(NULL), + colour(255, 0, 0, 255) { sim = new Simulation(); ren = new Renderer(ui::Engine::Ref().g, sim); @@ -108,6 +109,7 @@ GameModel::GameModel(): 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)); //Set default brush palette brushList.push_back(new EllipseBrush(ui::Point(4, 4))); @@ -135,6 +137,15 @@ GameModel::GameModel(): if(stampFile && stampFile->GetGameSave()) stamp = stampFile->GetGameSave(); } + + + //Set default decoration colour + unsigned char colourR = min(Client::Ref().GetPrefInteger("Decoration.Red", 200), 255); + unsigned char colourG = min(Client::Ref().GetPrefInteger("Decoration.Green", 100), 255); + unsigned char colourB = min(Client::Ref().GetPrefInteger("Decoration.Blue", 50), 255); + unsigned char colourA = min(Client::Ref().GetPrefInteger("Decoration.Alpha", 255), 255); + + SetColourSelectorColour(ui::Colour(colourR, colourG, colourB, colourA)); } GameModel::~GameModel() @@ -148,6 +159,11 @@ GameModel::~GameModel() std::vector renderModes = ren->GetRenderMode(); Client::Ref().SetPref("Renderer.RenderModes", std::vector(renderModes.begin(), renderModes.end())); + Client::Ref().SetPref("Decoration.Red", (int)colour.Red); + Client::Ref().SetPref("Decoration.Green", (int)colour.Green); + Client::Ref().SetPref("Decoration.Blue", (int)colour.Blue); + Client::Ref().SetPref("Decoration.Alpha", (int)colour.Alpha); + for(int i = 0; i < menuList.size(); i++) { delete menuList[i]; diff --git a/src/simulation/Simulation.cpp b/src/simulation/Simulation.cpp index 7c8241944..4a7bede0e 100644 --- a/src/simulation/Simulation.cpp +++ b/src/simulation/Simulation.cpp @@ -700,6 +700,10 @@ void Simulation::ApplyDecoration(int x, int y, int colR_, int colG_, int colB_, tg = colG; tb = colB; } + else if (mode == DECO_CLEAR) + { + ta = tr = tg = tb = 0.0f; + } else if (mode == DECO_ADD) { ta += (colA*0.1f)*colA; @@ -773,27 +777,32 @@ void Simulation::ApplyDecoration(int x, int y, int colR_, int colG_, int colB_, parts[rp>>8].dcolour = ((colA_<<24)|(colR_<<16)|(colG_<<8)|colB_); } -void Simulation::ApplyDecorationPoint(int x, int y, int rx, int ry, int colR, int colG, int colB, int colA, int mode, Brush * cBrush) +void Simulation::ApplyDecorationPoint(int positionX, int positionY, int colR, int colG, int colB, int colA, int mode, Brush * cBrush) { int i, j; if(cBrush) { - rx = cBrush->GetRadius().X; - ry = cBrush->GetRadius().Y; - } + int radiusX, radiusY, sizeX, sizeY; + + radiusX = cBrush->GetRadius().X; + radiusY = cBrush->GetRadius().Y; + + sizeX = cBrush->GetSize().X; + sizeY = cBrush->GetSize().Y; - if (rx == 0 && ry == 0) - { - ApplyDecoration(x, y, colR, colG, colB, colA, mode); - return; + unsigned char *bitmap = cBrush->GetBitmap(); + for(int y = 0; y < sizeY; y++) + { + for(int x = 0; x < sizeX; x++) + { + if(bitmap[(y*sizeX)+x] && (positionX+(x-radiusX) >= 0 && positionY+(y-radiusY) >= 0 && positionX+(x-radiusX) < XRES && positionY+(y-radiusY) < YRES)) + { + ApplyDecoration(positionX+(x-radiusX), positionY+(y-radiusY), colR, colG, colB, colA, mode); + } + } + } } - - unsigned char *bitmap = cBrush->GetBitmap(); - for (j=-ry; j<=ry; j++) - for (i=-rx; i<=rx; i++) - if(bitmap[(j+ry)*(rx*2)+(i+rx)]) - ApplyDecoration(x+i, y+j, colR, colG, colB, colA, mode); } void Simulation::ApplyDecorationBox(int x1, int y1, int x2, int y2, int colR, int colG, int colB, int colA, int mode) @@ -814,13 +823,20 @@ void Simulation::ApplyDecorationBox(int x1, int y1, int x2, int y2, int colR, in } for (j=y1; j<=y2; j++) for (i=x1; i<=x2; i++) - ApplyDecorationPoint(i, j, 0, 0, colR, colG, colB, colA, mode); + ApplyDecoration(i, j, colR, colG, colB, colA, mode); } -void Simulation::ApplyDecorationLine(int x1, int y1, int x2, int y2, int rx, int ry, int colR, int colG, int colB, int colA, int mode, Brush * cBrush) +void Simulation::ApplyDecorationLine(int x1, int y1, int x2, int y2, int colR, int colG, int colB, int colA, int mode, Brush * cBrush) { - int cp=abs(y2-y1)>abs(x2-x1), x, y, dx, dy, sy; + int cp=abs(y2-y1)>abs(x2-x1), x, y, dx, dy, sy, rx, ry; float e, de; + + if(cBrush) + { + rx = cBrush->GetRadius().X; + ry = cBrush->GetRadius().Y; + } + if (cp) { y = x1; @@ -851,9 +867,9 @@ void Simulation::ApplyDecorationLine(int x1, int y1, int x2, int y2, int rx, int for (x=x1; x<=x2; x++) { if (cp) - ApplyDecorationPoint(y, x, rx, ry, colR, colG, colB, colA, mode, cBrush); + ApplyDecorationPoint(y, x, colR, colG, colB, colA, mode, cBrush); else - ApplyDecorationPoint(x, y, rx, ry, colR, colG, colB, colA, mode, cBrush); + ApplyDecorationPoint(x, y, colR, colG, colB, colA, mode, cBrush); e += de; if (e >= 0.5f) { @@ -861,9 +877,9 @@ void Simulation::ApplyDecorationLine(int x1, int y1, int x2, int y2, int rx, int if (!(rx+ry)) { if (cp) - ApplyDecorationPoint(y, x, rx, ry, colR, colG, colB, colA, mode, cBrush); + ApplyDecorationPoint(y, x, colR, colG, colB, colA, mode, cBrush); else - ApplyDecorationPoint(x, y, rx, ry, colR, colG, colB, colA, mode, cBrush); + ApplyDecorationPoint(x, y, colR, colG, colB, colA, mode, cBrush); } e -= 1.0f; } diff --git a/src/simulation/Simulation.h b/src/simulation/Simulation.h index e4e4120cb..d4f43f2d9 100644 --- a/src/simulation/Simulation.h +++ b/src/simulation/Simulation.h @@ -173,8 +173,8 @@ public: void CreateWallLine(int x1, int y1, int x2, int y2, int rx, int ry, int c, int flags, Brush * cBrush = NULL); void ApplyDecoration(int x, int y, int colR, int colG, int colB, int colA, int mode); - void ApplyDecorationPoint(int x, int y, int rx, int ry, int colR, int colG, int colB, int colA, int mode, Brush * cBrush = NULL); - void ApplyDecorationLine(int x1, int y1, int x2, int y2, int rx, int ry, int colR, int colG, int colB, int colA, int mode, Brush * cBrush = NULL); + void ApplyDecorationPoint(int x, int y, int colR, int colG, int colB, int colA, int mode, Brush * cBrush = NULL); + void ApplyDecorationLine(int x1, int y1, int x2, int y2, int colR, int colG, int colB, int colA, int mode, Brush * cBrush = NULL); void ApplyDecorationBox(int x1, int y1, int x2, int y2, int colR, int colG, int colB, int colA, int mode); void *transform_save(void *odata, int *size, matrix2d transform, vector2d translate); diff --git a/src/simulation/SimulationData.h b/src/simulation/SimulationData.h index 72a8e5877..ee64ce21e 100644 --- a/src/simulation/SimulationData.h +++ b/src/simulation/SimulationData.h @@ -81,6 +81,7 @@ #define DECO_MULTIPLY 3 #define DECO_DIVIDE 4 #define DECO_SMUDGE 5 +#define DECO_CLEAR 6 //Old IDs for GOL types #define GT_GOL 78