fix syntax
This commit is contained in:
100
action.cm
100
action.cm
@@ -25,55 +25,60 @@ action.get_icon_for_action = function(action)
|
||||
{
|
||||
var bindings = this.get_bindings_for_device(action)
|
||||
if (!length(bindings)) return null
|
||||
|
||||
|
||||
var primary_binding = bindings[0]
|
||||
|
||||
var button = null
|
||||
var key_mapping = {
|
||||
'escape': 'escape',
|
||||
'return': 'return',
|
||||
'space': 'space',
|
||||
'up': 'arrow_up',
|
||||
'down': 'arrow_down',
|
||||
'left': 'arrow_left',
|
||||
'right': 'arrow_right'
|
||||
}
|
||||
var key = null
|
||||
var controller_prefix = null
|
||||
var gamepad_mapping = null
|
||||
var icon_name = null
|
||||
|
||||
if (this.current_device == 'keyboard') {
|
||||
if (starts_with(primary_binding, 'mouse_button_')) {
|
||||
var button = replace(primary_binding, 'mouse_button_', '')
|
||||
button = replace(primary_binding, 'mouse_button_', '')
|
||||
return 'ui/mouse/mouse_' + button + '.png'
|
||||
} else {
|
||||
// Handle special keyboard keys
|
||||
var key_mapping = {
|
||||
'escape': 'escape',
|
||||
'return': 'return',
|
||||
'space': 'space',
|
||||
'up': 'arrow_up',
|
||||
'down': 'arrow_down',
|
||||
'left': 'arrow_left',
|
||||
'right': 'arrow_right'
|
||||
}
|
||||
var key = key_mapping[primary_binding] || primary_binding
|
||||
key = key_mapping[primary_binding] || primary_binding
|
||||
return 'ui/keyboard/keyboard_' + key + '.png'
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (this.current_device == 'gamepad' && this.current_gamepad_type) {
|
||||
var controller_prefix = controller_map[this.current_gamepad_type] || 'playstation'
|
||||
|
||||
controller_prefix = controller_map[this.current_gamepad_type] || 'playstation'
|
||||
|
||||
// Map gamepad inputs to icon names
|
||||
var gamepad_mapping = {
|
||||
gamepad_mapping = {
|
||||
'gamepad_a': this.current_gamepad_type == 'ps5' || this.current_gamepad_type == 'ps4' || this.current_gamepad_type == 'ps3' ? 'playstation_button_cross' : 'xbox_button_a',
|
||||
'gamepad_b': this.current_gamepad_type == 'ps5' || this.current_gamepad_type == 'ps4' || this.current_gamepad_type == 'ps3' ? 'playstation_button_circle' : 'xbox_button_b',
|
||||
'gamepad_b': this.current_gamepad_type == 'ps5' || this.current_gamepad_type == 'ps4' || this.current_gamepad_type == 'ps3' ? 'playstation_button_circle' : 'xbox_button_b',
|
||||
'gamepad_x': this.current_gamepad_type == 'ps5' || this.current_gamepad_type == 'ps4' || this.current_gamepad_type == 'ps3' ? 'playstation_button_square' : 'xbox_button_x',
|
||||
'gamepad_y': this.current_gamepad_type == 'ps5' || this.current_gamepad_type == 'ps4' || this.current_gamepad_type == 'ps3' ? 'playstation_button_triangle' : 'xbox_button_y',
|
||||
'gamepad_dpup': controller_prefix + '_dpad_up',
|
||||
'gamepad_dpdown': controller_prefix + '_dpad_down',
|
||||
'gamepad_dpdown': controller_prefix + '_dpad_down',
|
||||
'gamepad_dpleft': controller_prefix + '_dpad_left',
|
||||
'gamepad_dpright': controller_prefix + '_dpad_right',
|
||||
'gamepad_l1': controller_prefix + '_trigger_l1',
|
||||
'gamepad_r1': controller_prefix + '_trigger_r1',
|
||||
'gamepad_r1': controller_prefix + '_trigger_r1',
|
||||
'gamepad_l2': controller_prefix + '_trigger_l2',
|
||||
'gamepad_r2': controller_prefix + '_trigger_r2',
|
||||
'gamepad_start': this.get_start_button_icon()
|
||||
}
|
||||
|
||||
var icon_name = gamepad_mapping[primary_binding]
|
||||
|
||||
icon_name = gamepad_mapping[primary_binding]
|
||||
if (icon_name) {
|
||||
return 'ui/' + controller_prefix + '/' + icon_name + '.png'
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
@@ -181,23 +186,26 @@ var SWIPE_MAX_TIME = 500 // ms
|
||||
action.on_input = function(action_id, evt)
|
||||
{
|
||||
// 1) Detect & store which device user is on (only from raw input, ignore passive inputs)
|
||||
var new_device = null
|
||||
var is_real_kb_mouse = false
|
||||
var gamepad_type = null
|
||||
if (action_id != 'mouse_move' && !starts_with(action_id, 'mouse_pos') && evt.pressed) {
|
||||
var new_device = detect_device(action_id)
|
||||
|
||||
new_device = detect_device(action_id)
|
||||
|
||||
// For keyboard/mouse detection, also check if the event has modifier keys or is a mouse button
|
||||
// This helps distinguish real keyboard/mouse events from mapped actions
|
||||
var is_real_kb_mouse = (new_device == 'keyboard' &&
|
||||
(evt.ctrl != null || evt.shift != null || evt.alt != null ||
|
||||
starts_with(action_id, 'mouse_button')))
|
||||
|
||||
is_real_kb_mouse = (new_device == 'keyboard' &&
|
||||
(evt.ctrl != null || evt.shift != null || evt.alt != null ||
|
||||
starts_with(action_id, 'mouse_button')))
|
||||
|
||||
if (new_device == 'keyboard' && !is_real_kb_mouse) {
|
||||
// This might be a mapped action, not a real keyboard/mouse event
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
if (new_device != this.current_device) {
|
||||
if (new_device == 'gamepad') {
|
||||
var gamepad_type = evt.which != null ? input.gamepad_id_to_type(evt.which) : 'unknown'
|
||||
gamepad_type = evt.which != null ? input.gamepad_id_to_type(evt.which) : 'unknown'
|
||||
log.console("Input switched to 'gamepad' (event: " + action_id + ", type: " + gamepad_type + ")")
|
||||
this.current_gamepad_type = gamepad_type
|
||||
} else if (this.current_device == 'gamepad') {
|
||||
@@ -236,8 +244,9 @@ action.on_input = function(action_id, evt)
|
||||
})
|
||||
|
||||
// Send all matched actions (only if we found mappings - this means it's a raw input)
|
||||
var i = 0
|
||||
if (length(matched_actions) > 0) {
|
||||
for (var i = 0; i < length(matched_actions); i++) {
|
||||
for (i = 0; i < length(matched_actions); i++) {
|
||||
// scene.recurse(game.root, 'on_input', [matched_actions[i], evt])
|
||||
}
|
||||
}
|
||||
@@ -261,7 +270,8 @@ action.rebind_action = function(action_name, new_key) {
|
||||
|
||||
// Clear existing bindings for the current device from the target action
|
||||
var target_bindings = this.action_map[action_name]
|
||||
for (var i = length(target_bindings) - 1; i >= 0; i--) {
|
||||
var i = length(target_bindings) - 1
|
||||
for (; i >= 0; i--) {
|
||||
if (detect_device(target_bindings[i]) == this.current_device)
|
||||
this.action_map[action_name] = array(array(this.action_map[action_name], 0, i), array(this.action_map[action_name], i+1))
|
||||
}
|
||||
@@ -299,22 +309,28 @@ action.get_current_device_binding = function(action_name) {
|
||||
}
|
||||
|
||||
action.save_bindings = function() {
|
||||
try {
|
||||
io.slurpwrite('keybindings.json', json.encode(this.action_map))
|
||||
} catch(e) {
|
||||
log.console("Failed to save key bindings:", e)
|
||||
var self = this
|
||||
var _save = function() {
|
||||
io.slurpwrite('keybindings.json', json.encode(self.action_map))
|
||||
} disruption {
|
||||
log.console("Failed to save key bindings")
|
||||
}
|
||||
_save()
|
||||
}
|
||||
|
||||
action.load_bindings = function() {
|
||||
try {
|
||||
var self = this
|
||||
var _load = function() {
|
||||
var data = null
|
||||
var bindings = null
|
||||
if (io.exists('keybindings.json')) {
|
||||
var data = io.slurp('keybindings.json')
|
||||
var bindings = object(json.decode(data), this.action_map)
|
||||
data = io.slurp('keybindings.json')
|
||||
bindings = object(json.decode(data), self.action_map)
|
||||
}
|
||||
} catch(e) {
|
||||
log.console("Failed to load key bindings:", e)
|
||||
} disruption {
|
||||
log.console("Failed to load key bindings")
|
||||
}
|
||||
_load()
|
||||
}
|
||||
|
||||
action.reset_to_defaults = function() {
|
||||
|
||||
Reference in New Issue
Block a user