Class: GlimR::EmptyButton

Inherits:
Widget show all
Defined in:
lib/glimr/widgets/button.rb

Constant Summary

Constants included from Layoutable

Layoutable::DIMENSIONS

Instance Attribute Summary collapse

Attributes inherited from Widget

#focused, #hover

Attributes included from Layoutable

#align, #valign

Attributes inherited from Model

#geometry, #material, #shader, #texture, #transform

Attributes inherited from SceneObject

#children, #drawables, #mtime, #parent, #viewport

Attributes included from EventListener

#event_listeners, #listener_count

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Widget

#activate, #blur, #click, #focus, for, #lock, #mouse_move, #unlock

Methods included from Layoutable

#attach, #children_change, #constant_size?, #detach, #expand!, #expand?, #expand_height, #expand_to_max_height!, #expand_to_max_width!, #expand_width, #fit_height!, #fit_to_children!, #fit_width!, #free_height, #free_width, #full_depth, #full_height, #full_width, #inner_depth, #inner_height, #inner_width, #inspect, #layout, #layoutable_children, #margin, #margin=, #max_height=, #max_width=, #min_height=, #min_width=, #padding, #padding=, #parent=, #size_changing, size_changing_accessor, #tell_children_of_size_change, #x=, #y=

Methods inherited from Model

#absolute_transform, #apply, #inspect, #pop_state, #push_state

Methods inherited from SceneObject

#<<, #absolute_geometry, #absolute_material, #absolute_shader, #absolute_texture, #absolute_transform, #absolute_transform_for_drawing, #add_drawables, #apply, #attach, #clone, #detach, #detach_self, #inspect, #pop_state, #push_state, #remove_drawables, #render, #replace_node, #root, #touch!, #visible

Methods included from EventListener

#add_event_listener, #decrement_listener_count, #dispatch_event, #event_root, #increment_listener_count, #method_missing, #multicast_event, #process_event, #remove_event_listener

Constructor Details

#initialize(*args, &block) ⇒ EmptyButton

Returns a new instance of EmptyButton.



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/glimr/widgets/button.rb', line 38

def initialize(*args, &block)
  @image_cache = {}
  super(*args, &nil)
  self.background = @up_background
  @min_width = @background_model.width
  @min_height = @background_model.height
  self.label = @label
  self.background = @up_background
  self.cover = @up_cover
  self.hover_indicator = @up_hover_indicator
  self.focus_indicator = @up_focus_indicator
  on_activate(&block) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class GlimR::EventListener

Instance Attribute Details

#backgroundObject

Returns the value of attribute background.



34
35
36
# File 'lib/glimr/widgets/button.rb', line 34

def background
  @background
end

#coverObject

Returns the value of attribute cover.



34
35
36
# File 'lib/glimr/widgets/button.rb', line 34

def cover
  @cover
end

#downObject (readonly)

Returns the value of attribute down.



36
37
38
# File 'lib/glimr/widgets/button.rb', line 36

def down
  @down
end

#down_backgroundObject

Returns the value of attribute down_background.



35
36
37
# File 'lib/glimr/widgets/button.rb', line 35

def down_background
  @down_background
end

#down_coverObject

Returns the value of attribute down_cover.



35
36
37
# File 'lib/glimr/widgets/button.rb', line 35

def down_cover
  @down_cover
end

#hover_indicatorObject

Returns the value of attribute hover_indicator.



34
35
36
# File 'lib/glimr/widgets/button.rb', line 34

def hover_indicator
  @hover_indicator
end

#labelObject

Returns the value of attribute label.



34
35
36
# File 'lib/glimr/widgets/button.rb', line 34

def label
  @label
end

#up_backgroundObject

Returns the value of attribute up_background.



35
36
37
# File 'lib/glimr/widgets/button.rb', line 35

def up_background
  @up_background
end

#up_coverObject

Returns the value of attribute up_cover.



35
36
37
# File 'lib/glimr/widgets/button.rb', line 35

def up_cover
  @up_cover
end

Class Method Details

.load_image(img_name) ⇒ Object



11
12
13
# File 'lib/glimr/widgets/button.rb', line 11

def self.load_image(img_name)
  @@image_cache[img_name] ||= Texture.load(GlimR.theme + img_name)
end

Instance Method Details

#button_downObject



119
120
121
122
123
124
125
126
# File 'lib/glimr/widgets/button.rb', line 119

def button_down
  self.background = @down_background
  self.cover = @down_cover
  self.hover_indicator = @down_hover_indicator
  self.focus_indicator = @down_focus_indicator
  self.label_model = @down_label_model
  dispatch_event Event.new(:down)
end

#button_upObject



128
129
130
131
132
133
134
135
# File 'lib/glimr/widgets/button.rb', line 128

def button_up
  self.background = @up_background
  self.cover = @up_cover
  self.hover_indicator = @up_hover_indicator
  self.focus_indicator = @up_focus_indicator
  self.label_model = @up_label_model
  dispatch_event Event.new(:up)
end

#default_configObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/glimr/widgets/button.rb', line 17

def default_config
  super.merge(
    :fit_children => false,
    :align => :center,
    :valign => :middle,
    :label_down_offset => [0,2],
    :up_background => nil,
    :down_background => nil,
    :up_cover => nil,
    :down_cover => nil,
    :up_hover_indicator => nil,
    :down_hover_indicator => nil,
    :up_focus_indicator => nil,
    :down_focus_indicator => nil
  )
end

#focus_indicator=(new_focus_indicator) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/glimr/widgets/button.rb', line 213

def focus_indicator= new_focus_indicator
  return if @focus_indicator == new_focus_indicator
  @focus_indicator_model.detach_self if @focus_indicator_model
  @focus_indicator_model =  case new_focus_indicator
                      when Layoutable
                        new_focus_indicator
                      when String, Pathname
                        load_image(new_focus_indicator, 3)
                      when nil
                        new_focus_indicator
                      else
                        raise "Bad focus indicator"
                      end
  if @focus_indicator_model
    @focus_indicator_model.visible = focused
    attach @focus_indicator_model
  end
end

#focused=(f) ⇒ Object



137
138
139
140
# File 'lib/glimr/widgets/button.rb', line 137

def focused= f
  super
  @focus_indicator_model.visible = f if @focus_indicator_model
end

#key_down(o, e) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/glimr/widgets/button.rb', line 81

def key_down(o,e)
  super
  if @focused
    case e.key
    when 13
      @down = true
      button_down
    end
  end
end

#key_up(o, e) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/glimr/widgets/button.rb', line 92

def key_up(o,e)
  super
  if @focused and @down
    case e.key
    when 13
      dispatch_event(Event.new(:activate))
      @down = false
      button_up
    end
  elsif @down and (e.key == 13)
    @down = false
    button_up
  end
end

#label_model=(new_label_model) ⇒ Object



170
171
172
173
174
175
# File 'lib/glimr/widgets/button.rb', line 170

def label_model= new_label_model
  return if new_label_model == @label_model
  @label_model.detach_self if @label_model
  @label_model = new_label_model
  attach @label_model if @label_model
end

#load_image(img_name, z) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/glimr/widgets/button.rb', line 52

def load_image(img_name, z)
  i = @image_cache[[img_name,z]] ||= (
    tex = self.class.load_image(img_name)
    img = StretchableImage.new
    img.texture = tex
    img.z = z
    img
  )
  i.set_dimensions(@width, @height) unless i.width == @width and i.height == @height
  i
end

#mouse_down(o, e) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/glimr/widgets/button.rb', line 64

def mouse_down(o,e)
  super
  if not @down and e.button == :left
    @down = true
    button_down
  end
end

#mouse_out(o, e) ⇒ Object



107
108
109
110
111
# File 'lib/glimr/widgets/button.rb', line 107

def mouse_out(o,e)
  super
  @hover_indicator_model.visible = false if @hover_indicator_model
  button_up if @down
end

#mouse_over(o, e) ⇒ Object



113
114
115
116
117
# File 'lib/glimr/widgets/button.rb', line 113

def mouse_over(o,e)
  super
  @hover_indicator_model.visible = true if @hover_indicator_model
  button_down if @down
end

#mouse_up(o, e) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/glimr/widgets/button.rb', line 72

def mouse_up(o,e)
  super
  if @down and e.button == :left
    dispatch_event(Event.new(:activate)) if @hover
    @down = false
    button_up
  end
end