Sha256: 89c6253b73b128096f65d7ca779dc3aa29288824c3112a41ae75ef3cf4aa0f4d

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

module Vedeu
  class Menu
    def initialize(collection)
      @collection = collection
      @current    = 0
      @selected   = nil
      @events     = events
    end

    def events
      @_events ||= Vedeu.events.add(self) do
        on(:menu_next)     { next_item     }
        on(:menu_prev)     { prev_item     }
        on(:menu_top)      { top_item      }
        on(:menu_bottom)   { bottom_item   }
        on(:menu_select)   { select_item   }
        on(:menu_deselect) { deselect_item }

        on(:menu_selected) { selected_item }
        on(:menu_current)  { current_item  }
        on(:menu_items)    { items         }
      end
    end

    def current
      @current
    end

    def selected
      @selected
    end

    def current_item
      @collection[@current]
    end

    def selected_item
      return nil unless @selected

      @collection[@selected]
    end

    def items
      items = []
      @collection.each_with_index do |item, index|
        if index == @current && index == @selected
          items << [true, true, item]

        elsif index == @current
          items << [false, true, item]

        elsif index == @selected
          items << [true, false, item]

        else
          items << [false, false, item]

        end
      end
      items
    end

    def view
      items[@current, @collection.size]
    end

    def top_item
      @current = 0

      items
    end

    def bottom_item
      @current = last

      items
    end

    def next_item
      @current += 1 if @current < last

      items
    end

    def prev_item
      @current -= 1 if @current > 0

      items
    end

    def select_item
      @selected = @current

      items
    end

    def deselect_item
      @selected = nil

      items
    end

    def last
      @collection.size - 1
    end

    def size
      @collection.size
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vedeu-0.1.10 lib/vedeu/support/menu.rb