minigl 1.3.0 → 1.3.1
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 +4 -4
- data/README.md +7 -5
- data/data/img/btn.png +0 -0
- data/data/img/check.png +0 -0
- data/lib/minigl/forms.rb +91 -40
- data/lib/minigl/global.rb +4 -2
- data/test/game.rb +11 -4
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 77d77deca3bb6d5d439cb293a9f4782abcba1b9f
|
4
|
+
data.tar.gz: 68bdd7b0fb6c06fb0041717e0895b4f054b67464
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7c3b435fe29547b9f1ceb4507bb4fcbfc665cd058c0a22ba5a8843903f60df8908fe0348ca3af1e2aa2d85c7ad990a905ae9edfc5bf0d2fd1073823f822f6910
|
7
|
+
data.tar.gz: 12522cfd3056d20e4afed6f8d89d57dc7f930e47febec71175bae399bf1905da591008ab68b7b14e78479307f291526ecae30c36b4f6a832b6ccae6be4841b0e
|
data/README.md
CHANGED
@@ -22,9 +22,11 @@ this [working game example](https://github.com/victords/aventura-do-saber).
|
|
22
22
|
* An auxiliary, tutorial-like documentation is under construction
|
23
23
|
[here](https://github.com/victords/minigl/wiki).
|
24
24
|
|
25
|
-
**Version 1.3.
|
25
|
+
**Version 1.3.1**
|
26
26
|
|
27
|
-
* Introduced the `
|
28
|
-
*
|
29
|
-
*
|
30
|
-
|
27
|
+
* Introduced the `enabled` and `visible` attributes for the form components.
|
28
|
+
* Fixed issue with right shift for selection in the `TextField`.
|
29
|
+
* Added missing keys to `KB`.
|
30
|
+
|
31
|
+
**WARNING**: this version can generate incompatibility, because of the parameter
|
32
|
+
order in the constructors for `Button`, `ToggleButton` and `TextField`.
|
data/data/img/btn.png
CHANGED
Binary file
|
data/data/img/check.png
CHANGED
Binary file
|
data/lib/minigl/forms.rb
CHANGED
@@ -1,8 +1,22 @@
|
|
1
1
|
require_relative 'global'
|
2
2
|
|
3
3
|
module AGL
|
4
|
+
class Component # :nodoc:
|
5
|
+
attr_accessor :enabled, :visible
|
6
|
+
|
7
|
+
def initialize x, y, font, text, text_color, disabled_text_color
|
8
|
+
@x = x
|
9
|
+
@y = y
|
10
|
+
@font = font
|
11
|
+
@text = text
|
12
|
+
@text_color = text_color
|
13
|
+
@disabled_text_color = disabled_text_color
|
14
|
+
@enabled = @visible = true
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
4
18
|
# This class represents a button.
|
5
|
-
class Button
|
19
|
+
class Button < Component
|
6
20
|
# Creates a button.
|
7
21
|
#
|
8
22
|
# Parameters:
|
@@ -11,12 +25,15 @@ module AGL
|
|
11
25
|
# [font] The <code>Gosu::Font</code> object that will be used to draw the
|
12
26
|
# button text.
|
13
27
|
# [text] The button text. Can be +nil+ or empty.
|
14
|
-
# [img] A spritesheet containing
|
28
|
+
# [img] A spritesheet containing four images in a column, representing,
|
15
29
|
# from top to bottom, the default state, the hover state (when the
|
16
|
-
# mouse is over the button)
|
17
|
-
# button is down and the cursor is over the button)
|
18
|
-
# +width+ and +height+ parameters must be
|
30
|
+
# mouse is over the button), the pressed state (when the mouse
|
31
|
+
# button is down and the cursor is over the button) and the disabled
|
32
|
+
# state. If +nil+, the +width+ and +height+ parameters must be
|
33
|
+
# provided.
|
19
34
|
# [text_color] Color of the button text, in hexadecimal RRGGBB format.
|
35
|
+
# [disabled_text_color] Color of the button text, when it's disabled, in
|
36
|
+
# hexadecimal RRGGBB format.
|
20
37
|
# [center] Whether the button text should be centered in its area (the
|
21
38
|
# area is defined by the image size, when an image is given, or
|
22
39
|
# by the +width+ and +height+ parameters, otherwise).
|
@@ -36,13 +53,11 @@ module AGL
|
|
36
53
|
# parameters.
|
37
54
|
# [action] The block of code executed when the button is clicked (or by
|
38
55
|
# calling the +click+ method).
|
39
|
-
def initialize x, y, font, text, img, text_color = 0, center = true, margin_x = 0, margin_y = 0,
|
40
|
-
|
41
|
-
|
42
|
-
@font = font
|
43
|
-
@text = text
|
56
|
+
def initialize x, y, font, text, img, text_color = 0, disabled_text_color = 0, center = true, margin_x = 0, margin_y = 0,
|
57
|
+
width = nil, height = nil, params = nil, &action
|
58
|
+
super x, y, font, text, text_color, disabled_text_color
|
44
59
|
@img =
|
45
|
-
if img; Res.imgs img, 1,
|
60
|
+
if img; Res.imgs img, 1, 4, true
|
46
61
|
else; nil; end
|
47
62
|
@w =
|
48
63
|
if img; @img[0].width
|
@@ -57,18 +72,19 @@ module AGL
|
|
57
72
|
@text_x = x + margin_x
|
58
73
|
@text_y = y + margin_y
|
59
74
|
end
|
60
|
-
@text_color = text_color
|
61
75
|
@center = center
|
62
76
|
@action = action
|
63
77
|
@params = params
|
64
78
|
|
65
79
|
@state = :up
|
66
|
-
@img_index = 0
|
80
|
+
@img_index = @enabled ? 0 : 3
|
67
81
|
end
|
68
82
|
|
69
83
|
# Updates the button, checking the mouse movement and buttons to define
|
70
84
|
# the button state.
|
71
85
|
def update
|
86
|
+
return unless @enabled and @visible
|
87
|
+
|
72
88
|
mouse_over = Mouse.over? @x, @y, @w, @h
|
73
89
|
mouse_press = Mouse.button_pressed? :left
|
74
90
|
mouse_rel = Mouse.button_released? :left
|
@@ -143,8 +159,10 @@ module AGL
|
|
143
159
|
# [alpha] The opacity with which the button will be drawn. Allowed values
|
144
160
|
# vary between 0 (fully transparent) and 255 (fully opaque).
|
145
161
|
def draw alpha = 0xff
|
162
|
+
return unless @visible
|
163
|
+
|
146
164
|
color = (alpha << 24) | 0xffffff
|
147
|
-
text_color = (alpha << 24) | @text_color
|
165
|
+
text_color = (alpha << 24) | (@enabled ? @text_color : @disabled_text_color)
|
148
166
|
@img[@img_index].draw @x, @y, 0, 1, 1, color if @img
|
149
167
|
if @text
|
150
168
|
if @center
|
@@ -154,6 +172,12 @@ module AGL
|
|
154
172
|
end
|
155
173
|
end
|
156
174
|
end
|
175
|
+
|
176
|
+
def enabled= value # :nodoc:
|
177
|
+
@enabled = value
|
178
|
+
@state = :up
|
179
|
+
@img_index = 3
|
180
|
+
end
|
157
181
|
end
|
158
182
|
|
159
183
|
# This class represents a toggle button, which can be also interpreted as a
|
@@ -165,7 +189,7 @@ module AGL
|
|
165
189
|
|
166
190
|
# Creates a ToggleButton. All parameters work the same as in Button,
|
167
191
|
# except the image, +img+, which now has to be composed of two columns
|
168
|
-
# and
|
192
|
+
# and four rows, the first column with images for the unchecked state,
|
169
193
|
# and the second with images for the checked state.
|
170
194
|
#
|
171
195
|
# The +action+ block now will always receive a first boolean parameter
|
@@ -175,10 +199,11 @@ module AGL
|
|
175
199
|
# puts "button was checked" if checked
|
176
200
|
# # do something with params
|
177
201
|
# }
|
178
|
-
def initialize x, y, font, text, img, text_color = 0, center = true, margin_x = 0, margin_y = 0,
|
179
|
-
|
202
|
+
def initialize x, y, font, text, img, text_color = 0, disabled_text_color = 0, center = true, margin_x = 0, margin_y = 0,
|
203
|
+
width = nil, height = nil, params = nil, &action
|
204
|
+
super x, y, font, text, nil, text_color, disabled_text_color, center, margin_x, margin_y, width, height, params, &action
|
180
205
|
@img =
|
181
|
-
if img; Res.imgs img, 2,
|
206
|
+
if img; Res.imgs img, 2, 4, true
|
182
207
|
else; nil; end
|
183
208
|
@w =
|
184
209
|
if img; @img[0].width
|
@@ -191,6 +216,8 @@ module AGL
|
|
191
216
|
# Updates the button, checking the mouse movement and buttons to define
|
192
217
|
# the button state.
|
193
218
|
def update
|
219
|
+
return unless @enabled and @visible
|
220
|
+
|
194
221
|
super
|
195
222
|
@img_index *= 2
|
196
223
|
@img_index += 1 if @checked
|
@@ -212,10 +239,16 @@ module AGL
|
|
212
239
|
click if value != @checked
|
213
240
|
@checked = value
|
214
241
|
end
|
242
|
+
|
243
|
+
def enabled= value # :nodoc:
|
244
|
+
@enabled = value
|
245
|
+
@state = :up
|
246
|
+
@img_index = @checked ? 7 : 6
|
247
|
+
end
|
215
248
|
end
|
216
249
|
|
217
250
|
# This class represents a text field (input).
|
218
|
-
class TextField
|
251
|
+
class TextField < Component
|
219
252
|
# The current text inside the text field.
|
220
253
|
attr_reader :text
|
221
254
|
|
@@ -232,6 +265,8 @@ module AGL
|
|
232
265
|
# [cursor_img] An image for the blinking cursor that stands in the point
|
233
266
|
# where text will be inserted. If +nil+, a simple black line
|
234
267
|
# will be drawn instead.
|
268
|
+
# [disabled_img] Image for the text field when it's disabled. If +nil+,
|
269
|
+
# a darkened version of +img+ will be used.
|
235
270
|
# [text_color] Color of the button text, in hexadecimal RRGGBB format.
|
236
271
|
# [margin_x] The x offset, from the field x-coordinate, to draw the text.
|
237
272
|
# [margin_y] The y offset, from the field y-coordinate, to draw the text.
|
@@ -246,33 +281,34 @@ module AGL
|
|
246
281
|
# <code>"abcdefghijklmnopqrstuvwxyz1234567890 ABCDEFGHIJKLMNOPQRSTUVWXYZ'-=/[]\\\\,.;\"_+?{}|<>:!@#$%¨&*()"</code>.
|
247
282
|
# [text_color] The color with which the text will be drawn, in hexadecimal
|
248
283
|
# RRGGBB format.
|
284
|
+
# [disabled_text_color] The color with which the text will be drawn, when
|
285
|
+
# the text field is disabled, in hexadecimal RRGGBB
|
286
|
+
# format.
|
249
287
|
# [selection_color] The color of the rectangle highlighting selected text,
|
250
288
|
# in hexadecimal RRGGBB format. The rectangle will
|
251
289
|
# always be drawn with 50% of opacity.
|
252
290
|
# [params] An object containing any parameters you want passed to the
|
253
291
|
# +on_text_changed+ block. When the text of the text field is
|
254
292
|
# changed, the following is called:
|
255
|
-
# @on_text_changed.call @params
|
256
|
-
#
|
257
|
-
# parameters.
|
293
|
+
# @on_text_changed.call @text, @params
|
294
|
+
# Thus, +params+ will be the second parameter. Note that this
|
295
|
+
# doesn't force you to declare a block that takes parameters.
|
258
296
|
# [on_text_changed] The block of code executed when the text in the text
|
259
297
|
# field is changed, either by user input or by calling
|
260
|
-
# +text=+.
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
@font = font
|
298
|
+
# +text=+. The new text is passed as a first parameter
|
299
|
+
# to this block, followed by +params+. Can be +nil+.
|
300
|
+
def initialize x, y, font, img, cursor_img = nil, disabled_img = nil, margin_x = 0, margin_y = 0, max_length = 100, active = false, text = "",
|
301
|
+
allowed_chars = nil, text_color = 0, disabled_text_color = 0, selection_color = 0, params = nil, &on_text_changed
|
302
|
+
super x, y, font, text, text_color, disabled_text_color
|
266
303
|
@img = Res.img img
|
267
304
|
@w = @img.width
|
268
305
|
@h = @img.height
|
269
306
|
@cursor_img = Res.img(cursor_img) if cursor_img
|
307
|
+
@disabled_img = Res.img(disabled_img) if disabled_img
|
270
308
|
@max_length = max_length
|
271
309
|
@active = active
|
272
|
-
@text = text
|
273
310
|
@text_x = x + margin_x
|
274
311
|
@text_y = y + margin_y
|
275
|
-
@text_color = text_color
|
276
312
|
@selection_color = selection_color
|
277
313
|
|
278
314
|
@nodes = [x + margin_x]
|
@@ -310,6 +346,8 @@ module AGL
|
|
310
346
|
|
311
347
|
# Updates the text field, checking for mouse events and keyboard input.
|
312
348
|
def update
|
349
|
+
return unless @enabled and @visible
|
350
|
+
|
313
351
|
################################ Mouse ################################
|
314
352
|
if Mouse.over? @x, @y, @w, @h
|
315
353
|
if not @active and Mouse.button_pressed? :left
|
@@ -356,10 +394,10 @@ module AGL
|
|
356
394
|
end
|
357
395
|
|
358
396
|
############################### Keyboard ##############################
|
359
|
-
shift = KB.key_down?
|
360
|
-
if KB.key_pressed?
|
397
|
+
shift = ((KB.key_down? @k[53]) or (KB.key_down? @k[54]))
|
398
|
+
if ((KB.key_pressed? @k[53]) or (KB.key_pressed? @k[54])) # shift
|
361
399
|
@anchor1 = @cur_node if @anchor1.nil?
|
362
|
-
elsif KB.key_released?
|
400
|
+
elsif ((KB.key_released? @k[53]) or (KB.key_released? @k[54]))
|
363
401
|
@anchor1 = nil if @anchor2.nil?
|
364
402
|
end
|
365
403
|
inserted = false
|
@@ -485,7 +523,7 @@ module AGL
|
|
485
523
|
@anchor1 = nil
|
486
524
|
@anchor2 = nil
|
487
525
|
set_cursor_visible
|
488
|
-
@on_text_changed.call @params if @on_text_changed
|
526
|
+
@on_text_changed.call @text, @params if @on_text_changed
|
489
527
|
end
|
490
528
|
|
491
529
|
# Returns the currently selected text.
|
@@ -532,9 +570,12 @@ module AGL
|
|
532
570
|
# [alpha] The opacity with which the text field will be drawn. Allowed
|
533
571
|
# values vary between 0 (fully transparent) and 255 (fully opaque).
|
534
572
|
def draw alpha = 0xff
|
535
|
-
|
536
|
-
|
537
|
-
|
573
|
+
return unless @visible
|
574
|
+
|
575
|
+
color = (alpha << 24) | ((@enabled or @disabled_img) ? 0xffffff : 0x808080)
|
576
|
+
text_color = (alpha << 24) | (@enabled ? @text_color : @disabled_text_color)
|
577
|
+
img = ((@enabled or @disabled_img.nil?) ? @img : @disabled_img)
|
578
|
+
img.draw @x, @y, 0, 1, 1, color
|
538
579
|
@font.draw @text, @text_x, @text_y, 0, 1, 1, text_color
|
539
580
|
|
540
581
|
if @anchor1 and @anchor2
|
@@ -558,6 +599,16 @@ module AGL
|
|
558
599
|
end
|
559
600
|
end
|
560
601
|
|
602
|
+
def enabled= value # :nodoc:
|
603
|
+
@enabled = value
|
604
|
+
unfocus unless @enabled
|
605
|
+
end
|
606
|
+
|
607
|
+
def visible= value # :nodoc:
|
608
|
+
@visible = value
|
609
|
+
unfocus unless @visible
|
610
|
+
end
|
611
|
+
|
561
612
|
private
|
562
613
|
|
563
614
|
def set_cursor_visible
|
@@ -589,7 +640,7 @@ module AGL
|
|
589
640
|
end
|
590
641
|
@cur_node += 1
|
591
642
|
set_cursor_visible
|
592
|
-
@on_text_changed.call @params if @on_text_changed
|
643
|
+
@on_text_changed.call @text, @params if @on_text_changed
|
593
644
|
end
|
594
645
|
|
595
646
|
def remove_interval will_insert = false
|
@@ -608,7 +659,7 @@ module AGL
|
|
608
659
|
@anchor1 = nil
|
609
660
|
@anchor2 = nil
|
610
661
|
set_cursor_visible
|
611
|
-
@on_text_changed.call @params if @on_text_changed and not will_insert
|
662
|
+
@on_text_changed.call @text, @params if @on_text_changed and not will_insert
|
612
663
|
end
|
613
664
|
|
614
665
|
def remove_char back
|
@@ -620,7 +671,7 @@ module AGL
|
|
620
671
|
@nodes[i] -= char_width
|
621
672
|
end
|
622
673
|
set_cursor_visible
|
623
|
-
@on_text_changed.call @params if @on_text_changed
|
674
|
+
@on_text_changed.call @text, @params if @on_text_changed
|
624
675
|
end
|
625
676
|
end
|
626
677
|
end
|
data/lib/minigl/global.rb
CHANGED
@@ -106,6 +106,7 @@ module AGL
|
|
106
106
|
Gosu::KbUp, Gosu::KbDown,
|
107
107
|
Gosu::KbReturn, Gosu::KbEscape,
|
108
108
|
Gosu::KbLeftControl, Gosu::KbRightControl,
|
109
|
+
Gosu::KbLeftAlt, Gosu::KbRightAlt,
|
109
110
|
Gosu::KbA, Gosu::KbB, Gosu::KbC, Gosu::KbD, Gosu::KbE, Gosu::KbF,
|
110
111
|
Gosu::KbG, Gosu::KbH, Gosu::KbI, Gosu::KbJ, Gosu::KbK, Gosu::KbL,
|
111
112
|
Gosu::KbM, Gosu::KbN, Gosu::KbO, Gosu::KbP, Gosu::KbQ, Gosu::KbR,
|
@@ -116,7 +117,7 @@ module AGL
|
|
116
117
|
Gosu::KbNumpad5, Gosu::KbNumpad6, Gosu::KbNumpad7, Gosu::KbNumpad8,
|
117
118
|
Gosu::KbNumpad9, Gosu::KbNumpad0, Gosu::KbSpace, Gosu::KbBackspace,
|
118
119
|
Gosu::KbDelete, Gosu::KbLeft, Gosu::KbRight, Gosu::KbHome,
|
119
|
-
Gosu::KbEnd, Gosu::KbLeftShift, Gosu::KbRightShift,
|
120
|
+
Gosu::KbEnd, Gosu::KbLeftShift, Gosu::KbRightShift, Gosu::KbTab,
|
120
121
|
Gosu::KbBacktick, Gosu::KbMinus, Gosu::KbEqual, Gosu::KbBracketLeft,
|
121
122
|
Gosu::KbBracketRight, Gosu::KbBackslash, Gosu::KbApostrophe,
|
122
123
|
Gosu::KbComma, Gosu::KbPeriod, Gosu::KbSlash
|
@@ -162,6 +163,7 @@ module AGL
|
|
162
163
|
# [key] Code of the key to be checked. The available codes are <code>
|
163
164
|
# Gosu::KbUp, Gosu::KbDown, Gosu::KbReturn, Gosu::KbEscape,
|
164
165
|
# Gosu::KbLeftControl, Gosu::KbRightControl,
|
166
|
+
# Gosu::KbLeftAlt, Gosu::KbRightAlt,
|
165
167
|
# Gosu::KbA, Gosu::KbB, Gosu::KbC, Gosu::KbD, Gosu::KbE, Gosu::KbF,
|
166
168
|
# Gosu::KbG, Gosu::KbH, Gosu::KbI, Gosu::KbJ, Gosu::KbK, Gosu::KbL,
|
167
169
|
# Gosu::KbM, Gosu::KbN, Gosu::KbO, Gosu::KbP, Gosu::KbQ, Gosu::KbR,
|
@@ -172,7 +174,7 @@ module AGL
|
|
172
174
|
# Gosu::KbNumpad5, Gosu::KbNumpad6, Gosu::KbNumpad7, Gosu::KbNumpad8,
|
173
175
|
# Gosu::KbNumpad9, Gosu::KbNumpad0, Gosu::KbSpace, Gosu::KbBackspace,
|
174
176
|
# Gosu::KbDelete, Gosu::KbLeft, Gosu::KbRight, Gosu::KbHome,
|
175
|
-
# Gosu::KbEnd, Gosu::KbLeftShift, Gosu::KbRightShift,
|
177
|
+
# Gosu::KbEnd, Gosu::KbLeftShift, Gosu::KbRightShift, Gosu::KbTab,
|
176
178
|
# Gosu::KbBacktick, Gosu::KbMinus, Gosu::KbEqual, Gosu::KbBracketLeft,
|
177
179
|
# Gosu::KbBracketRight, Gosu::KbBackslash, Gosu::KbApostrophe,
|
178
180
|
# Gosu::KbComma, Gosu::KbPeriod, Gosu::KbSlash</code>.
|
data/test/game.rb
CHANGED
@@ -11,9 +11,13 @@ class MyGame < Gosu::Window
|
|
11
11
|
|
12
12
|
@font = Res.font :font1, 20
|
13
13
|
@writer = TextHelper.new @font, 5
|
14
|
-
@btn = Button.new(10, 560, @font, "Test", :btn, 0x008000, 0, 0, 0, 0, 0, "friends") { |x| puts "hello #{x}" }
|
15
|
-
@
|
16
|
-
@
|
14
|
+
@btn = Button.new(10, 560, @font, "Test", :btn, 0x008000, 0x808080, 0, 0, 0, 0, 0, "friends") { |x| puts "hello #{x}" }
|
15
|
+
@btn.enabled = false
|
16
|
+
@chk = ToggleButton.new(210, 560, @font, "Click me", :check, 0xffffff, 0x808080, false, 36, 5, 0, 0, "friends") { |c, x|
|
17
|
+
puts "hello #{x}, checked: #{c}"
|
18
|
+
}
|
19
|
+
@txt = TextField.new(10, 520, @font, :text, nil, nil, 15, 5, 16, false, "", nil, 0, 0, 0x0000ff, "test") { |t, x| puts "field #{x}, text: #{t}" }
|
20
|
+
@txt.visible = false
|
17
21
|
end
|
18
22
|
|
19
23
|
def needs_cursor?
|
@@ -27,8 +31,11 @@ class MyGame < Gosu::Window
|
|
27
31
|
@obj1.y += 1 if KB.key_held? Gosu::KbDown
|
28
32
|
@obj1.x -= 1 if KB.key_down? Gosu::KbLeft
|
29
33
|
@btn.set_position rand(700), rand(550) if KB.key_pressed? Gosu::KbSpace
|
34
|
+
@btn.enabled = !@btn.enabled if KB.key_pressed? Gosu::KbLeftControl
|
30
35
|
@chk.checked = false if KB.key_pressed? Gosu::KbEscape
|
31
|
-
@
|
36
|
+
@chk.enabled = !@chk.enabled if KB.key_pressed? Gosu::KbRightControl
|
37
|
+
@txt.visible = !@txt.visible if KB.key_pressed? Gosu::KbReturn
|
38
|
+
@txt.enabled = !@txt.enabled if KB.key_pressed? Gosu::KbLeftAlt
|
32
39
|
|
33
40
|
Mouse.update
|
34
41
|
if Mouse.double_click? :left
|