Sha256: 460c3819c006df50d41a16e748e40a5562f41cd5de4364cb8704d89113453d9f

Contents?: true

Size: 1.57 KB

Versions: 5

Compression:

Stored size: 1.57 KB

Contents

module CyberarmEngine
  class Element
    class ToggleButton < Button
      attr_reader :toggled, :value

      def initialize(options, block = nil)
        if options.dig(:theme, :ToggleButton, :checkmark_image)
          options[:theme][:ToggleButton][:image_width] ||= options[:theme][:TextBlock][:text_size]
          super(get_image(options.dig(:theme, :ToggleButton, :checkmark_image)), options, block)

          @_image = @image
        else
          super(options[:checkmark], options, block)
        end

        @value = options[:checked] || false

        if @value
          @image = @_image if @_image
          @raw_text = @options[:checkmark]
        else
          @image = nil
          @raw_text = ""
        end
      end

      def clicked_left_mouse_button(_sender, _x, _y)
        self.value = !@value

        @block.call(self) if @block

        :handled
      end

      def recalculate
        super
        return if @image

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

        @width  = _width  || @text.textobject.text_width(@options[:checkmark])
        @height = _height || @text.height

        update_background
      end

      def value=(boolean)
        @value = boolean

        if boolean
          @image = @_image if @_image
          @raw_text = @options[:checkmark]
        else
          @image = nil
          @raw_text = ""
        end

        recalculate

        publish(:changed, @value)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
cyberarm_engine-0.24.2 lib/cyberarm_engine/ui/elements/toggle_button.rb
cyberarm_engine-0.24.1 lib/cyberarm_engine/ui/elements/toggle_button.rb
cyberarm_engine-0.24.0 lib/cyberarm_engine/ui/elements/toggle_button.rb
cyberarm_engine-0.23.0 lib/cyberarm_engine/ui/elements/toggle_button.rb
cyberarm_engine-0.22.0 lib/cyberarm_engine/ui/elements/toggle_button.rb