From 45563e97e813cfd21724ad1111e5de3e04679e1a Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Tue, 15 May 2012 00:10:10 +0100 Subject: [PATCH] Make flood_prop use PropertyType from StructProperty, Property tool works --- src/game/PropertyTool.cpp | 112 ++++++++++++++++++++++++++----- src/game/SignTool.cpp | 33 +++++---- src/simulation/Simulation.cpp | 26 ++++--- src/simulation/Simulation.h | 7 +- src/simulation/elements/BANG.cpp | 2 +- 5 files changed, 136 insertions(+), 44 deletions(-) diff --git a/src/game/PropertyTool.cpp b/src/game/PropertyTool.cpp index 6d6bb6806..ccc7a8d4a 100644 --- a/src/game/PropertyTool.cpp +++ b/src/game/PropertyTool.cpp @@ -1,4 +1,5 @@ #include +#include #include "Style.h" #include "simulation/Simulation.h" #include "Tool.h" @@ -7,6 +8,7 @@ #include "interface/Label.h" #include "interface/Textbox.h" #include "interface/DropDown.h" +#include "dialogues/ErrorMessage.h" class PropertyWindow: public ui::Window { @@ -17,22 +19,25 @@ public: Simulation * sim; int signID; ui::Point position; + std::vector properties; PropertyWindow(PropertyTool * tool_, Simulation * sim_, ui::Point position_); + void SetProperty(); virtual void OnDraw(); virtual ~PropertyWindow() {} -}; - -class OkayAction: public ui::ButtonAction -{ -public: - PropertyWindow * prompt; - OkayAction(PropertyWindow * prompt_) { prompt = prompt_; } - void ActionCallback(ui::Button * sender) + class OkayAction: public ui::ButtonAction { - ui::Engine::Ref().CloseWindow(); - - prompt->SelfDestruct(); - } + public: + PropertyWindow * prompt; + OkayAction(PropertyWindow * prompt_) { prompt = prompt_; } + void ActionCallback(ui::Button * sender) + { + ui::Engine::Ref().CloseWindow(); + if(prompt->textField->GetText().length()) + prompt->SetProperty(); + prompt->SelfDestruct(); + return; + } + }; }; PropertyWindow::PropertyWindow(PropertyTool * tool_, Simulation * sim_, ui::Point position_): @@ -40,6 +45,8 @@ ui::Window(ui::Point(-1, -1), ui::Point(200, 87)), sim(sim_), position(position_) { + properties = Particle::GetProperties(); + ui::Label * messageLabel = new ui::Label(ui::Point(4, 5), ui::Point(Size.X-8, 14), "Edit property"); messageLabel->SetTextColour(style::Colour::InformationTitle); messageLabel->SetAlignment(AlignLeft, AlignTop); @@ -53,9 +60,10 @@ position(position_) property = new ui::DropDown(ui::Point(8, 25), ui::Point(Size.X-16, 17)); AddComponent(property); - property->AddOption(std::pair("Left", (int)sign::Left)); - property->AddOption(std::pair("Centre", (int)sign::Centre)); - property->AddOption(std::pair("Right", (int)sign::Right)); + for(int i = 0; i < properties.size(); i++) + { + property->AddOption(std::pair(properties[i].Name, i)); + } property->SetOption(0); textField = new ui::Textbox(ui::Point(8, 46), ui::Point(Size.X-16, 16), ""); @@ -64,6 +72,80 @@ position(position_) ui::Engine::Ref().ShowWindow(this); } +void PropertyWindow::SetProperty() +{ + if(property->GetOption().second!=-1) + { + void * propValue; + int tempInt; + unsigned int tempUInt; + float tempFloat; + std::string value = textField->GetText(); + try { + switch(properties[property->GetOption().second].Type) + { + case StructProperty::Integer: + case StructProperty::ParticleType: + if(value.length() > 2 && value.substr(0, 2) == "0x") + { + //0xC0FFEE + stringstream buffer; + buffer << std::hex << value.substr(2); + buffer >> tempInt; + } + else if(value.length() > 1 && value[0] == '#') + { + //#C0FFEE + stringstream buffer; + buffer << std::hex << value.substr(1); + buffer >> tempInt; + } + else + { + istringstream(value) >> tempInt; + } + propValue = &tempInt; + break; + case StructProperty::UInteger: + if(value.length() > 2 && value.substr(0, 2) == "0x") + { + //0xC0FFEE + stringstream buffer; + buffer << std::hex << value.substr(2); + buffer >> tempUInt; + } + else if(value.length() > 1 && value[0] == '#') + { + //#C0FFEE + stringstream buffer; + buffer << std::hex << value.substr(1); + buffer >> tempUInt; + } + else + { + istringstream(value) >> tempUInt; + } + propValue = &tempUInt; + break; + case StructProperty::Float: + istringstream(value) >> tempFloat; + propValue = &tempFloat; + break; + default: + new ErrorMessage("Could not set property", "Invalid property"); + } + sim->flood_prop( + position.X, + position.Y, + properties[property->GetOption().second].Offset, + propValue, + properties[property->GetOption().second].Type + ); + } catch (const std::exception& ex) { + new ErrorMessage("Could not set property", "Invalid value provided"); + } + } +} void PropertyWindow::OnDraw() { Graphics * g = ui::Engine::Ref().g; diff --git a/src/game/SignTool.cpp b/src/game/SignTool.cpp index a1db7fa9d..2d7a6e5ca 100644 --- a/src/game/SignTool.cpp +++ b/src/game/SignTool.cpp @@ -20,26 +20,25 @@ public: SignWindow(SignTool * tool_, Simulation * sim_, int signID_, ui::Point position_); virtual void OnDraw(); virtual ~SignWindow() {} -}; - -class OkayAction: public ui::ButtonAction -{ -public: - SignWindow * prompt; - OkayAction(SignWindow * prompt_) { prompt = prompt_; } - void ActionCallback(ui::Button * sender) + class OkayAction: public ui::ButtonAction { - ui::Engine::Ref().CloseWindow(); - if(prompt->signID==-1 && prompt->textField->GetText().length()) + public: + SignWindow * prompt; + OkayAction(SignWindow * prompt_) { prompt = prompt_; } + void ActionCallback(ui::Button * sender) { - prompt->sim->signs.push_back(sign(prompt->textField->GetText(), prompt->signPosition.X, prompt->signPosition.Y, (sign::Justification)prompt->justification->GetOption().second)); + ui::Engine::Ref().CloseWindow(); + if(prompt->signID==-1 && prompt->textField->GetText().length()) + { + prompt->sim->signs.push_back(sign(prompt->textField->GetText(), prompt->signPosition.X, prompt->signPosition.Y, (sign::Justification)prompt->justification->GetOption().second)); + } + else if(prompt->textField->GetText().length()) + { + prompt->sim->signs[prompt->signID] = sign(sign(prompt->textField->GetText(), prompt->signPosition.X, prompt->signPosition.Y, (sign::Justification)prompt->justification->GetOption().second)); + } + prompt->SelfDestruct(); } - else if(prompt->textField->GetText().length()) - { - prompt->sim->signs[prompt->signID] = sign(sign(prompt->textField->GetText(), prompt->signPosition.X, prompt->signPosition.Y, (sign::Justification)prompt->justification->GetOption().second)); - } - prompt->SelfDestruct(); - } + }; }; SignWindow::SignWindow(SignTool * tool_, Simulation * sim_, int signID_, ui::Point position_): diff --git a/src/simulation/Simulation.cpp b/src/simulation/Simulation.cpp index a992ef82c..0032bb29c 100644 --- a/src/simulation/Simulation.cpp +++ b/src/simulation/Simulation.cpp @@ -88,7 +88,7 @@ void Simulation::CreateWallBox(int x1, int y1, int x2, int y2, int c, int flags) CreateWalls(i, j, 0, 0, c, flags); } -int Simulation::flood_prop_2(int x, int y, size_t propoffset, void * propvalue, int proptype, int parttype, char * bitmap) +int Simulation::flood_prop_2(int x, int y, size_t propoffset, void * propvalue, StructProperty::PropertyType proptype, int parttype, char * bitmap) { int x1, x2, i, dy = 1; x1 = x2 = x; @@ -111,12 +111,22 @@ int Simulation::flood_prop_2(int x, int y, size_t propoffset, void * propvalue, for (x=x1; x<=x2; x++) { i = pmap[y][x]>>8; - if(proptype==2){ - *((float*)(((char*)&parts[i])+propoffset)) = *((float*)propvalue); - } else if(proptype==0) { - *((int*)(((char*)&parts[i])+propoffset)) = *((int*)propvalue); - } else if(proptype==1) { - *((char*)(((char*)&parts[i])+propoffset)) = *((char*)propvalue); + switch (proptype) { + case StructProperty::Float: + *((float*)(((char*)&parts[i])+propoffset)) = *((float*)propvalue); + break; + + case StructProperty::ParticleType: + case StructProperty::Integer: + *((int*)(((char*)&parts[i])+propoffset)) = *((int*)propvalue); + break; + + case StructProperty::UInteger: + *((unsigned int*)(((char*)&parts[i])+propoffset)) = *((unsigned int*)propvalue); + break; + + default: + break; } bitmap[(y*XRES)+x] = 1; } @@ -133,7 +143,7 @@ int Simulation::flood_prop_2(int x, int y, size_t propoffset, void * propvalue, return 1; } -int Simulation::flood_prop(int x, int y, size_t propoffset, void * propvalue, int proptype) +int Simulation::flood_prop(int x, int y, size_t propoffset, void * propvalue, StructProperty::PropertyType proptype) { int r = 0; char * bitmap = (char *)malloc(XRES*YRES); //Bitmap for checking diff --git a/src/simulation/Simulation.h b/src/simulation/Simulation.h index 6a7c73338..fe65d4166 100644 --- a/src/simulation/Simulation.h +++ b/src/simulation/Simulation.h @@ -32,6 +32,7 @@ struct StructProperty std::string Name; PropertyType Type; intptr_t Offset; + StructProperty(std::string name, PropertyType type, intptr_t offset): Name(name), Type(type), @@ -54,7 +55,7 @@ struct Particle unsigned int dcolour; /** Returns a list of properties, their type and offset within the structure that can be changed by higher-level processes refering to them by name such as Lua or the property tool **/ - std::vector GetProperties() + static std::vector GetProperties() { std::vector properties; properties.push_back(StructProperty("type", StructProperty::ParticleType, offsetof(Particle, type))); @@ -269,8 +270,8 @@ public: void create_cherenkov_photon(int pp); void create_gain_photon(int pp); inline void kill_part(int i); - int flood_prop(int x, int y, size_t propoffset, void * propvalue, int proptype); - int flood_prop_2(int x, int y, size_t propoffset, void * propvalue, int proptype, int parttype, char * bitmap); + int flood_prop(int x, int y, size_t propoffset, void * propvalue, StructProperty::PropertyType proptype); + int flood_prop_2(int x, int y, size_t propoffset, void * propvalue, StructProperty::PropertyType proptype, int parttype, char * bitmap); int flood_water(int x, int y, int i, int originaly, int check); inline void detach(int i); inline void part_change_type(int i, int x, int y, int t); diff --git a/src/simulation/elements/BANG.cpp b/src/simulation/elements/BANG.cpp index ac538caaf..ea22b058d 100644 --- a/src/simulation/elements/BANG.cpp +++ b/src/simulation/elements/BANG.cpp @@ -76,7 +76,7 @@ int Element_BANG::update(UPDATE_FUNC_ARGS) else if(parts[i].tmp==1) { int tempvalue = 2; - sim->flood_prop(x, y, offsetof(Particle, tmp), &tempvalue, 0); + sim->flood_prop(x, y, offsetof(Particle, tmp), &tempvalue, StructProperty::Integer); } else if(parts[i].tmp==2) {