mirror of
https://github.com/cyberarm/i-mic-fps.git
synced 2025-12-15 15:42:35 +00:00
Added Window#input_hijack to allow objects to get exclusive access to button_down/up callbacks, improved Chat widget
This commit is contained in:
@@ -32,5 +32,13 @@ class IMICFPS
|
|||||||
def update
|
def update
|
||||||
@hud_elements.each(&:update)
|
@hud_elements.each(&:update)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def button_down(id)
|
||||||
|
@hud_elements.each { |e| e.button_down(id) }
|
||||||
|
end
|
||||||
|
|
||||||
|
def button_up(id)
|
||||||
|
@hud_elements.each { |e| e.button_up(id) }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -57,6 +57,20 @@ class IMICFPS
|
|||||||
|
|
||||||
def update
|
def update
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def button_down(id)
|
||||||
|
end
|
||||||
|
|
||||||
|
def button_up(id)
|
||||||
|
end
|
||||||
|
|
||||||
|
def hijack_input!
|
||||||
|
$window.input_hijack = self
|
||||||
|
end
|
||||||
|
|
||||||
|
def release_input!
|
||||||
|
$window.input_hijack = nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -4,41 +4,93 @@ class IMICFPS
|
|||||||
class HUD
|
class HUD
|
||||||
class ChatWidget < HUD::Widget
|
class ChatWidget < HUD::Widget
|
||||||
def setup
|
def setup
|
||||||
|
@deliver_to_text = Text.new("", size: 28, font: BOLD_SANS_FONT)
|
||||||
@text = Text.new("", size: 28, font: SANS_FONT)
|
@text = Text.new("", size: 28, font: SANS_FONT)
|
||||||
|
|
||||||
|
@text_input = nil
|
||||||
@background = Gosu::Color.new(0x88c64600)
|
@background = Gosu::Color.new(0x88c64600)
|
||||||
|
@selection_color = Gosu::Color.new(0x88222222)
|
||||||
|
@width = @options[:width] || 400
|
||||||
|
|
||||||
|
@delivery_options = [:all, :team, :squad]
|
||||||
end
|
end
|
||||||
|
|
||||||
def draw
|
def draw
|
||||||
return unless window.text_input
|
return unless @text_input
|
||||||
|
|
||||||
Gosu.draw_rect(
|
Gosu.draw_rect(
|
||||||
@text.x - Widget.horizontal_padding, @text.y - Widget.horizontal_padding,
|
Widget.horizontal_margin, $window.height / 2 - (@text.height / 2 + Widget.horizontal_padding),
|
||||||
@text.width + Widget.vertical_padding * 2, @text.height + Widget.vertical_padding * 2,
|
@width - Widget.horizontal_padding * 2, @text.height + Widget.vertical_padding * 2,
|
||||||
@background
|
@background
|
||||||
)
|
)
|
||||||
|
|
||||||
@text.draw
|
@deliver_to_text.draw
|
||||||
|
|
||||||
|
clip_width = @deliver_to_text.width + Widget.horizontal_padding * 3 + Widget.horizontal_margin
|
||||||
|
Gosu.clip_to(@text.x, @text.y, @width - clip_width, @text.height) do
|
||||||
|
x = Widget.horizontal_margin + Widget.horizontal_padding + @deliver_to_text.width
|
||||||
|
|
||||||
|
cursor_x = x + @text.width(@text_input.text[0...@text_input.caret_pos])
|
||||||
|
selection_x = x + @text.width(@text_input.text[0...@text_input.selection_start])
|
||||||
|
selection_width = cursor_x - selection_x
|
||||||
|
cursor_thickness = 2
|
||||||
|
|
||||||
|
Gosu.draw_rect(selection_x, @text.y, selection_width, @text.height, @selection_color)
|
||||||
|
Gosu.draw_rect(cursor_x, @text.y, cursor_thickness, @text.height, Gosu::Color::WHITE)
|
||||||
|
@text.draw
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
# NOTE: Account for Y in QWERTZ layout
|
@deliver_to_text.text = "#{@deliver_to}: "
|
||||||
text = window.text_input&.text
|
@deliver_to_text.x = Widget.horizontal_margin + Widget.horizontal_padding
|
||||||
|
@deliver_to_text.y = $window.height / 2 - (@text.height / 2)
|
||||||
|
|
||||||
if window.text_input.nil? && (Gosu.button_down?(Gosu::KbT) || Gosu.button_down?(Gosu::KbY) || Gosu.button_down?(Gosu::KbU))
|
@text.text = @text_input&.text.to_s
|
||||||
window.text_input = Gosu::TextInput.new
|
@text.x = Widget.horizontal_margin + Widget.horizontal_padding + @deliver_to_text.width
|
||||||
|
@text.y = $window.height / 2 - (@text.height / 2)
|
||||||
|
end
|
||||||
|
|
||||||
|
def button_down(id)
|
||||||
|
# TODO: Use InputMapper keymap to function
|
||||||
|
# NOTE: Account for Y in QWERTZ layout
|
||||||
|
case id
|
||||||
|
when Gosu::KB_T, Gosu::KB_Y, Gosu::KB_U
|
||||||
|
return if @text_input
|
||||||
|
|
||||||
|
hijack_input!
|
||||||
|
|
||||||
|
@text_input = window.text_input = Gosu::TextInput.new
|
||||||
|
|
||||||
@deliver_to = :all if Gosu.button_down?(Gosu::KbT)
|
@deliver_to = :all if Gosu.button_down?(Gosu::KbT)
|
||||||
@deliver_to = :team if Gosu.button_down?(Gosu::KbY)
|
@deliver_to = :team if Gosu.button_down?(Gosu::KbY)
|
||||||
@deliver_to = :squad if Gosu.button_down?(Gosu::KbU)
|
@deliver_to = :squad if Gosu.button_down?(Gosu::KbU)
|
||||||
end
|
when Gosu::KB_TAB
|
||||||
|
return unless @text_input
|
||||||
|
|
||||||
if window.text_input && (Gosu.button_down?(Gosu::KbEnter) || Gosu.button_down?(Gosu::KbReturn))
|
cycle_deliver_to
|
||||||
window.text_input = nil
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
@text.text = text.to_s
|
def button_up(id)
|
||||||
@text.x = window.width / 2 - (Widget.horizontal_margin + @text.width / 2 + Widget.horizontal_padding)
|
return unless @text_input
|
||||||
@text.y = window.height - (Widget.vertical_margin + @text.height + Widget.vertical_padding)
|
|
||||||
|
case id
|
||||||
|
when Gosu::KB_ENTER, Gosu::KB_RETURN
|
||||||
|
release_input!
|
||||||
|
|
||||||
|
# TODO: Deliver message to server
|
||||||
|
|
||||||
|
@text_input = window.text_input = nil
|
||||||
|
when Gosu::KB_ESCAPE
|
||||||
|
release_input!
|
||||||
|
@text_input = window.text_input = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def cycle_deliver_to
|
||||||
|
i = @delivery_options.index(@deliver_to)
|
||||||
|
@deliver_to = @delivery_options[(i + 1) % (@delivery_options.size)]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -69,6 +69,8 @@ class IMICFPS
|
|||||||
window.director.map.entities.each do |entity|
|
window.director.map.entities.each do |entity|
|
||||||
entity.button_down(id) if defined?(entity.button_down)
|
entity.button_down(id) if defined?(entity.button_down)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@hud.button_down(id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def button_up(id)
|
def button_up(id)
|
||||||
@@ -81,6 +83,8 @@ class IMICFPS
|
|||||||
window.director.map.entities.each do |entity|
|
window.director.map.entities.each do |entity|
|
||||||
entity.button_up(id) if defined?(entity.button_up)
|
entity.button_up(id) if defined?(entity.button_up)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@hud.button_up(id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -40,14 +40,17 @@ Fallback <b>immediate mode renderer</b> will be used."
|
|||||||
(Linux Only) For MESA based drivers append <b>--mesa-override</b>
|
(Linux Only) For MESA based drivers append <b>--mesa-override</b>
|
||||||
as a commandline argument to override reported version."
|
as a commandline argument to override reported version."
|
||||||
message += linux_mesa_message if RUBY_PLATFORM =~ /linux/ && gl_version.downcase.include?(" mesa ")
|
message += linux_mesa_message if RUBY_PLATFORM =~ /linux/ && gl_version.downcase.include?(" mesa ")
|
||||||
@old_gl_warning = Gosu::Image.from_markup(message, 24, align: :center, font: "")
|
@old_gl_warning = Text.new(message, size: 24, z: Float::INFINITY, border: true, border_color: Gosu::Color::BLACK)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def draw
|
def draw
|
||||||
super
|
super
|
||||||
|
|
||||||
@old_gl_warning&.draw(window.width / 2 - @old_gl_warning.width / 2, window.height - (@old_gl_warning.height + 10), Float::INFINITY)
|
@old_gl_warning&.x = window.width / 2 - @old_gl_warning.width / 2
|
||||||
|
@old_gl_warning&.y = window.height - (@old_gl_warning.height + 10)
|
||||||
|
|
||||||
|
@old_gl_warning&.draw
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -53,6 +53,12 @@ class IMICFPS
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def input_hijack=(hijacker)
|
||||||
|
@input_hijacker = hijacker
|
||||||
|
|
||||||
|
InputMapper.reset_keys
|
||||||
|
end
|
||||||
|
|
||||||
def needs_cursor?
|
def needs_cursor?
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
@@ -92,6 +98,8 @@ class IMICFPS
|
|||||||
def button_down(id)
|
def button_down(id)
|
||||||
if @show_console
|
if @show_console
|
||||||
@console.button_down(id)
|
@console.button_down(id)
|
||||||
|
elsif @input_hijacker
|
||||||
|
@input_hijacker.button_down(id)
|
||||||
else
|
else
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
@@ -105,6 +113,8 @@ class IMICFPS
|
|||||||
def button_up(id)
|
def button_up(id)
|
||||||
if @show_console
|
if @show_console
|
||||||
@console.button_up(id)
|
@console.button_up(id)
|
||||||
|
elsif @input_hijacker
|
||||||
|
@input_hijacker.button_up(id)
|
||||||
else
|
else
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user