-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrollermode.lua
More file actions
642 lines (527 loc) · 16 KB
/
Copy pathcontrollermode.lua
File metadata and controls
642 lines (527 loc) · 16 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
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
--todo: situation based controls
function startControllerMode()
local laststate
function startPress()
local a=getApplication()
if a.ProcessWindow==nil or a.ProcessWindow.Visible==false then
MainForm.sbOpenProcess.doClick()
end
end
if f then
f.destroy()
f=nil
end
local dpadrepeater
function handleDPAD(v,state)
if (state==false) then --key got released
if dpadrepeater then
dpadrepeater.destroy()
dpadrepeater=nil
end
end
if dpadrepeater then
dpadrepeater.enabled=false
dpadrepeater=nil
end
if state then
local f=getFocusedForm()
if f then
local c=f.ActiveControl
if c then
print("dpad "..v.." f="..f.Name..':'..f.ClassName..' - '..c.Name..':'..c.ClassName)
dpadrepeater=createTimer(100, function() handleDPAD(v, true) end)
if v==0 then --up
--print("up")
if c.ItemIndex then
if c.ItemIndex>0 then
c.ItemIndex=c.ItemIndex-1
end
elseif c.CaretY then
c.CaretY=c.CaretY-1
else
if c.inheritsFrom('TCustomTreeView') then
--treeview inherited
if c.Selected==nil then
c.Selected=c.items.getFirstVisibleNode()
else
local nextnode=c.Selected.getPrevVisible()
if nextnode then
c.Selected=nextnode
end
end
if c.Selected then
c.Selected.makeVisible()
end
elseif c.ClassName=='THexView' then
c.Address=c.Address-c.BytesPerLine
end
end
elseif v==1 then --down
--print("down")
if c.ItemIndex then
if c.ItemIndex<c.Items.Count-1 then
c.ItemIndex=c.ItemIndex+1
end
elseif c.CaretY then
c.CaretY=c.CaretY+1
else
if c.inheritsFrom('TCustomTreeView') then
--treeview inherited
if c.Selected==nil then
c.Selected=c.Items.getLastVisibleNode()
else
local nextnode=c.Selected.getNextVisible()
if nextnode then
c.Selected=nextnode
end
end
if c.Selected then
c.Selected.makeVisible()
end
elseif c.ClassName=='THexView' then
c.Address=c.Address+c.BytesPerLine
end
end
elseif v==2 then --left
if c.inheritsFrom('TCustomTreeView') then
--treeview inherited
if c.Selected==nil then
c.Selected=c.Items.getFirstVisibleNode()
else
local nextnode=c.Selected.Parent
if nextnode then
c.Selected=nextnode
end
end
if c.Selected then
c.Selected.makeVisible()
end
elseif c.ClassName=='THexView' then
c.Address=c.Address-1
end
elseif v==3 then --right
if c.inheritsFrom('TCustomTreeView') then
--treeview inherited
if c.Selected==nil then
c.Selected=c.Items.getLastVisibleNode()
else
if c.Selected.HasChildren then
local nextnode=c.Selected.getFirstVisibleChild()
if nextnode then
c.Selected=nextnode
end
end
end
if c.Selected then
c.Selected.makeVisible()
end
elseif c.ClassName=='THexView' then
c.Address=c.Address+1
end
end
end
end
end
end
local controllerKeyHandler={}
controllerKeyHandler.GAMEPAD_DPAD_UP=function(state) handleDPAD(0,state) end
controllerKeyHandler.GAMEPAD_DPAD_DOWN=function(state) handleDPAD(1,state) end
controllerKeyHandler.GAMEPAD_DPAD_LEFT=function(state) handleDPAD(2,state) end
controllerKeyHandler.GAMEPAD_DPAD_RIGHT=function(state) handleDPAD(3,state) end
controllerKeyHandler.GAMEPAD_LEFT_SHOULDER=function(state)
--send the current form to the back
if state then
local l={}
for i=getFormCount()-1,1,-1 do
local f=getForm(i)
if f.Visible then
table.insert(l,f)
end
end
for i=1,#l do
if i==#l then
l[i].show()
else
l[i].bringToFront()
end
end
end
end
controllerKeyHandler.GAMEPAD_RIGHT_SHOULDER=function(state)
if state then
for i=getFormCount()-1,1,-1 do
local f=getForm(i)
if f.Visible then
f.show()
break
end
end
end
end
controllerKeyHandler.GAMEPAD_A=function(state)
if state then
--print("A")
local f=getFocusedForm()
if f then
local c=f.ActiveControl
if c.ClassName=='TEdit' then
--todo
--spawn a keyboard. On steamdeck use the steamdeck keyboard. todo: create my own keyboard ?
showMessage('editing here')
--if the result doesn't include Enter/Return then
return
end
if c.OnClick then
queue(function()
c.doClick()
if c.ModalResult and c.ModalResult~=0 then
f.ModalResult=c.ModalResult
end
end)
return
end
if c.OnDblClick then
queue(function()
c.OnDblClick(c)
if c.ModalResult and c.ModalResult~=0 then
f.ModalResult=c.ModalResult
end
end)
return
end
if c.ModalResult and c.ModalResult~=0 then
f.ModalResult=c.ModalResult
return
end
if f.FormState:find('fsModal') then
if f.DefaultControl then
f.DefaultControl.doClick()
if f.ModalResult==0 then
f.ModalResult=f.DefaultControl.ModalResult
if f.ModalResult then return end
else
return
end
end
end
local c=f.ActiveControl
if c then
queue(function()
c.doClick()
end)
else
end
else
--print("no form")
end
end
end
controllerKeyHandler.GAMEPAD_B=function(state)
if state then
--print("B")
local f=getFocusedForm()
c=f.ActiveControl
if f then
if f.CancelControl then
f.CancelControl.doClick()
if f.ModalResult==0 then
f.ModalResult=f.CancelControl.ModalResult
if f.ModalResult then return end
else
return
end
else
if f.FormState:find('fsModal') then
f.ModalResult=mrCancel
end
end
end
end
end
controllerKeyHandler.GAMEPAD_X=function(state)
if state then
local f=getFocusedForm()
if f then
local c=f.ActiveControl
if c then
--c.color=clRed
c.performTab(laststate.RightTrigger<127)
c=f.ActiveControl
--c.color=clGreen
end
end
end
end
controllerKeyHandler.GAMEPAD_Y=function(state)
if state then
if newAddressList then
if getFocusedForm()==newAddressList then
--pressing Y when already in the addresslist shows the opentable dialog
queue(function()
local f=createOpenTableDialog()
if f then
loadTable(f)
end
end)
else
newAddressList.show()
end
end
end
end
--[[
x=tab
y=addresslist
left trigger+y=open table
--]]
controllerKeyHandler.GAMEPAD_START={function() startPress() end}
f=createThread(function(t)
local startwasdown=false
local upwasdown=false
local lastpacket=0
local currentControl, currentControlClass
laststate=getXBox360ControllerState()
t.freeOnTerminate(false)
while not t.Terminated do
local hasfocus
if getOperatingSystem()==0 then
hasfocus=getForegroundProcess()==getCheatEngineProcessID()
else
hasfocus=getForegroundWindow()~=0
end
if hasfocus then
local s=getXBox360ControllerState() --todo, add a waitForPacket param so the loop isn't in here
if s.PacketNumber~=laststate.PacketNumber then
for controllerfield,value in pairs(s) do
if value~=laststate[controllerfield] then
--check if there is a handler for this field
local handler=controllerKeyHandler[controllerfield]
if handler then
if type(handler)=='table' then
queue(handler[1])
else
synchronize(handler,value)
end
end
end
end
laststate=s
else
sleep(16)
end
else
sleep(32)
end
end
end)
local w=getScreenWidth() / 2
if newAddressList then
newAddressList.close()
end
newAddressList=createForm()
newAddressList.Name='newAddressList'
newAddressList.Caption='Cheat Engine Address List'
newAddressList.BorderStyle=bsSizeable
newAddressList.Width=w
newAddressList.Height=getScreenHeight() / 2
newAddressList.Position=poScreenCenter
local al=MainForm.AddressList
al.Font.Size=20
al.Parent=newAddressList
al.setFocus()
newAddressList.OnResize=function(c)
local w=newAddressList.clientWidth
al.Header.Sections[0].Width=w * 0.10
al.Header.Sections[1].Width=w * 0.30
al.Header.Sections[2].Width=w * 0.20
al.Header.Sections[3].Width=w * 0.15 --type
al.Header.Sections[4].Width=w * 0.25 --value
end
newAddressList.OnClose=function()
local al=MainForm.AddressList
local w=MainForm.ClientWidth
al.Parent=MainForm.Panel1
al.Font.Size=0
al.Header.Sections[0].Width=w * 0.10
al.Header.Sections[1].Width=w * 0.30
al.Header.Sections[2].Width=w * 0.20
al.Header.Sections[3].Width=w * 0.15 --type
al.Header.Sections[4].Width=w * 0.25 --value
newAddressList=nil
return caFree
end
local oldmfclose=MainForm.OnCloseQuery
MainForm.OnCloseQuery=function(...)
if newAddressList then
newAddressList.close()
newAddressList=nil
end
return oldmfclose(...)
end
function getControllerPanelText(f)
local s
local A,B,X,Y
local rest=''
if f.Name=='newAddressList' then
A='Activate/Deactivate'
Y='Open Table'
rest='Shoulder=switch window'
else
A='Select/Confirm'
B='Cancel/Back'
X='Tab'
if f.FormState:find('fsModal')==nil then
Y='AddressList'
rest='Shoulder=switch window'
end
end
local c=f.ActiveControl
if c then
--add extra buttons
if c.ClassName=='TEdit' then
A='Open editor' --if steamdeck
end
end
s=''
if A then s=s..'A='..A..' ' end
if B then s=s..'B='..B..' ' end
if X then s=s..'X='..X..' ' end
if Y then s=s..'Y='..Y..' ' end
if rest then s=s..rest end
return s
end
registerActiveFormChangeNotification(function(f)
if f.ControllerPanel==nil then
print(f.Name..':'..f.ClassName)
lowest=f.Control[0]
for i=1, f.ControlCount-1 do
local c=f.Control[i]
if lowest.Top+lowest.Height<c.Top+c.Height then
lowest=c
end
end
if lowest then
p=createPanel(f)
p.Name='ControllerPanel'
p.Caption=getControllerPanelText(f)
p.Color=clRed
if f.ClassName=='TProcessWindow' then
--place the panel where it looks best
p.Parent=f.Panel1
p.AnchorSideTop.Control=f.Panel5
p.AnchorSideTop.Side=asrBottom
p.BorderSpacing.Top=4
p.AnchorSideLeft.Control=f.Panel1
p.AnchorSideLeft.Side=asrLeft
p.AnchorSideRight.Control=f.Panel1
p.AnchorSideRight.Side=asrRight
p.Anchors='[akLeft, akRight, akTop]'
elseif f.ClassName=='TformSettings' then
p.Parent=f.Panel6
p.AnchorSideTop.Control=f.Panel9
p.AnchorSideTop.Side=asrBottom
p.BorderSpacing.Top=4
p.AnchorSideLeft.Control=f.Panel6
p.AnchorSideLeft.Side=asrLeft
p.AnchorSideRight.Control=f.Panel6
p.AnchorSideRight.Side=asrRight
p.Anchors='[akLeft, akRight, akTop]'
elseif f.ClassName=='TformAddressChange' then
p.Parent=f
p.AnchorSideTop.Control=f.btnOK
p.AnchorSideTop.Side=asrBottom
p.BorderSpacing.Top=4
p.AnchorSideLeft.Control=f
p.AnchorSideLeft.Side=asrLeft
p.AnchorSideRight.Control=f
p.AnchorSideRight.Side=asrRight
p.Anchors='[akLeft, akRight, akTop]'
f.Constraints.OnChange=function()
--print("poo")
local oldf=f.Constraints.OnChange
f.Constraints.OnChange=nil
f.Constraints.MinHeight=p.Top+p.Height
f.Constraints.OnChange=oldf
end
elseif f.ClassName=='TPromptDialog' then
p.Parent=f
p.AnchorSideTop.Control=lowest
p.AnchorSideTop.Side=asrBottom
p.BorderSpacing.Top=4
p.AnchorSideLeft.Control=f
p.AnchorSideLeft.Side=asrLeft
p.AnchorSideRight.Control=f
p.AnchorSideRight.Side=asrRight
p.Anchors='[akLeft, akRight, akTop]'
f.Constraints.MinHeight=p.Top+p.Height
else
--unknown/default works fine
if inheritsFromWinControl(lowest) then
p.Parent=lowest
end
p.Align=alBottom
end
f.Constraints.MinWidth=f.Canvas.getTextWidth(p.Caption)+5
end
else
f.ControllerPanel.Caption=getControllerPanelText(f)
end
end)
registerActiveControlChangeNotification(function(c)
local f
if c then
while c.Parent do
c=c.Parent
end
f=c
end
if f==nil then
f=getActiveForm()
end
if f and f.ControllerPanel then
f.ControllerPanel.Caption=getControllerPanelText(f)
end
end)
end
if getControllerState() then
if MainForm.miControllerModeBase then
MainForm.miControllerModeBase.destroy()
end
if MainForm.tControllerModeLauncherWatcher then
MainForm.tControllerModeLauncherWatcher.destroy()
end
local miControllerModeBase=createMenuItem(MainForm)
miControllerModeBase.Name='miControllerModeBase'
miControllerModeBase.Caption='Controller Mode (Press A+B)'
MainForm.Menu.Items.insert(MainForm.Menu.Items.Count-1, miControllerModeBase)
local miControllerModeStart=createMenuItem(MainForm)
miControllerModeStart.Name='miControllerModeStart'
miControllerModeStart.Caption='Start Ctrontroller mode (A+B)'
miControllerModeStart.OnClick=function()
startControllerMode()
end
miControllerModeBase.add(miControllerModeStart)
tControllerModeLauncherWatcher=createTimer(MainForm)
tControllerModeLauncherWatcher.Name='tControllerModeLauncherWatcher'
tControllerModeLauncherWatcher.Interval=100
tControllerModeLauncherWatcher.OnTimer=function(t)
if getForegroundWindow()==MainForm.Handle then
local s=getControllerState()
if s==nil then
print("error:no controllerstate result")
t.Enabled=false
t.destroy()
return
end
if s.GAMEPAD_A and s.GAMEPAD_B then
print("launching controllermode")
t.Enabled=false
t.destroy()
miControllerModeBase.destroy()
startControllerMode()
end
end
end
end
--[[
--]]