forked from Taurenkey/OtterGui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathItemSelector.cs
492 lines (403 loc) · 14.5 KB
/
ItemSelector.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
using Dalamud.Interface;
using ImGuiNET;
using OtterGui.Raii;
namespace OtterGui;
public class ItemSelector<T>
{
[Flags]
public enum Flags : byte
{
None = 0x00, // Just a list
Delete = 0x01, // Add a delete button for the current element (Trashcan), uses OnDelete
Add = 0x02, // Add an Add button to create a new empty element given a name (Plus), uses OnAdd
Import = 0x04, // Add an Import button to create a new element from the string in your clipboard (Clipboard), uses OnImport
Duplicate = 0x08, // Add a Duplicate button to create copy of the currently selected element given a name, uses OnDuplicate
Filter = 0x10, // Add a filter up top that filters the visible elements and uses Filtered.
Move = 0x20, // Items can be moved by drag and drop, uses OnMove.
Drop = 0x40, // Items can be dropped onto, uses OnDrop. Create Sources with CreateDropSource.
ButtonMask = 0x0F,
All = 0x7F,
}
// Initial setup.
public IList<T> Items;
private readonly Flags _flags;
private readonly byte _numButtons;
// Used by Filter
// Indices of the currently available items after Filtered.
protected readonly List<int> FilteredItems;
protected string Filter = string.Empty;
protected bool FilterDirty = true;
private int _lastSize;
// Used by Add, Duplicate and Import buttons
private string _newName = string.Empty;
// Used by Move and Drop
private object? _dragDropData;
// Labels
private readonly string _label = "##ItemSelector";
public string Label
{
get => _label;
init
{
_label = value;
DragDropLabel = $"{value}DragDrop";
MoveLabel = $"{value}Move";
}
}
public readonly string DragDropLabel = "##ItemSelectorDragDrop";
public readonly string MoveLabel = "##ItemSelectorMove";
public ItemSelector(IList<T> items, Flags flags = Flags.None)
{
Items = items;
_lastSize = Items.Count;
_flags = flags;
FilteredItems = Enumerable.Range(0, Items.Count).ToList();
_numButtons = (byte)(_flags & Flags.ButtonMask) switch
{
0x01 => 1,
0x02 => 1,
0x03 => 2,
0x04 => 1,
0x05 => 2,
0x06 => 2,
0x07 => 3,
0x08 => 1,
0x09 => 2,
0x0A => 2,
0x0B => 3,
0x0C => 3,
0x0D => 3,
0x0E => 3,
0x0F => 4,
_ => 0,
};
Label = "##ItemSelector";
SetCurrent(0);
}
public void CreateDropSource<TData>(TData data, string tooltip)
{
using var source = ImRaii.DragDropSource();
if (!source)
return;
_dragDropData = data;
ImGui.SetDragDropPayload(DragDropLabel, IntPtr.Zero, 0);
ImGui.TextUnformatted(tooltip);
}
public bool CreateDropTarget<TData>(Action<TData> action)
{
using var target = ImRaii.DragDropTarget();
if (!target)
return false;
if (!ImGuiUtil.IsDropping(DragDropLabel))
return false;
if (_dragDropData is not TData data)
return false;
action(data);
return true;
}
public bool CreateDropTarget<TData>(Func<TData, bool> func)
{
using var target = ImRaii.DragDropTarget();
if (!target)
return false;
if (!ImGuiUtil.IsDropping(DragDropLabel))
return false;
return _dragDropData is TData data && func(data);
}
public void SetFilterDirty()
=> FilterDirty = true;
// Selection
public int CurrentIdx;
public T? Current { get; set; }
protected void ClearCurrentSelection()
{
CurrentIdx = -1;
Current = default;
}
public void TryRestoreCurrent()
{
CurrentIdx = Current == null ? -1 : Items.IndexOf(Current);
if (CurrentIdx == -1)
Current = default;
}
private void SetCurrent(int idx)
{
if (idx < Items.Count)
{
CurrentIdx = idx;
Current = Items[idx];
}
else if (Items.Count > 0)
{
CurrentIdx = Items.Count - 1;
Current = Items.Last();
}
else
{
ClearCurrentSelection();
}
}
protected void SetCurrent(T item)
{
var idx = Items.IndexOf(item);
if (idx >= 0)
SetCurrent(idx);
}
public T? EnsureCurrent()
{
TryRestoreCurrent();
if (Current == null)
SetCurrent(0);
return Current;
}
// Customization points.
protected virtual bool OnDraw(int idx, out bool changes)
=> throw new NotImplementedException();
protected virtual bool Filtered(int idx)
=> throw new NotImplementedException();
protected virtual bool OnDelete(int idx)
=> throw new NotImplementedException();
protected virtual bool OnAdd(string name)
=> throw new NotImplementedException();
protected virtual bool OnMove(int idx1, int idx2)
=> throw new NotImplementedException();
protected virtual bool OnClipboardImport(string name, string data)
=> throw new NotImplementedException();
protected virtual bool OnDuplicate(string name, int idx)
=> throw new NotImplementedException();
protected virtual void OnDrop(object? data, int idx)
=> throw new NotImplementedException();
public event EventHandler<bool> ItemAdded;
public event EventHandler<bool> ItemDeleted;
public event EventHandler<bool> ItemSkipTriggered;
private void InternalDraw(int idx)
{
// Add a slight distance from the border so that the padding of a selectable fills the whole border.
ImGui.SetCursorPosX(ImGui.GetCursorPosX() + ImGui.GetStyle().FramePadding.X);
// Assume that OnDraw functions like a Selectable, if it returns true, select the value.
if (OnDraw(idx, out bool changes) && idx != CurrentIdx)
{
CurrentIdx = idx;
Current = Items[idx];
}
if (changes)
{
ItemSkipTriggered.Invoke(this, changes);
}
// If the ItemSelector supports Move, every item is a Move-DragDropSource. The data is the index of the dragged element.
if (_flags.HasFlag(Flags.Move))
{
using var source = ImRaii.DragDropSource();
if (source)
{
_dragDropData = idx;
ImGui.SetDragDropPayload(MoveLabel, IntPtr.Zero, 0);
ImGui.TextUnformatted($"Reordering {idx + 1}...");
}
}
// If the ItemSelector supports Move or Drop, every item is a DragDropTarget.
if ((_flags & (Flags.Move | Flags.Drop)) == Flags.None)
return;
using var target = ImRaii.DragDropTarget();
if (!target)
return;
// Handle drops.
if (ImGuiUtil.IsDropping(DragDropLabel))
{
OnDrop(_dragDropData, idx);
}
else if (ImGuiUtil.IsDropping(MoveLabel))
{
var oldIdx = (int)(_dragDropData ?? idx);
if (OnMove(oldIdx, idx) && oldIdx == CurrentIdx)
SetCurrent(idx);
}
}
private void DrawFilter(float width)
{
if (!_flags.HasFlag(Flags.Filter))
return;
var newFilter = Filter;
using var style = ImRaii.PushStyle(ImGuiStyleVar.FrameRounding, 0);
ImGui.SetNextItemWidth(width);
var enterPressed = ImGui.InputTextWithHint(string.Empty, "Filter...", ref newFilter, 64, ImGuiInputTextFlags.EnterReturnsTrue);
if (newFilter != Filter)
{
Filter = newFilter;
FilterDirty = true;
}
// Select the topmost item of the filtered list on enter.
if (enterPressed)
{
UpdateFilteredItems();
if (FilteredItems.Count > 0)
SetCurrent(FilteredItems.First());
}
style.Pop();
}
// Update filtered items whenever the size of the base collection changes
// or the filter was set to dirty for any reason.
private void UpdateFilteredItems()
{
if (_lastSize != Items.Count)
{
FilterDirty = true;
_lastSize = Items.Count;
}
if (!FilterDirty)
return;
FilteredItems.Clear();
for (var idx = 0; idx < Items.Count; ++idx)
{
if (!Filtered(idx))
FilteredItems.Add(idx);
}
FilterDirty = false;
}
private void DrawAddButton(float width)
{
const string newNamePopupAdd = "##NewNameAdd";
if (ImGui.Button(FontAwesomeIcon.Plus.ToIconString(), Vector2.UnitX * width))
ImGui.OpenPopup(newNamePopupAdd);
using var font = ImRaii.PushFont(UiBuilder.DefaultFont);
ImGuiUtil.HoverTooltip(AddButtonTooltip());
if (!OpenNameField(newNamePopupAdd, out var newName))
return;
if (OnAdd(newName))
{
TryRestoreCurrent();
SetFilterDirty();
ItemAdded?.Invoke(this, true);
}
}
private void DrawImportButton(float width)
{
const string newNamePopupImport = "##NewNameImport";
if (ImGui.Button(FontAwesomeIcon.Clipboard.ToIconString(), Vector2.UnitX * width))
ImGui.OpenPopup(newNamePopupImport);
using var font = ImRaii.PushFont(UiBuilder.DefaultFont);
ImGuiUtil.HoverTooltip("Import from Clipboard");
if (!OpenNameField(newNamePopupImport, out var newName))
return;
if (OnClipboardImport(newName, ImGuiUtil.GetClipboardText()))
{
TryRestoreCurrent();
SetFilterDirty();
}
}
// The popup to enter a name used by all Creation-buttons.
private bool OpenNameField(string popupName, out string newName)
{
newName = string.Empty;
if (ImGuiUtil.OpenNameField(popupName, ref _newName))
{
newName = _newName;
_newName = string.Empty;
return true;
}
return false;
}
private void DrawDuplicateButton(float width)
{
const string newNamePopupDuplicate = "##NewNameDuplicate";
if (ImGui.Button(FontAwesomeIcon.Clone.ToIconString(), Vector2.UnitX * width))
ImGui.OpenPopup(newNamePopupDuplicate);
using var font = ImRaii.PushFont(UiBuilder.DefaultFont);
ImGuiUtil.HoverTooltip("Duplicate Current Selection");
if (!OpenNameField(newNamePopupDuplicate, out var newName))
return;
if (OnDuplicate(newName, CurrentIdx))
{
TryRestoreCurrent();
SetFilterDirty();
}
}
protected virtual bool DeleteButtonEnabled()
=> ImGui.GetIO().KeyCtrl;
protected virtual string DeleteButtonTooltip()
=> "Delete Current Selection. Hold Control while clicking.";
protected virtual string AddButtonTooltip()
=> "Add new item.";
private void DrawDeleteButton(float width)
{
using var font = ImRaii.DefaultFont();
if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Trash.ToIconString(), new Vector2(width, 0), DeleteButtonTooltip(), !DeleteButtonEnabled(), true)
&& CurrentIdx >= 0
&& OnDelete(CurrentIdx))
{
FilterDirty = true;
SetCurrent(CurrentIdx > 0 ? CurrentIdx - 1 : CurrentIdx);
ItemDeleted?.Invoke(this, true);
}
}
private void DrawButtons(float width)
{
if (_numButtons == 0)
return;
ImGui.Dummy(new Vector2(0, 15f));
using var font = ImRaii.PushFont(UiBuilder.IconFont);
var buttonWidth = width / _numButtons;
if (_flags.HasFlag(Flags.Add))
{
DrawAddButton(buttonWidth);
ImGui.SameLine();
}
if (_flags.HasFlag(Flags.Import))
{
DrawImportButton(buttonWidth);
ImGui.SameLine();
}
if (_flags.HasFlag(Flags.Duplicate))
{
DrawDuplicateButton(buttonWidth);
ImGui.SameLine();
}
if (_flags.HasFlag(Flags.Delete))
{
DrawDeleteButton(buttonWidth);
ImGui.SameLine();
}
ImGui.NewLine();
}
public void Draw(float width)
{
using var id = ImRaii.PushId(Label);
using var style = ImRaii.PushStyle(ImGuiStyleVar.WindowPadding, Vector2.Zero);
using var group = ImRaii.Group();
using var child = ImRaii.Child(string.Empty, new Vector2(width, -ImGui.GetFrameHeight() -15f), true);
if (!child)
return;
style.Pop();
DrawFilter(width);
UpdateFilteredItems();
ImGuiClip.ClippedDraw(FilteredItems, InternalDraw, ImGui.GetTextLineHeightWithSpacing() + 0.25f);
style.Push(ImGuiStyleVar.FrameRounding, 0)
.Push(ImGuiStyleVar.WindowPadding, Vector2.Zero)
.Push(ImGuiStyleVar.ItemSpacing, Vector2.Zero);
child.Dispose();
DrawButtons(width);
}
}
public static class ItemDetailsWindow
{
public static void Draw(string label, Action drawHeader, Action drawDetails)
{
using var group = ImRaii.Group();
using var style = ImRaii.PushStyle(ImGuiStyleVar.WindowPadding, Vector2.Zero);
using var id = ImRaii.PushId(label);
using var child = ImRaii.Child(string.Empty, ImGui.GetContentRegionAvail(), true, ImGuiWindowFlags.MenuBar);
if (!child)
return;
style.Pop();
if (ImGui.BeginMenuBar())
{
drawHeader();
ImGui.EndMenuBar();
}
ImGui.Dummy(ImGui.GetStyle().WindowPadding * Vector2.UnitY);
ImGui.Dummy(ImGui.GetStyle().WindowPadding);
ImGui.SameLine();
using var detailGroup = ImRaii.Group();
drawDetails();
}
}