File: gui.lua

package info (click to toggle)
instead 3.5.2%2Bdfsg-0.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,316 kB
  • sloc: ansic: 28,336; sh: 452; makefile: 236
file content (519 lines) | stat: -rw-r--r-- 10,629 bytes parent folder | download | duplicates (2)
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
-- sdl-instead specific methods

local std = stead

local instead = std.obj {
	nam = '@instead';

	version_table = {3, 5, 2};

	ini = function(s) -- after reset always do fade
		s.need_fading(true)
	end;
}

-- luacheck: read globals instead_clipboard
-- luacheck: read globals instead_wait_use
-- luacheck: read globals instead_grab_events
-- luacheck: read globals instead_text_input
-- luacheck: no self

instead.clipboard = instead_clipboard
instead.wait_use = instead_wait_use
instead.grab_events = instead_grab_events
instead.text_input = instead_text_input

function instead.atleast(...)
	for k, v in std.ipairs {...} do
		if std.type(v) ~= 'number' then
			return false
		end
		if v > (instead.version_table[k] or 0) then
			return false
		end
		if v < (instead.version_table[k] or 0) then
			return true
		end
	end
	return true
end

function instead.version(...)
	if #{...} == 0 then
		return instead.version_table
	end
	if not instead.atleast(...) then
		local v = false
		for _, n in std.ipairs({...}) do
			if std.type(n) ~= 'number' then
				std.err([[Wrong instead.version argument: ]]..std.tostr(n), 2)
			end
			v = (v and (v .. '.') or '').. std.tostr(n)
		end
		std.err ([[The game requires instead engine of version ]] ..(v or '???').. [[ or higher.
		https://instead-hub.github.io]], 2)
	end
end

function instead.need_fading(v)
	local ov = instead.__need_fading
	if v ~= nil then
		instead.__need_fading = v
	end
	return ov
end

local iface = std '@iface'
local type = std.type

local dict = {}

instead.inv_delim = '\n'
instead.hinv_delim = ' | '
instead.ways_delim = ' | '

local function get_bool(o, nam)
	if type(o[nam]) == 'boolean' then
		return o[nam]
	end
	if type(o[nam]) == 'function' then
		return o[nam](o)
	end
	return nil
end

instead.notitle = false

instead.get_title = std.cacheable('title', function()
	if get_bool(instead, 'notitle') then
		return
	end
	return iface:fmt(iface:bold(std.titleof(stead.here())), false)
end)

instead.noways = false

instead.get_ways = std.cacheable('ways', function()
	if get_bool(instead, 'noways') then
		return
	end
	local str = iface:cmd("way");
	if str then
		str = std.string.gsub(str, '\n$','');
		str = std.string.gsub(str, '\\?['..std.delim ..']',
			{ [std.delim] = instead.ways_delim, [ '\\'..std.delim ] = std.delim });
		return iface:center(str);
	end
	return str
end)

instead.noinv = false

instead.get_inv = std.cacheable('inv', function(horiz)
	if get_bool(instead, 'noinv') then
		return
	end
	local str = iface:cmd("inv");
	if str then
		str = std.string.gsub(str, '\n$','');
		if not horiz then
			str = std.string.gsub(str, '\\?['.. std.delim ..']',
				{ [std.delim] = instead.inv_delim, ['\\'..std.delim] = std.delim });
		else
			str = std.string.gsub(str, '\\?['.. std.delim ..']',
				{ [std.delim] = instead.hinv_delim, ['\\'..std.delim] = std.delim });
		end
	end
	return str
end)

instead.nopic = false

-- luacheck: push ignore savedpicture
local savedpicture
-- luacheck: pop
instead.get_picture = std.cacheable('pic', function()
	if get_bool(instead, 'nopic') then
		return
	end
	local s = stead.call(std.here(), 'pic')
	if not s then
		s = stead.call(std.ref 'game', 'pic')
	end
	savedpicture = s -- to save picture sprite from unload
	return s and std.tostr(s)
end)

local last_picture

instead.fading_value = 4 -- default fading

function instead.get_fading()
	if not instead.fading then
		return false
	end

	if type(instead.fading) == 'function' and
		not instead.fading() then
		return false
	end

	return true, instead.fading_value
end

function instead.fading()
	local pic = instead.get_picture()

	if type(pic) == 'string' and pic:find('spr:', 1, true) == 1 then
		pic = 'spr:'
	end

	if std.me():need_scene() or instead.need_fading() or pic ~= last_picture then
		last_picture = pic
		return true
	end
end

function instead.get_restart()
	local restart = instead.__restart or false
	instead.__restart = nil
	return restart
end


function instead.get_menu()
	local menu = instead.__menu
	instead.__menu = nil
	return menu
end

instead.nosave = false
instead.noautosave = false

function instead.isEnableSave()
	local s = get_bool(instead, 'nosave')
	return not s
end

function instead.isEnableAutosave()
	if instead.get_autosave() then
		return true
	end
	return not get_bool(instead, 'noautosave')
end

function instead.autosave(slot)
	instead.__autosave = true
	instead.__autosave_slot = slot
end

function instead.get_autosave()
	return instead.__autosave or false, instead.__autosave_slot
end

function instead.menu(n)
	if n == nil then
		n = 'main'
	elseif type(n) ~= 'string' and n ~= false then
		n = 'toggle'
	end
	instead.__menu = n
end

function instead.restart(v)
	if v == false then v = false else v = true end
	instead.__restart = v
end

function iface:title() -- hide title
	return
end

-- luacheck: globals stat
std.stat = std.class({
	__stat_type = true;
}, std.obj);

-- luacheck: globals menu
std.menu = std.class({
	__menu_type = true;
	new = function(_, v)
		if type(v) ~= 'table' then
			std.err ("Wrong argument to std.menu:"..std.tostr(v), 2)
		end
		v = std.obj(v)
--		std.setmt(v, self)
		return v
	end;
	inv = function(s, ...)
		local r, v
		if s.menu ~= nil then
			r, v = std.call(s, 'menu', ...) -- special method while in inv
		else
			r, v = std.call(s, 'act', ...) -- fallback to act
		end
		if not r and not v then
			return true, false -- menu mode
		end
		return r, v
	end;
}, std.obj);

std.setmt(std.phr, std.menu) -- make phrases menus
std.setmt(std.ref '@', std.menu) -- make xact menu

function iface:esc(str)
	str = str:gsub("\\?[\\<>]", { ['\\\\'] = '\\\\\\\\\\', ['>'] = iface:nb('>'), ['<'] = iface:nb('<') })
	return str
end

function iface:xref(str, o, ...)
	if type(str) ~= 'string' then
		std.err ("Wrong parameter to iface:xref: "..std.tostr(str), 2)
	end
	if not std.is_obj(o) or std.is_obj(o, 'stat') or o:disabled() then
		return str
	end
	local a = { ... }
	local args = ''
	for i = 1, #a do
		if type(a[i]) ~= 'string' and type(a[i]) ~= 'number' and type(a[i]) ~= 'boolean' then
			std.err ("Wrong argument to iface:xref: "..std.tostr(a[i]), 2)
		end
		args = args .. ' '..std.dump(a[i])
	end
	local xref = std.string.format("%s%s", std.deref_str(o), args)
	-- std.string.format("%s%s", iface:esc(std.deref_str(o)), iface:esc(args))
	if not dict[xref] then
		table.insert(dict, xref)
		dict[xref] = #dict
	end
	xref = std.tostr(dict[xref])
	if std.cmd[1] == 'way' then
		return std.string.format("<a:go %s>", xref)..str.."</a>"
	elseif std.is_obj(o, 'menu') or std.is_system(o) then
		return std.string.format("<a:act %s>", xref)..str.."</a>"
	elseif std.cmd[1] == 'inv' then
		return std.string.format("<a:use %s>", xref)..str.."</a>"
	end
	return std.string.format("<a:obj/act %s>", xref)..str.."</a>"
end

function iface:em(str)
	if type(str) == 'string' then
		return '<i>'..str..'</i>'
	end
end

function iface:center(str)
	if type(str) == 'string' then
		return '<c>'..str..'</c>'
	end
end

function iface:just(str)
	if type(str) == 'string' then
		return '<j>'..str..'</j>'
	end
end

function iface:left(str)
	if type(str) == 'string' then
		return '<l>'..str..'</l>'
	end
end

function iface:right(str)
	if type(str) == 'string' then
		return '<r>'..str..'</r>'
	end
end

function iface:bold(str)
	if type(str) == 'string' then
		return '<b>'..str..'</b>'
	end
end

function iface:top(str)
	if type(str) == 'string' then
		return '<t>'..str..'</t>'
	end
end

function iface:bottom(str)
	if type(str) == 'string' then
		return '<d>'..str..'</d>'
	end
end

function iface:middle(str)
	if type(str) == 'string' then
		return '<m>'..str..'</m>'
	end
end

function iface:nb(str)
	if type(str) == 'string' then
		return "<w:"..str:gsub("\\", "\\\\\\\\"):gsub(">","\\>"):gsub("%^","\\^")..">";
	end
end

function iface:anchor()
	return '<a:#>'
end

function iface:img(str)
	if str then
		return "<g:"..std.tostr(str)..">"
	end
end;

function iface:imgl(str)
	if str then
		return "<g:"..std.tostr(str).."\\|left>"
	end
end;

function iface:imgr(str)
	if str then
		return "<g:"..std.tostr(str).."\\|right>"
	end
end

function iface:under(str)
	if type(str) == 'string' then
		return "<u>"..str.."</u>"
	end
end;

function iface:st(str)
	if type(str) == 'string' then
		return "<s>"..str.."</s>"
	end
end

function iface:tab(str, al)
	if type(str) == 'number' then
		str = std.tostr(str)
	end
	if type(str) ~= 'string' then
		return
	end
	if al == 'right' then
		str = str .. ",right"
	elseif al == 'center' then
		str = str .. ",center"
	end
	return '<x:'..str..'>'
end

function iface:y(str, al)
	if stead.tonum(str) then
		str = stead.tostr(str)
	end
	if stead.type(str) ~= 'string' then
		return nil;
	end
	if al == 'middle' then
		str = str .. ",middle"
	elseif al == 'top' then
		str = str .. ",top"
	end
	return '<y:'..str..'>'
end;

function iface:input(event, ...)
	local input = std.ref '@input'
	if type(input) ~= 'table' then
		return
	end
	if event == 'kbd' then
		if type(input.key) == 'function' then
			return input:key(...); -- pressed, event
		end
	elseif event == 'mouse' then
		if type(input.click) == 'function' then
			return input:click(...); -- pressed, x, y, mb
		end
	elseif event == 'wheel' then
		if type(input.wheel) == 'function' then
			return input:wheel(...);
		end
	elseif event == 'finger' then
		if type(input.finger) == 'function' then
			return input:finger(...); -- pressed, x, y, finger
		end
	elseif event == 'event' then
		if type(input.event) == 'function' then
			return input:event(...);
		end
	elseif event == 'text' then
		if type(input.text) == 'function' then
			return input:text(...);
		end
	end
	return
end

local iface_cmd = iface.cmd -- save old

function iface:cmd(inp)
	local a = std.split(inp)
	if a[1] == 'act' or a[1] == 'use' or a[1] == 'go' then
		if a[1] == 'use' then
			local use = std.split(a[2], ',')
			for i = 1, 2 do
				local u = std.tonum(use[i])
				if u then
					use[i] = dict[u]
				end
			end
			a[2] = std.join(use, ',')
		elseif std.tonum(a[2]) then
			a[2] = dict[std.tonum(a[2])]
		end
		inp = std.join(a)
	end
	return iface_cmd(self, inp)
end

std.obj { -- input object
	nam = '@input';
};

-- some aliases
menu = std.menu
stat = std.stat

std.mod_init(function()
	std.rawset(_G, 'instead', instead)
	require "ext/sandbox"
end)

std.mod_cmd(function()
	instead.need_fading(false)
end)

std.mod_step(function(state)
	if state then
		dict = {}
	end
end)

std.mod_start(function()
	dict = {}
end)

std.mod_done(function()
	last_picture = nil
end)

std.mod_save(function()
	instead.__autosave = nil
	instead.__autosave_slot = nil
end)

if std.rawget(_G, 'DEBUG') then
	require 'dbg'
end