godot-third-person-basic-scene/scripts/common.gd

294 lines
7.9 KiB
GDScript3
Raw Normal View History

2022-02-25 00:28:14 +00:00
extends Node
2022-03-06 21:28:43 +00:00
var debug:bool = true
2022-04-15 14:37:38 +00:00
var mouse_camera_player_reverse_horizontal:bool = false
var mouse_camera_player_reverse_vertical:bool = false
var mouse_camera_only_reverse_horizontal:bool = false
var mouse_camera_only_reverse_vertical:bool = false
var mouse_camera_player_move_reverse_horizontal:bool = false
var mouse_camera_player_move_reverse_vertical:bool = false
2022-04-15 07:53:44 +00:00
func _ready():
randomize()
2022-03-25 18:20:34 +00:00
func get_time_text() -> String:
var time = Time.get_datetime_dict_from_system()
return "%s/%02d/%02d %02d:%02d:%02d" % [
time['year'], time['month'], time['day'],
time['hour'], time['minute'], time['second'],
]
2022-03-06 21:28:43 +00:00
2022-03-29 22:30:32 +00:00
func get_time_only_text() -> String:
var time = Time.get_datetime_dict_from_system()
return "%02d:%02d:%02d" % [
time['hour'], time['minute'], time['second'],
]
2022-03-06 21:28:43 +00:00
func msg_debug(text):
if debug:
var frame = get_stack()[1]
2022-03-25 18:20:34 +00:00
print("%s DEBUG [%s:%d] %s" % [get_time_text(), frame.source, frame.line, text] )
2022-03-06 21:28:43 +00:00
func msg_info(text):
var frame = get_stack()[1]
2022-03-25 18:20:34 +00:00
print("%s INFO [%s:%d] %s" % [get_time_text(), frame.source, frame.line, text] )
2022-03-06 21:28:43 +00:00
func msg_error(text):
var frame = get_stack()[1]
2022-03-25 18:20:34 +00:00
print("%s ERROR [%s:%d] %s" % [get_time_text(), frame.source, frame.line, text] )
2022-03-06 21:28:43 +00:00
2022-02-25 00:28:14 +00:00
func get_string_input_mousse_button(event:InputEventMouseButton) -> String:
match event.get_button_index():
1:
return "BUTTON_LEFT"
3:
return "BUTTON_MIDDLE"
2:
return "BUTTON_RIGHT"
4:
return "BUTTON_WHEEL_UP"
5:
return "BUTTON_WHEEL_DOWN"
6:
return "BUTTON_WHEEL_LEFT"
7:
return "BUTTON_WHEEL_RIGHT"
8:
return "BUTTON_XBUTTON1"
9:
return "BUTTON_XBUTTON2"
_:
return "MOUSSE BUTTON: " + str(event.get_button_index())
func get_string_input_joypad_button(event:InputEventJoypadButton) -> String:
match event.get_button_index():
0:
return "JOY_BUTTON_A"
1:
return "JOY_BUTTON_B"
2:
return "JOY_BUTTON_X"
3:
return "JOY_BUTTON_Y"
4:
return "JOY_BUTTON_BACK"
5:
return "JOY_BUTTON_GUIDE"
6:
return "JOY_BUTTON_START"
7:
return "JOY_BUTTON_LEFT_STICK"
8:
return "JOY_BUTTON_RIGHT_STICK"
9:
return "JOY_BUTTON_LEFT_SHOULDER"
10:
return "JOY_BUTTON_RIGHT_SHOULDER"
11:
return "JOY_BUTTON_DPAD_UP"
12:
return "JOY_BUTTON_DPAD_DOWN"
13:
return "JOY_BUTTON_DPAD_LEFT"
14:
return "JOY_BUTTON_DPAD_RIGHT"
_:
return "JOYPAD BUTTON: " + str(event.get_button_index())
func get_string_input_keyboard(event:InputEventKey) -> String:
var option:String = ""
if event.is_ctrl_pressed():
option += "Control+"
if event.is_shift_pressed():
option += "Shift+"
if event.is_alt_pressed():
option += "Alt+"
if event.is_meta_pressed():
option += "MetaKey+"
# if event.is_command_pressed():
# option += "Command+"
if event.keycode == 0:
if event.physical_keycode > 0:
return option + OS.get_keycode_string(event.physical_keycode)
else:
print("Error Event unknown keycode:" , event.keycode, ", physical_keycode:", event.physical_keycode)
return option + OS.get_keycode_string(event.keycode)
func get_string_input(event) -> String:
if event is InputEventKey:
return get_string_input_keyboard(event) # OS.get_scancode_string(event.get_scancode_with_modifiers())
elif event is InputEventMouseButton:
return get_string_input_mousse_button(event)
elif event is InputEventJoypadButton:
return get_string_input_joypad_button(event)
else:
return str(event)
2022-04-15 07:53:44 +00:00
2022-04-15 12:33:00 +00:00
#func compare_type_event(event1, event2):
# if event1 is InputEventKey and event2 is InputEventKey:
# return true
# elif event1 is InputEventMouseButton and event2 is InputEventMouseButton:
# return true
# elif event1 is InputEventJoypadButton and event2 is InputEventJoypadButton:
# return true
# return false
func get_event_plus_ref(action:String, event_ref):
if event_ref is InputEventMouseButton and InputMap.has_action(action + "_PLUS"):
for z in InputMap.action_get_events(action + "_PLUS"):
if z is InputEventMouseButton:
return z
return null
2022-04-15 07:53:44 +00:00
func get_input_plus_event(action:String, event_ref):
2022-04-15 12:33:00 +00:00
if event_ref is InputEventMouseButton and InputMap.has_action(action + "_PLUS"):
2022-04-15 07:53:44 +00:00
for z in InputMap.action_get_events(action + "_PLUS"):
2022-04-15 12:33:00 +00:00
if z is InputEventMouseButton:
2022-04-15 07:53:44 +00:00
return " & " + tr(Common.get_string_input(z))
return ""
func get_input_plus(action:String):
var comment:String = "None"
if InputMap.has_action(action):
for z in InputMap.action_get_events(action):
return tr(Common.get_string_input(z)) + get_input_plus_event(action, z)
# Common.msg_debug(comment)
# #$Window/Tab/MOUSE/HBox/SelectButton.set_text(text1)
# break
# if InputMap.has_action(action + "_PLUS"):
# for z in InputMap.action_get_events(action + "_PLUS"):
# if z is InputEventMouseButton:
# comment = comment + " & " + tr(Common.get_string_input(z))
# Common.msg_debug(comment)
# break
return comment
2022-04-15 12:33:00 +00:00
func set_input(action:String, event, origin, eraselast:bool):
2022-04-15 07:53:44 +00:00
if eraselast:
2022-04-15 22:12:12 +00:00
# for curaction in InputMap.get_actions():
# var text:String = curaction
# if text == action:
# for z in InputMap.action_get_events(curaction):
# if origin is InputEventMouseButton and z is InputEventMouseButton:
# InputMap.action_erase_event(text, z)
for z in InputMap.action_get_events(action):
if (origin is InputEventMouseButton and z is InputEventMouseButton) or \
(origin is InputEventJoypadMotion and z is InputEventJoypadMotion) or \
(origin is InputEventJoypadButton and z is InputEventJoypadButton):
InputMap.action_erase_event(action, z)
2022-04-15 12:33:00 +00:00
if event != null:
2022-04-15 22:12:12 +00:00
Common.msg_debug("action: " + str(action) + " / event:" + str(event))
2022-04-15 12:33:00 +00:00
InputMap.action_add_event(action, event)
2022-04-15 07:53:44 +00:00
func set_input_plus(action:String, event, eventplus, eraselast:bool):
2022-04-15 22:12:12 +00:00
Common.msg_debug("set_input_plus: " + str(action) + " eraselast:" + str(eraselast))
2022-04-15 12:33:00 +00:00
set_input(action, event, event, eraselast)
set_input(action + "_PLUS", eventplus, event, eraselast)
2022-04-15 14:37:38 +00:00
func set_mouse_camera_player_reverse_horizontal(value:bool):
mouse_camera_player_reverse_horizontal = value
func set_mouse_camera_player_reverse_vertical(value:bool):
mouse_camera_player_reverse_vertical = value
func set_mouse_camera_only_reverse_horizontal(value:bool):
mouse_camera_only_reverse_horizontal = value
func set_mouse_camera_only_reverse_vertical(value:bool):
mouse_camera_only_reverse_vertical = value
func set_mouse_camera_player_move_reverse_horizontal(value:bool):
mouse_camera_player_move_reverse_horizontal = value
func set_mouse_camera_player_move_reverse_vertical(value:bool):
mouse_camera_player_move_reverse_vertical = value
func get_mouse_camera_player_reverse_horizontal() -> bool:
return mouse_camera_player_reverse_horizontal
func get_mouse_camera_player_reverse_vertical() -> bool:
return mouse_camera_player_reverse_vertical
func get_mouse_camera_only_reverse_horizontal() -> bool:
return mouse_camera_only_reverse_horizontal
func get_mouse_camera_only_reverse_vertical() -> bool:
return mouse_camera_only_reverse_vertical
func get_mouse_camera_player_move_reverse_horizontal() -> bool:
return mouse_camera_player_move_reverse_horizontal
func get_mouse_camera_player_move_reverse_vertical() -> bool:
return mouse_camera_player_move_reverse_vertical
func get_mul_mouse_camera_player_reverse_horizontal() -> float:
if mouse_camera_player_reverse_horizontal:
return -1.0
else:
return 1.0
func get_mul_mouse_camera_player_reverse_vertical() -> float:
if mouse_camera_player_reverse_vertical:
return -1.0
else:
return 1.0
func get_mul_mouse_camera_only_reverse_horizontal() -> float:
if mouse_camera_only_reverse_horizontal:
return -1.0
else:
return 1.0
func get_mul_mouse_camera_only_reverse_vertical() -> float:
if mouse_camera_only_reverse_vertical:
return -1.0
else:
return 1.0
func get_mul_mouse_camera_player_move_reverse_horizontal() -> float:
if mouse_camera_player_move_reverse_horizontal:
return -1.0
else:
return 1.0
func get_mul_mouse_camera_player_move_reverse_vertical() -> float:
if mouse_camera_player_move_reverse_vertical:
return -1.0
else:
return 1.0