Skip to content

Commit 2e1bf97

Browse files
committed
feat: add IIdentifiable interface and IdentifiableResource class for unique identification
1 parent e11fb92 commit 2e1bf97

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

src/features/Common/IIdentifiable.cs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using Godot;
2+
3+
namespace DiceRolling.Common;
4+
5+
public interface IIdentifiable {
6+
string Id { get; }
7+
Callable GenerateNewIdButton { get; }
8+
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uid://dmmxpye74ucdn
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Godot;
2+
using DiceRolling.Services;
3+
4+
namespace DiceRolling.Common;
5+
6+
[Tool]
7+
public abstract partial class IdentifiableResource : Resource, IIdentifiable {
8+
[ExportGroup("🆔 Identification")]
9+
[Export] public string Id { get; private set; } = IdService.GenerateNewId();
10+
[ExportToolButton("Generate Id")] public Callable GenerateNewIdButton => Callable.From(GenerateNewId);
11+
12+
public void GenerateNewId() {
13+
Id = IdService.GenerateNewId();
14+
}
15+
16+
protected void EnsureValidId() {
17+
if (string.IsNullOrEmpty(Id)) {
18+
GenerateNewId();
19+
}
20+
}
21+
22+
public override void _ValidateProperty(Godot.Collections.Dictionary property) {
23+
if (property["name"].AsStringName() == "Id") {
24+
var usage = property["usage"].As<PropertyUsageFlags>() | PropertyUsageFlags.ReadOnly;
25+
property["usage"] = (int)usage;
26+
}
27+
base._ValidateProperty(property);
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uid://c4qkr3mvpu6tl

0 commit comments

Comments
 (0)