Description:
Move cursor rendering out of FNAGame to separate Renderer.
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r564:628e13aebaa0 -

@@ -0,0 +1,36
1 using Microsoft.Xna.Framework;
2 using Microsoft.Xna.Framework.Graphics;
3
4 using isometricparkfna.UI;
5 using isometricparkfna.Components;
6
7 using Encompass;
8 using SpriteFontPlus;
9
10 namespace isometricparkfna.Renderers
11 {
12 public class CursorRenderer : GeneralRenderer
13 {
14 private SpriteBatch batch;
15 private SpriteFont font;
16
17 public CursorRenderer(SpriteBatch batch, SpriteFont font)
18 {
19 this.batch = batch;
20 this.font = font;
21 }
22
23 public override void Render()
24 {
25
26 foreach (ref readonly var entity in ReadEntities<CursorComponent>())
27 {
28
29 var cursorComponent = GetComponent<CursorComponent>(entity);
30 Tile.OutlineSquare(batch, cursorComponent.position.X, cursorComponent.position.Y, Color.Yellow);
31 }
32 }
33
34
35 }
36 }
@@ -24,10 +24,13
24 typeof(GameStateMessage),
24 typeof(GameStateMessage),
25 typeof(QuitGameMessage),
25 typeof(QuitGameMessage),
26 typeof(SpawnSelection),
26 typeof(SpawnSelection),
27 typeof(AdjustSelection))]
27 typeof(AdjustSelection)
28 )]
28 [Reads(typeof(WindowTypeComponent),
29 [Reads(typeof(WindowTypeComponent),
29 typeof(GameStateComponent),
30 typeof(GameStateComponent),
30 typeof(VisibilityComponent))]
31 typeof(VisibilityComponent),
32 typeof(CursorComponent))]
33 [Writes(typeof(CursorComponent))]
31 public class InputEngine : Engine
34 public class InputEngine : Engine
32 {
35 {
33 private KeyboardState keyboardPrev;
36 private KeyboardState keyboardPrev;
@@ -39,15 +42,31
39
42
40 //Area to ignore:
43 //Area to ignore:
41 private int menuBarHeight;
44 private int menuBarHeight;
45 private int height;
46 private int width;
42
47
43 public InputEngine(int menuBarHeight, Camera camera,
48 public InputEngine(int menuBarHeight, Camera camera,
44 GraphicsDeviceManager gdm) {
49 GraphicsDeviceManager gdm, int height, int width) {
45 //initialize to blank for now
50 //initialize to blank for now
46 this.keyboardPrev = new KeyboardState();
51 this.keyboardPrev = new KeyboardState();
47 this.menuBarHeight = menuBarHeight;
52 this.menuBarHeight = menuBarHeight;
48 this.camera = camera;
53 this.camera = camera;
49 this.gdm = gdm;
54 this.gdm = gdm;
50 this.graphicsDevice = gdm.GraphicsDevice;
55 this.graphicsDevice = gdm.GraphicsDevice;
56 this.height = height;
57 this.width = width;
58 }
59
60
61 Vector2 calculateMousegrid(Vector2 normalizedMousePos)
62 {
63 Vector2 adjust = new Vector2(Tile.TileSpriteWidth / 2, Tile.TileSpriteHeight);
64 Vector2 adjustedMousePos = normalizedMousePos - adjust;
65
66 float boardx = ((adjustedMousePos.X / Tile.TileSpriteWidth) + (adjustedMousePos.Y / Tile.TileSpriteHeight));
67 float boardy = ((adjustedMousePos.Y / Tile.TileSpriteHeight) - (adjustedMousePos.X / Tile.TileSpriteWidth));
68
69 return new Vector2((int)boardx, (int)boardy);
51 }
70 }
52
71
53 public override void Update(double dt) {
72 public override void Update(double dt) {
@@ -268,6 +287,23
268 {
287 {
269 SendMessage(new AdjustSelection {End = CellMap.calculateMousegrid(original_point)});
288 SendMessage(new AdjustSelection {End = CellMap.calculateMousegrid(original_point)});
270 }
289 }
290
291 var transformedPosition = Vector2.Transform(new Vector2(mouseCur.X, mouseCur.Y), Matrix.Invert(camera.get_transformation(this.graphicsDevice)));
292 var gridPosition = calculateMousegrid(transformedPosition);
293 if (MathUtils.Between(gridPosition.X, 0, this.width)
294 && MathUtils.Between(gridPosition.Y, 0, this.height))
295 {
296 foreach (ref readonly var entity in ReadEntities<CursorComponent>()) {
297
298
299 var cursorComponent = GetComponent<CursorComponent>(entity);
300
301 SetComponent(entity, new CursorComponent { position = gridPosition,
302 size = 1 });
303
304 }
305 }
306
271 }
307 }
272
308
273 #endregion
309 #endregion
@@ -137,6 +137,15
137 _ => 1000M
137 _ => 1000M
138 };
138 };
139
139
140 #region
141
142 var cursorEntity = CreateEntity();
143 AddComponent(cursorEntity,
144 new CursorComponent { position = new Vector2(20, 20),
145 size = 1 });
146
147 #endregion
148
140 Logging.Success("Spawned new game.");
149 Logging.Success("Spawned new game.");
141 }
150 }
142 }
151 }
@@ -265,7 +265,8
265
265
266 Logging.Debug(this.Story.ContinueMaximally());
266 Logging.Debug(this.Story.ContinueMaximally());
267
267
268 WorldBuilder.AddEngine(new InputEngine(Menu.MENU_BAR_HEIGHT, this.camera, gdm));
268 WorldBuilder.AddEngine(new InputEngine(Menu.MENU_BAR_HEIGHT, this.camera, gdm,
269 this.simulation.map.MapHeight, this.simulation.map.MapWidth));
269 WorldBuilder.AddEngine(new UIEngine(this.Story));
270 WorldBuilder.AddEngine(new UIEngine(this.Story));
270 WorldBuilder.AddEngine(new DialogEngine(this.Story, this.grammar));
271 WorldBuilder.AddEngine(new DialogEngine(this.Story, this.grammar));
271
272
@@ -292,6 +293,7
292
293
293 WorldBuilder.AddGeneralRenderer(new AreaRenderer(this.tileBatch, this.monoFont), 1);
294 WorldBuilder.AddGeneralRenderer(new AreaRenderer(this.tileBatch, this.monoFont), 1);
294 WorldBuilder.AddGeneralRenderer(new ImGuiWindowRenderer(this, this.simulation, this.imGuiWindowBridgeEngine, this.gdm), 2);
295 WorldBuilder.AddGeneralRenderer(new ImGuiWindowRenderer(this, this.simulation, this.imGuiWindowBridgeEngine, this.gdm), 2);
296 WorldBuilder.AddGeneralRenderer(new CursorRenderer(this.tileBatch, this.monoFont), 3);
295 var contractWindow = WorldBuilder.CreateEntity();
297 var contractWindow = WorldBuilder.CreateEntity();
296 WorldBuilder.SetComponent(contractWindow, new VisibilityComponent { visible = false });
298 WorldBuilder.SetComponent(contractWindow, new VisibilityComponent { visible = false });
297 WorldBuilder.SetComponent(contractWindow, new WindowTypeComponent { type = isometricparkfna.Messages.Window.Contracts });
299 WorldBuilder.SetComponent(contractWindow, new WindowTypeComponent { type = isometricparkfna.Messages.Window.Contracts });
@@ -733,13 +735,6
733 drawTileAt(12, 25, 300, 2);
735 drawTileAt(12, 25, 300, 2);
734 #endif
736 #endif
735
737
736 #region draw_cursor
737
738 if (MathUtils.Between(this.mouseGrid.X, 0, this.simulation.map.MapWidth)
739 && MathUtils.Between(this.mouseGrid.Y, 0, this.simulation.map.MapHeight))
740 {
741 Tile.OutlineSquare(tileBatch, this.mouseGrid.X, this.mouseGrid.Y, Color.Yellow, 1);
742 }
743
738
744 #if DEBUG
739 #if DEBUG
745 Tile.OutlineSquare(tileBatch, 1, 1, Color.Red, 2);
740 Tile.OutlineSquare(tileBatch, 1, 1, Color.Red, 2);
@@ -763,8 +758,6
763 Quad.FillSquare2(tileBatch, 8, 7, Color.Teal, .125f, 0.79f);
758 Quad.FillSquare2(tileBatch, 8, 7, Color.Teal, .125f, 0.79f);
764 #endif
759 #endif
765
760
766 #endregion draw_cursor
767
768 stopWatch2 = new Stopwatch();
761 stopWatch2 = new Stopwatch();
769 stopWatch2.Start();
762 stopWatch2.Start();
770 #region draw_trees
763 #region draw_trees
@@ -1,4 +1,3
1
2 using Microsoft.Xna.Framework;
1 using Microsoft.Xna.Framework;
3 using Microsoft.Xna.Framework.Graphics;
2 using Microsoft.Xna.Framework.Graphics;
4
3
@@ -15,7 +14,6
15 private SpriteBatch batch;
14 private SpriteBatch batch;
16 private SpriteFont font;
15 private SpriteFont font;
17
16
18
19 public AreaRenderer(SpriteBatch batch, SpriteFont font)
17 public AreaRenderer(SpriteBatch batch, SpriteFont font)
20 {
18 {
21 this.batch = batch;
19 this.batch = batch;
@@ -46,7 +46,7
46
46
47 static public Texture2D TileSetTexture;
47 static public Texture2D TileSetTexture;
48
48
49
49 public const float DEPTH = 0.79f;
50
50
51
51
52 public Tile()
52 public Tile()
@@ -55,7 +55,6
55 }
55 }
56
56
57
57
58
59 static public Rectangle GetSourceRectangle(int tileIndex)
58 static public Rectangle GetSourceRectangle(int tileIndex)
60 {
59 {
61 int tileY = tileIndex / (TileSetTexture.Width / TileWidth);
60 int tileY = tileIndex / (TileSetTexture.Width / TileWidth);
@@ -128,26 +127,26
128 new Vector2(((x - y) * Tile.TileSpriteWidth / 2), (x + y) * Tile.TileSpriteHeight / 2) + adjust2,
127 new Vector2(((x - y) * Tile.TileSpriteWidth / 2), (x + y) * Tile.TileSpriteHeight / 2) + adjust2,
129 //new Vector2(this.squaresAcross * Tile.TileSpriteWidth, (y+1) * Tile.TileSpriteHeight),
128 //new Vector2(this.squaresAcross * Tile.TileSpriteWidth, (y+1) * Tile.TileSpriteHeight),
130 new Vector2(((x - y + size_less_one) * Tile.TileSpriteWidth / 2), (x + y + size_less_one) * Tile.TileSpriteHeight / 2) + adjust2,
129 new Vector2(((x - y + size_less_one) * Tile.TileSpriteWidth / 2), (x + y + size_less_one) * Tile.TileSpriteHeight / 2) + adjust2,
131 color, 0.79f);
130 color, Tile.DEPTH);
132
131
133 //Bottom right
132 //Bottom right
134 Line.drawLine(batch,
133 Line.drawLine(batch,
135 new Vector2(((x + size - y) * Tile.TileSpriteWidth / 2), (x + size_less_one + y) * Tile.TileSpriteHeight / 2) + adjust2,
134 new Vector2(((x + size - y) * Tile.TileSpriteWidth / 2), (x + size_less_one + y) * Tile.TileSpriteHeight / 2) + adjust2,
136 //new Vector2(this.squaresAcross * Tile.TileSpriteWidth, (y+1) * Tile.TileSpriteHeight),
135 //new Vector2(this.squaresAcross * Tile.TileSpriteWidth, (y+1) * Tile.TileSpriteHeight),
137 new Vector2(((x + size - (y + size)) * Tile.TileSpriteWidth / 2), (x + size_less_one + (y + size_less_one)) * Tile.TileSpriteHeight / 2) + adjust2,
136 new Vector2(((x + size - (y + size)) * Tile.TileSpriteWidth / 2), (x + size_less_one + (y + size_less_one)) * Tile.TileSpriteHeight / 2) + adjust2,
138 color, 0.79f);
137 color, Tile.DEPTH);
139 //Bottom left
138 //Bottom left
140 Line.drawLine(batch,
139 Line.drawLine(batch,
141 new Vector2(((x - (y + size)) * Tile.TileSpriteWidth / 2), (x + y + size) * Tile.TileSpriteHeight / 2) + adjust2,
140 new Vector2(((x - (y + size)) * Tile.TileSpriteWidth / 2), (x + y + size) * Tile.TileSpriteHeight / 2) + adjust2,
142 //new Vector2(this.squaresAcross * Tile.TileSpriteWidth, (y+1) * Tile.TileSpriteHeight),
141 //new Vector2(this.squaresAcross * Tile.TileSpriteWidth, (y+1) * Tile.TileSpriteHeight),
143 new Vector2(((x + size - (y + size)) * Tile.TileSpriteWidth / 2), (x + size + (y + size)) * Tile.TileSpriteHeight / 2) + adjust2,
142 new Vector2(((x + size - (y + size)) * Tile.TileSpriteWidth / 2), (x + size + (y + size)) * Tile.TileSpriteHeight / 2) + adjust2,
144 color, 0.79f);
143 color, Tile.DEPTH);
145 //Upper left
144 //Upper left
146 Line.drawLine(batch,
145 Line.drawLine(batch,
147 new Vector2(((x - y) * Tile.TileSpriteWidth / 2), (x + y) * Tile.TileSpriteHeight / 2) + adjust2,
146 new Vector2(((x - y) * Tile.TileSpriteWidth / 2), (x + y) * Tile.TileSpriteHeight / 2) + adjust2,
148 //new Vector2(this.squaresAcross * Tile.TileSpriteWidth, (y+1) * Tile.TileSpriteHeight),
147 //new Vector2(this.squaresAcross * Tile.TileSpriteWidth, (y+1) * Tile.TileSpriteHeight),
149 new Vector2(((x - (y + size)) * Tile.TileSpriteWidth / 2), (x + (y + size)) * Tile.TileSpriteHeight / 2) + adjust2,
148 new Vector2(((x - (y + size)) * Tile.TileSpriteWidth / 2), (x + (y + size)) * Tile.TileSpriteHeight / 2) + adjust2,
150 color, 0.79f);
149 color, Tile.DEPTH);
151 }
150 }
152
151
153 public static void drawOutline(SpriteBatch batch, Color color) {
152 public static void drawOutline(SpriteBatch batch, Color color) {
You need to be logged in to leave comments. Login now