/*
======================================================================
plugui.c

Ernie Wright  01 Jun 05

The graphical user interface for the SPD plug-in.
====================================================================== */

#include <lwxpanel.h>
#include <lwsurf.h>
#include <lwhost.h>
#include <stdlib.h>
#include <string.h>
#include "def.h"


static char *namelist[] = {
   "Gears",       // SPD_GEARS
   "Jacks",       // SPD_JACKS
   "Lattice",     // SPD_LATTICE
   "Mountain",    // SPD_MOUNT
   "Rings",       // SPD_RINGS
   "Sample",      // SPD_GENERIC
   "Shells",      // SPD_SHELLS
   "Sombrero",    // SPD_SOMBRERO
   "Sphereflake", // SPD_BALLS
   "Teapot",      // SPD_TEAPOT
   "Tetra",       // SPD_TETRA
   "Tree",        // SPD_TREE
   NULL,          0
};

static int indexlist[] = {
   SPD_GEARS,
   SPD_JACKS,
   SPD_LATTICE,
   SPD_MOUNT,
   SPD_RINGS,
   SPD_GENERIC,
   SPD_SHELLS,
   SPD_SOMBRERO,
   SPD_BALLS,
   SPD_TEAPOT,
   SPD_TETRA,
   SPD_TREE,
   0
};


/*
======================================================================
match_spdname()

Given the name of an SPD routine, return the corresponding ID, or 0 if
the name isn't found.
====================================================================== */

int match_spdname( char *name )
{
   int i;

   for ( i = 0; namelist[ i ]; i++ )
      if ( !stricmp( name, namelist[ i ] ))
         return indexlist[ i ];

   return 0;
}


/*
======================================================================
match_spdindex()

Given the SPD_ index of an SPD routine, return the corresponding array
index in the namelist[] and indexlist[] arrays.  Returns 0 if the
index isn't found.
====================================================================== */

static int match_spdindex( int index )
{
   int i;

   for ( i = 0; namelist[ i ]; i++ )
      if ( index == indexlist[ i ] )
         return i;
   return 0;
}


/*
======================================================================
get_user()

Display a modal user interface.
====================================================================== */

int get_user( LWXPanelFuncs *xpanf, int *spd, int *sf, int *teslvl,
   int *merge, int *savescene, char *filename )
{
   LWXPanelID panel;
   int ok = 0, index;

   enum {
      ID_SPDLIST = 0x8001,
      ID_SIZEF,
      ID_TESLVL,
      ID_MERGE,
      ID_SAVESCENE,
      ID_FILENAME,
      ID_SIZEGRP,
      ID_MERGEGRP,
      ID_FILEGRP
   };
   LWXPanelControl ctl[] = {
      { ID_SPDLIST,   "Type",              "iPopChoice" },
      { ID_SIZEF,     "Complexity",        "integer"    },
      { ID_TESLVL,    "Tesselation",       "integer"    },
      { ID_MERGE,     "Merge Points",      "iBoolean"   },
      { ID_SAVESCENE, "Create Scene File", "iBoolean"   },
      { ID_FILENAME,  "Filename",          "sFileName"  },
      { 0 }
   };
   LWXPanelDataDesc cdata[] = {
      { ID_SPDLIST,   "Type",              "integer"    },
      { ID_SIZEF,     "Complexity",        "integer"    },
      { ID_TESLVL,    "Tesselation",       "integer"    },
      { ID_MERGE,     "Merge POints",      "integer"    },
      { ID_SAVESCENE, "Create Scene File", "integer"    },
      { ID_FILENAME,  "File",              "string"     },
      { 0 }
   };
   LWXPanelHint hint[] = {
      XpLABEL( 0, "Standard Procedurals" ),
      XpSTRLIST( ID_SPDLIST, namelist ),
      XpMIN( ID_SIZEF, 1 ),
      XpMAX( ID_SIZEF, 20 ),
      XpMIN( ID_TESLVL, 1 ),
      XpMAX( ID_TESLVL, 32 ),
      XpXREQCFG( ID_FILENAME, LWXPREQ_SAVE, "SPD Scene File", LWFTYPE_SCENE ),
      XpENABLE_( ID_SAVESCENE ),
         XpH( ID_FILENAME ),
         XpEND,
      XpGROUP_( ID_SIZEGRP ),
         XpH( ID_SIZEF ),
         XpH( ID_TESLVL ),
         XpEND,
      XpGROUP_( ID_MERGEGRP ),
         XpH( ID_MERGE ),
         XpEND,
      XpGROUP_( ID_FILEGRP ),
         XpH( ID_SAVESCENE ),
         XpH( ID_FILENAME ),
         XpEND,
      XpEND
   };

   panel = xpanf->create( LWXP_FORM, ctl );
   if ( !panel ) return 0;

   xpanf->describe( panel, cdata, NULL, NULL );
   xpanf->hint( panel, 0, hint );
   xpanf->formSet( panel, ID_SIZEF, sf );
   xpanf->formSet( panel, ID_TESLVL, teslvl );
   index = match_spdindex( *spd );
   xpanf->formSet( panel, ID_SPDLIST, &index );
   xpanf->formSet( panel, ID_MERGE, merge );
   xpanf->formSet( panel, ID_SAVESCENE, savescene );
   xpanf->formSet( panel, ID_FILENAME, filename );

   ok = xpanf->post( panel );

   if ( ok ) {
      int *i;
      char *s;

      i = xpanf->formGet( panel, ID_SPDLIST );
      *spd = indexlist[ *i ];
      i = xpanf->formGet( panel, ID_SIZEF );
      *sf = *i;
      i = xpanf->formGet( panel, ID_TESLVL );
      *teslvl = *i;
      i = xpanf->formGet( panel, ID_MERGE );
      *merge = *i;
      i = xpanf->formGet( panel, ID_SAVESCENE );
      *savescene = *i;
      s = xpanf->formGet( panel, ID_FILENAME );
      strcpy( filename, s );
   }

   xpanf->destroy( panel );
   return ok;
}
