summaryrefslogtreecommitdiff
path: root/addons/gut/input_factory.gd
blob: 5505553a9e8bc5f3abc615a0190ccef8e656ed1e (plain)
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
# ##############################################################################
#(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
	if(typeof(key_code) == TYPE_STRING):
		key_code = key_code.to_upper().to_ascii()[0]
	return key_code


static func new_mouse_button_event(position, global_position, pressed, button_index):
    var event = InputEventMouseButton.new()
    event.position = position
    if(global_position != null):
        event.global_position = global_position
    event.pressed = pressed
    event.button_index = button_index

    return event


static func key_up(which):
	var event = InputEventKey.new()
	event.scancode = _to_scancode(which)
	event.pressed = false
	return event


static func key_down(which):
	var event = InputEventKey.new()
	event.scancode = _to_scancode(which)
	event.pressed = true
	return event


static func action_up(which, strength=1.0):
	var event  = InputEventAction.new()
	event.action = which
	event.strength = strength
	return event


static func action_down(which, strength=1.0):
	var event  = InputEventAction.new()
	event.action = which
	event.strength = strength
	event.pressed = true
	return event


static func mouse_left_button_down(position, global_position=null):
	var event = new_mouse_button_event(position, global_position, true, BUTTON_LEFT)
	return event


static func mouse_left_button_up(position, global_position=null):
	var event = new_mouse_button_event(position, global_position, false, BUTTON_LEFT)
	return event


static func mouse_double_click(position, global_position=null):
	var event = new_mouse_button_event(position, global_position, false, BUTTON_LEFT)
	event.doubleclick = true
	return event


static func mouse_right_button_down(position, global_position=null):
	var event = new_mouse_button_event(position, global_position, true, BUTTON_RIGHT)
	return event


static func mouse_right_button_up(position, global_position=null):
	var event = new_mouse_button_event(position, global_position, false, BUTTON_RIGHT)
	return event


static func mouse_motion(position, global_position=null):
	var event = InputEventMouseMotion.new()
	event.position = position
	if(global_position != null):
		event.global_position = global_position
	return event


static func mouse_relative_motion(offset, last_motion_event=null, speed=Vector2(0, 0)):
	var event = null
	if(last_motion_event == null):
		event = mouse_motion(offset)
		event.speed = speed
	else:
		event = last_motion_event.duplicate()
		event.position += offset
		event.global_position += offset
		event.relative = offset
		event.speed = speed
	return event