-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSampleVirtualCursor.cs
More file actions
300 lines (266 loc) · 9.51 KB
/
SampleVirtualCursor.cs
File metadata and controls
300 lines (266 loc) · 9.51 KB
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
using FishUI;
using FishUI.Controls;
using System;
using System.Numerics;
namespace FishUIDemos
{
/// <summary>
/// Demonstrates the virtual cursor/mouse functionality for keyboard/gamepad UI navigation.
/// Use Arrow keys to move cursor, Space/Enter for left click, Right Shift for right click.
/// </summary>
public class SampleVirtualCursor : ISample
{
FishUI.FishUI FUI;
IFishUIInput InputRef;
Label statusLabel;
Label positionLabel;
CheckBox enabledCheckbox;
Slider speedSlider;
/// <summary>
/// Display name of the sample.
/// </summary>
public string Name => "VirtualCursor";
/// <summary>
/// Action to take a screenshot, set by Program.cs.
/// </summary>
public TakeScreenshotFunc TakeScreenshot { get; set; }
public FishUI.FishUI CreateUI(FishUISettings UISettings, IFishUIGfx Gfx, IFishUIInput Input, IFishUIEvents Events)
{
FUI = new FishUI.FishUI(UISettings, Gfx, Input, Events);
FUI.Init();
InputRef = Input;
// Load theme
FishUITheme theme = UISettings.LoadTheme(ThemePreferences.LoadThemePath(), applyImmediately: true);
// Initialize virtual mouse at center of screen
FUI.VirtualMouse.Initialize(FUI.Width, FUI.Height);
FUI.VirtualMouse.Enabled = true;
FUI.VirtualMouse.DrawCursor = true;
FUI.VirtualMouse.CursorColor = new FishColor(255, 200, 50, 255);
FUI.VirtualMouse.Speed = 400f;
return FUI;
}
public void Init()
{
// Main panel
Panel mainPanel = new Panel();
mainPanel.Position = new Vector2(50, 50);
mainPanel.Size = new Vector2(400, 430);
FUI.AddControl(mainPanel);
// Title
Label titleLabel = new Label("Virtual Cursor Demo");
titleLabel.Position = new Vector2(20, 15);
titleLabel.Size = new Vector2(300, 20);
titleLabel.Alignment = Align.Left;
mainPanel.AddChild(titleLabel);
// Instructions
StaticText instructions = new StaticText(
"Controls:\n" +
"• Arrow Keys: Move cursor\n" +
"• Space/Enter: Left click\n" +
"• Right Shift: Right click\n" +
"Uncheck checkbox to use hybrid mode (follows real mouse)"
);
instructions.Position = new Vector2(20, 45);
instructions.Size = new Vector2(360, 90);
instructions.HorizontalAlignment = Align.Left;
instructions.VerticalAlignment = VerticalAlign.Top;
instructions.ShowBackground = true;
instructions.BackgroundColor = new FishColor(40, 40, 60, 180);
mainPanel.AddChild(instructions);
// Enabled checkbox - controls keyboard input vs hybrid mode
enabledCheckbox = new CheckBox("Keyboard Control (uncheck for Hybrid)");
enabledCheckbox.Position = new Vector2(20, 150);
enabledCheckbox.Size = new Vector2(15, 15);
enabledCheckbox.IsChecked = true;
mainPanel.AddChild(enabledCheckbox);
// Speed slider
Label speedLabel = new Label("Cursor Speed:");
speedLabel.Position = new Vector2(20, 185);
speedLabel.Size = new Vector2(100, 20);
speedLabel.Alignment = Align.Left;
mainPanel.AddChild(speedLabel);
speedSlider = new Slider();
speedSlider.Position = new Vector2(130, 185);
speedSlider.Size = new Vector2(200, 24);
speedSlider.MinValue = 100;
speedSlider.MaxValue = 800;
speedSlider.Value = 400;
speedSlider.ShowValueLabel = true;
speedSlider.OnValueChanged += (slider, val) =>
{
FUI.VirtualMouse.Speed = val;
};
mainPanel.AddChild(speedSlider);
// Cursor image selection
Label cursorLabel = new Label("Cursor Image:");
cursorLabel.Position = new Vector2(20, 215);
cursorLabel.Size = new Vector2(100, 20);
cursorLabel.Alignment = Align.Left;
mainPanel.AddChild(cursorLabel);
// Load cursor images
string[] cursorFiles = new string[]
{
"(Default)",
"arrow.png",
"Beam.png",
"Cursor_3.png",
"Cursor_4.png",
"Cursor_5.png",
"Cursor_6.png",
"help_win95.png"
};
DropDown cursorDropdown = new DropDown();
cursorDropdown.Position = new Vector2(130, 212);
cursorDropdown.Size = new Vector2(200, 24);
foreach (var cursor in cursorFiles)
{
cursorDropdown.AddItem(new DropDownItem(cursor));
}
cursorDropdown.SelectIndex(0);
cursorDropdown.OnItemSelected += (dd, item) =>
{
if (item.Text == "(Default)")
{
// Use default drawn cursor
FUI.VirtualMouse.CursorImage = null;
}
else
{
// Load cursor image
string path = $"data/images/cursors/{item.Text}";
ImageRef cursorImg = FUI.Graphics.LoadImage(path);
FUI.VirtualMouse.CursorImage = cursorImg;
}
};
mainPanel.AddChild(cursorDropdown);
// Position label
positionLabel = new Label("Position: (0, 0)");
positionLabel.Position = new Vector2(20, 250);
positionLabel.Size = new Vector2(300, 20);
positionLabel.Alignment = Align.Left;
mainPanel.AddChild(positionLabel);
// Status label
statusLabel = new Label("Status: Virtual cursor enabled");
statusLabel.Position = new Vector2(20, 275);
statusLabel.Size = new Vector2(300, 20);
statusLabel.Alignment = Align.Left;
mainPanel.AddChild(statusLabel);
// Test buttons
Label testLabel = new Label("Test Buttons (click with virtual cursor):");
testLabel.Position = new Vector2(20, 310);
testLabel.Size = new Vector2(300, 20);
testLabel.Alignment = Align.Left;
mainPanel.AddChild(testLabel);
Button btn1 = new Button();
btn1.Text = "Button 1";
btn1.Position = new Vector2(20, 335);
btn1.Size = new Vector2(100, 35);
btn1.OnButtonPressed += (ctrl, btn, pos) => statusLabel.Text = "Status: Button 1 clicked!";
mainPanel.AddChild(btn1);
Button btn2 = new Button();
btn2.Text = "Button 2";
btn2.Position = new Vector2(140, 335);
btn2.Size = new Vector2(100, 35);
btn2.OnButtonPressed += (ctrl, btn, pos) => statusLabel.Text = "Status: Button 2 clicked!";
mainPanel.AddChild(btn2);
Button btn3 = new Button();
btn3.Text = "Button 3";
btn3.Position = new Vector2(260, 335);
btn3.Size = new Vector2(100, 35);
btn3.OnButtonPressed += (ctrl, btn, pos) => statusLabel.Text = "Status: Button 3 clicked!";
mainPanel.AddChild(btn3);
// Checkbox test
CheckBox testCheckbox = new CheckBox("Checkbox Test");
testCheckbox.Position = new Vector2(20, 380);
testCheckbox.Size = new Vector2(15, 15);
mainPanel.AddChild(testCheckbox);
// Screenshot button
ImageRef iconCamera = FUI.Graphics.LoadImage("data/silk_icons/camera.png");
Button screenshotBtn = new Button();
screenshotBtn.Position = new Vector2(340, 10);
screenshotBtn.Size = new Vector2(24, 24);
screenshotBtn.Icon = iconCamera;
screenshotBtn.IsImageButton = true;
screenshotBtn.TooltipText = "Take screenshot";
screenshotBtn.OnButtonPressed += (ctrl, btn, pos) => TakeScreenshot?.Invoke(Name);
mainPanel.AddChild(screenshotBtn);
// Second panel for more interaction targets
Panel targetPanel = new Panel();
targetPanel.Position = new Vector2(500, 50);
targetPanel.Size = new Vector2(300, 200);
targetPanel.Variant = PanelVariant.Dark;
FUI.AddControl(targetPanel);
Label targetTitle = new Label("More Targets");
targetTitle.Position = new Vector2(20, 15);
targetTitle.Size = new Vector2(200, 20);
targetTitle.Alignment = Align.Left;
targetPanel.AddChild(targetTitle);
// Slider target
Slider targetSlider = new Slider();
targetSlider.Position = new Vector2(20, 50);
targetSlider.Size = new Vector2(200, 24);
targetSlider.MinValue = 0;
targetSlider.MaxValue = 100;
targetSlider.Value = 50;
targetSlider.ShowValueLabel = true;
targetSlider.OnValueChanged += (slider, val) =>
{
statusLabel.Text = $"Status: Slider value = {val:F0}";
};
targetPanel.AddChild(targetSlider);
// NumericUpDown target
NumericUpDown targetNumeric = new NumericUpDown(25, 0, 100, 5);
targetNumeric.Position = new Vector2(20, 90);
targetNumeric.Size = new Vector2(120, 24);
targetNumeric.OnValueChanged += (num, val) =>
{
statusLabel.Text = $"Status: NumericUpDown value = {val}";
};
targetPanel.AddChild(targetNumeric);
// ToggleSwitch target
ToggleSwitch targetToggle = new ToggleSwitch();
targetToggle.Position = new Vector2(20, 130);
targetToggle.OnToggleChanged += (toggle, isOn) =>
{
statusLabel.Text = $"Status: Toggle is {(isOn ? "ON" : "OFF")}";
};
targetPanel.AddChild(targetToggle);
Label toggleLabel = new Label("Toggle Switch");
toggleLabel.Position = new Vector2(75, 130);
toggleLabel.Size = new Vector2(150, 24);
toggleLabel.Alignment = Align.Left;
targetPanel.AddChild(toggleLabel);
}
/// <summary>
/// Called every frame. Handles hybrid mode - when checkbox is unchecked,
/// sync virtual cursor with real mouse position and clicks.
/// </summary>
public void Update(float dt)
{
// Update position label
positionLabel.Text = $"Position: ({FUI.VirtualMouse.Position.X:F0}, {FUI.VirtualMouse.Position.Y:F0})";
// Hybrid mode: when keyboard control is unchecked, follow real mouse position and clicks
if (!enabledCheckbox.IsChecked)
{
// Disable keyboard input handling in virtual mouse
FUI.VirtualMouse.UseKeyboardInput = false;
// Sync virtual cursor with real mouse position and button states
FUI.VirtualMouse.SyncWithRealMouse(InputRef);
FUI.VirtualMouse.SyncButtonsWithRealMouse(InputRef);
statusLabel.Text = "Status: Hybrid mode (following real mouse + clicks)";
}
else
{
// Enable keyboard input handling
FUI.VirtualMouse.UseKeyboardInput = true;
}
}
private void UpdateStatus()
{
if (FUI.VirtualMouse.Enabled)
statusLabel.Text = "Status: Virtual cursor enabled";
else
statusLabel.Text = "Status: Virtual cursor disabled (use real mouse)";
}
}
}