open window 640,512
U=16:D=64:L=128:R=32:X=16384:SQ=32768:TRI=4096:CIR=8192
R1=2048:R2=512:ST=8:L2=256:L1=1024:SEL=1
 
'#################################################
'# 2D EDITOR by tappi
'#
'# HELP:
'# If you have created an object you will get the
'# co-ordinates printed as text in the text and 
'# graphic window. The coords are sorts as follows:
'# TRIANGLE 1: X,Y,X,Y,X,Y TRIANGLE 2....
'# Here's a short example how you could use the created coordinates:
'# 
'# open window 640,512
'#
'# triangles=2
'# coords=triangles*3
'# dim dx(coords),dy(coords)
'#
'# for c=1 to coords
'#  read dx(c),dy(c)
'# next
'#
'# REM   X,  Y,  X,  Y,  X,  Y
'# data 10, 10,-10, 10,-10,-10
'# data 10, 10, 10,-10,-10,-10
'#
'# x=320 : y=256
'#
'# for c=1 to coords step 3
'#  setrgb 1,100,100,100
'#  fill triangle x+dx(c),y+dy(c) to x+dx(c+1),y+dy(c+1) to x+dx(c+2),y+dy(c+2)
'# next
'#
'# inkey$():exit
'#################################################
 gosub init

 repeat
  gosub new_screen
  setrgb 1,200,200,200
  text 320,350, c$,"cc"
 until(peek("port1")=X)
 repeat:until(peek("port1")<>X)

label main
 gosub new_screen
 gosub pad
 gosub draw_field
 gosub draw_tris
 gosub draw_cursor
 gosub help
goto main
'======================================
label help
 if HELP>0 return
 ht=ht+.07 : ht=mod(ht,hlp)

 setrgb 1,250,250,250
 text 640-ht*20,505, hlp$
return
'======================================
label draw_cursor
 CB=CB+.2:CC=150+100*sin(CB)
 setrgb 1,CC,CC,CC
 fill rect x(PX)-3,y(PY)-3,x(PX)+3,y(PY)+3

REM ###-PREVIEW-###
 if mod(T,3)<>0 then
  if mod(T,3)=1 setrgb 1,  0,250, 50
  if mod(T,3)=2 setrgb 1,  0,250,250
  triangle px(1),py(1) to px(2),py(2) to x(PX),y(PY)
 endif
return
'======================================
label draw_tris
 if TR<1 return
 c=1:repeat

  on view+1 goto 1,2,3

1 REM JUST A UNFILLED TRIANGLE
  setrgb 1,230,230,230
  triangle nx(c),ny(c) to nx(c+1),ny(c+1) to nx(c+2),ny(c+2)
goto 0

2 REM FILLED TRI
  setrgb 1,0,0,0
  fill triangle nx(c),ny(c) to nx(c+1),ny(c+1) to nx(c+2),ny(c+2)
goto 0

3 REM BOTH
  setrgb 1,0,0,0
  fill triangle nx(c),ny(c) to nx(c+1),ny(c+1) to nx(c+2),ny(c+2)
  setrgb 1,230,230,230
  triangle nx(c),ny(c) to nx(c+1),ny(c+1) to nx(c+2),ny(c+2)
0
 c=c+3
 until(c>=TR*3)
return
'======================================
label draw_field
 for x=220 to 420 step 20
 for y=156 to 356 step 20
  CZ=250-abs(320-x)-abs(256-y)
  setrgb 1,CZ,0,CZ
  line x,156,x,356 : line 220,y,420,y
 next:next

 setrgb 1,200,200,200

 text  10,400, "Triangles  : " + str$(TR)
 text  10,420, "Coordinates: " + str$(T)
 text 300,400, "Cursor X   : " + str$(x(PX)-320)
 text 300,420, "Cursor Y   : " + str$(y(PY)-256)
return
'======================================
label pad
 k=peek("port1")

 if and(k,or(L,or(R,or(U,or(D,0)))))>0 then
  r=r+1
 else
  r=0
 endif
'---MOVE CURSOR RIGHT---
 if and(k,R)>0 then
  if r<2 PX=PX+1
  if mod(r,2)=0 and r>10 PX=PX+1
 endif
'---MOVE CURSOR LEFT---
 if and(k,L)>0 then
  if r<2 PX=PX-1
  if mod(r,2)=0 and r>10 PX=PX-1
 endif
'---MOVE CURSOR UP---
 if and(k,U)>0 then
  if r<2 PY=PY-1
  if mod(r,2)=0 and r>10 PY=PY-1
 endif
'---MOVE CURSOR DOWN---
 if and(k,D)>0 then
  if r<2 PY=PY+1
  if mod(r,2)=0 and r>10 PY=PY+1
 endif

 PX=max(1,min(MA-1,PX))
 PY=max(1,min(MA-1,PY))

'---ADD COORDINATE---
 if and(k,X)>0 and P=0 then
  gosub create_coord : P=1
 endif
'---REMOVE TRIANGLE---
 if and(k,SQ)>0 and P=0 then
  gosub erase_tri : P=1
 endif
'---SWITCH THRU DRAW MODES---
 if and(k,CIR)>0 and P=0 then
  view=view+1 : view=mod(view,3) : P=1
 endif
'---HELP TEXT ON/OFF---
 if and(k,TRI)>0 and P=0 then
  HELP=HELP+1 : HELP=mod(HELP,2) : P=1
 endif

 if k=ST goto show
 if k=0 P=0
return
'======================================
label new_screen
 setdrawbuf buf:buf=1-buf:setdispbuf buf
 setrgb 1,0,0,0:setrgb 2,10,60,80:setrgb 3,0,0,0
 gtriangle 0,0 to 0,512 to 640,0
 setrgb 1,10,60,80
 gtriangle 640,512 to 0,512 to 640,0
return
'======================================
label create_coord
 T=T+1
 dim nx(T),ny(T)
 nx(T)=x(PX) : ny(T)=y(PY)

 if mod(T,3)=0 TR=TR+1

REM --JUST PREVIEW--
 if mod(T,3)=1 then
  px(1)=x(PX) : py(1)=y(PY)
  px(2)=x(PX) : py(2)=y(PY)
 endif

 if mod(T,3)=2 then
  px(2)=x(PX) : py(2)=y(PY)
 endif
return
'======================================
label erase_tri
 if mod(T,3)=0 then
  TR=TR-1  :  TR=max(0,TR)  :  T=TR*3
 else
  T=TR*3:T=max(0,T)
 endif
return
'======================================
label show
 for ST=1 to TR
  a$=a$+" #"+str$(ST,"##")+" "
 for SC=1 to 3
  AC=AC+1 : x=nx(AC)-320 : y=ny(AC)-256
  a$=a$+str$(x,"###")+","+str$(y,"###")+","
 next:if mod(ST,2)=0 a$=a$+"\n":next

 print " #--  X   Y   X   Y   X   Y   #--  X   Y   X   Y   X   Y "
 print a$

 repeat
  gosub new_screen
  yt=50 : xt=70 : AC=0

 for ST=1 to TR
  yt=yt+26

                 setrgb 1,30,30,130
  if mod(ST,2)=0 setrgb 1,120,30,30
  fill rect 100,yt-scr-15,510,yt-scr+10

  setrgb 1,220,220,220
  text 100,yt-scr, str$(ST)
  line 100,yt-scr+10,510,yt-scr+10

 for SC=1 to 3
  xt=xt+120

  setrgb 1,220,220,220
  line xt-40,0,xt-40,512
  setrgb 1,120,120,120
  line xt+20,0,xt+20,512

  AC=AC+1
  setrgb 1,220,220,220
  text xt   ,2+yt-scr, str$(nx(AC)-320),"rc"
  text xt+60,2+yt-scr, str$(ny(AC)-256),"rc"
 next:xt=70:next

 k=peek("port1")
 scr=scr+and(k,U)/(U/4)-and(k,D)/(D/4)
 until(k=X)
exit
'####-INITIALISATION-####
label init
 T=0:TR=0
 MA=40

 dim x(MA),y(MA),px(2),py(2)

 PX=MA/2 : PY=MA/2 :REM SETS CURSOR TO MIDDLE

 for a=1 to MA
  x(a)=a*5+220
  y(a)=a*5+156
 next

 c$=   "2D EDITOR\ncoded by tappi\n\n\n"
 c$=c$+"for information or help follow the links on\n"
 c$=c$+"www.ps2-yabasic.co.uk\n\n\n"
 c$=c$+"press X to start"
 hlp$=     "X - ADD COORDINATE | CIRCLE - CHANGE VIEW | SQUARE - "
 hlp$=hlp$+"ERASE TRI | TRIANGLE - HELP TEXT ON/OFF | "
 hlp$=hlp$+"START-SHOW COORDS,CLOSE APP"
 hlp=len(hlp$)
return

  
