minigl 2.3.6 → 2.3.7

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: db8b9de89a3372e81cfdef102808b9bbaec3fbd88a930c95cd5af2f946cb744d
4
- data.tar.gz: d4fd66ee8e177e80fcbbe14808e4a8518a526fc6cec47d41adfc032da881750c
3
+ metadata.gz: 6623569ecf1fbd81357b4fe2e06b22024c2ce05829298750e1574e08a02b2706
4
+ data.tar.gz: ded142065a95df7c458d873a6c2049f4229f3fd40863872fdf19033f07331635
5
5
  SHA512:
6
- metadata.gz: c966227f4b6a7d020c5e07c601fc18d8ea6603f07f6d661bc92127ae1335b02118e71ed997c2f25fd1c9089ae0e4730d61d0395a82ef0c2e495025b25fd019c3
7
- data.tar.gz: e603e887d2c595c61e16bdeee8fd1b928414cf895caa7f98dd69b2aed0cf7bf2eb2dd0d115a460b07797d19ce3dccfeafc825151935012c62bc94671fdd90f2d
6
+ metadata.gz: 6cbb8a59c7b77104a1a8bba97d2ab740520cc6987eda4761e438b8b36fcd30b85eaacaa6caab32884118f52dadf3b763718017114db25b09b6f416dd7e1d5588
7
+ data.tar.gz: 6b3db4da549096602dc258f26101f665713422f510f5623cf7e53bdb1940226dd4a97740e44446a60554e6e1ea29abfb981355ac3caa2006e8111b32dc3aa9d2
data/README.md CHANGED
@@ -14,6 +14,14 @@ It provides the following features:
14
14
  More functionalities are coming. Feel free to contribute! You can send feedback
15
15
  to victordavidsantos@gmail.com.
16
16
 
17
+ ## Made with MiniGL
18
+
19
+ Below are two full games built with MiniGL, both available for free download and also open source.
20
+ * [Super Bombinhas](https://victords.itch.io/super-bombinhas) ([source](https://github.com/victords/super-bombinhas))
21
+ * [ConnecMan](https://victords.itch.io/connecman) ([source](https://github.com/victords/connecman))
22
+
23
+ *Super Bombinhas* is also available on [Steam](https://store.steampowered.com/app/1553840).
24
+
17
25
  ## Installing
18
26
 
19
27
  MiniGL was built on top of the Gosu gem. This gem has its own dependencies for
@@ -29,9 +37,10 @@ After installing the Gosu dependencies, you can just `gem install minigl`.
29
37
  * The [wiki](https://github.com/victords/minigl/wiki) is a work in progress with tutorials and examples.
30
38
  * Test package and examples aren't complete!
31
39
 
32
- ## Version 2.3.6
40
+ ## Version 2.3.7
33
41
 
34
- * Added inverted ramps (for sloped ceilings, still has some issues).
42
+ * Exposed the `Panel#controls`, `TextField#focused` and `DropDownList#open` properties.
43
+ * Fixed a bug when clicking overlapping buttons: only the click action of the button with highest z-index (or last updated if z-indexes are the same) will be triggered. **WARNING**: the click callback will only be executed if `Mouse.update` is called after the click happened. If you call `Mouse.update` every frame (which is recommended), there's nothing to worry about.
35
44
 
36
45
  ## Contributing
37
46
 
data/lib/minigl/forms.rb CHANGED
@@ -98,6 +98,9 @@ module MiniGL
98
98
  # Gets or sets whether the panel (and thus all components inside it) are visible.
99
99
  attr_accessor :visible
100
100
 
101
+ # The components contained in this panel.
102
+ attr_reader :controls
103
+
101
104
  # Creates a new Panel.
102
105
  # Parameters:
103
106
  # [x] The horizontal coordinate of the top-left corner of the panel, or the horizontal offset from the anchor, if provided.
@@ -348,8 +351,10 @@ module MiniGL
348
351
  @img_index = 0
349
352
  @state = :up
350
353
  elsif mouse_press
351
- @img_index = 2
352
- @state = :down
354
+ Mouse.add_click(@z_index || 0, lambda do
355
+ @img_index = 2
356
+ @state = :down
357
+ end)
353
358
  else
354
359
  @img_index = 1
355
360
  end
@@ -360,7 +365,7 @@ module MiniGL
360
365
  elsif mouse_rel
361
366
  @img_index = 1
362
367
  @state = :over
363
- click
368
+ Mouse.add_click(@z_index || 0, method(:perform_action))
364
369
  else
365
370
  @img_index = 2
366
371
  end
@@ -379,7 +384,7 @@ module MiniGL
379
384
 
380
385
  # Executes the button click action.
381
386
  def click
382
- @action.call @params if @action
387
+ perform_action
383
388
  end
384
389
 
385
390
  # Sets the position of the button in the screen.
@@ -404,6 +409,7 @@ module MiniGL
404
409
  # will be drawn on top of the ones with smaller z-orders.
405
410
  # [color] Color to apply a filter to the image.
406
411
  def draw(alpha = 0xff, z_index = 0, color = 0xffffff)
412
+ @z_index = z_index
407
413
  return unless @visible
408
414
 
409
415
  color = (alpha << 24) | color
@@ -435,6 +441,12 @@ module MiniGL
435
441
  @state = :up
436
442
  @img_index = 3
437
443
  end
444
+
445
+ private
446
+
447
+ def perform_action
448
+ @action.call @params if @action
449
+ end
438
450
  end
439
451
 
440
452
  # This class represents a toggle button, which can be also interpreted as a
@@ -517,20 +529,12 @@ module MiniGL
517
529
  @img_index += 1 if @checked
518
530
  end
519
531
 
520
- # Executes the button click action, and toggles its state. The +action+
521
- # block always receives as a first parameter +true+, if the button has
522
- # been changed to checked, or +false+, otherwise.
523
- def click
524
- @checked = !@checked
525
- @action.call @checked, @params if @action
526
- end
527
-
528
532
  # Sets the state of the button to the value given.
529
533
  #
530
534
  # Parameters:
531
535
  # [value] The state to be set (+true+ for checked, +false+ for unchecked).
532
536
  def checked=(value)
533
- click if value != @checked
537
+ @action.call(value, @params) if @action && value != @checked
534
538
  @checked = value
535
539
  end
536
540
 
@@ -539,6 +543,13 @@ module MiniGL
539
543
  @state = :up
540
544
  @img_index = @checked ? 7 : 6
541
545
  end
546
+
547
+ private
548
+
549
+ def perform_action
550
+ @checked = !@checked
551
+ @action.call(@checked, @params) if @action
552
+ end
542
553
  end
543
554
 
544
555
  # This class represents a text field (input).
@@ -547,6 +558,9 @@ module MiniGL
547
558
  # INCOMPLETE!
548
559
  attr_reader :locale
549
560
 
561
+ # Whether the text field is focused (accepting input)
562
+ attr_reader :focused
563
+
550
564
  # Creates a new text field.
551
565
  #
552
566
  # Parameters:
@@ -566,9 +580,9 @@ module MiniGL
566
580
  # [margin_x] The x offset, from the field x-coordinate, to draw the text.
567
581
  # [margin_y] The y offset, from the field y-coordinate, to draw the text.
568
582
  # [max_length] The maximum length of the text inside the field.
569
- # [active] Whether the text field must be focused by default. If +false+,
570
- # focus can be granted by clicking inside the text field or by
571
- # calling the +focus+ method.
583
+ # [focused] Whether the text field must be focused by default. If +false+,
584
+ # focus can be granted by clicking inside the text field or by
585
+ # calling the +focus+ method.
572
586
  # [text] The starting text. Must not be +nil+.
573
587
  # [allowed_chars] A string containing all characters that can be typed
574
588
  # inside the text field. The complete set of supported
@@ -606,7 +620,7 @@ module MiniGL
606
620
  # *Obs.:* This method accepts named parameters, but +x+, +y+, +font+ and
607
621
  # +img+ are mandatory.
608
622
  def initialize(x, y = nil, font = nil, img = nil, cursor_img = nil, disabled_img = nil, margin_x = 0, margin_y = 0,
609
- max_length = 100, active = false, text = '', allowed_chars = nil,
623
+ max_length = 100, focused = false, text = '', allowed_chars = nil,
610
624
  text_color = 0, disabled_text_color = 0, selection_color = 0, locale = 'en-us',
611
625
  params = nil, retro = nil, scale_x = 1, scale_y = 1, anchor = nil, &on_text_changed)
612
626
  if x.is_a? Hash
@@ -618,7 +632,7 @@ module MiniGL
618
632
  margin_x = x.fetch(:margin_x, 0)
619
633
  margin_y = x.fetch(:margin_y, 0)
620
634
  max_length = x.fetch(:max_length, 100)
621
- active = x.fetch(:active, false)
635
+ focused = x.fetch(:focused, false)
622
636
  text = x.fetch(:text, '')
623
637
  allowed_chars = x.fetch(:allowed_chars, nil)
624
638
  text_color = x.fetch(:text_color, 0)
@@ -647,7 +661,7 @@ module MiniGL
647
661
  @cursor_img = Res.img(cursor_img, false, false, '.png', retro) if cursor_img
648
662
  @disabled_img = Res.img(disabled_img, false, false, '.png', retro) if disabled_img
649
663
  @max_length = max_length
650
- @active = active
664
+ @focused = focused
651
665
  @text_x = x + margin_x * @scale_x
652
666
  @text_y = y + margin_y * @scale_y
653
667
  @selection_color = selection_color
@@ -690,14 +704,14 @@ module MiniGL
690
704
 
691
705
  ################################ Mouse ################################
692
706
  if Mouse.over? @x, @y, @w, @h
693
- if not @active and Mouse.button_pressed? :left
694
- focus
707
+ if not @focused and Mouse.button_pressed? :left
708
+ Mouse.add_click(@z_index || 0, method(:focus))
695
709
  end
696
710
  elsif Mouse.button_pressed? :left
697
711
  unfocus
698
712
  end
699
713
 
700
- return unless @active
714
+ return unless @focused
701
715
 
702
716
  if Mouse.double_click? :left
703
717
  if @nodes.size > 1
@@ -708,11 +722,7 @@ module MiniGL
708
722
  end
709
723
  set_cursor_visible
710
724
  elsif Mouse.button_pressed? :left
711
- set_node_by_mouse
712
- @anchor1 = @cur_node
713
- @anchor2 = nil
714
- @double_clicked = false
715
- set_cursor_visible
725
+ Mouse.add_click(@z_index || 0, method(:focus))
716
726
  elsif Mouse.button_down? :left
717
727
  if @anchor1 and not @double_clicked
718
728
  set_node_by_mouse
@@ -902,7 +912,12 @@ module MiniGL
902
912
 
903
913
  # Grants focus to the text field, so that it allows keyboard input.
904
914
  def focus
905
- @active = true
915
+ @focused = true
916
+ set_node_by_mouse
917
+ @anchor1 = @cur_node
918
+ @anchor2 = nil
919
+ @double_clicked = false
920
+ set_cursor_visible
906
921
  end
907
922
 
908
923
  # Removes focus from the text field, so that no keyboard input will be
@@ -911,7 +926,7 @@ module MiniGL
911
926
  @anchor1 = @anchor2 = nil
912
927
  @cursor_visible = false
913
928
  @cursor_timer = 0
914
- @active = false
929
+ @focused = false
915
930
  end
916
931
 
917
932
  # Sets the position of the text field in the screen.
@@ -941,6 +956,7 @@ module MiniGL
941
956
  # [disabled_color] Color to apply a filter to the image when the field is
942
957
  # disabled.
943
958
  def draw(alpha = 0xff, z_index = 0, color = 0xffffff, disabled_color = 0x808080)
959
+ @z_index = z_index
944
960
  return unless @visible
945
961
 
946
962
  color = (alpha << 24) | ((@enabled or @disabled_img) ? color : disabled_color)
@@ -1250,6 +1266,9 @@ module MiniGL
1250
1266
  # The selected value in the drop-down list. This is one of the +options+.
1251
1267
  attr_reader :value
1252
1268
 
1269
+ # Whether the list of options is currently visible.
1270
+ attr_reader :open
1271
+
1253
1272
  # An array containing all the options (each of them +String+s) that can be
1254
1273
  # selected in the drop-down list.
1255
1274
  attr_accessor :options
data/lib/minigl/global.rb CHANGED
@@ -397,6 +397,11 @@ module MiniGL
397
397
  @down.clear
398
398
  @dbl_click.clear
399
399
 
400
+ if @click
401
+ @click[:action].call
402
+ @click = nil
403
+ end
404
+
400
405
  @dbl_click_timer.each do |k, v|
401
406
  if v < G.double_click_delay; @dbl_click_timer[k] += 1
402
407
  else; @dbl_click_timer.delete k; end
@@ -474,6 +479,13 @@ module MiniGL
474
479
  return @x >= x.x && @x < x.x + x.w && @y >= x.y && @y < x.y + x.h if x.is_a? Rectangle
475
480
  @x >= x && @x < x + w && @y >= y && @y < y + h
476
481
  end
482
+
483
+ # :nodoc:
484
+ def add_click(z_index, action)
485
+ return if @click && @click[:z_index] > z_index
486
+
487
+ @click = { z_index: z_index, action: action }
488
+ end
477
489
  end
478
490
  end
479
491
 
data/test/game.rb CHANGED
@@ -32,11 +32,12 @@ class MyGame < GameWindow
32
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
+ @btn2 = Button.new(x: 0, y: 80, font: @font1, text: 'Below', img: :btn, anchor: :north) { puts 'Below the dropdown' }
35
36
 
36
37
  @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: 5, font: @font1, text: 'Opa', img: :text, margin_x: 5, margin_y: 5, anchor: :top_left),
39
+ Button.new(x: 5, y: 5, font: @font1, text: 'Teste', img: :btn) { puts 'top left' },
38
40
  @lbl = Label.new(0, 70, @font1, 'Teste de label', 0, 0x666666, 1, 1, :north),
39
- TextField.new(x: 5, y: 40, font: @font1, text: 'Opa', img: :text, margin_x: 5, margin_y: 5, anchor: :top_left),
40
41
  Button.new(x: 0, y: 5, font: @font1, text: 'Teste', img: :btn, anchor: :top),
41
42
  DropDownList.new(x: 0, y: 40, width: 150, height: 25, font: @font1, options: ['olá amigos', 'opção 2', 'terceira'], anchor: :north),
42
43
  Button.new(x: 5, y: 5, font: @font1, text: 'Teste', img: :btn, anchor: :northeast),
@@ -44,7 +45,7 @@ class MyGame < GameWindow
44
45
  Button.new(x: 0, y: 0, font: @font1, text: 'Teste', img: :btn, anchor: :center),
45
46
  Button.new(x: 5, y: 0, font: @font1, text: 'Teste', img: :btn, anchor: :right),
46
47
  ToggleButton.new(x: 5, y: 40, img: :check, center_x: false, margin_x: 36, anchor: :east),
47
- Button.new(x: 5, y: 5, font: @font1, text: 'Teste', img: :btn, anchor: :southwest),
48
+ Button.new(x: 5, y: 5, font: @font1, text: 'Teste', img: :btn, anchor: :southwest) { @lbl.visible = !@lbl.visible },
48
49
  Button.new(x: 0, y: 5, font: @font1, text: 'Teste', img: :btn, anchor: :south),
49
50
  ProgressBar.new(0, 40, 200, 20, :barbg, :barfg, 3456, 70, 2, 2, @font1, 0xff000080, nil, nil, 1, 1, :bottom)
50
51
  ], :text, :tiled, true, 2, 2, :bottom_right)
@@ -80,7 +81,11 @@ class MyGame < GameWindow
80
81
  @panel.enabled = !@panel.enabled if KB.key_pressed? Gosu::KbN
81
82
  @panel.visible = !@panel.visible if KB.key_pressed? Gosu::KbM
82
83
 
83
- @panel.add_component(Button.new(x: 5, y: 5, font: @font1, text: 'Teste', img: :btn, anchor: :southeast)) if KB.key_pressed?(Gosu::KbB)
84
+ if KB.key_pressed?(Gosu::KbB)
85
+ @panel.add_component(Button.new(x: 5, y: 5, font: @font1, text: 'Teste', img: :btn, anchor: :southeast) {
86
+ puts 'new button added'
87
+ })
88
+ end
84
89
  @lbl.text = 'Test of changed text' if KB.key_pressed?(Gosu::KB_C)
85
90
  @lbl2.text = 'Shorter text' if KB.key_pressed?(Gosu::KB_X)
86
91
 
@@ -112,6 +117,7 @@ class MyGame < GameWindow
112
117
  @chk.update
113
118
  @txt.update
114
119
  @ddl.update
120
+ @btn2.update
115
121
 
116
122
  @panel.update
117
123
 
@@ -145,6 +151,7 @@ class MyGame < GameWindow
145
151
  780, 450, 300, :right, 0xff0000, 255, 1
146
152
 
147
153
  @ddl.draw 0x80, 1, 0xff8080
154
+ @btn2.draw
148
155
  @btn.draw 0xcc, 1, 0x33ff33
149
156
  @chk.draw
150
157
  @txt.draw
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.3.6
4
+ version: 2.3.7
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: 2021-10-02 00:00:00.000000000 Z
11
+ date: 2021-12-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gosu