minigl 2.2.9 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +3 -2
  3. data/lib/minigl/text.rb +137 -32
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 79bd9440a172b26f147083ef8bd486af9adcaded814bf84473535c24704f2ded
4
- data.tar.gz: 667b535e408b7d3302e45c4b3d02fe5fa20d85c4e31b8af68eaa4f1023851a68
3
+ metadata.gz: e0362df674b485b64cd44d6b167377b2d46d6c33fd9b2d65428fe75e8de9e027
4
+ data.tar.gz: d91ae820bfc76cb9895f43227646028b23927cffe2e4b226f3b2b14779fe08a3
5
5
  SHA512:
6
- metadata.gz: 6a26a33090716a53212cf084158ed623cb9d54e28407a990e8238f2784ca679a08d42908a03c23266836e830943afde4f5176f4d8946ba7302530f895d5b347a
7
- data.tar.gz: e60f740c8eec1033d01adb79af11d2960e709bd5a0df1165949622a6ba43ce49e9dc8fcb8d7f6c0ba93bd550fad1382dbf2ed73187038162561489b01f206399
6
+ metadata.gz: 70af1a521abd0f23110b62b2c88a1de5f1fd94aeb664fc8c9e4ca8cd414a024c0ac93037889b9b8431598c7e63cf11e62b00f657213f5eb68957da70ca4fdfc5
7
+ data.tar.gz: 685115af1a42dc3662af276580771b0be33eae0348fd54e1f11f679c2de26a10f8d158a9d00cf0e67ae42ae5bab2af4841adbf466aca38b46b6e75665ca3b773
data/README.md CHANGED
@@ -29,9 +29,10 @@ After installing the Gosu dependencies, you can just `gem install minigl`.
29
29
  * The [wiki](https://github.com/victords/minigl/wiki) is a work in progress with tutorials and examples.
30
30
  * Test package and examples aren't complete!
31
31
 
32
- ## Version 2.2.8
32
+ ## Version 2.3.0
33
33
 
34
- * Add `stop_time` parameter to the `cycle` method of `Movement` (included in `GameObject`).
34
+ * Added the `ImageFont` class.
35
+ * Added scale and line spacing options to `TextHelper` methods.
35
36
 
36
37
  ## Contributing
37
38
 
@@ -1,4 +1,100 @@
1
1
  module MiniGL
2
+ # This class represents a font and exposes most of the methods from +Gosu::Font+,
3
+ # but allows the font to be created from an image, allowing for better customization
4
+ # and also using the +retro+ option.
5
+ #
6
+ # The image used to load the font must meet these criteria:
7
+ # * The characters should be laid out in lines of the same height in pixels.
8
+ # * The full image must have a height that is a multiple of that line height.
9
+ # * The characters should occupy the maximum available space in each line, i.e.,
10
+ # if a character fits in the current line it must not be placed in the next
11
+ # one. In the last line there can be any amount of free space at the end.
12
+ class ImageFont
13
+ # The height of this font in pixels.
14
+ attr_reader :height
15
+
16
+ # Creates an +ImageFont+.
17
+ #
18
+ # Parameters:
19
+ # [img_path] Identifier of an image fitting the description in the class documentation,
20
+ # as used in +Res.img+.
21
+ # [chars] A string containing all characters that will be present in the image, in the
22
+ # same order as they appear in the image. Do not include white space.
23
+ # [widths] An integer representing the width of the chars in pixels, if this is a fixed
24
+ # width font, or an array containing the width of each char, in the same order
25
+ # as they appear in the +chars+ string.
26
+ # [height] The height of the lines in the image (see description above).
27
+ # [space_width] The width of the white space character in this font.
28
+ # [global] Parameter that will be passed to +Res.img+ when loading the image.
29
+ # [ext] Parameter that will be passed to +Res.img+ when loading the image.
30
+ # [retro] Parameter that will be passed to +Res.img+ when loading the image.
31
+ def initialize(img_path, chars, widths, height, space_width, global = true, ext = '.png', retro = nil)
32
+ retro = Res.retro_images if retro.nil?
33
+ img = Res.img(img_path, global, false, ext, retro)
34
+ @chars = chars
35
+ @images = []
36
+ @height = height
37
+ @space_width = space_width
38
+ wa = widths.is_a?(Array)
39
+ if wa && widths.length != chars.length
40
+ raise 'Wrong widths array size!'
41
+ end
42
+ x = y = 0
43
+ (0...chars.length).each do |i|
44
+ @images.push(img.subimage(x, y, wa ? widths[i] : widths, height))
45
+ new_x = x + (wa ? widths[i] : widths)
46
+ if i < chars.length - 1 && new_x + (wa ? widths[i+1] : widths) > img.width
47
+ x = 0
48
+ y += height
49
+ else
50
+ x = new_x
51
+ end
52
+ end
53
+ end
54
+
55
+ # Returns the width, in pixels, of a given string written by this font.
56
+ #
57
+ # Parameters:
58
+ # [text] The string to be measured
59
+ def markup_width(text)
60
+ text.chars.reduce(0) { |w, c| if c == ' '; w += @space_width; else; i = @chars.index(c); w += i ? @images[i].width : 0; end }
61
+ end
62
+
63
+ # See <code>Gosu::Font#draw_markup_rel</code> for details.
64
+ def draw_markup_rel(text, x, y, z, rel_x, rel_y, scale_x, scale_y, color)
65
+ text = text.to_s unless text.is_a?(String)
66
+ if rel_x == 0.5
67
+ x -= scale_x * markup_width(text) / 2
68
+ elsif rel_x == 1
69
+ x -= scale_x * markup_width(text)
70
+ end
71
+ if rel_y == 0.5
72
+ y -= scale_y * @height / 2
73
+ elsif rel_y == 1
74
+ y -= scale_x * @height
75
+ end
76
+ text.each_char do |c|
77
+ if c == ' '
78
+ x += scale_x * @space_width
79
+ next
80
+ end
81
+ i = @chars.index(c)
82
+ next if i.nil?
83
+ @images[i].draw(x, y, z, scale_x, scale_y, color)
84
+ x += scale_x * @images[i].width
85
+ end
86
+ end
87
+
88
+ # See <code>Gosu::Font#draw_markup</code> for details.
89
+ def draw_markup(text, x, y, z, scale_x, scale_y, color)
90
+ draw_markup_rel(text, x, y, z, 0, 0, scale_x, scale_y, color)
91
+ end
92
+
93
+ alias :draw_text_rel :draw_markup_rel
94
+ alias :draw_text :draw_markup
95
+ alias :text_width :markup_width
96
+ end
97
+
2
98
  # This class provides methods for easily drawing one or multiple lines of
3
99
  # text, with control over the text alignment and coloring.
4
100
  class TextHelper
@@ -8,9 +104,11 @@ module MiniGL
8
104
  # [font] A <code>Gosu::Font</code> that will be used to draw the text.
9
105
  # [line_spacing] When drawing multiple lines, the distance, in pixels,
10
106
  # between each line.
11
- def initialize(font, line_spacing = 0)
107
+ def initialize(font, line_spacing = 0, scale_x = 1, scale_y = 1)
12
108
  @font = font
13
109
  @line_spacing = line_spacing
110
+ @scale_x = scale_x
111
+ @scale_y = scale_y
14
112
  end
15
113
 
16
114
  # Draws a single line of text.
@@ -48,7 +146,7 @@ module MiniGL
48
146
  # mandatory.
49
147
  def write_line(text, x = nil, y = nil, mode = :left, color = 0, alpha = 0xff,
50
148
  effect = nil, effect_color = 0, effect_size = 1, effect_alpha = 0xff,
51
- z_index = 0)
149
+ z_index = 0, scale_x = nil, scale_y = nil)
52
150
  if text.is_a? Hash
53
151
  x = text[:x]
54
152
  y = text[:y]
@@ -60,9 +158,13 @@ module MiniGL
60
158
  effect_size = text.fetch(:effect_size, 1)
61
159
  effect_alpha = text.fetch(:effect_alpha, 0xff)
62
160
  z_index = text.fetch(:z_index, 0)
161
+ scale_x = text.fetch(:scale_x, nil)
162
+ scale_y = text.fetch(:scale_y, nil)
63
163
  text = text[:text]
64
164
  end
65
165
 
166
+ scale_x = @scale_x if scale_x.nil?
167
+ scale_y = @scale_y if scale_y.nil?
66
168
  color = (alpha << 24) | color
67
169
  rel =
68
170
  case mode
@@ -74,19 +176,19 @@ module MiniGL
74
176
  if effect
75
177
  effect_color = (effect_alpha << 24) | effect_color
76
178
  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
179
+ @font.draw_markup_rel text, x - effect_size, y - effect_size, z_index, rel, 0, scale_x, scale_y, effect_color
180
+ @font.draw_markup_rel text, x, y - effect_size, z_index, rel, 0, scale_x, scale_y, effect_color
181
+ @font.draw_markup_rel text, x + effect_size, y - effect_size, z_index, rel, 0, scale_x, scale_y, effect_color
182
+ @font.draw_markup_rel text, x + effect_size, y, z_index, rel, 0, scale_x, scale_y, effect_color
183
+ @font.draw_markup_rel text, x + effect_size, y + effect_size, z_index, rel, 0, scale_x, scale_y, effect_color
184
+ @font.draw_markup_rel text, x, y + effect_size, z_index, rel, 0, scale_x, scale_y, effect_color
185
+ @font.draw_markup_rel text, x - effect_size, y + effect_size, z_index, rel, 0, scale_x, scale_y, effect_color
186
+ @font.draw_markup_rel text, x - effect_size, y, z_index, rel, 0, scale_x, scale_y, effect_color
85
187
  elsif effect == :shadow
86
- @font.draw_markup_rel text, x + effect_size, y + effect_size, z_index, rel, 0, 1, 1, effect_color
188
+ @font.draw_markup_rel text, x + effect_size, y + effect_size, z_index, rel, 0, scale_x, scale_y, effect_color
87
189
  end
88
190
  end
89
- @font.draw_markup_rel text, x, y, z_index, rel, 0, 1, 1, color
191
+ @font.draw_markup_rel text, x, y, z_index, rel, 0, scale_x, scale_y, color
90
192
  end
91
193
 
92
194
  # Draws text, breaking lines when needed and when explicitly caused by the
@@ -94,7 +196,7 @@ module MiniGL
94
196
  #
95
197
  # Parameters:
96
198
  # [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.
199
+ # `<b>` tag for bold, `<i>` for italic and `<c=rrggbb>` for colors.
98
200
  # [x] The horizontal reference for drawing the text. Works like in
99
201
  # +write_line+ for the +:left+, +:right+ and +:center+ modes. For the
100
202
  # +:justified+ mode, works the same as for +:left+.
@@ -109,11 +211,14 @@ module MiniGL
109
211
  # transparent) to 255 (fully opaque).
110
212
  # [z_index] The z-order to draw the object. Objects with larger z-orders
111
213
  # 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)
214
+ def write_breaking(text, x, y, width, mode = :left, color = 0, alpha = 0xff, z_index = 0, scale_x = nil, scale_y = nil, line_spacing = nil)
215
+ line_spacing = @line_spacing if line_spacing.nil?
216
+ scale_x = @scale_x if scale_x.nil?
217
+ scale_y = @scale_y if scale_y.nil?
113
218
  color = (alpha << 24) | color
114
219
  text.split("\n").each do |p|
115
220
  if mode == :justified
116
- y = write_paragraph_justified p, x, y, width, color, z_index
221
+ y = write_paragraph_justified p, x, y, width, color, z_index, scale_x, scale_y, line_spacing
117
222
  else
118
223
  rel =
119
224
  case mode
@@ -122,40 +227,40 @@ module MiniGL
122
227
  when :right then 1
123
228
  else 0
124
229
  end
125
- y = write_paragraph p, x, y, width, rel, color, z_index
230
+ y = write_paragraph p, x, y, width, rel, color, z_index, scale_x, scale_y, line_spacing
126
231
  end
127
232
  end
128
233
  end
129
234
 
130
235
  private
131
236
 
132
- def write_paragraph(text, x, y, width, rel, color, z_index)
237
+ def write_paragraph(text, x, y, width, rel, color, z_index, scale_x, scale_y, line_spacing)
133
238
  line = ''
134
239
  line_width = 0
135
240
  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
241
+ w = @font.markup_width(word)
242
+ if line_width + w * scale_x > width
243
+ @font.draw_markup_rel line.chop, x, y, z_index, rel, 0, scale_x, scale_y, color
139
244
  line = ''
140
245
  line_width = 0
141
- y += @font.height + @line_spacing
246
+ y += (@font.height + line_spacing) * scale_y
142
247
  end
143
248
  line += "#{word} "
144
- line_width += @font.markup_width "#{word} "
249
+ line_width += @font.markup_width("#{word} ") * scale_x
145
250
  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
251
+ @font.draw_markup_rel line.chop, x, y, z_index, rel, 0, scale_x, scale_y, color unless line.empty?
252
+ y + (@font.height + line_spacing) * scale_y
148
253
  end
149
254
 
150
- def write_paragraph_justified(text, x, y, width, color, z_index)
151
- space_width = @font.text_width ' '
255
+ def write_paragraph_justified(text, x, y, width, color, z_index, scale_x, scale_y, line_spacing)
256
+ space_width = @font.text_width(' ') * scale_x
152
257
  spaces = [[]]
153
258
  line_index = 0
154
259
  new_x = x
155
260
  words = text.split(' ')
156
261
  words.each do |word|
157
- w = @font.markup_width word
158
- if new_x + w > x + width
262
+ w = @font.markup_width(word)
263
+ if new_x + w * scale_x > x + width
159
264
  space = x + width - new_x + space_width
160
265
  index = 0
161
266
  while space > 0
@@ -170,7 +275,7 @@ module MiniGL
170
275
 
171
276
  new_x = x
172
277
  end
173
- new_x += @font.markup_width(word) + space_width
278
+ new_x += @font.markup_width(word) * scale_x + space_width
174
279
  spaces[line_index] << space_width
175
280
  end
176
281
 
@@ -178,11 +283,11 @@ module MiniGL
178
283
  spaces.each do |line|
179
284
  new_x = x
180
285
  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
286
+ @font.draw_markup(words[index], new_x, y, z_index, scale_x, scale_y, color)
287
+ new_x += @font.markup_width(words[index]) * scale_x + s
183
288
  index += 1
184
289
  end
185
- y += @font.height + @line_spacing
290
+ y += (@font.height + line_spacing) * scale_y
186
291
  end
187
292
  y
188
293
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minigl
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.9
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor David Santos
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-07 00:00:00.000000000 Z
11
+ date: 2020-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gosu