class Console
Attributes
Public Class Methods
Initialize the console, default is inactive
-
screen Screen surface
-
font Font to use
-
height Height from top of window. A multiple of the font’s height is recommended.
-
alpha Alpha (transparency) to set console to
-
activation Key which activates console default is tilde
-
background
Console
background color -
inputchar
String
to signify input line
# File lib/console.rb, line 32 def initialize(screen, font, height, alpha = 255, activation = 96, background = [255, 255, 255], inputchar = "[>") @screen, @font, @height = screen, font, height @background = background @alpha = alpha @inputchar = inputchar @activation = activation reset @id = 0 @hpos = -1 @tab = "" @active = false end
Public Instance Methods
Render console
# File lib/console.rb, line 172 def draw return if !@active x = @font.textSize(@inputchar)[0] # Can be any key or number, just for a test y = @height - (@font.height * 2) @screen.drawFilledRectAlpha(0, 0, @screen.w, @height, @background, @alpha) # Draw white console with space for 5 lines @font.drawBlendedUTF8(@screen, @inputchar, 0, @height - @font.height, 0, 0, 0) # Draw input symbol unless @buffer.empty? @font.drawBlendedUTF8(@screen, @buffer, x, @height - @font.height, 0, 0, 0) # Draw buffer end @screen.drawLine(x + (@cursor_pos * @font.textSize("a")[0]), @height - @font.height, x + (@cursor_pos * @font.textSize("a")[0]), @height, [0, 0, 0]) @console_view.reverse_each do |message| # Draw each message that is in view @font.drawBlendedUTF8(@screen, message[0], 0, y, 0, 0, 0) y -= @font.height end end
Parses text and executes command if found
-
text Text to parse
# File lib/console.rb, line 66 def execute_string(text) if text.include?(" ") index = text.index(" ") if index > 0 @name = text.split(" ")[0] @args = text.split(" ")[1..text.length] # In case of more than one arg end else # No arguments, so only command @name = text @args = nil end # Delete the last executed command so that only the result shows @console_view.delete_at(-1) @console_archive.delete_at(-1) begin self.__send__(@name, @args) rescue begin self.__send__(@name, "usage") rescue put("unknown command: #{@name}") end end # Reset buffer and cursorPos @buffer = "" @cursor_pos = 0 end
Find all user-executable commands
# File lib/console.rb, line 112 def find_commands Console.protected_instance_methods(false) end
What to do on keypress
-
key Key which is pressed
# File lib/console.rb, line 130 def keypress?(key) if key.sym != @activation case key.sym when SDL::Key::RETURN if @buffer != "" @command_archive << @buffer put(@buffer) execute_string(@buffer) end when SDL::Key::SPACE insert(" ") when SDL::Key::BACKSPACE @buffer.slice!(@cursor_pos - 1..@cursor_pos - 1) unless @cursor_pos == 0 cursor_left when SDL::Key::DELETE @buffer.slice!(@cursor_pos..@cursor_pos) when SDL::Key::PAGEUP scroll_up when SDL::Key::PAGEDOWN scroll_down when SDL::Key::UP history_up when SDL::Key::DOWN history_down when SDL::Key::LEFT cursor_left when SDL::Key::RIGHT cursor_right when SDL::Key::ESCAPE @active = !@active when SDL::Key::TAB tab_complete else display_key(key.sym, key.mod) end if @active else @active = !@active end end
# File lib/console.rb, line 107 def method_missing(meth, *args) put("unknown command: #{@name}") end
Output string to console
-
string
String
to output
# File lib/console.rb, line 50 def put(string) @id += 1 # Increment the id so that each one is unique # Put string, @id in both console archive and the console view @console_archive << [string, @id] @console_view << [string, @id] room = ((@height - (@height % @font.height)) / @font.height) - 1 while @console_view.length > room @console_view.shift end scroll_down end
# File lib/console.rb, line 99 def reset @buffer = "" @cursor_pos = 0 @console_view = [] @console_archive = [] @command_archive = [] end
Tab complete string from buffer
# File lib/console.rb, line 117 def tab_complete cmds = find_commands.sort e = cmds.find {|i| i.starts_with?(@buffer)} if !e.nil? @buffer = e cursor_end end end
Protected Instance Methods
Clears console screen
# File lib/command.rb, line 94 def clear(args = nil) if args == "usage" put("clear: Clears the console") else reset end end
Outputs string to console
-
args
String
to output to console
# File lib/command.rb, line 16 def echo(args) if args == "usage" put("echo [string]: Outputs string to console") elsif args.length > 0 text = args.join(" ") put(text) end end
# File lib/command.rb, line 102 def help(args = nil) if args.nil? || args == "usage" put("help [command]: Shows the usage for the command") elsif args.length > 0 self.__send__(args[0], "usage") end end
List commands
-
args If set,
list_commands
will only output commands that include args
# File lib/command.rb, line 27 def list_commands(args = nil) if args == "usage" put("list_commands: Outputs possible commands to console") else commands = find_commands.sort put("Possible commands:") commands.each do |command| if !args.nil? if command.include?(args[0]) put(command) end else put(command) end end end end
Exits program
# File lib/command.rb, line 85 def quit(args = nil) if args == "usage" put("quit: Exits the application") else exit end end
Sets alpha of console
-
args Alpha to set console to
# File lib/command.rb, line 47 def setalpha(args) if args == "usage" put("setalpha [integer]: Sets the alpha of console to int") elsif args.length > 0 if (0..255) === args[0].to_i @alpha = args[0].to_i put("Alpha set to #{@alpha}") else put("Alpha must be between 0 and 255") end end end
Sets console height from top of screen
-
args Integer to set height
# File lib/command.rb, line 62 def setconsolesize(args) if args == "usage" put("setconsolesize [integer]: Sets the height of the console to int") elsif args.length > 0 if args[0].to_i < @font.height * 2 put("Console size must be above #{@font.height * 2}") elsif args[0].to_i > @screen.h put("Console must be less than #{@screen.h}") else @height = args[0].to_i @console_view = [] room = ((@height - (@height % @font.height)) / @font.height) - 1 (0..room).each do |i| text = @console_archive[-1 - i] @console_view << text if !text.nil? end @console_view.reverse! put("Console size set to #{@height}") end end end