-
-
Notifications
You must be signed in to change notification settings - Fork 348
Expand file tree
/
Copy pathGUIUtils.lua
More file actions
Ignoring revisions in .git-blame-ignore-revs.
2886 lines (2638 loc) · 98 KB
/
GUIUtils.lua
File metadata and controls
2886 lines (2638 loc) · 98 KB
Edit and raw actions
OlderNewer
1
----------------------------------------------------------------------------------
2
--- Mudlet GUI Utils
3
----------------------------------------------------------------------------------
4
5
6
--- The <i>gaugesTable table</i>. First we need to make this table which will be
7
--- used later to store important data in.
8
---
9
--- @class function
10
--- @name gaugesTable
11
gaugesTable = {}
12
13
--- The <i>color_table table</i> holds definition of color names. These are intended to be
14
-- used in conjunction with fg() and bg() colorizer functions.
15
-- Mudlet's original table - going back a few years - differs from the
16
-- "standard" which can be found at https://www.w3.org/TR/SVG11/types.html#ColorKeywords
17
-- Mudlet has additional colours not in the standard:
18
-- "light_goldenrod", "light_slate_blue", "navy_blue" and "violet_red"
19
-- Mudlet is missing some colours:
20
-- "aqua", "fuchsia", "dark_blue", "dark_cyan", "dark_gray"/"dark_grey",
21
-- "dark_magenta", "dark_red", "indigo", "light_green", "olive", "silver",
22
-- "teal", "violet_red"
23
-- Also Mudlet redefines:
24
-- "gray"/"grey", "green", "maroon" and "purple"
25
-- All of the above also implies the Camel Case equivalents; in summary:
26
-- Colour Mudlet Web Standard
27
-- aqua 0, 255, 255
28
-- fuchsia 255, 0, 255
29
-- dark_blue 0, 0, 139
30
-- dark_gray/dark_grey 169, 169, 169
31
-- dark_magenta 139, 0, 139
32
-- dark_red 139, 0, 0
33
-- gray/grey 190, 190, 190 128, 128, 128
34
-- green 0, 255, 0 0, 128, 0
35
-- indigo 75, 0, 130
36
-- light_goldrod 238, 221, 130
37
-- light_slate_blue 132, 112, 255
38
-- light_green 144, 238, 144
39
-- maroon 176, 48, 96 128, 0, 0
40
-- navy_blue 0, 0, 128
41
-- olive 128, 128, 0
42
-- purple 160, 32, 240 128, 0, 128
43
-- silver 192, 192, 192
44
-- teal 0, 128, 128
45
-- violet_red 208, 32, 240
46
-- @see showColors
47
-- @see bg
48
-- @see fg
49
-- @class function
50
-- @name color_table
51
color_table = color_table or {}
52
color_table["alice_blue"] = { 240, 248, 255 }
53
color_table["AliceBlue"] = { 240, 248, 255 }
54
color_table["antique_white"] = { 250, 235, 215 }
55
color_table["AntiqueWhite"] = { 250, 235, 215 }
56
color_table["aquamarine"] = { 127, 255, 212 }
57
color_table["azure"] = { 240, 255, 255 }
58
color_table["beige"] = { 245, 245, 220 }
59
color_table["bisque"] = { 255, 228, 196 }
60
color_table["black"] = { 0, 0, 0 }
61
color_table["blanched_almond"] = { 255, 235, 205 }
62
color_table["BlanchedAlmond"] = { 255, 235, 205 }
63
color_table["blue"] = { 0, 0, 255 }
64
color_table["blue_violet"] = { 138, 43, 226 }
65
color_table["BlueViolet"] = { 138, 43, 226 }
66
color_table["brown"] = { 165, 42, 42 }
67
color_table["burlywood"] = { 222, 184, 135 }
68
color_table["cadet_blue"] = { 95, 158, 160 }
69
color_table["CadetBlue"] = { 95, 158, 160 }
70
color_table["chartreuse"] = { 127, 255, 0 }
71
color_table["chocolate"] = { 210, 105, 30 }
72
color_table["coral"] = { 255, 127, 80 }
73
color_table["cornflower_blue"] = { 100, 149, 237 }
74
color_table["CornflowerBlue"] = { 100, 149, 237 }
75
color_table["cornsilk"] = { 255, 248, 220 }
76
color_table["cyan"] = { 0, 255, 255 }
77
color_table["dark_goldenrod"] = { 184, 134, 11 }
78
color_table["DarkGoldenrod"] = { 184, 134, 11 }
79
color_table["dark_green"] = { 0, 100, 0 }
80
color_table["DarkGreen"] = { 0, 100, 0 }
81
color_table["dark_khaki"] = { 189, 183, 107 }
82
color_table["DarkKhaki"] = { 189, 183, 107 }
83
color_table["dark_olive_green"] = { 85, 107, 47 }
84
color_table["DarkOliveGreen"] = { 85, 107, 47 }
85
color_table["dark_orange"] = { 255, 140, 0 }
86
color_table["DarkOrange"] = { 255, 140, 0 }
87
color_table["dark_orchid"] = { 153, 50, 204 }
88
color_table["DarkOrchid"] = { 153, 50, 204 }
89
color_table["dark_salmon"] = { 233, 150, 122 }
90
color_table["DarkSalmon"] = { 233, 150, 122 }
91
color_table["dark_slate_blue"] = { 72, 61, 139 }
92
color_table["dark_sea_green"] = { 143, 188, 143 }
93
color_table["DarkSeaGreen"] = { 143, 188, 143 }
94
color_table["DarkSlateBlue"] = { 72, 61, 139 }
95
color_table["dark_slate_gray"] = { 47, 79, 79 }
96
color_table["DarkSlateGray"] = { 47, 79, 79 }
97
color_table["dark_slate_grey"] = { 47, 79, 79 }
98
color_table["DarkSlateGrey"] = { 47, 79, 79 }
99
color_table["dark_turquoise"] = { 0, 206, 209 }
100
color_table["DarkTurquoise"] = { 0, 206, 209 }
101
color_table["dark_violet"] = { 148, 0, 211 }
102
color_table["DarkViolet"] = { 148, 0, 211 }
103
color_table["deep_pink"] = { 255, 20, 147 }
104
color_table["DeepPink"] = { 255, 20, 147 }
105
color_table["deep_sky_blue"] = { 0, 191, 255 }
106
color_table["DeepSkyBlue"] = { 0, 191, 255 }
107
color_table["dodger_blue"] = { 30, 144, 255 }
108
color_table["DodgerBlue"] = { 30, 144, 255 }
109
color_table["dim_gray"] = { 105, 105, 105 }
110
color_table["DimGray"] = { 105, 105, 105 }
111
color_table["dim_grey"] = { 105, 105, 105 }
112
color_table["DimGrey"] = { 105, 105, 105 }
113
color_table["firebrick"] = { 178, 34, 34 }
114
color_table["floral_white"] = { 255, 250, 240 }
115
color_table["FloralWhite"] = { 255, 250, 240 }
116
color_table["forest_green"] = { 34, 139, 34 }
117
color_table["ForestGreen"] = { 34, 139, 34 }
118
color_table["gainsboro"] = { 220, 220, 220 }
119
color_table["ghost_white"] = { 248, 248, 255 }
120
color_table["GhostWhite"] = { 248, 248, 255 }
121
color_table["gold"] = { 255, 215, 0 }
122
color_table["goldenrod"] = { 218, 165, 32 }
123
color_table["gray"] = { 190, 190, 190 }
124
color_table["grey"] = { 190, 190, 190 }
125
color_table["green"] = { 0, 255, 0 }
126
color_table["green_yellow"] = { 173, 255, 47 }
127
color_table["GreenYellow"] = { 173, 255, 47 }
128
color_table["honeydew"] = { 240, 255, 240 }
129
color_table["hot_pink"] = { 255, 105, 180 }
130
color_table["HotPink"] = { 255, 105, 180 }
131
color_table["indian_red"] = { 205, 92, 92 }
132
color_table["IndianRed"] = { 205, 92, 92 }
133
color_table["khaki"] = { 240, 230, 140 }
134
color_table["ivory"] = { 255, 255, 240 }
135
color_table["lavender"] = { 230, 230, 250 }
136
color_table["lavender_blush"] = { 255, 240, 245 }
137
color_table["LavenderBlush"] = { 255, 240, 245 }
138
color_table["lawn_green"] = { 124, 252, 0 }
139
color_table["LawnGreen"] = { 124, 252, 0 }
140
color_table["lemon_chiffon"] = { 255, 250, 205 }
141
color_table["LemonChiffon"] = { 255, 250, 205 }
142
color_table["light_blue"] = { 173, 216, 230 }
143
color_table["LightBlue"] = { 173, 216, 230 }
144
color_table["light_coral"] = { 240, 128, 128 }
145
color_table["LightCoral"] = { 240, 128, 128 }
146
color_table["light_cyan"] = { 224, 255, 255 }
147
color_table["LightCyan"] = { 224, 255, 255 }
148
color_table["light_goldenrod"] = { 238, 221, 130 }
149
color_table["LightGoldenrod"] = { 238, 221, 130 }
150
color_table["light_goldenrod_yellow"] = { 250, 250, 210 }
151
color_table["LightGoldenrodYellow"] = { 250, 250, 210 }
152
color_table["light_gray"] = { 211, 211, 211 }
153
color_table["LightGray"] = { 211, 211, 211 }
154
color_table["light_grey"] = { 211, 211, 211 }
155
color_table["LightGrey"] = { 211, 211, 211 }
156
color_table["light_pink"] = { 255, 182, 193 }
157
color_table["LightPink"] = { 255, 182, 193 }
158
color_table["light_salmon"] = { 255, 160, 122 }
159
color_table["LightSalmon"] = { 255, 160, 122 }
160
color_table["light_sea_green"] = { 32, 178, 170 }
161
color_table["LightSeaGreen"] = { 32, 178, 170 }
162
color_table["light_sky_blue"] = { 135, 206, 250 }
163
color_table["LightSkyBlue"] = { 135, 206, 250 }
164
color_table["light_slate_blue"] = { 132, 112, 255 }
165
color_table["LightSlateBlue"] = { 132, 112, 255 }
166
color_table["light_slate_gray"] = { 119, 136, 153 }
167
color_table["LightSlateGray"] = { 119, 136, 153 }
168
color_table["light_slate_grey"] = { 119, 136, 153 }
169
color_table["LightSlateGrey"] = { 119, 136, 153 }
170
color_table["light_steel_blue"] = { 176, 196, 222 }
171
color_table["LightSteelBlue"] = { 176, 196, 222 }
172
color_table["light_yellow"] = { 255, 255, 224 }
173
color_table["LightYellow"] = { 255, 255, 224 }
174
color_table["lime_green"] = { 50, 205, 50 }
175
color_table["LimeGreen"] = { 50, 205, 50 }
176
color_table["linen"] = { 250, 240, 230 }
177
color_table["magenta"] = { 255, 0, 255 }
178
color_table["maroon"] = { 176, 48, 96 }
179
color_table["medium_aquamarine"] = { 102, 205, 170 }
180
color_table["MediumAquamarine"] = { 102, 205, 170 }
181
color_table["medium_blue"] = { 0, 0, 205 }
182
color_table["MediumBlue"] = { 0, 0, 205 }
183
color_table["medium_orchid"] = { 186, 85, 211 }
184
color_table["MediumOrchid"] = { 186, 85, 211 }
185
color_table["medium_purple"] = { 147, 112, 219 }
186
color_table["MediumPurple"] = { 147, 112, 219 }
187
color_table["medium_sea_green"] = { 60, 179, 113 }
188
color_table["MediumSeaGreen"] = { 60, 179, 113 }
189
color_table["medium_slate_blue"] = { 123, 104, 238 }
190
color_table["MediumSlateBlue"] = { 123, 104, 238 }
191
color_table["medium_spring_green"] = { 0, 250, 154 }
192
color_table["MediumSpringGreen"] = { 0, 250, 154 }
193
color_table["medium_turquoise"] = { 72, 209, 204 }
194
color_table["MediumTurquoise"] = { 72, 209, 204 }
195
color_table["medium_violet_red"] = { 199, 21, 133 }
196
color_table["MediumVioletRed"] = { 199, 21, 133 }
197
color_table["midnight_blue"] = { 25, 25, 112 }
198
color_table["MidnightBlue"] = { 25, 25, 112 }
199
color_table["mint_cream"] = { 245, 255, 250 }
200
color_table["MintCream"] = { 245, 255, 250 }
201
color_table["misty_rose"] = { 255, 228, 225 }
202
color_table["MistyRose"] = { 255, 228, 225 }
203
color_table["moccasin"] = { 255, 228, 181 }
204
color_table["navajo_white"] = { 255, 222, 173 }
205
color_table["NavajoWhite"] = { 255, 222, 173 }
206
color_table["navy"] = { 0, 0, 128 }
207
color_table["navy_blue"] = { 0, 0, 128 }
208
color_table["NavyBlue"] = { 0, 0, 128 }
209
color_table["old_lace"] = { 253, 245, 230 }
210
color_table["OldLace"] = { 253, 245, 230 }
211
color_table["olive_drab"] = { 107, 142, 35 }
212
color_table["OliveDrab"] = { 107, 142, 35 }
213
color_table["orange"] = { 255, 165, 0 }
214
color_table["orange_red"] = { 255, 69, 0 }
215
color_table["OrangeRed"] = { 255, 69, 0 }
216
color_table["orchid"] = { 218, 112, 214 }
217
color_table["pale_goldenrod"] = { 238, 232, 170 }
218
color_table["PaleGoldenrod"] = { 238, 232, 170 }
219
color_table["pale_green"] = { 152, 251, 152 }
220
color_table["PaleGreen"] = { 152, 251, 152 }
221
color_table["pale_turquoise"] = { 175, 238, 238 }
222
color_table["PaleTurquoise"] = { 175, 238, 238 }
223
color_table["pale_violet_red"] = { 219, 112, 147 }
224
color_table["PaleVioletRed"] = { 219, 112, 147 }
225
color_table["papaya_whip"] = { 255, 239, 213 }
226
color_table["PapayaWhip"] = { 255, 239, 213 }
227
color_table["peach_puff"] = { 255, 218, 185 }
228
color_table["PeachPuff"] = { 255, 218, 185 }
229
color_table["peru"] = { 205, 133, 63 }
230
color_table["pink"] = { 255, 192, 203 }
231
color_table["plum"] = { 221, 160, 221 }
232
color_table["powder_blue"] = { 176, 224, 230 }
233
color_table["PowderBlue"] = { 176, 224, 230 }
234
color_table["purple"] = { 160, 32, 240 }
235
color_table["royal_blue"] = { 65, 105, 225 }
236
color_table["RoyalBlue"] = { 65, 105, 225 }
237
color_table["red"] = { 255, 0, 0 }
238
color_table["rosy_brown"] = { 188, 143, 143 }
239
color_table["RosyBrown"] = { 188, 143, 143 }
240
color_table["saddle_brown"] = { 139, 69, 19 }
241
color_table["SaddleBrown"] = { 139, 69, 19 }
242
color_table["salmon"] = { 250, 128, 114 }
243
color_table["sandy_brown"] = { 244, 164, 96 }
244
color_table["SandyBrown"] = { 244, 164, 96 }
245
color_table["sea_green"] = { 46, 139, 87 }
246
color_table["SeaGreen"] = { 46, 139, 87 }
247
color_table["seashell"] = { 255, 245, 238 }
248
color_table["sienna"] = { 160, 82, 45 }
249
color_table["sky_blue"] = { 135, 206, 235 }
250
color_table["SkyBlue"] = { 135, 206, 235 }
251
color_table["slate_blue"] = { 106, 90, 205 }
252
color_table["SlateBlue"] = { 106, 90, 205 }
253
color_table["slate_gray"] = { 112, 128, 144 }
254
color_table["SlateGray"] = { 112, 128, 144 }
255
color_table["slate_grey"] = { 112, 128, 144 }
256
color_table["SlateGrey"] = { 112, 128, 144 }
257
color_table["snow"] = { 255, 250, 250 }
258
color_table["steel_blue"] = { 70, 130, 180 }
259
color_table["SteelBlue"] = { 70, 130, 180 }
260
color_table["spring_green"] = { 0, 255, 127 }
261
color_table["SpringGreen"] = { 0, 255, 127 }
262
color_table["tan"] = { 210, 180, 140 }
263
color_table["thistle"] = { 216, 191, 216 }
264
color_table["tomato"] = { 255, 99, 71 }
265
color_table["transparent"] = { 255, 255, 255, 0}
266
color_table["turquoise"] = { 64, 224, 208 }
267
color_table["violet_red"] = { 208, 32, 144 }
268
color_table["VioletRed"] = { 208, 32, 144 }
269
color_table["violet"] = { 238, 130, 238 }
270
color_table["wheat"] = { 245, 222, 179 }
271
color_table["white"] = { 255, 255, 255 }
272
color_table["white_smoke"] = { 245, 245, 245 }
273
color_table["WhiteSmoke"] = { 245, 245, 245 }
274
color_table["yellow"] = { 255, 255, 0 }
275
color_table["yellow_green"] = { 154, 205, 50 }
276
color_table["YellowGreen"] = { 154, 205, 50 }
277
278
279
--- Move a custom gauge.
280
---
281
--- @usage This would move the health bar gauge to the location 1200, 400.
282
--- <pre>
283
--- moveGauge("healthBar", 1200, 400)
284
--- </pre>
285
---
286
--- @see createGauge
287
function moveGauge(gaugeName, x, y)
288
assert(gaugesTable[gaugeName], "moveGauge: no such gauge exists.")
289
assert(type(x) == 'number', 'moveGauge: bad argument #2 type (expected number, got '..type(x)..'!)')
290
assert(type(y) == 'number', 'moveGauge: bad argument #3 type (expected number, got '..type(y)..'!)')
291
moveWindow(gaugeName .. "_back", x, y)
292
moveWindow(gaugeName .. "_text", x, y)
293
-- save new values in table
294
gaugesTable[gaugeName].x, gaugesTable[gaugeName].y = x, y
295
setGauge(gaugeName, gaugesTable[gaugeName].value, 1)
296
end
297
298
299
--- Hide a custom gauge.
300
---
301
--- @usage This should hide the given gauge.
302
--- <pre>
303
--- hideGauge("healthBar")
304
--- </pre>
305
---
306
--- @see createGauge, moveGauge, showGauge
307
function hideGauge(gaugeName)
308
assert(gaugesTable[gaugeName], "hideGauge: no such gauge exists.")
309
hideWindow(gaugeName .. "_back")
310
hideWindow(gaugeName .. "_front")
311
hideWindow(gaugeName .. "_text")
312
end
313
314
315
--- Show a custom gauge.
316
---
317
--- @usage This should show the given gauge.
318
--- <pre>
319
--- showGauge("healthBar")
320
--- </pre>
321
---
322
--- @see createGauge, moveGauge, hideGauge
323
function showGauge(gaugeName)
324
assert(gaugesTable[gaugeName], "showGauge: no such gauge exists.")
325
showWindow(gaugeName .. "_back")
326
showWindow(gaugeName .. "_front")
327
showWindow(gaugeName .. "_text")
328
end
329
330
--- @see createGauge
331
function setGaugeWindow(windowName, gaugeName, x, y, show)
332
windowName = windowName or "main"
333
x = x or 0
334
y = y or 0
335
show = show or true
336
assert(gaugesTable[gaugeName], "setGaugeWindow: no such gauge exists.")
337
setWindow(windowName, gaugeName .. "_back", x, y, show)
338
setWindow(windowName, gaugeName .. "_front", x, y, show)
339
setWindow(windowName, gaugeName .. "_text", x, y, show)
340
-- save new values in table
341
gaugesTable[gaugeName].x, gaugesTable[gaugeName].y = x, y
342
setGauge(gaugeName, gaugesTable[gaugeName].value, 1)
343
end
344
345
--- Set the text on a custom gauge.
346
---
347
--- @usage
348
--- <pre>
349
--- setGaugeText("healthBar", "HP: 100%", 40, 40, 40)
350
--- </pre>
351
--- @usage
352
--- <pre>
353
--- setGaugeText("healthBar", "HP: 100%", "red")
354
--- </pre>
355
---
356
--- @param gaugeName
357
--- @param gaugeText An empty gaugeText will clear the text entirely.
358
--- @param color1 Colors are optional and will default to 0,0,0(black) if not passed as args.
359
--- @param color2
360
--- @param color3
361
---
362
--- @see createGauge
363
function setGaugeText(gaugeName, gaugeText, r, g, b)
364
assert(gaugesTable[gaugeName], "setGaugeText: no such gauge exists.")
365
if r ~= nil then
366
if g == nil then
367
r, g, b = getRGB(r)
368
end
369
else
370
r, g, b = 0, 0, 0
371
end
372
gaugeText = gaugeText or ""
373
local echoString = [[<font color="#]] .. RGB2Hex(r, g, b) .. [[">]] .. gaugeText .. [[</font>]]
374
echo(gaugeName .. "_text", echoString)
375
-- save new values in table
376
gaugesTable[gaugeName].text = echoString
377
end
378
379
--- Set gauge to no longer intercept mouse events
380
--- @param gaugeName
381
function enableGaugeClickthrough(gaugeName)
382
assert(gaugesTable[gaugeName], "enableGaugeClickthrough: no such gauge exists.")
383
enableClickthrough(gaugeName .. "_back")
384
enableClickthrough(gaugeName .. "_front")
385
enableClickthrough(gaugeName .. "_text")
386
end
387
388
--- Set gauge to once again intercept mouse events
389
--- @param gaugeName
390
function disableGaugeClickthrough(gaugeName)
391
assert(gaugesTable[gaugeName], "disableGaugeClickthrough: no such gauge exists.")
392
disableClickthrough(gaugeName .. "_back")
393
disableClickthrough(gaugeName .. "_front")
394
disableClickthrough(gaugeName .. "_text")
395
end
396
397
--- Set gauge to have a tooltip
398
--- @param gaugeName
399
--- @param text the tooltip text
400
--- @param duration tooltip duration
401
function setGaugeToolTip(gaugeName, text, duration)
402
duration = duration or 0
403
assert(gaugesTable[gaugeName], "setGaugeToolTip: no such gauge exists.")
404
setLabelToolTip(gaugeName .. "_text", text, duration)
405
end
406
407
--- Reset gauge tooltip
408
--- @param gaugeName
409
function resetGaugeToolTip(gaugeName)
410
assert(gaugesTable[gaugeName], "resetGaugeToolTip: no such gauge exists.")
411
resetLabelToolTip(gaugeName .. "_text")
412
end
413
414
--- Pads a hex number to ensure a minimum of 2 digits.
415
---
416
--- @usage Following command will returns "F0".
417
--- <pre>
418
--- PadHexNum("F")
419
--- </pre>
420
function PadHexNum(incString)
421
assert(type(incString) == 'string', 'PadHexNum: bad argument #1 type (expected string, got '..type(incString)..'!)')
422
local l_Return = incString
423
if tonumber(incString, 16) < 16 then
424
if tonumber(incString, 16) < 10 then
425
l_Return = "0" .. l_Return
426
elseif tonumber(incString, 16) > 10 then
427
l_Return = l_Return .. "0"
428
end
429
end
430
431
return l_Return
432
end
433
434
435
436
--- Converts an RGB value into an HTML compliant(label usable) HEX number.
437
--- This function is colorNames aware and can take any defined global color as its first argument.
438
---
439
--- @usage Both following commands will returns "FFFFFF".
440
--- <pre>
441
--- RGB2Hex(255,255,255)
442
--- RGB2Hex("white")
443
--- </pre>
444
---
445
--- @see showColor
446
function RGB2Hex(red, green, blue)
447
assert(red, "RGB2Hex: require at least one argument (color_name or r,g,b)!")
448
local l_Red, l_Green, l_Blue = 0, 0, 0
449
if green == nil then
450
-- Not an RGB but a "color" instead!
451
l_Red, l_Green, l_Blue = getRGB(red)
452
else -- Nope, true color here
453
l_Red, l_Green, l_Blue = red, green, blue
454
end
455
456
return PadHexNum(string.format("%X", l_Red)) ..
457
PadHexNum(string.format("%X", l_Green)) ..
458
PadHexNum(string.format("%X", l_Blue))
459
end
460
461
462
463
--- Get RGB component from color name.
464
---
465
--- @usage Following will display "0.255.0" on your screen.
466
--- <pre>
467
--- local red, green, blue = getRGB("green")
468
--- echo(red .. "." .. green .. "." .. blue )
469
--- </pre>
470
function getRGB(colorName)
471
assert(type(colorName) == 'string', 'getRGB: bad argument #1 type (expected string, got '..type(colorName)..'!)')
472
local red = color_table[colorName][1]
473
local green = color_table[colorName][2]
474
local blue = color_table[colorName][3]
475
return red, green, blue
476
end
477
478
479
480
--- Make your very own customized gauge with this function.
481
---
482
--- @usage This would make a gauge at that's 300px width, 20px in height, located at Xpos and Ypos and is green.
483
--- <pre>
484
--- createGauge("healthBar", 300, 20, 30, 300, nil, 0, 255, 0)
485
--- </pre>
486
--- @usage The second example is using the same names you'd use for something like fg() or bg().
487
--- <pre>
488
--- createGauge("healthBar", 300, 20, 30, 300, nil, "green")
489
--- </pre>
490
--- @usage Finally we'll add some text to our gauge.
491
--- <pre>
492
--- createGauge("healthBar", 300, 20, 30, 300, "Now with some text", "green")
493
--- </pre>
494
--- @usage You can add an orientation argument as well now:
495
--- <pre>
496
--- createGauge("healthBar", 300, 20, 30, 300, "Now with some text", "green", "horizontal, vertical, goofy, or batty")
497
--- </pre>
498
function createGauge(windowname, gaugeName, width, height, x, y, gaugeText, r, g, b, orientation)
499
--Make windowname optional
500
if type(gaugeName) == "number" then
501
orientation = b
502
b = g
503
g = r
504
r = gaugeText
505
gaugeText = y
506
y = x
507
x = height
508
height = width
509
width = gaugeName
510
gaugeName = windowname
511
windowname = nil
512
end
513
windowname = windowname or "main"
514
gaugeText = gaugeText or ""
515
if type(r) == "string" then
516
orientation = g
517
r, g, b = getRGB(r)
518
elseif r == nil then
519
orientation = orientation or g
520
-- default colors
521
r, g, b = 128, 128, 128
522
end
523
524
assert(type(x) == 'number', 'createGauge: expected x to be a number (got '..type(x)..'!)')
525
assert(type(y) == 'number', 'createGauge: expected y to be a number (got '..type(y)..'!)')
526
assert(type(width) == 'number', 'createGauge: expected width to be a number (got '..type(width)..'!)')
527
assert(type(height) == 'number', 'createGauge: expected height to be a number (got '..type(height)..'!)')
528
529
orientation = orientation or "horizontal"
530
assert(table.contains({ "horizontal", "vertical", "goofy", "batty" }, orientation), "createGauge: orientation must be horizontal, vertical, goofy, or batty")
531
local tbl = { width = width, height = height, x = x, y = y, text = gaugeText, r = r, g = g, b = b, orientation = orientation, value = 1 }
532
createLabel(windowname, gaugeName .. "_back", 0, 0, 0, 0, 1)
533
setBackgroundColor(gaugeName .. "_back", r, g, b, 100)
534
535
createLabel(windowname, gaugeName .. "_front", 0, 0, 0, 0, 1)
536
setBackgroundColor(gaugeName .. "_front", r, g, b, 255)
537
538
createLabel(windowname, gaugeName .. "_text", 0, 0, 0, 0, 1)
539
setBackgroundColor(gaugeName .. "_text", 0, 0, 0, 0)
540
541
-- save new values in table
542
gaugesTable[gaugeName] = tbl
543
resizeGauge(gaugeName, tbl.width, tbl.height)
544
moveGauge(gaugeName, tbl.x, tbl.y)
545
setGaugeText(gaugeName, gaugeText, "black")
546
showGauge(gaugeName)
547
end
548
549
550
551
--- Use this function when you want to change the gauges look according to your values.
552
--- Typical usage would be in a prompt with your current health or whatever value, and throw
553
--- in some variables instead of the numbers.
554
---
555
--- @usage In that example, we'd change the looks of the gauge named healthBar and make it fill
556
--- to half of its capacity. The height is always remembered.
557
--- <pre>
558
--- setGauge("healthBar", 200, 400)
559
--- </pre>
560
--- @usage Change the text on your gauge.
561
--- <pre>
562
--- setGauge("healthBar", 200, 400, "some text")
563
--- </pre>
564
function setGauge(gaugeName, currentValue, maxValue, gaugeText)
565
assert(gaugesTable[gaugeName], "setGauge: no such gauge exists.")
566
assert(tonumber(currentValue) ~= nil, 'setGauge: bad argument #2 type (unable to convert '..type(currentValue)..' to a number!)')
567
assert(tonumber(maxValue) ~= nil, 'setGauge: bad argument #3 type (unable to convert '..type(maxValue)..' to a number!)')
568
local value = currentValue / maxValue
569
-- save new values in table
570
gaugesTable[gaugeName].value = value
571
local info = gaugesTable[gaugeName]
572
local x, y, w, h = info.x, info.y, info.width, info.height
573
574
if info.orientation == "horizontal" then
575
resizeWindow(gaugeName .. "_front", w * value, h)
576
moveWindow(gaugeName .. "_front", x, y)
577
elseif info.orientation == "vertical" then
578
resizeWindow(gaugeName .. "_front", w, h * value)
579
moveWindow(gaugeName .. "_front", x, y + h * (1 - value))
580
elseif info.orientation == "goofy" then
581
resizeWindow(gaugeName .. "_front", w * value, h)
582
moveWindow(gaugeName .. "_front", x + w * (1 - value), y)
583
elseif info.orientation == "batty" then
584
resizeWindow(gaugeName .. "_front", w, h * value)
585
moveWindow(gaugeName .. "_front", x, y)
586
end
587
if gaugeText then
588
setGaugeText(gaugeName, gaugeText)
589
end
590
end
591
592
593
594
--- Make a new console window with ease. The default background is black and text color white.
595
--- If you wish to change the color you can easily do this when updating your text or manually somewhere, using
596
--- setFgColor() and setBackgroundColor().
597
---
598
--- @usage This will create a miniconsole window that has a font size of 8pt, will display 80 characters in width,
599
--- hold a maximum of 20 lines and be place at 200x400 of your Mudlet window.
600
--- <pre>
601
--- createConsole("myConsoleWindow", 8, 80, 20, 200, 400)
602
--- </pre>
603
function createConsole(windowName, consoleName, fontSize, charsPerLine, numberOfLines, Xpos, Ypos)
604
if Ypos == nil then
605
Ypos = Xpos
606
Xpos = numberOfLines
607
numberOfLines = charsPerLine
608
charsPerLine = fontSize
609
fontSize = consoleName
610
consoleName = windowName
611
windowName = "main"
612
end
613
assert(type(windowName) == 'string', 'createConsole: invalid type for windowName (expected string, got '..type(windowName)..'!)')
614
assert(type(consoleName) == 'string', 'createConsole: invalid type for consoleName (expected string, got '..type(consoleName)..'!)')
615
assert(type(fontSize) == 'number', 'createConsole: invalid type for fontSize (expected number, got '..type(fontSize)..'!)')
616
assert(type(charsPerLine) == 'number', 'createConsole: invalid type for charsPerLine (expected number, got '..type(charsPerLine)..'!)')
617
assert(type(numberOfLines) == 'number', 'createConsole: invalid type for numberOfLines (expected number, got '..type(numberOfLines)..'!)')
618
assert(type(Xpos) == 'number', 'createConsole: invalid type for Xpos (expected number, got '..type(Xpos)..'!)')
619
assert(type(Ypos) == 'number', 'createConsole: invalid type for Ypos (expected number, got '..type(Ypos)..'!)')
620
createMiniConsole(windowName, consoleName, 0, 0, 1, 1)
621
setMiniConsoleFontSize(consoleName, fontSize)
622
local x, y = calcFontSize( fontSize )
623
resizeWindow(consoleName, x * charsPerLine, y * numberOfLines)
624
setWindowWrap(consoleName, charsPerLine)
625
moveWindow(consoleName, Xpos, Ypos)
626
627
setBackgroundColor(consoleName, 0, 0, 0, 0)
628
setFgColor(consoleName, 255, 255, 255)
629
end
630
631
632
633
634
--- Function will gag the whole line. <b>Use deleteLine() instead.</b>
635
function gagLine()
636
deleteLine()
637
end
638
639
640
641
--- Replaces all occurrences of what in the current line with <i>with</i>.
642
---
643
--- @usage This will replace all occurrences of John with the word Doe.
644
--- <pre>
645
--- replaceAll("John", "Doe")
646
---
647
--- -- also handles recursive matches:
648
--- replaceAll("you", "you and me")
649
--- </pre>
650
function replaceAll(word, what, keepColor)
651
assert(type(word) == 'string', 'replaceAll: bad argument #1 type (expected string, got '..type(word)..'!)')
652
assert(type(what) == 'string', 'replaceAll: bad argument #2 type (expected string, got '..type(what)..'!)')
653
local getCurrentLine, selectSection, replace = getCurrentLine, selectSection, replace
654
local startp, endp = 1, 1
655
while true do
656
startp, endp = getCurrentLine():find(word, endp)
657
if not startp then
658
break
659
end
660
selectSection(startp - 1, endp - startp + 1)
661
replace(what, keepColor)
662
endp = endp + (#what - #word) + 1 -- recalculate the new word ending to start search from there
663
end
664
end
665
666
667
668
--- Replace an entire line with a string you'd like.
669
---
670
--- @see deleteLine
671
function replaceLine(window, text)
672
assert(type(window) == 'string', 'replaceLine: bad argument #1 type (expected string, got '..type(window)..'!)')
673
if not text then
674
selectCurrentLine()
675
else
676
selectCurrentLine(window)
677
end
678
replace(window, text)
679
end
680
681
682
683
--- Default resizeEvent handler function. Overwrite this function to make a custom event handler
684
--- if the main window is being resized. <br/><br/>
685
---
686
--- The standard implementation of this function does nothing. However, this function gets called whenever
687
--- the main window is being manually resized. You can overwrite this function in your own scripts to handle window
688
--- resize events yourself and e.g. adjust the screen position and size of your mini console windows, labels or
689
--- other relevant GUI elements in your scripts that depend on the size of the main Window. To override this
690
--- function you can simply put a function with the same name in one of your scripts thus overwriting the
691
--- original empty implementation of this function.
692
--- <pre>
693
--- function handleWindowResizeEvent()
694
--- -- determine the size of your screen
695
--- WindowWidth=0;
696
--- WindowHeight=0;
697
--- WindowWidth, WindowHeight = getMainWindowSize();
698
--- -- move mini console "sys" to the far right side of the screen whenever the screen gets resized
699
--- moveWindow("sys",WindowWidth-300,0)
700
--- end
701
--- </pre>
702
function handleWindowResizeEvent()
703
end
704
705
706
--- Sets current background color to a named color.
707
---
708
--- @usage Set background color to magenta.
709
--- <pre>
710
--- bg("magenta")
711
---
712
--- bg("my miniconsole", "blue")
713
--- </pre>
714
---
715
--- @see fg
716
--- @see showColors
717
function bg(console, colorName)
718
colorName = colorName or console
719
if colorName == nil then
720
error("bg: bad argument #1 type (color name as string expected, got nil)!")
721
end
722
if not color_table[colorName] then
723
error(string.format("bg: '%s' color doesn't exist - see showColors()", colorName))
724
end
725
local alpha = color_table[colorName][4] or 255
726
727
if console == colorName or console == "main" then
728
setBgColor(color_table[colorName][1], color_table[colorName][2], color_table[colorName][3], alpha)
729
else
730
setBgColor(console, color_table[colorName][1], color_table[colorName][2], color_table[colorName][3], alpha)
731
end
732
end
733
734
735
736
--- Sets current foreground color to a named color.
737
---
738
--- @usage Set foreground color to black.
739
--- <pre>
740
--- fg("black")
741
--- </pre>
742
---
743
--- @see bg
744
--- @see showColors
745
function fg(console, colorName)
746
colorName = colorName or console
747
if colorName == nil then
748
error("fg: bad argument #1 type (color name as string expected, got nil)!")
749
end
750
if not color_table[colorName] then
751
error(string.format("fg: '%s' color doesn't exist - see showColors()", colorName))
752
end
753
754
if console == colorName or console == "main" then
755
setFgColor(color_table[colorName][1], color_table[colorName][2], color_table[colorName][3])
756
else
757
setFgColor(console, color_table[colorName][1], color_table[colorName][2], color_table[colorName][3])
758
end
759
end
760
761
762
763
--- Replaces the given wildcard (as a number) with the given text.
764
---
765
--- @usage Replace "goodbye" with "hello" on a trigger of "^You wave (goodbye)\.$"
766
--- <pre>
767
--- replaceWildcard(2, "hello")
768
--- </pre>
769
--- Is equivalent to doing:
770
--- <pre>
771
--- selectString(matches[2], 1)
772
--- replace("hello")
773
--- </pre>
774
function replaceWildcard(what, replacement, keepColor)
775
if replacement == nil or what == nil then
776
return
777
end
778
selectCaptureGroup(what)
779
replace(replacement, keepColor)
780
end
781
782
-- internal sorting function, sorts first by hue, then luminosity, then value
783
local sortColorsByHue = function(lhs,rhs)
784
local lh,ll,lv = unpack(lhs.sort)
785
local rh,rl,rv = unpack(rhs.sort)
786
if lh < rh then
787
return true
788
elseif lh > rh then
789
return false
790
elseif ll < rl then
791
return true
792
elseif ll > rl then
793
return false
794
else
795
return lv < rv
796
end
797
end
798
799
-- internal sorting function, removes _ from snake_case and compares to camelCase
800
local sortColorsByName = function(a,b)
801
local aname = string.gsub(string.lower(a.name), "_", "")
802
local bname = string.gsub(string.lower(b.name), "_", "")
803
return aname < bname
804
end
805
806
-- internal function, converts rgb to hsv
807
-- found at https://github.com/EmmanuelOga/columns/blob/master/utils/color.lua#L89
808
local rgbToHsv = function(r, g, b)
809
r, g, b = r / 255, g / 255, b / 255
810
local max, min = math.max(r, g, b), math.min(r, g, b)
811
local h, s, v
812
v = max
813
814
local d = max - min
815
if max == 0 then
816
s = 0
817
else
818
s = d / max
819
end
820
821
if max == min then
822
h = 0 -- achromatic
823
else
824
if max == r then
825
h = (g - b) / d
826
if g < b then h = h + 6 end
827
elseif max == g then
828
h = (b - r) / d + 2
829
elseif max == b then
830
h = (r - g) / d + 4
831
end
832
h = h / 6
833
end
834
835
return h, s, v
836
end
837
838
-- internal stepping function, removes some of the noise for a more pleasing sort
839
-- cribbed from the python on https://www.alanzucconi.com/2015/09/30/colour-sorting/
840
local step = function(r,g,b)
841
local lum = math.sqrt( .241 * r + .691 * g + .068 * b )
842
local reps = 8
843
844
local h, s, v = rgbToHsv(r,g,b)
845
846
local h2 = math.floor(h * reps)
847
local v2 = math.floor(v * reps)
848
if h2 % 2 == 1 then
849
v2 = reps - v2
850
lum = reps - lum
851
end
852
return h2, lum, v2
853
end
854
855
local function calc_luminosity(r,g,b)
856
r = r < 11 and r / (255 * 12.92) or ((0.055 + r / 255) / 1.055) ^ 2.4
857
g = g < 11 and g / (255 * 12.92) or ((0.055 + g / 255) / 1.055) ^ 2.4
858
b = b < 11 and b / (255 * 12.92) or ((0.055 + b / 255) / 1.055) ^ 2.4
859
return (0.2126 * r) + (0.7152 * g) + (0.0722 * b)
860
end
861
862
863
--- Prints out a formatted list of all available named colors (EXCEPT FOR
864
--- the 256 colors with names of form "ansi_###" where ### is 000 to 255),
865
--- optional args specifies:
866
--- * (number) number of columns to print in, defaults to 4;
867
--- * (string) substring required to match to include in output, defaults to
868
--- showing all if not supplied;
869
--- * (boolean) whether to sort the output, defaults to false.
870
--- @usage Print list in 4 columns by default.
871
--- <pre>
872
--- showColors()
873
--- </pre>
874
--- @usage Print list in 2 columns.
875
--- <pre>
876
--- showColors(2)
877
--- </pre>
878
---
879
--- @see color_table
880
function showColors(...)
881
local cols, search, sort = 4, "", false
882
for _, val in ipairs(arg) do
883
if type(val) == "string" then
884
search = val:lower()
885
elseif type(val) == "number" then
886
cols = val
887
elseif type(val) == "boolean" then
888
sort = val
889
end
890
end
891
892
local colors = {}
893
for k, v in pairs(color_table) do
894
local color = {}
895
color.rgb = v
896
color.name = k
897
color.sort = {step(unpack(v))}
898
if not string.find(k, "ansi_%d%d%d") then
899
table.insert(colors,color)
900
end
901
end
902
903
if sort then
904
table.sort(colors, sortColorsByName)
905
else
906
table.sort(colors,sortColorsByHue)
907
end
908
local i = 1
909
for _, k in ipairs(colors) do
910
if k.name:lower():find(search) then
911
local v = k.rgb
912
local fgc = "white"
913
if calc_luminosity(v[1],v[2],v[3]) > 0.5 then
914
fgc = "black"
915
end
916
cechoLink(string.format('<%s:%s> %-23s<reset> ',fgc,k.name,k.name), [[appendCmdLine("]] .. k.name .. [[")]], table.concat(v, ", "), true)
917
if i == cols then
918
echo("\n")
919
i = 1
920
else
921
i = i + 1
922
end
923
end
924
end
925
if i ~= 1 then echo("\n") end
926
end
927
928
--- Prints out a sorted, formatted list of the 256 colors with names of form
929
--- "ansi_###" where ### is 000 to 255), optional arg specifies:
930
--- * (number) number of columns to print in, defaults to 4;
931
--- @usage Print list in 4 columns by default.
932
--- <pre>
933
--- showAnsiColors()
934
--- </pre>
935
--- @usage Print list in 2 columns.
936
--- <pre>
937
--- showAnsiColors(2)
938
--- </pre>
939
---
940
--- @see color_table
941
function showAnsiColors(...)
942
local cols = 8
943
for _, val in ipairs(arg) do
944
if type(val) == "number" then
945
cols = val
946
end
947
end
948
949
local colors = {}
950
for k, v in pairs(color_table) do
951
-- Only use the ansi_### 256 colors entries
952
if string.find(k, "ansi_%d%d%d") then
953
table.insert(colors,k)
954
end
955
end
956
957
table.sort(colors)
958
959
local i = 1
960
for _, k in ipairs(colors) do
961
local v = color_table[k]
962
local fgc = "white"
963
if calc_luminosity(v[1],v[2],v[3]) > 0.5 then
964
fgc = "black"
965
end
966
cechoLink(string.format('<%s:%s> %8s <reset> ',fgc,k,k), [[printCmdLine("]] .. k .. [[")]], table.concat(v, ", "), true)
967
if i == cols then
968
echo("\n")
969
i = 1
970
else
971
i = i + 1
972
end
973
end
974
if i ~= 1 then echo("\n") end
975
end
976
977
978
--- <b><u>TODO</u></b> resizeGauge(gaugeName, width, height)
979
function resizeGauge(gaugeName, width, height)
980
assert(gaugesTable[gaugeName], "resizeGauge: no such gauge exists.")
981
assert(type(width) == 'number', 'resizeGauge: bad argument #2 type (expected number, got '..type(width)..'!)')
982
assert(type(height) == 'number', 'resizeGauge: bad argument #3 type (expected number, got '..type(height)..')')
983
resizeWindow(gaugeName .. "_back", width, height)
984
resizeWindow(gaugeName .. "_text", width, height)
985
-- save new values in table
986
gaugesTable[gaugeName].width, gaugesTable[gaugeName].height = width, height
987
setGauge(gaugeName, gaugesTable[gaugeName].value, 1)
988
end
989
990
991
992
--- <b><u>TODO</u></b> setGaugeStyleSheet(gaugeName, css, cssback)
993
function setGaugeStyleSheet(gaugeName, css, cssback, csstext)
994
if not setLabelStyleSheet then
995
return
996
end -- mudlet 1.0.5 and lower compatibility
997
assert(gaugesTable[gaugeName], "setGaugeStyleSheet: no such gauge exists.")
998
assert(type(css) == 'string', 'setGaugeStyleSheet: bad argument #2 type (expected string, got '..type(css)..'!)')
999
setLabelStyleSheet(gaugeName .. "_back", cssback or css)
1000
setLabelStyleSheet(gaugeName .. "_front", css)