minigl 1.2.3 → 1.2.4

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: a86c42ce7c6ca9f79f56802e343f9cc86444088a
4
- data.tar.gz: ac13807120b6a030599ee04df958d1bdfde33a0a
3
+ metadata.gz: 33a7e31b76d2c0a419ac03bc766d6007650129df
4
+ data.tar.gz: 2fb3a8a9df9915b64e241b09363d3191494d2297
5
5
  SHA512:
6
- metadata.gz: 2222057067a8f1db28f43be8d116605ed574beb73574eb050c1f5bde0fb23558507e1750253128c01157e6685022647eeba2dc164094a3bfdb9eff7d64effce5
7
- data.tar.gz: be9b5881dd8d36b826a2706933a22bfcae2fd2d1df41e471b36f77bfa46ed22da4ea9c71c7c69bb5a4fda0cb18a298714cdacb75fd25fd96b9bea167540f5e39
6
+ metadata.gz: 676c67bd7a8475cd4f46dab03cad823d40b20b7b1e504b3b443d074d441f72473af077cf37dffb2c4830a17c2d86289ad5fa9c6d948d32fc80977cb1cbbb988f
7
+ data.tar.gz: a680e44471368044703cd69557329ffc70340a9281c796d992c54a959ce4214b1c05e84f2ec74dfe126cddf9b2fdc3045b1a344d878e7e79d660ca265b836124
data/README.md CHANGED
@@ -21,8 +21,8 @@ this [working game example](https://github.com/victords/aventura-do-saber).
21
21
  * The [documentation](https://github.com/victords/minigl/wiki) is under
22
22
  construction.
23
23
 
24
- **Version 1.2.2**
24
+ **Version 1.2.4**
25
25
 
26
- * Adjust in `Res.font` to distinguish fonts loaded with different sizes.
27
- * Adjust in `TextField` to draw cursor image in the correct position and
28
- z-index.
26
+ * Moved color options from `draw` to `initialize` in `Button` and `TextField`.
27
+ * Added focus control to `TextField`.
28
+ * Marked some methods of various classes as private.
@@ -2,12 +2,13 @@ require_relative 'global'
2
2
 
3
3
  module AGL
4
4
  class Button
5
- def initialize x, y, font, text, img, center = true, margin_x = 0, margin_y = 0, &action
5
+ def initialize x, y, font, text, img, text_color = 0, center = true, margin_x = 0, margin_y = 0, &action
6
6
  @x = x
7
7
  @y = y
8
8
  @font = font
9
9
  @text = text
10
10
  @img = Res.imgs img, 1, 3, true
11
+ @text_color = text_color
11
12
  @w = @img[0].width
12
13
  @h = @img[0].height
13
14
  if center
@@ -62,9 +63,9 @@ module AGL
62
63
  end
63
64
  end
64
65
 
65
- def draw text_color = 0, alpha = 0xff
66
+ def draw alpha = 0xff
66
67
  color = (alpha << 24) | 0xffffff
67
- text_color = (alpha << 24) | text_color
68
+ text_color = (alpha << 24) | @text_color
68
69
  @img[@img_index].draw @x, @y, 0, 1, 1, color
69
70
  if @center
70
71
  @font.draw_rel @text, @text_x, @text_y, 0, 0.5, 0.5, 1, 1, text_color
@@ -77,7 +78,8 @@ module AGL
77
78
  class TextField
78
79
  attr_reader :text
79
80
 
80
- def initialize x, y, font, img, cursor_img = nil, text = "", margin_x = 0, margin_y = 0, max_length = 100
81
+ def initialize x, y, font, img, cursor_img = nil, margin_x = 0, margin_y = 0, max_length = 100, active = false,
82
+ text = "", text_color = 0, selection_color = 0
81
83
  @x = x
82
84
  @y = y
83
85
  @font = font
@@ -85,14 +87,17 @@ module AGL
85
87
  @w = @img.width
86
88
  @h = @img.height
87
89
  @cursor_img = Res.img(cursor_img) if cursor_img
90
+ @max_length = max_length
91
+ @active = active
88
92
  @text = text
89
93
  @text_x = x + margin_x
90
94
  @text_y = y + margin_y
91
- @max_length = max_length
95
+ @text_color = text_color
96
+ @selection_color = selection_color
92
97
 
93
98
  @nodes = [x + margin_x]
94
99
  @cur_node = 0
95
- @cursor_visible = true
100
+ @cursor_visible = false
96
101
  @cursor_timer = 0
97
102
 
98
103
  @k = [
@@ -135,30 +140,48 @@ module AGL
135
140
  @text[min..max]
136
141
  end
137
142
 
143
+ def focus
144
+ @active = true
145
+ end
146
+
138
147
  def update
139
148
  ################################ Mouse ################################
140
149
  if Mouse.over? @x, @y, @w, @h
141
- if Mouse.double_click? :left
150
+ if not @active and Mouse.button_pressed? :left
151
+ @active = true
152
+ end
153
+ elsif Mouse.button_pressed? :left
154
+ @anchor1 = @anchor2 = nil
155
+ @cursor_visible = false
156
+ @cursor_timer = 0
157
+ @active = false
158
+ end
159
+
160
+ return unless @active
161
+
162
+ if Mouse.double_click? :left
163
+ if @nodes.size > 1
142
164
  @anchor1 = 0
143
165
  @anchor2 = @nodes.size - 1
144
166
  @cur_node = @anchor2
145
- set_cursor_visible
146
- elsif Mouse.button_pressed? :left
147
- set_node_by_mouse
148
- @anchor1 = @cur_node
149
- @anchor2 = nil
150
- set_cursor_visible
167
+ @double_clicked = true
151
168
  end
152
- end
153
- if Mouse.button_down? :left
154
- if @anchor1
169
+ set_cursor_visible
170
+ elsif Mouse.button_pressed? :left
171
+ set_node_by_mouse
172
+ @anchor1 = @cur_node
173
+ @anchor2 = nil
174
+ @double_clicked = false
175
+ set_cursor_visible
176
+ elsif Mouse.button_down? :left
177
+ if @anchor1 and not @double_clicked
155
178
  set_node_by_mouse
156
179
  if @cur_node != @anchor1; @anchor2 = @cur_node
157
180
  else; @anchor2 = nil; end
158
181
  set_cursor_visible
159
182
  end
160
183
  elsif Mouse.button_released? :left
161
- if @anchor1
184
+ if @anchor1 and not @double_clicked
162
185
  if @cur_node != @anchor1; @anchor2 = @cur_node
163
186
  else; @anchor1 = nil; end
164
187
  end
@@ -282,6 +305,35 @@ module AGL
282
305
  end
283
306
  end
284
307
 
308
+ def draw alpha = 0xff
309
+ color = (alpha << 24) | 0xffffff
310
+ text_color = (alpha << 24) | @text_color
311
+ @img.draw @x, @y, 0, 1, 1, color
312
+ @font.draw @text, @text_x, @text_y, 0, 1, 1, text_color
313
+
314
+ if @anchor1 and @anchor2
315
+ selection_color = ((alpha / 2) << 24) | @selection_color
316
+ Game.window.draw_quad @nodes[@anchor1], @text_y, selection_color,
317
+ @nodes[@anchor2] + 1, @text_y, selection_color,
318
+ @nodes[@anchor2] + 1, @text_y + @font.height, selection_color,
319
+ @nodes[@anchor1], @text_y + @font.height, selection_color, 0
320
+ end
321
+
322
+ if @cursor_visible
323
+ if @cursor_img
324
+ @cursor_img.draw @nodes[@cur_node] - @cursor_img.width / 2, @text_y, 0
325
+ else
326
+ cursor_color = alpha << 24
327
+ Game.window.draw_quad @nodes[@cur_node], @text_y, cursor_color,
328
+ @nodes[@cur_node] + 1, @text_y, cursor_color,
329
+ @nodes[@cur_node] + 1, @text_y + @font.height, cursor_color,
330
+ @nodes[@cur_node], @text_y + @font.height, cursor_color, 0
331
+ end
332
+ end
333
+ end
334
+
335
+ private
336
+
285
337
  def set_cursor_visible
286
338
  @cursor_visible = true
287
339
  @cursor_timer = 0
@@ -342,31 +394,5 @@ module AGL
342
394
  end
343
395
  set_cursor_visible
344
396
  end
345
-
346
- def draw text_color = 0, selection_color = 0, alpha = 0xff
347
- color = (alpha << 24) | 0xffffff
348
- text_color = (alpha << 24) | text_color
349
- @img.draw @x, @y, 0, 1, 1, color
350
- @font.draw @text, @text_x, @text_y, 0, 1, 1, text_color
351
-
352
- if @anchor1 and @anchor2
353
- selection_color = ((alpha / 2) << 24) | selection_color
354
- Game.window.draw_quad @nodes[@anchor1], @text_y, selection_color,
355
- @nodes[@anchor2] + 1, @text_y, selection_color,
356
- @nodes[@anchor2] + 1, @text_y + @font.height, selection_color,
357
- @nodes[@anchor1], @text_y + @font.height, selection_color, 0
358
- end
359
-
360
- if @cursor_visible
361
- if @cursor_img; @cursor_img.draw @nodes[@cur_node] - @cursor_img.width / 2, @text_y, 0
362
- else
363
- cursor_color = (alpha << 24) | 0
364
- Game.window.draw_quad @nodes[@cur_node], @text_y, cursor_color,
365
- @nodes[@cur_node] + 1, @text_y, cursor_color,
366
- @nodes[@cur_node] + 1, @text_y + @font.height, cursor_color,
367
- @nodes[@cur_node], @text_y + @font.height, cursor_color, 0
368
- end
369
- end
370
- end
371
397
  end
372
398
  end
@@ -3,7 +3,7 @@ require_relative 'global'
3
3
  module AGL
4
4
  class Map
5
5
  attr_reader :tile_size, :size, :cam
6
-
6
+
7
7
  def initialize t_w, t_h, t_x_count, t_y_count, scr_w = 800, scr_h = 600
8
8
  @tile_size = Vector.new t_w, t_h
9
9
  @size = Vector.new t_x_count, t_y_count
@@ -11,39 +11,49 @@ module AGL
11
11
  @max_x = t_x_count * t_w - scr_w
12
12
  @max_y = t_y_count * t_h - scr_h
13
13
  end
14
-
14
+
15
15
  def get_absolute_size
16
16
  Vector.new(@tile_size.x * @size.x, @tile_size.y * @size.y)
17
17
  end
18
-
18
+
19
19
  def get_center
20
20
  Vector.new(@tile_size.x * @size.x / 2, @tile_size.y * @size.y / 2)
21
21
  end
22
-
22
+
23
23
  def get_screen_pos map_x, map_y
24
24
  Vector.new(map_x * @tile_size.x - @cam.x, map_y * @tile_size.y - @cam.y)
25
25
  end
26
-
26
+
27
27
  def get_map_pos scr_x, scr_y
28
28
  Vector.new((scr_x + @cam.x) / @tile_size.x, (scr_y + @cam.y) / @tile_size.y)
29
29
  end
30
-
30
+
31
31
  def is_in_map v
32
32
  v.x >= 0 && v.y >= 0 && v.x < @size.x && v.y < @size.y
33
33
  end
34
-
34
+
35
35
  def set_camera cam_x, cam_y
36
36
  @cam.x = cam_x
37
37
  @cam.y = cam_y
38
38
  set_bounds
39
39
  end
40
-
40
+
41
41
  def move_camera x, y
42
42
  @cam.x += x
43
43
  @cam.y += y
44
44
  set_bounds
45
45
  end
46
-
46
+
47
+ def foreach
48
+ for j in @min_vis_y..@max_vis_y
49
+ for i in @min_vis_x..@max_vis_x
50
+ yield i, j, i * @tile_size.x - @cam.x, j * @tile_size.y - @cam.y
51
+ end
52
+ end
53
+ end
54
+
55
+ private
56
+
47
57
  def set_bounds
48
58
  @cam.x = @cam.x.round
49
59
  @cam.y = @cam.y.round
@@ -51,31 +61,23 @@ module AGL
51
61
  @cam.x = @max_x if @cam.x > @max_x
52
62
  @cam.y = 0 if @cam.y < 0
53
63
  @cam.y = @max_y if @cam.y > @max_y
54
-
64
+
55
65
  @min_vis_x = @cam.x / @tile_size.x
56
66
  @min_vis_y = @cam.y / @tile_size.y
57
67
  @max_vis_x = (@cam.x + @cam.w - 1) / @tile_size.x
58
68
  @max_vis_y = (@cam.y + @cam.h - 1) / @tile_size.y
59
-
69
+
60
70
  if @min_vis_y < 0; @min_vis_y = 0
61
71
  elsif @min_vis_y > @size.y - 1; @min_vis_y = @size.y - 1; end
62
-
72
+
63
73
  if @max_vis_y < 0; @max_vis_y = 0
64
74
  elsif @max_vis_y > @size.y - 1; @max_vis_y = @size.y - 1; end
65
-
75
+
66
76
  if @min_vis_x < 0; @min_vis_x = 0
67
77
  elsif @min_vis_x > @size.x - 1; @min_vis_x = @size.x - 1; end
68
-
78
+
69
79
  if @max_vis_x < 0; @max_vis_x = 0
70
80
  elsif @max_vis_x > @size.x - 1; @max_vis_x = @size.x - 1; end
71
81
  end
72
-
73
- def foreach
74
- for j in @min_vis_y..@max_vis_y
75
- for i in @min_vis_x..@max_vis_x
76
- yield i, j, i * @tile_size.x - @cam.x, j * @tile_size.y - @cam.y
77
- end
78
- end
79
- end
80
82
  end
81
83
  end
@@ -173,52 +173,6 @@ module AGL
173
173
  end
174
174
  check_contact obst, ramps
175
175
  end
176
- def check_contact obst, ramps
177
- @top = @bottom = @left = @right = nil
178
- obst.each do |o|
179
- x2 = @x + @w; y2 = @y + @h; x2o = o.x + o.w; y2o = o.y + o.h
180
- @right = o if !o.passable && x2.round(6) == o.x.round(6) && y2 > o.y && @y < y2o
181
- @left = o if !o.passable && @x.round(6) == x2o.round(6) && y2 > o.y && @y < y2o
182
- @bottom = o if y2.round(6) == o.y.round(6) && x2 > o.x && @x < x2o
183
- @top = o if !o.passable && @y.round(6) == y2o.round(6) && x2 > o.x && @x < x2o
184
- end
185
- if @bottom.nil?
186
- ramps.each do |r|
187
- if r.contact? self
188
- @bottom = r
189
- break
190
- end
191
- end
192
- end
193
- end
194
- def find_right_limit coll_list
195
- limit = @x + @w + @speed.x
196
- coll_list.each do |c|
197
- limit = c.x if !c.passable && c.x < limit
198
- end
199
- limit
200
- end
201
- def find_left_limit coll_list
202
- limit = @x + @speed.x
203
- coll_list.each do |c|
204
- limit = c.x + c.w if !c.passable && c.x + c.w > limit
205
- end
206
- limit
207
- end
208
- def find_down_limit coll_list
209
- limit = @y + @h + @speed.y
210
- coll_list.each do |c|
211
- limit = c.y if c.y < limit && c.y >= @y + @h
212
- end
213
- limit
214
- end
215
- def find_up_limit coll_list
216
- limit = @y + @speed.y
217
- coll_list.each do |c|
218
- limit = c.y + c.h if !c.passable && c.y + c.h > limit
219
- end
220
- limit
221
- end
222
176
 
223
177
  def move_carrying aim, speed, obstacles
224
178
  x_d = aim.x - @x; y_d = aim.y - @y
@@ -286,5 +240,58 @@ module AGL
286
240
  end
287
241
  cur_point
288
242
  end
243
+
244
+ private
245
+
246
+ def check_contact obst, ramps
247
+ @top = @bottom = @left = @right = nil
248
+ obst.each do |o|
249
+ x2 = @x + @w; y2 = @y + @h; x2o = o.x + o.w; y2o = o.y + o.h
250
+ @right = o if !o.passable && x2.round(6) == o.x.round(6) && y2 > o.y && @y < y2o
251
+ @left = o if !o.passable && @x.round(6) == x2o.round(6) && y2 > o.y && @y < y2o
252
+ @bottom = o if y2.round(6) == o.y.round(6) && x2 > o.x && @x < x2o
253
+ @top = o if !o.passable && @y.round(6) == y2o.round(6) && x2 > o.x && @x < x2o
254
+ end
255
+ if @bottom.nil?
256
+ ramps.each do |r|
257
+ if r.contact? self
258
+ @bottom = r
259
+ break
260
+ end
261
+ end
262
+ end
263
+ end
264
+
265
+ def find_right_limit coll_list
266
+ limit = @x + @w + @speed.x
267
+ coll_list.each do |c|
268
+ limit = c.x if !c.passable && c.x < limit
269
+ end
270
+ limit
271
+ end
272
+
273
+ def find_left_limit coll_list
274
+ limit = @x + @speed.x
275
+ coll_list.each do |c|
276
+ limit = c.x + c.w if !c.passable && c.x + c.w > limit
277
+ end
278
+ limit
279
+ end
280
+
281
+ def find_down_limit coll_list
282
+ limit = @y + @h + @speed.y
283
+ coll_list.each do |c|
284
+ limit = c.y if c.y < limit && c.y >= @y + @h
285
+ end
286
+ limit
287
+ end
288
+
289
+ def find_up_limit coll_list
290
+ limit = @y + @speed.y
291
+ coll_list.each do |c|
292
+ limit = c.y + c.h if !c.passable && c.y + c.h > limit
293
+ end
294
+ limit
295
+ end
289
296
  end
290
297
  end
@@ -5,7 +5,8 @@ module AGL
5
5
  @line_spacing = line_spacing
6
6
  end
7
7
 
8
- def write_line text, x, y, mode = :left, color = 0xffffffff
8
+ def write_line text, x, y, mode = :left, color = 0, alpha = 0xff
9
+ color = (alpha << 24) | color
9
10
  rel =
10
11
  case mode
11
12
  when :left then 0
@@ -16,7 +17,8 @@ module AGL
16
17
  @font.draw_rel text, x, y, 0, rel, 0, 1, 1, color
17
18
  end
18
19
 
19
- def write_breaking text, x, y, width, mode = :left, color = 0xffffffff
20
+ def write_breaking text, x, y, width, mode = :left, color = 0, alpha = 0xff
21
+ color = (alpha << 24) | color
20
22
  text.split("\n").each do |p|
21
23
  if mode == :justified
22
24
  y = write_paragraph_justified p, x, y, width, color
@@ -33,6 +35,8 @@ module AGL
33
35
  end
34
36
  end
35
37
 
38
+ private
39
+
36
40
  def write_paragraph text, x, y, width, rel, color
37
41
  line = ""
38
42
  line_width = 0
@@ -4,15 +4,15 @@ include AGL
4
4
  class MyGame < Gosu::Window
5
5
  def initialize
6
6
  super 800, 600, false # creating a 800 x 600 window, not full screen
7
- Game.initialize self, Vector.new(0, 1), 10, 2, 20
7
+ Game.initialize self, Vector.new(0, 1), 10, 2
8
8
 
9
9
  @obj1 = GameObject.new 10, 10, 80, 80, :img1, Vector.new(-10, -10)
10
10
  @obj2 = Sprite.new 400, 0, :img1
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, false, 15, 5) {}
15
- @txt = TextField.new 10, 520, @font, :text, nil, "", 15, 5, 16
14
+ @btn = Button.new(10, 560, @font, "Test", :btn, 0x008000, false, 15, 5) {}
15
+ @txt = TextField.new 10, 520, @font, :text, nil, 15, 5, 16, false, "", 0, 0x0000ff
16
16
  end
17
17
 
18
18
  def needs_cursor?
@@ -44,8 +44,8 @@ class MyGame < Gosu::Window
44
44
  "Furthermore, the text must be right-aligned.",
45
45
  780, 300, 300, :right
46
46
 
47
- @btn.draw 0x008000, 0xcc
48
- @txt.draw 0, 0x0000ff
47
+ @btn.draw 0xcc
48
+ @txt.draw
49
49
  end
50
50
  end
51
51
 
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.3
4
+ version: 1.2.4
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-06-28 00:00:00.000000000 Z
11
+ date: 2014-07-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gosu