Skip to content

Commit 1d7de01

Browse files
hry-ghwixoaGit
andauthored
diff verbs instead of recreating every time (#2545)
Co-authored-by: harryob <55142896+harryob@users.noreply.github.com> Co-authored-by: wixoa <wixoag@gmail.com>
1 parent fc4c892 commit 1d7de01

2 files changed

Lines changed: 51 additions & 22 deletions

File tree

OpenDreamClient/Interface/Controls/ControlInfo.cs

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ internal sealed class VerbPanel : InfoPanel {
183183
private readonly ClientVerbSystem? _verbSystem;
184184

185185
private readonly VerbPanelGrid _grid;
186+
private readonly Dictionary<(int VerbId, ClientObjectReference Src), Button> _verbButtons = new();
186187

187188
private Color _highlightColor;
188189
private Color _textColor;
@@ -218,35 +219,52 @@ public override void UpdateElementDescriptor(ControlDescriptorInfo descriptor) {
218219
}
219220

220221
public void RefreshVerbs(IEnumerable<(int, ClientObjectReference, VerbSystem.VerbInfo)> verbs) {
221-
_grid.Children.Clear();
222+
var panelVerbs = verbs
223+
.Where(v => v.Item3.GetCategoryOrDefault(DefaultVerbPanel) == PanelName)
224+
.Order(VerbNameComparer.OrdinalInstance);
225+
226+
var seenKeys = new HashSet<(int, ClientObjectReference)>();
227+
var gridIndex = 0;
228+
foreach (var (verbId, src, verbInfo) in panelVerbs) {
229+
var key = (verbId, src);
230+
seenKeys.Add(key);
231+
232+
if (!_verbButtons.ContainsKey(key)) {
233+
var verbButton = new Button {
234+
Margin = new Thickness(2),
235+
Text = verbInfo.Name,
236+
TextAlign = Label.AlignMode.Center
237+
};
238+
239+
verbButton.Label.Margin = new Thickness(6, 0, 6, 2);
240+
verbButton.Label.FontColorOverride = _textColor;
241+
verbButton.StyleBoxOverride = new StyleBoxEmpty();
222242

223-
foreach (var (verbId, src, verbInfo) in verbs.Order(VerbNameComparer.OrdinalInstance)) {
224-
if (verbInfo.GetCategoryOrDefault(DefaultVerbPanel) != PanelName)
225-
continue;
243+
verbButton.OnButtonDown += _ => {
244+
_verbSystem?.ExecuteVerb(src, verbId);
245+
};
226246

227-
Button verbButton = new Button {
228-
Margin = new Thickness(2),
229-
Text = verbInfo.Name,
230-
TextAlign = Label.AlignMode.Center
231-
};
247+
verbButton.OnMouseEntered += _ => {
248+
verbButton.Label.FontColorOverride = _highlightColor;
249+
};
232250

233-
verbButton.Label.Margin = new Thickness(6, 0, 6, 2);
234-
verbButton.Label.FontColorOverride = _textColor;
235-
verbButton.StyleBoxOverride = new StyleBoxEmpty();
251+
verbButton.OnMouseExited += _ => {
252+
verbButton.Label.FontColorOverride = _textColor;
253+
};
236254

237-
verbButton.OnButtonDown += _ => {
238-
_verbSystem?.ExecuteVerb(src, verbId);
239-
};
255+
_verbButtons[key] = verbButton;
256+
_grid.AddChild(verbButton);
257+
verbButton.SetPositionInParent(gridIndex);
258+
}
240259

241-
verbButton.OnMouseEntered += _ => {
242-
verbButton.Label.FontColorOverride = _highlightColor;
243-
};
260+
gridIndex++;
261+
}
244262

245-
verbButton.OnMouseExited += _ => {
246-
verbButton.Label.FontColorOverride = _textColor;
247-
};
263+
foreach (var key in _verbButtons.Keys) {
264+
if (seenKeys.Contains(key)) continue;
248265

249-
_grid.Children.Add(verbButton);
266+
_grid.RemoveChild(_verbButtons[key]);
267+
_verbButtons.Remove(key);
250268
}
251269
}
252270
}

TestGame/code.dm

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,17 @@
206206
verb/roll_dice(dice as text)
207207
var/result = roll(dice)
208208
usr << "The total shown on the dice is: [result]"
209+
210+
verb/give_other_verb()
211+
usr.verbs += /mob/proc/given_other_verb
212+
usr << "other verb given"
213+
214+
verb/take_other_verb()
215+
usr.verbs -= /mob/proc/given_other_verb
216+
usr << "other verb taken"
217+
218+
proc/given_other_verb()
219+
usr << "other verb working"
209220

210221
verb/test_alert()
211222
set category = "Test"

0 commit comments

Comments
 (0)