Sample OSK with netlib support

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
imhotep
Posts: 41
Joined: Tue Dec 13, 2005 9:15 pm

Sample OSK with netlib support

Post by imhotep »

hi guys, i coded this tonight, hope you like it

Code: Select all

--imhotep OSK '06 imhotep
-- with code from lumo and netlib code (ver 1.0)
-- credits see netlib.lua
-- OSK code by me

activebox=1
maxboxes=5

text=""
init_textX=20
init_textY=20

endinput=true

typedchars=0
maxchars=40

erase=0

help=0
helpscreen=Image.load("helpme.png")

startcharset=1
endcharset=3

white=Color.new(255,255,255)
grey=Color.new(100,100,100)
grey2=Color.new(50,50,50)
red=Color.new(230,20,20)
darkblue=Color.new(20,50,150)
black=Color.new(0,0,0)

-----------------------------------------------------------------------------------

function build_box()

charsX=80
charsY=200

--charsets
chars={}
chars={	[1]="qwertzuio123,- ",
		[2]="asdfghjkp456.+ ",
		&#91;3&#93;="yxcvbnml 7890< ",
		&#91;4&#93;="QWERTZUIO!\"§;_",
		&#91;5&#93;="ASDFGHJKP\$%&&#58;* ",
		&#91;6&#93;="YXCVBNML /&#40;&#41;=> "		&#125;
		
boxX=charsX-2
boxY=charsY-2
box_width=string.len&#40;chars&#91;1&#93;&#41;*20-10
box_height=52

screen&#58;fillRect&#40;boxX+2,boxY+2,box_width+2,box_height+2,grey2&#41;
screen&#58;fillRect&#40;boxX,boxY,box_width,box_height,grey&#41;

end
------------------------------------------------------------------------------

function build_chars&#40;&#41;

for a=startcharset,endcharset do
for b=1,string.len&#40;chars&#91;a&#93;&#41; do
	if startcharset==4 then 
		screen&#58;print&#40;charsX+&#40;b-1&#41;*20,charsY+&#40;a-4&#41;*20,string.sub&#40;chars&#91;a&#93;,b,b&#41;,white&#41;
	else
		screen&#58;print&#40;charsX+&#40;b-1&#41;*20,charsY+&#40;a-1&#41;*20,string.sub&#40;chars&#91;a&#93;,b,b&#41;,white&#41;
	end

end
end



end

------------------------------------------------------------------------------

function read_osk_controls&#40;&#41;

oldpad=pad
pressedbutton=""

pad=Controls.read&#40;&#41;

left=0
right=0
up=0
down=0

if pad~=oldpad then

if pad&#58;r&#40;&#41; then activebox=activebox+1 end
if pad&#58;l&#40;&#41; then activebox=activebox-1 end
if pad&#58;cross&#40;&#41; and typedchars<maxchars then 
	pressedbutton=redchar
	typedchars=typedchars+1
end
--space
if pad&#58;circle&#40;&#41; then pressedbutton=" " end
if pad&#58;square&#40;&#41; then --erase
	erase=1
	typedchars=typedchars-2
end

--SHIFT
if pad&#58;up&#40;&#41; then 
	startcharset=4
	endcharset=6
end

if pad&#58;down&#40;&#41; then
	startcharset=1
	endcharset=3
end

--abort
if pad&#58;left&#40;&#41; then endinput=true end

--send the shit
if pad&#58;right&#40;&#41; then 
	if text~="" then netsend&#40;"instantmessenger",username.."%3A "..text.."&#93;","a"&#41; end
	refresh&#40;&#41;
	endinput=true
end	

--show help
if pad&#58;select&#40;&#41; then help=1   end
	
--close help
if pad&#58;triangle&#40;&#41; then help=0  end
	
	
--end oldpad
end

if activebox<1 then activebox=1 end
if activebox>maxboxes then activebox=maxboxes end
	
if pad&#58;analogX&#40;&#41;>34 then right=1 end
if pad&#58;analogX&#40;&#41;<-34 then left=1 end
if pad&#58;analogY&#40;&#41;>104 then down=1 end
if pad&#58;analogY&#40;&#41;<-104 then up=1 end

end

------------------------------------------------------------------------------

function highlight_activebox&#40;&#41;

boxleft=boxX-4+&#40;activebox-1&#41;*60
boxright=boxX-4+&#40;activebox-1&#41;*60+60
boxup=boxY-4
boxdown=boxY+box_height+4
screen&#58;drawLine&#40;boxleft,boxup,boxleft,boxdown,white&#41;	  	--left
screen&#58;drawLine&#40;boxleft,boxup,boxright,boxup,white&#41; 			--top
screen&#58;drawLine&#40;boxright,boxup,boxright,boxdown,white&#41; 	 	--right
screen&#58;drawLine&#40;boxleft,boxdown,boxright,boxdown,white&#41; 	 	--down

end
------------------------------------------------------------------------------

function show_cursor&#40;&#41;

init_cursorX=boxleft+26
init_cursorY=charsY+20

borderleft=init_cursorX-33
borderright=init_cursorX+32
borderup=init_cursorY-20
borderdown=init_cursorY+20

cursorX=init_cursorX
cursorY=init_cursorY

if left==1 then cursorX=cursorX-20 end
if right==1 then cursorX=cursorX+20 end
if up==1 then cursorY=cursorY-20 end
if down==1 then cursorY=cursorY+20 end

if cursorY<borderup then cursorY=borderup end
if cursorY>borderdown then cursorY=borderdown end
if cursorX<borderleft then cursorX=borderleft end
if cursorX>borderright then cursorX=borderright end

--determine active character

activechar=5

if left==1 then
	activechar=4
		if up==1 then activechar=1 end
		if down==1 then activechar=7 end
end

if right==1 then
	activechar=6
		if up==1 then activechar=3 end
		if down==1 then activechar=9 end
end

if left==0 and right==0 then
	activechar=5
	if up==1 then activechar=2 end
	if down==1 then activechar=8 end
end

	if activechar<=3 then
		charoffset=&#40;activebox-1&#41;*3+activechar
		setnumber=1
	elseif activechar>3 and activechar<=6 then
		charoffset=&#40;activechar-3&#41;+&#40;activebox-1&#41;*3
		setnumber=2
	elseif activechar>6 then
		charoffset=&#40;activechar-6&#41;+&#40;activebox-1&#41;*3
		setnumber=3
	end
		
	if startcharset==4 then setnumber=setnumber+3 end
	
	redchar=string.sub&#40;chars&#91;setnumber&#93;,charoffset,charoffset&#41;

screen&#58;print&#40;cursorX,cursorY,redchar,red&#41;

end

------------------------------------------------------------------------------

function print_text&#40;&#41;

if oldbutton~=pressedbutton then text=text..pressedbutton end

if erase==1 then
	erase=0
	pressedbutton=""
	text=string.sub&#40;text,1,string.len&#40;text&#41;-1&#41;  --erase one char
end

pressedbutton=oldbutton

--frame for text
screen&#58;fillRect&#40;10,10,string.len&#40;chars&#91;1&#93;&#41;*20+10,40,grey&#41;
screen&#58;print&#40;init_textX,init_textY,text,white&#41;

--end function
end


-------------------------------------------------------------------------------

function show_pointer&#40;&#41;

pointerX=init_textX+8*string.len&#40;text&#41;
pointerY=init_textY+10

screen&#58;drawLine&#40;pointerX,pointerY,pointerX+8,pointerY,darkblue&#41;

end
-------------------------------------------------------------------------------

function show_osk_help&#40;&#41;

midX=&#40;480-helpscreen&#58;width&#40;&#41; &#41;/2
midY=&#40;272-helpscreen&#58;height&#40;&#41; &#41;/2
screen&#58;blit&#40;midX,midY,helpscreen,true&#41;

end

-------------------------------------------------------------------------------


function new_message&#40;&#41;

build_box&#40;&#41;

highlight_activebox&#40;&#41;

build_chars&#40;&#41;

show_cursor&#40;&#41;

print_text&#40;&#41;

show_pointer&#40;&#41;

if help==0 then 
	screen&#58;fillRect&#40;boxX,boxY-20,box_width,15,grey2&#41;
	screen&#58;print&#40;81,181,"select for help...",grey&#41;
	screen&#58;print&#40;80,180,"select for help...",white&#41;
end
if help==1 then show_osk_help&#40;&#41;  end

--frame
screen&#58;drawLine&#40;2,2,478,2,grey&#41;  	--top
screen&#58;drawLine&#40;2,2,2,270,grey&#41;	  --left
screen&#58;drawLine&#40;478,2,478,270,grey&#41;  --right
screen&#58;drawLine&#40;2,270,478,270,grey&#41;  	--down

--end function
end

---------------------------------------------------------------------------------

function explode&#40;text, needle&#41; --thanks to LuMo.
	local array=&#123;&#125;
	_, count = string.gsub&#40;text, needle, needle&#41;
	for index=1,count+1 do
		i, j = string.find&#40;text, needle&#41;
		if i == nil or j==nil then
			i=0
			j=0
		end
	  table.insert&#40;array,string.sub&#40;text, 1, i-1&#41;&#41;
		text=string.sub&#40;text, j+1 &#41;
  end
  return array
end

---------------------------------------------------------------------------------

function refresh&#40;&#41;
posts = netget&#40;"instantmessenger"&#41;
posts = explode&#40;posts,"&#93;"&#41;
end

--------------------------------------------------------------------------------

function choose_wlan&#40;&#41;

Wlan.init&#40;&#41;

index=1
maxnet=0
netconfig=&#123;&#125;
for i=1,10 do
	if Wlan.getConnectionConfigs&#40;&#41;&#91;i&#93;~=nil then 
		maxnet=maxnet+1
		netconfig&#91;i&#93;=Wlan.getConnectionConfigs&#40;&#41;&#91;i&#93;
	end
end

while true do

screen&#58;print&#40;20,20,"Please choose the appropriate connection&#58;",white&#41;
screen&#58;print&#40;20,30,"-----------------------------------------",white&#41;


for i=1,maxnet do
if index==i then netcol=white else netcol=grey end
	screen&#58;print&#40;20,50+i*12,netconfig&#91;i&#93;,netcol&#41; 
end

pad=Controls.read&#40;&#41;

if pad&#58;up&#40;&#41; then index=index-1 end
if index<1 then index=1 end
if pad&#58;down&#40;&#41; then index=index+1 end
if index>maxnet then index=maxnet end

if Controls.read&#40;&#41;&#58;cross&#40;&#41; then 
	netchoice=index
	break
end

screen.waitVblankStart&#40;&#41;
screen&#58;flip&#40;&#41;
screen&#58;clear&#40;&#41;

end


return netchoice

--end function
end

---------------------------------------------------------------------------------

function wlan_init&#40;&#41;
--wlan init
username = "User"..tostring&#40;math.random&#40;3000,5000&#41;&#41;
dofile&#40;"netlib.lua"&#41;

screen&#58;clear&#40;&#41;
screen&#58;print&#40;0,0,"Connecting to "..Wlan.getConnectionConfigs&#40;&#41;&#91;netchoice&#93;.."...",white&#41;
screen.flip&#40;&#41;

Wlan.useConnectionConfig&#40;netchoice&#41;

screen&#58;clear&#40;&#41;
screen&#58;print&#40;0,0,"Connecting to "..Wlan.getConnectionConfigs&#40;&#41;&#91;netchoice&#93;.."...done",white&#41;
screen.flip&#40;&#41;
screen.waitVblankStart&#40;&#41;

refresh&#40;&#41;

end

-----------------------------------------------------------------------------------

choose_wlan&#40;&#41;

wlan_init&#40;&#41;

ref_timer=0

while true do

ref_timer=ref_timer+1

screen&#58;clear&#40;&#41;
if table.getn&#40;posts&#41; > 34 then
b = 35
for a=table.getn&#40;posts&#41;,table.getn&#40;posts&#41;-34,-1 do
b = b - 1
screen&#58;print&#40;0,b*8,posts&#91;a&#93;,white&#41;
end
else
for a=0,table.getn&#40;posts&#41;-1 do
screen&#58;print&#40;0,a*8,posts&#91;a+1&#93;,white&#41;
end
end

read_osk_controls&#40;&#41;

if endinput==false then
	new_message&#40;&#41; 
else
	if pad&#58;square&#40;&#41; then refresh&#40;&#41; end

	if pad&#58;cross&#40;&#41; then
		System.sleep&#40;200&#41;
		endinput=false
		text=""
	end
	
	if pad&#58;start&#40;&#41; then
		netdelete&#40;username&#41;
		Wlan.term&#40;&#41;
		break
	end
end

if ref_timer>4000 then 
	refresh&#40;&#41;
	ref_timer=0
end

screen.waitVblankStart&#40;&#41;
screen.flip&#40;&#41;

end

helpme.png....

Image


netlib.lua...

Code: Select all


--&#91;&#91;
netlib v1.0
Copyright 2006 by youresam
Thanks to Yeldarb for the hosting!
&#93;&#93;--

function replace&#40;thestring,char1,char2&#41;
lastfound = 1
while true do
hasfound = string.find&#40;thestring,char1&#41;
if hasfound == nil then break end
thestring = string.sub&#40;thestring,1,hasfound-1&#41;..char2..string.sub&#40;thestring,hasfound+1&#41;
lastfound = hasfound + 1
end
return thestring
end

function netsend&#40;id, data, abtribute&#41;
if id == "" then error&#40;"invalid id"&#41; end
if data == "" then return end
if not &#40;abtribute == "a" or abtribute == "w"&#41; then
error&#40;"bad abtribute, must be 'a' or 'w'"&#41;
end
data = replace&#40;data," ","%20"&#41;
data = replace&#40;data,"&#58;","%3A"&#41;
socket = Socket.connect&#40;"www.psp-programming.com", 80&#41;
while not socket&#58;isConnected&#40;&#41; do System.sleep&#40;100&#41; end
bytesSent = socket&#58;send&#40;"GET /youresam/net/write"..abtribute..".php?id="..id.."&data="..data.." HTTP/1.0\r\n"&#41;
bytesSent = socket&#58;send&#40;"host&#58; www.psp-programming.com\r\n\r\n"&#41;
socket&#58;close&#40;&#41;
end

function netget&#40;id&#41;
if id == "" then error&#40;"invalid id"&#41; end
socket = Socket.connect&#40;"www.psp-programming.com", 80&#41;
while not socket&#58;isConnected&#40;&#41; do System.sleep&#40;100&#41; end
bytesSent = socket&#58;send&#40;"GET /youresam/net/id/"..id..".txt HTTP/1.0\r\n"&#41;
bytesSent = socket&#58;send&#40;"host&#58; www.psp-programming.com\r\n\r\n"&#41;
total = ""
request = 0
while true do
	buffer = socket&#58;recv&#40;&#41;
		if string.len&#40;buffer&#41; > 0 then 
			total = total..buffer
			request = 1
		else
			if request == 1 then request = 2 end
		end
	
	if request == 2 then 
		socket&#58;close&#40;&#41;
		break
	end
screen.waitVblankStart&#40;6&#41;
end
--sort out data
start = string.find&#40;total,"text/plain"&#41; or 1
begin = start+14
total = string.sub&#40;total,begin&#41;
return total
end


function netcheck&#40;id&#41;
if id == "" then error&#40;"no id"&#41; end
socket = Socket.connect&#40;"www.psp-programming.com", 80&#41;
while not socket&#58;isConnected&#40;&#41; do System.sleep&#40;100&#41; end
bytesSent = socket&#58;send&#40;"GET /youresam/net/read.php?id="..id.." HTTP/1.0\r\n"&#41;
bytesSent = socket&#58;send&#40;"host&#58; www.psp-programming.com\r\n\r\n"&#41;
total = ""
request = 0
while true do
	buffer = socket&#58;recv&#40;&#41;
		if string.len&#40;buffer&#41; > 0 then 
			total = total..buffer
			request = 1
		else
			if request == 1 then request = 2 end
		end
	
	if request == 2 then 
		socket&#58;close&#40;&#41;
		break
	end
screen.waitVblankStart&#40;6&#41;
end
if string.find&#40;total,"READ ERROR"&#41; == nil then
return true
else
return false
end
end

function netdelete&#40;id&#41;
if id == "" then error&#40;"invalid id"&#41; end
socket = Socket.connect&#40;"www.psp-programming.com", 80&#41;
while not socket&#58;isConnected&#40;&#41; do System.sleep&#40;100&#41; end
bytesSent = socket&#58;send&#40;"GET /youresam/net/delete.php?id="..id.." HTTP/1.0\r\n"&#41;
bytesSent = socket&#58;send&#40;"host&#58; www.psp-programming.com\r\n\r\n"&#41;
socket&#58;close&#40;&#41;
end


-=imhotep=-
glynnder
Posts: 35
Joined: Sun Sep 04, 2005 9:54 pm

Post by glynnder »

cool, can anyone use this in any of their progs
imhotep
Posts: 41
Joined: Tue Dec 13, 2005 9:15 pm

Post by imhotep »

of course, you'd just have to modify your code a bit perhaps, but you can leave it this way and you have a working instant messenger system.
Post Reply