Class: MiniGL::DropDownList

Inherits:
Component show all
Defined in:
lib/minigl/forms.rb

Overview

This class represents a “drop-down list” form component, here composed of a group of Button objects.

Instance Attribute Summary collapse

Attributes inherited from Component

#enabled, #h, #params, #visible, #w, #x, #y

Instance Method Summary collapse

Constructor Details

#initialize(x, y = nil, font = nil, img = nil, opt_img = nil, options = nil, option = 0, text_margin = 0, width = nil, height = nil, text_color = 0, disabled_text_color = 0, over_text_color = 0, down_text_color = 0, &on_changed) ⇒ DropDownList

Creates a new drop-down list.

Parameters:

x

The x-coordinate of the object.

y

The y-coordinate of the object.

font

Font to be used by the buttons that compose the drop-donwn list.

img

Image of the main button, i.e., the one at the top, that toggles visibility of the other buttons (the “option” buttons).

opt_img

Image for the “option” buttons, as described above.

options

Array of available options for this control (+String+s).

option

Index of the firstly selected option.

text_margin

Left margin of the text inside the buttons (vertically, the text will always be centered).

width

Width of the control, used when no image is provided.

height

Height of the control, used when no image is provided.

text_color

Used as the text_color parameter in the constructor of the buttons.

disabled_text_color

Analogous to text_color.

over_text_color

Same as above.

down_text_color

Same as above.

on_changed

Action performed when the value of the dropdown is changed. It must be a block with two parameters, which will receive the old and the new value, respectively.

Obs.: This method accepts named parameters, but x, y, font and options are mandatory (also, img and opt_img are mandatory when width and height are not provided, and vice-versa).



1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
# File 'lib/minigl/forms.rb', line 1051

def initialize(x, y = nil, font = nil, img = nil, opt_img = nil, options = nil,
               option = 0, text_margin = 0, width = nil, height = nil,
               text_color = 0, disabled_text_color = 0, over_text_color = 0, down_text_color = 0, &on_changed)
  if x.is_a? Hash
    y = x[:y]
    font = x[:font]
    img = x[:img]
    opt_img = x[:opt_img]
    options = x[:options]
    option = x.fetch(:option, 0)
    text_margin = x.fetch(:text_margin, 0)
    width = x.fetch(:width, nil)
    height = x.fetch(:height, nil)
    text_color = x.fetch(:text_color, 0)
    disabled_text_color = x.fetch(:disabled_text_color, 0)
    over_text_color = x.fetch(:over_text_color, 0)
    down_text_color = x.fetch(:down_text_color, 0)
    x = x[:x]
  end

  super x, y, font, options[option], text_color, disabled_text_color
  @img = img
  @opt_img = opt_img
  @options = options
  @value = @options[option]
  @open = false
  @buttons = []
  @buttons.push(
    Button.new(x, y, font, @value, img, text_color, disabled_text_color, over_text_color, down_text_color,
               false, true, text_margin, 0, width, height) {
                 toggle
               }
  )
  @w = @buttons[0].w
  @h = @buttons[0].h
  @options.each_with_index do |o, i|
    b = Button.new(x, y + (i+1) * @h, font, o, opt_img, text_color, disabled_text_color, over_text_color, down_text_color,
                   false, true, text_margin, 0, @w, @h) {
                     old = @value
                     @value = @buttons[0].text = o
                     @on_changed.call(old, o) if @on_changed
                     toggle
                   }
    b.visible = false
    @buttons.push b
  end
  @max_h = (@options.size + 1) * @h

  @on_changed = on_changed
end

Instance Attribute Details

#optionsObject

An array containing all the options (each of them Strings) that can be selected in the drop-down list.



1022
1023
1024
# File 'lib/minigl/forms.rb', line 1022

def options
  @options
end

#valueObject

The selected value in the drop-down list. This is one of the options.



1018
1019
1020
# File 'lib/minigl/forms.rb', line 1018

def value
  @value
end

Instance Method Details

#draw(alpha = 0xff, z_index = 0) ⇒ Object

Draws the drop-down list.

Parameters:

alpha

(Fixnum) The opacity with which the drop-down list will be drawn. Allowed values vary between 0 (fully transparent) and 255 (fully opaque).

z_index

(Fixnum) The z-order to draw the object. Objects with larger z-orders will be drawn on top of the ones with smaller z-orders.



1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
# File 'lib/minigl/forms.rb', line 1136

def draw(alpha = 0xff, z_index = 0)
  return unless @visible
  unless @img
    bottom = @open ? @y + @max_h + 1 : @y + @h + 1
    b_color = (alpha << 24)
    G.window.draw_quad @x - 1, @y - 1, b_color,
                       @x + @w + 1, @y - 1, b_color,
                       @x + @w + 1, bottom, b_color,
                       @x - 1, bottom, b_color, z_index
    @buttons.each do |b|
      color = (alpha << 24) | (b.state == :over ? 0xcccccc : 0xffffff)
      G.window.draw_quad b.x, b.y, color,
                         b.x + b.w, b.y, color,
                         b.x + b.w, b.y + b.h, color,
                         b.x, b.y + b.h, color, z_index if b.visible
    end
  end
  @buttons.each { |b| b.draw alpha, z_index }
end

#enabled=(value) ⇒ Object

:nodoc:



1122
1123
1124
1125
1126
# File 'lib/minigl/forms.rb', line 1122

def enabled=(value) # :nodoc:
  toggle if @open
  @buttons[0].enabled = value
  @enabled = value
end

#updateObject

Updates the control.



1103
1104
1105
1106
1107
1108
1109
1110
# File 'lib/minigl/forms.rb', line 1103

def update
  return unless @enabled and @visible
  if @open and Mouse.button_pressed? :left and not Mouse.over?(@x, @y, @w, @max_h)
    toggle
    return
  end
  @buttons.each { |b| b.update }
end