diff options
| author | MaciejDym <[email protected]> | 2021-01-08 18:37:07 +0100 |
|---|---|---|
| committer | Amir Rajan <[email protected]> | 2021-01-11 04:26:28 -0600 |
| commit | b6e6b9187ef88f4900b8b29bbce56ec1631ce099 (patch) | |
| tree | b78e1c55c0392f205252f17c1ce9aa268c683b5b | |
| parent | d430e6bd3434b6262169012377d9b5b27f0356ba (diff) | |
| download | dragonruby-game-toolkit-contrib-b6e6b9187ef88f4900b8b29bbce56ec1631ce099.tar.gz dragonruby-game-toolkit-contrib-b6e6b9187ef88f4900b8b29bbce56ec1631ce099.zip | |
Added support for `HOME`, `END`, `Ctrl+left` and `Ctrl+right`
| -rw-r--r-- | dragon/console.rb | 16 | ||||
| -rw-r--r-- | dragon/console_prompt.rb | 34 | ||||
| -rw-r--r-- | dragon/inputs.rb | 6 |
3 files changed, 53 insertions, 3 deletions
diff --git a/dragon/console.rb b/dragon/console.rb index c07c36b..ddf0702 100644 --- a/dragon/console.rb +++ b/dragon/console.rb @@ -436,6 +436,10 @@ S if args.inputs.keyboard.key_down.control || args.inputs.keyboard.key_down.meta prompt << $gtk.ffi_misc.getclipboard end + elsif args.inputs.keyboard.key_down.home + prompt.move_cursor_home + elsif args.inputs.keyboard.key_down.end + prompt.move_cursor_end elsif args.inputs.keyboard.key_down.up if @command_history_index == -1 @nonhistory_input = current_input_str @@ -454,9 +458,17 @@ S self.current_input_str = @command_history[@command_history_index].dup end elsif args.inputs.keyboard.key_down.left - prompt.move_cursor_left + if args.inputs.keyboard.key_down.control + prompt.move_cursor_left_word + else + prompt.move_cursor_left + end elsif args.inputs.keyboard.key_down.right - prompt.move_cursor_right + if args.inputs.keyboard.key_down.control + prompt.move_cursor_right_word + else + prompt.move_cursor_right + end 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 e8c2820..9fb1b95 100644 --- a/dragon/console_prompt.rb +++ b/dragon/console_prompt.rb @@ -32,6 +32,7 @@ module GTK def current_input_str=(str) @current_input_str = str @cursor_position = str.length + update_cursor_position_px end def <<(str) @@ -63,11 +64,44 @@ module GTK update_cursor_position_px end + # ? Works well enough, could be better + # TODO: Maybe possibly add skipping multiple word-breaking characters + # TODO: (requires re-writing a lot of the code most likely) + # ?? how can we put in regex? the code would be cleaner that way \ amir please fix :P \\ abolish consoles :> + def move_cursor_left_word + str = @current_input_str[0..[@cursor_position - 1, 0].sort[1]] + # ? Can be changed, it was just taken from my editor settings :> + @cursor_position = ("`~!@#$%^&*-=+()[]{}\|;:'\",.<>/?_ \t\n\0".chars.map { |char| + (val = str.rindex char) ? (val + 1 == @cursor_position ? val : val + 1) : 0 + }.sort.reverse[0]) + update_cursor_position_px + end + def move_cursor_right @cursor_position += 1 if @cursor_position < current_input_str.length update_cursor_position_px end + def move_cursor_right_word + str = @current_input_str[@cursor_position..@current_input_str.length] + # ? Can be changed, it was just taken from my editor settings :> + cand = ("`~!@#$%^&*-=+()[]{}\|;:'\",.<>/?_ \t\n\0".chars.map { |char| + (val = str.index char) ? val : 0 + }.sort - [0]) + (cand == []) ? @cursor_position = @current_input_str.length : @cursor_position += (cand[0]+0) + update_cursor_position_px + end + + def move_cursor_home + @cursor_position = 0 + update_cursor_position_px + end + + def move_cursor_end + @cursor_position = @current_input_str.length + update_cursor_position_px + end + def clear @current_input_str = '' @cursor_position = 0 diff --git a/dragon/inputs.rb b/dragon/inputs.rb index 30f1a2d..6e6439d 100644 --- a/dragon/inputs.rb +++ b/dragon/inputs.rb @@ -33,6 +33,7 @@ module GTK :control_left, :control_right, :alt_left, :alt_right, :meta_left, :meta_right, + :home, :end, :left, :right, :up, :down, :pageup, :pagedown, :char, :plus, :at, :forward_slash, :back_slash, :asterisk, :less_than, :greater_than, :carat, :ampersand, :superscript_two, @@ -48,6 +49,8 @@ module GTK raw_key == 1073741906 || raw_key == 1073741899 || raw_key == 1073741902 || + raw_key == 1073741898 || + raw_key == 1073741901 || (raw_key >= 1073742048 && raw_key <= 1073742055) # Modifier Keys char = KeyboardKeys.char_with_shift raw_key, modifier @@ -168,6 +171,8 @@ module GTK "?" => [:question_mark], '%' => [:percent_sign], "ยบ" => [:ordinal_indicator], + 1073741898 => [:home], + 1073741901 => [:end], 1073741903 => [:right], 1073741904 => [:left], 1073741905 => [:down], @@ -270,7 +275,6 @@ module GTK if m.end_with_bang? clear_after_return = true end - value = self.instance_variable_get("@#{m.without_ending_bang}".to_sym) clear_key m if clear_after_return [m.without_ending_bang, value] |
