Skip to content

Commit 680927a

Browse files
committed
Fixes to map & minimap drawing
Current implementation of map drawing with _offsets in BaseGameView in some cases didn't work well (e.g. zoomed out round maps), so I introduced an additional xShift parameter. It should also make it easier to understand what's going on in code. A bunch of fixes for minimap panel, which should also be working now for long flat/round maps. I also added an option to show grid on map.
1 parent 9b0293b commit 680927a

File tree

12 files changed

+234
-128
lines changed

12 files changed

+234
-128
lines changed

Civ2Gold/Civ2GoldInterface.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public override void Initialize()
259259
new MenuElement("Max Zoom Out|Ctrl+X", new Shortcut(KeyboardKey.X, ctrl: true),
260260
KeyboardKey.Null, commandId: MaxZoomOut),
261261
new MenuElement("Show Map Grid|Ctrl+G", new Shortcut(KeyboardKey.G, ctrl: true),
262-
KeyboardKey.Null),
262+
KeyboardKey.Null, commandId: ShowMapGrid),
263263
new MenuElement("Arrange Windows", Shortcut.None, KeyboardKey.Null),
264264
new MenuElement("Show Hidden Terrain|t", new Shortcut(KeyboardKey.T), KeyboardKey.T),
265265
new MenuElement("&Center View|c", new Shortcut(KeyboardKey.C), KeyboardKey.C)

Civ2TOT/TestOfTimeInterface.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ public override void Initialize()
341341
new MenuElement("Max Zoom Out|Ctrl+X", new Shortcut(KeyboardKey.X, ctrl: true),
342342
KeyboardKey.Null, commandId: MaxZoomOut),
343343
new MenuElement("Show Map Grid|Ctrl+G", new Shortcut(KeyboardKey.G, ctrl: true),
344-
KeyboardKey.Null),
344+
KeyboardKey.Null, commandId: ShowMapGrid),
345345
new MenuElement("Arrange Windows", Shortcut.None, KeyboardKey.Null),
346346
new MenuElement("Show Hidden Terrain|t", new Shortcut(KeyboardKey.T), KeyboardKey.T),
347347
new MenuElement("&Center View|c", new Shortcut(KeyboardKey.C), KeyboardKey.C),

Engine/src/Utils.cs

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ namespace Civ2engine
77
{
88
public static class Utils
99
{
10+
public static int WrapNumber(int number, int range) => (number % range + range) % range;
11+
1012
public static int GreatestCommonFactor(int a, int b)
1113
{
1214
while (b != 0)

Model/Core/Mapping/Tile.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class Tile : IMapItem
1717
private bool[] _visibility;
1818
public int X { get; }
1919
public int Y { get; }
20-
20+
2121
public int Z { get;}
2222

2323
public int Odd { get; }

Model/Events/MapEventArgs.cs

+2-15
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,12 @@ public class MapEventArgs : EventArgs
1010
public MapEventType EventType { get; }
1111
public List<Tile> TilesChanged { get; set; }
1212

13-
public int[] CentrXy, MapStartXy, MapDrawSq;
14-
public int Zoom;
13+
public int[] MapStartXy, MapDrawSq;
14+
public int Zoom, Xshift;
1515

1616
public MapEventArgs(MapEventType eventType)
1717
{
1818
EventType = eventType;
1919
}
20-
21-
public MapEventArgs(MapEventType eventType, int[] centrXy)
22-
{
23-
EventType = eventType;
24-
CentrXy = centrXy;
25-
}
26-
27-
public MapEventArgs(MapEventType eventType, int[] mapStartXy, int[] mapDrawSq)
28-
{
29-
EventType = eventType;
30-
MapStartXy = mapStartXy;
31-
MapDrawSq = mapDrawSq;
32-
}
3320
}
3421
}

Model/Menu/CommandIds.cs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public static class CommandIds
1515
public const string StandardZoom = "STANDARD_ZOOM";
1616
public const string MediumZoomOut = "MEDIUM_ZOOM_OUT";
1717
public const string MaxZoomOut = "MAX_ZOOM_OUT";
18+
public const string ShowMapGrid = "SHOW_MAP_GRID";
1819
public const string MapLayoutToggle = "MAP_LAYOUT";
1920

2021
public const string WaitOrder = "UNIT_ORDER_WAIT";
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Civ2engine;
2+
using Model;
3+
using Model.Dialog;
4+
using Model.Images;
5+
using Model.Menu;
6+
using Raylib_CSharp.Interact;
7+
8+
namespace RaylibUI.RunGame.Commands;
9+
10+
public class ShowMapGrid : IGameCommand
11+
{
12+
private readonly GameScreen _gameScreen;
13+
14+
public ShowMapGrid(GameScreen gameScreen)
15+
{
16+
_gameScreen = gameScreen;
17+
}
18+
19+
public string Id => CommandIds.ShowMapGrid;
20+
public Shortcut[] ActivationKeys { get; set; } = { new(KeyboardKey.G, ctrl: true) };
21+
public CommandStatus Status => CommandStatus.Normal;
22+
23+
public bool Update()
24+
{
25+
return true;
26+
}
27+
28+
public void Action()
29+
{
30+
_gameScreen.ShowMapGrid();
31+
}
32+
33+
public MenuCommand? Command { get; set; }
34+
public string ErrorDialog { get; } = string.Empty;
35+
public DialogImageElements? ErrorImage { get; } = null;
36+
public string? Name { get; }
37+
}

RaylibUI/RunGame/GameControls/Mapping/MapControl.cs

+7-4
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ private void SetDimensions()
124124
{
125125
_backgroundImage.Value.Unload();
126126
}
127-
_backgroundImage = ImageUtils.PaintDialogBase(_gameScreen.Main.ActiveInterface, Width, Height, _padding, noWallpaper:true);
127+
_backgroundImage = ImageUtils.PaintDialogBase(_active, Width, Height, _padding, noWallpaper:true);
128128

129129
if (!_gameScreen.ToTPanelLayout)
130130
{
@@ -187,8 +187,8 @@ private void OnClick(object? sender, MouseEventArgs mouseEventArgs)
187187

188188
var map = _gameScreen.CurrentMap;
189189
var dim = _gameScreen.TileCache.GetDimensions(map, _gameScreen.Zoom);
190-
var clickedTilePosition = clickPosition - new Vector2(_padding.Left + _padding.Right, _padding.Top) + _currentView.Offsets;
191-
var y = Math.DivRem((int)(clickedTilePosition.Y), dim.HalfHeight, out var yRemainder);
190+
var clickedTilePosition = clickPosition - new Vector2(_padding.Left, _padding.Top) + _currentView.Offsets;
191+
var y = Math.DivRem((int)clickedTilePosition.Y, dim.HalfHeight, out var yRemainder);
192192
var odd = y % 2 == 1;
193193
var clickX = (int)(odd ? clickedTilePosition.X - dim.HalfWidth : clickedTilePosition.X);
194194
if (clickX < 0)
@@ -256,6 +256,7 @@ private void OnClick(object? sender, MouseEventArgs mouseEventArgs)
256256

257257
if (0 <= y && y < map.Tile.GetLength(1))
258258
{
259+
x = Utils.WrapNumber(2 * x + _currentView.Xshift, 2 * map.XDim) / 2;
259260
return map.Tile[x, y];
260261
}
261262

@@ -276,6 +277,7 @@ private void MapEventTriggered(object sender, MapEventArgs e)
276277
{
277278
case MapEventType.MinimapViewChanged:
278279
{
280+
ForceRedraw = true;
279281
if (_currentView.IsDefault)
280282
{
281283
if (_gameScreen.ActiveMode != _gameScreen.ViewPiece)
@@ -322,7 +324,7 @@ public override void Draw(bool pulse)
322324
}
323325

324326
var paddedLoc = new Vector2(Location.X + _padding.Left, Location.Y + _padding.Top);
325-
Graphics.DrawTextureEx(_currentView.BaseImage, paddedLoc, 0f,1f,
327+
Graphics.DrawTextureEx(_currentView.BaseImage, paddedLoc, 0f, 1f,
326328
Color.White);
327329

328330
var cityDetails = new List<CityData>();
@@ -368,6 +370,7 @@ public override void Draw(bool pulse)
368370

369371
if (_backgroundImage != null)
370372
Graphics.DrawTextureEx(_backgroundImage.Value, Location, 0f, 1f, Color.White);
373+
371374
if (!_gameScreen.ToTPanelLayout)
372375
{
373376
_headerLabel.Draw(pulse);

0 commit comments

Comments
 (0)