Community Curator/Source/TSKeyboard

-- Keyboard for TagShell. Key = {} local FillRect, DStr, alert = draw.fillrect, draw.string, sys.alert

SX, SY = draw.getport

Key.History = {}

function TRec(X1,Y1,X2,Y2,TX,TY) if TX>=X1 and TX<=X2 then if TY>=Y1 and TY<=Y2 then return true end end return false end

require "TSKeyboard1"

function Key.DrawKey(Shift) draw.setfont("Arial", 16) local FillRect=FillRect local L1,L2,L3,L4=SY-128, SY-96, SY-64, SY-32 local X1, X2, X3, X4=L2-2, L3-2, L4-2, SY-2 local TA=-5 -- the height of the text local T1, T2, T3, T4 = L1-TA, L2-TA, L3-TA, L4-TA local TxtC = draw.black local LS = 4 FillRect(0, L1-2, SX, SY, draw.lightGray) FillRect(2, L1, 40-LS, X1, draw.white) DStr("Q", 10-2, T1, TxtC) FillRect(42-LS, L1, 80-(LS*2), X1, draw.white) DStr("W", 50-4, T1, TxtC) FillRect(82-(LS*2), L1, 120-(LS*3), X1, draw.white) DStr("E", 88-6, T1, TxtC) FillRect(122-(LS*3), L1, 160-(LS*4), X1, draw.white) DStr("R", 126-8, T1, TxtC) FillRect(162-(LS*4), L1, 200-(LS*5), X1, draw.white) DStr("T", 164-10, T1, TxtC) FillRect(202-(LS*5), L1, 240-(LS*6), X1, draw.white) DStr("Y", 202-12, T1, TxtC) FillRect(242-(LS*6), L1, 280-(LS*7), X1, draw.white) DStr("U", 240-14, T1, TxtC) FillRect(282-(LS*7), L1, 320-(LS*8), X1, draw.white) DStr("I", 278-16, T1, TxtC) FillRect(322-(LS*8), L1, 360-(LS*9), X1, draw.white) DStr("O", 316-18, T1, TxtC) FillRect(362-(LS*9), L1, 400-(LS*10), X1, draw.white) DStr("P", 344-16, T1, TxtC)

FillRect(2, L2, 40, X2, draw.white) DStr("A", 14, T2, TxtC) FillRect(42, L2, 80, X2, draw.white) DStr("S", 54, T2, TxtC) FillRect(82, L2, 120, X2, draw.white) DStr("D", 94, T2, TxtC) FillRect(122, L2, 160, X2, draw.white) DStr("F", 134, T2, TxtC) FillRect(162, L2, 200, X2, draw.white) DStr("G", 174, T2, TxtC) FillRect(202, L2, 240, X2, draw.white) DStr("H", 214, T2, TxtC) FillRect(242, L2, 280, X2, draw.white) DStr("J", 254, T2, TxtC) FillRect(282, L2, 320, X2, draw.white) DStr("K", 294, T2, TxtC) FillRect(322, L2, 360, X2, draw.white) DStr("L", 334, T2, TxtC)

if Shift then FillRect(2, L3, 40, X3, draw.red) else FillRect(2, L3, 40, X3, draw.cyan) end DStr("Shift", 4, T3, TxtC) FillRect(42, L3, 80, X3, draw.white) DStr("Z", 54, T3, TxtC) FillRect(82, L3, 120, X3, draw.white) DStr("X", 94, T3, TxtC) FillRect(122, L3, 160, X3, draw.white) DStr("C", 134, T3, TxtC) FillRect(162, L3, 200, X3, draw.white) DStr("V", 174, T3, TxtC) FillRect(202, L3, 240, X3, draw.white) DStr("B", 214, T3, TxtC) FillRect(242, L3, 280, X3, draw.white) DStr("N", 254, T3, TxtC) FillRect(282, L3, 320, X3, draw.white) DStr("M", 294, T3, TxtC) FillRect(322, L3, 360, X3, draw.white) DStr("Back", 324, T3, TxtC)

FillRect(2, L4, 200, X4, draw.white) DStr("Space", 4, T4, TxtC) -- Scroll Bar FillRect(202, L4, 232, X4, draw.white) DStr("<", 204, T4, TxtC) FillRect(292, L4, 320, X4, draw.white) DStr(">", 294, T4, TxtC) FillRect(322, L4, 360, X4, draw.white) DStr("Enter", 323, T4, TxtC)

--Numberpad FillRect(362, L1, 400, X1, draw.white) DStr("9", 384, T1, TxtC) FillRect(402, L1, 440, X1, draw.white) DStr("8", 414, T1, TxtC) FillRect(442, L1, 480, X1, draw.white) DStr("7", 454, T1, TxtC)

FillRect(362, L2, 400, X2, draw.white) DStr("6", 374, T2, TxtC) FillRect(402, L2, 440, X2, draw.white) DStr("5", 414, T2, TxtC) FillRect(442, L2, 480, X2, draw.white) DStr("4", 454, T2, TxtC)

FillRect(362, L3, 400, X3, draw.white) DStr("3", 374, T3, TxtC) FillRect(402, L3, 440, X3, draw.white) DStr("2", 414, T3, TxtC) FillRect(442, L3, 480, X3, draw.white) DStr("1", 454, T3, TxtC)

FillRect(362, L4, 400, X4, draw.white) DStr("0", 374, T4, TxtC) FillRect(402, L4, 440, X4, draw.white) DStr("Cancel", 402, T4, TxtC) FillRect(442, L4, 480, X4, draw.white) DStr("/", 454, T4, TxtC) end

function Key.HozScBar(DSTtr, S1) local S2 = math.max(draw.stringsize(DSTtr)-SX, 0) local X1, X2 = 200, 320 Y1, Y2 = X1, X2  --local BY1 = (((X2-64)-(X1+32))*((ScrollX/S2)*(1))) + (X1+32)   local BY2 = BY1+(((X2-32)-(X1+32))/S2)

local BY1 = ((((Y2-64)-(Y1+32))*((S1/S2)*(1)))*1) + (Y1+34) local BY2 = BY1+32

FillRect(BY1, SY-32, BY2, SY, draw.cyan) end

function Key.DrawHistory for k, v in ipairs(Key.History) do     draw.roundedrect(2, 64+((k-1)*20), (6+draw.stringsize(v)), 64+(k*20), 2, draw.black) draw.string(v,3,64+((k-1)*20),draw.black) end end

function Key.TappedHistory(TX,TY) for k, v in ipairs(Key.History) do     local Tapped = TRec(2, 64+((k-1)*20), (6+draw.stringsize(v)), 64+(k*20), TX, TY) if Tapped then return k     end end end

function Key.Keyboard(TitleStr, StartStr) local KeyRun = true local Str = StartStr or "" local Shift = false local ScrollX = 0 while KeyRun do     draw.beginframe draw.clear draw.string(TitleStr or "Keyboard", 2, 2, draw.black) if TitleStr~="Password" then DSTtr = Str draw.string(Str, 2 - ScrollX, 32, draw.black) else DSTtr = string.rep("*", #Str) draw.string(DSTtr, 2 - ScrollX, 32, draw.black) end Key.DrawHistory Key.DrawKey(Shift) Key.HozScBar(DSTtr, ScrollX) draw.endframe --Touch TX, TY = draw.waittouch local Answer = Key.Tap(TX,TY) if Answer~="" then alert("tock") end if Answer=="Back" then Str = Str:sub(1,-2) elseif Answer=="Enter" then KeyRun=false Key.History[#Key.History+1] = Str elseif Answer=="Shift" then Shift = not Shift elseif Answer=="Space" then Str = Str.." " elseif Answer=="Ca" then return false elseif Answer=="SL" then ScrollX = math.max(ScrollX - 16, 0) elseif Answer=="SR" then ScrollX = math.min(ScrollX + 16, math.max(draw.stringsize(DSTtr)-SizeX, 0)) elseif Answer~="History" then if Shift then Str = Str..tostring(Answer) or Str else Str = Str..string.lower(tostring(Answer)) end else local Hst = Key.TappedHistory(TX,TY) if Hst ~= nil then KeyRun=false return Key.History[Hst] end end end return Str end

print("Keyboard loaded") return Key