Line snapping in gameview - hold alt

This commit is contained in:
Simon Robertshaw
2012-07-03 13:32:08 +01:00
parent 8b200ab326
commit 5edca6e243
2 changed files with 54 additions and 6 deletions

View File

@@ -31,7 +31,8 @@ GameView::GameView():
selectPoint2(0, 0), selectPoint2(0, 0),
placeSaveThumb(NULL), placeSaveThumb(NULL),
mousePosition(0, 0), mousePosition(0, 0),
lastOffset(0) lastOffset(0),
drawSnap(false)
{ {
int currentX = 1; int currentX = 1;
@@ -636,14 +637,27 @@ void GameView::OnMouseUp(int x, int y, unsigned button)
isMouseDown = false; isMouseDown = false;
if(drawMode == DrawRect || drawMode == DrawLine) if(drawMode == DrawRect || drawMode == DrawLine)
{ {
ui::Point finalDrawPoint2(0, 0);
drawPoint2 = ui::Point(x, y); drawPoint2 = ui::Point(x, y);
finalDrawPoint2 = drawPoint2;
if(drawSnap && drawMode == DrawLine)
{
finalDrawPoint2 = lineSnapCoords(drawPoint1, drawPoint2);
}
if(drawSnap && drawMode == DrawRect)
{
finalDrawPoint2 = rectSnapCoords(drawPoint1, drawPoint2);
}
if(drawMode == DrawRect) if(drawMode == DrawRect)
{ {
c->DrawRect(toolIndex, drawPoint1, drawPoint2); c->DrawRect(toolIndex, drawPoint1, finalDrawPoint2);
} }
if(drawMode == DrawLine) if(drawMode == DrawLine)
{ {
c->DrawLine(toolIndex, drawPoint1, drawPoint2); c->DrawLine(toolIndex, drawPoint1, finalDrawPoint2);
} }
} }
if(drawMode == DrawPoints) if(drawMode == DrawPoints)
@@ -724,6 +738,9 @@ void GameView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool
} }
switch(key) switch(key)
{ {
case KEY_ALT:
drawSnap = true;
break;
case KEY_CTRL: case KEY_CTRL:
if(drawModeReset) if(drawModeReset)
drawModeReset = false; drawModeReset = false;
@@ -815,6 +832,9 @@ void GameView::OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bo
drawModeReset = true; drawModeReset = true;
switch(key) switch(key)
{ {
case KEY_ALT:
drawSnap = false;
break;
case 'z': case 'z':
if(!zoomCursorFixed) if(!zoomCursorFixed)
c->SetZoomEnabled(false); c->SetZoomEnabled(false);
@@ -1030,17 +1050,27 @@ void GameView::OnDraw()
ren->FinaliseParts(); ren->FinaliseParts();
if(activeBrush && currentMouse.X > 0 && currentMouse.X < XRES && currentMouse.Y > 0 && currentMouse.Y < YRES) if(activeBrush && currentMouse.X > 0 && currentMouse.X < XRES && currentMouse.Y > 0 && currentMouse.Y < YRES)
{ {
ui::Point finalCurrentMouse = c->PointTranslate(currentMouse);
if(drawMode==DrawRect && isMouseDown) if(drawMode==DrawRect && isMouseDown)
{ {
activeBrush->RenderRect(g, c->PointTranslate(drawPoint1), c->PointTranslate(currentMouse)); if(drawSnap)
{
finalCurrentMouse = rectSnapCoords(c->PointTranslate(drawPoint1), finalCurrentMouse);
}
activeBrush->RenderRect(g, c->PointTranslate(drawPoint1), finalCurrentMouse);
} }
else if(drawMode==DrawLine && isMouseDown) else if(drawMode==DrawLine && isMouseDown)
{ {
activeBrush->RenderLine(g, c->PointTranslate(drawPoint1), c->PointTranslate(currentMouse)); if(drawSnap)
{
finalCurrentMouse = lineSnapCoords(c->PointTranslate(drawPoint1), finalCurrentMouse);
}
activeBrush->RenderLine(g, c->PointTranslate(drawPoint1), finalCurrentMouse);
} }
else else
{ {
activeBrush->RenderPoint(g, c->PointTranslate(currentMouse)); activeBrush->RenderPoint(g, finalCurrentMouse);
} }
} }
ren->RenderZoom(); ren->RenderZoom();
@@ -1129,3 +1159,18 @@ void GameView::OnDraw()
g->drawtext(XRES+BARSIZE-(10+Graphics::textwidth((char*)sampleInfo.str().c_str())), 10, (const char*)sampleInfo.str().c_str(), 255, 255, 255, 255); g->drawtext(XRES+BARSIZE-(10+Graphics::textwidth((char*)sampleInfo.str().c_str())), 10, (const char*)sampleInfo.str().c_str(), 255, 255, 255, 255);
} }
ui::Point GameView::lineSnapCoords(ui::Point point1, ui::Point point2)
{
ui::Point newPoint(0, 0);
float snapAngle = floor(atan2(point2.Y-point1.Y, point2.X-point1.X)/(M_PI*0.25)+0.5)*M_PI*0.25;
float lineMag = sqrtf(pow(point2.X-point1.X,2)+pow(point2.Y-point1.Y,2));
newPoint.X = (int)(lineMag*cos(snapAngle)+point1.X+0.5f);
newPoint.Y = (int)(lineMag*sin(snapAngle)+point1.Y+0.5f);
return newPoint;
}
ui::Point GameView::rectSnapCoords(ui::Point point1, ui::Point point2)
{
return point2;
}

View File

@@ -35,6 +35,7 @@ private:
bool isMouseDown; bool isMouseDown;
bool zoomEnabled; bool zoomEnabled;
bool zoomCursorFixed; bool zoomCursorFixed;
bool drawSnap;
int toolIndex; int toolIndex;
queue<ui::Point*> pointQueue; queue<ui::Point*> pointQueue;
GameController * c; GameController * c;
@@ -82,6 +83,8 @@ private:
int lastOffset; int lastOffset;
void setToolButtonOffset(int offset); void setToolButtonOffset(int offset);
void changeColour(); void changeColour();
virtual ui::Point lineSnapCoords(ui::Point point1, ui::Point point2);
virtual ui::Point rectSnapCoords(ui::Point point1, ui::Point point2);
public: public:
GameView(); GameView();