forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Sample OSK with netlib support

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development
View previous topic :: View next topic  
Author Message
imhotep



Joined: 13 Dec 2005
Posts: 41

PostPosted: Thu Apr 13, 2006 4:35 pm    Post subject: Sample OSK with netlib support Reply with quote

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

Code:
--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.+ ",
      [3]="yxcvbnml 7890< ",
      [4]="QWERTZUIO!\"§;_",
      [5]="ASDFGHJKP\$%&:* ",
      [6]="YXCVBNML /()=> "      }
      
boxX=charsX-2
boxY=charsY-2
box_width=string.len(chars[1])*20-10
box_height=52

screen:fillRect(boxX+2,boxY+2,box_width+2,box_height+2,grey2)
screen:fillRect(boxX,boxY,box_width,box_height,grey)

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

function build_chars()

for a=startcharset,endcharset do
for b=1,string.len(chars[a]) do
   if startcharset==4 then
      screen:print(charsX+(b-1)*20,charsY+(a-4)*20,string.sub(chars[a],b,b),white)
   else
      screen:print(charsX+(b-1)*20,charsY+(a-1)*20,string.sub(chars[a],b,b),white)
   end

end
end



end

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

function read_osk_controls()

oldpad=pad
pressedbutton=""

pad=Controls.read()

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

if pad~=oldpad then

if pad:r() then activebox=activebox+1 end
if pad:l() then activebox=activebox-1 end
if pad:cross() and typedchars<maxchars then
   pressedbutton=redchar
   typedchars=typedchars+1
end
--space
if pad:circle() then pressedbutton=" " end
if pad:square() then --erase
   erase=1
   typedchars=typedchars-2
end

--SHIFT
if pad:up() then
   startcharset=4
   endcharset=6
end

if pad:down() then
   startcharset=1
   endcharset=3
end

--abort
if pad:left() then endinput=true end

--send the shit
if pad:right() then
   if text~="" then netsend("instantmessenger",username.."%3A "..text.."]","a") end
   refresh()
   endinput=true
end   

--show help
if pad:select() then help=1   end
   
--close help
if pad:triangle() then help=0  end
   
   
--end oldpad
end

if activebox<1 then activebox=1 end
if activebox>maxboxes then activebox=maxboxes end
   
if pad:analogX()>34 then right=1 end
if pad:analogX()<-34 then left=1 end
if pad:analogY()>104 then down=1 end
if pad:analogY()<-104 then up=1 end

end

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

function highlight_activebox()

boxleft=boxX-4+(activebox-1)*60
boxright=boxX-4+(activebox-1)*60+60
boxup=boxY-4
boxdown=boxY+box_height+4
screen:drawLine(boxleft,boxup,boxleft,boxdown,white)        --left
screen:drawLine(boxleft,boxup,boxright,boxup,white)          --top
screen:drawLine(boxright,boxup,boxright,boxdown,white)        --right
screen:drawLine(boxleft,boxdown,boxright,boxdown,white)        --down

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

function show_cursor()

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=(activebox-1)*3+activechar
      setnumber=1
   elseif activechar>3 and activechar<=6 then
      charoffset=(activechar-3)+(activebox-1)*3
      setnumber=2
   elseif activechar>6 then
      charoffset=(activechar-6)+(activebox-1)*3
      setnumber=3
   end
      
   if startcharset==4 then setnumber=setnumber+3 end
   
   redchar=string.sub(chars[setnumber],charoffset,charoffset)

screen:print(cursorX,cursorY,redchar,red)

end

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

function print_text()

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

if erase==1 then
   erase=0
   pressedbutton=""
   text=string.sub(text,1,string.len(text)-1)  --erase one char
end

pressedbutton=oldbutton

--frame for text
screen:fillRect(10,10,string.len(chars[1])*20+10,40,grey)
screen:print(init_textX,init_textY,text,white)

--end function
end


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

function show_pointer()

pointerX=init_textX+8*string.len(text)
pointerY=init_textY+10

screen:drawLine(pointerX,pointerY,pointerX+8,pointerY,darkblue)

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

function show_osk_help()

midX=(480-helpscreen:width() )/2
midY=(272-helpscreen:height() )/2
screen:blit(midX,midY,helpscreen,true)

end

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


function new_message()

build_box()

highlight_activebox()

build_chars()

show_cursor()

print_text()

show_pointer()

if help==0 then
   screen:fillRect(boxX,boxY-20,box_width,15,grey2)
   screen:print(81,181,"select for help...",grey)
   screen:print(80,180,"select for help...",white)
end
if help==1 then show_osk_help()  end

--frame
screen:drawLine(2,2,478,2,grey)     --top
screen:drawLine(2,2,2,270,grey)     --left
screen:drawLine(478,2,478,270,grey)  --right
screen:drawLine(2,270,478,270,grey)     --down

--end function
end

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

function explode(text, needle) --thanks to LuMo.
   local array={}
   _, count = string.gsub(text, needle, needle)
   for index=1,count+1 do
      i, j = string.find(text, needle)
      if i == nil or j==nil then
         i=0
         j=0
      end
     table.insert(array,string.sub(text, 1, i-1))
      text=string.sub(text, j+1 )
  end
  return array
end

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

function refresh()
posts = netget("instantmessenger")
posts = explode(posts,"]")
end

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

function choose_wlan()

Wlan.init()

index=1
maxnet=0
netconfig={}
for i=1,10 do
   if Wlan.getConnectionConfigs()[i]~=nil then
      maxnet=maxnet+1
      netconfig[i]=Wlan.getConnectionConfigs()[i]
   end
end

while true do

screen:print(20,20,"Please choose the appropriate connection:",white)
screen:print(20,30,"-----------------------------------------",white)


for i=1,maxnet do
if index==i then netcol=white else netcol=grey end
   screen:print(20,50+i*12,netconfig[i],netcol)
end

pad=Controls.read()

if pad:up() then index=index-1 end
if index<1 then index=1 end
if pad:down() then index=index+1 end
if index>maxnet then index=maxnet end

if Controls.read():cross() then
   netchoice=index
   break
end

screen.waitVblankStart()
screen:flip()
screen:clear()

end


return netchoice

--end function
end

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

function wlan_init()
--wlan init
username = "User"..tostring(math.random(3000,5000))
dofile("netlib.lua")

screen:clear()
screen:print(0,0,"Connecting to "..Wlan.getConnectionConfigs()[netchoice].."...",white)
screen.flip()

Wlan.useConnectionConfig(netchoice)

screen:clear()
screen:print(0,0,"Connecting to "..Wlan.getConnectionConfigs()[netchoice].."...done",white)
screen.flip()
screen.waitVblankStart()

refresh()

end

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

choose_wlan()

wlan_init()

ref_timer=0

while true do

ref_timer=ref_timer+1

screen:clear()
if table.getn(posts) > 34 then
b = 35
for a=table.getn(posts),table.getn(posts)-34,-1 do
b = b - 1
screen:print(0,b*8,posts[a],white)
end
else
for a=0,table.getn(posts)-1 do
screen:print(0,a*8,posts[a+1],white)
end
end

read_osk_controls()

if endinput==false then
   new_message()
else
   if pad:square() then refresh() end

   if pad:cross() then
      System.sleep(200)
      endinput=false
      text=""
   end
   
   if pad:start() then
      netdelete(username)
      Wlan.term()
      break
   end
end

if ref_timer>4000 then
   refresh()
   ref_timer=0
end

screen.waitVblankStart()
screen.flip()

end



helpme.png....

[img]home.arcor.de/hm82/helpme.png[/img]


netlib.lua...

Code:


--[[
netlib v1.0
Copyright 2006 by youresam
Thanks to Yeldarb for the hosting!
]]--

function replace(thestring,char1,char2)
lastfound = 1
while true do
hasfound = string.find(thestring,char1)
if hasfound == nil then break end
thestring = string.sub(thestring,1,hasfound-1)..char2..string.sub(thestring,hasfound+1)
lastfound = hasfound + 1
end
return thestring
end

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

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


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

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




-=imhotep=-
Back to top
View user's profile Send private message
glynnder



Joined: 04 Sep 2005
Posts: 35

PostPosted: Fri Apr 21, 2006 7:45 pm    Post subject: Reply with quote

cool, can anyone use this in any of their progs
Back to top
View user's profile Send private message
imhotep



Joined: 13 Dec 2005
Posts: 41

PostPosted: Sat Apr 22, 2006 7:01 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group