minigl 2.1.2 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 747a0bd4b9dd2c916653d3db1e2b317221f872a5e349815dbeeda361ca5ab5c7
4
- data.tar.gz: 6bbf77ea8795d6c22643b4c768b5f2c566ab736205088c5eee59e25637dfc845
3
+ metadata.gz: bf74cda701aadb5941c6b9e7a35c1ef534fcb55608bfc6cf9e2614fce53e48c5
4
+ data.tar.gz: 53ba4be1ad4f60c97ba926986d8909df7f7db2eb9706f25a7a62f3f1c91ed68c
5
5
  SHA512:
6
- metadata.gz: e7c10ddb0bf26f542da3924b5fef29e6843e64fe5f4702ef41d12f83fbca4ba5fd2eafa6a327480afb175fb78c75aaba62219c7527a18334f18dc85a8cfe3af3
7
- data.tar.gz: 494c31d51030cea111e48f81bca2538dd094d5ad8d08b776c6ce85353bde32cfbfd83087b13fea857a32ea18fea611e52933be8f5c5d610889bf938d322d59fa
6
+ metadata.gz: 97699a40a88821a6506e4bf8861d0917c71753424b94131bcbecd7e7355ba484575114df951a423d0998307cfed731f18f80e6551de9bd2f539618a1bf562ddf
7
+ data.tar.gz: 79ebb0b3647c2e80bb43d1927c42f6ba027970d6fe11c273aba72bc2747cf9207c6cfc2868928bdd772b7eee7296e388d1a58924c03290e13c659a56bb08e34b
data/README.md CHANGED
@@ -29,6 +29,8 @@ 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.1.2
32
+ ## Version 2.2.0
33
33
 
34
- * Fixed `GameObject#draw` when rotated and scaled.
34
+ * Added the `tileable` option to `Res::imgs`.
35
+ * Added the `anchor` option to all form components.
36
+ * Added the `Panel` class, to easily group form components - take a look at the docs!
@@ -1,6 +1,27 @@
1
1
  require_relative 'global'
2
2
 
3
3
  module MiniGL
4
+ module FormUtils # :nodoc:
5
+ def self.check_anchor(anchor, x, y, w, h, area_w = G.window.width, area_h = G.window.height)
6
+ if anchor
7
+ case anchor
8
+ when /^top(_center)?$|^north$/i then anchor_alias = :top_center; x += (area_w - w) / 2
9
+ when /^top_right$|^northeast$/i then anchor_alias = :top_right; x = area_w - w - x
10
+ when /^(center_)?left$|^west$/i then anchor_alias = :center_left; y += (area_h - h) / 2
11
+ when /^center$/i then anchor_alias = :center; x += (area_w - w) / 2; y += (area_h - h) / 2
12
+ when /^(center_)?right$|^east$/i then anchor_alias = :center_right; x = area_w - w - x; y += (area_h - h) / 2
13
+ when /^bottom_left$|^southwest$/i then anchor_alias = :bottom_left; y = area_h - h - y
14
+ when /^bottom(_center)?$|^south$/i then anchor_alias = :bottom_center; x += (area_w - w) / 2; y = area_h - h - y
15
+ when /^bottom_right$|^southeast$/i then anchor_alias = :bottom_right; x = area_w - w - x; y = area_h - h - y
16
+ else anchor_alias = :top_left
17
+ end
18
+ else
19
+ anchor_alias = :top_left
20
+ end
21
+ [anchor_alias, x, y]
22
+ end
23
+ end
24
+
4
25
  # This class is an abstract ancestor for all form components (Button,
5
26
  # ToggleButton and TextField).
6
27
  class Component
@@ -16,6 +37,8 @@ module MiniGL
16
37
  # The height of the component
17
38
  attr_reader :h
18
39
 
40
+ attr_reader :anchor, :anchor_offset_x, :anchor_offset_y # :nodoc:
41
+
19
42
  # Determines whether the control is enabled, i.e., will process user input.
20
43
  attr_accessor :enabled
21
44
 
@@ -38,6 +61,131 @@ module MiniGL
38
61
  @disabled_text_color = disabled_text_color
39
62
  @enabled = @visible = true
40
63
  end
64
+
65
+ def update; end # :nodoc:
66
+
67
+ # Sets the position of the component.
68
+ # Parameters:
69
+ # [x] The new x coordinate.
70
+ # [y] The new y coordinate.
71
+ def set_position(x, y)
72
+ @x = x; @y = y
73
+ end
74
+ end
75
+
76
+ # Represents a container of form components.
77
+ class Panel
78
+ # Whether the components inside this panel are enabled.
79
+ attr_reader :enabled
80
+
81
+ # Gets or sets whether the panel (and thus all components inside it) are visible.
82
+ attr_accessor :visible
83
+
84
+ # Creates a new Panel.
85
+ # Parameters:
86
+ # [x] The horizontal coordinate of the top-left corner of the panel, or the horizontal offset from the anchor, if provided.
87
+ # [y] The vertical coordinate of the top-left corner of the panel, or the vertical offset from the anchor, if provided.
88
+ # [w] The width of the panel, in pixels.
89
+ # [h] The height of the panel, in pixels.
90
+ # [controls] An array of <code>Component</code>s that will be initially inside this panel.
91
+ # [img] Identifier of the image for the panel (see details in +Res::img+).
92
+ # [img_mode] Mode to scale the image. If +:normal+ (default), the image will be loaded as a single image and scaled to fit the entire size of the panel;
93
+ # if +:tiled+, the image will be loaded as a 3x3 spritesheet, where the "corner" images (i.e., indices 0, 2, 6 and 8) will be scaled by the +scale_x+ and +scale_y+ parameters,
94
+ # the "border" images (indices 1, 3, 5 and 7) will be stretched in the corresponding direction (indices 1 and 7 will be horizontally stretched and indices 3 and 5, vertically),
95
+ # and the "center" image (index 4) will be stretched in both directions, as needed, to fill the width and height of the panel.
96
+ # [retro] Whether the image should be loaded in retro mode.
97
+ # [scale_x] The fixed horizontal scale for "corner" and left and right "border" images (if +img_mode+ is +:tiled+).
98
+ # [scale_y] The fixed vertical scale for "corner" and top and bottom "border" images (if +img_mode+ is +:tiled+).
99
+ # [anchor] An alias for a predefined position of the window to be used as "anchor", i.e., reference for the positioning of the panel.
100
+ # Following are the valid values and a description of the corresponding position if +x+ and +y+ are 0 (these will be offsets from the reference position):
101
+ # * +:north+ or +:top+ or +:top_center+: the panel will be horizontally centered and its top will be at the top of the window.
102
+ # * +:northeast+ or +:top_right+: the top-right corner of the panel will meet the top-right corner of the window.
103
+ # * +:west+ or +:left+ or +:center_left+: the panel will be vertically centered and its left edge will be at the left edge of the window.
104
+ # * +:center+: the panel will be horizontally and vertically centered on the window.
105
+ # * +:east+ or +:right+ or +:center_right+: the panel will be vertically centered and its right edge will be at the right edge of the window.
106
+ # * +:southwest+ or +:bottom_left+: the bottom-left corner of the panel will meet the bottom-left corner of the window.
107
+ # * +:south+ or +:bottom+ or +:bottom_center+: the panel will be horizontally centered and its bottom will be at the bottom of the window.
108
+ # * +:southeast+ or +:bottom_right+: the bottom-right corner of the panel will meet the bottom-right corner of the window.
109
+ # If a value is not provided, the reference is the top-left corner of the screen.
110
+ # Components added as children of <code>Panel</code>s use the panel's coordinates as reference instead of the window.
111
+ def initialize(x, y, w, h, controls = [], img = nil, img_mode = :normal, retro = nil, scale_x = 1, scale_y = 1, anchor = nil)
112
+ _, x, y = FormUtils.check_anchor(anchor, x, y, w, h)
113
+ @x = x; @y = y; @w = w; @h = h
114
+ @controls = controls
115
+ controls.each do |c|
116
+ _, x, y = FormUtils.check_anchor(c.anchor, c.anchor_offset_x, c.anchor_offset_y, c.w, c.h, @w, @h)
117
+ c.set_position(@x + x, @y + y)
118
+ end
119
+
120
+ if img
121
+ retro = Res.retro_images if retro.nil?
122
+ if img_mode == :tiled
123
+ @img = Res.imgs(img, 3, 3, true, '.png', retro, true)
124
+ @scale_x = scale_x
125
+ @scale_y = scale_y
126
+ @tile_w = @img[0].width * @scale_x
127
+ @tile_h = @img[0].height * @scale_y
128
+ @draw_center_x = @w > 2 * @tile_w
129
+ @draw_center_y = @h > 2 * @tile_h
130
+ @center_scale_x = (@w - 2 * @tile_w).to_f / @tile_w * @scale_x
131
+ @center_scale_y = (@h - 2 * @tile_h).to_f / @tile_h * @scale_y
132
+ else
133
+ @img = Res.img(img, true, false, '.png', retro)
134
+ end
135
+ end
136
+
137
+ @visible = @enabled = true
138
+ end
139
+
140
+ # Updates all child components of this panel.
141
+ def update
142
+ @controls.each(&:update)
143
+ end
144
+
145
+ # Enables or disables all child components of this panel.
146
+ # Parameters:
147
+ # [value] Whether the components should be enabled.
148
+ def enabled=(value)
149
+ @enabled = value
150
+ @controls.each { |c| c.enabled = value }
151
+ end
152
+
153
+ # Adds a component to this panel.
154
+ # Parameters:
155
+ # [c] The component to add.
156
+ def add_component(c)
157
+ _, x, y = FormUtils.check_anchor(c.anchor, c.anchor_offset_x, c.anchor_offset_y, c.w, c.h, @w, @h)
158
+ c.set_position(@x + x, @y + y)
159
+ @controls << c
160
+ end
161
+
162
+ # Draws the panel and all its child components.
163
+ # Parameters:
164
+ # [alpha] The opacity of the panel (0 = fully transparent, 255 = fully opaque).
165
+ # [z_index] The z-index to draw the panel.
166
+ # [color] The color to apply as filter to the panel image and to all child components' images as well.
167
+ def draw(alpha = 255, z_index = 0, color = 0xffffff)
168
+ return unless @visible
169
+
170
+ c = (alpha << 24) | color
171
+ if @img
172
+ if @img.is_a?(Array)
173
+ @img[0].draw(@x, @y, z_index, @scale_x, @scale_y, c)
174
+ @img[1].draw(@x + @tile_w, @y, z_index, @center_scale_x, @scale_y, c) if @draw_center_x
175
+ @img[2].draw(@x + @w - @tile_w, @y, z_index, @scale_x, @scale_y, c)
176
+ @img[3].draw(@x, @y + @tile_h, z_index, @scale_x, @center_scale_y, c) if @draw_center_y
177
+ @img[4].draw(@x + @tile_w, @y + @tile_h, z_index, @center_scale_x, @center_scale_y, c) if @draw_center_x && @draw_center_y
178
+ @img[5].draw(@x + @w - @tile_w, @y + @tile_h, z_index, @scale_x, @center_scale_y, c) if @draw_center_y
179
+ @img[6].draw(@x, @y + @h - @tile_h, z_index, @scale_x, @scale_y, c)
180
+ @img[7].draw(@x + @tile_w, @y + @h - @tile_h, z_index, @center_scale_x, @scale_y, c) if @draw_center_x
181
+ @img[8].draw(@x + @w - @tile_w, @y + @h - @tile_h, z_index, @scale_x, @scale_y, c)
182
+ else
183
+ @img.draw(@x, @y, z_index, @w.to_f / @img.width, @h.to_f / @img.height)
184
+ end
185
+ end
186
+
187
+ @controls.each { |k| k.draw(alpha, z_index, color) }
188
+ end
41
189
  end
42
190
 
43
191
  # This class represents a button.
@@ -94,6 +242,7 @@ module MiniGL
94
242
  # +Res.retro_images+ value will be used.
95
243
  # [scale_x] Horizontal scale to draw the component with.
96
244
  # [scale_y] Vertical scale to draw the component with.
245
+ # [anchor] See parameter with the same name in <code>Panel#initialize</code> for details.
97
246
  # [action] The block of code executed when the button is clicked (or by
98
247
  # calling the +click+ method).
99
248
  #
@@ -103,7 +252,7 @@ module MiniGL
103
252
  def initialize(x, y = nil, font = nil, text = nil, img = nil,
104
253
  text_color = 0, disabled_text_color = 0, over_text_color = 0, down_text_color = 0,
105
254
  center_x = true, center_y = true, margin_x = 0, margin_y = 0, width = nil, height = nil,
106
- params = nil, retro = nil, scale_x = 1, scale_y = 1, &action)
255
+ params = nil, retro = nil, scale_x = 1, scale_y = 1, anchor = nil, &action)
107
256
  if x.is_a? Hash
108
257
  y = x[:y]
109
258
  font = x[:font]
@@ -123,24 +272,29 @@ module MiniGL
123
272
  retro = x.fetch(:retro, nil)
124
273
  scale_x = x.fetch(:scale_x, 1)
125
274
  scale_y = x.fetch(:scale_y, 1)
275
+ anchor = x.fetch(:anchor, nil)
126
276
  x = x[:x]
127
277
  end
128
278
 
129
- super x, y, font, text, text_color, disabled_text_color
130
- @over_text_color = over_text_color
131
- @down_text_color = down_text_color
132
279
  retro = Res.retro_images if retro.nil?
133
280
  @scale_x = scale_x
134
281
  @scale_y = scale_y
135
282
  @img =
136
- if img; Res.imgs img, 1, 4, true, '.png', retro
137
- else; nil; end
283
+ if img; Res.imgs img, 1, 4, true, '.png', retro
284
+ else; nil; end
138
285
  @w =
139
- if img; @img[0].width * @scale_x
140
- else; width * @scale_x; end
286
+ if img; @img[0].width * @scale_x
287
+ else; width * @scale_x; end
141
288
  @h =
142
- if img; @img[0].height * @scale_y
143
- else; height * @scale_y; end
289
+ if img; @img[0].height * @scale_y
290
+ else; height * @scale_y; end
291
+
292
+ @anchor_offset_x = x; @anchor_offset_y = y
293
+ @anchor, x, y = FormUtils.check_anchor(anchor, x, y, @w, @h)
294
+
295
+ super x, y, font, text, text_color, disabled_text_color
296
+ @over_text_color = over_text_color
297
+ @down_text_color = down_text_color
144
298
  if center_x; @text_x = x + @w / 2 if @w
145
299
  else; @text_x = x + margin_x * @scale_x; end
146
300
  if center_y; @text_y = y + @h / 2 if @h
@@ -206,7 +360,7 @@ module MiniGL
206
360
 
207
361
  # Executes the button click action.
208
362
  def click
209
- @action.call @params
363
+ @action.call @params if @action
210
364
  end
211
365
 
212
366
  # Sets the position of the button in the screen.
@@ -291,7 +445,7 @@ module MiniGL
291
445
  def initialize(x, y = nil, font = nil, text = nil, img = nil, checked = false,
292
446
  text_color = 0, disabled_text_color = 0, over_text_color = 0, down_text_color = 0,
293
447
  center_x = true, center_y = true, margin_x = 0, margin_y = 0, width = nil, height = nil,
294
- params = nil, retro = nil, scale_x = 1, scale_y = 1, &action)
448
+ params = nil, retro = nil, scale_x = 1, scale_y = 1, anchor = nil, &action)
295
449
  if x.is_a? Hash
296
450
  y = x[:y]
297
451
  font = x[:font]
@@ -312,11 +466,12 @@ module MiniGL
312
466
  retro = x.fetch(:retro, nil)
313
467
  scale_x = x.fetch(:scale_x, 1)
314
468
  scale_y = x.fetch(:scale_y, 1)
469
+ anchor = x.fetch(:anchor, nil)
315
470
  x = x[:x]
316
471
  end
317
472
 
318
473
  super x, y, font, text, nil, text_color, disabled_text_color, over_text_color, down_text_color,
319
- center_x, center_y, margin_x, margin_y, 0, 0, params, retro, scale_x, scale_y, &action
474
+ center_x, center_y, margin_x, margin_y, 0, 0, params, retro, scale_x, scale_y, anchor, &action
320
475
  @img =
321
476
  if img; Res.imgs img, 2, 4, true, '.png', retro
322
477
  else; nil; end
@@ -326,6 +481,8 @@ module MiniGL
326
481
  @h =
327
482
  if img; @img[0].height * @scale_y
328
483
  else; height * @scale_y; end
484
+ _, x, y = FormUtils.check_anchor(anchor, @anchor_offset_x, @anchor_offset_y, @w, @h)
485
+ set_position(x, y)
329
486
  @text_x = x + @w / 2 if center_x
330
487
  @text_y = y + @h / 2 if center_y
331
488
  @checked = checked
@@ -346,7 +503,7 @@ module MiniGL
346
503
  # been changed to checked, or +false+, otherwise.
347
504
  def click
348
505
  @checked = !@checked
349
- @action.call @checked, @params
506
+ @action.call @checked, @params if @action
350
507
  end
351
508
 
352
509
  # Sets the state of the button to the value given.
@@ -424,6 +581,7 @@ module MiniGL
424
581
  # +Res.retro_images+ value will be used.
425
582
  # [scale_x] Horizontal scale to draw the component with.
426
583
  # [scale_y] Vertical scale to draw the component with.
584
+ # [anchor] See parameter with the same name in <code>Panel#initialize</code> for details.
427
585
  # [on_text_changed] The block of code executed when the text in the text
428
586
  # field is changed, either by user input or by calling
429
587
  # +text=+. The new text is passed as a first parameter
@@ -434,7 +592,7 @@ module MiniGL
434
592
  def initialize(x, y = nil, font = nil, img = nil, cursor_img = nil, disabled_img = nil, margin_x = 0, margin_y = 0,
435
593
  max_length = 100, active = false, text = '', allowed_chars = nil,
436
594
  text_color = 0, disabled_text_color = 0, selection_color = 0, locale = 'en-us',
437
- params = nil, retro = nil, scale_x = 1, scale_y = 1, &on_text_changed)
595
+ params = nil, retro = nil, scale_x = 1, scale_y = 1, anchor = nil, &on_text_changed)
438
596
  if x.is_a? Hash
439
597
  y = x[:y]
440
598
  font = x[:font]
@@ -455,16 +613,21 @@ module MiniGL
455
613
  retro = x.fetch(:retro, nil)
456
614
  scale_x = x.fetch(:scale_x, 1)
457
615
  scale_y = x.fetch(:scale_y, 1)
616
+ anchor = x.fetch(:anchor, nil)
458
617
  x = x[:x]
459
618
  end
460
619
 
461
- super x, y, font, text, text_color, disabled_text_color
462
620
  retro = Res.retro_images if retro.nil?
463
621
  @scale_x = scale_x
464
622
  @scale_y = scale_y
465
623
  @img = Res.img img, false, false, '.png', retro
466
624
  @w = @img.width * @scale_x
467
625
  @h = @img.height * @scale_y
626
+
627
+ @anchor_offset_x = x; @anchor_offset_y = y
628
+ @anchor, x, y = FormUtils.check_anchor(anchor, x, y, @w, @h)
629
+
630
+ super x, y, font, text, text_color, disabled_text_color
468
631
  @cursor_img = Res.img(cursor_img, false, false, '.png', retro) if cursor_img
469
632
  @disabled_img = Res.img(disabled_img, false, false, '.png', retro) if disabled_img
470
633
  @max_length = max_length
@@ -908,12 +1071,13 @@ module MiniGL
908
1071
  # +Res.retro_images+ value will be used.
909
1072
  # [scale_x] Horizontal scale to draw the component with.
910
1073
  # [scale_y] Vertical scale to draw the component with.
1074
+ # [anchor] See parameter with the same name in <code>Panel#initialize</code> for details.
911
1075
  #
912
1076
  # *Obs.:* This method accepts named parameters, but +x+, +y+, +w+, +h+, +bg+
913
1077
  # and +fg+ are mandatory.
914
1078
  def initialize(x, y = nil, w = nil, h = nil, bg = nil, fg = nil,
915
1079
  max_value = 100, value = 100, fg_margin_x = 0, fg_margin_y = 0, # fg_left = nil, fg_right = nil,
916
- font = nil, text_color = 0, format = nil, retro = nil, scale_x = 1, scale_y = 1)
1080
+ font = nil, text_color = 0, format = nil, retro = nil, scale_x = 1, scale_y = 1, anchor = nil)
917
1081
  if x.is_a? Hash
918
1082
  y = x[:y]
919
1083
  w = x[:w]
@@ -930,14 +1094,12 @@ module MiniGL
930
1094
  retro = x.fetch(:retro, nil)
931
1095
  scale_x = x.fetch(:scale_x, 1)
932
1096
  scale_y = x.fetch(:scale_y, 1)
1097
+ anchor = x.fetch(:anchor, nil)
933
1098
  x = x[:x]
934
1099
  end
935
1100
 
936
- super x, y, font, '', text_color, text_color
937
1101
  @scale_x = scale_x
938
1102
  @scale_y = scale_y
939
- @w = w * @scale_x
940
- @h = h * @scale_y
941
1103
  retro = Res.retro_images if retro.nil?
942
1104
  if bg.is_a? Integer
943
1105
  @bg_color = bg
@@ -952,6 +1114,14 @@ module MiniGL
952
1114
  end
953
1115
  @fg_margin_x = fg_margin_x * @scale_x
954
1116
  @fg_margin_y = fg_margin_y * @scale_y
1117
+
1118
+ @w = (@bg ? @bg.width : w) * @scale_x
1119
+ @h = (@bg ? @bg.height : h) * @scale_y
1120
+
1121
+ @anchor_offset_x = x; @anchor_offset_y = y
1122
+ @anchor, x, y = FormUtils.check_anchor(anchor, x, y, @w, @h)
1123
+
1124
+ super x, y, font, '', text_color, text_color
955
1125
  # @fg_left = fg_left
956
1126
  # @fg_right = fg_right
957
1127
  @max_value = max_value
@@ -1051,7 +1221,7 @@ module MiniGL
1051
1221
  if @font
1052
1222
  c = (alpha << 24) | @text_color
1053
1223
  @text = @format == '%' ? "#{(@value.to_f / @max_value * 100).round}%" : "#{@value}/#{@max_value}"
1054
- @font.draw_rel @text, @x + @fg_margin_x + @w / 2, @y + @fg_margin_y + @h / 2, 0, 0.5, 0.5, @scale_x, @scale_y, c
1224
+ @font.draw_rel @text, @x + @w / 2, @y + @h / 2, z_index, 0.5, 0.5, @scale_x, @scale_y, c
1055
1225
  end
1056
1226
  end
1057
1227
  end
@@ -1091,6 +1261,7 @@ module MiniGL
1091
1261
  # +Res.retro_images+ value will be used.
1092
1262
  # [scale_x] Horizontal scale to draw the component with.
1093
1263
  # [scale_y] Vertical scale to draw the component with.
1264
+ # [anchor] See parameter with the same name in <code>Panel#initialize</code> for details.
1094
1265
  # [on_changed] Action performed when the value of the dropdown is changed.
1095
1266
  # It must be a block with two parameters, which will receive
1096
1267
  # the old and the new value, respectively.
@@ -1101,7 +1272,7 @@ module MiniGL
1101
1272
  def initialize(x, y = nil, font = nil, img = nil, opt_img = nil, options = nil,
1102
1273
  option = 0, text_margin = 0, width = nil, height = nil,
1103
1274
  text_color = 0, disabled_text_color = 0, over_text_color = 0, down_text_color = 0,
1104
- retro = nil, scale_x = 1, scale_y = 1, &on_changed)
1275
+ retro = nil, scale_x = 1, scale_y = 1, anchor = nil, &on_changed)
1105
1276
  if x.is_a? Hash
1106
1277
  y = x[:y]
1107
1278
  font = x[:font]
@@ -1119,10 +1290,9 @@ module MiniGL
1119
1290
  retro = x.fetch(:retro, nil)
1120
1291
  scale_x = x.fetch(:scale_x, 1)
1121
1292
  scale_y = x.fetch(:scale_y, 1)
1293
+ anchor = x.fetch(:anchor, nil)
1122
1294
  x = x[:x]
1123
1295
  end
1124
-
1125
- super x, y, font, options[option], text_color, disabled_text_color
1126
1296
  @img = img
1127
1297
  @opt_img = opt_img
1128
1298
  @options = options
@@ -1135,13 +1305,18 @@ module MiniGL
1135
1305
  toggle
1136
1306
  }
1137
1307
  )
1138
-
1308
+
1139
1309
  @scale_x = scale_x
1140
1310
  @scale_y = scale_y
1141
1311
  @w = @buttons[0].w
1142
1312
  @h = @buttons[0].h
1143
1313
  @max_h = (@options.size + 1) * @h
1144
1314
 
1315
+ @anchor_offset_x = x; @anchor_offset_y = y
1316
+ @anchor, x, y = FormUtils.check_anchor(anchor, x, y, @w, @h)
1317
+ super x, y, font, options[option], text_color, disabled_text_color
1318
+ @buttons[0].set_position(x, y)
1319
+
1145
1320
  @options.each_with_index do |o, i|
1146
1321
  b = Button.new(x, y + (i+1) * @h, font, o, opt_img, text_color, disabled_text_color, over_text_color, down_text_color,
1147
1322
  false, true, text_margin, 0, width, height, nil, retro, scale_x, scale_y) {
@@ -1183,6 +1358,11 @@ module MiniGL
1183
1358
  @enabled = value
1184
1359
  end
1185
1360
 
1361
+ def set_position(x, y)
1362
+ @x = x; @y = y
1363
+ @buttons.each_with_index { |b, i| b.set_position(x, y + i * @h) }
1364
+ end
1365
+
1186
1366
  # Draws the drop-down list.
1187
1367
  #
1188
1368
  # Parameters:
@@ -615,12 +615,12 @@ module MiniGL
615
615
  # [retro] Whether the image should be loaded with the 'retro' option set
616
616
  # (see +Gosu::Image+ for details). If the value is omitted, the
617
617
  # +Res.retro_images+ value will be used.
618
- def imgs(id, sprite_cols, sprite_rows, global = false, ext = '.png', retro = nil)
618
+ def imgs(id, sprite_cols, sprite_rows, global = false, ext = '.png', retro = nil, tileable = false)
619
619
  a = global ? @global_imgs : @imgs
620
620
  return a[id] if a[id]
621
621
  s = @prefix + @img_dir + id.to_s.split(@separator).join('/') + ext
622
622
  retro = Res.retro_images if retro.nil?
623
- imgs = Gosu::Image.load_tiles s, -sprite_cols, -sprite_rows, tileable: false, retro: retro
623
+ imgs = Gosu::Image.load_tiles s, -sprite_cols, -sprite_rows, tileable: tileable, retro: retro
624
624
  a[id] = imgs
625
625
  end
626
626
 
Binary file
@@ -22,17 +22,33 @@ class MyGame < GameWindow
22
22
  @btn = Button.new(10, 560, @font1, 'Test', :btn, 0x008000, 0x808080, 0xffffff, 0xff9980, true, true, 0, 4, 0, 0, 'friends', nil, 2, 2) { |x| puts "hello #{x}" }
23
23
  @btn.enabled = false
24
24
  @chk =
25
- ToggleButton.new(x: 40, y: 300, font: @font1, text: 'Click me', img: :check, center_x: false, margin_x: 36, params: 'friends', scale_x: 2.3, scale_y: 1.4) { |c, x|
25
+ ToggleButton.new(x: 0, y: 30, font: @font1, text: 'Click me', img: :check, center_x: false, margin_x: 36, params: 'friends', anchor: :south) { |c, x|
26
26
  puts "hello #{x}, checked: #{c}"
27
27
  }
28
- @txt = TextField.new(x: 10, y: 520, font: @font1, img: :text, margin_x: 15, margin_y: 5, max_length: 16, locale: 'PT-BR', scale_x: 1.2, scale_y: 0.8)
28
+ @txt = TextField.new(x: 0, y: 0, font: @font1, img: :text, margin_x: 15, margin_y: 5, max_length: 16, locale: 'PT-BR', scale_x: 1.2, scale_y: 0.8, anchor: :center_right)
29
29
  @txt.visible = false
30
30
 
31
- @pb = ProgressBar.new(5, 240, 200, 20, :barbg, :barfg, 3456, 70, 2, 2, @font1, 0xff000080, nil, nil, 1.8, 2)
32
- @ddl = DropDownList.new(5, 270, @font1, nil, nil, ['olá amigos', 'opção 2', 'terceira'], 0, 3, 150, 25, 0, 0x808080, 0xffffff, 0xffff00, nil, 2, 2.5) { |a, b|
31
+ @pb = ProgressBar.new(50, 0, 200, 20, :barbg, :barfg, 3456, 70, 2, 2, @font1, 0xff000080, nil, nil, 1.8, 2, :center_left)
32
+ @ddl = DropDownList.new(0, 10, @font1, nil, nil, ['olá amigos', 'opção 2', 'terceira'], 0, 3, 150, 25, 0, 0x808080, 0xffffff, 0xffff00, nil, 2, 2.5, :north) { |a, b|
33
33
  puts "mudou de #{a} para #{b}"
34
34
  }
35
35
 
36
+ @panel = Panel.new(10, 10,720, 520, [
37
+ Button.new(x: 5, y: 5, font: @font1, text: 'Teste', img: :btn),
38
+ TextField.new(x: 5, y: 40, font: @font1, img: :text, margin_x: 5, margin_y: 5, anchor: :top_left),
39
+ Button.new(x: 0, y: 5, font: @font1, text: 'Teste', img: :btn, anchor: :top),
40
+ DropDownList.new(x: 0, y: 40, width: 150, height: 25, font: @font1, options: ['olá amigos', 'opção 2', 'terceira'], anchor: :north),
41
+ Button.new(x: 5, y: 5, font: @font1, text: 'Teste', img: :btn, anchor: :northeast),
42
+ Button.new(x: 5, y: 0, font: @font1, text: 'Teste', img: :btn, anchor: :left),
43
+ Button.new(x: 0, y: 0, font: @font1, text: 'Teste', img: :btn, anchor: :center),
44
+ Button.new(x: 5, y: 0, font: @font1, text: 'Teste', img: :btn, anchor: :right),
45
+ ToggleButton.new(x: 5, y: 40, img: :check, center_x: false, margin_x: 36, anchor: :east),
46
+ Button.new(x: 5, y: 5, font: @font1, text: 'Teste', img: :btn, anchor: :southwest),
47
+ Button.new(x: 0, y: 5, font: @font1, text: 'Teste', img: :btn, anchor: :south),
48
+ ProgressBar.new(0, 40, 200, 20, :barbg, :barfg, 3456, 70, 2, 2, @font1, 0xff000080, nil, nil, 1, 1, :bottom),
49
+
50
+ ], :text, :tiled, true, 2, 2, :bottom_right)
51
+
36
52
  @eff = Effect.new(100, 100, :check, 2, 4, 10, nil, nil, '1')
37
53
 
38
54
  @angle = 0
@@ -59,6 +75,10 @@ class MyGame < GameWindow
59
75
  @pb.visible = !@pb.visible if KB.key_pressed? Gosu::KbE
60
76
  @ddl.enabled = !@ddl.enabled if KB.key_pressed? Gosu::KbQ
61
77
  @ddl.visible = !@ddl.visible if KB.key_pressed? Gosu::KbW
78
+ @panel.enabled = !@panel.enabled if KB.key_pressed? Gosu::KbN
79
+ @panel.visible = !@panel.visible if KB.key_pressed? Gosu::KbM
80
+
81
+ @panel.add_component(Button.new(x: 5, y: 5, font: @font1, text: 'Teste', img: :btn, anchor: :southeast)) if KB.key_pressed?(Gosu::KbB)
62
82
 
63
83
  @pb.increase 1 if KB.key_down? Gosu::KbD
64
84
  @pb.decrease 1 if KB.key_down? Gosu::KbA
@@ -89,6 +109,8 @@ class MyGame < GameWindow
89
109
  @txt.update
90
110
  @ddl.update
91
111
 
112
+ @panel.update
113
+
92
114
  @eff.update
93
115
 
94
116
  @objs.each_with_index do |o, i|
@@ -119,6 +141,8 @@ class MyGame < GameWindow
119
141
  @txt.draw
120
142
  @pb.draw 0x66
121
143
 
144
+ @panel.draw(204, 10)
145
+
122
146
  @eff.draw
123
147
  end
124
148
  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.1.2
4
+ version: 2.2.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: 2018-10-12 00:00:00.000000000 Z
11
+ date: 2018-11-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gosu