From a75afaf47a4e1a5aef10ceddc3cbbe8c5ab751f7 Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Mon, 25 Apr 2022 20:12:10 -0500 Subject: [PATCH] Added support for UI to have background_images, fixed TextBlock text overdrawing --- lib/cyberarm_engine.rb | 1 + lib/cyberarm_engine/background_image.rb | 55 +++++++++++++++++++ lib/cyberarm_engine/ui/element.rb | 32 +++++++++++ lib/cyberarm_engine/ui/elements/text_block.rb | 9 ++- 4 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 lib/cyberarm_engine/background_image.rb diff --git a/lib/cyberarm_engine.rb b/lib/cyberarm_engine.rb index ecc1343..f56f080 100644 --- a/lib/cyberarm_engine.rb +++ b/lib/cyberarm_engine.rb @@ -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" diff --git a/lib/cyberarm_engine/background_image.rb b/lib/cyberarm_engine/background_image.rb new file mode 100644 index 0000000..6efdafc --- /dev/null +++ b/lib/cyberarm_engine/background_image.rb @@ -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 diff --git a/lib/cyberarm_engine/ui/element.rb b/lib/cyberarm_engine/ui/element.rb index e269851..37dc2af 100644 --- a/lib/cyberarm_engine/ui/element.rb +++ b/lib/cyberarm_engine/ui/element.rb @@ -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? diff --git a/lib/cyberarm_engine/ui/elements/text_block.rb b/lib/cyberarm_engine/ui/elements/text_block.rb index f4d88f5..0e065c9 100644 --- a/lib/cyberarm_engine/ui/elements/text_block.rb +++ b/lib/cyberarm_engine/ui/elements/text_block.rb @@ -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