From 3b56ea35e3d50eab97f31fdb92c20b7fab798799 Mon Sep 17 00:00:00 2001 From: Kurt Date: Sun, 25 Jan 2026 16:48:12 -0600 Subject: [PATCH] Minor clean --- .../Map/Layers/LayerPositionConfig.cs | 29 +++++---- .../Structures/Map/Managers/MapMutator.cs | 10 ++- NHSE.Sprites/Field/ItemLayerSprite.cs | 10 ++- NHSE.Sprites/Field/TerrainSprite.cs | 62 +++++++++++++++++-- NHSE.WinForms/Subforms/Map/FieldItemEditor.cs | 44 ++++++------- 5 files changed, 108 insertions(+), 47 deletions(-) diff --git a/NHSE.Core/Structures/Map/Layers/LayerPositionConfig.cs b/NHSE.Core/Structures/Map/Layers/LayerPositionConfig.cs index 89b75bc..506aa4e 100644 --- a/NHSE.Core/Structures/Map/Layers/LayerPositionConfig.cs +++ b/NHSE.Core/Structures/Map/Layers/LayerPositionConfig.cs @@ -62,6 +62,11 @@ namespace NHSE.Core; #pragma warning restore CA1857 } + /// + /// Calculates the absolute map coordinates based on the specified relative X and Y coordinates within the layer. + /// + /// The relative X-coordinate within the layer. + /// The relative Y-coordinate within the layer. public (int X, int Y) GetCoordinatesAbsolute(int relX, int relY) { var absX = relX + ((ShiftWidth * MetaTileSize) << TileBitShift); @@ -69,6 +74,17 @@ namespace NHSE.Core; return (absX, absY); } + /// + /// Gets the absolute coordinates of the layer's origin (0,0) in the map. + /// + public (int X, int Y) GetCoordinatesAbsolute() => GetCoordinatesAbsolute(0, 0); + + /// + /// Calculates the relative coordinates within the layer, based on the specified absolute X and Y coordinates. + /// + /// The absolute X coordinate to convert. + /// The absolute Y coordinate to convert. + /// A tuple containing the X and Y coordinates relative to the layer. public (int X, int Y) GetCoordinatesRelative(int absX, int absY) { var relX = absX - ((ShiftWidth * MetaTileSize) << TileBitShift); @@ -91,12 +107,6 @@ public bool IsCoordinateValidRelative(int relX, int relY) return true; } - public bool IsCoordinateValidAbsolute(int absX, int absY) - { - var (relX, relY) = GetCoordinatesRelative(absX, absY); - return IsCoordinateValidRelative(relX, relY); - } - /// /// Layer total width in tiles. /// @@ -116,11 +126,4 @@ public bool IsCoordinateValidAbsolute(int absX, int absY) /// Gets the total height of the map, in tiles. /// public int MapTotalHeight => MapAcreHeight * TilesPerAcre; - - public int GetAcreIndexRelative(int relX, int relY) - { - var acreX = relX >> TileBitShift; - var acreY = relY >> TileBitShift; - return (CountHeight * acreX) + acreY; - } } \ No newline at end of file diff --git a/NHSE.Core/Structures/Map/Managers/MapMutator.cs b/NHSE.Core/Structures/Map/Managers/MapMutator.cs index 47b381d..b434d5d 100644 --- a/NHSE.Core/Structures/Map/Managers/MapMutator.cs +++ b/NHSE.Core/Structures/Map/Managers/MapMutator.cs @@ -38,11 +38,10 @@ public int ModifyFieldItems(Func action, in bool wholeM } else { - (xMin, yMin) = (View.X, View.Y); // Convert absolute to relative coordinates - if (!Manager.ConfigItems.IsCoordinateValidAbsolute(xMin, yMin)) + (xMin, yMin) = Manager.ConfigItems.GetCoordinatesRelative(View.X, View.Y); + if (!Manager.ConfigItems.IsCoordinateValidRelative(xMin, yMin)) return 0; - (xMin, yMin) = Manager.ConfigItems.GetCoordinatesRelative(xMin, yMin); var info = layerField.TileInfo; (width, height) = info.DimAcre; @@ -69,11 +68,10 @@ private int ReplaceFieldItems(Item oldItem, Item newItem, bool wholeMap, LayerFi } else { - (xMin, yMin) = (View.X, View.Y); // Convert absolute to relative coordinates - if (!Manager.ConfigItems.IsCoordinateValidAbsolute(xMin, yMin)) + (xMin, yMin) = Manager.ConfigItems.GetCoordinatesRelative(View.X, View.Y); + if (!Manager.ConfigItems.IsCoordinateValidRelative(xMin, yMin)) return 0; - (xMin, yMin) = Manager.ConfigItems.GetCoordinatesRelative(xMin, yMin); var info = layerField.TileInfo; (width, height) = info.DimAcre; diff --git a/NHSE.Sprites/Field/ItemLayerSprite.cs b/NHSE.Sprites/Field/ItemLayerSprite.cs index 7fe75ad..0d874b5 100644 --- a/NHSE.Sprites/Field/ItemLayerSprite.cs +++ b/NHSE.Sprites/Field/ItemLayerSprite.cs @@ -23,7 +23,7 @@ public static class ItemLayerSprite /// Configuration for layer positioning. private static void LoadBitmapLayer(ReadOnlySpan items, Span bmpData, in LayerPositionConfig cfg) { - var (shiftX, shiftY) = cfg.GetCoordinatesAbsolute(0, 0); + var (shiftX, shiftY) = cfg.GetCoordinatesAbsolute(); // Iterate through the relative positions within the layer. // Then, map to absolute positions in the bitmap with the configured shift. @@ -395,6 +395,14 @@ public static void LoadItemLayer1(LayerPositionConfig cfg, LayerItem layer, Span ImageUtil.ClampAllTransparencyTo(data, transparency); } + /// + /// Draws a square reticle on the specified map image to indicate the current viewport area. + /// + /// The bitmap image on which to draw the reticle. + /// The viewport describing the area of the map present within the viewport. + /// The absolute X-coordinate, in tile units, of the top-left corner of the viewport. + /// The absolute Y-coordinate, in tile units, of the top-left corner of the viewport. + /// The image upscale scale factor to apply to the reticle's size and position. Must be a positive integer. The default is 1. public static void DrawViewReticle(Bitmap map, TileGridViewport g, int absX, int absY, int scale = 1) { using var gfx = Graphics.FromImage(map); diff --git a/NHSE.Sprites/Field/TerrainSprite.cs b/NHSE.Sprites/Field/TerrainSprite.cs index 8e8bafc..81cd247 100644 --- a/NHSE.Sprites/Field/TerrainSprite.cs +++ b/NHSE.Sprites/Field/TerrainSprite.cs @@ -32,7 +32,15 @@ public static class TerrainSprite private const int PlazaWidth = 6 * Scale; private const int PlazaHeight = 5 * Scale; - public static void GenerateMap(Bitmap map, MapMutator mut, Span scale1, Span scaleX, int imgScale) + /// + /// Generates a terrain map by loading, scaling, and applying terrain data to the specified bitmap. + /// + /// The bitmap to which the generated terrain map will be applied. + /// The map information manager that provides access to terrain configuration and management. + /// A span of integers used as a buffer for the initial terrain pixel data. + /// A span of integers used as a buffer for the upscaled terrain pixel data. + /// The scaling factor to apply when upscaling the terrain image. Must be a positive integer. + private static void GenerateMapTerrainAndUpscale(Bitmap map, MapMutator mut, Span scale1, Span scaleX, int imgScale) { // Load the terrain pixels, then upscale. var mgr = mut.Manager.LayerTerrain; @@ -41,10 +49,29 @@ public static void GenerateMap(Bitmap map, MapMutator mut, Span scale1, Spa map.SetBitmapData(scaleX); } + /// + /// Draws the map with all buildings and the plaza overlay onto the specified bitmap, using the provided map editor + /// and scaling information. + /// + /// + /// The method modifies the provided bitmap in place. + /// The scaling spans must be properly initialized to match the expected map dimensions. + /// If a specific building index is provided, only that building may be highlighted or rendered differently; + /// otherwise, all buildings are drawn normally. + /// + /// The bitmap on which the map, buildings, and plaza will be rendered. + /// The map editor instance containing map data, building information, and scaling parameters. + /// A span representing the primary scaling factors for rendering the map. + /// A span representing the secondary scaling factors for rendering the map. + /// + /// The index of a specific building to highlight or focus on. + /// Set to -1 to render all buildings without highlighting any particular one. + /// + /// The bitmap with the map, plaza, and buildings drawn onto it. The same instance as the input bitmap is returned. public static Bitmap GetMapWithBuildings(Bitmap map, MapEditor m, Span scale1, Span scaleX, int buildingIndex = -1) { var imgScale = m.MapScale * 2; // because terrain is 16px per tile, items are 32px per tile - GenerateMap(map, m.Mutator, scale1, scaleX, imgScale); + GenerateMapTerrainAndUpscale(map, m.Mutator, scale1, scaleX, imgScale); using var gfx = Graphics.FromImage(map); var plaza = m.Mutator.Manager.Plaza; @@ -53,6 +80,31 @@ public static Bitmap GetMapWithBuildings(Bitmap map, MapEditor m, Span scal return map; } + /// + /// Renders the current map viewport onto the specified bitmap, including terrain, buildings, grid overlays, and labels. + /// + /// + /// This method draws both graphical and textual elements of the map viewport, including overlays and labels. + /// It should be called whenever the viewport needs to be refreshed, such as after map edits or navigation. + /// The method modifies the provided bitmap in place. + /// + /// The bitmap onto which the viewport will be drawn. + /// The map editor instance providing map data, building information, and viewport configuration. + /// The font used to render building and terrain tile names within the viewport. + /// A span representing the primary scaling factors for rendering terrain pixels. + /// A span used for horizontal scaling and pixel data manipulation during rendering. + /// + /// The index of the currently selected building. + /// Used to highlight or annotate the selected building in the viewport. + /// + /// + /// The transparency level to apply when rendering buildings. + /// A value of 0xFF is fully opaque; lower values increase transparency. + /// + /// + /// The transparency level to apply when rendering terrain tile names. + /// A value of 0xFF is fully opaque; lower values increase transparency. + /// public static void LoadViewport(Bitmap img, MapEditor m, Font f, Span scale1, Span scaleX, int selectedBuildingIndex, byte transparencyBuilding, byte transTerrain) @@ -99,7 +151,7 @@ public static Bitmap GetMapWithBuildings(Bitmap map, MapEditor m, Span scal // Draw Text of Terrain Tile Names if (transTerrain != 0) - DrawViewTerrainTileNames(gfx, m.Terrain, cfg, f, relX, relY, m.ViewScale * 2, transTerrain); + gfx.DrawViewTerrainTileNames(m.Terrain, cfg, f, relX, relY, m.ViewScale * 2, transTerrain); // Done. } @@ -124,7 +176,7 @@ private static void DrawViewBuildings(this Graphics gfx, MapEditor m, int select private static void LoadTerrainPixels(LayerTerrain mgr, LayerPositionConfig cfg, Span pixels) { - var (shiftX, shiftY) = cfg.GetCoordinatesAbsolute(0, 0); + var (shiftX, shiftY) = cfg.GetCoordinatesAbsolute(); // Iterate through the relative positions within the layer. // Then, map to absolute positions in the bitmap with the configured shift. @@ -238,7 +290,7 @@ private static void GetViewTerrain1(LayerTerrain t, LayerPositionConfig cfg, int } } - private static void DrawViewTerrainTileNames(Graphics gfx, LayerTerrain t, LayerPositionConfig cfg, Font f, + private static void DrawViewTerrainTileNames(this Graphics gfx, LayerTerrain t, LayerPositionConfig cfg, Font f, int relX, int relY, int scale, byte transparency) { var pen = Tile; diff --git a/NHSE.WinForms/Subforms/Map/FieldItemEditor.cs b/NHSE.WinForms/Subforms/Map/FieldItemEditor.cs index 49fd751..b131f1e 100644 --- a/NHSE.WinForms/Subforms/Map/FieldItemEditor.cs +++ b/NHSE.WinForms/Subforms/Map/FieldItemEditor.cs @@ -328,15 +328,15 @@ private bool GetTile(MouseEventArgs e, LayerFieldItem layerField, [NotNullWhen(t private bool GetTile(LayerFieldItem layerField, int absX, int absY, [NotNullWhen(true)] out TileCheck? item) { var cfg = Editor.Mutator.Manager.ConfigItems; - if (!cfg.IsCoordinateValidAbsolute(absX, absY)) + var (relX, relY) = cfg.GetCoordinatesRelative(absX, absY); + if (!cfg.IsCoordinateValidRelative(relX, relY)) { item = null; return false; } - var rel = cfg.GetCoordinatesRelative(absX, absY); - var tile = layerField.GetTile(rel.X, rel.Y); - item = new TileCheck(tile, absX, absY, rel.X, rel.Y); + var tile = layerField.GetTile(relX, relY); + item = new TileCheck(tile, absX, absY, relX, relY); return true; } @@ -350,15 +350,15 @@ private bool GetTile(MouseEventArgs e, LayerTerrain layerField, [NotNullWhen(tru private bool GetTile(LayerTerrain layerField, int absX, int absY, [NotNullWhen(true)] out TileCheck? item) { var cfg = Editor.Mutator.Manager.ConfigItems; - if (!cfg.IsCoordinateValidAbsolute(absX, absY)) + var (relX, relY) = cfg.GetCoordinatesRelative(absX, absY); + if (!cfg.IsCoordinateValidRelative(relX, relY)) { item = null; return false; } - var rel = cfg.GetCoordinatesRelative(absX, absY); - var tile = layerField.GetTile(rel.X, rel.Y); - item = new TileCheck(tile, absX, absY, rel.X, rel.Y); + var tile = layerField.GetTile(relX, relY); + item = new TileCheck(tile, absX, absY, relX, relY); return true; } @@ -644,13 +644,13 @@ private void Menu_View_Click(object sender, EventArgs e) { var (absX, absY) = GetAbsoluteCoordinatesHover(); var cfg = Editor.Mutator.Manager.ConfigItems; - if (!cfg.IsCoordinateValidAbsolute(absX, absY)) + var (relX, relY) = cfg.GetCoordinatesRelative(absX, absY); + if (!cfg.IsCoordinateValidRelative(relX, relY)) { System.Media.SystemSounds.Asterisk.Play(); return; } - var (relX, relY) = cfg.GetCoordinatesRelative(absX, absY); if (RB_Item.Checked) { var tile = CurrentLayer.GetTile(relX, relY); @@ -669,12 +669,12 @@ private void Menu_Set_Click(object sender, EventArgs e) { var (absX, absY) = GetAbsoluteCoordinatesHover(); var cfg = Editor.Mutator.Manager.ConfigItems; - if (!cfg.IsCoordinateValidAbsolute(absX, absY)) + var (relX, relY) = cfg.GetCoordinatesRelative(absX, absY); + if (!cfg.IsCoordinateValidRelative(relX, relY)) { System.Media.SystemSounds.Asterisk.Play(); return; } - var (relX, relY) = cfg.GetCoordinatesRelative(absX, absY); var tile = CurrentLayer.GetTile(relX, relY); SetTile(tile, relX, relY); @@ -683,13 +683,13 @@ private void Menu_Set_Click(object sender, EventArgs e) { var (absX, absY) = GetAbsoluteCoordinatesHoverTerrain(); var cfg = Editor.Mutator.Manager.ConfigTerrain; - if (!cfg.IsCoordinateValidAbsolute(absX, absY)) + var (relX, relY) = cfg.GetCoordinatesRelative(absX, absY); + if (!cfg.IsCoordinateValidRelative(relX, relY)) { System.Media.SystemSounds.Asterisk.Play(); return; } - var (relX, relY) = cfg.GetCoordinatesRelative(absX, absY); var tile = Editor.Terrain.GetTile(relX, relY); SetTile(tile); } @@ -701,12 +701,12 @@ private void Menu_Reset_Click(object sender, EventArgs e) { var (absX, absY) = GetAbsoluteCoordinatesHover(); var cfg = Editor.Mutator.Manager.ConfigItems; - if (!cfg.IsCoordinateValidAbsolute(absX, absY)) + var (relX, relY) = cfg.GetCoordinatesRelative(absX, absY); + if (!cfg.IsCoordinateValidRelative(relX, relY)) { System.Media.SystemSounds.Asterisk.Play(); return; } - var (relX, relY) = cfg.GetCoordinatesRelative(absX, absY); var tile = CurrentLayer.GetTile(relX, relY); DeleteTile(tile, relX, relY); @@ -715,13 +715,13 @@ private void Menu_Reset_Click(object sender, EventArgs e) { var (absX, absY) = GetAbsoluteCoordinatesHoverTerrain(); var cfg = Editor.Mutator.Manager.ConfigTerrain; - if (!cfg.IsCoordinateValidAbsolute(absX, absY)) + var (relX, relY) = cfg.GetCoordinatesRelative(absX, absY); + if (!cfg.IsCoordinateValidRelative(relX, relY)) { System.Media.SystemSounds.Asterisk.Play(); return; } - var (relX, relY) = cfg.GetCoordinatesRelative(absX, absY); var tile = Editor.Terrain.GetTile(relX, relY); DeleteTile(tile); } @@ -739,10 +739,10 @@ private void CM_Click_Opening(object sender, System.ComponentModel.CancelEventAr var (absX, absY) = GetAbsoluteCoordinatesHover(); var cfg = Editor.Mutator.Manager.ConfigItems; - if (!cfg.IsCoordinateValidAbsolute(absX, absY)) + var (relX, relY) = cfg.GetCoordinatesRelative(absX, absY); + if (!cfg.IsCoordinateValidRelative(relX, relY)) return; - var (relX, relY) = cfg.GetCoordinatesRelative(absX, absY); var flagLayer = NUD_Layer.Value == 0 ? Map.LayerItemFlag0 : Map.LayerItemFlag1; var isActive = flagLayer.GetIsActive(relX, relY); Menu_Activate.Text = isActive ? "Inactivate" : "Activate"; @@ -754,10 +754,10 @@ private void Menu_Activate_Click(object sender, EventArgs e) { var (absX, absY) = GetAbsoluteCoordinatesHover(); var cfg = Editor.Mutator.Manager.ConfigItems; - if (!cfg.IsCoordinateValidAbsolute(absX, absY)) + var (relX, relY) = cfg.GetCoordinatesRelative(absX, absY); + if (!cfg.IsCoordinateValidRelative(relX, relY)) return; - var (relX, relY) = cfg.GetCoordinatesRelative(absX, absY); var flagLayer = NUD_Layer.Value == 0 ? Map.LayerItemFlag0 : Map.LayerItemFlag1; var isActive = flagLayer.GetIsActive(relX, relY); flagLayer.SetIsActive(relX, relY, !isActive);