-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImWPF.cs
279 lines (239 loc) · 9.56 KB
/
ImWPF.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
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using CmplTime = System.Runtime.CompilerServices;
using Wpf = System.Windows;
using WpfElement = System.Windows.UIElement;
using WpfCtrls = System.Windows.Controls;
using WpfElementList = System.Windows.Controls.UIElementCollection;
using WpfDocs = System.Windows.Documents;
using WpfHandler = System.Windows.RoutedEventHandler;
namespace ImWpf
{
public static class CompileTime
{
public static string ID(
[CmplTime.CallerFilePath] string srcFilePath = "",
[CmplTime.CallerLineNumber] int srcLineNumber = 0)
{
return srcFilePath + srcLineNumber;
}
}
public class ImFormsMgr
{
public class ImElement
{
public enum State
{
NotDrawn,
Drawn
}
public readonly WpfElement WpfElement;
public State ImState { get; set; }
public int SortKey { get; set; }
public string ID { get { return WpfElement.Uid; } }
public WpfHandler Handler;
public WpfUnsubber Unsubber = (_, __) => { };
public ImElement(WpfElement wpfElement, WpfHandler del)
{
WpfElement = wpfElement;
Handler = del;
}
}
public delegate void WpfUnsubber(WpfElement element, WpfHandler handler);
public struct ElmtMakerTuple
{
public readonly WpfElement WpfElement;
public readonly WpfHandler Handler;
public readonly WpfUnsubber Unsubber;
public ElmtMakerTuple(WpfElement wpfElement, WpfHandler handler, WpfUnsubber unsubber)
{
WpfElement = wpfElement;
Handler = handler;
Unsubber = unsubber;
}
public static ElmtMakerTuple WithNoEvtHandling(WpfElement wpfElement)
{
return new ElmtMakerTuple(wpfElement, null, null);
}
}
public delegate ElmtMakerTuple imFormsElmtMaker(string id);
private int RemainingRedraws = 0;
private TaskCompletionSource<bool> TCS;
private Dictionary<string, ImElement> ImElements;
public WpfElementList DisplayedElements;
private int CurrentSortKey;
private string InteractedElementId;
// OH NOTE This could be configurable by the user in the _distant_ future
private int RedrawsPerInteraction = 1;
public ImFormsMgr(WpfElementList collection)
{
InteractedElementId = null;
ImElements = new Dictionary<string, ImElement>();
TCS = new TaskCompletionSource<bool>();
CurrentSortKey = 0;
DisplayedElements = collection;
}
public void QueueRedraws(int numRedraws) { RemainingRedraws += numRedraws; }
public ElmtMakerTuple ButtonLikeMaker<TBtn>(string id)
where TBtn : WpfCtrls.Primitives.ButtonBase, new()
{
var btn = new TBtn() { Uid = id };
var handler = new WpfHandler(LetImGuiHandleIt);
btn.Click += handler;
return new ElmtMakerTuple(btn, handler,
(btn1, hndl1) => ((TBtn)btn1).Click -= hndl1); // closure-free lambda
}
public WpfDocs.Hyperlink TryGetHyperlink(WpfElement wpfElement)
{
var tb = (wpfElement as WpfCtrls.TextBlock);
if (tb != null)
{
var firstInline = tb.Inlines.FirstInline;
if (firstInline != null)
{
return firstInline as WpfDocs.Hyperlink;
}
}
return null;
}
public void UnsubLinkTextBlock(WpfElement element, WpfHandler handler)
{
TryGetHyperlink(element).Click -= handler;
}
public ElmtMakerTuple LinkTextBlockMaker(string id)
{
var link = new WpfDocs.Hyperlink(new WpfDocs.Run());
// Easiest way to do this is to capture id in a closure
WpfCtrls.TextBlock linktxt = new WpfCtrls.TextBlock(link) { Uid = id };
WpfHandler handler = (o, e) =>
{
InteractedElementId = id;
QueueRedraws(RedrawsPerInteraction);
TCS.SetResult(true);
};
link.Click += handler;
return new ElmtMakerTuple(linktxt, handler, UnsubLinkTextBlock);
}
public void LetImGuiHandleIt(object sender, Wpf.RoutedEventArgs args)
{
InteractedElementId = ((WpfCtrls.Control)sender).Uid;
QueueRedraws(RedrawsPerInteraction);
TCS.SetResult(true);
}
public ImElement ProcureElement(string id, imFormsElmtMaker maker)
{
ImElement elmt;
if (!ImElements.TryGetValue(id, out elmt))
{
ElmtMakerTuple t = maker(id);
elmt = new ImElement(t.WpfElement, t.Handler);
ImElements.Add(id, elmt);
}
elmt.ImState = ImElement.State.Drawn;
elmt.SortKey = CurrentSortKey;
elmt.WpfElement.Visibility = Wpf.Visibility.Visible ;
CurrentSortKey++;
return elmt;
}
public void Space(string id)
{
Text("", id);
}
public void Text(string text, string id = null)
{
if (id == null) id = text;
var elmt = ProcureElement(
id ?? text,
id1 => ElmtMakerTuple.WithNoEvtHandling(new WpfCtrls.TextBlock() { Uid = id }));
((WpfCtrls.TextBlock)elmt.WpfElement).Text = text;
}
public bool Button(string text, string id = null)
{
var elmt = ProcureElement(id ?? text, ButtonLikeMaker<WpfCtrls.Button>);
var Button = (WpfCtrls.Button)elmt.WpfElement;
Button.Content = text;
return InteractedElementId == elmt.ID;
}
public bool LinkText(string text, string id = null)
{
var elmt = ProcureElement(id ?? text, LinkTextBlockMaker);
var hyperlink = (WpfDocs.Hyperlink)((WpfCtrls.TextBlock)elmt.WpfElement).Inlines.FirstInline;
((WpfDocs.Run)hyperlink.Inlines.FirstInline).Text = text;
// NOTE: this works because the Hyperlink's event handler explicitly assigns id
return InteractedElementId == elmt.ID;
}
public bool Checkbox(string text, ref bool checkBoxChecked, string id = null)
{
var elmt = ProcureElement(id ?? text, ButtonLikeMaker<WpfCtrls.CheckBox>);
var checkBox = (WpfCtrls.CheckBox)elmt.WpfElement;
checkBox.Content = text;
var wasInteracted = InteractedElementId == elmt.ID;
if (wasInteracted) { checkBoxChecked = checkBox.IsChecked.GetValueOrDefault(); }
else { checkBox.IsChecked = checkBoxChecked; }
return wasInteracted;
}
public bool RadioButton(string text, ref int value, int checkAgainst, string id = null)
{
var elmt = ProcureElement(id ?? text, ButtonLikeMaker<WpfCtrls.RadioButton>);
var radioButton = (WpfCtrls.RadioButton)elmt.WpfElement;
radioButton.Content = text;
var wasInteracted = InteractedElementId == elmt.ID;
if (wasInteracted) { value = checkAgainst; }
else { radioButton.IsChecked = (value == checkAgainst); }
return wasInteracted;
}
public void Refresh()
{
TCS.TrySetResult(true);
}
public async Task NextFrame()
{
// PrevInteractedElement = InteractedElement;
const int elmtsToTriggerCleanup = 100;
const int elmtsToCleanup = 50;
var undrawnElements = ImElements.Values.Where(elmt => elmt.ImState == ImElement.State.NotDrawn)
.Take(elmtsToTriggerCleanup).ToList();
if (undrawnElements.Count == elmtsToTriggerCleanup)
{
foreach (var elmt in undrawnElements.Take(elmtsToCleanup))
{
elmt.Unsubber(elmt.WpfElement, elmt.Handler);
ImElements.Remove(elmt.ID);
}
}
InteractedElementId = null;
WpfElement[] sortedWpfElements = ImElements.Values.Where(x => x.ImState == ImElement.State.Drawn)
.OrderBy(c => c.SortKey).Select(c => c.WpfElement).ToArray();
var wpfElementsChanged = DisplayedElements.Count != sortedWpfElements.Length
|| !Enumerable.Zip(
DisplayedElements.OfType<WpfElement>(),
sortedWpfElements,
(c1, c2) => c1 == c2).All(b => b);
if (wpfElementsChanged)
{
DisplayedElements.Clear();
foreach (var item in sortedWpfElements)
{
DisplayedElements.Add(item);
}
}
// Automatically go to next frame for each requested redraw
if (RemainingRedraws <= 0)
{
RemainingRedraws = 0;
await TCS.Task;
TCS = new TaskCompletionSource<bool>();
}
else
{
RemainingRedraws--;
}
foreach (var elmt in ImElements.Values)
{
elmt.ImState = ImElement.State.NotDrawn;
elmt.SortKey = 999999;
}
}
}
}