summaryrefslogtreecommitdiffhomepage
path: root/dragon
diff options
context:
space:
mode:
authorAmir Rajan <[email protected]>2020-10-04 11:00:32 -0500
committerGitHub <[email protected]>2020-10-04 11:00:32 -0500
commitcdf663a63bf59af5eddfd6e9f4ba065516082c13 (patch)
tree76a72746edf10b537cac093ffde2cfebb042da5b /dragon
parent20d5b4057b44ffcf92478b2a8e9476ace2fdc0f5 (diff)
parent6d5b91289a6db11f7fae2453fdee8297a495773e (diff)
downloaddragonruby-game-toolkit-contrib-cdf663a63bf59af5eddfd6e9f4ba065516082c13.tar.gz
dragonruby-game-toolkit-contrib-cdf663a63bf59af5eddfd6e9f4ba065516082c13.zip
Merge pull request #17 from kfischer-okarin/cursor-movement
Add left/right cursor movement to the console
Diffstat (limited to 'dragon')
-rw-r--r--dragon/console.rb4
-rw-r--r--dragon/console_prompt.rb26
2 files changed, 27 insertions, 3 deletions
diff --git a/dragon/console.rb b/dragon/console.rb
index 1dea2e0..32deb15 100644
--- a/dragon/console.rb
+++ b/dragon/console.rb
@@ -431,6 +431,10 @@ S
@command_history_index -= 1
self.current_input_str = @command_history[@command_history_index].dup
end
+ elsif args.inputs.keyboard.key_down.left
+ prompt.move_cursor_left
+ elsif args.inputs.keyboard.key_down.right
+ prompt.move_cursor_right
elsif inputs_scroll_up_full? args
scroll_up_full
elsif inputs_scroll_down_full? args
diff --git a/dragon/console_prompt.rb b/dragon/console_prompt.rb
index aea5df8..3d1257b 100644
--- a/dragon/console_prompt.rb
+++ b/dragon/console_prompt.rb
@@ -18,23 +18,43 @@ module GTK
@cursor_color = Color.new [187, 21, 6]
@console_text_width = console_text_width
+ @cursor_position = 0
+
@last_autocomplete_prefix = nil
@next_candidate_index = 0
end
+ def current_input_str=(str)
+ @current_input_str = str
+ @cursor_position = str.length
+ end
+
def <<(str)
- @current_input_str << str
+ @current_input_str = @current_input_str[0...@cursor_position] + str + @current_input_str[@cursor_position..-1]
+ @cursor_position += str.length
@current_input_changed_at = Kernel.global_tick_count
reset_autocomplete
end
def backspace
- @current_input_str.chop!
+ return if current_input_str.length.zero? || @cursor_position.zero?
+
+ @current_input_str = @current_input_str[0...(@cursor_position - 1)] + @current_input_str[@cursor_position..-1]
+ @cursor_position -= 1
reset_autocomplete
end
+ def move_cursor_left
+ @cursor_position -= 1 if @cursor_position > 0
+ end
+
+ def move_cursor_right
+ @cursor_position += 1 if @cursor_position < current_input_str.length
+ end
+
def clear
@current_input_str = ''
+ @cursor_position = 0
reset_autocomplete
end
@@ -112,7 +132,7 @@ S
def render(args, x:, y:)
args.outputs.reserved << font_style.label(x: x, y: y, text: "#{@prompt}#{current_input_str}", color: @text_color)
- args.outputs.reserved << font_style.label(x: x - 2, y: y + 3, text: (" " * (@prompt.length + current_input_str.length)) + "|", color: @cursor_color)
+ args.outputs.reserved << font_style.label(x: x - 4, y: y + 3, text: (" " * (@prompt.length + @cursor_position)) + "|", color: @cursor_color)
end
def tick