Class: CyberarmEngine::Console

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/cyberarm_engine/console.rb,
lib/cyberarm_engine/console/command.rb,
lib/cyberarm_engine/console/subcommand.rb,
lib/cyberarm_engine/console/commands/help_command.rb

Defined Under Namespace

Modules: Style Classes: Command, HelpCommand

Constant Summary collapse

Z =
100_000
PADDING =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common

#alt_down?, #control_down?, #current_state, #darken, #draw_rect, #fill, #find_element_by_tag, #get_asset, #get_image, #get_sample, #get_song, #lighten, #opacity, #pop_state, #previous_state, #push_state, #shift_down?, #shift_state, #show_cursor, #show_cursor=, #window

Constructor Details

#initialize(font: Gosu.default_font_name) ⇒ Console

Returns a new instance of Console.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cyberarm_engine/console.rb', line 11

def initialize(font: Gosu.default_font_name)
  @text_input = Gosu::TextInput.new
  @width  = window.width  / 4 * 3
  @height = window.height / 4 * 3

  @input = Text.new("", x: 4, y: @height - (PADDING * 2), z: Console::Z + 1, font: font)
  @input.y -= @input.height

  @history = Text.new("", x: 4, z: Console::Z + 1, font: font, border: true, border_color: Gosu::Color::BLACK)
  update_history_y

  @command_history       = []
  @command_history_index = 0

  @memory = ""

  @background_color = Gosu::Color.rgba(0, 0, 0, 200)
  @foreground_color = Gosu::Color.rgba(100, 100, 100, 100)
  @input_color      = Gosu::Color.rgba(100, 100, 100, 200)

  @showing_cursor = false
  @active_text_input = nil

  @show_caret = true
  @caret_last_change = Gosu.milliseconds
  @caret_interval = 250
  @caret_color = Gosu::Color::WHITE
  @selection_color = Gosu::Color.new(0x5522ff22)
end

Instance Attribute Details

#text_inputObject (readonly)

Returns the value of attribute text_input.



9
10
11
# File 'lib/cyberarm_engine/console.rb', line 9

def text_input
  @text_input
end

Instance Method Details

#abbrev_search(array, text) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/cyberarm_engine/console.rb', line 214

def abbrev_search(array, text)
  return [] unless text.length.positive?

  list = []
  Abbrev.abbrev(array).each do |abbrev, value|
    next unless abbrev&.start_with?(text)

    list << value
  end

  list.uniq
end

#blurObject



243
244
245
246
# File 'lib/cyberarm_engine/console.rb', line 243

def blur
  window.text_input  = @active_text_input
  window.needs_cursor = @showing_cursor
end

#button_down(id) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/cyberarm_engine/console.rb', line 104

def button_down(id)
  case id
  when Gosu::KB_ENTER, Gosu::KB_RETURN
    return unless @text_input.text.length.positive?

    @history.text += "\n<c=999999>> #{@text_input.text}</c>"
    @command_history << @text_input.text
    @command_history_index = @command_history.size
    update_history_y
    handle_command
    @text_input.text = ""

  when Gosu::KB_UP
    @command_history_index -= 1
    @command_history_index = 0 if @command_history_index.negative?
    @text_input.text = @command_history[@command_history_index]

  when Gosu::KB_DOWN
    @command_history_index += 1
    if @command_history_index > @command_history.size - 1
      @text_input.text = "" unless @command_history_index > @command_history.size
      @command_history_index = @command_history.size
    else
      @text_input.text = @command_history[@command_history_index]
    end

  when Gosu::KB_TAB
    split = @text_input.text.split(" ")

    if !@text_input.text.end_with?(" ") && split.size == 1
      list = abbrev_search(Console::Command.list_commands.map { |cmd| cmd.command.to_s }, @text_input.text)

      if list.size == 1
        @text_input.text = "#{list.first} "
      elsif list.size.positive?
        stdin("\n#{list.map { |cmd| Console::Style.highlight(cmd) }.join(', ')}")
      end
    elsif split.size.positive? && cmd = Console::Command.find(split.first)
      cmd.autocomplete(self)
    end

  when Gosu::KB_BACKTICK
    # Remove backtick character from input
    @text_input.text = if @text_input.text.size > 1
                         @text_input.text[0..@text_input.text.size - 2]
                       else
                         ""
                       end

  # Copy
  when Gosu::KB_C
    if control_down? && shift_down?
      @memory = @text_input.text[caret_start..caret_end - 1] if caret_start != caret_end
      p @memory
    elsif control_down?
      @text_input.text = ""
    end

  # Paste
  when Gosu::KB_V
    if control_down? && shift_down?
      string = @text_input.text.chars.insert(caret_pos, @memory).join
      _caret_pos = caret_pos
      @text_input.text = string
      @text_input.caret_pos = _caret_pos + @memory.length
      @text_input.selection_start = _caret_pos + @memory.length
    end

  # Cut
  when Gosu::KB_X
    if control_down? && shift_down?
      @memory = @text_input.text[caret_start..caret_end - 1] if caret_start != caret_end
      string  = @text_input.text.chars
      Array(caret_start..caret_end - 1).each_with_index do |i, j|
        string.delete_at(i - j)
      end

      @text_input.text = string.join
    end

  # Delete word to left of caret
  when Gosu::KB_W
    if control_down?
      split = @text_input.text.split(" ")
      split.delete(split.last)
      @text_input.text = split.join(" ")
    end

  # Clear history
  when Gosu::KB_L
    @history.text = "" if control_down?
  end
end

#button_up(id) ⇒ Object



198
199
# File 'lib/cyberarm_engine/console.rb', line 198

def button_up(id)
end

#caret_endObject



83
84
85
# File 'lib/cyberarm_engine/console.rb', line 83

def caret_end
  @text_input.selection_start > @text_input.caret_pos ? @text_input.selection_start : @text_input.caret_pos
end

#caret_from_leftObject



65
66
67
68
69
# File 'lib/cyberarm_engine/console.rb', line 65

def caret_from_left
  return 0 if @text_input.caret_pos.zero?

  @input.textobject.text_width(@text_input.text[0..@text_input.caret_pos - 1])
end

#caret_posObject



75
76
77
# File 'lib/cyberarm_engine/console.rb', line 75

def caret_pos
  @text_input.caret_pos
end

#caret_selection_widthObject



71
72
73
# File 'lib/cyberarm_engine/console.rb', line 71

def caret_selection_width
  @input.textobject.text_width(@text_input.text[caret_start..(caret_end - 1)])
end

#caret_startObject



79
80
81
# File 'lib/cyberarm_engine/console.rb', line 79

def caret_start
  @text_input.selection_start < @text_input.caret_pos ? @text_input.selection_start : @text_input.caret_pos
end

#drawObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cyberarm_engine/console.rb', line 41

def draw
  # Background/Border
  draw_rect(0, 0, @width, @height, @background_color, Console::Z)
  # Foregound/History
  draw_rect(PADDING, PADDING, @width - (PADDING * 2), @height - (PADDING * 2), @foreground_color, Console::Z)
  # Text bar
  draw_rect(2, @input.y, @width - (PADDING * 2), @input.height, @input_color, Console::Z)

  @history.draw
  @input.draw
  # Caret
  if @show_caret
    draw_rect(@input.x + caret_from_left, @input.y, Console::PADDING, @input.height, @caret_color, Console::Z + 2)
  end
  # Caret selection
  if caret_start != caret_end
    if caret_start < @text_input.selection_start
      draw_rect(@input.x + caret_from_left, @input.y, caret_selection_width, @input.height, @selection_color, Console::Z)
    else
      draw_rect((@input.x + caret_from_left) - caret_selection_width, @input.y, caret_selection_width, @input.height, @selection_color, Console::Z)
    end
  end
end

#focusObject



232
233
234
235
236
237
238
239
240
241
# File 'lib/cyberarm_engine/console.rb', line 232

def focus
  @active_text_input = window.text_input
  window.text_input = @text_input

  @showing_cursor = window.needs_cursor
  window.needs_cursor = true

  @show_caret = true
  @caret_last_change = Gosu.milliseconds
end

#handle_commandObject



205
206
207
208
209
210
211
212
# File 'lib/cyberarm_engine/console.rb', line 205

def handle_command
  string = @text_input.text
  split  = string.split(" ")
  command = split.first
  arguments = split.length.positive? ? split[1..split.length - 1] : []

  CyberarmEngine::Console::Command.use(command, arguments, self)
end

#stdin(string) ⇒ Object



227
228
229
230
# File 'lib/cyberarm_engine/console.rb', line 227

def stdin(string)
  @history.text += "\n#{string}"
  update_history_y
end

#updateObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/cyberarm_engine/console.rb', line 87

def update
  if Gosu.milliseconds - @caret_last_change >= @caret_interval
    @caret_last_change = Gosu.milliseconds
    @show_caret = !@show_caret
  end

  if @width != window.width || @height != @height
    @width  = window.width  / 4 * 3
    @height = window.height / 4 * 3

    @input.y = @height - (PADDING * 2 + @input.height)
    update_history_y
  end

  @input.text = @text_input.text
end

#update_history_yObject



201
202
203
# File 'lib/cyberarm_engine/console.rb', line 201

def update_history_y
  @history.y = @height - (PADDING * 2) - @input.height - (@history.text.lines.count * @history.textobject.height)
end