mirror of
https://github.com/cyberarm/cyberarm_engine.git
synced 2026-05-06 07:58:15 +00:00
Compare commits
3 Commits
b694269409
...
512d8a5c0f
| Author | SHA1 | Date | |
|---|---|---|---|
| 512d8a5c0f | |||
| d158d2fdd2 | |||
| 9f654129ef |
@@ -67,6 +67,8 @@ require_relative "cyberarm_engine/ui/elements/menu_item"
|
||||
|
||||
require_relative "cyberarm_engine/game_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"
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
module CyberarmEngine
|
||||
class BackgroundNineSlice
|
||||
include CyberarmEngine::Common
|
||||
attr_accessor :x, :y, :z, :width, :height, :left, :top, :right, :bottom, :mode, :color
|
||||
attr_reader :image
|
||||
attr_accessor :x, :y, :z, :width, :height, :mode, :color
|
||||
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)
|
||||
@image = get_image(image_path) if image_path
|
||||
@@ -32,8 +32,43 @@ module CyberarmEngine
|
||||
nine_slice if @image && old_image != @image
|
||||
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
|
||||
# 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_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_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_middle.draw(@x + @segment_top_left.width, @y + @segment_top.height, @z, width_scale, height_scale, @color) # SCALE X and SCALE Y
|
||||
end
|
||||
|
||||
|
||||
24
lib/cyberarm_engine/ui/dialog.rb
Normal file
24
lib/cyberarm_engine/ui/dialog.rb
Normal 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
|
||||
@@ -1,13 +1,13 @@
|
||||
module CyberarmEngine
|
||||
module DSL
|
||||
def every(milliseconds, &block)
|
||||
element_parent.gui_state.add_timer(
|
||||
element_parent.root.gui_state.add_timer(
|
||||
CyberarmEngine::Timer.new(milliseconds, true, &block)
|
||||
)
|
||||
end
|
||||
|
||||
def after(milliseconds, &block)
|
||||
element_parent.gui_state.add_timer(
|
||||
element_parent.root.gui_state.add_timer(
|
||||
CyberarmEngine::Timer.new(milliseconds, false, &block)
|
||||
)
|
||||
end
|
||||
@@ -112,6 +112,18 @@ module CyberarmEngine
|
||||
add_element(Element::Slider.new(options, block))
|
||||
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)
|
||||
element_parent.style.background = color
|
||||
end
|
||||
|
||||
@@ -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.left = safe_style_fetch(:background_nine_slice_left, :background_nine_slice_from_edge)
|
||||
@background_nine_slice_canvas.top = safe_style_fetch(:background_nine_slice_top, :background_nine_slice_from_edge)
|
||||
@background_nine_slice_canvas.right = safe_style_fetch(:background_nine_slice_right, :background_nine_slice_from_edge)
|
||||
@background_nine_slice_canvas.bottom = safe_style_fetch(:background_nine_slice_bottom, :background_nine_slice_from_edge)
|
||||
@background_nine_slice_canvas.set_edges(
|
||||
left: safe_style_fetch(:background_nine_slice_left, :background_nine_slice_from_edge) || 1,
|
||||
top: safe_style_fetch(:background_nine_slice_top, :background_nine_slice_from_edge) || 1,
|
||||
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
|
||||
|
||||
def set_background_image
|
||||
@@ -457,9 +459,9 @@ module CyberarmEngine
|
||||
|
||||
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
|
||||
@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
|
||||
|
||||
|
||||
@@ -197,9 +197,9 @@ module CyberarmEngine
|
||||
|
||||
case styled(:v_align)
|
||||
when :center
|
||||
@y = parent.y + parent.height / 2 - height / 2
|
||||
@y = parent.y + parent.styled(:margin_top) + parent.height / 2 - height / 2
|
||||
when :bottom
|
||||
@y = parent.y + parent.height - height
|
||||
@y = parent.y + parent.styled(:margin_top) + parent.height - height
|
||||
end
|
||||
end
|
||||
|
||||
@@ -208,9 +208,9 @@ module CyberarmEngine
|
||||
|
||||
case styled(:h_align)
|
||||
when :center
|
||||
@x = parent.x + parent.width / 2 - width / 2
|
||||
@x = parent.x + parent.styled(:margin_left) + parent.width / 2 - width / 2
|
||||
when :right
|
||||
@x = parent.x + parent.width - width
|
||||
@x = parent.x + parent.styled(:margin_left) + parent.width - width
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -14,8 +14,8 @@ module CyberarmEngine
|
||||
|
||||
def render
|
||||
@image.draw(
|
||||
styled(:border_thickness_left) + styled(:padding_left) + @x,
|
||||
styled(:border_thickness_top) + styled(:padding_top) + @y,
|
||||
styled(:margin_left) + styled(:border_thickness_left) + styled(:padding_left) + @x,
|
||||
styled(:margin_top) + styled(:border_thickness_top) + styled(:padding_top) + @y,
|
||||
@z + 2,
|
||||
@scale_x, @scale_y, styled(:color)
|
||||
)
|
||||
|
||||
@@ -18,6 +18,10 @@ module CyberarmEngine
|
||||
@active_width = window.width
|
||||
@active_height = window.height
|
||||
|
||||
@pages = {}
|
||||
@page_host = nil
|
||||
@page = nil
|
||||
|
||||
@menu = nil
|
||||
@focus = nil
|
||||
@mouse_over = nil
|
||||
@@ -43,6 +47,36 @@ module CyberarmEngine
|
||||
@menu
|
||||
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
|
||||
# Does NOT throw :focus event at element or set element as focused
|
||||
def focus=(element)
|
||||
@@ -98,6 +132,8 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
@needs_repaint = false
|
||||
|
||||
@page&.draw
|
||||
end
|
||||
|
||||
def needs_repaint?
|
||||
@@ -162,6 +198,8 @@ module CyberarmEngine
|
||||
|
||||
@last_mouse_pos = Vector.new(window.mouse_x, window.mouse_y)
|
||||
@mouse_pos = @last_mouse_pos.clone
|
||||
|
||||
@page&.update
|
||||
end
|
||||
|
||||
def button_down(id)
|
||||
@@ -179,6 +217,8 @@ module CyberarmEngine
|
||||
end
|
||||
|
||||
@focus.button_down(id) if @focus.respond_to?(:button_down)
|
||||
|
||||
@page&.button_down(id)
|
||||
end
|
||||
|
||||
def button_up(id)
|
||||
@@ -209,6 +249,8 @@ module CyberarmEngine
|
||||
|
||||
# Prevents menu from popping back up if the listbox is clicked to hide it.
|
||||
@hid_menu_for = nil
|
||||
|
||||
@page&.button_up(id)
|
||||
end
|
||||
|
||||
def tool_tip_delay
|
||||
|
||||
39
lib/cyberarm_engine/ui/page.rb
Normal file
39
lib/cyberarm_engine/ui/page.rb
Normal 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
|
||||
Reference in New Issue
Block a user