From 95998a7397767c53cdbf3924ebb1d587952df37b Mon Sep 17 00:00:00 2001 From: Qingping Hou Date: Tue, 10 Apr 2012 15:52:51 +0800 Subject: [PATCH] add: NumInputBox class for goto page feature --- inputbox.lua | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++ unireader.lua | 2 +- 2 files changed, 107 insertions(+), 1 deletion(-) diff --git a/inputbox.lua b/inputbox.lua index afa9603b3..a38950022 100644 --- a/inputbox.lua +++ b/inputbox.lua @@ -3,6 +3,11 @@ require "rendertext" require "keys" require "graphics" + +---------------------------------------------------- +-- General inputbox +---------------------------------------------------- + InputBox = { -- Class vars: h = 100, @@ -28,6 +33,13 @@ InputBox = { commands = nil, } +function InputBox:new(o) + o = o or {} + setmetatable(o, self) + self.__index = self + return o +end + function InputBox:refreshText() -- clear previous painted text fb.bb:paintRect(140, self.input_start_y-19, @@ -91,6 +103,10 @@ function InputBox:clearText() self.input_slot_w, self.h-25) end +function InputBox:drawHelpMsg(ypos, w, h) + return +end + function InputBox:drawBox(ypos, w, h, title) -- draw input border fb.bb:paintRect(20, ypos, w, h, 5) @@ -127,6 +143,7 @@ function InputBox:input(ypos, height, title, d_text) -- draw box and content w = fb.bb:getWidth() - 40 h = height - 45 + self:drawHelpMsg(ypos, w, h) self:drawBox(ypos, w, h, title) if d_text then self.input_string = d_text @@ -241,3 +258,92 @@ function InputBox:addAllCommands() end ) end + + +---------------------------------------------------- +-- Inputbox for numbers only +-- Designed by eLiNK +---------------------------------------------------- + +NumInputBox = InputBox:new{} + +function NumInputBox:addAllCommands() + if self.commands then + -- we only initialize once + return + end + self.commands = Commands:new{} + + INPUT_NUM_KEYS = { + {KEY_Q, "1"}, {KEY_W, "2"}, {KEY_E, "3"}, {KEY_R, "4"}, {KEY_T, "5"}, + {KEY_Y, "6"}, {KEY_U, "7"}, {KEY_I, "8"}, {KEY_O, "9"}, {KEY_P, "0"}, + + {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_NUM_KEYS) do + self.commands:add(v[1], nil, "", + "input "..v[2], + function(self) + self:addChar(v[2]) + end + ) + end -- for + + 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 + +function InputBox:drawHelpMsg(ypos, w, h) + local w = 415 + local y = ypos - 60 + local x = (G_width - w) / 2 + local h = 50 + local bw = 2 + local face = Font:getFace("scfont", 22) + + fb.bb:paintRect(x, y, w, h, 15) + fb.bb:paintRect(x+bw, y+bw, w-2*bw, h-2*bw, 0) + + local font_y = y + 22 + local font_x = x + 22 + INPUT_NUM_KEYS = { + {"Q", "1"}, {"W", "2"}, {"E", "3"}, {"R", "4"}, {"T", "5"}, + {"Y", "6"}, {"U", "7"}, {"I", "8"}, {"O", "9"}, {"P", "0"}, + } + for k,v in ipairs(INPUT_NUM_KEYS) do + renderUtf8Text(fb.bb, font_x, font_y, face, + v[1], true) + renderUtf8Text(fb.bb, font_x, font_y + 22, face, + v[2], true) + font_x = font_x + 40 + end + + fb:refresh(1, x, y, w, h) +end diff --git a/unireader.lua b/unireader.lua index b856f606d..d84d697e1 100644 --- a/unireader.lua +++ b/unireader.lua @@ -1081,7 +1081,7 @@ function UniReader:addAllCommands() self.commands:add(KEY_G,nil,"G", "goto page", function(unireader) - local page = InputBox:input(G_height-100, 100, "Page:") + local page = NumInputBox:input(G_height-100, 100, "Page:") -- convert string to number if not pcall(function () page = page + 0 end) then page = unireader.pageno