wads 0.2.1 → 0.2.2

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: abb19e4e8651f417126dac1b5f6d684d366924f13596e220395fb2c5106b8912
4
- data.tar.gz: e8e414d75ac5941bf934888ada51241a74728f186a6df60247f13499601e37cb
3
+ metadata.gz: 51b79bd0bfafd0b02a32240f3410d4d5fcfdef5938f1a9fe23e4d89914f778d0
4
+ data.tar.gz: 8ab8279ec6ed32b2c64b5f637d9c6db13e5fd7f157d24ea6c8d59ee4818649d5
5
5
  SHA512:
6
- metadata.gz: 94bf5046023232e7c4f9b46f7e8032c7c3ebd97ca9711a3423a3e20d42c23b1e3b39d3b8cd73dec4be8bc51c00bd685b41901fde8250576fffcba602cdf36b4b
7
- data.tar.gz: 3c34a400278a5e2db1afb161bad83ac81375084f40c5928b63bdb6ddfc3446cbed7f7bdead0307c919612f515aedacc2ed7e02b61c5b3cf7b85766739772bcd3
6
+ metadata.gz: 9f0e2d5d176468c106c290845767d27dc520bcd5009e4d30d4caa5c0e4bb93c4dfe861084660d0dd6ddd2f401f3fe274fc3816e1febc5a64808b4291199ef1a8
7
+ data.tar.gz: 14120137ff1e6a7398b3ae1a858127bfd37a072df1485777c36aab4888e3e7fd412a8f43e949efbdd766c24d51d23d0de93b82ae92cc88b2f6a32aa5c9a33619
data/lib/wads/app.rb CHANGED
@@ -16,6 +16,7 @@ module Wads
16
16
  WadsConfig.instance.set_window(self)
17
17
  set_display(widget)
18
18
  WadsConfig.instance.set_log_level("info")
19
+ @registered_hold_down_buttons = []
19
20
  end
20
21
 
21
22
  #
@@ -30,8 +31,23 @@ module Wads
30
31
  @main_widget
31
32
  end
32
33
 
34
+ # Register a key (identified by the Gosu id) to check if it is being held down.
35
+ # If so, the handle_key_held_down callback will be invoked on widgets
36
+ # For example, register_hold_down_key(Gosu::KbLeft)
37
+ def register_hold_down_key(id)
38
+ @registered_hold_down_buttons << id
39
+ end
40
+
33
41
  def update
34
42
  @main_widget.update(@update_count, mouse_x, mouse_y)
43
+
44
+ # Look for keys that are held down and delegate those events
45
+ @registered_hold_down_buttons.each do |id|
46
+ if button_down?(id)
47
+ @main_widget.handle_key_held_down id, mouse_x, mouse_y
48
+ end
49
+ end
50
+
35
51
  @update_count = @update_count + 1
36
52
  end
37
53
 
data/lib/wads/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Wads
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
data/lib/wads/widgets.rb CHANGED
@@ -805,26 +805,6 @@ module Wads
805
805
  end
806
806
  container.get_coordinates(element_type, args)
807
807
  end
808
-
809
- #
810
- # This is a convenience method that creates a panel divided into a left and right,
811
- # or east and west section. It will take up the entire space of the specified
812
- # ARG_SECTION in the args map.
813
- #
814
- def add_east_west_panel(args)
815
- section = args[ARG_SECTION]
816
- if section.nil?
817
- raise "East west panels require the arg '#{ARG_SECTION}' with value #{@container_map.keys.join(', ')}"
818
- end
819
- container = @container_map[section]
820
- new_panel = Panel.new(container.start_x, container.start_y,
821
- container.max_width, container.max_height)
822
- new_panel.set_layout(LAYOUT_EAST_WEST, args)
823
- new_panel.base_z = @parent_widget.base_z
824
- new_panel.disable_border
825
- @parent_widget.add_child(new_panel)
826
- new_panel
827
- end
828
808
  end
829
809
 
830
810
  # The layout sections are as follows:
@@ -841,9 +821,13 @@ module Wads
841
821
  super
842
822
  # Divide the height into 100, 100, and the middle gets everything else
843
823
  # Right now we are using 100 pixels rather than a percentage for the borders
844
- middle_section_y_start = y + 100
845
- height_middle_section = height - 100
846
- @container_map[SECTION_NORTH] = GuiContainer.new(x, y, width, 100)
824
+ header_section_height = 100
825
+ if args[ARG_DESIRED_HEIGHT]
826
+ header_section_height = args[ARG_DESIRED_HEIGHT]
827
+ end
828
+ middle_section_y_start = y + header_section_height
829
+ height_middle_section = height - header_section_height
830
+ @container_map[SECTION_NORTH] = GuiContainer.new(x, y, width, header_section_height)
847
831
  @container_map[SECTION_CENTER] = GuiContainer.new(x, middle_section_y_start, width, height_middle_section, FILL_VERTICAL_STACK)
848
832
  end
849
833
  end
@@ -1035,6 +1019,14 @@ module Wads
1035
1019
  @text_input_fields = []
1036
1020
  end
1037
1021
 
1022
+ def pad(str, size, left_align = false)
1023
+ str = str.to_s
1024
+ if left_align
1025
+ str[0, size].ljust(size, ' ')
1026
+ else
1027
+ str[0, size].rjust(size, ' ')
1028
+ end
1029
+ end
1038
1030
  def debug(message)
1039
1031
  WadsConfig.instance.get_logger.debug message
1040
1032
  end
@@ -1077,8 +1069,10 @@ module Wads
1077
1069
  end
1078
1070
 
1079
1071
  def add_panel(section, args = {})
1080
- get_layout.add_max_panel({ ARG_SECTION => section,
1081
- ARG_THEME => @gui_theme}.merge(args))
1072
+ new_panel = get_layout.add_max_panel({ ARG_SECTION => section,
1073
+ ARG_THEME => @gui_theme}.merge(args))
1074
+ new_panel.disable_border
1075
+ new_panel
1082
1076
  end
1083
1077
 
1084
1078
  def get_theme
@@ -1704,6 +1698,14 @@ module Wads
1704
1698
  # empty base implementation
1705
1699
  end
1706
1700
 
1701
+ #
1702
+ # This callback is invoked for any key registered by the
1703
+ # register_hold_down_key(id) method.
1704
+ #
1705
+ def handle_key_held_down id, mouse_x, mouse_y
1706
+ # empty base implementation
1707
+ end
1708
+
1707
1709
  #
1708
1710
  # Override this method in your subclass to perform any logic needed
1709
1711
  # as part of the main Gosu update loop. In most cases, this method is
@@ -1764,6 +1766,9 @@ module Wads
1764
1766
  @img = Gosu::Image.new(image)
1765
1767
  elsif image.is_a? Gosu::Image
1766
1768
  @img = image
1769
+ elsif image.is_a? Gosu::Color
1770
+ @img = nil
1771
+ @override_color = image
1767
1772
  else
1768
1773
  raise "ImageWidget requires either a filename or a Gosu::Image object"
1769
1774
  end
@@ -1773,11 +1778,16 @@ module Wads
1773
1778
  @scale = 1
1774
1779
  disable_border
1775
1780
  disable_background
1776
- set_dimensions(@img.width, @img.height)
1781
+ set_dimensions(@img.width, @img.height) if @img
1777
1782
  end
1778
1783
 
1779
1784
  def render
1780
- @img.draw @x, @y, z_order, @scale, @scale
1785
+ if @img.nil?
1786
+ # TODO draw a box
1787
+ Gosu::draw_rect(@x, @y, @width - 1, @height - 1, @override_color, relative_z_order(Z_ORDER_GRAPHIC_ELEMENTS))
1788
+ else
1789
+ @img.draw @x, @y, z_order, @scale, @scale
1790
+ end
1781
1791
  end
1782
1792
 
1783
1793
  def widget_z
Binary file
Binary file
Binary file
@@ -0,0 +1,34 @@
1
+ require 'gosu'
2
+
3
+ class AppWindow < Gosu::Window
4
+ def initialize
5
+ super(800, 600)
6
+ self.caption = 'Bouncing Ball'
7
+ @img = Gosu::Image.new("media/CircleYellow.png")
8
+ @x = 400
9
+ @y = 300
10
+ @delta_x = 2
11
+ @delta_y = 2
12
+ end
13
+
14
+ def update
15
+ @x = @x + @delta_x
16
+ @y = @y + @delta_y
17
+ if @x < 0
18
+ @delta_x = 2
19
+ elsif @x > 800 - @img.width
20
+ @delta_x = -2
21
+ end
22
+ if @y < 0
23
+ @delta_y = 2
24
+ elsif @y > 600 - @img.height
25
+ @delta_y = -2
26
+ end
27
+ end
28
+
29
+ def draw
30
+ @img.draw @x, @y, 1
31
+ end
32
+ end
33
+
34
+ AppWindow.new.show
@@ -0,0 +1,16 @@
1
+ require 'gosu'
2
+
3
+ class GameWindow < Gosu::Window
4
+ def initialize
5
+ super(400, 300)
6
+ self.caption = 'Hello World App'
7
+ @font = Gosu::Font.new(24)
8
+ end
9
+
10
+ def draw
11
+ @font.draw_text("Hello World", 144, 120, 1)
12
+ end
13
+ end
14
+
15
+ window = GameWindow.new
16
+ window.show
@@ -0,0 +1,121 @@
1
+ require 'gosu'
2
+
3
+ AVERAGE_RESPONSE_TIME = 284.to_f # Per humanbenchmark.com, in milliseconds
4
+ GAME_STATE_PROMPT_TO_START = 0
5
+ GAME_STATE_RED = 1
6
+ GAME_STATE_YELLOW = 2
7
+ GAME_STATE_GREEN = 3
8
+ GAME_STATE_OVER = 4
9
+
10
+ class AppWindow < Gosu::Window
11
+ def initialize
12
+ super(600, 300)
13
+ self.caption = 'Reaction Time Game'
14
+ @update_count = 0
15
+ @next_light_count = 0
16
+ @messages = []
17
+ @font = Gosu::Font.new(32)
18
+ @red_light = Gosu::Image.new("media/CircleRed.png")
19
+ @yellow_light = Gosu::Image.new("media/CircleYellow.png")
20
+ @green_light = Gosu::Image.new("media/CircleGreen.png")
21
+ # game state 0 1 2 3 4
22
+ # state Prompt Red Yellow Green Game over
23
+ @traffic_light_images = [@red_light, @red_light, @yellow_light, @green_light, @green_light]
24
+ @traffic_light_color = GAME_STATE_PROMPT_TO_START
25
+ end
26
+
27
+ def update
28
+ @update_count = @update_count + 1
29
+ @traffic_light_image = @traffic_light_images[@traffic_light_color]
30
+
31
+ # Once the game is started, the lights progress automatically until green
32
+ if @traffic_light_color > GAME_STATE_PROMPT_TO_START
33
+ next_traffic_light unless @update_count < @next_light_count
34
+ @button_text = "Click when the light turns green" unless @traffic_light_color > GAME_STATE_GREEN
35
+ end
36
+ end
37
+
38
+ def draw
39
+ @font.draw_text("Reaction Time Game", 180, 10, 1)
40
+ @traffic_light_image.draw(248, 60, 1)
41
+
42
+ if @traffic_light_color == GAME_STATE_PROMPT_TO_START
43
+ @font.draw_text("Press 'p' to play", 200, 200, 1)
44
+ elsif @traffic_light_color == GAME_STATE_OVER
45
+ @font.draw_text("Press 'p' to play again", 50, 200, 1)
46
+ else
47
+ draw_button(@button_text) unless @button_text.nil?
48
+ end
49
+
50
+ y = 232
51
+ @messages.each do |msg|
52
+ @font.draw_text(msg, 50, y, 1)
53
+ y = y + 32
54
+ end
55
+ end
56
+
57
+ def button_down id
58
+ close if id == Gosu::KbEscape
59
+
60
+ if @traffic_light_color == GAME_STATE_PROMPT_TO_START or @traffic_light_color == GAME_STATE_OVER
61
+ if id == Gosu::KbP
62
+ next_traffic_light
63
+ end
64
+ return
65
+ end
66
+
67
+ if id == Gosu::MsLeft and button_contains_click
68
+ if @traffic_light_color < GAME_STATE_YELLOW
69
+ next_traffic_light
70
+ elsif @traffic_light_color == GAME_STATE_YELLOW
71
+ @traffic_light_color = GAME_STATE_OVER
72
+ @messages = ["Sorry, you were too early"]
73
+ @button_text = "Click to play again"
74
+ elsif @traffic_light_color == GAME_STATE_GREEN
75
+ @button_text = nil
76
+ time_since_green = ((Time.now - @mark_time) * 1000.to_f).round(3)
77
+ @messages = ["Response time: #{time_since_green} ms"]
78
+ diff_from_average = (((time_since_green - AVERAGE_RESPONSE_TIME) / AVERAGE_RESPONSE_TIME) * 100).round
79
+ if diff_from_average > 0
80
+ @messages << "#{diff_from_average}% slower than the average human"
81
+ elsif diff_from_average < 0
82
+ @messages << "#{-diff_from_average}% faster than the average human"
83
+ else
84
+ @messages << "Wow, that is exactly the human average."
85
+ end
86
+ @button_text = "Click to play again"
87
+ @traffic_light_color = GAME_STATE_OVER
88
+ else
89
+ @traffic_light_color = GAME_STATE_PROMPT_TO_START
90
+ @messages = []
91
+ end
92
+ end
93
+ end
94
+
95
+ def next_traffic_light
96
+ return unless @traffic_light_color < GAME_STATE_GREEN
97
+ @traffic_light_color = @traffic_light_color + 1
98
+ @mark_time = Time.now
99
+ @next_light_count = @update_count + 60 + rand(100)
100
+ end
101
+
102
+ def button_contains_click
103
+ mouse_x > 100 and mouse_x < 500 and mouse_y > 198 and mouse_y < 232
104
+ end
105
+
106
+ def draw_button(text)
107
+ text_width = @font.text_width(text)
108
+ draw_box(100, 198, 500, 232)
109
+ text_x = (600 - text_width) / 2
110
+ @font.draw_text(@button_text, text_x, 200, 1)
111
+ end
112
+
113
+ def draw_box(x1, y1, x2, y2, color = Gosu::Color::WHITE)
114
+ Gosu::draw_line x1, y1, color, x2, y1, color, 1
115
+ Gosu::draw_line x1, y1, color, x1, y2, color, 1
116
+ Gosu::draw_line x1, y2, color, x2, y2, color, 1
117
+ Gosu::draw_line x2, y1, color, x2, y2, color, 1
118
+ end
119
+ end
120
+
121
+ AppWindow.new.show
data/samples/stocks.rb CHANGED
@@ -122,5 +122,5 @@ class StocksDisplay < Widget
122
122
  end
123
123
  end
124
124
 
125
- WadsConfig.instance.set_current_theme(WadsBrightTheme.new)
125
+ WadsConfig.instance.set_current_theme(WadsAquaTheme.new)
126
126
  SampleStocksApp.new.show
@@ -0,0 +1,117 @@
1
+ require 'gosu'
2
+ require_relative '../lib/wads'
3
+
4
+ include Wads
5
+
6
+ AVERAGE_RESPONSE_TIME = 284.to_f # Per humanbenchmark.com, in milliseconds
7
+ GAME_STATE_PROMPT_TO_START = 0
8
+ GAME_STATE_RED = 1
9
+ GAME_STATE_YELLOW = 2
10
+ GAME_STATE_GREEN = 3
11
+ GAME_STATE_OVER = 4
12
+
13
+
14
+ class ReactionTimeApp < WadsApp
15
+ def initialize
16
+ super(600, 400, "Reaction Time Game", ReactionTimeDisplay.new)
17
+ end
18
+ end
19
+
20
+ class ReactionTimeDisplay < Widget
21
+ def initialize
22
+ super(0, 0, 600, 400)
23
+ set_layout(LAYOUT_HEADER_CONTENT)
24
+ set_theme(WadsAquaTheme.new)
25
+ disable_border
26
+ @next_light_count = 0
27
+ @messages = []
28
+ @font = Gosu::Font.new(22)
29
+ @red_light = Gosu::Image.new("media/RedLight.png")
30
+ @yellow_light = Gosu::Image.new("media/YellowLight.png")
31
+ @green_light = Gosu::Image.new("media/GreenLight.png")
32
+ # game state 0 1 2 3 4
33
+ # state Prompt Red Yellow Green Game over
34
+ @traffic_light_images = [@red_light, @red_light, @yellow_light, @green_light, @green_light]
35
+ @traffic_light_color = GAME_STATE_PROMPT_TO_START
36
+
37
+ add_panel(SECTION_NORTH).get_layout.add_text("Reaction Time Game",
38
+ { ARG_TEXT_ALIGN => TEXT_ALIGN_CENTER,
39
+ ARG_USE_LARGE_FONT => true})
40
+
41
+ content_panel = get_layout.add_max_panel({ARG_SECTION => SECTION_CENTER,
42
+ ARG_LAYOUT => LAYOUT_EAST_WEST,
43
+ ARG_THEME => WadsAquaTheme.new,
44
+ ARG_PANEL_WIDTH => 140})
45
+ @traffic_light_image = content_panel.get_layout.add_image(@red_light,
46
+ {ARG_SECTION => SECTION_WEST})
47
+
48
+ left_side_panel = content_panel.add_panel(SECTION_EAST)
49
+ left_side_panel.disable_border
50
+ @start_button = left_side_panel.get_layout.add_button("Click to play") do
51
+ handle_button_click
52
+ end
53
+ @messages = left_side_panel.get_layout.add_document("")
54
+ end
55
+
56
+ def handle_update update_count, mouse_x, mouse_y
57
+ @traffic_light_image.img = @traffic_light_images[@traffic_light_color]
58
+
59
+ if @traffic_light_color == GAME_STATE_PROMPT_TO_START or @traffic_light_color == GAME_STATE_OVER
60
+ @start_button.visible = true
61
+
62
+ elsif @traffic_light_color == GAME_STATE_RED or
63
+ @traffic_light_color == GAME_STATE_YELLOW or
64
+ @traffic_light_color == GAME_STATE_GREEN
65
+ @messages.lines = ["Hit the space bar when the light turns green"]
66
+ @start_button.visible = false
67
+ if @mark_time.nil?
68
+ @mark_time = Time.now
69
+ @next_light_count = update_count + 60 + rand(100)
70
+ end
71
+ next_traffic_light unless update_count < @next_light_count
72
+ end
73
+ end
74
+
75
+ def handle_button_click
76
+ if @traffic_light_color == GAME_STATE_PROMPT_TO_START or @traffic_light_color == GAME_STATE_OVER
77
+ @messages.lines = []
78
+ next_traffic_light
79
+ end
80
+ end
81
+
82
+ def handle_key_press id, mouse_x, mouse_y
83
+ return WidgetResult.new(true) if id == Gosu::KbEscape
84
+
85
+ if id == Gosu::KbSpace
86
+ if @traffic_light_color < GAME_STATE_YELLOW
87
+ next_traffic_light
88
+ elsif @traffic_light_color == GAME_STATE_YELLOW
89
+ @traffic_light_color = GAME_STATE_OVER
90
+ @messages.lines = ["Sorry, you were too early"]
91
+ elsif @traffic_light_color == GAME_STATE_GREEN
92
+ time_since_green = ((Time.now - @mark_time) * 1000.to_f).round(3)
93
+ @messages.lines = ["Response time: #{time_since_green} ms"]
94
+ diff_from_average = (((time_since_green - AVERAGE_RESPONSE_TIME) / AVERAGE_RESPONSE_TIME) * 100).round
95
+ if diff_from_average > 0
96
+ @messages.lines << "#{diff_from_average}% slower than the average human"
97
+ elsif diff_from_average < 0
98
+ @messages.lines << "#{-diff_from_average}% faster than the average human"
99
+ else
100
+ @messages.lines << "Wow, that is exactly the human average."
101
+ end
102
+ @traffic_light_color = GAME_STATE_OVER
103
+ end
104
+ end
105
+ end
106
+
107
+ def next_traffic_light
108
+ if @traffic_light_color == GAME_STATE_OVER
109
+ @traffic_light_color = GAME_STATE_RED
110
+ elsif @traffic_light_color < GAME_STATE_OVER
111
+ @traffic_light_color = @traffic_light_color + 1
112
+ end
113
+ @mark_time = nil
114
+ end
115
+ end
116
+
117
+ ReactionTimeApp.new.show
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wads
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - dbroemme
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-10-09 00:00:00.000000000 Z
11
+ date: 2021-10-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gosu
@@ -87,18 +87,25 @@ files:
87
87
  - media/CircleRed.png
88
88
  - media/CircleWhite.png
89
89
  - media/CircleYellow.png
90
+ - media/GreenLight.png
91
+ - media/RedLight.png
90
92
  - media/SampleGraph.png
91
93
  - media/StocksSample.png
92
94
  - media/WadsScreenshot.png
95
+ - media/YellowLight.png
93
96
  - run-graph
94
97
  - run-star-wars
95
98
  - run-stocks
96
99
  - run-theme-test
97
100
  - samples/basic_gosu_with_graph_widget.rb
101
+ - samples/gosu_bouncing_ball.rb
102
+ - samples/gosu_hello_world.rb
103
+ - samples/gosu_reaction_time.rb
98
104
  - samples/graph.rb
99
105
  - samples/star_wars.rb
100
106
  - samples/stocks.rb
101
107
  - samples/theme_test.rb
108
+ - samples/wads_reaction_time.rb
102
109
  - wads.gemspec
103
110
  homepage: https://github.com/dbroemme/ruby-wads
104
111
  licenses: []