-
Notifications
You must be signed in to change notification settings - Fork 1
/
hedron.cr
139 lines (114 loc) · 3.47 KB
/
hedron.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
require "option_parser"
require "hedron"
require "./config"
require "./roller"
class RollerUI < Hedron::Application
@mainwin : Hedron::Window?
@roller: Roller?
def initialize(config : Config)
super()
@config = config
config.load
@roller = Roller.new
end
def on_closing(this)
this.destroy
self.stop
return false
end
def should_quit
@mainwin.not_nil!.destroy
return true
end
def on_set_selected(this)
@config.switchModel @config.sets[this.selected]
@config.save
reloadButtonBox
end
def on_button_clicked(this)
value = @config.buttonset[this.text]
results = @roller.not_nil!.parseLine(value)
label = results.join("\n")
@label.not_nil!.text = label
@entry.not_nil!.text = value
end
def on_roll(this)
value = @entry.not_nil!.text
results = @roller.not_nil!.parseLine(value)
label = results.join("\n")
@label.not_nil!.text = label
end
def draw
self.on_stop = ->should_quit
@mainwin = Hedron::Window.new("Dice Roller", {640, 480}, menubar: false)
mainwin = @mainwin.not_nil!
mainwin.margined = true
mainwin.on_close = ->on_closing(Hedron::Window)
@box = Hedron::VerticalBox.new
box = @box.not_nil!
box.padded = true
mainwin.child = box
hbox = Hedron::HorizontalBox.new
hbox.padded = true
box.push(hbox)
@entry = Hedron::Entry.new
entry = @entry.not_nil!
entry.text = "Dice descriptor"
entry.stretchy = true
roll = Hedron::Button.new("Roll!")
roll.on_click = -> on_roll(Hedron::Button)
hbox.push(
entry,
roll
)
@label = Hedron::Label.new("")
label = @label.not_nil!
label.stretchy = true
box.push(label)
cbox = Hedron::Combobox.new
cbox.choices = @config.sets
cbox.selected = @config.sets.index(@config.model).as(Int32)
cbox.on_select = -> on_set_selected(Hedron::Combobox)
box.push(cbox)
reloadButtonBox
mainwin.show
end
def reloadButtonBox
if !@buttonbox.nil?
@box.not_nil!.delete_at(3)
@buttonbox.not_nil!.destroy
end
@buttonbox = Hedron::HorizontalBox.new
buttonbox = @buttonbox.not_nil!
@config.buttonset.each { |label, value|
button = Hedron::Button.new(label)
button.on_click = ->on_button_clicked(Hedron::Button)
buttonbox.push(button)
}
@box.not_nil!.push buttonbox
end
end
XDG_CONFIG_HOME = ENV.fetch("XDG_CONFIG_HOME", "~/.config")
CONFIG_HOME = File.expand_path "#{XDG_CONFIG_HOME}/roll"
if !Dir.exists? CONFIG_HOME
Dir.mkdir CONFIG_HOME
end
def reloadUI
configfile = CONFIG_HOME+"/config.ini"
OptionParser.parse! do |parser|
parser.banner = "Usage: roll [options]"
parser.on("-c FILENAME", "--config=FILENAME", "Specifies the config filename") { |filename| configfile = filename }
parser.on("-h", "--help", "Show this help") { puts parser; exit }
parser.invalid_option do |flag|
STDERR.puts "ERROR: #{flag} is not a valid option."
STDERR.puts parser
exit(1)
end
end
config = Config.new(configfile)
ui = RollerUI.new(config)
ui.draw
ui.start
ui.close
end
reloadUI