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

View File

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