This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
OptionButtonDriver.cs
156 lines (135 loc) · 5.03 KB
/
OptionButtonDriver.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Godot;
using GodotTestDriver.Util;
using JetBrains.Annotations;
namespace GodotTestDriver.Drivers
{
/// <summary>
/// Driver for <see cref="OptionButton"/> controls.
/// </summary>
[PublicAPI]
public partial class OptionButtonDriver<T> : BaseButtonDriver<T> where T : OptionButton
{
public OptionButtonDriver(Func<T> producer, string description = "") : base(producer, description)
{
}
/// <summary>
/// Returns a list of all items in the option button.
/// </summary>
public IEnumerable<string> Items
{
get
{
var uiControl = PresentRoot;
for (var i = 0; i < uiControl.ItemCount; i++)
{
yield return uiControl.GetItemText(i);
}
}
}
/// <summary>
/// Returns a list of all items in the option button that are currently selectable (e.g. not disabled).
/// </summary>
public IEnumerable<string> SelectableItems
{
get
{
var uiControl = PresentRoot;
for (var i = 0; i < uiControl.ItemCount; i++)
{
if (!uiControl.IsItemDisabled(i) && !uiControl.IsItemSeparator(i))
{
yield return uiControl.GetItemText(i);
}
}
}
}
/// <summary>
/// Returns the currently selected item, or null if no item is selected.
/// </summary>
public string SelectedItem
{
get
{
var uiControl = PresentRoot;
return uiControl.Selected < 0 ? null : uiControl.GetItemText(uiControl.Selected);
}
}
/// <summary>
/// Returns the currently selected item index, or -1 if no item is selected.
/// </summary>
public int SelectedIndex => PresentRoot.Selected;
/// <summary>
/// Returns the currently selected item ID, or -1 if no item is selected.
/// </summary>
public int SelectedId => PresentRoot.Selected < 0 ? -1 : PresentRoot.GetItemId(PresentRoot.Selected);
/// <summary>
/// Returns the index of the item with the given text. Throws an exception if the item is not found or not selectable.
/// </summary>
private int IndexOf(string text)
{
var uiControl = VisibleRoot;
for (var i = 0; i < uiControl.ItemCount; i++)
{
if (uiControl.GetItemText(i) == text)
{
if (uiControl.IsItemDisabled(i) || uiControl.IsItemSeparator(i))
{
throw new InvalidOperationException(
ErrorMessage($"Item with text '{text}' is not selectable."));
}
return i;
}
}
throw new InvalidOperationException(
ErrorMessage($"Option button does not contain item with name '{text}'."));
}
/// <summary>
/// Selects an item with the given text.
/// </summary>
public async Task SelectItemWithText(string text)
{
var uiControl = VisibleRoot;
await uiControl.GetTree().NextFrame();
var index = IndexOf(text);
uiControl.Select(index);
// calling this function will not emit the signal so we need to do this ourselves
uiControl.EmitSignal(OptionButton.SignalName.ItemSelected, index);
await uiControl.GetTree().WaitForEvents();
}
/// <summary>
/// Selects an item with the given ID.
/// </summary>
public async Task SelectItemWithId(int id)
{
var uiControl = VisibleRoot;
await uiControl.GetTree().NextFrame();
for (var i = 0; i < uiControl.ItemCount; i++)
{
if (uiControl.GetItemId(i) != id)
{
continue;
}
if (uiControl.IsItemDisabled(i))
{
throw new InvalidOperationException(ErrorMessage($"Item with ID '{id}' is not selectable."));
}
uiControl.Select(i);
// calling this function will not emit the signal so we need to do this ourselves
uiControl.EmitSignal(OptionButton.SignalName.ItemSelected, i);
await uiControl.GetTree().WaitForEvents();
return;
}
throw new InvalidOperationException(ErrorMessage($"Option button does not contain item with ID '{id}'."));
}
}
[PublicAPI]
public sealed class OptionButtonDriver : OptionButtonDriver<OptionButton>
{
public OptionButtonDriver(Func<OptionButton> producer, string description = "") : base(producer, description)
{
}
}
}