Class: GlimR::Label

Inherits:
Image show all
Defined in:
lib/glimr/widgets/label.rb

Direct Known Subclasses

TextInput

Constant Summary

Constants included from Layoutable

GlimR::Layoutable::DIMENSIONS

Instance Attribute Summary

Attributes inherited from Image

#bound, #image

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

Instance Method Summary collapse

Methods inherited from Image

#create_colors, #create_normals, #create_texcoords, #create_vertices, #load, load, load_theme, #load_theme, #texture=, #update_geometry

Methods inherited from Widget

#activate, #blur, #click, #focus, for, #key_down, #key_up, #lock, #mouse_down, #mouse_move, #mouse_out, #mouse_over, #mouse_up, #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(a, &b) ⇒ Label

Returns a new instance of Label.



31
32
33
34
35
36
# File 'lib/glimr/widgets/label.rb', line 31

def initialize(a,&b)
  txt = a[:text]
  a.delete(:text)
  super(a, &b)
  self.text = txt if txt
end

Dynamic Method Handling

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

Instance Method Details

#as_alpha(pixels, alpha) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/glimr/widgets/label.rb', line 80

def as_alpha(pixels, alpha)
  i = 3
  while i < pixels.size
    pixels[i] = alpha[i-1]
    i += 4
  end
  pixels
end

#default_configObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/glimr/widgets/label.rb', line 20

def default_config
  super.merge(
    :valign => :center,
    :align => :center,
    :font => GlimR.label_font,
    :size => GlimR.label_size,
    :width => 0, :height => 0,
    :color => GlimR.label_color
  )
end

#text=(t) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/glimr/widgets/label.rb', line 38

def text= t
  @text = t.to_s
  sdl_font = SDL::TTF.open(font, size)
  lines = @text.split("\n")
  w,h = 0,0
  lines.each{|line|
    nw,nh = sdl_font.text_size(line)
    w = nw if nw > w
  }
  h = lines.size * (sdl_font.line_skip+1)

  texture.width = w
  texture.height = h
  rmask = 0xff000000
  gmask = 0x00ff0000
  bmask = 0x0000ff00
  amask = 0x000000ff
  mask = SDL::Surface.new(
      SDL::SWSURFACE|SDL::SRCALPHA,
      w, h, 32,
      amask,bmask,gmask,rmask)
  mask.fill_rect(0,0, w,h, [0,0,0,255])
  lines.each_with_index{|line,i|
    sdl_font.draw_blended_utf8(
      mask, line,
      0, i*(sdl_font.line_skip+1),
      255, 255, 255
    )
  }
  image = SDL::Surface.new(
      SDL::SWSURFACE|SDL::SRCALPHA,
      w, h, 32,
      amask,bmask,gmask,rmask)
  image.fill_rect(0,0,w,h, color.map{|i|(i*255).to_i})
  texture.mode = TEXTURE_2D
  texture.pixels = as_alpha(image.pixels, mask.pixels)
  @width = texture.width
  @height = texture.height
  @bound = texture.bound
  update_geometry
end