Line and rect drawing

This commit is contained in:
Simon Robertshaw 2012-01-30 16:03:18 +00:00
parent 259fc2bcf7
commit 1d297cb57a
13 changed files with 347 additions and 58 deletions

View File

@ -1761,7 +1761,8 @@ Renderer::Renderer(Graphics * g, Simulation * sim):
zoomWindowPosition(0, 0),
zoomScopePosition(0, 0),
zoomScopeSize(10),
ZFACTOR(8)
ZFACTOR(8),
zoomEnabled(false)
{
this->g = g;
this->sim = sim;

View File

@ -35,8 +35,29 @@ public:
if(bitmap)
delete bitmap;
}
virtual void RenderRect(Graphics * g, ui::Point position1, ui::Point position2)
{
int width, height, t;
width = position2.X-position1.X;
height = position2.Y-position1.Y;
if(height<0)
{
position1.Y += height;
height *= -1;
}
if(width<0)
{
position1.X += width;
width *= -1;
}
g->fillrect(position1.X-1, position1.Y-1, width+2, height+2, 255, 0, 255, 70);
}
virtual void RenderLine(Graphics * g, ui::Point position1, ui::Point position2)
{
g->blend_line(position1.X, position1.Y, position2.X, position2.Y, 255, 0, 255, 70);
}
//Draw the brush outline onto the screen
virtual void Render(Graphics * g, ui::Point position)
virtual void RenderPoint(Graphics * g, ui::Point position)
{
g->fillrect(position.X-size.X-1, position.Y-size.Y-1, (size.X*2)+2, (size.Y*2)+2, 255, 0, 255, 70);
}

View File

@ -19,7 +19,7 @@ public:
};
//Draw the brush outline onto the screen
virtual void Render(Graphics * g, ui::Point position)
virtual void RenderPoint(Graphics * g, ui::Point position)
{
if(!bitmap)
GenerateBitmap();

View File

@ -147,10 +147,35 @@ ui::Point GameController::PointTranslate(ui::Point point)
return point;
}
void GameController::DrawPoints(queue<ui::Point*> & pointQueue)
void GameController::DrawRect(int toolSelection, ui::Point point1, ui::Point point2)
{
Simulation * sim = gameModel->GetSimulation();
Tool * activeTool = gameModel->GetActiveTool();
Tool * activeTool = gameModel->GetActiveTool(toolSelection);
Brush * cBrush = gameModel->GetBrush();
if(!activeTool || !cBrush)
return;
activeTool->DrawRect(sim, cBrush, point1, point2);
}
void GameController::DrawLine(int toolSelection, ui::Point point1, ui::Point point2)
{
Simulation * sim = gameModel->GetSimulation();
Tool * activeTool = gameModel->GetActiveTool(toolSelection);
Brush * cBrush = gameModel->GetBrush();
if(!activeTool || !cBrush)
return;
activeTool->DrawLine(sim, cBrush, point1, point2);
}
void GameController::DrawFill(int toolSelection, ui::Point point)
{
}
void GameController::DrawPoints(int toolSelection, queue<ui::Point*> & pointQueue)
{
Simulation * sim = gameModel->GetSimulation();
Tool * activeTool = gameModel->GetActiveTool(toolSelection);
Brush * cBrush = gameModel->GetBrush();
if(!activeTool || !cBrush)
{
@ -249,9 +274,9 @@ void GameController::SetActiveMenu(Menu * menu)
gameModel->SetActiveMenu(menu);
}
void GameController::SetActiveTool(Tool * tool)
void GameController::SetActiveTool(int toolSelection, Tool * tool)
{
gameModel->SetActiveTool(tool);
gameModel->SetActiveTool(toolSelection, tool);
}
void GameController::OpenSearch()

View File

@ -38,12 +38,15 @@ public:
void SetZoomPosition(ui::Point position);
void AdjustBrushSize(int direction);
void AdjustZoomSize(int direction);
void DrawPoints(queue<ui::Point*> & pointQueue);
void DrawPoints(int toolSelection, queue<ui::Point*> & pointQueue);
void DrawRect(int toolSelection, ui::Point point1, ui::Point point2);
void DrawLine(int toolSelection, ui::Point point1, ui::Point point2);
void DrawFill(int toolSelection, ui::Point point);
void Update();
void SetPaused(bool pauseState);
void SetPaused();
void SetActiveMenu(Menu * menu);
void SetActiveTool(Tool * tool);
void SetActiveTool(int toolSelection, Tool * tool);
void OpenSearch();
void OpenLogin();
void OpenTags();

View File

@ -9,7 +9,7 @@
#include "client/Client.h"
GameModel::GameModel():
activeTool(NULL),
activeTools({NULL, NULL, NULL}),
sim(NULL),
ren(NULL),
currentBrush(0),
@ -44,7 +44,9 @@ GameModel::GameModel():
brushList.push_back(new Brush(ui::Point(4, 4)));
brushList.push_back(new EllipseBrush(ui::Point(4, 4)));
activeTool = new ElementTool(1, "TURD", 0, 0, 0);
activeTools[0] = new ElementTool(1, "TURD", 0, 0, 0);
activeTools[1] = new ElementTool(0, "TURD", 0, 0, 0);
//activeTool[1] = new ElementTool(0, "TURD", 0, 0, 0);
}
GameModel::~GameModel()
@ -63,8 +65,8 @@ GameModel::~GameModel()
}
delete sim;
delete ren;
if(activeTool)
delete activeTool;
if(activeTools)
delete activeTools;
}
void GameModel::SetVote(int direction)
@ -131,15 +133,15 @@ Menu * GameModel::GetActiveMenu()
return activeMenu;
}
Tool * GameModel::GetActiveTool()
Tool * GameModel::GetActiveTool(int selection)
{
return activeTool;
return activeTools[selection];
}
void GameModel::SetActiveTool(Tool * tool)
void GameModel::SetActiveTool(int selection, Tool * tool)
{
activeTool = tool;
notifyActiveToolChanged();
activeTools[selection] = tool;
notifyActiveToolsChanged();
}
vector<Menu*> GameModel::GetMenuList()
@ -162,6 +164,7 @@ void GameModel::SetSave(Save * newSave)
sim->Load(currentSave->GetData(), currentSave->GetDataLength());
}
notifySaveChanged();
notifyPausedChanged();
}
Simulation * GameModel::GetSimulation()
@ -313,11 +316,11 @@ void GameModel::notifyToolListChanged()
}
}
void GameModel::notifyActiveToolChanged()
void GameModel::notifyActiveToolsChanged()
{
for(int i = 0; i < observers.size(); i++)
{
observers[i]->NotifyActiveToolChanged(this);
observers[i]->NotifyActiveToolsChanged(this);
}
}

View File

@ -18,6 +18,15 @@ class GameView;
class Simulation;
class Renderer;
class ToolSelection
{
public:
enum
{
ToolPrimary, ToolSecondary, ToolTertiary
};
};
class GameModel
{
private:
@ -30,7 +39,7 @@ private:
Save * currentSave;
Simulation * sim;
Renderer * ren;
Tool * activeTool;
Tool * activeTools[3];
User currentUser;
//bool zoomEnabled;
void notifyRendererChanged();
@ -40,7 +49,7 @@ private:
void notifyBrushChanged();
void notifyMenuListChanged();
void notifyToolListChanged();
void notifyActiveToolChanged();
void notifyActiveToolsChanged();
void notifyUserChanged();
void notifyZoomChanged();
public:
@ -52,8 +61,8 @@ public:
Brush * GetBrush();
void SetSave(Save * newSave);
void AddObserver(GameView * observer);
Tool * GetActiveTool();
void SetActiveTool(Tool * tool);
Tool * GetActiveTool(int selection);
void SetActiveTool(int selection, Tool * tool);
bool GetPaused();
void SetPaused(bool pauseState);
void ClearSimulation();

View File

@ -11,7 +11,14 @@ GameView::GameView():
isMouseDown(false),
ren(NULL),
activeBrush(NULL),
currentMouse(0, 0)
currentMouse(0, 0),
toolIndex(0),
zoomEnabled(false),
zoomCursorFixed(false),
drawPoint1(0, 0),
drawPoint2(0, 0),
drawMode(DrawPoints),
drawModeReset(false)
{
int currentX = 1;
//Set up UI
@ -207,9 +214,11 @@ class GameView::ToolAction: public ui::ButtonAction
public:
Tool * tool;
ToolAction(GameView * _v, Tool * tool_) { v = _v; tool = tool_; }
void ActionCallback(ui::Button * sender)
void ActionCallback(ui::Button * sender_)
{
v->c->SetActiveTool(tool);
ToolButton *sender = (ToolButton*)sender_;
if(sender->GetSelectionState() >= 0 && sender->GetSelectionState() <= 2)
v->c->SetActiveTool(sender->GetSelectionState(), tool);
}
};
@ -242,17 +251,26 @@ void GameView::NotifyMenuListChanged(GameModel * sender)
}
}
void GameView::NotifyActiveToolChanged(GameModel * sender)
void GameView::NotifyActiveToolsChanged(GameModel * sender)
{
for(int i = 0; i < toolButtons.size(); i++)
{
if(((ToolAction*)toolButtons[i]->GetActionCallback())->tool==sender->GetActiveTool())
Tool * tool = ((ToolAction*)toolButtons[i]->GetActionCallback())->tool;
if(sender->GetActiveTool(0) == tool)
{
toolButtons[i]->SetToggleState(true);
toolButtons[i]->SetSelectionState(0); //Primary
}
else if(sender->GetActiveTool(1) == tool)
{
toolButtons[i]->SetSelectionState(1); //Secondary
}
else if(sender->GetActiveTool(2) == tool)
{
toolButtons[i]->SetSelectionState(2); //Tertiary
}
else
{
toolButtons[i]->SetToggleState(false);
toolButtons[i]->SetSelectionState(-1);
}
}
}
@ -281,25 +299,24 @@ void GameView::NotifyToolListChanged(GameModel * sender)
vector<Tool*> toolList = sender->GetToolList();
for(int i = 0; i < toolList.size(); i++)
{
ui::Button * tempButton = new ui::Button(ui::Point(currentX, YRES), ui::Point(32, 16), toolList[i]->GetName());
ToolButton * tempButton = new ToolButton(ui::Point(currentX, YRES), ui::Point(32, 16), toolList[i]->GetName());
currentX -= 36;
tempButton->SetTogglable(true);
tempButton->SetActionCallback(new ToolAction(this, toolList[i]));
totalColour = toolList[i]->colRed + 3*toolList[i]->colGreen + 2*toolList[i]->colBlue;
tempButton->SetBackgroundColour(ui::Colour(toolList[i]->colRed, toolList[i]->colGreen, toolList[i]->colBlue));
if (totalColour<544)
if(sender->GetActiveTool(0) == toolList[i])
{
tempButton->SetTextColour(ui::Colour(255, 255, 255));
tempButton->SetSelectionState(0); //Primary
}
else
else if(sender->GetActiveTool(1) == toolList[i])
{
tempButton->SetTextColour(ui::Colour(0, 0, 0));
tempButton->SetSelectionState(1); //Secondary
}
else if(sender->GetActiveTool(2) == toolList[i])
{
tempButton->SetSelectionState(2); //Tertiary
}
tempButton->SetBorderColour(ui::Colour(0, 0, 0));
tempButton->SetActiveBackgroundColour(ui::Colour(toolList[i]->colRed, toolList[i]->colGreen, toolList[i]->colBlue));
tempButton->SetActiveBorderColour(ui::Colour(0, 0, 255));
tempButton->SetAlignment(AlignCentre, AlignBottom);
AddComponent(tempButton);
@ -374,7 +391,7 @@ void GameView::NotifyBrushChanged(GameModel * sender)
void GameView::OnMouseMove(int x, int y, int dx, int dy)
{
currentMouse = ui::Point(x, y);
if(isMouseDown)
if(isMouseDown && drawMode == DrawPoints)
{
pointQueue.push(new ui::Point(x-dx, y-dy));
pointQueue.push(new ui::Point(x, y));
@ -385,8 +402,21 @@ void GameView::OnMouseDown(int x, int y, unsigned button)
{
if(currentMouse.X > 0 && currentMouse.X < XRES && currentMouse.Y > 0 && currentMouse.Y < YRES && !(zoomEnabled && !zoomCursorFixed))
{
if(button == BUTTON_LEFT)
toolIndex = 0;
if(button == BUTTON_RIGHT)
toolIndex = 1;
if(button == BUTTON_MIDDLE)
toolIndex = 2;
isMouseDown = true;
pointQueue.push(new ui::Point(x, y));
if(drawMode == DrawRect || drawMode == DrawLine)
{
drawPoint1 = ui::Point(x, y);
}
if(drawMode == DrawPoints)
{
pointQueue.push(new ui::Point(x, y));
}
}
}
@ -399,7 +429,27 @@ void GameView::OnMouseUp(int x, int y, unsigned button)
if(isMouseDown)
{
isMouseDown = false;
pointQueue.push(new ui::Point(x, y));
if(drawMode == DrawRect || drawMode == DrawLine)
{
drawPoint2 = ui::Point(x, y);
if(drawMode == DrawRect)
{
c->DrawRect(toolIndex, drawPoint1, drawPoint2);
}
if(drawMode == DrawLine)
{
c->DrawLine(toolIndex, drawPoint1, drawPoint2);
}
}
if(drawMode == DrawPoints)
{
pointQueue.push(new ui::Point(x, y));
}
if(drawModeReset)
{
drawModeReset = false;
drawMode = DrawPoints;
}
}
}
}
@ -426,6 +476,26 @@ void GameView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool
{
switch(key)
{
case KEY_CTRL:
if(drawModeReset)
drawModeReset = false;
else
drawPoint1 = currentMouse;
if(shift)
drawMode = DrawFill;
else
drawMode = DrawRect;
break;
case KEY_SHIFT:
if(drawModeReset)
drawModeReset = false;
else
drawPoint1 = currentMouse;
if(ctrl)
drawMode = DrawFill;
else
drawMode = DrawLine;
break;
case ' ': //Space
c->SetPaused();
break;
@ -442,26 +512,37 @@ void GameView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool
void GameView::OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt)
{
//switch(key)
//{
//case 'z':
if(!isMouseDown)
drawMode = DrawPoints;
else
drawModeReset = true;
switch(character)
{
case 'z':
if(!zoomCursorFixed)
c->SetZoomEnabled(false);
// break;
//}
break;
}
}
void GameView::OnTick(float dt)
{
if(zoomEnabled && !zoomCursorFixed)
c->SetZoomPosition(currentMouse);
if(isMouseDown)
if(drawMode == DrawPoints)
{
pointQueue.push(new ui::Point(currentMouse));
if(isMouseDown)
{
pointQueue.push(new ui::Point(currentMouse));
}
if(!pointQueue.empty())
{
c->DrawPoints(toolIndex, pointQueue);
}
}
if(!pointQueue.empty())
if(drawMode == DrawFill)
{
c->DrawPoints(pointQueue);
c->DrawFill(toolIndex, currentMouse);
}
c->Update();
}
@ -480,7 +561,18 @@ void GameView::OnDraw()
ren->DrawWalls();
if(activeBrush && currentMouse.X > 0 && currentMouse.X < XRES && currentMouse.Y > 0 && currentMouse.Y < YRES)
{
activeBrush->Render(ui::Engine::Ref().g, c->PointTranslate(currentMouse));
if(drawMode==DrawRect && isMouseDown)
{
activeBrush->RenderRect(ui::Engine::Ref().g, c->PointTranslate(drawPoint1), c->PointTranslate(currentMouse));
}
else if(drawMode==DrawLine && isMouseDown)
{
activeBrush->RenderLine(ui::Engine::Ref().g, c->PointTranslate(drawPoint1), c->PointTranslate(currentMouse));
}
else
{
activeBrush->RenderPoint(ui::Engine::Ref().g, c->PointTranslate(currentMouse));
}
}
ren->RenderZoom();
ren->DrawSigns();

View File

@ -8,25 +8,33 @@
#include "interface/Window.h"
#include "interface/Point.h"
#include "interface/Button.h"
#include "ToolButton.h"
#include "Brush.h"
using namespace std;
enum DrawMode
{
DrawPoints, DrawLine, DrawRect, DrawFill
};
class GameController;
class GameModel;
class GameView: public ui::Window
{
private:
DrawMode drawMode;
bool isMouseDown;
bool zoomEnabled;
bool zoomCursorFixed;
int toolIndex;
queue<ui::Point*> pointQueue;
GameController * c;
Renderer * ren;
Brush * activeBrush;
//UI Elements
vector<ui::Button*> menuButtons;
vector<ui::Button*> toolButtons;
vector<ToolButton*> toolButtons;
ui::Button * searchButton;
ui::Button * reloadButton;
ui::Button * saveSimulationButton;
@ -39,6 +47,10 @@ private:
ui::Button * displayModeButton;
ui::Button * pauseButton;
ui::Point currentMouse;
bool drawModeReset;
ui::Point drawPoint1;
ui::Point drawPoint2;
public:
GameView();
void AttachController(GameController * _c){ c = _c; }
@ -49,7 +61,7 @@ public:
void NotifyBrushChanged(GameModel * sender);
void NotifyMenuListChanged(GameModel * sender);
void NotifyToolListChanged(GameModel * sender);
void NotifyActiveToolChanged(GameModel * sender);
void NotifyActiveToolsChanged(GameModel * sender);
void NotifyUserChanged(GameModel * sender);
void NotifyZoomChanged(GameModel * sender);
virtual void OnMouseMove(int x, int y, int dx, int dy);

View File

@ -48,7 +48,9 @@ public:
virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) {
sim->create_line(position1.X, position1.Y, position2.X, position2.Y, 1, 1, toolID, 0, brush);
}
virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) {}
virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) {
sim->create_box(position1.X, position1.Y, position2.X, position2.Y, toolID, 0);
}
};
#endif /* TOOL_H_ */

88
src/game/ToolButton.cpp Normal file
View File

@ -0,0 +1,88 @@
/*
* ToolButton.cpp
*
* Created on: Jan 30, 2012
* Author: Simon
*/
#include "ToolButton.h"
#include "interface/Keys.h"
ToolButton::ToolButton(ui::Point position, ui::Point size, std::string text_):
ui::Button(position, size, text_)
{
SetSelectionState(-1);
}
void ToolButton::OnMouseClick(int x, int y, unsigned int button)
{
isButtonDown = true;
}
void ToolButton::OnMouseUp(int x, int y, unsigned int button)
{
if(isButtonDown)
{
if(button == BUTTON_LEFT)
SetSelectionState(0);
if(button == BUTTON_RIGHT)
SetSelectionState(1);
if(button == BUTTON_MIDDLE)
SetSelectionState(2);
DoAction();
}
isButtonDown = false;
}
void ToolButton::Draw(const ui::Point& screenPos)
{
Graphics * g = ui::Engine::Ref().g;
int totalColour = background.Red + 3*background.Green + 2*background.Blue;
g->fillrect(screenPos.X, screenPos.Y, Size.X, Size.Y, background.Red, background.Green, background.Blue, 255);
if (totalColour<544)
{
g->drawtext(screenPos.X+textPosition.X, screenPos.Y+textPosition.Y, ButtonText.c_str(), 255, 255, 255, 255);
}
else
{
g->drawtext(screenPos.X+textPosition.X, screenPos.Y+textPosition.Y, ButtonText.c_str(), 0, 0, 0, 255);
}
if(currentSelection!=-1)
{
//g->fillrect(screenPos.X+1, screenPos.Y+1, Size.X-2, Size.Y-2, 255, 255, 255, 170);
g->fillrect(screenPos.X+2, screenPos.Y+2, Size.Y-4, Size.Y-4, 0, 0, 0, 170);
g->drawtext(screenPos.X+5, screenPos.Y+4, selectionText, 255, 255, 255, 255);
}
}
void ToolButton::SetSelectionState(int state)
{
currentSelection = state;
switch(state)
{
case 0:
selectionText = "L";
break;
case 1:
selectionText = "R";
break;
case 2:
selectionText = "M";
break;
default:
selectionText = "";
break;
}
}
int ToolButton::GetSelectionState()
{
return currentSelection;
}
ToolButton::~ToolButton() {
// TODO Auto-generated destructor stub
}

26
src/game/ToolButton.h Normal file
View File

@ -0,0 +1,26 @@
/*
* ToolButton.h
*
* Created on: Jan 30, 2012
* Author: Simon
*/
#ifndef TOOLBUTTON_H_
#define TOOLBUTTON_H_
#include "interface/Button.h"
class ToolButton: public ui::Button {
int currentSelection;
std::string selectionText;
public:
ToolButton(ui::Point position, ui::Point size, std::string text_);
virtual void OnMouseUp(int x, int y, unsigned int button);
virtual void OnMouseClick(int x, int y, unsigned int button);
virtual void Draw(const ui::Point& screenPos);
void SetSelectionState(int state);
int GetSelectionState();
virtual ~ToolButton();
};
#endif /* TOOLBUTTON_H_ */

View File

@ -8,7 +8,14 @@
#define KEY_DELETE SDLK_DELETE
#define KEY_TAB SDLK_TAB
#define KEY_CTRL SDLK_LCTRL
#define KEY_ALT SDLK_LALT
#define KEY_SHIFT SDLK_LSHIFT
#define KEY_MOD_CONTROL KMOD_CTRL
#define KEY_MOD_ALT KMOD_ALT
#define KEY_MOD_SHIFT KMOD_SHIFT
#define BUTTON_LEFT SDL_BUTTON_LEFT
#define BUTTON_MIDDLE SDL_BUTTON_MIDDLE
#define BUTTON_RIGHT SDL_BUTTON_RIGHT