wads 0.1.3 → 0.2.0
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/CHANGELOG.md +7 -0
- data/README.md +11 -0
- data/data/sample_graph.csv +11 -0
- data/lib/wads/app.rb +40 -332
- data/lib/wads/data_structures.rb +236 -19
- data/lib/wads/textinput.rb +20 -14
- data/lib/wads/version.rb +1 -1
- data/lib/wads/widgets.rb +2142 -212
- data/lib/wads.rb +1 -1
- data/media/CircleAlpha.png +0 -0
- data/media/CircleAqua.png +0 -0
- data/media/CircleBlue.png +0 -0
- data/media/CircleGray.png +0 -0
- data/media/CircleGreen.png +0 -0
- data/media/CirclePurple.png +0 -0
- data/media/CircleRed.png +0 -0
- data/media/CircleWhite.png +0 -0
- data/media/CircleYellow.png +0 -0
- data/media/SampleGraph.png +0 -0
- data/media/WadsScreenshot.png +0 -0
- data/run-graph +3 -0
- data/run-star-wars +3 -0
- data/run-stocks +3 -0
- data/run-theme-test +3 -0
- data/samples/basic_gosu_with_graph_widget.rb +66 -0
- data/samples/graph.rb +72 -0
- data/samples/star_wars.rb +112 -0
- data/samples/stocks.rb +126 -0
- data/samples/theme_test.rb +256 -0
- metadata +22 -5
- data/run-sample-app +0 -3
- data/sample_app.rb +0 -64
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a1cf7ab7c4229f57f2bb4691819c5a4282a3fd0f7703e5caa6c93e49aeb35053
|
4
|
+
data.tar.gz: a1f0e971db1ea5064e44fef39c31767125f844b8ab3ef87eeaf09e3e92f27ad1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 315790b3c8fe99525c3ad78eaa90eecd17fadd09a2a3e16a13ba82cb61ea5279472c57ada6e88021e9c6649236bb598aad0a46a1954928a1c37dc8dbf8b45b5e
|
7
|
+
data.tar.gz: 89de4f997dcef2e1b9e25062af443d6994d7ee4070d89dab21f06cb492ed3d106f837a9c3bdf85d7ba7e6522d30a039694982d939d49dd41b7da30c2637299ea
|
data/CHANGELOG.md
CHANGED
@@ -10,3 +10,10 @@
|
|
10
10
|
|
11
11
|
- Fixed bugs with data structures. Added convenience methods to manage child widgets
|
12
12
|
|
13
|
+
## [0.1.3] - 2021-09-30
|
14
|
+
|
15
|
+
- Improvements to graph display including user ability to change focus node
|
16
|
+
|
17
|
+
## [0.2.0] - 2021-10-05
|
18
|
+
|
19
|
+
- Breaking change in order to refactor, including standardizing order of widget constructor args
|
data/README.md
CHANGED
@@ -2,6 +2,17 @@
|
|
2
2
|
|
3
3
|
A number of Ruby Gosu-based (W)idgets (a)nd (D)ata (S)tructures for your applications.
|
4
4
|
|
5
|
+
The user guide and examples can be found at https://www.rubydoesitagain.com
|
6
|
+
|
7
|
+
Wads (Widgets and Data Structures) is intended to help you quickly build native Ruby applications using the Gosu gem.
|
8
|
+
GUI widgets, such as tables and buttons, are provided to simplify building graphical applications.
|
9
|
+
Widgets in wads can be used independently within your Gosu app, or you can use wads as a holistic GUI framework.
|
10
|
+
Wads includes basic features found in many GUI toolkits including The use of themes and a layout manager.
|
11
|
+
|
12
|
+
One of the motivations for creating wads is to making data analysis, games, and productivity applications using Ruby easier to do.
|
13
|
+
Data structures are included that can be used for statistical analysis or graph-based applications.
|
14
|
+
The samples provided illustrate both use cases (Stocks - stats) and Star Wars character interactions (graphs).
|
15
|
+
|
5
16
|

|
6
17
|
## Installation
|
7
18
|
|
data/lib/wads/app.rb
CHANGED
@@ -1,349 +1,57 @@
|
|
1
1
|
require 'gosu'
|
2
|
-
require 'json'
|
3
|
-
require_relative 'data_structures'
|
4
2
|
require_relative 'widgets'
|
5
|
-
require_relative 'version'
|
6
3
|
|
7
|
-
|
8
|
-
|
9
|
-
class
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
@display_widget = nil
|
24
|
-
@update_count = 0
|
25
|
-
end
|
4
|
+
module Wads
|
5
|
+
#
|
6
|
+
# The WadsApp class provides a simple starting point to quickly build a native
|
7
|
+
# Ruby application using Gosu as an underlying library. It provides all the necessary
|
8
|
+
# hooks to get started. All you need to do is supply the parent Wads widget using
|
9
|
+
# the set_display(widget) method. See one of the Wads samples for example usage.
|
10
|
+
#
|
11
|
+
class WadsApp < Gosu::Window
|
12
|
+
def initialize(width, height, caption, widget)
|
13
|
+
super(width, height)
|
14
|
+
self.caption = caption
|
15
|
+
@update_count = 0
|
16
|
+
WadsConfig.instance.set_window(self)
|
17
|
+
set_display(widget)
|
18
|
+
WadsConfig.instance.set_log_level("info")
|
19
|
+
end
|
26
20
|
|
27
|
-
|
28
|
-
#
|
29
|
-
|
30
|
-
|
21
|
+
#
|
22
|
+
# This method must be invoked with any Wads::Widget instance. It then handles
|
23
|
+
# delegating all events and drawing all child widgets.
|
24
|
+
#
|
25
|
+
def set_display(widget)
|
26
|
+
@main_widget = widget
|
31
27
|
end
|
32
28
|
|
33
|
-
|
34
|
-
|
35
|
-
stats = process_stock_data
|
36
|
-
if opts[:text]
|
37
|
-
stats.report(Date::DAYNAMES[1..5])
|
38
|
-
else
|
39
|
-
@display_widget = SampleStocksDisplay.new(@small_font, stats)
|
40
|
-
show
|
41
|
-
end
|
42
|
-
|
43
|
-
elsif opts[:jedi]
|
44
|
-
graph = process_star_wars_data
|
45
|
-
@display_widget = SampleStarWarsDisplay.new(@small_font, graph)
|
46
|
-
show
|
47
|
-
|
48
|
-
elsif opts[:lottery]
|
49
|
-
process_lottery_data
|
50
|
-
|
51
|
-
elsif opts[:graph]
|
52
|
-
graph = create_test_graph #_using_nodes
|
53
|
-
if not opts[:text]
|
54
|
-
@display_widget = SampleGraphDisplay.new(@small_font, graph)
|
55
|
-
show
|
56
|
-
end
|
57
|
-
else
|
58
|
-
puts " "
|
59
|
-
puts "Select one of the following sample analysis options"
|
60
|
-
puts "-s Run sample stocks analysis"
|
61
|
-
puts "-j Run sample Star Wars character analysis"
|
62
|
-
puts "-g Run sample graph display"
|
63
|
-
puts " "
|
64
|
-
exit
|
29
|
+
def get_display
|
30
|
+
@main_widget
|
65
31
|
end
|
66
32
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
@update_count = @update_count + 1
|
72
|
-
end
|
33
|
+
def update
|
34
|
+
@main_widget.update(@update_count, mouse_x, mouse_y)
|
35
|
+
@update_count = @update_count + 1
|
36
|
+
end
|
73
37
|
|
74
|
-
|
75
|
-
|
76
|
-
@display_widget.draw
|
77
|
-
end
|
78
|
-
|
79
|
-
def draw_banner
|
80
|
-
@banner_image.draw(1,1,1,0.9,0.9)
|
81
|
-
@title_font.draw_text("Wads Sample App", 10, 20, 2, 1, 1, Gosu::Color::WHITE)
|
82
|
-
@version_font.draw_text("Version #{Wads::VERSION}", 13, 54, 2, 1, 1, Gosu::Color::WHITE)
|
83
|
-
end
|
84
|
-
|
85
|
-
def button_down id
|
86
|
-
close if id == Gosu::KbEscape
|
87
|
-
# Delegate button events to the primary display widget
|
88
|
-
result = @display_widget.button_down id, mouse_x, mouse_y
|
89
|
-
if not result.nil? and result.is_a? WidgetResult
|
90
|
-
if result.close_widget
|
91
|
-
close
|
92
|
-
end
|
38
|
+
def draw
|
39
|
+
@main_widget.draw
|
93
40
|
end
|
94
|
-
end
|
95
|
-
|
96
|
-
def button_up id
|
97
|
-
@display_widget.button_up id, mouse_x, mouse_y
|
98
|
-
end
|
99
|
-
|
100
|
-
def process_stock_data
|
101
|
-
# The data file comes from https://finance.yahoo.com
|
102
|
-
# The format of this file is as follows:
|
103
|
-
#
|
104
|
-
# Date,Open,High,Low,Close,Adj Close,Volume
|
105
|
-
# 2000-01-03,4186.189941,4192.189941,3989.709961,4131.149902,4131.149902,1510070000
|
106
|
-
# 2000-01-04,4020.000000,4073.250000,3898.229980,3901.689941,3901.689941,1511840000
|
107
|
-
# ...
|
108
|
-
# 2020-12-30,12906.509766,12924.929688,12857.759766,12870.000000,12870.000000,5292210000
|
109
|
-
# 2020-12-31,12877.089844,12902.070313,12821.230469,12888.280273,12888.280273,4771390000
|
110
|
-
|
111
|
-
stats = Stats.new("NASDAQ")
|
112
|
-
previous_close = nil
|
113
|
-
|
114
|
-
puts "Read the data file #{STOCKS_DATA_FILE}"
|
115
|
-
File.readlines(STOCKS_DATA_FILE).each do |line|
|
116
|
-
line = line.chomp # remove the carriage return
|
117
|
-
|
118
|
-
# Ignore header and any empty lines, process numeric data lines
|
119
|
-
if line.length > 0 and line[0].match(/[0-9]/)
|
120
|
-
values = line.split(",")
|
121
|
-
date = Date.strptime(values[0], "%Y-%m-%d")
|
122
|
-
weekday = Date::DAYNAMES[date.wday]
|
123
|
-
|
124
|
-
open_value = values[1].to_f
|
125
|
-
close_value = values[4].to_f
|
126
|
-
|
127
|
-
if previous_close.nil?
|
128
|
-
# Just use the first day to set the baseline
|
129
|
-
previous_close = close_value
|
130
|
-
else
|
131
|
-
change_from_previous_close = close_value - previous_close
|
132
|
-
change_intraday = close_value - open_value
|
133
|
-
change_overnight = open_value - previous_close
|
134
|
-
|
135
|
-
change_percent = change_from_previous_close / previous_close
|
136
|
-
|
137
|
-
stats.add(weekday, change_percent)
|
138
|
-
stats.add("#{weekday} prev close", change_from_previous_close)
|
139
|
-
stats.add("#{weekday} intraday", change_intraday)
|
140
|
-
stats.add("#{weekday} overnight", change_overnight)
|
141
41
|
|
142
|
-
|
42
|
+
def button_down id
|
43
|
+
close if id == Gosu::KbEscape
|
44
|
+
# Delegate button events to the primary display widget
|
45
|
+
result = @main_widget.button_down id, mouse_x, mouse_y
|
46
|
+
if not result.nil? and result.is_a? WidgetResult
|
47
|
+
if result.close_widget
|
48
|
+
close
|
143
49
|
end
|
144
50
|
end
|
145
51
|
end
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
def process_lottery_data
|
151
|
-
puts "The lottery example has not been implemented yet"
|
152
|
-
puts "however the data is available in the data directory,"
|
153
|
-
puts "and the same pattern used in the stocks example can"
|
154
|
-
puts "be applied here."
|
155
|
-
end
|
156
|
-
|
157
|
-
def process_star_wars_data
|
158
|
-
star_wars_json = File.read(STAR_WARS_DATA_FILE)
|
159
|
-
data_hash = JSON.parse(star_wars_json)
|
160
|
-
characters = data_hash['nodes']
|
161
|
-
interactions = data_hash['links']
|
162
|
-
|
163
|
-
# The interactions in the data set reference the characters by their
|
164
|
-
# zero based index, so we keep a reference in our graph by index.
|
165
|
-
# The character's value is the number of scenes in which they appear.
|
166
|
-
graph = Graph.new
|
167
|
-
characters.each do |character|
|
168
|
-
node_tags = {}
|
169
|
-
node_color_str = character['colour']
|
170
|
-
# This is a bit of a hack, but our background is black so black text
|
171
|
-
# will not show up. Change this to white
|
172
|
-
if node_color_str == "#000000"
|
173
|
-
node_color_str = "#FFFFFF"
|
174
|
-
end
|
175
|
-
# Convert hex string (ex. "#EE00AA") into int hex representation
|
176
|
-
# understood by Gosu color (ex. 0xFFEE00AA)
|
177
|
-
node_color = "0xFF#{node_color_str[1..-1]}".to_i(16)
|
178
|
-
node_tags['color'] = node_color
|
179
|
-
graph.add_node(Node.new(character['name'], character['value'], node_tags))
|
180
|
-
end
|
181
|
-
interactions.each do |interaction|
|
182
|
-
character_one = graph.node_by_index(interaction['source'])
|
183
|
-
character_two = graph.node_by_index(interaction['target'])
|
184
|
-
number_of_scenes_together = interaction['value']
|
185
|
-
edge_tags = {}
|
186
|
-
edge_tags["scenes"] = number_of_scenes_together
|
187
|
-
graph.add_edge(character_one, character_two, edge_tags)
|
188
|
-
end
|
189
|
-
graph
|
190
|
-
end
|
191
|
-
|
192
|
-
def create_test_graph
|
193
|
-
g = Graph.new
|
194
|
-
g.add("a")
|
195
|
-
g.add("b")
|
196
|
-
g.add("c")
|
197
|
-
g.add("d")
|
198
|
-
g.add("e")
|
199
|
-
g.add("f")
|
200
|
-
g.connect("a", "b")
|
201
|
-
g.connect("a", "c")
|
202
|
-
g.connect("b", "d")
|
203
|
-
g.connect("b", "e")
|
204
|
-
g.connect("e", "f")
|
205
|
-
g
|
206
|
-
end
|
207
|
-
|
208
|
-
def create_test_graph_using_nodes
|
209
|
-
root = Node.new("a")
|
210
|
-
b = root.add("b")
|
211
|
-
b.add("d")
|
212
|
-
b.add("e").add("f")
|
213
|
-
root.add("c")
|
214
|
-
Graph.new(root)
|
215
|
-
end
|
216
|
-
end
|
217
|
-
|
218
|
-
class SampleStocksDisplay < Widget
|
219
|
-
attr_accessor :stats
|
220
|
-
|
221
|
-
def initialize(font, stats)
|
222
|
-
super(10, 100, COLOR_HEADER_BRIGHT_BLUE)
|
223
|
-
set_dimensions(780, 500)
|
224
|
-
set_font(font)
|
225
|
-
add_document(sample_content, 5, 5, @width, @height)
|
226
|
-
add_button("Exit", 380, @height - 30) do
|
227
|
-
WidgetResult.new(true)
|
228
|
-
end
|
229
|
-
|
230
|
-
@stats = stats
|
231
|
-
@data_table = add_single_select_table(5, 100, # top left corner
|
232
|
-
770, 200, # width, height
|
233
|
-
["Day", "Min", "Avg", "StdDev", "Max", "p10", "p90"], # column headers
|
234
|
-
COLOR_WHITE) # font and text color
|
235
|
-
@data_table.selected_color = COLOR_LIGHT_GRAY
|
236
|
-
Date::DAYNAMES[1..5].each do |day|
|
237
|
-
min = format_percent(@stats.min(day))
|
238
|
-
avg = format_percent(@stats.average(day))
|
239
|
-
std = format_percent(@stats.std_dev(day))
|
240
|
-
max = format_percent(@stats.max(day))
|
241
|
-
p10 = format_percent(@stats.percentile(day, 0.1))
|
242
|
-
p90 = format_percent(@stats.percentile(day, 0.90))
|
243
|
-
@data_table.add_row([day, min, avg, std, max, p10, p90], COLOR_HEADER_BRIGHT_BLUE)
|
244
|
-
end
|
245
|
-
|
246
|
-
@selection_text = nil
|
247
|
-
end
|
248
|
-
|
249
|
-
def format_percent(val)
|
250
|
-
"#{(val * 100).round(3)}%"
|
251
|
-
end
|
252
|
-
|
253
|
-
def sample_content
|
254
|
-
<<~HEREDOC
|
255
|
-
This sample stock analysis uses NASDAQ data from https://finance.yahoo.com looking
|
256
|
-
at closing data through the years 2000 to 2020. The percent gain or loss is broken
|
257
|
-
down per day, as shown in the table below.
|
258
|
-
HEREDOC
|
259
|
-
end
|
260
|
-
|
261
|
-
def render
|
262
|
-
if @selection_text
|
263
|
-
@selection_text.draw
|
264
|
-
end
|
265
|
-
end
|
266
|
-
|
267
|
-
def handle_mouse_down mouse_x, mouse_y
|
268
|
-
if @data_table.contains_click(mouse_x, mouse_y)
|
269
|
-
val = @data_table.set_selected_row(mouse_y, 0)
|
270
|
-
if not val.nil?
|
271
|
-
@selection_text = Text.new("You selected #{val}, a great day!",
|
272
|
-
x + 5, y + 400, @font)
|
273
|
-
end
|
274
|
-
end
|
275
|
-
nil
|
276
|
-
end
|
277
|
-
end
|
278
|
-
|
279
|
-
class SampleStarWarsDisplay < Widget
|
280
|
-
attr_accessor :graph
|
281
|
-
|
282
|
-
def initialize(font, graph)
|
283
|
-
super(10, 100, COLOR_HEADER_BRIGHT_BLUE)
|
284
|
-
set_dimensions(780, 500)
|
285
|
-
set_font(font)
|
286
|
-
@graph = graph
|
287
|
-
|
288
|
-
add_document(sample_content, 5, 5, @width, @height)
|
289
|
-
add_button("Exit", 370, @height - 30) do
|
290
|
-
WidgetResult.new(true)
|
291
|
-
end
|
292
|
-
|
293
|
-
@data_table = add_single_select_table(5, 70, # top left corner
|
294
|
-
770, 180, # width, height
|
295
|
-
["Character", "Number of Scenes"], # column headers
|
296
|
-
COLOR_WHITE, # font and text color
|
297
|
-
5) # max visible rows
|
298
|
-
@data_table.selected_color = COLOR_LIGHT_GRAY
|
299
|
-
|
300
|
-
@graph.node_list.each do |character|
|
301
|
-
@data_table.add_row([character.name, character.value], character.get_tag("color"))
|
52
|
+
|
53
|
+
def button_up id
|
54
|
+
@main_widget.button_up id, mouse_x, mouse_y
|
302
55
|
end
|
303
|
-
@graph_display = add_graph_display(5, 260, 770, 200, @graph)
|
304
56
|
end
|
305
|
-
|
306
|
-
def sample_content
|
307
|
-
<<~HEREDOC
|
308
|
-
This sample analysis shows the interactions between characters in the Star Wars
|
309
|
-
Episode 4: A New Hope. Click on a character to see more detail.
|
310
|
-
HEREDOC
|
311
|
-
end
|
312
|
-
|
313
|
-
def handle_key_press id, mouse_x, mouse_y
|
314
|
-
if id == Gosu::KbUp
|
315
|
-
@data_table.scroll_up
|
316
|
-
elsif id == Gosu::KbDown
|
317
|
-
@data_table.scroll_down
|
318
|
-
end
|
319
|
-
WidgetResult.new(false)
|
320
|
-
end
|
321
|
-
|
322
|
-
def handle_mouse_down mouse_x, mouse_y
|
323
|
-
if @data_table.contains_click(mouse_x, mouse_y)
|
324
|
-
val = @data_table.set_selected_row(mouse_y, 0)
|
325
|
-
if not val.nil?
|
326
|
-
node = @graph.find_node(val)
|
327
|
-
@graph_display.set_center_node(node, 2)
|
328
|
-
end
|
329
|
-
end
|
330
|
-
nil
|
331
|
-
end
|
332
57
|
end
|
333
|
-
|
334
|
-
class SampleGraphDisplay < Widget
|
335
|
-
attr_accessor :graph
|
336
|
-
|
337
|
-
def initialize(font, graph)
|
338
|
-
super(10, 100, COLOR_CYAN)
|
339
|
-
set_dimensions(780, 500)
|
340
|
-
set_font(font)
|
341
|
-
@graph = graph
|
342
|
-
exit_button = add_button("Exit", 370, @height - 30) do
|
343
|
-
WidgetResult.new(true)
|
344
|
-
end
|
345
|
-
exit_button.text_color = COLOR_CYAN
|
346
|
-
@graph_display = add_graph_display(5, 60, 770, 400, @graph)
|
347
|
-
@graph_display.set_tree_display
|
348
|
-
end
|
349
|
-
end
|