minigl 2.2.2 → 2.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,188 +1,190 @@
1
- module MiniGL
2
- # This class provides methods for easily drawing one or multiple lines of
3
- # text, with control over the text alignment and coloring.
4
- class TextHelper
5
- # Creates a TextHelper.
6
- #
7
- # Parameters:
8
- # [font] A <code>Gosu::Font</code> that will be used to draw the text.
9
- # [line_spacing] When drawing multiple lines, the distance, in pixels,
10
- # between each line.
11
- def initialize(font, line_spacing = 0)
12
- @font = font
13
- @line_spacing = line_spacing
14
- end
15
-
16
- # Draws a single line of text.
17
- #
18
- # Parameters:
19
- # [text] The text to be drawn. No line breaks are allowed.
20
- # [x] The horizontal reference for drawing the text. If +mode+ is +:left+,
21
- # all text will be drawn from this point to the right; if +mode+ is
22
- # +:right+, all text will be drawn from this point to the left; and if
23
- # +mode+ is +:center+, the text will be equally distributed to the
24
- # left and to the right of this point.
25
- # [y] The vertical reference for drawing the text. All text will be drawn
26
- # from this point down.
27
- # [mode] The alignment of the text. Valid values are +:left+, +:right+ and
28
- # +:center+.
29
- # [color] The color of the text, in hexadecimal RRGGBB format.
30
- # [alpha] The opacity of the text. Valid values vary from 0 (fully
31
- # transparent) to 255 (fully opaque).
32
- # [effect] Effect to add to the text. It can be either +nil+, for no effect,
33
- # +:border+ for bordered text, or +:shadow+ for shadowed text (the
34
- # shadow is always placed below and to the right of the text).
35
- # [effect_color] Color of the effect, if any.
36
- # [effect_size] Size of the effect, if any. In the case of +:border+, this
37
- # will be the width of the border (the border will only look
38
- # good when +effect_size+ is relatively small, compared to the
39
- # size of the font); in the case of +:shadow+, it will be the
40
- # distance between the text and the shadow.
41
- # [effect_alpha] Opacity of the effect, if any. For shadows, it is usual to
42
- # provide less than 255.
43
- # [z_index] The z-order to draw the object. Objects with larger z-orders
44
- # will be drawn on top of the ones with smaller z-orders.
45
- #
46
- # *Obs.:* This method accepts named parameters, but +text+, +x+ and +y+ are
47
- # mandatory.
48
- def write_line(text, x = nil, y = nil, mode = :left, color = 0, alpha = 0xff,
49
- effect = nil, effect_color = 0, effect_size = 1, effect_alpha = 0xff,
50
- z_index = 0)
51
- if text.is_a? Hash
52
- x = text[:x]
53
- y = text[:y]
54
- mode = text.fetch(:mode, :left)
55
- color = text.fetch(:color, 0)
56
- alpha = text.fetch(:alpha, 0xff)
57
- effect = text.fetch(:effect, nil)
58
- effect_color = text.fetch(:effect_color, 0)
59
- effect_size = text.fetch(:effect_size, 1)
60
- effect_alpha = text.fetch(:effect_alpha, 0xff)
61
- z_index = text.fetch(:z_index, 0)
62
- text = text[:text]
63
- end
64
-
65
- color = (alpha << 24) | color
66
- rel =
67
- case mode
68
- when :left then 0
69
- when :center then 0.5
70
- when :right then 1
71
- else 0
72
- end
73
- if effect
74
- effect_color = (effect_alpha << 24) | effect_color
75
- if effect == :border
76
- @font.draw_rel text, x - effect_size, y - effect_size, z_index, rel, 0, 1, 1, effect_color
77
- @font.draw_rel text, x, y - effect_size, z_index, rel, 0, 1, 1, effect_color
78
- @font.draw_rel text, x + effect_size, y - effect_size, z_index, rel, 0, 1, 1, effect_color
79
- @font.draw_rel text, x + effect_size, y, z_index, rel, 0, 1, 1, effect_color
80
- @font.draw_rel text, x + effect_size, y + effect_size, z_index, rel, 0, 1, 1, effect_color
81
- @font.draw_rel text, x, y + effect_size, z_index, rel, 0, 1, 1, effect_color
82
- @font.draw_rel text, x - effect_size, y + effect_size, z_index, rel, 0, 1, 1, effect_color
83
- @font.draw_rel text, x - effect_size, y, z_index, rel, 0, 1, 1, effect_color
84
- elsif effect == :shadow
85
- @font.draw_rel text, x + effect_size, y + effect_size, z_index, rel, 0, 1, 1, effect_color
86
- end
87
- end
88
- @font.draw_rel text, x, y, z_index, rel, 0, 1, 1, color
89
- end
90
-
91
- # Draws text, breaking lines when needed and when explicitly caused by the
92
- # "\n" character.
93
- #
94
- # Parameters:
95
- # [text] The text to be drawn. Line breaks are allowed.
96
- # [x] The horizontal reference for drawing the text. Works like in
97
- # +write_line+ for the +:left+, +:right+ and +:center+ modes. For the
98
- # +:justified+ mode, works the same as for +:left+.
99
- # [y] The vertical reference for drawing the text. All text will be drawn
100
- # from this point down.
101
- # [width] The maximum width for the lines of text. Line is broken when
102
- # this width is exceeded.
103
- # [mode] The alignment of the text. Valid values are +:left+, +:right+,
104
- # +:center+ and +:justified+.
105
- # [color] The color of the text, in hexadecimal RRGGBB format.
106
- # [alpha] The opacity of the text. Valid values vary from 0 (fully
107
- # transparent) to 255 (fully opaque).
108
- # [z_index] The z-order to draw the object. Objects with larger z-orders
109
- # will be drawn on top of the ones with smaller z-orders.
110
- def write_breaking(text, x, y, width, mode = :left, color = 0, alpha = 0xff, z_index = 0)
111
- color = (alpha << 24) | color
112
- text.split("\n").each do |p|
113
- if mode == :justified
114
- y = write_paragraph_justified p, x, y, width, color, z_index
115
- else
116
- rel =
117
- case mode
118
- when :left then 0
119
- when :center then 0.5
120
- when :right then 1
121
- else 0
122
- end
123
- y = write_paragraph p, x, y, width, rel, color, z_index
124
- end
125
- end
126
- end
127
-
128
- private
129
-
130
- def write_paragraph(text, x, y, width, rel, color, z_index)
131
- line = ''
132
- line_width = 0
133
- text.split(' ').each do |word|
134
- w = @font.text_width word
135
- if line_width + w > width
136
- @font.draw_rel line.chop, x, y, z_index, rel, 0, 1, 1, color
137
- line = ''
138
- line_width = 0
139
- y += @font.height + @line_spacing
140
- end
141
- line += "#{word} "
142
- line_width += @font.text_width "#{word} "
143
- end
144
- @font.draw_rel line.chop, x, y, z_index, rel, 0, 1, 1, color unless line.empty?
145
- y + @font.height + @line_spacing
146
- end
147
-
148
- def write_paragraph_justified(text, x, y, width, color, z_index)
149
- space_width = @font.text_width ' '
150
- spaces = [[]]
151
- line_index = 0
152
- new_x = x
153
- words = text.split(' ')
154
- words.each do |word|
155
- w = @font.text_width word
156
- if new_x + w > x + width
157
- space = x + width - new_x + space_width
158
- index = 0
159
- while space > 0
160
- spaces[line_index][index] += 1
161
- space -= 1
162
- index += 1
163
- index = 0 if index == spaces[line_index].size - 1
164
- end
165
-
166
- spaces << []
167
- line_index += 1
168
-
169
- new_x = x
170
- end
171
- new_x += @font.text_width(word) + space_width
172
- spaces[line_index] << space_width
173
- end
174
-
175
- index = 0
176
- spaces.each do |line|
177
- new_x = x
178
- line.each do |s|
179
- @font.draw words[index], new_x, y, z_index, 1, 1, color
180
- new_x += @font.text_width(words[index]) + s
181
- index += 1
182
- end
183
- y += @font.height + @line_spacing
184
- end
185
- y
186
- end
187
- end
188
- end
1
+ module MiniGL
2
+ # This class provides methods for easily drawing one or multiple lines of
3
+ # text, with control over the text alignment and coloring.
4
+ class TextHelper
5
+ # Creates a TextHelper.
6
+ #
7
+ # Parameters:
8
+ # [font] A <code>Gosu::Font</code> that will be used to draw the text.
9
+ # [line_spacing] When drawing multiple lines, the distance, in pixels,
10
+ # between each line.
11
+ def initialize(font, line_spacing = 0)
12
+ @font = font
13
+ @line_spacing = line_spacing
14
+ end
15
+
16
+ # Draws a single line of text.
17
+ #
18
+ # Parameters:
19
+ # [text] The text to be drawn. No line breaks are allowed. You can use the
20
+ # `<b>` tag for bold, `<i>` for italic and `<c=rrggbb>` for colors.
21
+ # [x] The horizontal reference for drawing the text. If +mode+ is +:left+,
22
+ # all text will be drawn from this point to the right; if +mode+ is
23
+ # +:right+, all text will be drawn from this point to the left; and if
24
+ # +mode+ is +:center+, the text will be equally distributed to the
25
+ # left and to the right of this point.
26
+ # [y] The vertical reference for drawing the text. All text will be drawn
27
+ # from this point down.
28
+ # [mode] The alignment of the text. Valid values are +:left+, +:right+ and
29
+ # +:center+.
30
+ # [color] The color of the text, in hexadecimal RRGGBB format.
31
+ # [alpha] The opacity of the text. Valid values vary from 0 (fully
32
+ # transparent) to 255 (fully opaque).
33
+ # [effect] Effect to add to the text. It can be either +nil+, for no effect,
34
+ # +:border+ for bordered text, or +:shadow+ for shadowed text (the
35
+ # shadow is always placed below and to the right of the text).
36
+ # [effect_color] Color of the effect, if any.
37
+ # [effect_size] Size of the effect, if any. In the case of +:border+, this
38
+ # will be the width of the border (the border will only look
39
+ # good when +effect_size+ is relatively small, compared to the
40
+ # size of the font); in the case of +:shadow+, it will be the
41
+ # distance between the text and the shadow.
42
+ # [effect_alpha] Opacity of the effect, if any. For shadows, it is usual to
43
+ # provide less than 255.
44
+ # [z_index] The z-order to draw the object. Objects with larger z-orders
45
+ # will be drawn on top of the ones with smaller z-orders.
46
+ #
47
+ # *Obs.:* This method accepts named parameters, but +text+, +x+ and +y+ are
48
+ # mandatory.
49
+ def write_line(text, x = nil, y = nil, mode = :left, color = 0, alpha = 0xff,
50
+ effect = nil, effect_color = 0, effect_size = 1, effect_alpha = 0xff,
51
+ z_index = 0)
52
+ if text.is_a? Hash
53
+ x = text[:x]
54
+ y = text[:y]
55
+ mode = text.fetch(:mode, :left)
56
+ color = text.fetch(:color, 0)
57
+ alpha = text.fetch(:alpha, 0xff)
58
+ effect = text.fetch(:effect, nil)
59
+ effect_color = text.fetch(:effect_color, 0)
60
+ effect_size = text.fetch(:effect_size, 1)
61
+ effect_alpha = text.fetch(:effect_alpha, 0xff)
62
+ z_index = text.fetch(:z_index, 0)
63
+ text = text[:text]
64
+ end
65
+
66
+ color = (alpha << 24) | color
67
+ rel =
68
+ case mode
69
+ when :left then 0
70
+ when :center then 0.5
71
+ when :right then 1
72
+ else 0
73
+ end
74
+ if effect
75
+ effect_color = (effect_alpha << 24) | effect_color
76
+ if effect == :border
77
+ @font.draw_markup_rel text, x - effect_size, y - effect_size, z_index, rel, 0, 1, 1, effect_color
78
+ @font.draw_markup_rel text, x, y - effect_size, z_index, rel, 0, 1, 1, effect_color
79
+ @font.draw_markup_rel text, x + effect_size, y - effect_size, z_index, rel, 0, 1, 1, effect_color
80
+ @font.draw_markup_rel text, x + effect_size, y, z_index, rel, 0, 1, 1, effect_color
81
+ @font.draw_markup_rel text, x + effect_size, y + effect_size, z_index, rel, 0, 1, 1, effect_color
82
+ @font.draw_markup_rel text, x, y + effect_size, z_index, rel, 0, 1, 1, effect_color
83
+ @font.draw_markup_rel text, x - effect_size, y + effect_size, z_index, rel, 0, 1, 1, effect_color
84
+ @font.draw_markup_rel text, x - effect_size, y, z_index, rel, 0, 1, 1, effect_color
85
+ elsif effect == :shadow
86
+ @font.draw_markup_rel text, x + effect_size, y + effect_size, z_index, rel, 0, 1, 1, effect_color
87
+ end
88
+ end
89
+ @font.draw_markup_rel text, x, y, z_index, rel, 0, 1, 1, color
90
+ end
91
+
92
+ # Draws text, breaking lines when needed and when explicitly caused by the
93
+ # "\n" character.
94
+ #
95
+ # Parameters:
96
+ # [text] The text to be drawn. Line breaks are allowed. You can use the
97
+ # `<b>` tag for bold, `<i>` for italic and `<c=rrggbb>` for colors.
98
+ # [x] The horizontal reference for drawing the text. Works like in
99
+ # +write_line+ for the +:left+, +:right+ and +:center+ modes. For the
100
+ # +:justified+ mode, works the same as for +:left+.
101
+ # [y] The vertical reference for drawing the text. All text will be drawn
102
+ # from this point down.
103
+ # [width] The maximum width for the lines of text. Line is broken when
104
+ # this width is exceeded.
105
+ # [mode] The alignment of the text. Valid values are +:left+, +:right+,
106
+ # +:center+ and +:justified+.
107
+ # [color] The color of the text, in hexadecimal RRGGBB format.
108
+ # [alpha] The opacity of the text. Valid values vary from 0 (fully
109
+ # transparent) to 255 (fully opaque).
110
+ # [z_index] The z-order to draw the object. Objects with larger z-orders
111
+ # will be drawn on top of the ones with smaller z-orders.
112
+ def write_breaking(text, x, y, width, mode = :left, color = 0, alpha = 0xff, z_index = 0)
113
+ color = (alpha << 24) | color
114
+ text.split("\n").each do |p|
115
+ if mode == :justified
116
+ y = write_paragraph_justified p, x, y, width, color, z_index
117
+ else
118
+ rel =
119
+ case mode
120
+ when :left then 0
121
+ when :center then 0.5
122
+ when :right then 1
123
+ else 0
124
+ end
125
+ y = write_paragraph p, x, y, width, rel, color, z_index
126
+ end
127
+ end
128
+ end
129
+
130
+ private
131
+
132
+ def write_paragraph(text, x, y, width, rel, color, z_index)
133
+ line = ''
134
+ line_width = 0
135
+ text.split(' ').each do |word|
136
+ w = @font.markup_width word
137
+ if line_width + w > width
138
+ @font.draw_markup_rel line.chop, x, y, z_index, rel, 0, 1, 1, color
139
+ line = ''
140
+ line_width = 0
141
+ y += @font.height + @line_spacing
142
+ end
143
+ line += "#{word} "
144
+ line_width += @font.markup_width "#{word} "
145
+ end
146
+ @font.draw_markup_rel line.chop, x, y, z_index, rel, 0, 1, 1, color unless line.empty?
147
+ y + @font.height + @line_spacing
148
+ end
149
+
150
+ def write_paragraph_justified(text, x, y, width, color, z_index)
151
+ space_width = @font.text_width ' '
152
+ spaces = [[]]
153
+ line_index = 0
154
+ new_x = x
155
+ words = text.split(' ')
156
+ words.each do |word|
157
+ w = @font.markup_width word
158
+ if new_x + w > x + width
159
+ space = x + width - new_x + space_width
160
+ index = 0
161
+ while space > 0
162
+ spaces[line_index][index] += 1
163
+ space -= 1
164
+ index += 1
165
+ index = 0 if index == spaces[line_index].size - 1
166
+ end
167
+
168
+ spaces << []
169
+ line_index += 1
170
+
171
+ new_x = x
172
+ end
173
+ new_x += @font.markup_width(word) + space_width
174
+ spaces[line_index] << space_width
175
+ end
176
+
177
+ index = 0
178
+ spaces.each do |line|
179
+ new_x = x
180
+ line.each do |s|
181
+ @font.draw_markup words[index], new_x, y, z_index, 1, 1, color
182
+ new_x += @font.markup_width(words[index]) + s
183
+ index += 1
184
+ end
185
+ y += @font.height + @line_spacing
186
+ end
187
+ y
188
+ end
189
+ end
190
+ end
@@ -1,73 +1,73 @@
1
- <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2
- <!-- Created with Inkscape (http://www.inkscape.org/) -->
3
-
4
- <svg
5
- xmlns:dc="http://purl.org/dc/elements/1.1/"
6
- xmlns:cc="http://creativecommons.org/ns#"
7
- xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
8
- xmlns:svg="http://www.w3.org/2000/svg"
9
- xmlns="http://www.w3.org/2000/svg"
10
- xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
11
- xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
12
- width="204"
13
- height="24"
14
- id="svg2"
15
- version="1.1"
16
- inkscape:version="0.48.4 r9939"
17
- sodipodi:docname="barbg.svg"
18
- inkscape:export-filename="/home/victor/Projects/TCC/minigl/test/data/img/barbg.png"
19
- inkscape:export-xdpi="90"
20
- inkscape:export-ydpi="90">
21
- <defs
22
- id="defs4" />
23
- <sodipodi:namedview
24
- id="base"
25
- pagecolor="#ffffff"
26
- bordercolor="#666666"
27
- borderopacity="1.0"
28
- inkscape:pageopacity="0.0"
29
- inkscape:pageshadow="2"
30
- inkscape:zoom="4"
31
- inkscape:cx="127.40358"
32
- inkscape:cy="-8.2431265"
33
- inkscape:document-units="px"
34
- inkscape:current-layer="layer1"
35
- showgrid="false"
36
- inkscape:window-width="1364"
37
- inkscape:window-height="718"
38
- inkscape:window-x="0"
39
- inkscape:window-y="0"
40
- inkscape:window-maximized="1" />
41
- <metadata
42
- id="metadata7">
43
- <rdf:RDF>
44
- <cc:Work
45
- rdf:about="">
46
- <dc:format>image/svg+xml</dc:format>
47
- <dc:type
48
- rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
49
- <dc:title></dc:title>
50
- </cc:Work>
51
- </rdf:RDF>
52
- </metadata>
53
- <g
54
- inkscape:label="Layer 1"
55
- inkscape:groupmode="layer"
56
- id="layer1"
57
- transform="translate(0,-1028.3622)">
58
- <rect
59
- style="fill:#ffffff;fill-opacity:1;stroke:none"
60
- id="rect3753"
61
- width="204"
62
- height="24"
63
- x="0"
64
- y="1028.3622" />
65
- <rect
66
- style="fill:#ff0000;fill-opacity:1;stroke:none"
67
- id="rect3755"
68
- width="200"
69
- height="20"
70
- x="2"
71
- y="1030.3622" />
72
- </g>
73
- </svg>
1
+ <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2
+ <!-- Created with Inkscape (http://www.inkscape.org/) -->
3
+
4
+ <svg
5
+ xmlns:dc="http://purl.org/dc/elements/1.1/"
6
+ xmlns:cc="http://creativecommons.org/ns#"
7
+ xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
8
+ xmlns:svg="http://www.w3.org/2000/svg"
9
+ xmlns="http://www.w3.org/2000/svg"
10
+ xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
11
+ xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
12
+ width="204"
13
+ height="24"
14
+ id="svg2"
15
+ version="1.1"
16
+ inkscape:version="0.48.4 r9939"
17
+ sodipodi:docname="barbg.svg"
18
+ inkscape:export-filename="/home/victor/Projects/TCC/minigl/test/data/img/barbg.png"
19
+ inkscape:export-xdpi="90"
20
+ inkscape:export-ydpi="90">
21
+ <defs
22
+ id="defs4" />
23
+ <sodipodi:namedview
24
+ id="base"
25
+ pagecolor="#ffffff"
26
+ bordercolor="#666666"
27
+ borderopacity="1.0"
28
+ inkscape:pageopacity="0.0"
29
+ inkscape:pageshadow="2"
30
+ inkscape:zoom="4"
31
+ inkscape:cx="127.40358"
32
+ inkscape:cy="-8.2431265"
33
+ inkscape:document-units="px"
34
+ inkscape:current-layer="layer1"
35
+ showgrid="false"
36
+ inkscape:window-width="1364"
37
+ inkscape:window-height="718"
38
+ inkscape:window-x="0"
39
+ inkscape:window-y="0"
40
+ inkscape:window-maximized="1" />
41
+ <metadata
42
+ id="metadata7">
43
+ <rdf:RDF>
44
+ <cc:Work
45
+ rdf:about="">
46
+ <dc:format>image/svg+xml</dc:format>
47
+ <dc:type
48
+ rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
49
+ <dc:title></dc:title>
50
+ </cc:Work>
51
+ </rdf:RDF>
52
+ </metadata>
53
+ <g
54
+ inkscape:label="Layer 1"
55
+ inkscape:groupmode="layer"
56
+ id="layer1"
57
+ transform="translate(0,-1028.3622)">
58
+ <rect
59
+ style="fill:#ffffff;fill-opacity:1;stroke:none"
60
+ id="rect3753"
61
+ width="204"
62
+ height="24"
63
+ x="0"
64
+ y="1028.3622" />
65
+ <rect
66
+ style="fill:#ff0000;fill-opacity:1;stroke:none"
67
+ id="rect3755"
68
+ width="200"
69
+ height="20"
70
+ x="2"
71
+ y="1030.3622" />
72
+ </g>
73
+ </svg>