2022-06-02 04:13:43 +02:00
|
|
|
# ##############################################################################
|
|
|
|
#(G)odot (U)nit (T)est class
|
|
|
|
#
|
|
|
|
# ##############################################################################
|
|
|
|
# The MIT License (MIT)
|
|
|
|
# =====================
|
|
|
|
#
|
|
|
|
# Copyright (c) 2020 Tom "Butch" Wesley
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be included in
|
|
|
|
# all copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
# THE SOFTWARE.
|
|
|
|
#
|
|
|
|
# ##############################################################################
|
|
|
|
# Description
|
|
|
|
# -----------
|
|
|
|
# ##############################################################################
|
|
|
|
|
|
|
|
# Implemented InputEvent* convenience methods
|
|
|
|
# InputEventAction
|
|
|
|
# InputEventKey
|
|
|
|
# InputEventMouseButton
|
|
|
|
# InputEventMouseMotion
|
|
|
|
|
|
|
|
# Yet to implement InputEvents
|
|
|
|
# InputEventJoypadButton
|
|
|
|
# InputEventJoypadMotion
|
|
|
|
# InputEventMagnifyGesture
|
|
|
|
# InputEventMIDI
|
|
|
|
# InputEventPanGesture
|
|
|
|
# InputEventScreenDrag
|
|
|
|
# InputEventScreenTouch
|
|
|
|
|
|
|
|
static func _to_scancode(which):
|
|
|
|
var key_code = which
|
2023-01-20 23:34:39 +01:00
|
|
|
if typeof(key_code) == TYPE_STRING:
|
2022-11-09 21:57:46 +01:00
|
|
|
key_code = key_code.to_upper().to_ascii_buffer()[0]
|
2022-06-02 04:13:43 +02:00
|
|
|
return key_code
|
|
|
|
|
|
|
|
|
|
|
|
static func new_mouse_button_event(position, global_position, pressed, button_index):
|
|
|
|
var event = InputEventMouseButton.new()
|
|
|
|
event.position = position
|
2023-01-20 23:34:39 +01:00
|
|
|
if global_position != null:
|
2022-06-02 04:13:43 +02:00
|
|
|
event.global_position = global_position
|
2023-01-07 20:26:17 +01:00
|
|
|
event.pressed = pressed
|
2022-06-02 04:13:43 +02:00
|
|
|
event.button_index = button_index
|
|
|
|
|
|
|
|
return event
|
|
|
|
|
|
|
|
|
|
|
|
static func key_up(which):
|
|
|
|
var event = InputEventKey.new()
|
2023-01-07 20:26:17 +01:00
|
|
|
event.keycode = _to_scancode(which)
|
|
|
|
event.pressed = false
|
2022-06-02 04:13:43 +02:00
|
|
|
return event
|
|
|
|
|
|
|
|
|
|
|
|
static func key_down(which):
|
|
|
|
var event = InputEventKey.new()
|
2023-01-07 20:26:17 +01:00
|
|
|
event.keycode = _to_scancode(which)
|
|
|
|
event.pressed = true
|
2022-06-02 04:13:43 +02:00
|
|
|
return event
|
|
|
|
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
static func action_up(which, strength = 1.0):
|
|
|
|
var event = InputEventAction.new()
|
2022-06-02 04:13:43 +02:00
|
|
|
event.action = which
|
|
|
|
event.strength = strength
|
|
|
|
return event
|
|
|
|
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
static func action_down(which, strength = 1.0):
|
|
|
|
var event = InputEventAction.new()
|
2022-06-02 04:13:43 +02:00
|
|
|
event.action = which
|
|
|
|
event.strength = strength
|
2023-01-07 20:26:17 +01:00
|
|
|
event.pressed = true
|
2022-06-02 04:13:43 +02:00
|
|
|
return event
|
|
|
|
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
static func mouse_left_button_down(position, global_position = null):
|
2022-11-09 21:57:46 +01:00
|
|
|
var event = new_mouse_button_event(position, global_position, true, MOUSE_BUTTON_LEFT)
|
2022-06-02 04:13:43 +02:00
|
|
|
return event
|
|
|
|
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
static func mouse_left_button_up(position, global_position = null):
|
2022-11-09 21:57:46 +01:00
|
|
|
var event = new_mouse_button_event(position, global_position, false, MOUSE_BUTTON_LEFT)
|
2022-06-02 04:13:43 +02:00
|
|
|
return event
|
|
|
|
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
static func mouse_double_click(position, global_position = null):
|
2022-11-09 21:57:46 +01:00
|
|
|
var event = new_mouse_button_event(position, global_position, false, MOUSE_BUTTON_LEFT)
|
2023-01-07 20:26:17 +01:00
|
|
|
event.double_click = true
|
2022-06-02 04:13:43 +02:00
|
|
|
return event
|
|
|
|
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
static func mouse_right_button_down(position, global_position = null):
|
2022-11-09 21:57:46 +01:00
|
|
|
var event = new_mouse_button_event(position, global_position, true, MOUSE_BUTTON_RIGHT)
|
2022-06-02 04:13:43 +02:00
|
|
|
return event
|
|
|
|
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
static func mouse_right_button_up(position, global_position = null):
|
2022-11-09 21:57:46 +01:00
|
|
|
var event = new_mouse_button_event(position, global_position, false, MOUSE_BUTTON_RIGHT)
|
2022-06-02 04:13:43 +02:00
|
|
|
return event
|
|
|
|
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
static func mouse_motion(position, global_position = null):
|
2022-06-02 04:13:43 +02:00
|
|
|
var event = InputEventMouseMotion.new()
|
|
|
|
event.position = position
|
2023-01-20 23:34:39 +01:00
|
|
|
if global_position != null:
|
2022-06-02 04:13:43 +02:00
|
|
|
event.global_position = global_position
|
|
|
|
return event
|
|
|
|
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
static func mouse_relative_motion(offset, last_motion_event = null, speed = Vector2(0, 0)):
|
2022-06-02 04:13:43 +02:00
|
|
|
var event = null
|
2023-01-20 23:34:39 +01:00
|
|
|
if last_motion_event == null:
|
2022-06-02 04:13:43 +02:00
|
|
|
event = mouse_motion(offset)
|
2023-01-07 20:26:17 +01:00
|
|
|
event.velocity = speed
|
2022-06-02 04:13:43 +02:00
|
|
|
else:
|
|
|
|
event = last_motion_event.duplicate()
|
|
|
|
event.position += offset
|
|
|
|
event.global_position += offset
|
|
|
|
event.relative = offset
|
2023-01-07 20:26:17 +01:00
|
|
|
event.velocity = speed
|
2022-06-02 04:13:43 +02:00
|
|
|
return event
|