Sha256: 72dcff60309941e0b4e3d46be45d05b3d186d96c7c6a394aa4bdbe627ec70238

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

module CyberarmEngine
  class Element
    class ListBox < Button
      attr_accessor :items
      attr_reader :choose

      def initialize(options = {}, block = nil)
        @items = options[:items] || []
        @choose = options[:choose] || @items.first

        super(@choose, options, block)

        @style.background_canvas.background = default(:background)

        # TODO: "Clean Up" into own class?
        @menu = Stack.new(parent: parent, width: @options[:width], theme: @options[:theme])
        @menu.define_singleton_method(:recalculate_menu) do
          @x = @__list_box.x
          @y = @__list_box.y + @__list_box.height
        end
        @menu.instance_variable_set(:"@__list_box", self)

        def @menu.recalculate
          super
          recalculate_menu
        end
      end

      def choose=(item)
        valid = @items.detect { |i| i == item }
        return unless valid # TODO: Throw an error?

        @choose = item

        self.value = item.to_s

        recalculate
      end

      def released_left_mouse_button(_sender, _x, _y)
        show_menu

        :handled
      end

      def show_menu
        @menu.clear
        @items.each do |item|
          [@block]
          block = proc { self.choose = item; @block.call(item) if @block }
          b = Button.new(item,
                         { parent: @menu, width: 1.0, theme: @options[:theme], margin: 0, border_color: 0x00ffffff }, block)

          @menu.add(b)
        end
        recalculate

        root.gui_state.show_menu(@menu)
      end

      def recalculate
        super

        @menu.recalculate
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cyberarm_engine-0.15.0 lib/cyberarm_engine/ui/elements/list_box.rb