-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScript.rb
More file actions
1032 lines (951 loc) · 33.1 KB
/
Script.rb
File metadata and controls
1032 lines (951 loc) · 33.1 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
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#===============================================================================
# * Set the Controls Screen - by FL (Credits will be apreciated)
#===============================================================================
#
# This script is for Pokémon Essentials. It creates a "Set the controls" screen
# on pause menu, allowing the player to map the actions to the keys in keyboard
# and buttons in a gamepad, ignoring the values defined on F1. You can also
# define the default controls.
#
#== INSTALLATION ===============================================================
#
# To this script works, put it above main OR convert into a plugin.
#
#== NOTES ======================================================================
#
# Look at 'self.default_controls' and below for default controls and default
# names.
#
# '$PokemonSystem.game_controls = nil' resets the controls.
#
# 'SetControls.open_ui' opens the control UI. You can call it from places like
# an event.
#
# This script, by default, doesn't allow the player to redefine some commands
# like F8 (screenshot key), but if the player assign an action to this key,
# like the "Cancel" action, this key will do this action AND take screenshots
# when pressed. Remember that F12 will reset the game.
#
# You can comment keys/buttons in KEYBOARD_LIST (and gamepad ones) to remove
# the key as available key for user. Remember to remove it from default
# controls if it is in the list.
#
# To add more actions, look the lines in this script where there is a
# "Ready Menu" in, for example and do the same thing. You need to use defined
# values in Input (like Input::SPECIAL, used in Ready Menu case or the unused
# Input::AUX1 and Input::AUX2). This script also creates AUX3, AUX4 and AUX5,
# but you can create more (just follows AUX5 format).
#
# 'SetControls.key_array(act)' return the key/button array, where act is the
# action name (like "Cancel"). You can use it to set in a variable and inform
# player of a certain key/button, example in an event:
#
# @>Script: $game_variables[42] =
# : SetControls.key_array("Cancel")[0]
# @>Text: Press \v[42] to exit from menus.
#
#===============================================================================
if !PluginManager.installed?("Set the Controls Screen")
PluginManager.register({
:name => "Set the Controls Screen",
:version => "1.2.8",
:link => "https://www.pokecommunity.com/showthread.php?t=309391",
:credits => "FL"
})
end
module SetControls
# Change it to false for easily disable this script, without affecting saves.
# After changing this value, close and open the game window.
ENABLED = true
# Automatic sort the keys by index. This way gamepad buttons always goes last.
AUTO_SORT = true
# Control screen won't allow player to add more keys/button to a single
# action after reaching at this number.
MAX_KEYS_PER_ACTION = 9
end
# Class stored in saves.
class ControlConfig
attr_reader :control_action
attr_accessor :key_code
def initialize(control_action, key=nil)
@control_action = control_action
@key_code = Keys.key_code(key) if key
end
def self.new_by_code(control_action, key_code)
ret = self.new(control_action)
ret.key_code = key_code
return ret
end
# Create multiple per key and return an array with new initialized instances
def self.multiple_new(control_action, key_array)
return key_array.map{|key| self.new(control_action,key)}
end
def key_name
return Keys.key_name(@key_code)
end
end
module Input
AUX3 = 53
AUX4 = 54
AUX5 = 55
AXIS_ENABLED = true
# Used offsets to support the same variable for both gamepad and keyboard.
GAMEPAD_OFFSET = 500
AXIS_OFFSET = 100 + GAMEPAD_OFFSET
AXIS_THRESHOLD = 0.5
AXIS_REPEAT_INITIAL_DELAY = 0.5
AXIS_REPEAT_DELAY = 0.1
# Using this for manual check
LEFT_STICK_LEFT = 0x00
LEFT_STICK_RIGHT = 0x01
LEFT_STICK_UP = 0x02
LEFT_STICK_DOWN = 0x03
RIGHT_STICK_LEFT = 0x04
RIGHT_STICK_RIGHT = 0x05
RIGHT_STICK_UP = 0x06
RIGHT_STICK_DOWN = 0x07
LEFT_TRIGGER = 0x09
RIGHT_TRIGGER = 0x0B
AXIS_COUNT = RIGHT_TRIGGER+1
class << self
if !method_defined?(:_old_fl_press?)
alias :_old_fl_press? :press?
def press?(button)
key = buttonToKey(button)
return key ? pressex_array?(key) : _old_fl_press?(button)
end
alias :_old_fl_trigger? :trigger?
def trigger?(button)
key = buttonToKey(button)
return key ? triggerex_array?(key) : _old_fl_trigger?(button)
end
alias :_old_fl_repeat? :repeat?
def repeat?(button)
key = buttonToKey(button)
return key ? repeatex_array?(key) : _old_fl_repeat?(button)
end
alias :_old_fl_release? :release?
def release?(button)
key = buttonToKey(button)
return key ? releaseex_array?(key) : _old_fl_release?(button)
end
end
def pressex_array?(array)
for item in array
if item >= AXIS_OFFSET
return true if axis_pressex?(item - AXIS_OFFSET)
elsif item >= GAMEPAD_OFFSET
return true if Controller.pressex?(item - GAMEPAD_OFFSET)
else
return true if pressex?(item)
end
end
return false
end
def triggerex_array?(array)
for item in array
if item >= AXIS_OFFSET
return true if axis_triggerex?(item - AXIS_OFFSET)
elsif item >= GAMEPAD_OFFSET
return true if Controller.triggerex?(item - GAMEPAD_OFFSET)
else
return true if triggerex?(item)
end
end
return false
end
def repeatex_array?(array)
for item in array
if item >= AXIS_OFFSET
# Trigger is checked in axis_repeatex?
return true if axis_repeatex?(item - AXIS_OFFSET)
elsif item >= GAMEPAD_OFFSET
return true if Controller.repeatex?(item - GAMEPAD_OFFSET)
return true if Controller.triggerex?(item - GAMEPAD_OFFSET)
else
return true if repeatex?(item)
return true if triggerex?(item)
end
end
return false
end
def releaseex_array?(array)
for item in array
if item >= AXIS_OFFSET
return true if axis_releaseex?(item - AXIS_OFFSET)
elsif item >= GAMEPAD_OFFSET
return true if Controller.releaseex?(item - GAMEPAD_OFFSET)
else
return true if releaseex?(item)
end
end
return false
end
def dir4
return 0 if press?(DOWN) && press?(UP)
return 0 if press?(LEFT) && press?(RIGHT)
for button in [DOWN,LEFT,RIGHT,UP]
return button if press?(button)
end
return 0
end
def dir8
buttons = []
for b in [DOWN,LEFT,RIGHT,UP]
buttons.push(b) if press?(b)
end
if buttons.length==0
return 0
elsif buttons.length==1
return buttons[0]
elsif buttons.length==2
return 0 if (buttons[0]==DOWN && buttons[1]==UP)
return 0 if (buttons[0]==LEFT && buttons[1]==RIGHT)
end
up_down = 0
left_right = 0
for b in buttons
up_down = b if up_down==0 && (b==UP || b==DOWN)
left_right = b if left_right==0 && (b==LEFT || b==RIGHT)
end
if up_down==DOWN
return 1 if left_right==LEFT
return 3 if left_right==RIGHT
return 2
elsif up_down==UP
return 7 if left_right==LEFT
return 9 if left_right==RIGHT
return 8
else
return 4 if left_right==LEFT
return 6 if left_right==RIGHT
return 0
end
end
def buttonToKey(button)
$PokemonSystem = PokemonSystem.new if !$PokemonSystem
return case button
when Input::DOWN
$PokemonSystem.game_control_code("Down")
when Input::LEFT
$PokemonSystem.game_control_code("Left")
when Input::RIGHT
$PokemonSystem.game_control_code("Right")
when Input::UP
$PokemonSystem.game_control_code("Up")
when Input::ACTION # Z, W, Y, Shift
$PokemonSystem.game_control_code("Menu")
when Input::BACK # X, ESC
$PokemonSystem.game_control_code("Cancel")
when Input::USE # C, ENTER, Space
$PokemonSystem.game_control_code("Action")
when Input::JUMPUP # A, Q, Page Up
$PokemonSystem.game_control_code("Scroll Up")
when Input::JUMPDOWN # S, Page Down
$PokemonSystem.game_control_code("Scroll Down")
when Input::SPECIAL # F, F5, Tab
$PokemonSystem.game_control_code("Ready Menu")
# when Input::AUX1
# $PokemonSystem.game_control_code("Example A")
# when Input::AUX2
# $PokemonSystem.game_control_code("Example B")
# when Input::AUX3
# $PokemonSystem.game_control_code("Example C")
# when Input::AUX4
# $PokemonSystem.game_control_code("Example D")
# when Input::AUX5
# $PokemonSystem.game_control_code("Example E")
else
nil
end
end
@@axis_states = Array.new(AXIS_COUNT, false)
@@axis_states_old = @@axis_states.clone
@@axis_states_trigger = @@axis_states.clone
@@axis_states_repeat = @@axis_states.clone
@@axis_states_release = @@axis_states.clone
@@axis_states_trigger_time = Array.new(@@axis_states.size, 0.0)
@@axis_states_repeat_time = Array.new(@@axis_states.size, 0.0)
def refresh_axis_array
for i in 0...@@axis_states.size
@@axis_states_old[i] = @@axis_states[i]
@@axis_states[i] = axis_state(i) > AXIS_THRESHOLD
@@axis_states_trigger[i]= @@axis_states[i] && !@@axis_states_old[i]
@@axis_states_release[i]= !@@axis_states[i] && @@axis_states_old[i]
@@axis_states_trigger_time[i]= System.uptime if @@axis_states_trigger[i]
@@axis_states_repeat[i] = @@axis_states_trigger[i] || (
@@axis_states[i] && (
System.uptime >= @@axis_states_trigger_time[i] + AXIS_REPEAT_INITIAL_DELAY
) && System.uptime >= @@axis_states_repeat_time[i] + AXIS_REPEAT_DELAY
)
@@axis_states_repeat_time[i] = System.uptime if @@axis_states_repeat[i]
end
end
def axis_state(key)
return case key
when LEFT_STICK_LEFT; -Controller.axes_left[0]
when LEFT_STICK_RIGHT; Controller.axes_left[0]
when LEFT_STICK_UP; -Controller.axes_left[1]
when LEFT_STICK_DOWN; Controller.axes_left[1]
when RIGHT_STICK_LEFT; -Controller.axes_right[0]
when RIGHT_STICK_RIGHT; Controller.axes_right[0]
when RIGHT_STICK_UP; -Controller.axes_right[1]
when RIGHT_STICK_DOWN; Controller.axes_right[1]
when LEFT_TRIGGER; Controller.axes_trigger[0]
when RIGHT_TRIGGER; Controller.axes_trigger[1]
else 0
end
end
def axis_pressex?(index)
return @@axis_states[index]
end
def axis_triggerex?(index)
return @@axis_states_trigger[index]
end
def axis_repeatex?(index)
return @@axis_states_repeat[index]
end
def axis_releaseex?(index)
return @@axis_states_release[index]
end
# For compatibility with other scripts, use update_KGC_ScreenCapture
# instead of Input.update
if (
AXIS_ENABLED &&
defined?(Controller) && !method_defined?(:_old_fl_update_kgc)
)
alias :_old_fl_update_kgc :update_KGC_ScreenCapture
def update_KGC_ScreenCapture
_old_fl_update_kgc
refresh_axis_array
end
end
end
end if SetControls::ENABLED
module Keys
# Here you can change the default values
def self.default_controls
return default_controls_no_gamepad if !Input.const_defined?(:Controller)
return (
ControlConfig.multiple_new("Down", ["Down","D-Pad Down","L-Stick Down"]) +
ControlConfig.multiple_new("Left", ["Left","D-Pad Left","L-Stick Left"]) +
ControlConfig.multiple_new("Right", ["Right", "D-Pad Right", "L-Stick Right"]) +
ControlConfig.multiple_new("Up", ["Up", "D-Pad Up","L-Stick Up"]) +
ControlConfig.multiple_new("Action", ["C","Enter","Space", "Button A"]) +
ControlConfig.multiple_new("Cancel", ["X","Esc", "Numpad 0", "Button B"])+
ControlConfig.multiple_new("Menu", ["Z", "Shift", "Button X"]) +
ControlConfig.multiple_new("Scroll Up", ["A", "Left Shoulder"]) +
ControlConfig.multiple_new("Scroll Down", ["S", "Right Shoulder"]) +
ControlConfig.multiple_new("Ready Menu", ["D","Button Y"])
)
end
# Used only in Essentials v20.1 or lower. You can copy it to above method
# if you want remove gamepad buttons in default values
def self.default_controls_no_gamepad
return (
ControlConfig.multiple_new("Down", ["Down"]) +
ControlConfig.multiple_new("Left", ["Left"]) +
ControlConfig.multiple_new("Right", ["Right"]) +
ControlConfig.multiple_new("Up", ["Up"]) +
ControlConfig.multiple_new("Action", ["C","Enter","Space"]) +
ControlConfig.multiple_new("Cancel", ["X","Esc"])+
ControlConfig.multiple_new("Menu", ["Z", "Shift"]) +
ControlConfig.multiple_new("Scroll Up", ["A"]) +
ControlConfig.multiple_new("Scroll Down", ["S"]) +
ControlConfig.multiple_new("Ready Menu", ["D"])
)
end
# Available keys in keyboard
KEYBOARD_LIST = {
# Mouse buttons
"Backspace" => 0x08,
"Tab" => 0x09,
"Clear" => 0x0C,
"Enter" => 0x0D,
"Shift" => 0x10,
"Ctrl" => 0x11,
"Alt" => 0x12,
"Pause" => 0x13,
# IME keys
"Caps Lock" => 0x14,
"Esc" => 0x1B,
"Space" => 0x20,
"Page Up" => 0x21,
"Page Down" => 0x22,
"End" => 0x23,
"Home" => 0x24,
"Left" => 0x25,
"Up" => 0x26,
"Right" => 0x27,
"Down" => 0x28,
"Select" => 0x29,
"Print" => 0x2A,
"Execute" => 0x2B,
"Print Screen" => 0x2C,
"Insert" => 0x2D,
"Delete" => 0x2E,
"Help" => 0x2F,
"0" => 0x30,
"1" => 0x31,
"2" => 0x32,
"3" => 0x33,
"4" => 0x34,
"5" => 0x35,
"6" => 0x36,
"7" => 0x37,
"8" => 0x38,
"9" => 0x39,
"A" => 0x41,
"B" => 0x42,
"C" => 0x43,
"D" => 0x44,
"E" => 0x45,
"F" => 0x46,
"G" => 0x47,
"H" => 0x48,
"I" => 0x49,
"J" => 0x4A,
"K" => 0x4B,
"L" => 0x4C,
"M" => 0x4D,
"N" => 0x4E,
"O" => 0x4F,
"P" => 0x50,
"Q" => 0x51,
"R" => 0x52,
"S" => 0x53,
"T" => 0x54,
"U" => 0x55,
"V" => 0x56,
"W" => 0x57,
"X" => 0x58,
"Y" => 0x59,
"Z" => 0x5A,
# Windows keys
"Numpad 0" => 0x60,
"Numpad 1" => 0x61,
"Numpad 2" => 0x62,
"Numpad 3" => 0x63,
"Numpad 4" => 0x64,
"Numpad 5" => 0x65,
"Numpad 6" => 0x66,
"Numpad 7" => 0x67,
"Numpad 8" => 0x68,
"Numpad 9" => 0x69,
"Multiply" => 0x6A,
"Add" => 0x6B,
"Separator" => 0x6C,
"Subtract" => 0x6D,
"Decimal" => 0x6E,
"Divide" => 0x6F,
"F1" => 0x70,
"F2" => 0x71,
"F3" => 0x72,
"F4" => 0x73,
"F5" => 0x74,
"F6" => 0x75,
"F7" => 0x76,
"F8" => 0x77,
"F9" => 0x78,
"F10" => 0x79,
"F11" => 0x7A,
"F12" => 0x7B,
"F13" => 0x7C,
"F14" => 0x7D,
"F15" => 0x7E,
"F16" => 0x7F,
"F17" => 0x80,
"F18" => 0x81,
"F19" => 0x82,
"F20" => 0x83,
"F21" => 0x84,
"F22" => 0x85,
"F23" => 0x86,
"F24" => 0x87,
"Num Lock" => 0x90,
"Scroll Lock" => 0x91,
# Multiple position Shift, Ctrl and Menu keys
";:" => 0xBA,
"+" => 0xBB,
"," => 0xBC,
"-" => 0xBD,
"." => 0xBE,
"/?" => 0xBF,
"`~" => 0xC0,
"{" => 0xDB,
"\|" => 0xDC,
"}" => 0xDD,
"'\"" => 0xDE,
"AX" => 0xE1 # Japan only
}
# Available buttons at gamepad.
GAMEPAD_LIST = {
"Button A" => 0x00,
"Button B" => 0x01,
"Button X" => 0x02,
"Button Y" => 0x03,
"Button Back" => 0x04,
"Button Guide" => 0x05,
"Button Start" => 0x06,
"Left Stick" => 0x07,
"Right Stick" => 0x08,
"Left Shoulder" => 0x09,
"Right Shoulder" => 0x0A,
"D-Pad Up" => 0x0B,
"D-Pad Down" => 0x0C,
"D-Pad Left" => 0x0D,
"D-Pad Right" => 0x0E,
# The below ones are commented since they aren't working
# properly in my last test.
# "Button Misc" => 0x0F, # Xbox Series X share button, PS5 microphone button, Nintendo Switch Pro capture button, Amazon Luna microphone button
# "Paddle 1" => 0x10, # Xbox Elite paddle P1 (upper left, facing the back)
# "Paddle 2" => 0x11, # Xbox Elite paddle P3 (upper right, facing the back)
# "Paddle 3" => 0x12, # Xbox Elite paddle P2 (lower left, facing the back)
# "Paddle 4" => 0x13, # Xbox Elite paddle P4 (lower right, facing the back)
# "Touchpad" => 0x14, # PS4/PS5 touchpad button
}
# Available axis at gamepad.
# This one is manually checked
GAMEPAD_AXIS_LIST = {
"L-Stick Left" => Input::LEFT_STICK_LEFT,
"L-Stick Right" => Input::LEFT_STICK_RIGHT,
"L-Stick Up" => Input::LEFT_STICK_UP,
"L-Stick Down" => Input::LEFT_STICK_DOWN,
"RStick Left" => Input::RIGHT_STICK_LEFT,
"RStick Right" => Input::RIGHT_STICK_RIGHT,
"RStick Up" => Input::RIGHT_STICK_UP,
"RStick Down" => Input::RIGHT_STICK_DOWN,
"Left Trigger" => Input::LEFT_TRIGGER,
"Right Trigger" => Input::RIGHT_TRIGGER,
}
def self.key_name(key_code)
ret = KEYBOARD_LIST.key(key_code)
return ret if ret
ret = GAMEPAD_LIST.key(key_code - Input::GAMEPAD_OFFSET)
return ret if ret
ret = GAMEPAD_AXIS_LIST.key(key_code - Input::AXIS_OFFSET)
return ret if ret
return key_code==0 ? "None" : "?"
end
def self.key_code(key_name)
ret = KEYBOARD_LIST[key_name]
if !ret && GAMEPAD_LIST.has_key?(key_name)
ret = GAMEPAD_LIST[key_name] + Input::GAMEPAD_OFFSET
end
if !ret && GAMEPAD_AXIS_LIST.has_key?(key_name)
ret = GAMEPAD_AXIS_LIST[key_name] + Input::AXIS_OFFSET
end
raise "The key #{key_name} no longer exists! " if !ret
return ret
end
def self.detect_key
loop do
Graphics.update
Input.update
for key_code in KEYBOARD_LIST.values
next if !Input.triggerex?(key_code)
return key_code
end
if Input.const_defined?(:Controller)
for original_code in GAMEPAD_LIST.values
next if !Input::Controller.triggerex?(original_code)
return original_code + Input::GAMEPAD_OFFSET
end
for original_code in GAMEPAD_AXIS_LIST.values
next if !Input.axis_triggerex?(original_code)
return original_code + Input::AXIS_OFFSET
end
end
end
end
end if SetControls::ENABLED
# Existing class stored in saves.
class PokemonSystem
attr_writer :game_controls
def game_controls
@game_controls = Keys.default_controls if !@game_controls
return @game_controls
end
def game_control_code(control_action)
ret = []
for control in game_controls
ret.push(control.key_code) if control.control_action == control_action
end
return ret
end
end
module SetControls
def self.open_ui(menu_to_refresh=nil)
scene=Scene.new
screen=Screen.new(scene)
pbFadeOutIn {
screen.start_screen
menu_to_refresh.pbRefresh if menu_to_refresh
}
end
# Returns an array with all keys who does the action.
def self.key_array(action)
return $PokemonSystem.game_controls.find_all{|c|
c.control_action==action
}.map{|c| c.key_name}
end
# Actions handler. It has an array with all actions.
# Workaround to work with older script version saves in Window_Controls
class ActionHandler
def [](index)
return @data_array[index]
end
def size
return @data_array.size
end
def initialize(controls)
@data_array = create_data_array(
Keys.default_controls.map{|c| c.control_action}.uniq,
create_controls_per_action(controls)
)
end
def create_controls_per_action(controls)
ret = {}
for control in controls
ret[control.control_action] ||= []
ret[control.control_action].push(control)
end
return ret
end
def create_data_array(action_array, controls_per_action)
return action_array.map{|a| ActionData.new(a,controls_per_action[a])}
end
def create_save_control_array
return @data_array.map{|action_data| action_data.control_array}.flatten
end
def clear_keys_with_input(input)
for index in 0...size
key_index = 0
while key_index < self[index].size
if self[index].control_array[key_index].key_code==input
if self[index].size > 1
self[index].delete_key_at(key_index)
key_index-=1
else
self[index].control_array[key_index].key_code = 0
end
end
key_index+=1
end
end
end
def set_key(new_input, action_index, key_index)
if key_index >= self[action_index].size
self[action_index].add_key(new_input)
else
self[action_index].control_array[key_index].key_code = new_input
end
self[action_index].sort_keys! if SetControls::AUTO_SORT
end
end
# Has an action, with all of his keys and controls
class ActionData
attr_reader :name
attr_reader :control_array
def initialize(name, control_array)
@name = name
@control_array = control_array || []
sort_keys! if SetControls::AUTO_SORT
end
def size
return @control_array.size
end
def has_any_key?
return size>1 || (size!=0 && @control_array[0].key_code!=0)
end
def key_code_equals?(index, key_code)
return size > index && @control_array[index].key_code == key_code
end
# All keys text, like "C, B"
def keys_text
return key_array.join(", ")
end
def key_array
return @control_array.map{|control| _INTL(control.key_name)}
end
# The value also need to be added in main array
# Return new added value
def add_key(new_input)
@control_array.push(ControlConfig.new_by_code(@name, new_input))
return @control_array[-1]
end
# The value also need to be removed from main array
def delete_key_at(index)
@control_array.delete_at(index)
end
def sort_keys!
return if size <= 1
sorted_keys = @control_array.map{|c|c.key_code}.sort
for i in 0...size
@control_array[i].key_code = sorted_keys[i]
end
end
end
class Window_Controls < Window_DrawableCommand
attr_reader :reading_input
attr_reader :changed
DEFAULT_EXTRA_INDEX = 0
EXIT_EXTRA_INDEX = 1
def initialize(controls,x,y,width,height)
@action_handler = ActionHandler.new(controls)
@name_base_color = Color.new(88,88,80)
@name_shadow_color = Color.new(168,184,184)
@sel_base_color = Color.new(24,112,216)
@sel_shadow_color = Color.new(136,168,208)
@reading_key_index = nil
@changed = false
super(x,y,width,height)
end
def itemCount
return @action_handler.size+EXIT_EXTRA_INDEX+1
end
def controls
return @action_handler.create_save_control_array
end
def reading_input?
return @reading_key_index != nil
end
def set_new_input(new_input)
if @action_handler[@index].key_code_equals?(@reading_key_index, new_input)
@reading_key_index = nil
return
end
@action_handler.clear_keys_with_input(new_input)
@action_handler.set_key(new_input, @index, @reading_key_index)
@reading_key_index = nil
@changed = true
refresh
end
def on_exit_index?
return @action_handler.size + EXIT_EXTRA_INDEX == @index
end
def on_default_index?
return @action_handler.size + DEFAULT_EXTRA_INDEX == @index
end
def item_description
ret=nil
if on_exit_index?
ret=_INTL(
"Exit. If you changed anything, asks if you want to keep changes."
)
elsif on_default_index?
ret=_INTL("Restore the default controls.")
else
ret= control_description(@action_handler[@index].name)
end
return ret
end
def control_description(control_action)
hash = {}
hash["Down" ] = _INTL("Moves the character. Select entries and navigate menus.")
hash["Left" ] = hash["Down"]
hash["Right" ] = hash["Down"]
hash["Up" ] = hash["Down"]
hash["Action" ] = _INTL("Confirm a choice, check things, talk to people, and move through text.")
hash["Cancel" ] = _INTL("Exit, cancel a choice or mode, and move at field in a different speed.")
hash["Menu" ] = _INTL("Open the menu. Also has various functions depending on context.")
hash["Scroll Up" ] = _INTL("Advance quickly in menus.")
hash["Scroll Down" ] = hash[ "Scroll Up"]
hash["Ready Menu" ] = _INTL("Open Ready Menu, with registered items and available field moves.")
return hash.fetch(control_action, _INTL("Set the controls."))
end
def drawItem(index,_count,rect)
rect=drawCursor(index,rect)
name = case index - @action_handler.size
when DEFAULT_EXTRA_INDEX ; _INTL("Default")
when EXIT_EXTRA_INDEX ; _INTL("Exit")
else ; @action_handler[index].name
end
width= rect.width*6/20
pbDrawShadowText(
self.contents,rect.x,rect.y,width,rect.height,
name,@name_base_color,@name_shadow_color
)
self.contents.draw_text(rect.x,rect.y,width,rect.height,name)
return if index>=@action_handler.size
value = @action_handler[index].keys_text
xpos = width+rect.x
width = rect.width*14/20
pbDrawShadowText(
self.contents,xpos,rect.y,width,rect.height,
value,@sel_base_color,@sel_shadow_color
)
self.contents.draw_text(xpos,rect.y,width,rect.height,value)
end
def update
oldindex=self.index
super
do_refresh=self.index!=oldindex
if self.active && self.index <= @action_handler.size
if Input.trigger?(Input::C)
if on_default_index?
if pbConfirmMessage(_INTL("Are you sure? Anyway, you can exit this screen without keeping the changes."))
pbPlayDecisionSE()
@action_handler = ActionHandler.new(Keys.default_controls)
@changed = true
do_refresh = true
end
elsif self.index<@action_handler.size
if !@action_handler[index].has_any_key?
@reading_key_index = 0 # Replace input
else
do_refresh ||= open_action_menu
end
end
end
end
refresh if do_refresh
end
# Return if a refresh is necessary
def open_action_menu
command = pbMessage(_INTL("What you want to do?"),[
_INTL("Replace new key"), _INTL("Add key"),
_INTL("Remove key"), _INTL("Cancel")
],4)
case command
when 0 # Replace
if @action_handler[index].size==1
@reading_key_index = 0
else
@reading_key_index = choose_control_key(
self.index, _INTL("Choose an assigned key.")
)
end
when 1 # Add
if MAX_KEYS_PER_ACTION == @action_handler[index].size
pbMessage(_INTL(
"You can't add more than {1} keys to an action!",
MAX_KEYS_PER_ACTION
))
else
@reading_key_index = @action_handler[index].size
end
when 2 # Remove
if @action_handler[index].size==1
pbMessage(_INTL("You can't remove a key when there was only one!"))
else
key_index = choose_control_key(
self.index, _INTL("Choose an assigned key.")
)
if key_index
@action_handler[index].delete_key_at(key_index)
@changed = true
return true
end
end
end
return false
end
def choose_control_key(index, message)
ret = pbMessage(
message,
@action_handler[index].key_array + [_INTL("Cancel")],
@action_handler[index].size + 1
)
ret = nil if ret==@action_handler[index].size
return ret
end
end
class Scene
def start_scene
@sprites={}
@viewport=Viewport.new(0,0,Graphics.width,Graphics.height)
@viewport.z=99999
@sprites["title"]=Window_UnformattedTextPokemon.newWithSize(
_INTL("Controls"),0,0,Graphics.width,64,@viewport
)
@sprites["textbox"]=pbCreateMessageWindow
@sprites["textbox"].letterbyletter=false
game_controls = $PokemonSystem.game_controls.map{|c| c.clone}
@sprites["controlwindow"]=Window_Controls.new(
game_controls,0,@sprites["title"].height,Graphics.width,
Graphics.height-@sprites["title"].height-@sprites["textbox"].height
)
@sprites["controlwindow"].viewport=@viewport
@sprites["controlwindow"].visible=true
@changed = false
pbDeactivateWindows(@sprites)
pbFadeInAndShow(@sprites) { update }
end
def update
pbUpdateSpriteHash(@sprites)
end
def main
pbActivateWindow(@sprites,"controlwindow"){ main_loop}
end
def main_loop
last_index=-1
loop do
Graphics.update
Input.update
update
should_refresh_text = @sprites["controlwindow"].index!=last_index
if @sprites["controlwindow"].reading_input?
@sprites["textbox"].text=_INTL("Press a new key.")
@sprites["controlwindow"].set_new_input(Keys.detect_key)
should_refresh_text = true
@changed = true
else
if Input.trigger?(Input::B) || (
Input.trigger?(Input::C) && @sprites["controlwindow"].on_exit_index?
)
if(
@sprites["controlwindow"].changed &&
pbConfirmMessage(_INTL("Keep changes?"))
)
should_refresh_text = true # Visual effect
if @sprites["controlwindow"].controls.find{|c| c.key_code == 0}
@sprites["textbox"].text=_INTL("Fill all fields!")
should_refresh_text = false
else
$PokemonSystem.game_controls=@sprites["controlwindow"].controls
break
end
else
break
end
end
end
if should_refresh_text
if(
@sprites["textbox"].text!=@sprites["controlwindow"].item_description
)
@sprites["textbox"].text=@sprites["controlwindow"].item_description
end
last_index = @sprites["controlwindow"].index
end
end
end