minigl 1.2.7 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 450b8d40d9d7a949621ae94fac7cdf702742e80a
4
- data.tar.gz: e70c4b21bcf68daed5a06704dfbbb2cc16234cfe
3
+ metadata.gz: 667a3bf9b0c24c5b639a09bd70f8020fa09d7f88
4
+ data.tar.gz: 6c57295ac2e1e00a442d8ab87a959c4dc10b63d9
5
5
  SHA512:
6
- metadata.gz: 159a32f1c0ea9688e10d8599984b6d0e65ed4579e49c94b80a02bf77c065d7bceca0127be3a011acf174c6d5cd1067f51996c60e17d9a36ed56dece1e340aa7b
7
- data.tar.gz: c9bc082ef4e2ce42b531d665ad0be11e65897830fdf4367e6262c2aba790f23bfba857ff4cf01a7d776313a264293b43c9c07851ce6407b73005b1d129d3d3bc
6
+ metadata.gz: 3e958267a7ff88286ed27bba81e4e59554fbb91dc80f5fe351368060074b4c6a12439f43fd91ef388d6117f73bf0e8a8931e69209eb0b8ef3acf3ee134bcf4d8
7
+ data.tar.gz: a5f27fd536134d5a4dd69113ed5c35d98db356fe0afaad3856be0160489fe98606c877e3d0fcb26a86f2e2f7e673fd2e12d9298b73903df6b3d75338bea0c150
data/README.md CHANGED
@@ -22,8 +22,9 @@ 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.2.7**
25
+ **Version 1.3.0**
26
26
 
27
- * Completed and added documentation for the `Effect` class.
28
- * Slight adjusts in the documentation.
29
- * Slight adjust in the implementation of `Button`.
27
+ * Introduced the `ToggleButton` class.
28
+ * `Button` action now accepts parameters.
29
+ * `TextField` now triggers a "text changed" event (which accepts parameters,
30
+ as well).
Binary file
data/lib/minigl/forms.rb CHANGED
@@ -28,9 +28,15 @@ module AGL
28
28
  # if +img+ is +nil+.
29
29
  # [height] Height of the button clickable area. This parameter is used
30
30
  # only if +img+ is +nil+.
31
+ # [params] An object containing any parameters you want passed to the
32
+ # +action+ block. When the button is clicked, the following is
33
+ # called:
34
+ # @action.call @params
35
+ # Note that this doesn't force you to declare a block that takes
36
+ # parameters.
31
37
  # [action] The block of code executed when the button is clicked (or by
32
38
  # calling the +click+ method).
33
- def initialize x, y, font, text, img, text_color = 0, center = true, margin_x = 0, margin_y = 0, width = nil, height = nil, &action
39
+ def initialize x, y, font, text, img, text_color = 0, center = true, margin_x = 0, margin_y = 0, width = nil, height = nil, params = nil, &action
34
40
  @x = x
35
41
  @y = y
36
42
  @font = font
@@ -54,6 +60,7 @@ module AGL
54
60
  @text_color = text_color
55
61
  @center = center
56
62
  @action = action
63
+ @params = params
57
64
 
58
65
  @state = :up
59
66
  @img_index = 0
@@ -70,6 +77,8 @@ module AGL
70
77
  if mouse_over
71
78
  @img_index = 1
72
79
  @state = :over
80
+ else
81
+ @img_index = 0
73
82
  end
74
83
  elsif @state == :over
75
84
  if not mouse_over
@@ -78,30 +87,36 @@ module AGL
78
87
  elsif mouse_press
79
88
  @img_index = 2
80
89
  @state = :down
90
+ else
91
+ @img_index = 1
81
92
  end
82
93
  elsif @state == :down
83
94
  if not mouse_over
84
95
  @img_index = 0
85
96
  @state = :down_out
86
97
  elsif mouse_rel
87
- @img_index = 0
88
- @state = :up
98
+ @img_index = 1
99
+ @state = :over
89
100
  click
101
+ else
102
+ @img_index = 2
90
103
  end
91
- elsif @state == :down_out
104
+ else # :down_out
92
105
  if mouse_over
93
106
  @img_index = 2
94
107
  @state = :down
95
108
  elsif mouse_rel
96
109
  @img_index = 0
97
110
  @state = :up
111
+ else
112
+ @img_index = 0
98
113
  end
99
114
  end
100
115
  end
101
116
 
102
117
  # Executes the button click action.
103
118
  def click
104
- @action.call
119
+ @action.call @params
105
120
  end
106
121
 
107
122
  # Sets the position of the button in the screen.
@@ -141,6 +156,64 @@ module AGL
141
156
  end
142
157
  end
143
158
 
159
+ # This class represents a toggle button, which can be also interpreted as a
160
+ # check box. It is always in one of two states, given as +true+ or +false+
161
+ # by its property +checked+.
162
+ class ToggleButton < Button
163
+ # Defines the state of the button (returns +true+ or +false+).
164
+ attr_reader :checked
165
+
166
+ # Creates a ToggleButton. All parameters work the same as in Button,
167
+ # except the image, +img+, which now has to be composed of two columns
168
+ # and three rows, the first column with images for the unchecked state,
169
+ # and the second with images for the checked state.
170
+ #
171
+ # The +action+ block now will always receive a first boolean parameter
172
+ # corresponding to the value of +checked+. So, if you want to pass
173
+ # parameters to the block, you should declare it like this:
174
+ # b = ToggleButton.new ... { |checked, params|
175
+ # puts "button was checked" if checked
176
+ # # do something with params
177
+ # }
178
+ def initialize x, y, font, text, img, text_color = 0, center = true, margin_x = 0, margin_y = 0, width = nil, height = nil, params = nil, &action
179
+ super x, y, font, text, nil, text_color, center, margin_x, margin_y, width, height, params, &action
180
+ @img =
181
+ if img; Res.imgs img, 2, 3, true
182
+ else; nil; end
183
+ @w =
184
+ if img; @img[0].width
185
+ else; width; end
186
+ @h =
187
+ if img; @img[0].height
188
+ else; height; end
189
+ end
190
+
191
+ # Updates the button, checking the mouse movement and buttons to define
192
+ # the button state.
193
+ def update
194
+ super
195
+ @img_index *= 2
196
+ @img_index += 1 if @checked
197
+ end
198
+
199
+ # Executes the button click action, and toggles its state. The +action+
200
+ # block always receives as a first parameter +true+, if the button has
201
+ # been changed to checked, or +false+, otherwise.
202
+ def click
203
+ @checked = !@checked
204
+ @action.call @checked, @params
205
+ end
206
+
207
+ # Sets the state of the button to the value given.
208
+ #
209
+ # Parameters:
210
+ # [value] The state to be set (+true+ for checked, +false+ for unchecked).
211
+ def checked= value
212
+ click if value != @checked
213
+ @checked = value
214
+ end
215
+ end
216
+
144
217
  # This class represents a text field (input).
145
218
  class TextField
146
219
  # The current text inside the text field.
@@ -176,8 +249,17 @@ module AGL
176
249
  # [selection_color] The color of the rectangle highlighting selected text,
177
250
  # in hexadecimal RRGGBB format. The rectangle will
178
251
  # always be drawn with 50% of opacity.
252
+ # [params] An object containing any parameters you want passed to the
253
+ # +on_text_changed+ block. When the text of the text field is
254
+ # changed, the following is called:
255
+ # @on_text_changed.call @params
256
+ # Note that this doesn't force you to declare a block that takes
257
+ # parameters.
258
+ # [on_text_changed] The block of code executed when the text in the text
259
+ # field is changed, either by user input or by calling
260
+ # +text=+. Can be +nil+.
179
261
  def initialize x, y, font, img, cursor_img = nil, margin_x = 0, margin_y = 0, max_length = 100, active = false, text = "",
180
- allowed_chars = nil, text_color = 0, selection_color = 0
262
+ allowed_chars = nil, text_color = 0, selection_color = 0, params = nil, &on_text_changed
181
263
  @x = x
182
264
  @y = y
183
265
  @font = font
@@ -221,6 +303,9 @@ module AGL
221
303
  else
222
304
  @chars
223
305
  end
306
+
307
+ @on_text_changed = on_text_changed
308
+ @params = params
224
309
  end
225
310
 
226
311
  # Updates the text field, checking for mouse events and keyboard input.
@@ -280,7 +365,7 @@ module AGL
280
365
  inserted = false
281
366
  for i in 0..46 # alnum
282
367
  if KB.key_pressed?(@k[i]) or KB.key_held?(@k[i])
283
- remove_interval if @anchor1 and @anchor2
368
+ remove_interval true if @anchor1 and @anchor2
284
369
  if i < 26
285
370
  # bool capsLock = Console.CapsLock;
286
371
  if shift
@@ -400,6 +485,7 @@ module AGL
400
485
  @anchor1 = nil
401
486
  @anchor2 = nil
402
487
  set_cursor_visible
488
+ @on_text_changed.call @params if @on_text_changed
403
489
  end
404
490
 
405
491
  # Returns the currently selected text.
@@ -495,19 +581,18 @@ module AGL
495
581
  end
496
582
 
497
583
  def insert_char char
498
- return unless @allowed_chars.index char
499
- if @text.length < @max_length
500
- @text.insert @cur_node, char
501
- @nodes.insert @cur_node + 1, @nodes[@cur_node] + @font.text_width(char)
502
- for i in (@cur_node + 2)..(@nodes.size - 1)
503
- @nodes[i] += @font.text_width(char)
504
- end
505
- @cur_node += 1
506
- set_cursor_visible
584
+ return unless @allowed_chars.index char and @text.length < @max_length
585
+ @text.insert @cur_node, char
586
+ @nodes.insert @cur_node + 1, @nodes[@cur_node] + @font.text_width(char)
587
+ for i in (@cur_node + 2)..(@nodes.size - 1)
588
+ @nodes[i] += @font.text_width(char)
507
589
  end
590
+ @cur_node += 1
591
+ set_cursor_visible
592
+ @on_text_changed.call @params if @on_text_changed
508
593
  end
509
594
 
510
- def remove_interval
595
+ def remove_interval will_insert = false
511
596
  min = @anchor1 < @anchor2 ? @anchor1 : @anchor2
512
597
  max = min == @anchor1 ? @anchor2 : @anchor1
513
598
  interval_width = 0
@@ -523,6 +608,7 @@ module AGL
523
608
  @anchor1 = nil
524
609
  @anchor2 = nil
525
610
  set_cursor_visible
611
+ @on_text_changed.call @params if @on_text_changed and not will_insert
526
612
  end
527
613
 
528
614
  def remove_char back
@@ -534,6 +620,7 @@ module AGL
534
620
  @nodes[i] -= char_width
535
621
  end
536
622
  set_cursor_visible
623
+ @on_text_changed.call @params if @on_text_changed
537
624
  end
538
625
  end
539
626
  end
data/test/game.rb CHANGED
@@ -11,8 +11,9 @@ 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) {}
15
- @txt = TextField.new 10, 520, @font, :text, nil, 15, 5, 16, false, "", nil, 0, 0x0000ff
14
+ @btn = Button.new(10, 560, @font, "Test", :btn, 0x008000, 0, 0, 0, 0, 0, "friends") { |x| puts "hello #{x}" }
15
+ @chk = ToggleButton.new(210, 560, @font, "Click me", :check, 0xffffffff, false, 36, 5, 0, 0, "friends") { |c, x| puts "hello #{x}, checked: #{c}" }
16
+ @txt = TextField.new(10, 520, @font, :text, nil, 15, 5, 16, false, "", nil, 0, 0x0000ff, "test") { |x| puts "field #{x}" }
16
17
  end
17
18
 
18
19
  def needs_cursor?
@@ -26,6 +27,7 @@ class MyGame < Gosu::Window
26
27
  @obj1.y += 1 if KB.key_held? Gosu::KbDown
27
28
  @obj1.x -= 1 if KB.key_down? Gosu::KbLeft
28
29
  @btn.set_position rand(700), rand(550) if KB.key_pressed? Gosu::KbSpace
30
+ @chk.checked = false if KB.key_pressed? Gosu::KbEscape
29
31
  @txt.set_position rand(700), rand(550) if KB.key_pressed? Gosu::KbReturn
30
32
 
31
33
  Mouse.update
@@ -35,6 +37,7 @@ class MyGame < Gosu::Window
35
37
  end
36
38
 
37
39
  @btn.update
40
+ @chk.update
38
41
  @txt.update
39
42
  end
40
43
 
@@ -44,9 +47,10 @@ class MyGame < Gosu::Window
44
47
  @writer.write_breaking "Testing multiple line text.\nThis should draw text "\
45
48
  "across multiple lines, respecting a limit width. "\
46
49
  "Furthermore, the text must be right-aligned.",
47
- 780, 300, 300, :right
50
+ 780, 300, 300, :right, 0xffffffff
48
51
 
49
52
  @btn.draw 0xcc
53
+ @chk.draw
50
54
  @txt.draw
51
55
  end
52
56
  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: 1.2.7
4
+ version: 1.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: 2014-08-11 00:00:00.000000000 Z
11
+ date: 2014-08-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gosu
@@ -41,6 +41,7 @@ files:
41
41
  - Rakefile
42
42
  - data/font/font1.ttf
43
43
  - data/img/btn.png
44
+ - data/img/check.png
44
45
  - data/img/image.png
45
46
  - data/img/img1.png
46
47
  - data/img/text.png