brand new prop tool. draws like any other tool

This commit is contained in:
mniip 2013-11-13 08:00:21 +04:00 committed by jacob1
parent a32a66c9ab
commit 8f2fbcd9e1
3 changed files with 161 additions and 31 deletions

View File

@ -1032,10 +1032,10 @@ void GameController::SetActiveTool(int toolSelection, Tool * tool)
for(int i = 0; i < 3; i++) for(int i = 0; i < 3; i++)
{ {
if(gameModel->GetActiveTool(i) == gameModel->GetMenuList().at(SC_WALL)->GetToolList().at(WL_GRAV)) if(gameModel->GetActiveTool(i) == gameModel->GetMenuList().at(SC_WALL)->GetToolList().at(WL_GRAV))
{
gameModel->GetRenderer()->gravityZonesEnabled = true; gameModel->GetRenderer()->gravityZonesEnabled = true;
}
} }
if(tool->GetIdentifier() == "DEFAULT_UI_PROPERTY")
((PropertyTool *)tool)->OpenWindow(gameModel->GetSimulation());
} }
int GameController::GetReplaceModeFlags() int GameController::GetReplaceModeFlags()

View File

@ -1,6 +1,7 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include "gui/Style.h" #include "gui/Style.h"
#include "gui/game/Brush.h"
#include "simulation/Simulation.h" #include "simulation/Simulation.h"
#include "Tool.h" #include "Tool.h"
#include "gui/interface/Window.h" #include "gui/interface/Window.h"
@ -16,12 +17,10 @@ class PropertyWindow: public ui::Window
public: public:
ui::DropDown * property; ui::DropDown * property;
ui::Textbox * textField; ui::Textbox * textField;
SignTool * tool; PropertyTool * tool;
Simulation * sim; Simulation *sim;
int signID;
ui::Point position;
std::vector<StructProperty> properties; std::vector<StructProperty> properties;
PropertyWindow(PropertyTool * tool_, Simulation * sim_, ui::Point position_); PropertyWindow(PropertyTool *tool_, Simulation *sim);
void SetProperty(); void SetProperty();
virtual void OnDraw(); virtual void OnDraw();
virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt); virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
@ -43,10 +42,10 @@ public:
}; };
}; };
PropertyWindow::PropertyWindow(PropertyTool * tool_, Simulation * sim_, ui::Point position_): PropertyWindow::PropertyWindow(PropertyTool * tool_, Simulation *sim_):
ui::Window(ui::Point(-1, -1), ui::Point(200, 87)), ui::Window(ui::Point(-1, -1), ui::Point(200, 87)),
sim(sim_), tool(tool_),
position(position_) sim(sim_)
{ {
properties = Particle::GetProperties(); properties = Particle::GetProperties();
@ -81,21 +80,28 @@ position(position_)
{ {
property->AddOption(std::pair<std::string, int>(properties[i].Name, i)); property->AddOption(std::pair<std::string, int>(properties[i].Name, i));
} }
property->SetOption(0); property->SetOption(Client::Ref().GetPrefInteger("Prop.Type", 0));
textField = new ui::Textbox(ui::Point(8, 46), ui::Point(Size.X-16, 16), "", "[value]"); textField = new ui::Textbox(ui::Point(8, 46), ui::Point(Size.X-16, 16), "", "[value]");
textField->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; textField->Appearance.HorizontalAlign = ui::Appearance::AlignLeft;
textField->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; textField->Appearance.VerticalAlign = ui::Appearance::AlignMiddle;
textField->SetText(Client::Ref().GetPrefString("Prop.Value", ""));
AddComponent(textField); AddComponent(textField);
FocusComponent(textField); FocusComponent(textField);
ui::Engine::Ref().ShowWindow(this); ui::Engine::Ref().ShowWindow(this);
} }
void PropertyWindow::SetProperty() void PropertyWindow::SetProperty()
{ {
if(property->GetOption().second!=-1 && textField->GetText().length() > 0) if(property->GetOption().second!=-1 && textField->GetText().length() > 0)
{ {
void * propValue; Client::Ref().SetPref("Prop.Type", property->GetOption().second);
Client::Ref().SetPref("Prop.Value", textField->GetText());
if(tool->propValue)
free(tool->propValue);
tool->propValue=malloc(sizeof(int));
int tempInt; int tempInt;
unsigned int tempUInt; unsigned int tempUInt;
float tempFloat; float tempFloat;
@ -155,7 +161,7 @@ void PropertyWindow::SetProperty()
#ifdef DEBUG #ifdef DEBUG
std::cout << "Got int value " << tempInt << std::endl; std::cout << "Got int value " << tempInt << std::endl;
#endif #endif
propValue = &tempInt; *(int *)tool->propValue = tempInt;
break; break;
case StructProperty::UInteger: case StructProperty::UInteger:
if(value.length() > 2 && value.substr(0, 2) == "0x") if(value.length() > 2 && value.substr(0, 2) == "0x")
@ -183,7 +189,7 @@ void PropertyWindow::SetProperty()
#ifdef DEBUG #ifdef DEBUG
std::cout << "Got uint value " << tempUInt << std::endl; std::cout << "Got uint value " << tempUInt << std::endl;
#endif #endif
propValue = &tempUInt; *(unsigned int *)tool->propValue = tempUInt;
break; break;
case StructProperty::Float: case StructProperty::Float:
{ {
@ -193,20 +199,15 @@ void PropertyWindow::SetProperty()
#ifdef DEBUG #ifdef DEBUG
std::cout << "Got float value " << tempFloat << std::endl; std::cout << "Got float value " << tempFloat << std::endl;
#endif #endif
propValue = &tempFloat; *(float *)tool->propValue = tempFloat;
} }
break; break;
default: default:
new ErrorMessage("Could not set property", "Invalid property"); new ErrorMessage("Could not set property", "Invalid property");
return; return;
} }
sim->flood_prop( tool->propOffset = properties[property->GetOption().second].Offset;
position.X, tool->propType = properties[property->GetOption().second].Type;
position.Y,
properties[property->GetOption().second].Offset,
propValue,
properties[property->GetOption().second].Type
);
} catch (const std::exception& ex) { } catch (const std::exception& ex) {
new ErrorMessage("Could not set property", "Invalid value provided"); new ErrorMessage("Could not set property", "Invalid value provided");
} }
@ -235,7 +236,128 @@ void PropertyWindow::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl
property->SetOption(property->GetOption().second+1); property->SetOption(property->GetOption().second+1);
} }
void PropertyTool::Click(Simulation * sim, Brush * brush, ui::Point position) void PropertyTool::OpenWindow(Simulation *sim)
{ {
new PropertyWindow(this, sim, position); new PropertyWindow(this, sim);
}
void PropertyTool::SetProperty(Simulation *sim, ui::Point position)
{
if(!propValue)
return;
if(position.X<0 || position.X>XRES || position.Y<0 || position.Y>YRES)
return;
int i = sim->pmap[position.Y][position.X];
if(!i)
i = sim->photons[position.Y][position.X];
if(!i)
return;
switch (propType)
{
case StructProperty::Float:
*((float*)(((char*)&sim->parts[i>>8])+propOffset)) = *((float*)propValue);
break;
case StructProperty::ParticleType:
case StructProperty::Integer:
*((int*)(((char*)&sim->parts[i>>8])+propOffset)) = *((int*)propValue);
break;
case StructProperty::UInteger:
*((unsigned int*)(((char*)&sim->parts[i>>8])+propOffset)) = *((unsigned int*)propValue);
break;
default:
break;
}
}
void PropertyTool::Draw(Simulation *sim, Brush *cBrush, ui::Point position)
{
if(cBrush)
{
int radiusX = cBrush->GetRadius().X, radiusY = cBrush->GetRadius().Y, sizeX = cBrush->GetSize().X, sizeY = cBrush->GetSize().Y;
unsigned char *bitmap = cBrush->GetBitmap();
for(int y = 0; y < sizeY; y++)
for(int x = 0; x < sizeX; x++)
if(bitmap[(y*sizeX)+x] && (position.X+(x-radiusX) >= 0 && position.Y+(y-radiusY) >= 0 && position.X+(x-radiusX) < XRES && position.Y+(y-radiusY) < YRES))
SetProperty(sim, ui::Point(position.X+(x-radiusX), position.Y+(y-radiusY)));
}
}
void PropertyTool::DrawLine(Simulation *sim, Brush *cBrush, ui::Point position, ui::Point position2, bool dragging)
{
int x1 = position.X, y1 = position.Y, x2 = position2.X, y2 = position2.Y;
bool reverseXY = abs(y2-y1) > abs(x2-x1);
int x, y, dx, dy, sy, rx = cBrush->GetRadius().X, ry = cBrush->GetRadius().Y;
float e = 0.0f, de;
if (reverseXY)
{
y = x1;
x1 = y1;
y1 = y;
y = x2;
x2 = y2;
y2 = y;
}
if (x1 > x2)
{
y = x1;
x1 = x2;
x2 = y;
y = y1;
y1 = y2;
y2 = y;
}
dx = x2 - x1;
dy = abs(y2 - y1);
if (dx)
de = dy/(float)dx;
else
de = 0.0f;
y = y1;
sy = (y1<y2) ? 1 : -1;
for (x=x1; x<=x2; x++)
{
if (reverseXY)
Draw(sim, cBrush, ui::Point(y, x));
else
Draw(sim, cBrush, ui::Point(x, y));
e += de;
if (e >= 0.5f)
{
y += sy;
if (!(rx+ry) && ((y1<y2) ? (y<=y2) : (y>=y2)))
{
if (reverseXY)
Draw(sim, cBrush, ui::Point(y, x));
else
Draw(sim, cBrush, ui::Point(x, y));
}
e -= 1.0f;
}
}
}
void PropertyTool::DrawRect(Simulation *sim, Brush *cBrush, ui::Point position, ui::Point position2)
{
int x1 = position.X, y1 = position.Y, x2 = position2.X, y2 = position2.Y;
int i, j;
if (x1>x2)
{
i = x2;
x2 = x1;
x1 = i;
}
if (y1>y2)
{
j = y2;
y2 = y1;
y1 = j;
}
for (j=y1; j<=y2; j++)
for (i=x1; i<=x2; i++)
SetProperty(sim, ui::Point(i, j));
}
void PropertyTool::DrawFill(Simulation *sim, Brush *cBrush, ui::Point position)
{
sim->flood_prop(position.X, position.Y, propOffset, propValue, propType);
} }

View File

@ -6,6 +6,7 @@
using namespace std; using namespace std;
#include "gui/interface/Point.h" #include "gui/interface/Point.h"
#include "simulation/StructProperty.h"
class Simulation; class Simulation;
class Brush; class Brush;
@ -63,7 +64,7 @@ class SampleTool: public Tool
{ {
GameModel * gameModel; GameModel * gameModel;
public: public:
SampleTool(GameModel * model): SampleTool(GameModel *model):
Tool(0, "SMPL", "Sample an element on the screen.", 0, 0, 0, "DEFAULT_UI_SAMPLE", SampleTool::GetIcon), Tool(0, "SMPL", "Sample an element on the screen.", 0, 0, 0, "DEFAULT_UI_SAMPLE", SampleTool::GetIcon),
gameModel(model) gameModel(model)
{ {
@ -81,15 +82,22 @@ class PropertyTool: public Tool
{ {
public: public:
PropertyTool(): PropertyTool():
Tool(0, "PROP", "Property Edit. Click to alter the properties of elements in the field.", 0xfe, 0xa9, 0x00, "DEFAULT_UI_PROPERTY", NULL) Tool(0, "PROP", "Property Edit. Click to alter the properties of elements in the field.", 0xfe, 0xa9, 0x00, "DEFAULT_UI_PROPERTY", NULL),
propValue(NULL)
{ {
} }
StructProperty::PropertyType propType;
void *propValue;
size_t propOffset;
void OpenWindow(Simulation *sim);
virtual ~PropertyTool() {} virtual ~PropertyTool() {}
virtual void Click(Simulation * sim, Brush * brush, ui::Point position); virtual void SetProperty(Simulation *sim, ui::Point position);
virtual void Draw(Simulation * sim, Brush * brush, ui::Point position) {} virtual void Click(Simulation * sim, Brush * brush, ui::Point position) { }
virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging = false) { } virtual void Draw(Simulation *sim, Brush *brush, ui::Point position);
virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) { } virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging = false);
virtual void DrawFill(Simulation * sim, Brush * brush, ui::Point position) { } virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2);
virtual void DrawFill(Simulation * sim, Brush * brush, ui::Point position);
}; };
class Element_LIGH_Tool: public Tool class Element_LIGH_Tool: public Tool