mirror of
https://github.com/cyberarm/i-mic-fps.git
synced 2025-12-16 08:02:36 +00:00
Moved menu files to be under /ui directory, added support for commands to Console!
This commit is contained in:
45
lib/ui/command.rb
Normal file
45
lib/ui/command.rb
Normal file
@@ -0,0 +1,45 @@
|
||||
class IMICFPS
|
||||
class Commands
|
||||
class Command
|
||||
def self.inherited(subclass)
|
||||
@list ||= []
|
||||
@commands ||= []
|
||||
@list << subclass
|
||||
end
|
||||
|
||||
def self.setup
|
||||
@list ||= []
|
||||
@commands = []
|
||||
@list.each do |subclass|
|
||||
@commands << subclass.new
|
||||
end
|
||||
end
|
||||
|
||||
def self.use(command, arguments, console)
|
||||
found_command = @commands.detect { |cmd| cmd.command == command.to_sym }
|
||||
|
||||
if found_command
|
||||
found_command.handle(arguments, console)
|
||||
else
|
||||
console.stdin("Command <c=ff5555>#{command}</c> not found.")
|
||||
end
|
||||
end
|
||||
|
||||
def group
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def command
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def handle(arguments, console)
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def usage
|
||||
raise NotImplementedError
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
24
lib/ui/commands/help_command.rb
Normal file
24
lib/ui/commands/help_command.rb
Normal file
@@ -0,0 +1,24 @@
|
||||
class IMICFPS
|
||||
class Commands
|
||||
class HelpCommand < Command
|
||||
def initialize
|
||||
end
|
||||
|
||||
def group
|
||||
:global
|
||||
end
|
||||
|
||||
def command
|
||||
:help
|
||||
end
|
||||
|
||||
def handle(arguments, console)
|
||||
console.stdin(usage)
|
||||
end
|
||||
|
||||
def usage
|
||||
"HELP\ncommand [arguments]\ncommand subcommand [argument]"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -62,8 +62,9 @@ class IMICFPS
|
||||
case id
|
||||
when Gosu::KbEnter, Gosu::KbReturn
|
||||
return unless @text_input.text.length > 0
|
||||
@history.text += "\n> #{@text_input.text}"
|
||||
@history.text += "\n<c=999999>> #{@text_input.text}</c>"
|
||||
update_history
|
||||
handle_command
|
||||
@text_input.text = ""
|
||||
when Gosu::KbBacktick
|
||||
# Removed backtick character from input
|
||||
@@ -82,6 +83,20 @@ class IMICFPS
|
||||
@history.y = @history_height - (@history.text.lines.count * (@history.textobject.height))
|
||||
end
|
||||
|
||||
def handle_command
|
||||
string = @text_input.text
|
||||
split = string.split(" ")
|
||||
command = split.first
|
||||
arguments = split.length > 0 ? split[1..split.length - 1] : []
|
||||
|
||||
IMICFPS::Commands::Command.use(command, arguments, self)
|
||||
end
|
||||
|
||||
def stdin(string)
|
||||
@history.text += "\n#{string}"
|
||||
update_history
|
||||
end
|
||||
|
||||
def focus
|
||||
@active_text_input = window.text_input
|
||||
window.text_input = @text_input
|
||||
|
||||
128
lib/ui/menu.rb
Normal file
128
lib/ui/menu.rb
Normal file
@@ -0,0 +1,128 @@
|
||||
class IMICFPS
|
||||
class Menu < IMICFPS::GameState
|
||||
def initialize(*args)
|
||||
@elements = []
|
||||
@size = 50
|
||||
@slope = 250
|
||||
@color_step = 10
|
||||
@base_color = Gosu::Color.rgb(255, 127, 0)
|
||||
super(*args)
|
||||
end
|
||||
|
||||
def title(text, color = @base_color)
|
||||
@elements << Text.new(text, color: color, size: 100, x: 0, y: 15, alignment: :center)
|
||||
end
|
||||
|
||||
def link(text, color = Gosu::Color.rgb(0,127,127), &block)
|
||||
text = Text.new(text, color: color, size: 50, x: 0, y: 100+(60*@elements.count), alignment: :center)
|
||||
@elements << Link.new(text, self, block)
|
||||
end
|
||||
|
||||
def draw
|
||||
@background ||= Gosu.record(Gosu.screen_width, Gosu.screen_height) do
|
||||
((Gosu.screen_height+@slope)/@size).times do |i|
|
||||
fill_quad(
|
||||
0, i*@size,
|
||||
0, @slope+(i*@size),
|
||||
window.width/2, (-@slope)+(i*@size),
|
||||
window.width/2, i*@size,
|
||||
Gosu::Color.rgba(@base_color.red-i*@color_step, @base_color.green-i*@color_step, @base_color.blue-i*@color_step, 200)
|
||||
)
|
||||
fill_quad(
|
||||
window.width, i*@size,
|
||||
window.width, @slope+(i*@size),
|
||||
window.width/2, (-@slope)+(i*@size),
|
||||
window.width/2, i*@size,
|
||||
Gosu::Color.rgba(@base_color.red-i*@color_step, @base_color.green-i*@color_step, @base_color.blue-i*@color_step, 200)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
@background.draw(0, 0, 0)
|
||||
|
||||
# Box
|
||||
draw_rect(
|
||||
window.width/4, 0,
|
||||
window.width/2, window.height,
|
||||
Gosu::Color.rgba(0, 0, 0, 150)
|
||||
# Gosu::Color.rgba(@base_color.red+@color_step, @base_color.green+@color_step, @base_color.blue+@color_step, 200)
|
||||
)
|
||||
|
||||
# Texts
|
||||
@elements.each do |e|
|
||||
e.draw
|
||||
end
|
||||
|
||||
# Cursor
|
||||
fill_quad(
|
||||
mouse_x, mouse_y,
|
||||
mouse_x+16, mouse_y,
|
||||
mouse_x, mouse_y+16,
|
||||
mouse_x, mouse_y+16,
|
||||
Gosu::Color::WHITE, Float::INFINITY
|
||||
)
|
||||
end
|
||||
|
||||
def update
|
||||
@elements.each do |e|
|
||||
e.update
|
||||
end
|
||||
end
|
||||
|
||||
def fill_quad(x1, y1, x2, y2, x3, y3, x4, y4, color = Gosu::Color::WHITE, z = 0, mode = :default)
|
||||
draw_quad(
|
||||
x1,y1, color,
|
||||
x2,y2, color,
|
||||
x3,y3, color,
|
||||
x4,y4, color,
|
||||
z, mode
|
||||
)
|
||||
end
|
||||
|
||||
def button_up(id)
|
||||
window.close if id == Gosu::KbEscape
|
||||
if id == Gosu::MsLeft
|
||||
@elements.each do |e|
|
||||
next unless e.is_a?(Link)
|
||||
if mouse_over?(e)
|
||||
e.clicked
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def mouse_over?(object)
|
||||
mouse_x.between?(object.x, object.x+object.width) &&
|
||||
mouse_y.between?(object.y, object.y+object.height)
|
||||
end
|
||||
|
||||
class Link
|
||||
attr_reader :text, :block
|
||||
def initialize(text, host, block)
|
||||
@text, @host, @block = text, host, block
|
||||
@color = @text.color
|
||||
@hover_color = Gosu::Color.rgb(64, 127, 255)
|
||||
@text.shadow_alpha = 100
|
||||
end
|
||||
|
||||
def update
|
||||
if @host.mouse_over?(self)
|
||||
@text.color = @hover_color
|
||||
@text.shadow_color= Gosu::Color::BLACK
|
||||
@text.shadow_size = 3
|
||||
else
|
||||
@text.color = @color
|
||||
@text.shadow_color = nil
|
||||
@text.shadow_size = 1
|
||||
end
|
||||
end
|
||||
|
||||
def x; text.x; end
|
||||
def y; text.y; end
|
||||
def width; text.width; end
|
||||
def height; text.height; end
|
||||
def draw; text.draw; end
|
||||
def clicked; @block.call; end
|
||||
end
|
||||
end
|
||||
end
|
||||
16
lib/ui/menus/main_menu.rb
Normal file
16
lib/ui/menus/main_menu.rb
Normal file
@@ -0,0 +1,16 @@
|
||||
class IMICFPS
|
||||
class MainMenu < Menu
|
||||
def setup
|
||||
title "I-MIC FPS"
|
||||
link "Single Player" do
|
||||
push_state(LoadingState.new(forward: Game))
|
||||
end
|
||||
link "Settings" do
|
||||
# push_game_state(SettingsMenu)
|
||||
end
|
||||
link "Exit" do
|
||||
window.close
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user