lighttroupe/luz-next

View on GitHub
gui/gui_select.rb

Summary

Maintainability
A
0 mins
Test Coverage
#
# GuiSelect is a non-popup selection from a list
#
class GuiSelect < GuiListSelect
    DEFAULT_LABEL_WIDTH = 30        # in letters

    def initialize(object, method, options)        # options is [[:one,'One'],[:two,'Two']]
        super(object, method)
        @options = options
        @list = @options.map(&:first)        # keys
        @selected_label = GuiLabel.new.set(:width => DEFAULT_LABEL_WIDTH, :string => selected_label_text, :scale_x => 0.9)
        @color = [0.6, 0.6, 1.0, 1.0]
    end

    pipe [:width=, :text_align=], :selected_label        # (label width override by parent)

    #
    # API
    #
    def set_value(value)
        super(value)
        @selected_label.set_string(selected_label_text)
    end

    def list
        @list
    end

    #
    # Rendering
    #
    def gui_render
        with_gui_object_properties {
            @selected_label.gui_render
        }
    end

    #
    # Pointer
    #
    def click(pointer)
        scroll_down!(pointer)
    end

    #
    # Settings
    #
    def step_amount
        1
    end

private

    def find_option_string_by_value(value)
        v = @options.find { |o| o.first == value }
        v.last if v        # otherwise nil (last is the string part)
    end

    def selected_label_text
        find_option_string_by_value(get_value) || ''
    end
end