mirror of
https://github.com/The-Powder-Toy/The-Powder-Toy.git
synced 2025-09-02 20:42:36 +02:00
Make flood_prop use PropertyType from StructProperty, Property tool works
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
#include "Style.h"
|
#include "Style.h"
|
||||||
#include "simulation/Simulation.h"
|
#include "simulation/Simulation.h"
|
||||||
#include "Tool.h"
|
#include "Tool.h"
|
||||||
@@ -7,6 +8,7 @@
|
|||||||
#include "interface/Label.h"
|
#include "interface/Label.h"
|
||||||
#include "interface/Textbox.h"
|
#include "interface/Textbox.h"
|
||||||
#include "interface/DropDown.h"
|
#include "interface/DropDown.h"
|
||||||
|
#include "dialogues/ErrorMessage.h"
|
||||||
|
|
||||||
class PropertyWindow: public ui::Window
|
class PropertyWindow: public ui::Window
|
||||||
{
|
{
|
||||||
@@ -17,22 +19,25 @@ public:
|
|||||||
Simulation * sim;
|
Simulation * sim;
|
||||||
int signID;
|
int signID;
|
||||||
ui::Point position;
|
ui::Point position;
|
||||||
|
std::vector<StructProperty> properties;
|
||||||
PropertyWindow(PropertyTool * tool_, Simulation * sim_, ui::Point position_);
|
PropertyWindow(PropertyTool * tool_, Simulation * sim_, ui::Point position_);
|
||||||
|
void SetProperty();
|
||||||
virtual void OnDraw();
|
virtual void OnDraw();
|
||||||
virtual ~PropertyWindow() {}
|
virtual ~PropertyWindow() {}
|
||||||
};
|
class OkayAction: public ui::ButtonAction
|
||||||
|
|
||||||
class OkayAction: public ui::ButtonAction
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
PropertyWindow * prompt;
|
|
||||||
OkayAction(PropertyWindow * prompt_) { prompt = prompt_; }
|
|
||||||
void ActionCallback(ui::Button * sender)
|
|
||||||
{
|
{
|
||||||
ui::Engine::Ref().CloseWindow();
|
public:
|
||||||
|
PropertyWindow * prompt;
|
||||||
prompt->SelfDestruct();
|
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_):
|
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_),
|
sim(sim_),
|
||||||
position(position_)
|
position(position_)
|
||||||
{
|
{
|
||||||
|
properties = Particle::GetProperties();
|
||||||
|
|
||||||
ui::Label * messageLabel = new ui::Label(ui::Point(4, 5), ui::Point(Size.X-8, 14), "Edit property");
|
ui::Label * messageLabel = new ui::Label(ui::Point(4, 5), ui::Point(Size.X-8, 14), "Edit property");
|
||||||
messageLabel->SetTextColour(style::Colour::InformationTitle);
|
messageLabel->SetTextColour(style::Colour::InformationTitle);
|
||||||
messageLabel->SetAlignment(AlignLeft, AlignTop);
|
messageLabel->SetAlignment(AlignLeft, AlignTop);
|
||||||
@@ -53,9 +60,10 @@ position(position_)
|
|||||||
|
|
||||||
property = new ui::DropDown(ui::Point(8, 25), ui::Point(Size.X-16, 17));
|
property = new ui::DropDown(ui::Point(8, 25), ui::Point(Size.X-16, 17));
|
||||||
AddComponent(property);
|
AddComponent(property);
|
||||||
property->AddOption(std::pair<std::string, int>("Left", (int)sign::Left));
|
for(int i = 0; i < properties.size(); i++)
|
||||||
property->AddOption(std::pair<std::string, int>("Centre", (int)sign::Centre));
|
{
|
||||||
property->AddOption(std::pair<std::string, int>("Right", (int)sign::Right));
|
property->AddOption(std::pair<std::string, int>(properties[i].Name, i));
|
||||||
|
}
|
||||||
property->SetOption(0);
|
property->SetOption(0);
|
||||||
|
|
||||||
textField = new ui::Textbox(ui::Point(8, 46), ui::Point(Size.X-16, 16), "");
|
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);
|
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()
|
void PropertyWindow::OnDraw()
|
||||||
{
|
{
|
||||||
Graphics * g = ui::Engine::Ref().g;
|
Graphics * g = ui::Engine::Ref().g;
|
||||||
|
@@ -20,26 +20,25 @@ public:
|
|||||||
SignWindow(SignTool * tool_, Simulation * sim_, int signID_, ui::Point position_);
|
SignWindow(SignTool * tool_, Simulation * sim_, int signID_, ui::Point position_);
|
||||||
virtual void OnDraw();
|
virtual void OnDraw();
|
||||||
virtual ~SignWindow() {}
|
virtual ~SignWindow() {}
|
||||||
};
|
class OkayAction: public ui::ButtonAction
|
||||||
|
|
||||||
class OkayAction: public ui::ButtonAction
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
SignWindow * prompt;
|
|
||||||
OkayAction(SignWindow * prompt_) { prompt = prompt_; }
|
|
||||||
void ActionCallback(ui::Button * sender)
|
|
||||||
{
|
{
|
||||||
ui::Engine::Ref().CloseWindow();
|
public:
|
||||||
if(prompt->signID==-1 && prompt->textField->GetText().length())
|
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_):
|
SignWindow::SignWindow(SignTool * tool_, Simulation * sim_, int signID_, ui::Point position_):
|
||||||
|
@@ -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);
|
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;
|
int x1, x2, i, dy = 1;
|
||||||
x1 = x2 = x;
|
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++)
|
for (x=x1; x<=x2; x++)
|
||||||
{
|
{
|
||||||
i = pmap[y][x]>>8;
|
i = pmap[y][x]>>8;
|
||||||
if(proptype==2){
|
switch (proptype) {
|
||||||
*((float*)(((char*)&parts[i])+propoffset)) = *((float*)propvalue);
|
case StructProperty::Float:
|
||||||
} else if(proptype==0) {
|
*((float*)(((char*)&parts[i])+propoffset)) = *((float*)propvalue);
|
||||||
*((int*)(((char*)&parts[i])+propoffset)) = *((int*)propvalue);
|
break;
|
||||||
} else if(proptype==1) {
|
|
||||||
*((char*)(((char*)&parts[i])+propoffset)) = *((char*)propvalue);
|
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;
|
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;
|
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;
|
int r = 0;
|
||||||
char * bitmap = (char *)malloc(XRES*YRES); //Bitmap for checking
|
char * bitmap = (char *)malloc(XRES*YRES); //Bitmap for checking
|
||||||
|
@@ -32,6 +32,7 @@ struct StructProperty
|
|||||||
std::string Name;
|
std::string Name;
|
||||||
PropertyType Type;
|
PropertyType Type;
|
||||||
intptr_t Offset;
|
intptr_t Offset;
|
||||||
|
|
||||||
StructProperty(std::string name, PropertyType type, intptr_t offset):
|
StructProperty(std::string name, PropertyType type, intptr_t offset):
|
||||||
Name(name),
|
Name(name),
|
||||||
Type(type),
|
Type(type),
|
||||||
@@ -54,7 +55,7 @@ struct Particle
|
|||||||
unsigned int dcolour;
|
unsigned int dcolour;
|
||||||
/** Returns a list of properties, their type and offset within the structure that can be changed
|
/** 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 **/
|
by higher-level processes refering to them by name such as Lua or the property tool **/
|
||||||
std::vector<StructProperty> GetProperties()
|
static std::vector<StructProperty> GetProperties()
|
||||||
{
|
{
|
||||||
std::vector<StructProperty> properties;
|
std::vector<StructProperty> properties;
|
||||||
properties.push_back(StructProperty("type", StructProperty::ParticleType, offsetof(Particle, type)));
|
properties.push_back(StructProperty("type", StructProperty::ParticleType, offsetof(Particle, type)));
|
||||||
@@ -269,8 +270,8 @@ public:
|
|||||||
void create_cherenkov_photon(int pp);
|
void create_cherenkov_photon(int pp);
|
||||||
void create_gain_photon(int pp);
|
void create_gain_photon(int pp);
|
||||||
inline void kill_part(int i);
|
inline void kill_part(int i);
|
||||||
int flood_prop(int x, int y, size_t propoffset, void * propvalue, int proptype);
|
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, int proptype, int parttype, char * bitmap);
|
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);
|
int flood_water(int x, int y, int i, int originaly, int check);
|
||||||
inline void detach(int i);
|
inline void detach(int i);
|
||||||
inline void part_change_type(int i, int x, int y, int t);
|
inline void part_change_type(int i, int x, int y, int t);
|
||||||
|
@@ -76,7 +76,7 @@ int Element_BANG::update(UPDATE_FUNC_ARGS)
|
|||||||
else if(parts[i].tmp==1)
|
else if(parts[i].tmp==1)
|
||||||
{
|
{
|
||||||
int tempvalue = 2;
|
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)
|
else if(parts[i].tmp==2)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user