Skip to content

Commit f227b3a

Browse files
clusterfackPJB3005
clusterfack
authored andcommitted
Adds grid to snap grid placement manager (#384)
* Adds grid to snap grid placement manager Fixes drawline * Done
1 parent 3920da2 commit f227b3a

File tree

3 files changed

+60
-8
lines changed

3 files changed

+60
-8
lines changed

SS14.Client.Graphics/CluwneLib.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,13 +372,15 @@ public static void drawHollowPoint(int posX, int posY, Color4 OutlineColor)
372372
/// <param name="rotate"> Line Rotation </param>
373373
/// <param name="thickness"> Line Thickness </param>
374374
/// <param name="Color"> Line Color </param>
375-
public static void drawLine(int posX, int posY, int rotate, float thickness, Color Color)
375+
public static void drawLine(float posX, float posY, float length, float rotate, float thickness, Color4 Color)
376376
{
377377
RectangleShape line = new RectangleShape();
378378
line.Position = new Vector2f(posX, posY);
379+
line.Size = new Vector2f(length, thickness);
379380
line.Rotation = rotate;
380381
line.OutlineThickness = thickness;
381-
line.FillColor = Color;
382+
line.FillColor = Color.Convert();
383+
line.OutlineColor = Color.Convert();
382384

383385
CurrentRenderTarget.Draw(line);
384386
}

SS14.Client/Placement/Modes/AlignSnapgridBorder.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,56 @@
88
using SS14.Shared.Utility;
99
using SS14.Shared.Maths;
1010
using System;
11+
using OpenTK.Graphics;
1112

1213
namespace SS14.Client.Placement.Modes
1314
{
1415
public class SnapgridBorder : PlacementMode
1516
{
17+
bool ongrid;
18+
float snapsize;
19+
1620
public SnapgridBorder(PlacementManager pMan) : base(pMan)
1721
{
1822
}
1923

24+
public override void Render()
25+
{
26+
base.Render();
27+
if (ongrid)
28+
{
29+
var position = CluwneLib.ScreenToWorld(new Vector2i(0,0)); //Find world coordinates closest to screen origin
30+
var gridstart = CluwneLib.WorldToScreen(new Vector2( //Find snap grid closest to screen origin and convert back to screen coords
31+
(float)Math.Round((position.X / (double)snapsize), MidpointRounding.AwayFromZero) * snapsize,
32+
(float)Math.Round((position.Y / (double)snapsize), MidpointRounding.AwayFromZero) * snapsize));
33+
for (float a = gridstart.X; a < CluwneLib.ScreenViewportSize.X; a += snapsize * 32) //Iterate through screen creating gridlines
34+
{
35+
CluwneLib.drawLine(a, 0, CluwneLib.ScreenViewportSize.Y, 90, 0.5f, Color4.Blue);
36+
}
37+
for (float a = gridstart.Y; a < CluwneLib.ScreenViewportSize.Y; a += snapsize * 32)
38+
{
39+
CluwneLib.drawLine(0, a, CluwneLib.ScreenViewportSize.X, 0, 0.5f, Color4.Blue);
40+
}
41+
}
42+
}
43+
2044
public override bool Update(Vector2i mouseS, IMapManager currentMap)
2145
{
2246
if (currentMap == null) return false;
2347

2448
mouseScreen = mouseS;
2549
mouseWorld = CluwneLib.ScreenToWorld(mouseScreen);
2650

27-
if (!currentMap.TryFindGridAt(mouseWorld, out IMapGrid currentgrid)) //Cant find a grid
51+
if (! (ongrid = currentMap.TryFindGridAt(mouseWorld, out IMapGrid currentgrid))) //Cant find a grid
2852
return false;
2953

3054
var mouselocal = currentgrid.WorldToLocal(mouseWorld); //Convert code to local grid coordinates
31-
var snapsize = currentgrid.SnapSize; //Find snap size
55+
snapsize = currentgrid.SnapSize; //Find snap size.
56+
3257
mouselocal = new Vector2( //Round local coordinates onto the snap grid
3358
(float)Math.Round((mouselocal.X / (double)snapsize), MidpointRounding.AwayFromZero) * snapsize,
3459
(float)Math.Round((mouselocal.Y / (double)snapsize), MidpointRounding.AwayFromZero) * snapsize);
35-
60+
3661
//Convert back to original world and screen coordinates after applying offset
3762
mouseWorld = currentgrid.LocalToWorld(mouselocal) + new Vector2(pManager.CurrentPrototype.PlacementOffset.X, pManager.CurrentPrototype.PlacementOffset.Y);
3863
mouseScreen = (Vector2i)CluwneLib.WorldToScreen(mouseWorld);

SS14.Client/Placement/Modes/AlignSnapgridCenter.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,52 @@
88
using SS14.Shared.Utility;
99
using SS14.Shared.Maths;
1010
using System;
11+
using OpenTK.Graphics;
1112

1213
namespace SS14.Client.Placement.Modes
1314
{
1415
public class SnapgridCenter : PlacementMode
1516
{
17+
bool ongrid;
18+
float snapsize;
19+
1620
public SnapgridCenter(PlacementManager pMan) : base(pMan)
1721
{
1822
}
1923

24+
public override void Render()
25+
{
26+
base.Render();
27+
if (ongrid)
28+
{
29+
var position = CluwneLib.ScreenToWorld(new Vector2i(0, 0)); //Find world coordinates closest to screen origin
30+
var gridstart = CluwneLib.WorldToScreen(new Vector2( //Find snap grid closest to screen origin and convert back to screen coords
31+
(float)(Math.Round((position.X / (double)snapsize-0.5), MidpointRounding.AwayFromZero)+0.5) * snapsize,
32+
(float)(Math.Round((position.Y / (double)snapsize-0.5), MidpointRounding.AwayFromZero)+0.5) * snapsize));
33+
for (float a = gridstart.X; a < CluwneLib.ScreenViewportSize.X; a += snapsize * 32) //Iterate through screen creating gridlines
34+
{
35+
CluwneLib.drawLine(a, 0, CluwneLib.ScreenViewportSize.Y, 90, 0.5f, Color4.Blue);
36+
}
37+
for (float a = gridstart.Y; a < CluwneLib.ScreenViewportSize.Y; a += snapsize * 32)
38+
{
39+
CluwneLib.drawLine(0, a, CluwneLib.ScreenViewportSize.X, 0, 0.5f, Color4.Blue);
40+
}
41+
}
42+
}
43+
2044
public override bool Update(Vector2i mouseS, IMapManager currentMap)
2145
{
2246
if (currentMap == null) return false;
2347

2448
mouseScreen = mouseS;
2549
mouseWorld = CluwneLib.ScreenToWorld(mouseScreen);
26-
27-
if (!currentMap.TryFindGridAt(mouseWorld, out IMapGrid currentgrid)) //Cant find a grid
50+
51+
if (! (ongrid = currentMap.TryFindGridAt(mouseWorld, out IMapGrid currentgrid))) //Cant find a grid
2852
return false;
2953

3054
var mouselocal = currentgrid.WorldToLocal(mouseWorld); //Convert code to local grid coordinates
31-
var snapsize = currentgrid.SnapSize; //Find snap size
55+
snapsize = currentgrid.SnapSize; //Find snap size.
56+
3257
mouselocal = new Vector2( //Round local coordinates onto the snap grid
3358
(float)(Math.Round((mouselocal.X / (double)snapsize-0.5), MidpointRounding.AwayFromZero)+0.5) * snapsize,
3459
(float)(Math.Round((mouselocal.Y / (double)snapsize-0.5), MidpointRounding.AwayFromZero)+0.5) * snapsize);

0 commit comments

Comments
 (0)