Skip to content

Commit

Permalink
refactor TileManager.TileServer property to have a Godot-accessible m…
Browse files Browse the repository at this point in the history
…irror

Since godotengine/godot#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
  • Loading branch information
Treer committed Mar 20, 2024
1 parent 6979c2a commit adb1d7a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 25 deletions.
6 changes: 3 additions & 3 deletions WorldWanderer/scenes/mapview/mapview.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions WorldWanderer/src/GITileServer.cs
Original file line number Diff line number Diff line change
@@ -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
{

/// <summary>
/// Wraps a GodotObject around an ITileServer to allow Godot to access the methods and properties exposed by the interface
/// </summary>
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<ITile> GetTile(Vector2 worldCoord) => WrappedITileServer.GetTile(worldCoord);
public Vector2I TileContainingWorldCoord(Vector2 worldCoord) => WrappedITileServer.TileContainingWorldCoord(worldCoord);

public GITileServer(ITileServer tileServer) { WrappedITileServer = tileServer; }
}
}
32 changes: 10 additions & 22 deletions WorldWanderer/src/TileManager.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -74,18 +74,26 @@ public Vector2 OffscreenColumnsAndRows {
[Signal]
public delegate void tileserver_changedEventHandler(int oldServer, int newServer);

/// <seealso cref="tile_server"/>
public ITileServer TileServer {
get { return _tileServer; }
private set {
if (value == null) throw new ArgumentNullException("TileServer");
// 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;
}
}

/// <summary>
/// A Godot-accessible mirror of the <see cref="TileServer"/> property
/// </summary>
public GITileServer tile_server => _godotTileServer;


/// <summary>
/// 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.
Expand All @@ -109,27 +117,6 @@ private set {
/// <summary>The section name for values in TileServerConfig that can be set via console commands</summary>
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
/// <summary>Width/Height of the tile in the world coordinate system.</summary>
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
/// <summary>The size of a datapoint in the world coordinate system. Equal to TileLength / TileResolution</summary>
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
/// <summary>
/// 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.
/// </summary>
public string DiagnosticFilenameSuffix() =>
// expose DiagnosticFilenameSuffix to GDScript, since it can't use an ITileServer
TileServer?.DiagnosticFilenameSuffix ?? "";

/// <summary>
/// should normally be false, set true for debugging.
/// Not sure this is properly implemented yet.
Expand Down Expand Up @@ -163,6 +150,7 @@ public void RerenderAllTiles() {
private List<List<ITile>> _tileRowList = new List<List<ITile>>();
private Dictionary<Type, string> _availableTileServers = new Dictionary<Type, string>();
private ITileServer _tileServer = testTileServer;
private GITileServer _godotTileServer = new GITileServer(testTileServer);

private Dictionary<ITile, Sprite2D> sprites = new Dictionary<ITile, Sprite2D>();
private List<(IDeferredTile, Texture2D)> newlyGeneratedTiles = new List<(IDeferredTile, Texture2D)>();
Expand Down

0 comments on commit adb1d7a

Please sign in to comment.