mod: rewrite inputbox with commands

This commit is contained in:
Qingping Hou
2012-04-09 17:17:23 +08:00
parent ecd56a3745
commit ff60a41604

View File

@@ -25,6 +25,7 @@ InputBox = {
fhash = "m25",
fheight = 25,
fwidth = 15,
commands = nil,
}
function InputBox:refreshText()
@@ -109,6 +110,8 @@ end
----------------------------------------------------------------------
function InputBox:input(ypos, height, title, d_text)
-- do some initilization
self:addAllCommands()
self.ypos = ypos
self.h = height
self.input_start_y = ypos + 35
self.input_cur_x = self.input_start_x
@@ -138,116 +141,20 @@ function InputBox:input(ypos, height, title, d_text)
local ev = input.waitForEvent()
ev.code = adjustKeyEvents(ev)
if ev.type == EV_KEY and ev.value == EVENT_VALUE_KEY_PRESS then
--local secs, usecs = util.gettime()
if ev.code == KEY_FW_UP then
elseif ev.code == KEY_FW_DOWN then
elseif ev.code == KEY_A then
self:addChar("a")
elseif ev.code == KEY_B then
self:addChar("b")
elseif ev.code == KEY_C then
self:addChar("c")
elseif ev.code == KEY_D then
self:addChar("d")
elseif ev.code == KEY_E then
self:addChar("e")
elseif ev.code == KEY_F then
self:addChar("f")
elseif ev.code == KEY_G then
self:addChar("g")
elseif ev.code == KEY_H then
self:addChar("h")
elseif ev.code == KEY_I then
self:addChar("i")
elseif ev.code == KEY_J then
self:addChar("j")
elseif ev.code == KEY_K then
self:addChar("k")
elseif ev.code == KEY_L then
self:addChar("l")
elseif ev.code == KEY_M then
self:addChar("m")
elseif ev.code == KEY_N then
self:addChar("n")
elseif ev.code == KEY_O then
self:addChar("o")
elseif ev.code == KEY_P then
self:addChar("p")
elseif ev.code == KEY_Q then
self:addChar("q")
elseif ev.code == KEY_R then
self:addChar("r")
elseif ev.code == KEY_S then
self:addChar("s")
elseif ev.code == KEY_T then
self:addChar("t")
elseif ev.code == KEY_U then
self:addChar("u")
elseif ev.code == KEY_V then
self:addChar("v")
elseif ev.code == KEY_W then
self:addChar("w")
elseif ev.code == KEY_X then
self:addChar("x")
elseif ev.code == KEY_Y then
self:addChar("y")
elseif ev.code == KEY_Z then
self:addChar("z")
elseif ev.code == KEY_1 then
self:addChar("1")
elseif ev.code == KEY_2 then
self:addChar("2")
elseif ev.code == KEY_3 then
self:addChar("3")
elseif ev.code == KEY_4 then
self:addChar("4")
elseif ev.code == KEY_5 then
self:addChar("5")
elseif ev.code == KEY_6 then
self:addChar("6")
elseif ev.code == KEY_7 then
self:addChar("7")
elseif ev.code == KEY_8 then
self:addChar("8")
elseif ev.code == KEY_9 then
self:addChar("9")
elseif ev.code == KEY_0 then
self:addChar("0")
elseif ev.code == KEY_SPACE then
self:addChar(" ")
elseif ev.code == KEY_PGFWD then
elseif ev.code == KEY_PGBCK then
elseif ev.code == KEY_FW_LEFT then
if (self.cursor.x_pos + 3) > self.input_start_x then
self.cursor:moveHorizontalAndDraw(-self.fwidth)
fb:refresh(1, self.input_start_x-5, ypos,
self.input_slot_w, h)
end
elseif ev.code == KEY_FW_RIGHT then
if (self.cursor.x_pos + 3) < self.input_cur_x then
self.cursor:moveHorizontalAndDraw(self.fwidth)
fb:refresh(1,self.input_start_x-5, ypos,
self.input_slot_w, h)
end
elseif ev.code == KEY_ENTER or ev.code == KEY_FW_PRESS then
if self.input_string == "" then
self.input_string = nil
end
break
elseif ev.code == KEY_DEL then
if Keys.shiftmode then
self:clearText()
else
self:delChar()
end
elseif ev.code == KEY_BACK or ev.code == KEY_HOME then
self.input_string = nil
break
keydef = Keydef:new(ev.code, getKeyModifier())
print("key pressed: "..tostring(keydef))
command = self.commands:getByKeydef(keydef)
if command ~= nil then
print("command to execute: "..tostring(command))
ret_code = command.func(self, keydef)
else
print("command not found: "..tostring(command))
end
--local nsecs, nusecs = util.gettime()
--local dur = (nsecs - secs) * 1000000 + nusecs - usecs
--print("E: T="..ev.type.." V="..ev.value.." C="..ev.code.." DUR="..dur)
if ret_code == "break" then
break
end
end -- if
end -- while
@@ -255,3 +162,82 @@ function InputBox:input(ypos, height, title, d_text)
self.input_string = ""
return return_str
end
function InputBox:addAllCommands()
if self.commands then
-- we only initialize once
return
end
self.commands = Commands:new{}
INPUT_KEYS = {
{KEY_Q, "q"}, {KEY_W, "w"}, {KEY_E, "e"}, {KEY_R, "r"}, {KEY_T, "t"},
{KEY_Y, "y"}, {KEY_U, "u"}, {KEY_I, "i"}, {KEY_O, "o"}, {KEY_P, "p"},
{KEY_A, "a"}, {KEY_S, "s"}, {KEY_D, "d"}, {KEY_F, "f"}, {KEY_G, "g"},
{KEY_H, "h"}, {KEY_J, "j"}, {KEY_K, "k"}, {KEY_L, "l"},
{KEY_Z, "z"}, {KEY_X, "x"}, {KEY_C, "c"}, {KEY_V, "v"}, {KEY_B, "b"},
{KEY_N, "n"}, {KEY_M, "m"},
{KEY_1, "1"}, {KEY_2, "2"}, {KEY_3, "3"}, {KEY_4, "4"}, {KEY_5, "5"},
{KEY_6, "6"}, {KEY_7, "7"}, {KEY_8, "8"}, {KEY_9, "9"}, {KEY_0, "0"},
}
for k,v in ipairs(INPUT_KEYS) do
self.commands:add(v[1], nil, "",
"input "..v[2],
function(self)
self:addChar(v[2])
end
)
end
self.commands:add(KEY_FW_LEFT, nil, "",
"move cursor left",
function(self)
if (self.cursor.x_pos + 3) > self.input_start_x then
self.cursor:moveHorizontalAndDraw(-self.fwidth)
fb:refresh(1, self.input_start_x-5, self.ypos,
self.input_slot_w, self.h)
end
end
)
self.commands:add(KEY_FW_RIGHT, nil, "",
"move cursor right",
function(self)
if (self.cursor.x_pos + 3) < self.input_cur_x then
self.cursor:moveHorizontalAndDraw(self.fwidth)
fb:refresh(1,self.input_start_x-5, self.ypos,
self.input_slot_w, self.h)
end
end
)
self.commands:add({KEY_ENTER, KEY_FW_PRESS}, nil, "",
"submit input content",
function(self)
if self.input_string == "" then
self.input_string = nil
end
return "break"
end
)
self.commands:add(KEY_DEL, nil, "",
"delete one character",
function(self)
self:delChar()
end
)
self.commands:add(KEY_DEL, MOD_SHIFT, "",
"empty inputbox",
function(self)
self:clearText()
end
)
self.commands:add({KEY_BACK, KEY_HOME}, nil, "",
"cancel inputbox",
function(self)
self.input_string = nil
return "break"
end
)
end