Prevent property tool from being used with bad values (#791)

Co-authored-by: Tamás Bálint Misius <lbphacker@gmail.com>
This commit is contained in:
catsoften 2021-07-29 11:20:13 -04:00 committed by GitHub
parent e3c9176277
commit f13fe3d36b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 8 deletions

View File

@ -53,7 +53,7 @@ public:
Simulation *sim; Simulation *sim;
std::vector<StructProperty> properties; std::vector<StructProperty> properties;
PropertyWindow(PropertyTool *tool_, Simulation *sim); PropertyWindow(PropertyTool *tool_, Simulation *sim);
void SetProperty(); void SetProperty(bool warn);
void OnDraw() override; void OnDraw() override;
void OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt) override; void OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt) override;
void OnTryExit(ExitMethod method) override; void OnTryExit(ExitMethod method) override;
@ -81,7 +81,7 @@ sim(sim_)
if (textField->GetText().length()) if (textField->GetText().length())
{ {
CloseActiveWindow(); CloseActiveWindow();
SetProperty(); SetProperty(true);
SelfDestruct(); SelfDestruct();
} }
} }); } });
@ -103,14 +103,17 @@ sim(sim_)
textField->SetText(Client::Ref().GetPrefString("Prop.Value", "")); textField->SetText(Client::Ref().GetPrefString("Prop.Value", ""));
AddComponent(textField); AddComponent(textField);
FocusComponent(textField); FocusComponent(textField);
SetProperty(false);
MakeActiveWindow(); MakeActiveWindow();
} }
void PropertyWindow::SetProperty() void PropertyWindow::SetProperty(bool warn)
{ {
tool->validProperty = false;
if(property->GetOption().second!=-1 && textField->GetText().length() > 0) if(property->GetOption().second!=-1 && textField->GetText().length() > 0)
{ {
tool->validProperty = true;
String value = textField->GetText().ToUpper(); String value = textField->GetText().ToUpper();
try { try {
switch(properties[property->GetOption().second].Type) switch(properties[property->GetOption().second].Type)
@ -174,6 +177,8 @@ void PropertyWindow::SetProperty()
if (properties[property->GetOption().second].Name == "type" && (v < 0 || v >= PT_NUM || !sim->elements[v].Enabled)) if (properties[property->GetOption().second].Name == "type" && (v < 0 || v >= PT_NUM || !sim->elements[v].Enabled))
{ {
tool->validProperty = false;
if (warn)
new ErrorMessage("Could not set property", "Invalid particle type"); new ErrorMessage("Could not set property", "Invalid particle type");
return; return;
} }
@ -214,6 +219,8 @@ void PropertyWindow::SetProperty()
} }
break; break;
default: default:
tool->validProperty = false;
if (warn)
new ErrorMessage("Could not set property", "Invalid property"); new ErrorMessage("Could not set property", "Invalid property");
return; return;
} }
@ -221,6 +228,8 @@ void PropertyWindow::SetProperty()
tool->propType = properties[property->GetOption().second].Type; tool->propType = properties[property->GetOption().second].Type;
tool->changeType = properties[property->GetOption().second].Name == "type"; tool->changeType = properties[property->GetOption().second].Name == "type";
} catch (const std::exception& ex) { } catch (const std::exception& ex) {
tool->validProperty = false;
if (warn)
new ErrorMessage("Could not set property", "Invalid value provided"); new ErrorMessage("Could not set property", "Invalid value provided");
return; return;
} }
@ -258,7 +267,7 @@ void PropertyTool::OpenWindow(Simulation *sim)
void PropertyTool::SetProperty(Simulation *sim, ui::Point position) void PropertyTool::SetProperty(Simulation *sim, ui::Point position)
{ {
if(position.X<0 || position.X>XRES || position.Y<0 || position.Y>YRES) if(position.X<0 || position.X>XRES || position.Y<0 || position.Y>YRES || !validProperty)
return; return;
int i = sim->pmap[position.Y][position.X]; int i = sim->pmap[position.Y][position.X];
if(!i) if(!i)
@ -379,5 +388,6 @@ void PropertyTool::DrawRect(Simulation *sim, Brush *cBrush, ui::Point position,
void PropertyTool::DrawFill(Simulation *sim, Brush *cBrush, ui::Point position) void PropertyTool::DrawFill(Simulation *sim, Brush *cBrush, ui::Point position)
{ {
if (validProperty)
sim->flood_prop(position.X, position.Y, propOffset, propValue, propType); sim->flood_prop(position.X, position.Y, propOffset, propValue, propType);
} }

View File

@ -92,6 +92,7 @@ public:
PropertyValue propValue; PropertyValue propValue;
bool changeType; bool changeType;
size_t propOffset; size_t propOffset;
bool validProperty;
void OpenWindow(Simulation *sim); void OpenWindow(Simulation *sim);
virtual ~PropertyTool() {} virtual ~PropertyTool() {}