Module: Input::Console

Defined in:
mygame/lib/sprite_kit/vendor/input.rb

Instance Method Summary collapse

Instance Method Details

#autocomplete ⇒ Object



2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 2130

def autocomplete
  @autocompleting = true
  @prompt.shift_lock = true

  @autocomplete_menu ||= Menu.new(
    focussed: false,
    text_color: 0xDDDDDD,
    background_color: 0x333333DD
  )

  autocomplete_prefix
  autocomplete_next(0)
rescue Exception => e
  puts "* BUG: Tab autocompletion failed. Let us know about this.\n#{e}"
  puts e.backtrace
end

#autocomplete_accept ⇒ Object



2195
2196
2197
2198
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 2195

def autocomplete_accept
  @prompt.selection_end = @prompt.selection_start
  autocomplete_clear
end

#autocomplete_clear ⇒ Object



2200
2201
2202
2203
2204
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 2200

def autocomplete_clear
  @autocompleting = false
  @autocomplete_prefix = nil
  @prompt.shift_lock = false
end

#autocomplete_items(prefix) ⇒ Object



2182
2183
2184
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 2182

def autocomplete_items(prefix)
  autocomplete_object.autocomplete_methods.map(&:to_s).select { |m| m.start_with? prefix }
end

#autocomplete_next(dir) ⇒ Object



2163
2164
2165
2166
2167
2168
2169
2170
2171
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 2163

def autocomplete_next(dir)
  return unless @autocompleting

  @autocomplete_menu.selected_index -= dir
  @prompt.value = display_autocomplete_candidate(@autocomplete_menu.value)
  @prompt.selection_start = @prompt.value.length
rescue => e
  puts "Error in #autocomplete_next(#{dir}): <#{e.class.name}> #{e.message}"
end

#autocomplete_object ⇒ Object



2173
2174
2175
2176
2177
2178
2179
2180
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 2173

def autocomplete_object
  return GTK::ConsoleEvaluator unless @autocomplete_period_index

  GTK::ConsoleEvaluator.eval(@prompt.value[0, @autocomplete_period_index])
rescue => e # probably a RuntimeError
  puts "Error in #autocomplete_object: <#{e.class.name}> #{e.inspect}"
  nil
end

#autocomplete_prefix ⇒ Object



2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 2147

def autocomplete_prefix
  val = @prompt.value.to_s
  @autocomplete_period_index = lpi = val.rindex('.')
  prefix = lpi ? val[lpi + 1, @prompt.selection_end - lpi - 1] || '' : val
  if !@autocomplete_prefix || @autocomplete_prefix != prefix
    @autocomplete_prefix = prefix

    items = autocomplete_items(@autocomplete_prefix)
    if items.length.zero?
      autocomplete_clear
    else
      @autocomplete_menu.items = items
    end
  end
end

#current_input_str ⇒ Object



2219
2220
2221
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 2219

def current_input_str
  prompt.value.to_s
end

#current_input_str=(str) ⇒ Object



2223
2224
2225
2226
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 2223

def current_input_str=(str)
  prompt.value = str
  prompt.move_to_end
end

#display_autocomplete_candidate(candidate) ⇒ Object



2186
2187
2188
2189
2190
2191
2192
2193
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 2186

def display_autocomplete_candidate(candidate)
  candidate = candidate[0..-2] + " = " if candidate.end_with? '='
  if @autocomplete_period_index
    @prompt.value[0, @autocomplete_period_index + 1] + candidate.to_s
  else
    candidate.to_s
  end
end

#on_unhandled_key(key, input) ⇒ Object



2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 2053

def on_unhandled_key(key, input)
  if @autocompleting && key == :escape
    autocomplete_clear
  elsif $args.inputs.keyboard.key_down.enter
    if slide_progress > 0.5
      # in the event of an exception, the console window pops up
      # and is pre-filled with $gtk.reset.
      # there is an annoying scenario where the exception could be thrown
      # by pressing enter (while playing the game). if you press enter again
      # quickly, then the game is reset which closes the console.
      # so enter in the console is only evaluated if the slide_progress
      # is atleast half way down the page.
      eval_the_set_command
    end
  elsif $args.inputs.keyboard.key_down.up
    if @autocompleting
      autocomplete_next(+1)
    else
      if @command_history_index == -1
        @nonhistory_input = current_input_str
      end
      if @command_history_index < (@command_history.length - 1)
        @command_history_index += 1
        self.current_input_str = @command_history[@command_history_index].dup
      end
    end
  elsif $args.inputs.keyboard.key_down.down
    if @autocompleting
      autocomplete_next(-1)
    else
      if @command_history_index == 0
        @command_history_index = -1
        self.current_input_str = @nonhistory_input
        @nonhistory_input = ''
      elsif @command_history_index > 0
        @command_history_index -= 1
        self.current_input_str = @command_history[@command_history_index].dup
      end
    end
  elsif inputs_scroll_up_full? $args
    scroll_up_full
  elsif inputs_scroll_down_full? $args
    scroll_down_full
  elsif inputs_scroll_up_half? $args
    scroll_up_half
  elsif inputs_scroll_down_half? $args
    scroll_down_half
  elsif inputs_clear_command? $args
    prompt.clear
    @command_history_index = -1
    @nonhistory_input = ''
  elsif $args.inputs.keyboard.key_down.tab
    if @autocompleting
      autocomplete_accept
    else
      autocomplete
    end
  end
end

#process_inputs(args) ⇒ Object



2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 2027

def process_inputs args
  if console_toggle_key_down? args
    args.inputs.text.clear
    toggle
    args.inputs.keyboard.clear if !@visible
  end

  return unless visible?

  mouse_wheel_scroll args

  @log_offset = 0 if @log_offset < 0

  if @autocompleting
    # is the cursor before the period?
    if @autocomplete_period_index && @autocomplete_period_index >= @prompt.selection_end
      autocomplete_clear
    elsif @prompt.value_changed? || @prompt.selection_end != @autocomplete_selection_end
      autocomplete_prefix
      autocomplete_next(0)
    end

    @autocomplete_selection_end = @prompt.selection_end
  end
end

#prompt ⇒ Object



2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 2113

def prompt
  @prompt ||= Input::Prompt.new(
    x: 0,
    y: 0,
    w: 1280,
    prompt: 'Press CTRL+g or ESCAPE to clear the prompt. Press TAB for autocomplete.',
    text_color: 0xFFFFFF,
    background_color: 0x000000,
    cursor_color: [219, 182, 104],
    on_unhandled_key: method(:on_unhandled_key),
    focussed: true,
    draw_autocomplete_menu: false
  )
end

#render(args) ⇒ Object



2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 2206

def render args
  super

  # Add the autocomplete menu last so it renders on top
  if @autocompleting
    @autocomplete_menu.prepare_render_target
    @autocomplete_menu.x = @prompt.cursor_x
    @autocomplete_menu.y = @prompt.cursor_y + @prompt.font_height
    args.outputs.reserved << @autocomplete_menu
  end
end