Skip to content

Commit fefcabf

Browse files
author
Miguel Cartier
committed
feat(ui-service): add UiInstance struct to encapsulate presenter metadata
refactor(ui-service): replace LoadedPresenters property with GetLoadedPresenters method refactor(editor): auto-set instance address from addressable address
1 parent a445c63 commit fefcabf

File tree

3 files changed

+236
-105
lines changed

3 files changed

+236
-105
lines changed

Editor/UiConfigsEditor.cs

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public abstract class UiConfigsEditor<TSet> : Editor
6464
"UI Sets group multiple presenter instances that should be displayed together. " +
6565
"When a set is activated via UiService, all its presenters are loaded and shown simultaneously. " +
6666
"Presenters are loaded in the order listed (top to bottom).\n\n" +
67-
"You can add the same UI type multiple times with different instance addresses for multi-instance support.";
67+
"Each UI's instance address is automatically set to its Addressable address from the config.";
6868

6969
private Dictionary<string, string> _assetPathLookup;
7070
private List<string> _uiConfigsAddress;
@@ -258,14 +258,6 @@ private VisualElement CreateSetPresenterElement()
258258
dropdown.name = "ui-type-dropdown";
259259
container.Add(dropdown);
260260

261-
// Instance address field (optional)
262-
var instanceField = new TextField();
263-
instanceField.style.width = 120;
264-
instanceField.style.marginLeft = 5;
265-
instanceField.tooltip = "Optional instance address (leave empty for default instance)";
266-
instanceField.name = "instance-address-field";
267-
container.Add(instanceField);
268-
269261
// Delete button
270262
var deleteButton = new Button { text = "×" };
271263
deleteButton.style.width = 25;
@@ -335,8 +327,7 @@ private void BindSetPresenterElement(VisualElement element, int index, Serialize
335327
return;
336328

337329
var dropdown = element.Q<DropdownField>("ui-type-dropdown");
338-
var instanceField = element.Q<TextField>("instance-address-field");
339-
if (dropdown == null || instanceField == null)
330+
if (dropdown == null)
340331
return;
341332

342333
var entryProperty = uiEntriesProperty.GetArrayElementAtIndex(index);
@@ -371,14 +362,12 @@ private void BindSetPresenterElement(VisualElement element, int index, Serialize
371362
{
372363
// Unbind to prevent stale property references
373364
dropdown.Unbind();
374-
instanceField.Unbind();
375365

376366
// Set the current values
377367
dropdown.index = selectedIndex;
378-
instanceField.value = instanceAddressProperty.stringValue ?? string.Empty;
379368

380-
// Register callback to store type when changed
381-
dropdown.RegisterValueChangedCallback(evt =>
369+
// Register callback to store type and addressable address when changed
370+
dropdown.RegisterValueChangedCallback(_ =>
382371
{
383372
var newIndex = dropdown.index;
384373
if (newIndex >= 0 && newIndex < _uiConfigsAddress.Count)
@@ -387,25 +376,21 @@ private void BindSetPresenterElement(VisualElement element, int index, Serialize
387376
if (_uiTypesByAddress.TryGetValue(selectedAddress, out var selectedType))
388377
{
389378
typeNameProperty.stringValue = selectedType.AssemblyQualifiedName;
379+
// Use the addressable address as the instance address
380+
instanceAddressProperty.stringValue = selectedAddress;
390381
SaveSetChanges();
391382
}
392383
}
393384
});
394385

395-
// Register callback for instance address field
396-
instanceField.RegisterValueChangedCallback(evt =>
397-
{
398-
instanceAddressProperty.stringValue = evt.newValue ?? string.Empty;
399-
SaveSetChanges();
400-
});
401-
402386
// Set initial value if property is empty
403387
if (string.IsNullOrEmpty(typeNameProperty.stringValue) && selectedIndex < _uiConfigsAddress.Count)
404388
{
405389
var address = _uiConfigsAddress[selectedIndex];
406390
if (_uiTypesByAddress.TryGetValue(address, out var type))
407391
{
408392
typeNameProperty.stringValue = type.AssemblyQualifiedName;
393+
instanceAddressProperty.stringValue = address;
409394
serializedObject.ApplyModifiedProperties();
410395
}
411396
}
@@ -429,8 +414,8 @@ private void BindSetPresenterElement(VisualElement element, int index, Serialize
429414

430415
deleteButton.userData = clickHandler;
431416
deleteButton.RegisterCallback(clickHandler);
432-
}
433417
}
418+
}
434419

435420
private void OnPresenterItemsAdded(IEnumerable<int> indices, SerializedProperty uiEntriesProperty)
436421
{
@@ -451,7 +436,8 @@ private void OnPresenterItemsAdded(IEnumerable<int> indices, SerializedProperty
451436
var instanceAddressProperty = entryProperty.FindPropertyRelative(nameof(UiSetEntry.InstanceAddress));
452437

453438
typeNameProperty.stringValue = defaultType?.AssemblyQualifiedName ?? string.Empty;
454-
instanceAddressProperty.stringValue = string.Empty;
439+
// Use the addressable address as the instance address
440+
instanceAddressProperty.stringValue = defaultAddress;
455441
}
456442
}
457443

Runtime/IUiService.cs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,41 @@
88

99
namespace GameLovers.UiService
1010
{
11+
/// <summary>
12+
/// Represents a UI presenter instance with its type, address and presenter reference
13+
/// </summary>
14+
public readonly struct UiInstance
15+
{
16+
/// <summary>
17+
/// The type of the UI presenter
18+
/// </summary>
19+
public readonly Type Type;
20+
21+
/// <summary>
22+
/// The instance address (empty string for default/singleton instances)
23+
/// </summary>
24+
public readonly string Address;
25+
26+
/// <summary>
27+
/// The UI presenter reference
28+
/// </summary>
29+
public readonly UiPresenter Presenter;
30+
31+
public UiInstance(Type type, string address, UiPresenter presenter)
32+
{
33+
Type = type;
34+
Address = address;
35+
Presenter = presenter;
36+
}
37+
}
38+
1139
/// <summary>
1240
/// This service provides an abstraction layer to interact with the game's UI <seealso cref="UiPresenter"/>
1341
/// The Ui Service is organized by layers. The higher the layer the more close is to the camera viewport.
1442
/// Supports multiple instances of the same UI type through the UiInstanceId system.
1543
/// </summary>
1644
public interface IUiService
1745
{
18-
/// <summary>
19-
/// Gets a read-only dictionary of all Presenters currently loaded in memory by the UI service.
20-
/// Keys are UiInstanceId which contain both the Type and optional instance name.
21-
/// </summary>
22-
IReadOnlyDictionary<UiInstanceId, UiPresenter> LoadedPresenters { get; }
2346

2447
/// <summary>
2548
/// Gets a read-only list of all Presenter instances currently visible.
@@ -32,6 +55,12 @@ public interface IUiService
3255
/// </summary>
3356
IReadOnlyDictionary<int, UiSetConfig> UiSets { get; }
3457

58+
/// <summary>
59+
/// Gets all UI presenters currently loaded in memory by the UI service.
60+
/// </summary>
61+
/// <returns>A list of all loaded UI instances</returns>
62+
List<UiInstance> GetLoadedPresenters();
63+
3564
/// <summary>
3665
/// Requests the UI of given type <typeparamref name="T"/>
3766
/// </summary>

0 commit comments

Comments
 (0)