Skip to content

Commit c64e8f2

Browse files
ximblinry
authored andcommitted
terminal: Change input handling
1. Handle page up / down for scrolling terminal history 2. Handle repeat keypresses, so e.g. holding pgdn will scroll 3. _input is passed a single event, so we save some CPU cycles by using `elif` for all branches
1 parent 88eeef9 commit c64e8f2

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

scenes/terminal.gd

+28-20
Original file line numberDiff line numberDiff line change
@@ -45,29 +45,14 @@ func _ready():
4545
history_position = game.state["history"].size()
4646

4747
func _input(event):
48-
if not input.has_focus():
48+
if not input.has_focus() or not event.is_pressed():
4949
return
5050

51-
if game.state["history"].size() > 0:
52-
if event.is_action_pressed("ui_up"):
53-
if history_position > 0:
54-
history_position -= 1
55-
input.text = game.state["history"][history_position]
56-
input.caret_position = input.text.length()
57-
# This prevents the Input taking the arrow as a "skip to beginning" command.
58-
get_tree().set_input_as_handled()
59-
if event.is_action_pressed("ui_down"):
60-
if history_position < game.state["history"].size()-1:
61-
history_position += 1
62-
input.text = game.state["history"][history_position]
63-
input.caret_position = input.text.length()
64-
get_tree().set_input_as_handled()
65-
66-
if event.is_action_pressed("tab_complete"):
51+
if event.is_action("tab_complete"):
6752
if completions.visible:
6853
completions.get_root().get_children().select(0)
6954
get_tree().set_input_as_handled()
70-
if event.is_action_pressed("delete_word"):
55+
elif event.is_action("delete_word"):
7156
var first_half = input.text.substr(0,input.caret_position)
7257
var second_half = input.text.substr(input.caret_position)
7358

@@ -77,9 +62,32 @@ func _input(event):
7762
input.caret_position = idx+1
7863
else:
7964
input.text = "" + second_half
80-
if event.is_action_pressed("clear"):
65+
elif event.is_action("clear"):
8166
clear()
82-
67+
elif event.is_action("ui_page_up"):
68+
var scroll = output.get_v_scroll()
69+
scroll.set_value(scroll.value - output.get_rect().size.y / 2)
70+
elif event.is_action("ui_page_down"):
71+
var scroll = output.get_v_scroll()
72+
scroll.set_value(scroll.value + output.get_rect().size.y / 2)
73+
elif game.state["history"].size() > 0:
74+
if event.is_action("ui_up"):
75+
if history_position > 0:
76+
history_position -= 1
77+
input.text = game.state["history"][history_position]
78+
input.caret_position = input.text.length()
79+
# This prevents the Input taking the arrow as a "skip to beginning" command.
80+
get_tree().set_input_as_handled()
81+
elif event.is_action("ui_down"):
82+
if history_position < game.state["history"].size():
83+
history_position += 1
84+
if history_position == game.state["history"].size():
85+
input.text = ""
86+
else:
87+
input.text = game.state["history"][history_position]
88+
input.caret_position = input.text.length()
89+
get_tree().set_input_as_handled()
90+
8391
func load_command(id):
8492
input.text = premade_commands[id]
8593
input.caret_position = input.text.length()

0 commit comments

Comments
 (0)