steed/stead/input.lua

322 lines
5.3 KiB
Lua
Raw Normal View History

2010-06-07 16:46:49 +03:00
kbden = {
shifted = {
["1"] = "!",
["2"] = "@",
["3"] = "#",
["4"] = "$",
["5"] = "%",
2010-10-30 15:54:41 +03:00
["6"] = "^",
2010-06-07 16:46:49 +03:00
["7"] = "&",
["8"] = "*",
["9"] = "(",
["0"] = ")",
["-"] = "_",
["="] = "+",
["["] = "{",
["]"] = "}",
["\\"] = "|",
[";"] = ":",
["'"] = "\"",
[","] = "<",
["."] = ">",
["/"] = "?",
}
}
kbdru = {
["q"] = "й",
["w"] = "ц",
["e"] = "у",
["r"] = "к",
["t"] = "е",
["y"] = "н",
["u"] = "г",
["i"] = "ш",
["o"] = "щ",
["p"] = "з",
["["] = "х",
["]"] = "ъ",
["a"] = "ф",
["s"] = "ы",
["d"] = "в",
["f"] = "а",
["g"] = "п",
["h"] = "р",
["j"] = "о",
["k"] = "л",
["l"] = "д",
[";"] = "ж",
["'"] = "э",
["z"] = "я",
["x"] = "ч",
["c"] = "с",
["v"] = "м",
["b"] = "и",
["n"] = "т",
["m"] = "ь",
[","] = "б",
["."] = "ю",
["`"] = "ё",
shifted = {
["q"] = "Й",
["w"] = "Ц",
["e"] = "У",
["r"] = "К",
["t"] = "Е",
["y"] = "Н",
["u"] = "Г",
["i"] = "Ш",
["o"] = "Щ",
["p"] = "З",
["["] = "Х",
["]"] = "Ъ",
["a"] = "Ф",
["s"] = "Ы",
["d"] = "В",
["f"] = "А",
["g"] = "П",
["h"] = "Р",
["j"] = "О",
["k"] = "Л",
["l"] = "Д",
[";"] = "Ж",
["'"] = "Э",
["z"] = "Я",
["x"] = "Ч",
["c"] = "С",
["v"] = "М",
["b"] = "И",
["n"] = "Т",
["m"] = "Ь",
[","] = "Б",
["."] = "Ю",
["`"] = "Ё",
["1"] = "!",
["2"] = "@",
["3"] = "#",
["4"] = ";",
["5"] = "%",
["6"] = ":",
["7"] = "?",
["8"] = "*",
["9"] = "(",
["0"] = ")",
["-"] = "_",
["="] = "+",
}
}
kbdlower = {
['А'] = 'а',
['Б'] = 'б',
['В'] = 'в',
['Г'] = 'г',
['Д'] = 'д',
['Е'] = 'е',
['Ё'] = 'ё',
['Ж'] = 'ж',
['З'] = 'з',
['И'] = 'и',
['Й'] = 'й',
['К'] = 'к',
['Л'] = 'л',
['М'] = 'м',
['Н'] = 'н',
['О'] = 'о',
['П'] = 'п',
['Р'] = 'р',
['С'] = 'с',
['Т'] = 'т',
['У'] = 'у',
['Ф'] = 'ф',
['Х'] = 'х',
['Ц'] = 'ц',
['Ч'] = 'ч',
['Ш'] = 'ш',
['Щ'] = 'щ',
['Ъ'] = 'ъ',
['Э'] = 'э',
['Ь'] = 'ь',
['Ю'] = 'ю',
['Я'] = 'я',
}
2010-06-07 16:35:16 +03:00
function tolow(s)
if not s then
return
end
s = s:lower();
local xlat = kbdlower
if xlat then
local k,v
for k,v in pairs(xlat) do
s = s:gsub(k,v);
end
end
return s;
end
function kbdxlat(s)
local kbd
if s == 'space' then
return ' '
end
if s == 'return' then
return '\n'
end
if s:len() > 1 then
return
end
2010-07-18 07:39:39 +03:00
if input.kbd_alt and
(game.codepage == 'UTF-8' or game.codepage == 'utf-8') then
2010-06-14 16:01:19 +03:00
kbd = kbdru;
2010-06-07 16:35:16 +03:00
else
2010-06-14 16:01:19 +03:00
kbd = kbden
2010-06-07 16:35:16 +03:00
end
if kbd and input.kbd_shift then
kbd = kbd.shifted;
end
if not kbd[s] then
if input.kbd_shift then
return s:upper();
end
return s;
end
return kbd[s]
end
2010-06-23 15:09:33 +03:00
game.action = stead.hook(game.action, function (f, s, cmd, ...)
2010-06-07 16:35:16 +03:00
if cmd == 'kbd_enter' then
2010-07-29 11:57:00 +03:00
local r,v
2010-06-11 09:55:21 +03:00
if here().inp_enter then
2010-07-29 11:57:00 +03:00
r,v = call(here(), 'inp_enter');
elseif s.inp_enter then
r,v = call(s, 'inp_enter');
2010-06-11 09:55:21 +03:00
end
2010-09-25 16:36:19 +03:00
-- if r == nil and v == nil then
-- return nil, true
-- end
2010-07-29 11:57:00 +03:00
return r,v -- nothing todo
2010-06-07 16:35:16 +03:00
end
2011-02-23 12:11:27 +02:00
return f(s, cmd, ...)
2010-06-07 16:35:16 +03:00
end)
2010-06-14 15:55:29 +03:00
lookup_inp = function()
local i,o
for i,o in opairs(objs()) do
o = ref(o)
if o._edit then
return o
end
end
end
2010-06-07 16:35:16 +03:00
input_kbd = function(s, down, key)
if not input._txt then
return
end
if key:find("shift") then
input.kbd_shift = down
elseif key:find("alt") and down then
input.kbd_alt = not input.kbd_alt;
elseif down then
if key == "return" then
2010-06-14 15:55:29 +03:00
local o = lookup_inp();
if o then
o._edit = false
o._txt = input._txt
input._txt = false
return "kbd_enter"
end
2010-06-16 08:05:23 +03:00
return
2010-06-07 16:35:16 +03:00
end
if key == "backspace" then
if input._txt == '' then
return
end
if input._txt:byte(input._txt:len()) >= 128 then
input._txt = input._txt:sub(1, input._txt:len() - 2);
else
input._txt = input._txt:sub(1, input._txt:len() - 1);
end
2010-06-25 12:31:54 +03:00
return "wait"
2010-06-07 16:35:16 +03:00
end
local c = kbdxlat(key);
if not c then return end
input._txt = input._txt..c;
2010-06-25 12:31:54 +03:00
return "wait"
2010-06-07 16:35:16 +03:00
end
end
2010-07-22 13:39:48 +03:00
stead.module_init(function()
2010-07-22 14:47:55 +03:00
input.cursor = '_'
2010-07-22 13:39:48 +03:00
input.key = stead.hook(input.key,
function(f, ...)
2011-02-23 12:11:27 +02:00
local r = input_kbd(...)
2010-07-22 13:39:48 +03:00
if r then return r end
2011-02-23 12:11:27 +02:00
return f(...)
2010-07-22 13:39:48 +03:00
end)
2010-06-15 21:58:52 +03:00
end)
2010-06-07 16:35:16 +03:00
2010-06-16 11:36:19 +03:00
function input_esc(s)
2010-06-16 21:29:11 +03:00
local rep = function(s)
return txtnb(s)
end
2010-06-16 11:36:19 +03:00
if not s then return end
2010-10-30 15:54:41 +03:00
-- return s:gsub("\\","\\\\\\\\"):gsub(">","\\\\>"):gsub("%^","\\%^"):
return s:gsub("[^ ]+", rep):gsub("[ \t]", rep);
2010-06-16 11:36:19 +03:00
end
2010-06-25 12:31:54 +03:00
function inp(n, info, txt)
if type(n) ~= 'string' or type(info) ~= 'string' then
error ("Wrong parameter to inp.", 2);
end
local v = { nam = n, _txt = '', info = info }
2010-06-07 16:35:16 +03:00
if txt then
v._txt = txt
end
v.dsc = function(s)
if s._edit then
2010-06-16 21:29:11 +03:00
return s.info..input_esc(input._txt)..input.cursor
2010-06-07 16:35:16 +03:00
end
2010-06-16 21:29:11 +03:00
return s.info..input_esc(s._txt)
2010-06-07 16:35:16 +03:00
end
v.text = function(s)
if s._edit then return input._txt end
return s._txt
end
v.match = function(s, str)
local aa = tolow(tostring(str)):gsub("\*",".*"):gsub("[?]",".?");
local bb = tolow(tostring(s:text()));
if bb:find("^"..aa.."$") then
return true
end
end
v.act = function(s)
if input._txt and not s._edit then return true end -- somewhere else
s._edit = not s._edit;
if s._edit then
input._txt = s._txt;
else
s._txt = input._txt
input._txt = false
end
return true
end
2010-06-25 12:31:54 +03:00
v.save = function(self, name, h, need)
if need then
h:write(stead.string.format("%s = inp (%q, %q, %q);\n",
name, self.nam, self.info, self._txt))
end
savemembers(h, self, name, false);
end
2010-06-07 16:35:16 +03:00
return obj(v)
end
2010-07-06 13:15:27 +03:00
-- vim:ts=4