Sha256: d6f98b31487a8840362ca0e3629683915263889797df79abd7bb94bba16782aa

Contents?: true

Size: 1.91 KB

Versions: 2

Compression:

Stored size: 1.91 KB

Contents

module CyberarmEngine
  class Element
    class Image < Element
      def initialize(path_or_image, options = {}, block = nil)
        super(options, block)
        @path = path_or_image if path_or_image.is_a?(String)

        @image = Gosu::Image.new(path_or_image, retro: @options[:retro], tileable: @options[:tileable]) if @path
        @image = path_or_image unless @path

        @scale_x = 1
        @scale_y = 1
      end

      def render
        @image.draw(
          @style.border_thickness_left + @style.padding_left + @x,
          @style.border_thickness_top + @style.padding_top + @y,
          @z + 2,
          @scale_x, @scale_y, @style.color
        )
      end

      def clicked_left_mouse_button(_sender, _x, _y)
        @block.call(self) if @block

        :handled
      end

      def layout
        _width = dimensional_size(@style.width, :width)
        _height = dimensional_size(@style.height, :height)

        if _width && _height
          @scale_x = _width.to_f / @image.width
          @scale_y = _height.to_f / @image.height
        elsif _width
          @scale_x = _width.to_f / @image.width
          @scale_y = @scale_x
        elsif _height
          @scale_y = _height.to_f / @image.height
          @scale_x = @scale_y
        else
          @scale_x = 1
          @scale_y = 1
        end

        @width = _width || (@image.width * @scale_x).floor
        @height = _height || (@image.height * @scale_y).floor

        update_background
      end

      def value
        @image
      end

      def value=(path_or_image, retro: false, tileable: false)
        @path = path_or_image if path_or_image.is_a?(String)

        @image = Gosu::Image.new(path_or_image, retro: retro, tileable: tileable) if @path
        @image = path_or_image unless @path

        recalculate
      end

      def path
        @path
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cyberarm_engine-0.24.4 lib/cyberarm_engine/ui/elements/image.rb
cyberarm_engine-0.24.3 lib/cyberarm_engine/ui/elements/image.rb