Sha256: 29dbcdba7e34c021b6da6cc41c1d6ccecc6870ebc61cf068ca4dc4a7b102ec3b

Contents?: true

Size: 1.29 KB

Versions: 5

Compression:

Stored size: 1.29 KB

Contents

# encoding: utf-8

module Fidgit
  class Button < Label
    # @param (see Label#initialize)
    # @option (see Label#initialize)
    def initialize(text, options = {}, &block)
      options = {
        color: default(:color),
        background_color: default(:background_color),
        border_color: default(:border_color),
      }.merge! options

      super(text, options)

      update_colors
    end

    def clicked_left_mouse_button(sender, x, y)
      # TODO: Play click sound?
      nil
    end

    def enabled=(value)
      super(value)
      update_colors

      value
    end

    def enter(sender)
      @mouse_over = true
      update_colors

      nil
    end

    def leave(sender)
      @mouse_over = false
      update_colors

      nil
    end

    protected
    def update_colors
      @background_color = if @mouse_over and enabled?
        default(:hover, :background_color)
      else
        default(:background_color)
      end

      @color = if enabled?
        default(:color)
      else
        default(:disabled, :color)
      end

      nil
    end

    protected
    # A block added to any button subscribes to LMB click.
    def post_init_block(&block)
      subscribe :clicked_left_mouse_button, &block
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
fidgit-0.0.6alpha lib/fidgit/elements/button.rb
fidgit-0.0.5alpha lib/fidgit/elements/button.rb
fidgit-0.0.4alpha lib/fidgit/elements/button.rb
fidgit-0.0.3alpha lib/fidgit/elements/button.rb
fidgit-0.0.2alpha lib/fidgit/elements/button.rb