Skip to content

Commit

Permalink
Adds grid to snap grid placement manager (#384)
Browse files Browse the repository at this point in the history
* Adds grid to snap grid placement manager
Fixes drawline

* Done
  • Loading branch information
clusterfack authored and PJB3005 committed Aug 26, 2017
1 parent 3920da2 commit f227b3a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
6 changes: 4 additions & 2 deletions SS14.Client.Graphics/CluwneLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,13 +372,15 @@ public static void drawHollowPoint(int posX, int posY, Color4 OutlineColor)
/// <param name="rotate"> Line Rotation </param>
/// <param name="thickness"> Line Thickness </param>
/// <param name="Color"> Line Color </param>
public static void drawLine(int posX, int posY, int rotate, float thickness, Color Color)
public static void drawLine(float posX, float posY, float length, float rotate, float thickness, Color4 Color)
{
RectangleShape line = new RectangleShape();
line.Position = new Vector2f(posX, posY);
line.Size = new Vector2f(length, thickness);
line.Rotation = rotate;
line.OutlineThickness = thickness;
line.FillColor = Color;
line.FillColor = Color.Convert();
line.OutlineColor = Color.Convert();

CurrentRenderTarget.Draw(line);
}
Expand Down
31 changes: 28 additions & 3 deletions SS14.Client/Placement/Modes/AlignSnapgridBorder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,56 @@
using SS14.Shared.Utility;
using SS14.Shared.Maths;
using System;
using OpenTK.Graphics;

namespace SS14.Client.Placement.Modes
{
public class SnapgridBorder : PlacementMode
{
bool ongrid;
float snapsize;

public SnapgridBorder(PlacementManager pMan) : base(pMan)
{
}

public override void Render()
{
base.Render();
if (ongrid)
{
var position = CluwneLib.ScreenToWorld(new Vector2i(0,0)); //Find world coordinates closest to screen origin
var gridstart = CluwneLib.WorldToScreen(new Vector2( //Find snap grid closest to screen origin and convert back to screen coords
(float)Math.Round((position.X / (double)snapsize), MidpointRounding.AwayFromZero) * snapsize,
(float)Math.Round((position.Y / (double)snapsize), MidpointRounding.AwayFromZero) * snapsize));
for (float a = gridstart.X; a < CluwneLib.ScreenViewportSize.X; a += snapsize * 32) //Iterate through screen creating gridlines
{
CluwneLib.drawLine(a, 0, CluwneLib.ScreenViewportSize.Y, 90, 0.5f, Color4.Blue);
}
for (float a = gridstart.Y; a < CluwneLib.ScreenViewportSize.Y; a += snapsize * 32)
{
CluwneLib.drawLine(0, a, CluwneLib.ScreenViewportSize.X, 0, 0.5f, Color4.Blue);
}
}
}

public override bool Update(Vector2i mouseS, IMapManager currentMap)
{
if (currentMap == null) return false;

mouseScreen = mouseS;
mouseWorld = CluwneLib.ScreenToWorld(mouseScreen);

if (!currentMap.TryFindGridAt(mouseWorld, out IMapGrid currentgrid)) //Cant find a grid
if (! (ongrid = currentMap.TryFindGridAt(mouseWorld, out IMapGrid currentgrid))) //Cant find a grid
return false;

var mouselocal = currentgrid.WorldToLocal(mouseWorld); //Convert code to local grid coordinates
var snapsize = currentgrid.SnapSize; //Find snap size
snapsize = currentgrid.SnapSize; //Find snap size.

mouselocal = new Vector2( //Round local coordinates onto the snap grid
(float)Math.Round((mouselocal.X / (double)snapsize), MidpointRounding.AwayFromZero) * snapsize,
(float)Math.Round((mouselocal.Y / (double)snapsize), MidpointRounding.AwayFromZero) * snapsize);

//Convert back to original world and screen coordinates after applying offset
mouseWorld = currentgrid.LocalToWorld(mouselocal) + new Vector2(pManager.CurrentPrototype.PlacementOffset.X, pManager.CurrentPrototype.PlacementOffset.Y);
mouseScreen = (Vector2i)CluwneLib.WorldToScreen(mouseWorld);
Expand Down
31 changes: 28 additions & 3 deletions SS14.Client/Placement/Modes/AlignSnapgridCenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,52 @@
using SS14.Shared.Utility;
using SS14.Shared.Maths;
using System;
using OpenTK.Graphics;

namespace SS14.Client.Placement.Modes
{
public class SnapgridCenter : PlacementMode
{
bool ongrid;
float snapsize;

public SnapgridCenter(PlacementManager pMan) : base(pMan)
{
}

public override void Render()
{
base.Render();
if (ongrid)
{
var position = CluwneLib.ScreenToWorld(new Vector2i(0, 0)); //Find world coordinates closest to screen origin
var gridstart = CluwneLib.WorldToScreen(new Vector2( //Find snap grid closest to screen origin and convert back to screen coords
(float)(Math.Round((position.X / (double)snapsize-0.5), MidpointRounding.AwayFromZero)+0.5) * snapsize,
(float)(Math.Round((position.Y / (double)snapsize-0.5), MidpointRounding.AwayFromZero)+0.5) * snapsize));
for (float a = gridstart.X; a < CluwneLib.ScreenViewportSize.X; a += snapsize * 32) //Iterate through screen creating gridlines
{
CluwneLib.drawLine(a, 0, CluwneLib.ScreenViewportSize.Y, 90, 0.5f, Color4.Blue);
}
for (float a = gridstart.Y; a < CluwneLib.ScreenViewportSize.Y; a += snapsize * 32)
{
CluwneLib.drawLine(0, a, CluwneLib.ScreenViewportSize.X, 0, 0.5f, Color4.Blue);
}
}
}

public override bool Update(Vector2i mouseS, IMapManager currentMap)
{
if (currentMap == null) return false;

mouseScreen = mouseS;
mouseWorld = CluwneLib.ScreenToWorld(mouseScreen);
if (!currentMap.TryFindGridAt(mouseWorld, out IMapGrid currentgrid)) //Cant find a grid

if (! (ongrid = currentMap.TryFindGridAt(mouseWorld, out IMapGrid currentgrid))) //Cant find a grid
return false;

var mouselocal = currentgrid.WorldToLocal(mouseWorld); //Convert code to local grid coordinates
var snapsize = currentgrid.SnapSize; //Find snap size
snapsize = currentgrid.SnapSize; //Find snap size.

mouselocal = new Vector2( //Round local coordinates onto the snap grid
(float)(Math.Round((mouselocal.X / (double)snapsize-0.5), MidpointRounding.AwayFromZero)+0.5) * snapsize,
(float)(Math.Round((mouselocal.Y / (double)snapsize-0.5), MidpointRounding.AwayFromZero)+0.5) * snapsize);
Expand Down

0 comments on commit f227b3a

Please sign in to comment.