  #compact
 '%filename "t.exe"
 'uses RTL64
  uses DrawGui

  indexbase 1
  sys       texn[16]
  sys       GdiplusToken
  float     ang1
  sys       cube,pent



  BeginScript

  procedure main() 'triangle-data example
  ================

  =======
  'LAYERS
  =======
  'background    'z=-inf
  'grid          'z=-2.002
  'object        'z=-2
  'info panel    'z=-1.01
  'tool panels   'z=-1 +.001 each layer
  'tool controls '+ .0001 each layer
  'nodes         'z=0.0

  ===========
  'IDENTITIES
  ===========
  '.id
  'control panels
  '100..199
  'controls
  '111 slider1
  '121 slider2
  '131 slider3
  '141 slider4
  '151 light button1
  '161 light button2
  '171 light button3
  '181 light button4
  '200..299 unused
  '300..399 unused
  'data area---------
  'md       400
  'colored area------
  'mpol     900..999
  'polygon nodes-----
  'mu[1]   2100..2199
  'mu[2]   2200..2299
  'mu[3]   2300..2399
  'node[1000] 10000..12000
  '
  static single ra,ri,angi1=.5
  static sys    initscene
  '
  '
  'WaitForEvent '0 off 1 on (default on)
  static quad t1,t2

  if opening 'FIRST CALL ONLY
    timemark t1
    'picked=100
    'mbox "helo"
  end if
  '
  if closing 'FINAL CALL BEFORE SHUTDOWN
    'mbox "Bye!"
    exit sub
  end if
  '
  cls
  '


  subroutine ShowPolygon
  ======================
  '
  pushstate 'polygon
  pushstate 'mpol
  static moveableobject mpol
  mpol.id=900
  mpol.mode=1
  mpol.act
  float zp=-0.001
  '
  'move 0,0,-1
  shading
  UserMovement mu,2100,3 'symbol identity, count
  if opening
    mu[1].x= 0
    mu[1].y= 5
    mu[2].x=-5
    mu[2].y=-5
    mu[3].x= 5
    mu[3].y=-5
  endif
  scale 1
  picklabel 0
  int i
  for i=1 to 3
    pushstate
    mu[i].act
    color 1,.9,0,1
    scale 0.4
    go sphere
    popstate
  next
  '
  picklabel 0
  '
  float xa,xb,xc,ya,yb,yc
  xa=mu[1].x : ya=mu[1].y
  xb=mu[2].x : yb=mu[2].y
  xc=mu[3].x : yc=mu[3].y
  '
  'MOUSE DRAG ADJUSTMENT
  select picked
    'mu.id ...
    case 2100 : xa+=mu[1].d.x : ya+=mu[1].d.y
    case 2200 : xb+=mu[2].d.x : yb+=mu[2].d.y
    case 2300 : xc+=mu[3].d.x : yc+=mu[3].d.y
  end select
  flat
  thickness 2
  '
  color 1,1,0,1
  '
  pushstate
  move xa-.5,ya-1
  gprint "A"
  popstate
  '
  pushstate
  move xb+1,yb+1
  gprint "B"
  popstate
  '
  pushstate
  move xc-1,yc+1
  gprint "C"
  popstate
  '
  color rr,gg,bb,aa 'linked to slider panel 1
  picklabel mpol.id
  float zp=-0.001
  glBegin GL_TRIANGLES
  glVertex3f xa,ya,zp
  glVertex3f xb,yb,zp
  glVertex3f xc,yc,zp
  glEnd
  picklabel 0
  '
  color 0,.9,.9
  glBegin GL_LINES
  glVertex2f xa,ya
  glVertex2f xb,yb
  glVertex2f xb,yb
  glVertex2f xc,yc
  glVertex2f xc,yc
  glVertex2f xa,ya
  glEnd

  popstate 'mpol

  move 5

    move -20,12
    static sys tally
    timemark t2
    scale 1.0
    '
    int dp=3
    '
    '
    /*
    pushstate 'md
    move 0,0,0.99
    color .5,.5,.7,.7
    static moveableobject md
    md.id=400
    md.mode=1
    md.act
    float zp=-0.001
    glBegin GL_QUADS
      glVertex3f -.1,-17,zp
      glVertex3f 11,-17,zp
      glVertex3f 11,2,zp
      glVertex3f -.10,2,zp
    glEnd
    if pick
      goto nDataPanel
    endif
    '
    macro pr1(a) 'PRINTING LIST
    -----------------------------
      pushstate : color .5,1,1 : print a : popstate
      printl ""
    end macro
    '
    macro pr2(a,b) 'PRINTING LIST
    -----------------------------
      pushstate : color .5,1,1 : print a : popstate
      pushstate : color 1,1,.5 : move 4 : print str(b,dp) : popstate
      printl ""
    end macro
    '
    macro pr3(a,b,c) 'PRINTING LIST
    ------------------------------
      pushstate : color .5,1,1 : print a : popstate
      pushstate : color 1,1,.5 : move 4 : print str(b,dp) : popstate
      pushstate : color 1,1,.5 : move 8 : print str(c,dp) : popstate
      printl ""
    end macro
    '
    '
    'DISPLAY INFO
    -------------
    picklabel 0

    pushstate
    scale 2
    color 1,1,1
    print "Triangle Data"
    popstate
    pr1 ""
    pr1 ""
   'pr2 "Action Code:   ", act
   'pr2 "indexbase      ", indexbase
   'pr2 "Keyboard Code: ", keyd
    pr2 "Picked ID:     ", picked
    '
    pr3 "Point A: ", str(xa,3), ya
    pr3 "Point B: ", str(xb,3), yb
    pr3 "Point C: ", str(xc,3), yc
    '
    float ab,ac,bc,ar,ht,hb
    '
    ab=hypot(xa-xb, ya-yb)
    ac=hypot(xa-xc, ya-yc)
    bc=hypot(xb-xc, yb-yc)
    '
    hb=0.5* ( ab*ab - ac*ac + bc*bc ) / bc
    ht=sqr(ab*ab-hb*hb)
    ar=0.5*bc*ht

    'ANGLES
    float na,nb,nc
    nb=deg(asin(ht/ab))
    nc=deg(asin(ht/ac))
    na=180-nb-nc

    pr1 ""
    pr2 "Angle A:",na
    pr2 "Angle B:",nb
    pr2 "Angle C:",nc
   
    pr1 ""
    pr2 "Line AB:",ab
    pr2 "Line AC:",ac
    pr2 "Line BC:",bc
    pr2 "Left base:",hb
    pr2 "Height:",ht
    pr2 "Area:", ar
    pr1 ""
    '
    nDataPanel:
    ===========
    popstate 'md
    */

    picklabel 0
    '
    'end polygon display
    '--------------------
    '
    popstate 'polygon
  end subroutine 'ShowPolygon

  'PANEL DISPLAY
  ==============

  static single ra,ri,angi1=.5
  static sys    initscene
 
  '------------------------------
 'glClearColor 0.5, 0.5, 0.7, 0
 'glClearColor 0.1, 0.2, 0.1, 0
  '
  glColor4f .99,.99,.99,.99
  'glEnable GL_TEXTURE_2D
  '
  'RENDERED LOWEST FIRST (FOR TRANSPARENCY BLENDING)
  '
  %                    ordn=1
  static ControlPanelA cb[ordn]
  static int           ord[16]
  static int           idb[16]
  '
  if closing
    int i
    for i=1 to ordn
      cb[i].destructor
    next
    exit sub
  endif
 
  '
  'INITIAL PLACEMENT ETC
  '
  if initscene=0
    ord={1,2,3}
    idb={100,200,300} 'panel identities
    cb[1].x=.8 : cb[1].y=.6 : cb[1].lbl="Color"
    int i
    'set scale
    for i=1 to ordn : cb[i].sc=2.5 : next
    initscene=1
  end if
  '
  'ORDER OF BUILD (SUPPORTING TRANSPARENCY)
  '
  if picked and bleft
    int a=1+trunc(picked/100)
    PlaceTop(a,ord,ordn)
  end if
  '
  'RENDER SCENE
  '
 GuiElements()
 '
 'place center
 move 16*aspect,-16.0,0.0
 '
  
 'TEST ANCESTRAL POLYGON
 =======================
 move 0,0,-2
 gosub ShowPolygon


 'TEST DRAW GUI
 ==============
 move 0,0,2
 pushstate
 GuiElements()
 popstate
 '
 'CONTROL PANELS
 ===============
 glpushmatrix
 static float layr=.001
 static float f = -1 - layr*ordn
 glscalef 15,15,1
 gltranslatef .0, .0, f
 int i,j
 for i=1 to ordn
  j=ord[i]
  cb[j].act idb[j] 'set identity
  gltranslatef .0, .0, layr 'next micro-layer in front
 next
 '
 'LINK TO SLIDERS
 'COLOR
 rr=0.5+cb[1].c[1].vy*.5
 gg=0.5+cb[1].c[2].vy*.5
 bb=0.5+cb[1].c[3].vy*.5
 aa=0.5+cb[1].c[4].vy*.5
 glpopmatrix
 '
 '
 ' ang1+=angi1
 ' if ang1>=360 then ang1-=360
 ' 
 end procedure 'main
 
 EndScript
