-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModEntry.cs
448 lines (403 loc) · 13.5 KB
/
ModEntry.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
using HarmonyLib;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewModdingAPI.Framework;
using StardewModdingAPI.Framework.Input;
using StardewModdingAPI.Mobile;
using StardewValley;
using StardewValley.GameData.HomeRenovations;
using StardewValley.Menus;
using StardewValley.Mods;
using System.Reflection;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace VirtualKeyboard;
public class ModEntry : Mod
{
public static ModEntry Instance { get; private set; }
Dictionary<int, List<KeyButton>> keysLookup = new();
List<KeyButton> keys = new();
bool isShowKeyboard = false;
KeyButton floatingKeyboard;
KeyboardConfig config;
public override void Entry(IModHelper helper)
{
Instance = this;
Helper.Events.GameLoop.UpdateTicked += OnGameUpdateTicked;
var harmony = new Harmony(typeof(ModEntry).FullName);
harmony.PatchAll();
}
void OnGameUpdateTicked(object? sender, UpdateTickedEventArgs e)
{
//check if game loaded asset
if (Game1.dialogueFont == null)
return;
if (floatingKeyboard == null)
InitKeyboard();
if (isShouldRenderThisFrame())
{
refreshAllButtonPosition();
}
TryRestoreBlockInputThisFrame();
}
void InitKeyboard()
{
if (floatingKeyboard != null)
return;
//setup
config = this.Helper.ReadConfig<KeyboardConfig>();
floatingKeyboard = AddButton("", "Keyboard", OnFloatingKeyboard_KeyDown, 0);
floatingKeyboard.onKeyUp = OnFloatingKeyboard_KeyUp;
floatingKeyboard.SetIcon("assets/togglebutton.png");
floatingKeyboard.SetSize((int)config.Size);
floatingKeyboard.opacityInOut = new(0f, 0f, .1f);
SetKeyboardPosition(config.Position);
//init keys
AddButtons(config.Layout1, 1);
AddButtons(config.Layout2, 2);
AddButtons(config.Layout3, 3);
refreshAllButtonPosition();
//set visable keys
ToggleKeys(false);
ToggleKeyboardPage(false);
CommandMobile.Init();
Helper.Events.Input.ButtonPressed += OnGame_ButtonPressed;
Helper.Events.Input.ButtonReleased += OnGame_ButtonReleased;
Helper.Events.Input.CursorMoved += OnCursorMoved;
Helper.Events.Display.RenderedStep += Display_RenderedStep;
Console.WriteLine("Done init keyboard");
}
private void Display_RenderedStep(object? sender, RenderedStepEventArgs e)
{
switch (e.Step)
{
case RenderSteps.Overlays:
RenderKeyboard(e.SpriteBatch);
break;
default:
break;
}
}
public static bool IsCompressedFormat(SurfaceFormat format)
{
switch (format)
{
case SurfaceFormat.Dxt1:
case SurfaceFormat.Dxt3:
case SurfaceFormat.Dxt5:
case SurfaceFormat.Dxt1SRgb:
case SurfaceFormat.Dxt3SRgb:
case SurfaceFormat.Dxt5SRgb:
case SurfaceFormat.RgbPvrtc2Bpp:
case SurfaceFormat.RgbPvrtc4Bpp:
case SurfaceFormat.RgbaPvrtc2Bpp:
case SurfaceFormat.RgbaPvrtc4Bpp:
case SurfaceFormat.RgbEtc1:
case SurfaceFormat.Dxt1a:
case SurfaceFormat.RgbaAtcExplicitAlpha:
case SurfaceFormat.RgbaAtcInterpolatedAlpha:
case SurfaceFormat.Rgb8Etc2:
case SurfaceFormat.Srgb8Etc2:
case SurfaceFormat.Rgb8A1Etc2:
case SurfaceFormat.Srgb8A1Etc2:
case SurfaceFormat.Rgba8Etc2:
case SurfaceFormat.SRgb8A8Etc2:
return true;
default:
return false;
}
}
DateTime lastRender = DateTime.Now;
void TryRestoreBlockInputThisFrame()
{
if (isBlockPlayerInputThisFrame)
{
blockFrameCountDown--;
if (blockFrameCountDown <= 0)
{
isBlockPlayerInputThisFrame = false;
SetBlockPlayerInput(false);
}
}
}
void RenderKeyboard(SpriteBatch b)
{
try
{
var now = DateTime.Now;
var deltaTime = now - lastRender;
lastRender = now;
if (isDontRenderThisFrame())
{
if (floatingKeyboard.enabled)
SetKeyboardActive(false);
}
else
{
if (!floatingKeyboard.enabled)
SetKeyboardActive(true);
foreach (var buttonLayoutPair in keysLookup)
{
var layoutHorizon = buttonLayoutPair.Key;
var buttons = buttonLayoutPair.Value;
foreach (var button in buttons)
{
if (!button.enabled)
continue;
//update
button.Draw(b, deltaTime);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
bool isDontRenderThisFrame()
{
//render alway
TitleMenu titleMenu = Game1.activeClickableMenu as TitleMenu;
if (titleMenu != null)
{
//Improve Skip Load Screen
if (titleMenu.birds.Count > 0)
titleMenu.skipToTitleButtons();
//check if it's on customize character, so we dont need render
if (TitleMenu.subMenu != null)
return true;
return false;
}
//dont render when active menu & player not ready
return !Context.IsPlayerFree && Game1.activeClickableMenu?.GetType() != typeof(KeyboardPage);
}
bool isShouldRenderThisFrame() => !isDontRenderThisFrame();
void refreshAllButtonPosition()
{
try
{
const int buttonGapX = 20;
const int buttonGapY = 12;
var startX = (int)config.Position.X;
var startY = (int)config.Position.Y;
var lastPosY = startY;
foreach (var buttonLayoutPair in keysLookup)
{
var layoutHorizon = buttonLayoutPair.Key;
var buttons = buttonLayoutPair.Value;
var lastPosX = startX;
foreach (var button in buttonLayoutPair.Value)
{
button.bounds.X = lastPosX;
button.bounds.Y = lastPosY;
lastPosX += button.bounds.Width + buttonGapX;
}
lastPosY += buttons[0].bounds.Height + buttonGapY;
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
void AddButtons(string[] keys, int layout)
{
foreach (var keyString in keys)
{
var data = keyString.Split(":");
bool isCommand = data.Length > 1;
if (isCommand)
{
AddButton(data[1], data[0], OnKeyButtonDown_Command, layout);
}
else
{
AddButton(keyString, keyString, OnKeyButtonDown, layout);
}
}
}
//bool isShowKeysHold = false;
DateTime _lastKeyDownToggleKeyboard;
bool isMoveKeyboard = false;
void OnCursorMoved(object? sender, CursorMovedEventArgs e)
{
var now = DateTime.Now;
var offset = now - _lastKeyDownToggleKeyboard;
const int startDragTime = 150;
if (floatingKeyboard.isHeldDown && offset.TotalMilliseconds >= startDragTime)
{
SetKeyboardPosition(e.NewPosition.GetScaledScreenPixels());
isMoveKeyboard = true;
}
}
bool isNeedToSaveConfig = false;
//screen position
void SetKeyboardPosition(Vector2 newScreenPos)
{
//protect keyboard overflow
var posNormalize = new Vector2(newScreenPos.X / Game1.viewport.Width, newScreenPos.Y / Game1.viewport.Height);
const int minPadding = 20;
var uiScreenSize = Game1.uiViewport.Size;
var newPos = new Vector2(uiScreenSize.Width * posNormalize.X, uiScreenSize.Height * posNormalize.Y);
var uiScreenSizeMax = new Vector2(uiScreenSize.Width - minPadding, uiScreenSize.Height - minPadding);
//make pos to center to move
newPos.X -= floatingKeyboard.bounds.Width / 2f;
newPos.Y -= floatingKeyboard.bounds.Height / 2f;
//Left Top Pivot
var iconSize = floatingKeyboard.bounds.Size;
if (newPos.X < minPadding)
newPos.X = minPadding;
else if (newPos.X + iconSize.X > uiScreenSizeMax.X)
newPos.X = uiScreenSizeMax.X - iconSize.X;
if (newPos.Y < minPadding)
newPos.Y = minPadding;
else if (newPos.Y + iconSize.Y > uiScreenSizeMax.Y)
newPos.Y = uiScreenSizeMax.Y - iconSize.Y;
//Right Bottom Pivot
config.Position = newPos;
isNeedToSaveConfig = true;
}
void OnFloatingKeyboard_KeyUp(KeyButton button)
{
if (!isMoveKeyboard)
{
isShowKeyboard = !isShowKeyboard;
ToggleKeys(isShowKeyboard);
ToggleKeyboardPage(isShowKeyboard);
}
if (isNeedToSaveConfig)
{
isNeedToSaveConfig = false;
Helper.WriteConfig(config);
}
isMoveKeyboard = false;
if (!isBlockPlayerInputThisFrame)
SetBlockPlayerInput(false);
}
void OnFloatingKeyboard_KeyDown(KeyButton keyButton)
{
_lastKeyDownToggleKeyboard = DateTime.Now;
SetBlockPlayerInput(true);
}
void OnKeyButtonDown(KeyButton button)
{
//disable Active Clickable Menu first
//mod CJB Item Spawner need IsPlayerFree == true;
SetBlockPlayerInputThisFrame();
SetKeyboardActive(false);
var sButton = (SButton)Enum.Parse(typeof(SButton), button.key);
MobileInputTool.OverrideButton(sButton, true);
}
void OnKeyButtonDown_Command(KeyButton button)
{
SetBlockPlayerInputThisFrame();
SetKeyboardActive(false);
SendCommand(button.key);
}
void OnGame_ButtonPressed(object? sender, ButtonPressedEventArgs e)
{
//process when Button is Mouse
if (e.Button != SButton.MouseLeft)
return;
var screenPixels = Utility.ModifyCoordinatesForUIScale(e.Cursor.ScreenPixels);
bool isAnyClick = false;
foreach (var btn in keys)
{
btn.receiveLeftClick((int)screenPixels.X, (int)screenPixels.Y);
if (btn.isHeldDown)
isAnyClick = true;
}
//close keyboard when click any background
if (!isAnyClick & isShowKeyboard)
{
SetKeyboardActive(false);
SetBlockPlayerInputThisFrame();
}
}
void OnGame_ButtonReleased(object? sender, ButtonReleasedEventArgs e)
{
if (e.Button != SButton.MouseLeft)
return;
var screenPixels = Utility.ModifyCoordinatesForUIScale(e.Cursor.ScreenPixels);
foreach (var btn in keys)
{
btn.releaseLeftClick((int)screenPixels.X, (int)screenPixels.Y);
}
}
void SendCommand(string command)
{
MobileConsoleTool.WriteLine(command);
}
public void SetKeyboardActive(bool enable = true)
{
//reset init
ToggleKeys(false);
ToggleKeyboardPage(false);
floatingKeyboard.enabled = enable;
}
//For block player to touch & tap
public void ToggleKeyboardPage(bool toggle)
{
//enable
floatingKeyboard.opacityInOut.SetTarget(toggle ? 1f : config.Opacity);
if (toggle)
{
if (Game1.activeClickableMenu == null)
{
var keyboardPage = new KeyboardPage();
keyboardPage.behaviorBeforeCleanup = OnCloseKeyboardPage;
Game1.activeClickableMenu = keyboardPage;
}
return;
}
//disable
if (IsCurrentGameMenuKeyboardPage)
{
Game1.activeClickableMenu = null;
if (!isBlockPlayerInputThisFrame)
SetBlockPlayerInput(false);
}
}
public bool IsCurrentGameMenuKeyboardPage => Game1.activeClickableMenu?.GetType() == typeof(KeyboardPage);
//set visable keys
public void ToggleKeys(bool toggle)
{
this.isShowKeyboard = toggle;
foreach (var button in keys)
{
if (button != floatingKeyboard)
{
button.enabled = isShowKeyboard;
}
}
}
KeyButton AddButton(string key, string label, Action<KeyButton> callback, int horizonLayout)
{
var keyButton = new KeyButton(key, label, callback);
keyButton.enabled = false;
keyButton.buttonHorizonLayout = horizonLayout;
if (!keysLookup.ContainsKey(horizonLayout))
keysLookup.Add(horizonLayout, new());
keysLookup[horizonLayout].Add(keyButton);
keys.Add(keyButton);
return keyButton;
}
void OnCloseKeyboardPage(IClickableMenu menu)
{
ToggleKeyboardPage(false);
}
static void SetBlockPlayerInput(bool isBlock)
{
Game1.freezeControls = isBlock;
}
static bool isBlockPlayerInputThisFrame = false;
static int blockFrameCountDown;
static void SetBlockPlayerInputThisFrame()
{
blockFrameCountDown = 10;
isBlockPlayerInputThisFrame = true;
SetBlockPlayerInput(true);
}
}