Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn on down without release #333

Merged
merged 2 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions addons/gut/input_sender.gd
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,15 @@ class InputQueueItem:
var t = _delay_timer(time_delay)
t.connect("timeout", self, "_on_time_timeout")


# ##############################################################################
#
# ##############################################################################
var _utils = load('res://addons/gut/utils.gd').get_instance()
var InputFactory = load("res://addons/gut/input_factory.gd")

const INPUT_WARN = 'If using Input as a reciever it will not respond to *_down events until a *_up event is recieved. Call the appropriate *_up event or use .hold_for(...) to automatically release after some duration.'

var _lgr = _utils.get_logger()
var _receivers = []
var _input_queue = []
var _next_queue_item = null
Expand All @@ -119,10 +126,16 @@ func _init(r=null):

func _send_event(event):
if(event is InputEventKey):
if((event.pressed and !event.echo) and is_key_pressed(event.scancode)):
_lgr.warn(str("InputSender: key_down called for ", event.as_text(), " when that key is already pressed. ", INPUT_WARN))
_pressed_keys[event.scancode] = event.pressed
elif(event is InputEventAction):
if(event.pressed and is_action_pressed(event.action)):
_lgr.warn(str("InputSender: action_down called for ", event.action, " when that action is already pressed. ", INPUT_WARN))
_pressed_actions[event.action] = event.pressed
elif(event is InputEventMouseButton):
if(event.pressed and is_mouse_button_pressed(event.button_index)):
_lgr.warn(str("InputSender: mouse_button_down called for ", event.button_index, " when that mouse button is already pressed. ", INPUT_WARN))
_pressed_mouse_buttons[event.button_index] = event

for r in _receivers:
Expand Down Expand Up @@ -155,6 +168,7 @@ func _on_queue_item_ready(item):
done_event.queue_free()

if(_input_queue.size() == 0):
_next_queue_item = null
emit_signal("idle")
else:
_input_queue[0].start()
Expand Down Expand Up @@ -338,4 +352,14 @@ func clear():
_pressed_mouse_buttons.clear()

func is_idle():
return _input_queue.size() == 0
return _input_queue.size() == 0

func is_key_pressed(which):
var event = InputFactory.key_up(which)
return _pressed_keys.has(event.scancode) and _pressed_keys[event.scancode]

func is_action_pressed(which):
return _pressed_actions.has(which) and _pressed_actions[which]

func is_mouse_button_pressed(which):
return _pressed_mouse_buttons.has(which) and _pressed_mouse_buttons[which]
104 changes: 104 additions & 0 deletions test/unit/test_input_sender.gd
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ class InputTracker:
class TestTheBasics:
extends "res://addons/gut/test.gd"

func before_all():
InputMap.add_action("jump")

func after_all():
InputMap.erase_action("jump")

func test_can_make_one():
assert_not_null(InputSender.new())

Expand Down Expand Up @@ -93,6 +99,103 @@ class TestTheBasics:




func test_is_key_pressed_false_by_default():
var sender = InputSender.new()
assert_false(sender.is_key_pressed("F"))

func test_is_key_pressed_true_when_sent_key():
var sender = InputSender.new()
sender.key_down("F")
assert_true(sender.is_key_pressed("F"))

func test_is_action_pressed_false_by_default():
var sender = InputSender.new()
assert_false(sender.is_action_pressed("jump"))

func test_is_action_pressed_true_when_action_sent():
var sender = InputSender.new()
sender.action_down("jump")
assert_true(sender.is_action_pressed("jump"))

func test_is_mouse_button_pressed_false_by_default():
var sender = InputSender.new()
assert_false(sender.is_mouse_button_pressed(BUTTON_LEFT))

func test_is_mouse_button_pressed_true_when_button_sent():
var sender = InputSender.new()
sender.mouse_right_button_down(Vector2(1,1))
assert_true(sender.is_mouse_button_pressed(BUTTON_RIGHT))

func test_warns_when_key_down_for_a_pressed_key():
var sender = InputSender.new()
var lgr = _utils.Logger.new()
sender._lgr = lgr
sender.key_down("S")
sender.key_down("S")
assert_eq(lgr.get_warnings().size(), 1)

func test_does_now_warn_for_key_up():
var sender = InputSender.new()
var lgr = _utils.Logger.new()
sender._lgr = lgr
sender.key_down("S")
sender.key_up("S")
assert_eq(lgr.get_warnings().size(), 0)

func test_does_not_warn_for_key_echos():
var sender = InputSender.new()
var lgr = _utils.Logger.new()
sender._lgr = lgr
sender.key_down("S")
sender.key_echo()
sender.key_echo()
assert_eq(lgr.get_warnings().size(), 0)

func test_warns_when_action_down_for_a_pressed_action():
var sender = InputSender.new()
var lgr = _utils.Logger.new()
sender._lgr = lgr
sender.action_down("jump")
sender.action_down("jump")
assert_eq(lgr.get_warnings().size(), 1)

func test_does_not_warn_for_action_up():
var sender = InputSender.new()
var lgr = _utils.Logger.new()
sender._lgr = lgr
sender.action_down("jump")
sender.action_up("jump")
assert_eq(lgr.get_warnings().size(), 0)

func test_warns_when_mouse_down_for_a_pressed_mouse_button():
var sender = InputSender.new()
var lgr = _utils.Logger.new()
sender._lgr = lgr
sender.mouse_right_button_down(Vector2(1,1))
sender.mouse_right_button_down(Vector2(1,1))
assert_eq(lgr.get_warnings().size(), 1)

func test_does_not_warn_for_mouse_up():
var sender = InputSender.new()
var lgr = _utils.Logger.new()
sender._lgr = lgr
sender.mouse_right_button_down(Vector2(1,1))
sender.mouse_right_button_up(Vector2(1,1))
assert_eq(lgr.get_warnings().size(), 0)

func test_warns_for_2nd_down_event_after_idle():
var sender = InputSender.new()
var lgr = _utils.Logger.new()
sender._lgr = lgr

sender.key_down("R").wait(.2)
yield(sender, 'idle')

sender.key_down("R")
assert_eq(lgr.get_warnings().size(), 1)


class TestCreateKeyEvents:
extends "res://addons/gut/test.gd"

Expand Down Expand Up @@ -462,6 +565,7 @@ class TestSequence:
yield(yield_to(sender, "idle", 5), YIELD)
assert_eq(r.inputs[2].position, Vector2(6, 6))


class TestHoldFor:
extends "res://addons/gut/test.gd"

Expand Down