Added support for tiled 9 slice backgrounds

This commit is contained in:
2020-12-18 12:43:53 -06:00
parent 164a46c1fb
commit 0c9874c53b

View File

@@ -3,7 +3,7 @@ module CyberarmEngine
include CyberarmEngine::Common include CyberarmEngine::Common
attr_accessor :x, :y, :z, :width, :height attr_accessor :x, :y, :z, :width, :height
def initialize(image_path:, x: 0, y: 0, z: 0, width: 64, height: 64, left: 4, top: 4, right: 56, bottom: 56) def initialize(image_path:, x: 0, y: 0, z: 0, width: 64, height: 64, mode: :tiled, left: 4, top: 4, right: 56, bottom: 56)
@image = get_image(image_path) @image = get_image(image_path)
@x = x @x = x
@@ -13,6 +13,8 @@ module CyberarmEngine
@width = width @width = width
@height = height @height = height
@mode = mode
@left = left @left = left
@top = top @top = top
@right = right @right = right
@@ -62,6 +64,10 @@ module CyberarmEngine
end end
def draw def draw
@mode == :tiled ? draw_tiled : draw_stretched
end
def draw_stretched
@segment_top_left.draw(@x, @y, @z) @segment_top_left.draw(@x, @y, @z)
@segment_top.draw(@x + @segment_top_left.width, @y, @z, width_scale) # SCALE X @segment_top.draw(@x + @segment_top_left.width, @y, @z, width_scale) # SCALE X
@segment_top_right.draw((@x + @width) - @segment_top_right.width, @y, @z) @segment_top_right.draw((@x + @width) - @segment_top_right.width, @y, @z)
@@ -73,5 +79,47 @@ module CyberarmEngine
@segment_left.draw(@x, @y + @top, @z, 1, height_scale) # SCALE Y @segment_left.draw(@x, @y + @top, @z, 1, height_scale) # SCALE Y
@segment_middle.draw(@x + @segment_top_left.width, @y + @segment_top.height, @z, width_scale, height_scale) # SCALE X and SCALE Y @segment_middle.draw(@x + @segment_top_left.width, @y + @segment_top.height, @z, width_scale, height_scale) # SCALE X and SCALE Y
end end
def draw_tiled
@segment_top_left.draw(@x, @y, @z)
Gosu.clip_to(@x + @segment_top_left.width, @y, @segment_top.width * width_scale, @segment_top.height) do
width_scale.ceil.times do |i|
@segment_top.draw(@x + @segment_top_left.width + (@segment_top.width * i), @y, @z) # SCALE X
end
end
@segment_top_right.draw((@x + @width) - @segment_top_right.width, @y, @z)
Gosu.clip_to(@x + @width - @segment_top_right.width, @y + @top, @segment_right.width, @segment_right.height * height_scale) do
height_scale.ceil.times do |i|
@segment_right.draw((@x + @width) - @segment_right.width, @y + @top + (@segment_right.height * i), @z) # SCALE Y
end
end
@segment_bottom_right.draw((@x + @width) - @segment_bottom_right.width, @y + @height - @segment_bottom_right.height, @z)
Gosu.clip_to(@x + @segment_top_left.width, @y + @height - @segment_bottom.height, @segment_top.width * width_scale, @segment_bottom.height) do
width_scale.ceil.times do |i|
@segment_bottom.draw(@x + @segment_bottom_left.width + (@segment_bottom.width * i), (@y + @height) - @segment_bottom.height, @z) # SCALE X
end
end
@segment_bottom_left.draw(@x, (@y + @height) - @segment_bottom_left.height, @z)
Gosu.clip_to(@x, @y + @top, @segment_left.width, @segment_left.height * height_scale) do
height_scale.ceil.times do |i|
@segment_left.draw(@x, @y + @top + (@segment_left.height * i), @z) # SCALE Y
end
end
Gosu.clip_to(@x + @segment_top_left.width, @y + @segment_top.height, @width - (@segment_left.width + @segment_right.width), @height - (@segment_top.height + @segment_bottom.height)) do
height_scale.ceil.times do |y|
width_scale.ceil.times do |x|
@segment_middle.draw(@x + @segment_top_left.width + (@segment_middle.width * x), @y + @segment_top.height + (@segment_middle.height * y), @z) # SCALE X and SCALE Y
end
end
end
end
end end
end end