Compare commits

...

3 Commits

9 changed files with 174 additions and 17 deletions

View File

@@ -67,6 +67,8 @@ require_relative "cyberarm_engine/ui/elements/menu_item"
require_relative "cyberarm_engine/game_state" require_relative "cyberarm_engine/game_state"
require_relative "cyberarm_engine/ui/gui_state" require_relative "cyberarm_engine/ui/gui_state"
require_relative "cyberarm_engine/ui/dialog"
require_relative "cyberarm_engine/ui/page"
require_relative "cyberarm_engine/builtin/intro_state" require_relative "cyberarm_engine/builtin/intro_state"

View File

@@ -1,8 +1,8 @@
module CyberarmEngine module CyberarmEngine
class BackgroundNineSlice class BackgroundNineSlice
include CyberarmEngine::Common include CyberarmEngine::Common
attr_accessor :x, :y, :z, :width, :height, :left, :top, :right, :bottom, :mode, :color attr_accessor :x, :y, :z, :width, :height, :mode, :color
attr_reader :image attr_reader :image, :left, :top, :right, :bottom
def initialize(image_path: nil, x: 0, y: 0, z: 0, width: 0, height: 0, mode: :tiled, left: 1, top: 1, right: 1, bottom: 1, color: Gosu::Color::WHITE) def initialize(image_path: nil, x: 0, y: 0, z: 0, width: 0, height: 0, mode: :tiled, left: 1, top: 1, right: 1, bottom: 1, color: Gosu::Color::WHITE)
@image = get_image(image_path) if image_path @image = get_image(image_path) if image_path
@@ -32,8 +32,43 @@ module CyberarmEngine
nine_slice if @image && old_image != @image nine_slice if @image && old_image != @image
end end
def set_edges(left: @left, right: @right, top: @top, bottom: @bottom)
changed = [left == @left, right == @right, top == @top, bottom == @bottom].any?(false)
@left = left
@top = top
@right = right
@bottom = bottom
nine_slice if changed
end
def left=(n)
nine_slice if n != @left
@left = n
n
end
def right=(n)
nine_slice if n != @right
@right = n
n
end
def top=(n)
nine_slice if n != @top
@top = n
n
end
def bottom=(n)
nine_slice if n != @bottom
@bottom = n
n
end
def nine_slice def nine_slice
# pp [@left, @top, @right, @bottom, @image.width] # pp [@left, @top, @right, @bottom, @image.width, @image.height]
@segment_top_left = @image.subimage(0, 0, @left, @top) @segment_top_left = @image.subimage(0, 0, @left, @top)
@segment_top_right = @image.subimage(@image.width - @right, 0, @right, @top) @segment_top_right = @image.subimage(@image.width - @right, 0, @right, @top)
@@ -92,6 +127,7 @@ module CyberarmEngine
@segment_bottom.draw(@x + @segment_bottom_left.width, (@y + @height) - @segment_bottom.height, @z, width_scale, 1, @color) # SCALE X @segment_bottom.draw(@x + @segment_bottom_left.width, (@y + @height) - @segment_bottom.height, @z, width_scale, 1, @color) # SCALE X
@segment_bottom_left.draw(@x, (@y + @height) - @segment_bottom_left.height, @z, 1, 1, @color) @segment_bottom_left.draw(@x, (@y + @height) - @segment_bottom_left.height, @z, 1, 1, @color)
@segment_left.draw(@x, @y + @top, @z, 1, height_scale, @color) # SCALE Y @segment_left.draw(@x, @y + @top, @z, 1, height_scale, @color) # SCALE Y
@segment_middle.draw(@x + @segment_top_left.width, @y + @segment_top.height, @z, width_scale, height_scale, @color) # SCALE X and SCALE Y @segment_middle.draw(@x + @segment_top_left.width, @y + @segment_top.height, @z, width_scale, height_scale, @color) # SCALE X and SCALE Y
end end

View File

@@ -0,0 +1,24 @@
module CyberarmEngine
class Dialog < CyberarmEngine::GuiState
def draw
previous_state&.draw
Gosu.flush
super
end
def update
super
return unless window.current_state == self
window.states.reverse.each do |state|
# Don't update ourselves, forever
next if state == self && state.is_a?(CyberarmEngine::GuiState)
state.update
end
end
end
end

View File

@@ -1,13 +1,13 @@
module CyberarmEngine module CyberarmEngine
module DSL module DSL
def every(milliseconds, &block) def every(milliseconds, &block)
element_parent.gui_state.add_timer( element_parent.root.gui_state.add_timer(
CyberarmEngine::Timer.new(milliseconds, true, &block) CyberarmEngine::Timer.new(milliseconds, true, &block)
) )
end end
def after(milliseconds, &block) def after(milliseconds, &block)
element_parent.gui_state.add_timer( element_parent.root.gui_state.add_timer(
CyberarmEngine::Timer.new(milliseconds, false, &block) CyberarmEngine::Timer.new(milliseconds, false, &block)
) )
end end
@@ -112,6 +112,18 @@ module CyberarmEngine
add_element(Element::Slider.new(options, block)) add_element(Element::Slider.new(options, block))
end end
def page(klass, options = {})
element_parent.root.gui_state.page(klass, options)
end
def current_page
element_parent.root.gui_state.current_page
end
def dialog(klass, options = {})
element_parent.root.gui_state.window.push_state(klass, options)
end
def background(color = Gosu::Color::NONE) def background(color = Gosu::Color::NONE)
element_parent.style.background = color element_parent.style.background = color
end end

View File

@@ -135,10 +135,12 @@ module CyberarmEngine
@background_nine_slice_canvas.color = safe_style_fetch(:background_nine_slice_color) || Gosu::Color::WHITE @background_nine_slice_canvas.color = safe_style_fetch(:background_nine_slice_color) || Gosu::Color::WHITE
@background_nine_slice_canvas.left = safe_style_fetch(:background_nine_slice_left, :background_nine_slice_from_edge) @background_nine_slice_canvas.set_edges(
@background_nine_slice_canvas.top = safe_style_fetch(:background_nine_slice_top, :background_nine_slice_from_edge) left: safe_style_fetch(:background_nine_slice_left, :background_nine_slice_from_edge) || 1,
@background_nine_slice_canvas.right = safe_style_fetch(:background_nine_slice_right, :background_nine_slice_from_edge) top: safe_style_fetch(:background_nine_slice_top, :background_nine_slice_from_edge) || 1,
@background_nine_slice_canvas.bottom = safe_style_fetch(:background_nine_slice_bottom, :background_nine_slice_from_edge) right: safe_style_fetch(:background_nine_slice_right, :background_nine_slice_from_edge) || 1,
bottom: safe_style_fetch(:background_nine_slice_bottom, :background_nine_slice_from_edge) || 1
)
end end
def set_background_image def set_background_image
@@ -457,9 +459,9 @@ module CyberarmEngine
pairs_ << a_ unless pairs_.last == a_ pairs_ << a_ unless pairs_.last == a_
@cached_scroll_height = pairs_.sum { |pair| + styled(:padding_top) + styled(:border_thickness_top) + pair.map(&:outer_height).max } + styled(:padding_bottom) + styled(:border_thickness_bottom) @cached_scroll_height = pairs_.sum { |pair| pair.map(&:outer_height).max } + noncontent_height
else else
@cached_scroll_height = styled(:padding_top) + styled(:border_thickness_top) + @children.sum(&:outer_height) + styled(:padding_bottom) + styled(:border_thickness_bottom) @cached_scroll_height = noncontent_height + @children.sum(&:outer_height)
end end
end end

View File

@@ -197,9 +197,9 @@ module CyberarmEngine
case styled(:v_align) case styled(:v_align)
when :center when :center
@y = parent.y + parent.height / 2 - height / 2 @y = parent.y + parent.styled(:margin_top) + parent.height / 2 - height / 2
when :bottom when :bottom
@y = parent.y + parent.height - height @y = parent.y + parent.styled(:margin_top) + parent.height - height
end end
end end
@@ -208,9 +208,9 @@ module CyberarmEngine
case styled(:h_align) case styled(:h_align)
when :center when :center
@x = parent.x + parent.width / 2 - width / 2 @x = parent.x + parent.styled(:margin_left) + parent.width / 2 - width / 2
when :right when :right
@x = parent.x + parent.width - width @x = parent.x + parent.styled(:margin_left) + parent.width - width
end end
end end

View File

@@ -14,8 +14,8 @@ module CyberarmEngine
def render def render
@image.draw( @image.draw(
styled(:border_thickness_left) + styled(:padding_left) + @x, styled(:margin_left) + styled(:border_thickness_left) + styled(:padding_left) + @x,
styled(:border_thickness_top) + styled(:padding_top) + @y, styled(:margin_top) + styled(:border_thickness_top) + styled(:padding_top) + @y,
@z + 2, @z + 2,
@scale_x, @scale_y, styled(:color) @scale_x, @scale_y, styled(:color)
) )

View File

@@ -18,6 +18,10 @@ module CyberarmEngine
@active_width = window.width @active_width = window.width
@active_height = window.height @active_height = window.height
@pages = {}
@page_host = nil
@page = nil
@menu = nil @menu = nil
@focus = nil @focus = nil
@mouse_over = nil @mouse_over = nil
@@ -43,6 +47,36 @@ module CyberarmEngine
@menu @menu
end end
def page(klass, options = {})
@page&.blur
@pages[klass] = klass.new(parent: options[:parent] || self) unless @pages[klass]
@page = @pages[klass]
@page.options = options
page_host.clear do
@page.setup
end
@page.focus
end
def current_page
@page
end
def page_host
return @page.parent if @page.parent.is_a?(CyberarmEngine::Element::Container)
return @page_host if @page_host && @page_host.is_a?(CyberarmEngine::Element::Container)
@root_container
end
def page_host=(container)
raise "page host must be a CyberarmEngine::Element::Container" unless container.is_a?(Container)
@page_host = container
end
# throws :blur event to focused element and sets GuiState focused element # throws :blur event to focused element and sets GuiState focused element
# Does NOT throw :focus event at element or set element as focused # Does NOT throw :focus event at element or set element as focused
def focus=(element) def focus=(element)
@@ -98,6 +132,8 @@ module CyberarmEngine
end end
@needs_repaint = false @needs_repaint = false
@page&.draw
end end
def needs_repaint? def needs_repaint?
@@ -162,6 +198,8 @@ module CyberarmEngine
@last_mouse_pos = Vector.new(window.mouse_x, window.mouse_y) @last_mouse_pos = Vector.new(window.mouse_x, window.mouse_y)
@mouse_pos = @last_mouse_pos.clone @mouse_pos = @last_mouse_pos.clone
@page&.update
end end
def button_down(id) def button_down(id)
@@ -179,6 +217,8 @@ module CyberarmEngine
end end
@focus.button_down(id) if @focus.respond_to?(:button_down) @focus.button_down(id) if @focus.respond_to?(:button_down)
@page&.button_down(id)
end end
def button_up(id) def button_up(id)
@@ -209,6 +249,8 @@ module CyberarmEngine
# Prevents menu from popping back up if the listbox is clicked to hide it. # Prevents menu from popping back up if the listbox is clicked to hide it.
@hid_menu_for = nil @hid_menu_for = nil
@page&.button_up(id)
end end
def tool_tip_delay def tool_tip_delay

View File

@@ -0,0 +1,39 @@
module CyberarmEngine
class Page
include CyberarmEngine::DSL
include CyberarmEngine::Common
attr_reader :parent
def initialize(parent:)
@parent = parent
@options = {}
end
def options=(options)
@options = options
end
def setup
end
def focus
end
def blur
end
def draw
end
def update
end
def button_down(id)
end
def button_up(id)
end
end
end