From ecd56a3745d6f87e0f06992d42f40ac4b11d71d4 Mon Sep 17 00:00:00 2001 From: Qingping Hou Date: Mon, 9 Apr 2012 15:42:19 +0800 Subject: [PATCH 1/2] mod: prefix global variables width and height with G_ --- crereader.lua | 16 +++++++++------- djvureader.lua | 8 ++++---- filesearcher.lua | 11 ++++------- helppage.lua | 2 +- pdfreader.lua | 2 +- reader.lua | 4 ++-- rendertext_example.lua | 2 +- screen.lua | 4 +++- unireader.lua | 27 +++++++++++++++++---------- 9 files changed, 42 insertions(+), 34 deletions(-) diff --git a/crereader.lua b/crereader.lua index 880f99083..81e55dc73 100644 --- a/crereader.lua +++ b/crereader.lua @@ -25,7 +25,7 @@ function CREReader:open(filename) end local style_sheet = "./data/"..file_type..".css" ok, self.doc = pcall(cre.openDocument, filename, style_sheet, - width, height) + G_width, G_height) if not ok then return false, self.doc -- will contain error message end @@ -81,6 +81,8 @@ end ---------------------------------------------------- function CREReader:goto(pos, pos_type) local prev_xpointer = self.doc:getXPointer() + local width, height = G_width, G_height + if pos_type == "xpointer" then self.doc:gotoXPointer(pos) pos = self.doc:getCurrentPos() @@ -125,11 +127,11 @@ function CREReader:gotoTocEntry(entry) end function CREReader:nextView() - return self.pos + height - self.pan_overlap_vertical + return self.pos + G_height - self.pan_overlap_vertical end function CREReader:prevView() - return self.pos - height + self.pan_overlap_vertical + return self.pos - G_height + self.pan_overlap_vertical end ---------------------------------------------------- @@ -174,10 +176,10 @@ end ---------------------------------------------------- -- used in CREReader:showMenu() function CREReader:_drawReadingInfo() - local ypos = height - 50 + local ypos = G_height - 50 local load_percent = self.percent/100 - fb.bb:paintRect(0, ypos, width, 50, 0) + fb.bb:paintRect(0, ypos, G_width, 50, 0) ypos = ypos + 15 local face, fhash = Font:getFaceAndHash(22) @@ -189,7 +191,7 @@ function CREReader:_drawReadingInfo() "Position: "..load_percent.."%".." "..cur_section, true) ypos = ypos + 15 - blitbuffer.progressBar(fb.bb, 10, ypos, width-20, 15, + blitbuffer.progressBar(fb.bb, 10, ypos, G_width - 20, 15, 5, 4, load_percent/100, 8) end @@ -254,7 +256,7 @@ function CREReader:adjustCreReaderCommands() item_array = face_list, } - local item_no = fonts_menu:choose(0, height) + local item_no = fonts_menu:choose(0, G_height) print(face_list[item_no]) if item_no then cr.doc:setFontFace(face_list[item_no]) diff --git a/djvureader.lua b/djvureader.lua index c9ec0cdf4..460b2167d 100644 --- a/djvureader.lua +++ b/djvureader.lua @@ -66,13 +66,13 @@ function DJVUReader:_isEntireWordInScreenHeightRange(w) (self.cur_full_height - (w.y1 * self.globalzoom) >= -self.offset_y) and (self.cur_full_height - (w.y0 * self.globalzoom) <= - -self.offset_y + height) + -self.offset_y + G_height) end function DJVUReader:_isEntireWordInScreenWidthRange(w) return (w ~= nil) and (w.x0 * self.globalzoom >= -self.offset_x) and - (w.x1 * self.globalzoom <= -self.offset_x + width) + (w.x1 * self.globalzoom <= -self.offset_x + G_width) end -- make sure at least part of the word can be seen in screen @@ -81,9 +81,9 @@ function DJVUReader:_isWordInScreenRange(w) (self.cur_full_height - (w.y0 * self.globalzoom) >= -self.offset_y) and (self.cur_full_height - (w.y1 * self.globalzoom) <= - -self.offset_y + height) and + -self.offset_y + G_height) and (w.x1 * self.globalzoom >= -self.offset_x) and - (w.x0 * self.globalzoom <= -self.offset_x + width) + (w.x0 * self.globalzoom <= -self.offset_x + G_width) end function DJVUReader:toggleTextHighLight(word_list) diff --git a/filesearcher.lua b/filesearcher.lua index da013f386..415dac45f 100644 --- a/filesearcher.lua +++ b/filesearcher.lua @@ -164,7 +164,7 @@ function FileSearcher:addAllCommands() "invoke search inputbox", function(self) old_keywords = self.keywords - self.keywords = InputBox:input(height-100, 100, + self.keywords = InputBox:input(G_height - 100, 100, "Search:", old_keywords) if self.keywords then self:setSearchResult(self.keywords) @@ -181,7 +181,7 @@ function FileSearcher:addAllCommands() menu_title = "Fonts Menu", item_array = Font.fonts, } - local re = fonts_menu:choose(0, height) + local re = fonts_menu:choose(0, G_height) if re then Font.cfont = Font.fonts[re] Font:update() @@ -195,14 +195,10 @@ function FileSearcher:addAllCommands() file_entry = self.result[self.perpage*(self.page-1)+self.current] file_full_path = file_entry.dir .. "/" .. file_entry.name - -- rotation mode might be changed while reading, so - -- record height_percent here - local height_percent = height/fb.bb:getHeight() openFile(file_full_path) - --reset height and item index if screen has been rotated local item_no = self.perpage * (self.page - 1) + self.current - self.perpage = math.floor(height / self.spacing) - 2 + self.perpage = math.floor(G_height / self.spacing) - 2 self.current = item_no % self.perpage self.page = math.floor(item_no / self.perpage) + 1 @@ -218,6 +214,7 @@ function FileSearcher:addAllCommands() end function FileSearcher:choose(keywords) + local width, height = G_width, G_height self.perpage = math.floor(height / self.spacing) - 2 self.pagedirty = true self.markerdirty = false diff --git a/helppage.lua b/helppage.lua index 33ba5e9a9..cedb17906 100644 --- a/helppage.lua +++ b/helppage.lua @@ -30,7 +30,7 @@ HelpPage.hface, HelpPage.hfhash = Font:getFaceAndHash(HelpPage.hfsize, "sans") HelpPage.ffsize = 15 HelpPage.fface, HelpPage.ffhash = Font:getFaceAndHash(HelpPage.ffsize, "sans") -function HelpPage:show(ypos, height,commands) +function HelpPage:show(ypos, height, commands) self.commands = {} self.items = 0 local keys = {} diff --git a/pdfreader.lua b/pdfreader.lua index 9413e0127..334a69d85 100644 --- a/pdfreader.lua +++ b/pdfreader.lua @@ -13,7 +13,7 @@ function PDFReader:open(filename) return false, self.doc -- will contain error message end if self.doc:needsPassword() then - local password = InputBox:input(height-100, 100, "Pass:") + local password = InputBox:input(G_height-100, 100, "Pass:") if not password or not self.doc:authenticatePassword(password) then self.doc:close() self.doc = nil diff --git a/reader.lua b/reader.lua index 193eb768d..091f6f37b 100755 --- a/reader.lua +++ b/reader.lua @@ -121,7 +121,7 @@ if optarg["G"] ~= nil then end fb = einkfb.open("/dev/fb0") -width, height = fb:getSize() +G_width, G_height = fb:getSize() -- read current rotation mode Screen:updateRotationMode() Screen.native_rotation_mode = Screen.cur_rotation_mode @@ -146,7 +146,7 @@ if ARGV[optind] and lfs.attributes(ARGV[optind], "mode") == "directory" then local running = true FileChooser:setPath(ARGV[optind]) while running do - local file, callback = FileChooser:choose(0,height) + local file, callback = FileChooser:choose(0, G_height) if callback then callback() else diff --git a/rendertext_example.lua b/rendertext_example.lua index 12dbf524d..3c208b8f5 100755 --- a/rendertext_example.lua +++ b/rendertext_example.lua @@ -3,7 +3,7 @@ require "rendertext" require "graphics" fb = einkfb.open("/dev/fb0") -width, height = fb:getSize() +G_width, G_height = fb:getSize() print("open") diff --git a/screen.lua b/screen.lua index 0ac639044..25edb1096 100644 --- a/screen.lua +++ b/screen.lua @@ -77,6 +77,8 @@ function Screen:updateRotationMode() end function Screen:saveCurrentBB() + local width, height = G_width, G_height + if not self.saved_bb then self.saved_bb = Blitbuffer.new(width, height) end @@ -92,7 +94,7 @@ function Screen:resotreFromSavedBB() end function Screen:getCurrentScreenBB() - local bb = Blitbuffer.new(width, height) + local bb = Blitbuffer.new(G_width, G_height) bb:blitFullFrom(fb.bb) return bb end diff --git a/unireader.lua b/unireader.lua index bc5294a8e..ad14cd5d2 100644 --- a/unireader.lua +++ b/unireader.lua @@ -250,6 +250,7 @@ function UniReader:drawOrCache(no, preCache) -- ideally, this should be factored out and only be called when needed (TODO) local ok, page = pcall(self.doc.openPage, self.doc, no) + local width, height = G_width, G_height if not ok then -- TODO: error handling return nil @@ -362,6 +363,7 @@ end function UniReader:setzoom(page, preCache) local dc = DrawContext.new() local pwidth, pheight = page:getSize(self.nulldc) + local width, height = G_width, G_height print("# page::getSize "..pwidth.."*"..pheight); local x0, y0, x1, y1 = page:getUsedBBox() if x0 == 0.01 and y0 == 0.01 and x1 == -0.01 and y1 == -0.01 then @@ -517,6 +519,8 @@ end -- render and blit a page function UniReader:show(no) local pagehash, offset_x, offset_y = self:drawOrCache(no) + local width, height = G_width, G_height + if not pagehash then return end @@ -662,7 +666,8 @@ function UniReader:nextView() pageno = pageno + 1 else -- goto next view of current page - self.offset_y = self.offset_y - height + self.pan_overlap_vertical + self.offset_y = self.offset_y - G_height + + self.pan_overlap_vertical end else -- not in fit to content width pan mode, just do a page turn @@ -688,7 +693,8 @@ function UniReader:prevView() pageno = pageno - 1 else -- goto previous view of current page - self.offset_y = self.offset_y + height - self.pan_overlap_vertical + self.offset_y = self.offset_y + G_height + - self.pan_overlap_vertical end else -- not in fit to content width pan mode, just do a page turn @@ -735,7 +741,8 @@ end -- @ orien: 1 for clockwise rotate, -1 for anti-clockwise function UniReader:screenRotate(orien) Screen:screenRotate(orien) - width, height = fb:getSize() + -- update global width and height variable + G_width, G_height = fb:getSize() self:clearCache() self:redrawCurrentPage() end @@ -849,7 +856,7 @@ end -- used in UniReader:showMenu() function UniReader:_drawReadingInfo() - local ypos = height - 50 + local width, height = G_width, G_height local load_percent = (self.pageno / self.doc:getPages()) local face, fhash = Font:getFaceAndHash(22) @@ -1060,7 +1067,7 @@ function UniReader:addAllCommands() self.commands:add(KEY_G,nil,"G", "goto page", function(unireader) - local page = InputBox:input(height-100, 100, "Page:") + local page = InputBox:input(G_height-100, 100, "Page:") -- convert string to number if not pcall(function () page = page + 0 end) then page = unireader.pageno @@ -1074,7 +1081,7 @@ function UniReader:addAllCommands() self.commands:add(KEY_H,nil,"H", "show help page", function(unireader) - HelpPage:show(0,height,unireader.commands) + HelpPage:show(0, G_height, unireader.commands) unireader:redrawCurrentPage() end) self.commands:add(KEY_T,nil,"T", @@ -1130,8 +1137,8 @@ function UniReader:addAllCommands() local bbox = {} bbox["x0"] = - unireader.offset_x / unireader.globalzoom bbox["y0"] = - unireader.offset_y / unireader.globalzoom - bbox["x1"] = bbox["x0"] + width / unireader.globalzoom - bbox["y1"] = bbox["y0"] + height / unireader.globalzoom + bbox["x1"] = bbox["x0"] + G_width / unireader.globalzoom + bbox["y1"] = bbox["y0"] + G_height / unireader.globalzoom bbox.pan_x = unireader.pan_x bbox.pan_y = unireader.pan_y unireader.bbox[unireader.pageno] = bbox @@ -1176,8 +1183,8 @@ function UniReader:addAllCommands() x = unireader.shift_x / 5 y = unireader.shift_y / 5 elseif unireader.pan_by_page then - x = width; - y = height - unireader.pan_overlap_vertical; -- overlap for lines which didn't fit + x = G_width; + y = G_height - unireader.pan_overlap_vertical; -- overlap for lines which didn't fit else x = unireader.shift_x y = unireader.shift_y From ff60a41604cbcac27940378efa4a0d0245728424 Mon Sep 17 00:00:00 2001 From: Qingping Hou Date: Mon, 9 Apr 2012 17:17:23 +0800 Subject: [PATCH 2/2] mod: rewrite inputbox with commands --- inputbox.lua | 202 ++++++++++++++++++++++++--------------------------- 1 file changed, 94 insertions(+), 108 deletions(-) diff --git a/inputbox.lua b/inputbox.lua index abbaeb687..fb942f1bf 100644 --- a/inputbox.lua +++ b/inputbox.lua @@ -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