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.



34
35
36
37
38
39
# File 'lib/glimr/widgets/label.rb', line 34

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



109
110
111
112
113
114
115
116
# File 'lib/glimr/widgets/label.rb', line 109

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

#default_configObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/glimr/widgets/label.rb', line 23

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



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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/glimr/widgets/label.rb', line 41

def text= t
  @text = t.to_s
  lines = @text.split("\n")
  w,h = 0,0
 if false and MAC_OS_X
  cr = Cairo::Context.new(
    Cairo::ImageSurface.new(Cairo::FORMAT_ARGB32,0,0))
  cr.select_font_face(font, 
    Cairo::FONT_SLANT_NORMAL, 
    Cairo::FONT_WEIGHT_NORMAL)
  cr.font_size = size
  lines.each{|line|
    nw = cr.text_extents(line).x_advance
    w = nw.to_i if nw > w
  }
  h = (lines.size * (size+1)).to_i
  px = "\000"*(w*h*4)
  surf = Cairo::ImageSurface.new(
    px, Cairo::FORMAT_ARGB32, w,h, w*4)
  cr = Cairo::Context.new surf
  cr.set_source_rgba *color
  lines.each_with_index{|line,i|
    cr.save
      cr.show_text(line)
    cr.restore
    cr.rel_move_to(0, size+1)
  }
 else
  sdl_font = SDL::TTF.open(font, size)
  lines.each{|line|
    nw,nh = sdl_font.text_size(line)
    w = nw if nw > w
  }
  h = lines.size * (sdl_font.line_skip+1)
  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})
  px = as_alpha(image.pixels, mask.pixels)
 end

  texture.width = w
  texture.height = h
  texture.mode = TEXTURE_2D
  texture.pixels = px
  @width = texture.width
  @height = texture.height
  @bound = texture.bound
  update_geometry
end