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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
local STATE_FILE = "hydration.json"
local COLORS = {
panel = "#0F1414F5",
text = "#F4F1E8",
soft = "#D8DDD3",
muted = "#98A398",
accent = "#7ED7E8",
line = "#263335",
}
local cfg = rb.ui.schema({
goal = rb.ui.slider(8, {
min = 1,
max = 16,
step = 1,
label = "Daily goal (glasses)",
group = "Goal",
}),
add_key = rb.ui.keybind("Ctrl+Alt+H", {
label = "Add a glass",
group = "Hotkeys",
}),
reset_key = rb.ui.keybind("Ctrl+Alt+Shift+H", {
label = "Reset today",
group = "Hotkeys",
}),
auto_reset = rb.ui.toggle(true, {
label = "Reset automatically each day",
group = "Behavior",
}),
show_hud = rb.ui.toggle(true, {
label = "Show counter",
group = "HUD",
}),
hud_anchor = rb.ui.select("top-right", {
"top-left",
"top-right",
"bottom-left",
"bottom-right",
}, {
label = "Counter position",
group = "HUD",
}),
})
local state = {
count = 0,
date = "",
surface = nil,
signature = nil,
timer = nil,
}
local function today()
return os.date("%Y-%m-%d")
end
local function clamp(value, minimum, maximum)
return math.max(minimum, math.min(value, maximum))
end
local function add_rect(ops, x, y, width, height, fill, radius, border, border_width)
local op = {
kind = "rect",
x = x,
y = y,
w = width,
h = height,
fill = fill,
radius = radius or 0,
}
if border then
op.border = border
op.border_width = border_width or 1
end
ops[#ops + 1] = op
end
local function add_text(ops, x, y, size, value, color, bold)
ops[#ops + 1] = {
kind = "text",
x = x,
y = y,
size = size,
text = value,
color = color,
bold = bold == true,
}
end
local function notify(message)
pcall(rb.overlay.toast, message, {
title = "Hydration",
duration = 2400,
})
end
local function remove_surface()
if state.surface then
pcall(function()
state.surface:remove()
end)
state.surface = nil
end
end
local function save_state()
local ok = pcall(rb.file.write_json, STATE_FILE, {
count = state.count,
date = state.date,
})
if not ok then
rb.log.warn("Hydration: could not save " .. STATE_FILE)
end
end
local function refresh_day()
local current = today()
if state.date ~= current then
if cfg.auto_reset ~= false then
state.count = 0
end
state.date = current
save_state()
state.signature = nil
return true
end
return false
end
local function draw_hud()
if cfg.show_hud == false then
remove_surface()
state.signature = nil
return
end
local goal = clamp(tonumber(cfg.goal) or 8, 1, 16)
local count = clamp(state.count, 0, goal)
local fraction = count / goal
local signature = tostring(count) .. "/" .. tostring(goal) .. "|" .. tostring(cfg.hud_anchor)
if signature == state.signature then
return
end
state.signature = signature
local ops = {}
add_rect(ops, 0, 0, 280, 92, COLORS.panel, 14, COLORS.line, 1)
add_rect(ops, 16, 16, 3, 60, COLORS.accent, 1)
add_text(ops, 28, 12, 10, "HYDRATION", COLORS.muted, true)
add_text(ops, 28, 27, 26, tostring(count) .. " / " .. tostring(goal), COLORS.text, true)
add_text(ops, 28, 62, 9, "Ctrl+Alt+H add glass", COLORS.muted)
add_rect(ops, 16, 78, 248, 6, "#223034", 3)
if fraction > 0 then
add_rect(ops, 16, 78, math.max(2, 248 * fraction), 6, COLORS.accent, 3)
end
if count >= goal then
add_text(ops, 176, 12, 9, "GOAL MET", COLORS.accent, true)
end
if state.surface then
local ok = pcall(function()
state.surface:set({
width = 280,
height = 92,
ops = ops,
})
end)
if not ok then
remove_surface()
end
end
if not state.surface then
local ok, surface = pcall(rb.overlay.surface, {
anchor = cfg.hud_anchor,
width = 280,
height = 92,
ops = ops,
})
if ok then
state.surface = surface
else
rb.log.warn("Hydration: could not create surface: " .. tostring(surface))
end
end
end
local function add_glass()
refresh_day()
local goal = clamp(tonumber(cfg.goal) or 8, 1, 16)
if state.count >= goal then
notify("Daily goal already reached.")
return
end
state.count = math.min(goal, state.count + 1)
save_state()
state.signature = nil
draw_hud()
if state.count >= goal then
notify("Daily goal reached — " .. tostring(goal) .. " glasses.")
else
notify(tostring(state.count) .. " / " .. tostring(goal) .. " glasses")
end
end
local function reset_today()
state.count = 0
state.date = today()
save_state()
state.signature = nil
draw_hud()
notify("Today's count reset.")
end
rb.bind(cfg.add_key, add_glass)
rb.bind(cfg.reset_key, reset_today)
function OnStart()
local ok, data = pcall(rb.file.read_json, STATE_FILE)
if ok and type(data) == "table" then
state.count = math.max(0, math.floor(tonumber(data.count) or 0))
state.date = tostring(data.date or "")
end
if state.date == "" then
state.date = today()
end
refresh_day()
state.count = clamp(state.count, 0, clamp(tonumber(cfg.goal) or 8, 1, 16))
draw_hud()
state.timer = rb.timer.every(60000, function()
if refresh_day() then
draw_hud()
end
end)
end
function OnStop()
if state.timer then
pcall(function()
state.timer:stop()
end)
state.timer = nil
end
save_state()
remove_surface()
end