----Zion OSK (On Screen Keyboard)  V1.0 Beta--------To use this in your game/app you MUST give credit in the footer of the osk (on screen keyboard) screen saying : "Powered by : Zion Osk".-------- 

White = Color.new(255,255,255)
Red = Color.new(255,0,0)

---Set directorys.
Image_Dir = "Images/Zion_OSK/"

---DO NOT MODIFY BELOW CHARACTER TABLES  - DOING SO WILL MESS UP THE INPUT---
Characters_1 = {
		"a", "b", "c", "'",
		"d", "e", "f", ".",
		"g", "h", "i", "!",
		"j", "k", "l", "-", 
		"m", " ", "n", --[[Delete--]] 
		"o", "p", "q", "?", 
		"r", "s", "t", "(", 
		"u", "v", "w", ":", 
		"x", "y", "z", ")"
}

Characters_2 = {
		"A", "B", "C", "^", 
		"D", "E", "F", "@",
		"G", "H", "I", "*", 
		"J", "K", "L", "_", 
		"M", " ", "N", --[[Delete--]] 
		"O", "P", "Q", '"', 
		"R", "S", "T", "=",
		"U", "V", "W", ";", 
		"X", "Y", "Z", "/"
}

Characters_3 = {
		".", ".", "1", ".",
		".", ".", "2", ".",
		".", ".", "3", ".",
		".", ".", "4", ".",
		".", " ", "5", --[[Delete--]] 
		".", ".", "6", ".",
		".", ".", "7", ".",
		".", ".", "8", ".",
		".", "0", "9", ".",
}
	
Characters_4 = {
		"(", ".", ")", "'",
		"<", ",", ">", '"',
		"[", "_", "]", "-",
		"{", "?", "}", "!",
		".", --[[Delete--]] " ", ".",
		"\\", "=", "/", "+", 
		"@", ";", "#", ":", 
		"$", "`", "%", "~",
		"^", "|", "&", "*"		
}

---Font 
font = Font.load("font.ttf")
font:setPixelSizes(16, 16) 

---Images
Background  = Image.load(Image_Dir .. "Background.png")
Keys_Lower = Image.load(Image_Dir .. "Keys_Lower.png")
Keys_Lower_Selected = Image.load(Image_Dir .. "Keys_Lower_Selected.png")
Keys_Higher = Image.load(Image_Dir .. "Keys_Higher.png")
Keys_Higher_Selected = Image.load(Image_Dir .. "Keys_Higher_Selected.png")
Number_Keys_Set_1 = Image.load(Image_Dir .. "Number_Keys_Set_1.png")
Number_Keys_Set_1_Selected = Image.load(Image_Dir .. "Number_Keys_Set_1_Selected.png")
Number_Keys_Set_2 = Image.load(Image_Dir .. "Number_Keys_Set_2.png")
Number_Keys_Set_2_Selected = Image.load(Image_Dir .. "Number_Keys_Set_2_Selected.png")

---Sprite Sheet Settings ---- * To modify the keyboards position modify the Osk_Select_Op.x  &  Osk_No_Select_Op.x   &   Char_Set.x    NOTE : (THEY MUST ALL BE THE SAME NUMBER : EG they are al set to 265 now)
Osk_Select_Op = {x = 265, y = 100, Width = 50, Height = 50, ShowX = 0, ShowY = 50, Img = Keys_Lower_Selected} 
Osk_No_Select_Op = {x = 265, y = 100, Width = -50, Height = 50, ShowX = 0, ShowY = 0, Img = Keys_Lower}
Char_Set = {x = 265, y = 100, Img = Keys_Lower}

Name = ""
Selected_Character = 1
Print_Character = 1 ---Do not modify this (Print_Character is used to select with letter is printed, do not modify any instance of it or it will totaly mess up the character printing)
Max_Char = 25 -- Modify this to set the character Limit - (Eg. its now at 40 meaning if you try to enter anymore than 40 characters it stops character input until the number is under 40 again (by deleting))
Char_Remaining = Max_Char 
Option = 2 -- Do not modify this (modifying this wont do anything as the starting block is always the center one)
Opt_Row = 1  -- Do not modify this (modifying this wont do anything as the starting block is always the center one)
Set = 1 ---Selects the Currently selcted character set. (modifying this will change the starting character set only)
Case_Set = Characters_1 -- Do not modify this  (however as a precuation it gets set to 1 below)

function Remove_Char()
	Name = string.sub(Name, 1, string.len(Name) - 1)
	Char_Remaining = Char_Remaining + 1
end

oldpad = Controls.read()

function OSK_Main()

screen:clear()
pad = Controls.read()
anaX = pad:analogX()
anaY = pad:analogY()

---Check if Character Limit is reached
if string.len(Name) >= Max_Char then
	Allow_Print = false
else 
	Allow_Print = true
end 

if string.len(Name) <= 0 then
	Char_Remaining = Max_Char
end

if Option > 9 then
	Option = 9
elseif Option < 1 then
	Option = 1
end

if Set > 4 then
	Set = 1
elseif Set < 1 then
	Set = 4
end

--Change Set
if pad:r() and oldpad:r() ~= pad:r() then
	Set = Set + 1
elseif pad:l() and oldpad:l() ~= pad:l() then
	Set = Set - 1
end

if Set == 1 then
	Case_Set = Characters_1
	Osk_Select_Op.Img = Keys_Lower_Selected
	Osk_No_Select_Op.Img = Keys_Lower
	Char_Set.Img = Keys_Lower
elseif Set == 2 then
	Case_Set = Characters_2
	Osk_Select_Op.Img = Keys_Higher_Selected
	Osk_No_Select_Op.Img = Keys_Higher
	Char_Set.Img = Keys_Higher
elseif Set == 3 then
	Case_Set = Characters_3
	Osk_Select_Op.Img = Number_Keys_Set_1_Selected
	Osk_No_Select_Op.Img = Number_Keys_Set_1
	Char_Set.Img = Number_Keys_Set_1
elseif Set == 4 then
	Case_Set = Characters_4
	Osk_Select_Op.Img = Number_Keys_Set_2_Selected
	Osk_No_Select_Op.Img = Number_Keys_Set_2
	Char_Set.Img = Number_Keys_Set_2
end

--###D-PAD
---Row 1
if pad:right() and Opt_Row == 1 then
	Option = 3
elseif pad:left() and Opt_Row == 1 then
	Option = 1
elseif Opt_Row == 1 and not pad:left() or Opt_Row == 1 and not pad:right() then
	Option = 2
end

--Row 2
if pad:right() and Opt_Row == 2 then
	Option = 6
elseif pad:left() and Opt_Row == 2 then
	Option = 5
elseif Opt_Row == 2 and not pad:left() or Opt_Row == 2 and not pad:right() then
	Option = 4
end

--Row 3
if pad:right() and Opt_Row == 3 then
	Option = 9
elseif pad:left() and Opt_Row == 3 then
	Option = 8
elseif Opt_Row == 3 and not pad:left() or Opt_Row == 3 and not pad:right() then
	Option = 7
end

--###ANALOG
---Row 1
if anaX > 42 and Opt_Row == 1 then
	Option = 3
elseif anaX < -42 and Opt_Row == 1 then
	Option = 1
elseif Opt_Row == 1 and not anaX or Opt_Row == 1 and not anaX then
	Option = 2
end

--Row 2
if anaX > 42 and Opt_Row == 2 then
	Option = 6
elseif anaX < -42 and Opt_Row == 2 then
	Option = 5
elseif Opt_Row == 2 and not anaX or Opt_Row == 2 and not anaX then
	Option = 4
end

--Row 3
if anaX > 42 and Opt_Row == 3 then
	Option = 9
elseif anaX < -42 and Opt_Row == 3 then
	Option = 8
elseif Opt_Row == 3 and not anaX or Opt_Row == 3 and not anaX then
	Option = 7
end

--Control which row were on
if pad:up() then
	Opt_Row = 2
elseif pad:down() then
	Opt_Row = 3
else
	Opt_Row = 1
end

if anaY < -42 then 
	Opt_Row = 2
elseif anaY > 42 then 
	Opt_Row = 3
elseif not anaY then
	Opt_Row = 1
end

--## Cursor Movement & Highlighting ## (Do not modify anything in this section or the cursor hightlighting and movement will be messed up)---
---Opt Set 1
if Option == 1 then	--Collumn 2(middle) - left
	Osk_Select_Op.Width = 50
	Osk_Select_Op.Height = 50
	Osk_Select_Op.y = 150
	Osk_Select_Op.ShowY = 50
	Osk_No_Select_Op.Width = - 0
	Osk_No_Select_Op.Height = 100
end

if Option == 2 then --Collumn 2(middle) - Center
	Osk_Select_Op.Width = 100
	Osk_Select_Op.Height = 50
	Osk_Select_Op.y = 150
	Osk_Select_Op.ShowY = 50
	Osk_No_Select_Op.Width = 50  ---50 less
	Osk_No_Select_Op.Height = 100 ---50 more
end

if Option == 3 then --Collumn 2(middle) - Right
	Osk_Select_Op.Width = 150
	Osk_Select_Op.Height = 50
	Osk_Select_Op.y = 150
	Osk_Select_Op.ShowY = 50
	Osk_No_Select_Op.Width = 100
	Osk_No_Select_Op.Height = 100 
end 

--Opt Set 2
if Option == 4 then	--Collumn 2(Top) - Center
	Osk_Select_Op.Width = 100
	Osk_Select_Op.Height = 50
	Osk_Select_Op.y = 100
	Osk_Select_Op.ShowY = 0
	Osk_No_Select_Op.Width = 50
	Osk_No_Select_Op.Height = 50
end

if Option == 5 then --Collumn 2(Top) - Left
	Osk_Select_Op.Width = 50
	Osk_Select_Op.Height = 50
	Osk_Select_Op.y = 100
	Osk_Select_Op.ShowY = 0
	Osk_No_Select_Op.Width = -0  ---50 less
	Osk_No_Select_Op.Height = 50 ---50 more
end

if Option == 6 then --Collumn 2(Top) - Right
	Osk_Select_Op.Width = 150
	Osk_Select_Op.Height = 50
	Osk_Select_Op.y = 100
	Osk_Select_Op.ShowY = 0
	Osk_No_Select_Op.Width = 100
	Osk_No_Select_Op.Height = 50 
end 

--Opt Set 3
if Option == 7 then	--Collumn 2(Bottom) - Center
	Osk_Select_Op.Width = 100
	Osk_Select_Op.Height = 150
	Osk_Select_Op.y = 200
	Osk_Select_Op.ShowY = 100
	Osk_No_Select_Op.Width = 50
	Osk_No_Select_Op.Height = 150
end

if Option == 8 then --Collumn 2(Bottom) - Left
	Osk_Select_Op.Width = 50
	Osk_Select_Op.Height = 150
	Osk_Select_Op.y = 200
	Osk_Select_Op.ShowY = 100
	Osk_No_Select_Op.Width = -50  ---50 less
	Osk_No_Select_Op.Height = 100 ---50 more
end

if Option == 9 then --Collumn 2(Bottom) - Right
	Osk_Select_Op.Width = 150
	Osk_Select_Op.Height = 150
	Osk_Select_Op.y = 200
	Osk_Select_Op.ShowY = 100
	Osk_No_Select_Op.Width = 100
	Osk_No_Select_Op.Height = 150 
end 
---## End Cursor Movement & Highlighting ##--

--### Character 
--Option 1 (Left Button row 2) 
if Option == 1 and pad:square() and oldpad:square() ~= pad:square() and Allow_Print == true then
	Print_Character = 13
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
elseif Option == 1 and pad:cross() and oldpad:cross() ~= pad:cross() and Allow_Print == true then
	Print_Character = 14
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
elseif Option == 1 and pad:circle() and oldpad:circle() ~= pad:circle() and Allow_Print == true then
	Print_Character = 15
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
elseif Option == 1 and pad:triangle() and oldpad:triangle() ~= pad:triangle() and Allow_Print == true then
	Print_Character = 16
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
end

--Option 2 (Middle Button row 2)
if Option == 2 and pad:square() and oldpad:square() ~= pad:square()and Allow_Print == true then
	Print_Character = 17
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
elseif Option == 2 and pad:cross() and oldpad:cross() ~= pad:cross() and Allow_Print == true then
	Print_Character = 18
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
elseif Option == 2 and pad:circle() and oldpad:circle() ~= pad:circle() and Allow_Print == true then
	Print_Character = 19
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
elseif Option == 2 and pad:triangle() and oldpad:triangle() ~= pad:triangle() then
	Remove_Char()
end

--Option 3 (Right Button row 2)
if Option == 3 and pad:square() and oldpad:square() ~= pad:square() and Allow_Print == true then
	Print_Character = 20
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
elseif Option == 3 and pad:cross() and oldpad:cross() ~= pad:cross() and Allow_Print == true then
	Print_Character = 21
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
elseif Option == 3 and pad:circle() and oldpad:circle() ~= pad:circle() and Allow_Print == true then
	Print_Character = 22
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
elseif Option == 3 and pad:triangle() and oldpad:triangle() ~= pad:triangle() and Allow_Print == true then
	Print_Character = 23
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
end

---## ROW 1 - SET 1---##################
--Option 4 (Middle Button row 1)
if Option == 4 and pad:square() and oldpad:square() ~= pad:square() and Allow_Print == true then
	Print_Character = 5
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
elseif Option == 4 and pad:cross() and oldpad:cross() ~= pad:cross() and Allow_Print == true then
	Print_Character = 6
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
elseif Option == 4 and pad:circle() and oldpad:circle() ~= pad:circle() and Allow_Print == true then
	Print_Character = 7
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
elseif Option == 4 and pad:triangle() and oldpad:triangle() ~= pad:triangle() and Allow_Print == true then
	Print_Character = 8
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
end

--Option 5 (Left Button row 1)
if Option == 5 and pad:square() and oldpad:square() ~= pad:square() and Allow_Print == true then
	Print_Character = 1
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
elseif Option == 5 and pad:cross() and oldpad:cross() ~= pad:cross() and Allow_Print == true then
	Print_Character = 2
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
elseif Option == 5 and pad:circle() and oldpad:circle() ~= pad:circle() and Allow_Print == true then
	Print_Character = 3
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
elseif Option == 5 and pad:triangle() and oldpad:triangle() ~= pad:triangle() and Allow_Print == true then
	Print_Character = 4
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
end

--Option 6 (Right Button row 1)
if Option == 6 and pad:square() and oldpad:square() ~= pad:square() and Allow_Print == true then
	Print_Character = 9
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
elseif Option == 6 and pad:cross() and oldpad:cross() ~= pad:cross() and Allow_Print == true then
	Print_Character = 10
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
elseif Option == 6 and pad:circle() and oldpad:circle() ~= pad:circle() and Allow_Print == true then
	Print_Character = 11
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
elseif Option == 6 and pad:triangle() and oldpad:triangle() ~= pad:triangle() and Allow_Print == true then
	Print_Character = 12
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
end

---## ROW3 - SET 1---##################
--Option 7 (Middle Button row 3)
if Option == 7 and pad:square() and oldpad:square() ~= pad:square() and Allow_Print == true then
	Print_Character = 28
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
elseif Option == 7 and pad:cross() and oldpad:cross() ~= pad:cross() and Allow_Print == true then
	Print_Character = 29
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
elseif Option == 7 and pad:circle() and oldpad:circle() ~= pad:circle() and Allow_Print == true then
	Print_Character = 30
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
elseif Option == 7 and pad:triangle() and oldpad:triangle() ~= pad:triangle() and Allow_Print == true then
	Print_Character = 31
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
end

--Option 8 (Left Button row 3)
if Option == 8 and pad:square() and oldpad:square() ~= pad:square() and Allow_Print == true then
	Print_Character = 24
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
elseif Option == 8 and pad:cross() and oldpad:cross() ~= pad:cross() and Allow_Print == true then
	Print_Character = 25
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
elseif Option == 8 and pad:circle() and oldpad:circle() ~= pad:circle() and Allow_Print == true then
	Print_Character = 26
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
elseif Option == 8 and pad:triangle() and oldpad:triangle() ~= pad:triangle() and Allow_Print == true then
	Print_Character = 27
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
end

--Option 9 (Right Button row 3)
if Option == 9 and pad:square() and oldpad:square() ~= pad:square() and Allow_Print == true then
	Print_Character = 32
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
elseif Option == 9 and pad:cross() and oldpad:cross() ~= pad:cross()  and Allow_Print == true then
	Print_Character = 33
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
elseif Option == 9 and pad:circle() and oldpad:circle() ~= pad:circle()  and Allow_Print == true then
	Print_Character = 34
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
elseif Option == 9 and pad:triangle() and oldpad:triangle() ~= pad:triangle() and Allow_Print == true then
	Print_Character = 35
	Name = Name .. Case_Set[Print_Character]
	Char_Remaining = Char_Remaining - 1
end

if pad:start() then
	 ---do name entry confirmation or whatever here
end

if pad:select() then
	screen:save("OskScrenshot.png")
end

screen:blit(0, 0, Background)
screen:blit(Char_Set.x, Char_Set.y, Char_Set.Img)
screen:blit(Osk_Select_Op.x, Osk_Select_Op.y, Osk_Select_Op.Img, Osk_Select_Op.ShowX, Osk_Select_Op.ShowY, Osk_Select_Op.Width, Osk_Select_Op.Height)
screen:blit(Osk_No_Select_Op.x, Osk_No_Select_Op.y, Osk_No_Select_Op.Img, Osk_No_Select_Op.ShowX, Osk_No_Select_Op.ShowY, Osk_No_Select_Op.Width, Osk_No_Select_Op.Height)
screen:print(25, 15, "Welcome To Zion OSK V1.0 Beta!.", White)
screen:print(48, 30, "Use the on screen keyboard to fill in your name", White)
screen:print(10, 67, "Enter Your Name: ", White)
screen:fontPrint(font, 145, 74, Name .."_", Red)
screen:print(10, 95, "Characters Remaining :", White)
screen:print(190, 95, Char_Remaining, Red)
screen:print(10, 130, "Controls : ", White)
screen:print(10, 134, "__________", White)
screen:print(10, 150, "(R) : Change Case Set", White)
screen:print(10, 160, "(L) : Change Case Set", White)
screen:print(10, 170, "(O) : Enter Left Character", White)
screen:print(10, 180, "(X) : Enter Bottom Character", White)
screen:print(10, 190, "([]) : Enter Right Character", White)
screen:print(10, 200, "(/\\) : Enter Top Character", White)
screen:print(10, 210, "(Start) : Confirm", White)
screen:print(10, 220, "Analog : Navigate OSK.", White)
screen:print(05, 263, "Powered By Zion OSK V1.0 Beta.", White) ---YOU MUST LEAVE THIS IN FOR LEGAL USAGE
screen.waitVblankStart()
screen.flip()
oldpad = pad
end
