diff --git a/i-mic-fps.rb b/i-mic-fps.rb index dfb54b8..9f9d8f0 100644 --- a/i-mic-fps.rb +++ b/i-mic-fps.rb @@ -64,6 +64,7 @@ end $debug = ARGV.join.include?("--debug") ? true : false include CyberarmEngine +require_relative "lib/version" require_relative "lib/common_methods" require_relative "lib/trees/aabb_tree_debug" @@ -88,6 +89,8 @@ require_relative "lib/states/game_states/game" require_relative "lib/states/game_states/loading_state" require_relative "lib/states/menus/main_menu" +require_relative "lib/ui/console" + require_relative "lib/objects/entity" require_relative "lib/objects/model_loader" require_relative "lib/objects/light" diff --git a/lib/states/menu.rb b/lib/states/menu.rb index 6a6b4f5..25a7550 100644 --- a/lib/states/menu.rb +++ b/lib/states/menu.rb @@ -81,7 +81,7 @@ class IMICFPS def button_up(id) window.close if id == Gosu::KbEscape - if Gosu::MsLeft + if id == Gosu::MsLeft @elements.each do |e| next unless e.is_a?(Link) if mouse_over?(e) diff --git a/lib/ui/console.rb b/lib/ui/console.rb new file mode 100644 index 0000000..9323fac --- /dev/null +++ b/lib/ui/console.rb @@ -0,0 +1,101 @@ +class IMICFPS + class Console + Z = 100_000 + PADDING = 2 + include CommonMethods + + def initialize + @text_input = Gosu::TextInput.new + + @input = Text.new("", x: 4, y: window.height / 4 * 3 - (PADDING * 2), z: Console::Z + 1) + @input.y -= @input.height + + @history_height = window.height / 4 * 3 - (PADDING * 2 + @input.textobject.height) + @history = Text.new("=== #{IMICFPS::NAME} v#{IMICFPS::VERSION} (#{IMICFPS::RELEASE_NAME}) ===\n\n", x: 4, y: @history_height, z: Console::Z + 1) + update_history + + @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 + + @width = window.width / 4 * 3 + @height = window.height / 4 * 3 + end + + 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 + draw_rect(@input.x + caret_pos, @input.y, Console::PADDING, @input.height, @caret_color, Console::Z + 2) if @show_caret + end + + def caret_pos + return 0 if @text_input.caret_pos == 0 + @input.textobject.text_width(@text_input.text[0..@text_input.caret_pos-1]) + end + + def update + if Gosu.milliseconds - @caret_last_change >= @caret_interval + @caret_last_change = Gosu.milliseconds + @show_caret = !@show_caret + end + + @input.text = @text_input.text + end + + def button_down(id) + case id + when Gosu::KbEnter, Gosu::KbReturn + return unless @text_input.text.length > 0 + @history.text += "\n> #{@text_input.text}" + update_history + @text_input.text = "" + when Gosu::KbBacktick + # Removed backtick character from input + if @text_input.text.size > 1 + @text_input.text = @text_input.text[0..@text_input.text.size - 2] + else + @text_input.text = "" + end + end + end + + def button_up(id) + end + + def update_history + @history.y = @history_height - (@history.text.lines.count * (@history.textobject.height)) + end + + def focus + @active_text_input = window.text_input + window.text_input = @text_input + + @showing_cursor = window.show_cursor + window.show_cursor = true + + @show_caret = true + @caret_last_change = Gosu.milliseconds + end + + def blur + window.text_input = @active_text_input + window.show_cursor = @showing_cursor + end + end +end \ No newline at end of file diff --git a/lib/ui/menu/menu.rb b/lib/ui/menu/menu.rb deleted file mode 100644 index 3e15260..0000000 --- a/lib/ui/menu/menu.rb +++ /dev/null @@ -1,43 +0,0 @@ -class Menu - def initialize - @elements = [] - setup - end - - def setup - end - - def draw - @elements.each(&:draw) - end - - def update - @elements.each(&:update) - end - - def button(text, x:, y:, &block) - @element << Button.new(text, x, y, block) - end - - def label(text, x:, y:) - @element << Text.new(text, x: x, y: y, size: 24) - end - - class Button - PADDING = 10 - def initialize(text, x, y, block) - @text = Text.new(text, x: x, y: y) - end - - def draw - Gosu.draw_rect(x-PADDING, y-PADDING, @text.width+PADDING, @text.height+PADDING, Gosu::Color.rgb(0, 100, 0)) - @text.draw - end - - def update - end - - def mouse_over? - end - end -end diff --git a/lib/version.rb b/lib/version.rb new file mode 100644 index 0000000..af62ea2 --- /dev/null +++ b/lib/version.rb @@ -0,0 +1,5 @@ +class IMICFPS + NAME = "I-MIC FPS" + RELEASE_NAME = "InDev" + VERSION = "0.0.1" +end \ No newline at end of file diff --git a/lib/window.rb b/lib/window.rb index 6848b5b..5ca0047 100644 --- a/lib/window.rb +++ b/lib/window.rb @@ -15,7 +15,45 @@ class IMICFPS @needs_cursor = false @number_of_vertices = 0 + self.caption = "#{IMICFPS::NAME} v#{IMICFPS::VERSION} (#{IMICFPS::RELEASE_NAME})" + + @show_console = false + @console = Console.new + push_state(MainMenu) end + + def draw + super + + @console.draw if @show_console + end + + def update + super + + @console.update if @show_console + end + + def button_down(id) + if @show_console + @console.button_down(id) + else + super + end + + if id == Gosu::KbBacktick + @show_console ? @console.blur : @console.focus + @show_console = !@show_console + end + end + + def button_up(id) + if @show_console + @console.button_up(id) + else + super + end + end end end