mirror of
https://github.com/cyberarm/cyberarm_engine.git
synced 2025-12-17 13:32:34 +00:00
Compare commits
9 Commits
v0.20.0
...
31e909eb30
| Author | SHA1 | Date | |
|---|---|---|---|
| 31e909eb30 | |||
| 300e7c4e59 | |||
| be98fe47ad | |||
| a75afaf47a | |||
| d81df5f4e2 | |||
| f2ea0d9942 | |||
| 08d068f156 | |||
| a4d02038c3 | |||
| 37bdd6ef23 |
@@ -24,6 +24,7 @@ require_relative "cyberarm_engine/transform"
|
|||||||
require_relative "cyberarm_engine/ray"
|
require_relative "cyberarm_engine/ray"
|
||||||
require_relative "cyberarm_engine/background"
|
require_relative "cyberarm_engine/background"
|
||||||
require_relative "cyberarm_engine/background_nine_slice"
|
require_relative "cyberarm_engine/background_nine_slice"
|
||||||
|
require_relative "cyberarm_engine/background_image"
|
||||||
require_relative "cyberarm_engine/animator"
|
require_relative "cyberarm_engine/animator"
|
||||||
|
|
||||||
require_relative "cyberarm_engine/text"
|
require_relative "cyberarm_engine/text"
|
||||||
|
|||||||
93
lib/cyberarm_engine/background_image.rb
Normal file
93
lib/cyberarm_engine/background_image.rb
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
module CyberarmEngine
|
||||||
|
class BackgroundImage
|
||||||
|
include CyberarmEngine::Common
|
||||||
|
attr_accessor :x, :y, :z, :mode
|
||||||
|
attr_reader :image, :width, :height, :color
|
||||||
|
|
||||||
|
def initialize(image_path: nil, x: 0, y: 0, z: 0, width: 0, height: 0, mode: :fill, color: Gosu::Color::WHITE)
|
||||||
|
@image_path = image_path
|
||||||
|
@image = get_image(image_path) if image_path
|
||||||
|
|
||||||
|
@x = x
|
||||||
|
@y = y
|
||||||
|
@z = z
|
||||||
|
@width = width
|
||||||
|
@height = height
|
||||||
|
|
||||||
|
@mode = mode
|
||||||
|
|
||||||
|
@color = color
|
||||||
|
|
||||||
|
@cached_tiling = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def image=(image_path)
|
||||||
|
@cached_tiling = nil if image_path != @image_path
|
||||||
|
@image_path = image_path
|
||||||
|
@image = image_path ? get_image(image_path) : image_path
|
||||||
|
end
|
||||||
|
|
||||||
|
def width=(n)
|
||||||
|
@cached_tiling = nil if @width != n
|
||||||
|
@width = n
|
||||||
|
end
|
||||||
|
|
||||||
|
def height=(n)
|
||||||
|
@cached_tiling = nil if @height != n
|
||||||
|
@height = n
|
||||||
|
end
|
||||||
|
|
||||||
|
def color=(c)
|
||||||
|
@cached_tiling = nil if @color != c
|
||||||
|
@color = c
|
||||||
|
end
|
||||||
|
|
||||||
|
def width_scale
|
||||||
|
(@width.to_f / @image.width).abs
|
||||||
|
end
|
||||||
|
|
||||||
|
def height_scale
|
||||||
|
(@height.to_f / @image.height).abs
|
||||||
|
end
|
||||||
|
|
||||||
|
def draw
|
||||||
|
return unless @image
|
||||||
|
|
||||||
|
Gosu.clip_to(@x, @y, @width, @height) do
|
||||||
|
send(:"draw_#{mode}")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def draw_stretch
|
||||||
|
@image.draw(@x, @y, @z, width_scale, height_scale, @color)
|
||||||
|
end
|
||||||
|
|
||||||
|
def draw_tiled
|
||||||
|
@cached_tiling ||= Gosu.record(@width, @height) do
|
||||||
|
height_scale.ceil.times do |y|
|
||||||
|
width_scale.ceil.times do |x|
|
||||||
|
@image.draw(x * @image.width, y * @image.height, @z, 1, 1, @color)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@cached_tiling.draw(@x, @y, @z)
|
||||||
|
end
|
||||||
|
|
||||||
|
def draw_fill
|
||||||
|
if @width * width_scale > height * height_scale
|
||||||
|
draw_fill_width
|
||||||
|
else
|
||||||
|
draw_fill_height
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def draw_fill_width
|
||||||
|
@image.draw(@x, @y, @z, width_scale, width_scale, @color)
|
||||||
|
end
|
||||||
|
|
||||||
|
def draw_fill_height
|
||||||
|
@image.draw(@x, @y, @z, height_scale, height_scale, @color)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -38,6 +38,7 @@ module CyberarmEngine
|
|||||||
|
|
||||||
@style.background_canvas = Background.new
|
@style.background_canvas = Background.new
|
||||||
@style.background_nine_slice_canvas = BackgroundNineSlice.new
|
@style.background_nine_slice_canvas = BackgroundNineSlice.new
|
||||||
|
@style.background_image_canvas = BackgroundImage.new
|
||||||
@style.border_canvas = BorderCanvas.new(element: self)
|
@style.border_canvas = BorderCanvas.new(element: self)
|
||||||
|
|
||||||
@style_event = :default
|
@style_event = :default
|
||||||
@@ -60,6 +61,7 @@ module CyberarmEngine
|
|||||||
|
|
||||||
set_background
|
set_background
|
||||||
set_background_nine_slice
|
set_background_nine_slice
|
||||||
|
set_background_image
|
||||||
|
|
||||||
set_border_thickness
|
set_border_thickness
|
||||||
set_border_color
|
set_border_color
|
||||||
@@ -104,6 +106,14 @@ module CyberarmEngine
|
|||||||
@style.background_nine_slice_bottom = safe_style_fetch(:background_nine_slice_bottom) || @style.background_nine_slice_from_edge
|
@style.background_nine_slice_bottom = safe_style_fetch(:background_nine_slice_bottom) || @style.background_nine_slice_from_edge
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def set_background_image
|
||||||
|
@style.background_image = safe_style_fetch(:background_image)
|
||||||
|
@style.background_image_mode = safe_style_fetch(:background_image_mode) || :stretch
|
||||||
|
@style.background_image_color = safe_style_fetch(:background_image_color) || Gosu::Color::WHITE
|
||||||
|
@style.background_image_canvas.mode = @style.background_image_mode
|
||||||
|
@style.background_image_canvas.color = @style.background_image_color
|
||||||
|
end
|
||||||
|
|
||||||
def set_border_thickness
|
def set_border_thickness
|
||||||
@style.border_thickness = safe_style_fetch(:border_thickness)
|
@style.border_thickness = safe_style_fetch(:border_thickness)
|
||||||
|
|
||||||
@@ -288,6 +298,7 @@ module CyberarmEngine
|
|||||||
|
|
||||||
@style.background_canvas.draw
|
@style.background_canvas.draw
|
||||||
@style.background_nine_slice_canvas.draw
|
@style.background_nine_slice_canvas.draw
|
||||||
|
@style.background_image_canvas.draw
|
||||||
@style.border_canvas.draw
|
@style.border_canvas.draw
|
||||||
|
|
||||||
render
|
render
|
||||||
@@ -429,11 +440,34 @@ module CyberarmEngine
|
|||||||
def dimensional_size(size, dimension)
|
def dimensional_size(size, dimension)
|
||||||
raise "dimension must be either :width or :height" unless %i[width height].include?(dimension)
|
raise "dimension must be either :width or :height" unless %i[width height].include?(dimension)
|
||||||
|
|
||||||
if size.is_a?(Numeric) && size.between?(0.0, 1.0)
|
new_size = if size.is_a?(Numeric) && size.between?(0.0, 1.0)
|
||||||
(@parent.send(:"content_#{dimension}") * size).round - send(:"noncontent_#{dimension}").round
|
(@parent.send(:"content_#{dimension}") * size).floor - send(:"noncontent_#{dimension}").floor
|
||||||
else
|
else
|
||||||
size
|
size
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if @parent && @style.fill # Handle fill behavior
|
||||||
|
fill_siblings = @parent.children.select { |c| c.style.fill }.count.to_f # include self since we're dividing
|
||||||
|
|
||||||
|
if dimension == :width && @parent.is_a?(Flow)
|
||||||
|
space_available_width = ((@parent.content_width - (@parent.children.reject { |c| c.style.fill }).map(&:outer_width).sum) / fill_siblings)
|
||||||
|
space_available_width = space_available_width.nan? ? 0 : space_available_width.floor # The parent element might not have its dimensions, yet.
|
||||||
|
|
||||||
|
return space_available_width - noncontent_width
|
||||||
|
|
||||||
|
elsif dimension == :height && @parent.is_a?(Stack)
|
||||||
|
space_available_height = ((@parent.content_height - (@parent.children.reject { |c| c.style.fill }).map(&:outer_height).sum) / fill_siblings)
|
||||||
|
space_available_height = space_available_height.nan? ? 0 : space_available_height.floor # The parent element might not have its dimensions, yet.
|
||||||
|
|
||||||
|
return space_available_height - noncontent_height
|
||||||
|
end
|
||||||
|
|
||||||
|
else # Handle min_width/height and max_width/height
|
||||||
|
return @style.send(:"min_#{dimension}") if @style.send(:"min_#{dimension}") && new_size < @style.send(:"min_#{dimension}")
|
||||||
|
return @style.send(:"max_#{dimension}") if @style.send(:"max_#{dimension}") && new_size > @style.send(:"max_#{dimension}")
|
||||||
|
end
|
||||||
|
|
||||||
|
new_size
|
||||||
end
|
end
|
||||||
|
|
||||||
def background=(_background)
|
def background=(_background)
|
||||||
@@ -450,6 +484,7 @@ module CyberarmEngine
|
|||||||
|
|
||||||
@style.background_canvas.update
|
@style.background_canvas.update
|
||||||
update_background_nine_slice
|
update_background_nine_slice
|
||||||
|
update_background_image
|
||||||
@style.border_canvas.update
|
@style.border_canvas.update
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -477,6 +512,24 @@ module CyberarmEngine
|
|||||||
@style.background_nine_slice_canvas.image = @style.background_nine_slice
|
@style.background_nine_slice_canvas.image = @style.background_nine_slice
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def background_image=(image_path)
|
||||||
|
@style.background_image = image_path.is_a?(Gosu::Image) ? image_path : get_image(image_path)
|
||||||
|
update_background_image
|
||||||
|
end
|
||||||
|
|
||||||
|
def update_background_image
|
||||||
|
@style.background_image_canvas.x = @x
|
||||||
|
@style.background_image_canvas.y = @y
|
||||||
|
@style.background_image_canvas.z = @z
|
||||||
|
@style.background_image_canvas.width = width
|
||||||
|
@style.background_image_canvas.height = height
|
||||||
|
|
||||||
|
@style.background_image_canvas.mode = @style.background_image_mode
|
||||||
|
@style.background_image_canvas.color = @style.background_image_color
|
||||||
|
|
||||||
|
@style.background_image_canvas.image = @style.background_image
|
||||||
|
end
|
||||||
|
|
||||||
def root
|
def root
|
||||||
return self if is_root?
|
return self if is_root?
|
||||||
|
|
||||||
|
|||||||
@@ -64,8 +64,8 @@ module CyberarmEngine
|
|||||||
@scale_y = 1
|
@scale_y = 1
|
||||||
end
|
end
|
||||||
|
|
||||||
@width = _width || @image.width.round * @scale_x
|
@width = _width || @image.width.floor * @scale_x
|
||||||
@height = _height || @image.height.round * @scale_y
|
@height = _height || @image.height.floor * @scale_y
|
||||||
|
|
||||||
update_background
|
update_background
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -122,8 +122,8 @@ module CyberarmEngine
|
|||||||
_width = dimensional_size(@style.width, :width)
|
_width = dimensional_size(@style.width, :width)
|
||||||
_height = dimensional_size(@style.height, :height)
|
_height = dimensional_size(@style.height, :height)
|
||||||
|
|
||||||
@width = _width || (@children.map { |c| c.x + c.outer_width }.max || 0).round
|
@width = _width || (@children.map { |c| c.x + c.outer_width }.max || 0).floor
|
||||||
@height = _height || (@children.map { |c| c.y + c.outer_height }.max || 0).round
|
@height = _height || (@children.map { |c| c.y + c.outer_height }.max || 0).floor
|
||||||
end
|
end
|
||||||
|
|
||||||
# Move child to parent after positioning
|
# Move child to parent after positioning
|
||||||
@@ -151,12 +151,14 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def max_width
|
def max_width
|
||||||
_width = dimensional_size(@style.width, :width)
|
# _width = dimensional_size(@style.width, :width)
|
||||||
if _width
|
# if _width
|
||||||
|
# outer_width
|
||||||
|
# else
|
||||||
|
# window.width - (@parent ? @parent.style.margin_right + @style.margin_right : @style.margin_right)
|
||||||
|
# end
|
||||||
|
|
||||||
outer_width
|
outer_width
|
||||||
else
|
|
||||||
window.width - (@parent ? @parent.style.margin_right + @style.margin_right : @style.margin_right)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def fits_on_line?(element) # Flow
|
def fits_on_line?(element) # Flow
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def row_at(y)
|
def row_at(y)
|
||||||
((y - @text.y) / @text.textobject.height).round
|
((y - @text.y) / @text.textobject.height).floor
|
||||||
end
|
end
|
||||||
|
|
||||||
def column_at(x, y)
|
def column_at(x, y)
|
||||||
|
|||||||
@@ -45,8 +45,8 @@ module CyberarmEngine
|
|||||||
@scale_y = 1
|
@scale_y = 1
|
||||||
end
|
end
|
||||||
|
|
||||||
@width = _width || @image.width.round * @scale_x
|
@width = _width || @image.width.floor * @scale_x
|
||||||
@height = _height || @image.height.round * @scale_y
|
@height = _height || @image.height.floor * @scale_y
|
||||||
|
|
||||||
update_background
|
update_background
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -17,6 +17,8 @@ module CyberarmEngine
|
|||||||
@menu.define_singleton_method(:recalculate_menu) do
|
@menu.define_singleton_method(:recalculate_menu) do
|
||||||
@x = @__list_box.x
|
@x = @__list_box.x
|
||||||
@y = @__list_box.y + @__list_box.height
|
@y = @__list_box.y + @__list_box.height
|
||||||
|
|
||||||
|
@y = @__list_box.y - height if @y + height > window.height
|
||||||
end
|
end
|
||||||
@menu.instance_variable_set(:"@__list_box", self)
|
@menu.instance_variable_set(:"@__list_box", self)
|
||||||
|
|
||||||
|
|||||||
@@ -18,8 +18,15 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def render
|
def render
|
||||||
|
# Gosu.clip_to is too expensive to always use so check if we actually need it.
|
||||||
|
if @text.width > width || @text.height > height
|
||||||
|
Gosu.clip_to(@x, @y, width, height) do
|
||||||
@text.draw
|
@text.draw
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
@text.draw
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def recalculate
|
def recalculate
|
||||||
unless @enabled
|
unless @enabled
|
||||||
@@ -36,8 +43,8 @@ module CyberarmEngine
|
|||||||
|
|
||||||
handle_text_wrapping(_width)
|
handle_text_wrapping(_width)
|
||||||
|
|
||||||
@width = _width || @text.width.round
|
@width = _width || @text.width.floor
|
||||||
@height = _height || @text.height.round
|
@height = _height || @text.height.floor
|
||||||
|
|
||||||
@text.y = @style.border_thickness_top + @style.padding_top + @y
|
@text.y = @style.border_thickness_top + @style.padding_top + @y
|
||||||
@text.z = @z + 3
|
@text.z = @z + 3
|
||||||
|
|||||||
Reference in New Issue
Block a user