From adb1d7a90ce268a2cbc965b608ff1d2aa3e43d62 Mon Sep 17 00:00:00 2001 From: Treer Date: Thu, 21 Mar 2024 00:18:28 +1100 Subject: [PATCH] refactor TileManager.TileServer property to have a Godot-accessible mirror Since https://github.com/godotengine/godot/issues/67167 is complete but wasn't the only issue - I get the impression naked C# interfaces will never be GDscript-accessible, only GodotObject subclasses will be accessible in GDscript --- WorldWanderer/scenes/mapview/mapview.gd | 6 ++--- WorldWanderer/src/GITileServer.cs | 30 +++++++++++++++++++++++ WorldWanderer/src/TileManager.cs | 32 ++++++++----------------- 3 files changed, 43 insertions(+), 25 deletions(-) create mode 100644 WorldWanderer/src/GITileServer.cs diff --git a/WorldWanderer/scenes/mapview/mapview.gd b/WorldWanderer/scenes/mapview/mapview.gd index ec99c44..40dc762 100644 --- a/WorldWanderer/scenes/mapview/mapview.gd +++ b/WorldWanderer/scenes/mapview/mapview.gd @@ -203,7 +203,7 @@ func get_zoom_scale() -> float: zoomed_scale = 1 + zoom * (max_zoom_in_factor - 1) # increase the zoom amount by the scale of the tiles, so we keep the same max and min # number of tiles onscreen regardless of how many kms a tile actually represents - var tile_scale = $TileManager.TileScale() + var tile_scale = $TileManager.tile_server.Scale return zoomed_scale / tile_scale @@ -221,7 +221,7 @@ func scroll_by_window_size(direction: Vector2): $ParallaxBackground.scroll_offset += $ParallaxBackground/ParallaxLayer.get_viewport_rect().size / $ParallaxBackground.scale * direction * -1 func scroll_by_tile_size(direction: Vector2): - $ParallaxBackground.scroll_offset += $TileManager.TileLength() * direction * -1 + $ParallaxBackground.scroll_offset += $TileManager.tile_server.TileLength * direction * -1 func display_position_information(map_coords_pos: Vector2): var posInfo = $TileManager.GetPositionInformation(map_coords_pos) @@ -276,7 +276,7 @@ func on_screenshot_requested(): $SaveFileDialog.current_path = suggested_path # Get a filename-suitable description of the Tile source to append to the suggested filename - var suffix = $TileManager.DiagnosticFilenameSuffix() + var suffix = $TileManager.tile_server.DiagnosticFilenameSuffix var illegalCharsRegex = RegEx.new() illegalCharsRegex.compile("[:/\\?*\"|%<>]") # The chars that is_valid_filename() will fail suffix = illegalCharsRegex.sub(suffix, "", true) diff --git a/WorldWanderer/src/GITileServer.cs b/WorldWanderer/src/GITileServer.cs new file mode 100644 index 0000000..a5eb254 --- /dev/null +++ b/WorldWanderer/src/GITileServer.cs @@ -0,0 +1,30 @@ +// Copyright 2024 Treer (https://github.com/Treer) +// License: MIT, see LICENSE.txt for rights granted + +using Godot; +using MapGen.Tiles; +using System; +using System.Threading.Tasks; + +namespace MapViewer +{ + + /// + /// Wraps a GodotObject around an ITileServer to allow Godot to access the methods and properties exposed by the interface + /// + public partial class GITileServer : GodotObject, ITileServer // implements ITileServer so the compiler will ensure it exposes everything in the interface and is correct + { + public ITileServer WrappedITileServer { get; set; } + + public int TileLength => WrappedITileServer.TileLength; + public int TileResolution => WrappedITileServer.TileResolution; + public float Scale => WrappedITileServer.Scale; + public Type TileType => WrappedITileServer.TileType; + public string DiagnosticFilenameSuffix => WrappedITileServer.DiagnosticFilenameSuffix; + public ITileRender2D Render2D => WrappedITileServer.Render2D; + public Task GetTile(Vector2 worldCoord) => WrappedITileServer.GetTile(worldCoord); + public Vector2I TileContainingWorldCoord(Vector2 worldCoord) => WrappedITileServer.TileContainingWorldCoord(worldCoord); + + public GITileServer(ITileServer tileServer) { WrappedITileServer = tileServer; } + } +} diff --git a/WorldWanderer/src/TileManager.cs b/WorldWanderer/src/TileManager.cs index e312bd5..a95ea03 100644 --- a/WorldWanderer/src/TileManager.cs +++ b/WorldWanderer/src/TileManager.cs @@ -1,4 +1,4 @@ -// Copyright 2023 Treer (https://github.com/Treer) +// Copyright 2024 Treer (https://github.com/Treer) // License: MIT, see LICENSE.txt for rights granted using Godot; @@ -74,6 +74,7 @@ public Vector2 OffscreenColumnsAndRows { [Signal] public delegate void tileserver_changedEventHandler(int oldServer, int newServer); + /// public ITileServer TileServer { get { return _tileServer; } private set { @@ -81,11 +82,18 @@ private set { // Unload all the tiles first AddOrRemoveRows(true, -TileRowList.Count); // Remove all the rows, rather than columns, as code assumes rows have at least one column _tileServer = value; + _godotTileServer.WrappedITileServer = value; GD.Print($"Set _tileServer to {value.GetType().Name}"); updateRequired = true; } } + /// + /// A Godot-accessible mirror of the property + /// + public GITileServer tile_server => _godotTileServer; + + /// /// Provides GDScript with a way to get and set the current TileServer's configuration values /// The ConfigFile may contain two optional sections, 'Menu' and 'Console', for checkbox menu-items and console variables respectively. @@ -109,27 +117,6 @@ private set { /// The section name for values in TileServerConfig that can be set via console commands public const string TileServerConfigSection_Console = "Console"; - // TODO: Change TileLength to a getter once Godot can handle read-only properties https://github.com/godotengine/godot/issues/67167 - /// Width/Height of the tile in the world coordinate system. - public int TileLength() => - // expose TileLength to GDScript, since it can't use an ITileServer - TileServer?.TileLength ?? 0; - - // TODO: Change TileLength to a getter once Godot can handle read-only properties https://github.com/godotengine/godot/issues/67167 - /// The size of a datapoint in the world coordinate system. Equal to TileLength / TileResolution - public float TileScale() => - // expose TileLength to GDScript, since it can't use an ITileServer - TileServer?.Scale ?? 1f; - - // TODO: Change TileLength to a getter once Godot can handle read-only properties https://github.com/godotengine/godot/issues/67167 - /// - /// Gets a string that can be appended to e.g. the end of screenshot filenames. It might be empty, or just the name of - /// the TileServer, or a compact representation of the TileServer configuration which generated the screencaptured tiles. - /// - public string DiagnosticFilenameSuffix() => - // expose DiagnosticFilenameSuffix to GDScript, since it can't use an ITileServer - TileServer?.DiagnosticFilenameSuffix ?? ""; - /// /// should normally be false, set true for debugging. /// Not sure this is properly implemented yet. @@ -163,6 +150,7 @@ public void RerenderAllTiles() { private List> _tileRowList = new List>(); private Dictionary _availableTileServers = new Dictionary(); private ITileServer _tileServer = testTileServer; + private GITileServer _godotTileServer = new GITileServer(testTileServer); private Dictionary sprites = new Dictionary(); private List<(IDeferredTile, Texture2D)> newlyGeneratedTiles = new List<(IDeferredTile, Texture2D)>();