-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrollermode.lua
More file actions
858 lines (698 loc) · 21.8 KB
/
Copy pathcontrollermode.lua
File metadata and controls
858 lines (698 loc) · 21.8 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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
--todo: situation based controls
function startControllerMode()
--[[local--]] windowlist={MainForm} --visible windowlist
formhistory={}
local formChangeNotificationID,formRemovalNotificationID
currentForm=MainForm
getLuaEngine().cbShowOnPrint.Checked=false
function showForm(f)
if getApplication().ModalLevel>0 then
print("Modal mode")
return
end
if f~=currentForm then
table.insert(formhistory,currentForm)
end
while #formhistory>10 do
table.remove(formhistory,1)
end
printf("showForm:%s",tostringx(f))
if currentForm and currentForm.formState:find('fsModal') then return end --the current form is modal
local isnew=true
for i=1,#windowlist do
if f==windowlist[i] then
isnew=false
break
end
end
if isnew then
print("unknown form. adding it to the list")
table.insert(windowlist,f)
end
for i=1,#windowlist do
windowlist[i].visible=windowlist[i]==f --hide (not close) all other windows, except this one
end
f.show()
currentForm=f
end
formRemovalNotificationID=registerFormRemoveNotification(function(f)
printf("removeForm:%s",tostringx(f))
local i=1
while i<=#formhistory do
if formhistory[i]==f then
table.remove(formhistory,i)
else
i=i+1
end
end
for i=2,#windowlist do
if windowlist[i]==f then
table.remove(windowlist,i)
local nextform
if f==currentForm then
currentForm=nil
nextform=windowlist[i-1]
showForm(nextform)
end
return
end
end
end)
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)
if c.inheritsFrom('TCustomTreeView') then
dpadrepeater=createTimer(200, function() handleDPAD(v, true) end)
else
dpadrepeater=createTimer(120, function() handleDPAD(v, true) end)
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
elseif c.ClassName=='TDisassemblerview' then
c.SelectedAddress=c.SelectedAddress+getInstructionSize(c.SelectedAddress)
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
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)
if getApplication().ModalLevel>0 then return end
if state then
local windowIndex
for i=1,#windowlist do
if windowlist[i]==currentForm then
windowIndex=i
break
end
end
if windowIndex==nil then
print("left shoulder: currentForm was not found")
showForm(MainForm)
return
end
if windowIndex==1 then
windowIndex=#windowlist
else
windowIndex=windowIndex-1
end
showForm(windowlist[windowIndex])
end
end
controllerKeyHandler.GAMEPAD_RIGHT_SHOULDER=function(state)
if getApplication().ModalLevel>0 then return end
if state then
local windowIndex
for i=1,#windowlist do
if windowlist[i]==currentForm then
windowIndex=i
break
end
end
if windowIndex==nil then
print("right shoulder: currentForm was not found")
showForm(MainForm)
return
end
if windowIndex==#windowlist then
windowIndex=1
else
windowIndex=windowIndex+1
end
showForm(windowlist[windowIndex])
end
end
controllerKeyHandler.GAMEPAD_A=function(state)
if state then
--print("A")
local f=getFocusedForm()
if f then
local c=f.ActiveControl
if c==nil then return end
if c.ClassName=='TTreeviewWithScroll' then --addresslist
if c.Selected and c.Selected.memrec then
c.Selected.memrec.Active=not c.Selected.memrec.Active
return
end
end
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()
if f then
c=f.ActiveControl
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()==getCheatEnginePreturn rocessID()
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=800 --getScreenWidth()
newAddressList.Height=600 --getScreenHeight()
newAddressList.Position=poScreenCenter
newAddressList.PopupMode='pmNone'
local oldheight=MainForm.Height
MainForm.Height=MainForm.Height-AddressList.Height
showForm(newAddressList)
local al=MainForm.AddressList
al.Font.Size=20
al.Parent=newAddressList
al.List.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
MainForm.Height=oldheight
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
if formChangeNotificationID then
unregisterActiveFormChangeNotification(formChangeNotificationID)
formChangeNotificationID=nil
end
if formRemovalNotificationID then
unregisterFormRemoveNotification(formRemovalNotificationID)
formRemovalNotificationID=nil
end
-- print("Cleaning the panels")
for i=1,#windowlist do
windowlist[i].visible=true
local c=windowlist[i].ControllerPanel
if c then
--printf("%s has controller panel. destroying it", tostringx(windowlist[i]))
c.destroy()
else
--printf("%s has no controller panel", tostringx(windowlist[i]))
end
end
MainForm.show()
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
formChangeNotificationID=registerActiveFormChangeNotification(function(f)
if f.ControllerPanel==nil then
print(f.Name..':'..f.ClassName)
if f.ControlCount>0 then
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
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
if f.ControllerPanel then
f.Height=f.Height+f.ControllerPanel.Height
end
else
f.ControllerPanel.Caption=getControllerPanelText(f)
end
if f.inheritsFrom('THintWindow') then
print("hintwindow")
f.visible=false
CurrentForm.show()
else
printf("active form is %s",tostringx(f))
CurrentForm=f
local found=false
for i=1,#windowlist do
if windowlist[i]==f then
found=true
break
end
end
if not found then
print("new form. adding it to the windowlist")
table.insert(windowlist,f)
f.registerCloseCallback(function(frm, ca)
for i=1,#windowlist do
if windowlist[i]==frm then
print("found the form in the windowlist. Removing it. it's not visible anymore") --keep in history
table.remove(windowlist,i)
break
end
end
if ca==caFree then
local i=1
while i<=#formhistory do
if formhistory[i]==f then
table.remove(formhistory,i)
else
i=i+1
end
end
end
while (#formhistory>0) and (formhistory[#formhistory]==f) do --make sure it's never the last one
formhistory[#formhistory]=nil
end
if CurrentForm==f then
print("this was the focused form. Finding previous form")
if #formhistory then
printf("Previous form was %s", tostringx(formhistory[#formhistory]))
showForm(formhistory[#formhistory])
else
printf("Unknown previous form. Going for MainForm")
showForm(MainForm)
end
end
end)
showForm(f) --show it again
else
print("known form")
end
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 Controller 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
--[[
--]]