Added support for UI to have background_images, fixed TextBlock text overdrawing

This commit is contained in:
2022-04-25 20:12:10 -05:00
parent d81df5f4e2
commit a75afaf47a
4 changed files with 96 additions and 1 deletions

View File

@@ -24,6 +24,7 @@ require_relative "cyberarm_engine/transform"
require_relative "cyberarm_engine/ray"
require_relative "cyberarm_engine/background"
require_relative "cyberarm_engine/background_nine_slice"
require_relative "cyberarm_engine/background_image"
require_relative "cyberarm_engine/animator"
require_relative "cyberarm_engine/text"

View File

@@ -0,0 +1,55 @@
module CyberarmEngine
class BackgroundImage
include CyberarmEngine::Common
attr_accessor :x, :y, :z, :width, :height, :mode, :color
attr_reader :image
def initialize(image_path: nil, x: 0, y: 0, z: 0, width: 0, height: 0, mode: :fill, color: Gosu::Color::WHITE)
@image = get_image(image_path) if image_path
@x = x
@y = y
@z = z
@width = width
@height = height
@mode = mode
@color = color
end
def image=(image_path)
@image = image_path ? get_image(image_path) : image_path
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
send(:"draw_#{mode}")
end
def draw_stretch
@image.draw(@x, @y, @z, width_scale, height_scale, @color)
end
def draw_tiled
raise NotImplementedError
end
def draw_fill
if @width * width_scale > height * height_scale
@image.draw(@x, @y, @z, width_scale, width_scale, @color)
else
@image.draw(@x, @y, @z, height_scale, height_scale, @color)
end
end
end
end

View File

@@ -38,6 +38,7 @@ module CyberarmEngine
@style.background_canvas = Background.new
@style.background_nine_slice_canvas = BackgroundNineSlice.new
@style.background_image_canvas = BackgroundImage.new
@style.border_canvas = BorderCanvas.new(element: self)
@style_event = :default
@@ -60,6 +61,7 @@ module CyberarmEngine
set_background
set_background_nine_slice
set_background_image
set_border_thickness
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
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
@style.border_thickness = safe_style_fetch(:border_thickness)
@@ -288,6 +298,7 @@ module CyberarmEngine
@style.background_canvas.draw
@style.background_nine_slice_canvas.draw
@style.background_image_canvas.draw
@style.border_canvas.draw
render
@@ -469,6 +480,7 @@ module CyberarmEngine
@style.background_canvas.update
update_background_nine_slice
update_background_image
@style.border_canvas.update
end
@@ -496,6 +508,26 @@ module CyberarmEngine
@style.background_nine_slice_canvas.image = @style.background_nine_slice
end
def background_image=(image_path)
pp @style.background_image_canvas.image
@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
return self if is_root?

View File

@@ -18,7 +18,14 @@ module CyberarmEngine
end
def render
@text.draw
# 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
end
else
@text.draw
end
end
def recalculate