Mateusz SSJ8

Liczba postów: 511
Dołączył: 10-05-13
Pomógł: 40

|
Zaawansowane sterowanie
~ Skrypt na obsługę klawiatury i gamepadów. ~
Krótki opis:
Zmienia liczbę kontrolek sterowania do pełni tego, co masz na kontrolerze. Liczba kontrolerów też rośnie.
Autor:
Blizzard
Glitchfinder
Mateusz SSJ8
Zrzuty ekranu
Obsługa myszki aktualnie ograniczona do przycisków, w związku z czym nie ma żadnych.
Kod
Kliknij, aby zobaczyć
2/3. Wklej w moduł "Input"
class Keyboard
Keys = {}
Keys['Escape'] = 27
Keys['F1'] = 112
Keys['F2'] = 113
Keys['F3'] = 114
Keys['F4'] = 115
Keys['F5'] = 116
Keys['F6'] = 117
Keys['F7'] = 118
Keys['F8'] = 119
Keys['F9'] = 120
Keys['F10'] = 121
Keys['F11'] = 122
Keys['F12'] = 123
Keys['~'] = 192
Keys['Main0'] = 48
Keys['Main1'] = 49
Keys['Main2'] = 50
Keys['Main3'] = 51
Keys['Main4'] = 52
Keys['Main5'] = 53
Keys['Main6'] = 54
Keys['Main7'] = 55
Keys['Main8'] = 56
Keys['Main9'] = 57
Keys['Main-'] = 189
Keys['Main='] = 187
Keys['\\'] = 220
Keys['Backspace'] = 8
Keys['Tab'] = 9
Keys['Q'] = 81
Keys['W'] = 87
Keys['E'] = 69
Keys['R'] = 82
Keys['T'] = 84
Keys['Y'] = 89
Keys['U'] = 85
Keys['I'] = 73
Keys['O'] = 79
Keys['P'] = 80
Keys['['] = 219
Keys[']'] = 221
Keys['MainEnter'] = 13
Keys['CapsLock'] = 20
Keys['A'] = 65
Keys['S'] = 83
Keys['D'] = 68
Keys['F'] = 70
Keys['G'] = 71
Keys['H'] = 72
Keys['J'] = 74
Keys['K'] = 75
Keys['L'] = 76
Keys[';'] = 186
Keys['"'] = 222
Keys['LShift'] = 160
Keys['Z'] = 90
Keys['X'] = 88
Keys['C'] = 67
Keys['V'] = 86
Keys['B'] = 66
Keys['N'] = 78
Keys['M'] = 77
Keys['<'] = 188
Keys['>'] = 190
Keys['?'] = 191
Keys['RShift'] = 161
Keys['LCtrl'] = 162
Keys['LWin'] = 91
Keys['LAlt'] = 164
Keys['Space'] = 32
Keys['RAlt'] = 165
Keys['Rwin'] = 92
Keys['AppMenu'] = 93
Keys['RCtrl'] = 163
Keys['PrintScreen'] = 44
Keys['ScrollLock'] = 145
Keys['Pause'] = 19
Keys['Insert'] = 45
Keys['Delete'] = 46
Keys['Home'] = 36
Keys['End'] = 35
Keys['PageUp'] = 33
Keys['PageDown'] = 34
Keys['Arrow Left'] = 37
Keys['Arrow Up'] = 38
Keys['Arrow Right'] = 39
Keys['Arrow Down'] = 40
Keys['NumLock'] = 144
Keys['Num/'] = 111
Keys['Num*'] = 106
Keys['Num-'] = 109
Keys['Num7'] = 103
Keys['Num8'] = 104
Keys['Num9'] = 105
Keys['Num+'] = 107
Keys['Num4'] = 100
Keys['Num5'] = 101
Keys['Num6'] = 102
Keys['Num1'] = 97
Keys['Num2'] = 98
Keys['Num3'] = 99
Keys['NumX'] = 0
Keys['NumEnter'] = 0
Keys['Num0'] = 96
Keys['Num.'] = 110
Keys['Unknown'] = 12
Keys['MouseLeft'] = 1
Keys['MouseRight'] =2
Keys['MouseCenter'] = 4
Keys['MouseButton4'] = 5
Keys['MouseButton5'] = 6
DEAD_KEY_MASK = 0x80000000
KEY_DOWN_MASK = 128
KEY_TOOGLE_MASK = 1
GetKeyState = Win32API.new('user32', 'GetKeyboardState', 'p', 'l')
GetKeyLayout = Win32API.new('user32', 'GetKeyboardLayoutName', 'P', 'L')
LoadKeyLayout = Win32API.new('user32', 'LoadKeyboardLayout', 'pl', 'p')
def initialize
# @layout_name = '00000000'
# GetKeyLayout.call(@layout_name)
@layout_name = '00000409'
# @layout = LoadKeyLayout.call(@layout_name, 0x0000001)
@last = [0] * 256
@state = "\0" * 256
@repeat = Array.new(256, 0)
end
def update
@last = @state.clone#.unpack('B'*256)
result = GetKeyState.call(@state)
# @actual = @state.clone.unpack('B'*256)
256.times { |i|
if down?(@state[i]) == true
@repeat[i] += 1
@repeat[i] = 15 if @repeat[i] >= 17
else
@repeat[i] = 0
end }
end
def toogle?(key)
(@state[key] & KEY_TOOGLE_MASK == KEY_TOOGLE_MASK)
end
def down?(keystate)
(keystate & KEY_DOWN_MASK == KEY_DOWN_MASK)
end
def up?(keystate)
(keystate & KEY_DOWN_MASK != KEY_DOWN_MASK)
end
def trigger?(key)
key && down?(@state[key]) && up?(@last[key]) && validate_key(key)
end
def press?(key)
key && down?(@state[key])
end
def release?(key)
key && up?(@state[key]) && down?(@last[key])
end
def repeat?(key)
key && [1,16].include?(@repeat[key])
end
def validate_key(key)
return false if !(key.is_a?(Numeric))
key = key.to_i
return false if ((key < 0x01) || (key > 0x100) || !(key == key.to_i))
return true
end
end
2/3. Wklej w moduł Input.
class Gamepad
Keys = {}
Keys['Stick1 Up'] = 0
Keys['Stick1 Down'] = 1
Keys['Stick1 Left'] = 2
Keys['Stick1 Right'] = 3
Keys['Stick2 Up'] = 4
Keys['Stick2 Down'] = 5
Keys['Stick2 Left'] = 6
Keys['Stick2 Right'] = 7
Keys['Stick3 Up'] = 8
Keys['Stick3 Down'] = 9
Keys['Stick3 Left'] = 10
Keys['Stick3 Right'] = 11
Keys['Stick4 Up'] = 12
Keys['Stick4 Down'] = 13
Keys['Stick4 Left'] = 14
Keys['Stick4 Right'] = 15
Keys['DPad Up'] = 16
Keys['DPad Down'] = 17
Keys['DPad Left'] = 18
Keys['DPad Right'] = 19
32.times { |i| Keys['Button '+ sprintf('%02d', i+1)] = 20 + i }
Dead_Zone = [0, 0, 1000, 1000, 1000, 1000, 1000, 1000, 0, 0, 0, 1000, 1000]
GetPos = Win32API.new('winmm', 'joyGetPosEx', 'lp', 'l')
GetCaps = Win32API.new('winmm', 'joyGetDevCaps', 'LPL', 'l')
def initialize(id)
@id = id
@repeats = Array.new(52, 0)
@pluged_in = 162
@last = [52, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
@last_buttons = []
@actual = [52, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
@state = [52, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0].pack('L14')
@buttons = []
end
def actual
@actual
end
def update
@caps = '0'*404
GetCaps.call(@id, @caps, 404)
#@caps.unpack('S2Z32I19Z32Z260')
@last = @actual.clone
@last_buttons = @buttons.clone
@pluged_in = GetPos.call(@id, @state)
@actual = @state.unpack("L14")
update_buttons_down
update_repeat
end
def update_buttons_down
@buttons.clear
n = @actual[8]
all_buttons.times { |i|
next if 2 ** (all_buttons - i - 1) > n
@buttons.push(all_buttons - i)
n -= (2 ** (all_buttons - i - 1)) }
@buttons.sort!
end
def update_repeat
Keys.values.each { |i|
if down?(i, @actual, @buttons)
@repeats[i] += 1
@repeats[i] = 15 if @repeats[i] > 17
else
@repeats[i] = 0
end }
end
def axes
@caps[104]
end
def all_buttons
@caps[60]
end
def buttons
@buttons
end
def down?(index, state, buttons)
return false if index == nil
case index
when 0 then axis_tilt(state, 3) < 0 && axes >= 2
when 1 then axis_tilt(state, 3) > 0 && axes >= 2
when 2 then axis_tilt(state, 2) < 0 && axes >= 1
when 3 then axis_tilt(state, 2) > 0 && axes >= 1
when 4 then axis_tilt(state, 5) < 0 && axes >= 4
when 5 then axis_tilt(state, 5) > 0 && axes >= 4
when 6 then axis_tilt(state, 4) < 0 && axes >= 3
when 7 then axis_tilt(state, 4) > 0 && axes >= 3
when 8 then axis_tilt(state, 7) < 0 && axes >= 6
when 9 then axis_tilt(state, 7) > 0 && axes >= 6
when 10 then axis_tilt(state, 6) < 0 && axes >= 5
when 11 then axis_tilt(state, 6) > 0 && axes >= 5
when 12 then axis_tilt(state, 12) < 0 && axes >= 8
when 13 then axis_tilt(state, 12) < 0 && axes >= 8
when 14 then axis_tilt(state, 11) < 0 && axes >= 7
when 15 then axis_tilt(state, 11) < 0 && axes >= 7
when 16 then [31500, 0, 4500].include?(state[10])# && axes >= 1
when 17 then [13500, 18000, 22500].include?(state[10])# && axes >= 1
when 18 then [22500, 27000, 31500].include?(state[10])# && axes >= 1
when 19 then [4500, 9000, 13500].include?(state[10])# && axes >= 1
else
buttons.include?(index-19)
end
end
def up?(index, state, buttons)
case index
when 0 then axis_tilt(state, 3) == 0 && axes >= 2
when 1 then axis_tilt(state, 3) == 0 && axes >= 2
when 2 then axis_tilt(state, 2) == 0 && axes >= 1
when 3 then axis_tilt(state, 2) == 0 && axes >= 1
when 4 then axis_tilt(state, 5) == 0 && axes >= 4
when 5 then axis_tilt(state, 5) == 0 && axes >= 4
when 6 then axis_tilt(state, 4) == 0 && axes >= 3
when 7 then axis_tilt(state, 4) == 0 && axes >= 3
when 8 then axis_tilt(state, 7) == 0 && axes >= 6
when 9 then axis_tilt(state, 7) == 0 && axes >= 6
when 10 then axis_tilt(state, 6) == 0 && axes >= 5
when 11 then axis_tilt(state, 6) == 0 && axes >= 5
when 12 then axis_tilt(state, 12) == 0 && axes >= 8
when 13 then axis_tilt(state, 12) == 0 && axes >= 8
when 14 then axis_tilt(state, 11) == 0 && axes >= 7
when 15 then axis_tilt(state, 11) == 0 && axes >= 7
when 16 then [-1, 65535].include?(state[10])# && axes >= 1
when 17 then [-1, 65535].include?(state[10])# && axes >= 1
when 18 then [-1, 65535].include?(state[10])# && axes >= 1
when 19 then [-1, 65535].include?(state[10])# && axes >= 1
else
!(buttons.include?(index-19))
end
end
def trigger?(ctrl)
down?(Keys[ctrl], @actual, @buttons) && up?(Keys[ctrl], @last, @last_buttons)
end
def release?(ctrl)
down?(Keys[ctrl], @last, @last_buttons) && up?(Keys[ctrl],@actual, @buttons)
end
def press?(ctrl)
down?(Keys[ctrl], @actual, @buttons)
end
def repeat?(ctrl)
Keys[ctrl] != nil && [1,16].include?(@repeats[Keys[ctrl]])
end
def axis_tilt(buffer, index)
n = (buffer[index]) - 32767
n.abs > Dead_Zone[index] ? n : 0
end
end
1. Wklej do projektu ten moduł.
module Input
C = {}
C[:UP] = [[0, 'Arrow Up'], [1, 'Stick1 Up']]
C[:DOWN] = [[0, 'Arrow Down'], [1, 'Stick1 Down']]
C[:LEFT] = [[0, 'Arrow Left'], [1, 'Stick1 Left']]
C[:RIGHT] = [[0, 'Arrow Right'], [1, 'Stick1 Right']]
C[:A] = [[0, 'Z'], [1, 'Button 01']]
C[:B] = [[0, 'X'], [1, 'Button 02']]
C[:C] = [[0, 'C'], [1, 'Button 03']]
C[:X] = [[0, 'A'], [1, 'Button 04']]
C[:Y] = [[0, 'S'], [1, 'Button 05']]
C[:Z] = [[0, 'D'], [1, 'Button 06']]
C[:L] = [[0, 'Q'], [1, 'Button 07']]
C[:R] = [[0, 'W'], [1, 'Button 08']]
C[:SELECT] = [[0, 'Esc'], [1,'Button 09']]
C[:START] = [[0, ']'], [1, 'Button 10']]
#tu wstaw klasy kontrolerów
#/tu wstaw klasy kontrolerów
@keyboard = Keyboard.new
@gamepads = []
3.times { |i| @gamepads.push(Gamepad.new(i)) }
def self.keyboard
@keyboard
end
def self.gamepads
@gamepads
end
def self.update
@keyboard.update
@gamepads.each { |gamepad| gamepad.update }
end
def self.repeat?(key)
case key
when Symbol
C[key].any? { |key| key_repeat?(key) }
when Array
key_repeat?(key)
end
end
def self.key_repeat?(key)
if key[0] == 0
@keyboard.repeat?(Keyboard::Keys[key[1]])
elsif key[0] == 1
@gamepads.any? { |gamepad| gamepad.repeat?(key[1]) }
else
@gamepads[key[0] - 2].repeat?(key[1])
end
end
def self.trigger?(key)
case key
when Symbol
return C[key].any? { |key| key_trigger?(key) == true }
when Array
return key_trigger?(key) == true
end
false
end
def self.key_trigger?(key)
if key[0] == 0
@keyboard.trigger?(Keyboard::Keys[key[1]])
elsif key[0] == 1
@gamepads.any? { |gamepad| gamepad.trigger?(key[1]) }
else
@gamepads[key[0] - 2].trigger?(key[1])
end
end
def self.press?(key)
case key
when Symbol
return C[key].any? { |key| key_press?(key) == true }
when Array
return key_press?(key)
end
false
end
def self.key_press?(key)
if key[0] == 0
@keyboard.press?(Keyboard::Keys[key[1]])
elsif key[0] == 1
@gamepads.any? { |gamepad| gamepad.press?(key[1]) }
else
@gamepads[key[0] - 2].press?(key[1])
end
end
def self.dir8
dirs = []
dirs.push(2) if Input.press?(:DOWN)
dirs.push(4) if Input.press?(:LEFT)
dirs.push(6) if Input.press?(:RIGHT)
dirs.push(8) if Input.press?(:UP)
if dirs.include?(2) && dirs.include?(8)
[2, 8].each { |d| dirs.delete(d) }
end
if dirs.include?(4) && dirs.include?(6)
[4, 6].each { |d| dirs.delete(d) }
end
return 0 if dirs.empty?
if dirs.size == 2
return 1 if dirs.include?(2) && dirs.include?(4)
return 3 if dirs.include?(2) && dirs.include?(6)
return 7 if dirs.include?(8) && dirs.include?(4)
return 9 if dirs.include?(8) && dirs.include?(6)
end
dirs[0]
end
def self.dir4
dir8 if [0, 2, 4, 6, 8].include?(dir8)
end
Wymagania
Wersja RGSS1 z pliku "rgss102j.dll" (dla RPG Makera XP) (W RPG Makerze VX odpowiednik)
Instrukcja
Pusty skrypt, do którego wkleić poszczególne kawałki, może znajdować się pod ostatnim nadpisem modułu "Input". Instrukcje dodane do fragmentów kodu wykonać na owym pustym skrypcie.
Po wklejeniu skryptu do starszego RPG Makera, niż VX Ace standardowe kontrolki nie działają.
Funkcje
Pełna obsługa klawiatury
Obsługa przycisków myszy
Obsługa dowolnej liczby gamepadów
Możliwość przypisania konkretnego działania na konkretnym kontrolerze jako sygnał dla gry do wykonania akcji.
Aktualizacje
Niniejsza wersja pochodzi z projektu "HTF+MLP: Worlds in danger". Nie obsługuje blokady dla wygaszania ekranu.
Niestandardowe informacje
Użyj symbolu, aby użyć standardowej kontrolki (lista w stałej "C" modułu "Input")
Użyj zbioru, aby użyć konkretnej kontrolki (TYLKO Array)
Jeśli pierwszym elementem zbioru jest 0, to trzeba nacisnąć klawisz na klawiaturze lub przycisk myszy . Drugim musi wtedy być nazwa klawisza na klawiaturze lub przycisku myszy. (lista klawiszy w stałej "Keys" klasy "Keyboard")
Jeśli pierwszym elementem zbioru jest jest liczba 1 lub większa, to trzeba wykonać czynność powiązaną z działaniem na gamepadzie. Drugim musi wtedy być nazwa kontrolki na nim. (lista kontrolek gamepada w stałej "Keys" klasy "Gamepad").
Jeśli pierwszym elementem zbioru jest liczba 1, to sprawdzany jest każdy podłączony gamepad. Wyższa odpowiada konkretnemu.
Nienawidzony i nienawidzący. Przez nienawiść tego drugiego obaj będą martwi.
(Ten post był ostatnio modyfikowany: 14-04-17 09:39 przez Mateusz SSJ8.)
|
|