Permalink
Browse files

Implemented text justification for labels (and all derived elements, …

…of course).
  • Loading branch information...
1 parent afa3e4c commit a0049f7b769909982141a035f3f0526945209677 @Spooner Spooner committed Mar 25, 2011
Showing with 29 additions and 1 deletion.
  1. +1 −0 config/default_schema.yml
  2. +6 −0 examples/label_example.rb
  3. +22 −1 lib/fidgit/elements/label.rb
@@ -101,6 +101,7 @@
:height: ?scroll_bar_thickness
:Label: # < Element
+ :justify: :left
:disabled:
:color: ?light_gray
@@ -11,6 +11,12 @@ def initialize
label '', icon: Gosu::Image["head_icon.png"], tip: 'A label with just icon'
label '', background_color: Gosu::Color.rgb(0, 255, 0), tip: 'No text or icon'
end
+
+ pack :vertical do
+ label ":left justification", width: 400, background_color: Gosu::Color.rgb(0, 100, 0), justify: :left, tip: 'A label with text'
+ label ":right justification", width: 400, background_color: Gosu::Color.rgb(0, 100, 0), justify: :right, tip: 'A label with text'
+ label ":center justification", width: 400, background_color: Gosu::Color.rgb(0, 100, 0), justify: :center, tip: 'A label with text'
+ end
end
end
@@ -5,6 +5,8 @@ class Label < Element
attr_accessor :color, :background_color, :border_color
attr_reader :text, :icon
+ VALID_JUSTIFICATION = [:left, :right, :center]
+
def text=(value)
@text = value
recalc
@@ -22,9 +24,11 @@ def icon=(value)
#
# @option (see Element#initialize)
# @option options [Fidgit::Thumbnail, Gosu::Image, nil] :icon (nil)
+ # @option options [:left, :right, :center] :justify (:left) Text justification.
def initialize(text, options = {})
options = {
color: default(:color),
+ justify: default(:justify),
background_color: default(:background_color),
border_color: default(:border_color),
}.merge! options
@@ -33,6 +37,9 @@ def initialize(text, options = {})
@icon = options[:icon]
@color = options[:color].dup
+ raise "Justification must be one of #{VALID_JUSTIFICATION.inspect}" unless VALID_JUSTIFICATION.include? options[:justify]
+ @justify = options[:justify]
+
super(options)
end
@@ -44,7 +51,21 @@ def draw_foreground
end
unless @text.empty?
- font.draw(@text, current_x, y + padding_top, z, 1, 1, @color)
+ case @justify
+ when :left
+ rel_x = 0.0
+ center_x = current_x
+
+ when :right
+ rel_x = 1.0
+ center_x = x + rect.width - padding_right
+
+ when :center
+ rel_x = 0.5
+ center_x = (current_x + x + rect.width - padding_right) / 2.0
+ end
+
+ font.draw_rel(@text, center_x, y + padding_top, z, rel_x, 0, 1, 1, @color)
end
nil

0 comments on commit a0049f7

Please sign in to comment.