home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / macraysh.sit / Code / Source / mactexture.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-13  |  8.8 KB  |  316 lines

  1. /*
  2.  * Maceditor.c
  3.  */
  4.  
  5. #include <MacHeaders>
  6. #include <Windows.h>
  7. #include <QuickDraw.h>
  8. #include <Controls.h>
  9. #include <Dialogs.h>
  10. #include <Lists.h>
  11. #include <Types.h>
  12.  
  13. #include "texture.h"
  14. #include "geom.h"
  15. #include "surface.h"
  16. #include "light.h"
  17. #include "image.h"
  18.  
  19. #include "blotch.h"
  20. #include "bump.h"
  21. #include "checker.h"
  22. #include "cloud.h"
  23. #include "fbm.h"
  24. #include "fbmbump.h"
  25. #include "gloss.h"
  26. #include "imagetext.h"
  27. #include "marble.h"
  28. #include "mount.h"
  29. #include "sky.h"
  30. #include "stripe.h"
  31. #include "windy.h"
  32. #include "wood.h"
  33.  
  34. #include "macmodify.h"
  35. #include "maceditor.h"
  36. #include "macdialog.h"
  37.  
  38. extern DialogPtr editorDialog ;
  39. extern Surface *Surfaces ;        /* Named surface instances */
  40. extern Geom *Objects ;            /* Named objects */
  41. extern Geom *World;
  42. extern Light *Lights;
  43.  
  44. extern Texture *CreateTexture(short type) ;
  45. Surface *EditSurface(Geom *obj) ;
  46.  
  47.  
  48. struct TextureTemplate {
  49.     char *name ;
  50.     char *fieldnames[8] ;
  51.     char surface_enabled ;
  52. } ;
  53.  
  54. static Surface tmpsurf ;
  55. static ListHandle textList ;
  56. static struct TextureTemplate templates[14] = {
  57.     {"Blotch",{ "Mix",0,0,0,0,0,0,0 },1},
  58.     {"Bump",{ "Size",0,0,0,0,0,0,0 },0},
  59.     {"Checker",{ 0,0,0,0,0,0,0,0 },1},
  60.     {"Cloud",{"Scale","h","Lambda","Octaves","CThresh","LThresh","Transcale",0 },0},
  61.     {"FBm",{ "Offset","Scale","h","Lambda","Octaves","Thresh",0,0 },0},
  62.     {"FBm Bump",{ "Offset","Scale","h","Lambda","Octaves",0,0,0 },0},
  63.     {"Gloss",{ "Glossiness",0,0,0,0,0,0,0 },0},
  64.     {"Image",{ 0,0,0,0,0,0,0,0 },0},
  65.     {"Marble",{ 0,0,0,0,0,0,0,0 },0},
  66.     {"Sky",{ "Scale","h","Lambda","Octaves","cthresh","lthresh",0,0 },0},
  67.     {"Stripe",{ "Width","Bump", 0,0,0,0,0,0 },1},
  68.     {"Wood",{ 0,0,0,0,0,0,0 },0}
  69. } ;
  70.  
  71.  
  72. pascal void DrawTextureList(WindowPtr window, short item)
  73. {
  74.     GrafPtr        currPort ;
  75.     PenState    saveState;
  76.     short        itemType;
  77.     Handle        itemHandle;
  78.     Rect        itemBox;
  79.  
  80.     GetPort(&currPort) ;
  81.     SetPort(window) ;
  82.     GetPenState(&saveState);
  83.     GetDItem(window, item, &itemType, &itemHandle, &itemBox);
  84.     PenNormal() ;
  85.     InsetRect(&itemBox,-1,-1) ;
  86.     FrameRect(&itemBox);
  87.     LUpdate(window->visRgn, textList) ;
  88.     SetPenState(&saveState);
  89.     SetPort(currPort) ;
  90. }
  91.  
  92. void SetTextureText(DialogPtr dlog,int type)
  93. {
  94.     Str255 name ;
  95.     struct TextureTemplate *temp ;
  96.     short loop ;
  97.         
  98.     temp = &templates[type] ;
  99.     for(loop = textfield1ST ; loop <= textfield7ST ; loop++) {
  100.         if((type != -1) && (temp->fieldnames[loop-textfield1ST] != NULL)) {
  101.             name[0] = strlen(temp->fieldnames[loop-textfield1ST]) ;
  102.             sprintf(&name[1],"%s",temp->fieldnames[loop-textfield1ST]) ;
  103.             SetIText(SnatchHandle(dlog,loop),name) ;
  104.             ShowDItem(dlog,loop);
  105.             ShowDItem(dlog,loop-textfield1ST+textfield1ET);
  106.         }
  107.         else {
  108.             HideDItem(dlog,loop);
  109.             HideDItem(dlog,loop-textfield1ST+textfield1ET);
  110.         }
  111.     }
  112.     if(temp->surface_enabled)
  113.         ShowDItem(dlog,textsurfacepickBU);
  114.     else
  115.         HideDItem(dlog,textsurfacepickBU);
  116. }
  117.  
  118.  
  119. Texture *EditTexture(Geom *obj)
  120. {
  121.     GrafPtr currPort;
  122.     ControlHandle itemHandle ;
  123.     Rect itemRect, bounds ;
  124.     DialogPtr textDialog ;
  125.     short hitItem, itemType, loop, loop2, textsize ;
  126.     char exit = 0, buffer[255], doubleClick, fail ;
  127.     Texture *textarray[50], *chosentexture, *text;
  128.     Surface *surf, *surf2;
  129.     Geom dummyobject;
  130.     Point mousePoint, cellSize ;
  131.     Cell cell = {0,0}, myCell;
  132.     Mapping *mapping;
  133.     Float param[8] ;
  134.     int type ;
  135.  
  136.     textDialog = GetNewDialog(textdialogR, 0L, (WindowPtr)-1) ;
  137.     
  138.     dummyobject.trans = NULL;
  139.     
  140.     /* Turn our user item into a texture list */
  141.     GetDItem(textDialog, textlistUI, &itemType, &itemHandle, &itemRect);
  142.     SetDItem(textDialog, textlistUI, itemType, (ProcPtr) DrawTextureList, &itemRect);
  143.     SetRect(&bounds,0,0,1,0) ;
  144.     itemRect.right -= 15;
  145.     cellSize.v = cellSize.h = 0;
  146.     textList = LNew(&itemRect,&bounds,cellSize,0,textDialog,TRUE,FALSE,FALSE,TRUE) ;
  147.     /* Insert some values */
  148.     textsize = 0 ;
  149.     for(text = obj->texture ; text ; text = text->next) {
  150.         type = TextureType(text) ;
  151.         sprintf(buffer,"%s", templates[type].name) ;
  152.         for(loop = strlen(buffer) ; loop < 255 ; loop++) buffer[loop] = ' ';
  153.         cell.v = textsize ;
  154.         textarray[textsize++] = text ;
  155.         LAddRow(1,cell.v,textList) ;
  156.         LSetCell(buffer,255,cell,textList);
  157.     }
  158.  
  159.     /* Now set up the dialog text fields to correspond to our given template */
  160.     itemHandle = SnatchHandle(textDialog,texttypeCI) ;
  161.     type = GetCtlValue(itemHandle)-1 ;
  162.     SetTextureText(textDialog,type) ;
  163.     chosentexture = NULL;
  164.     surf = SurfaceCreate();
  165.     /* Redraw the dialog and add a thick band round the use button */
  166.     DrawDialog(textDialog) ;
  167.     GetPort(&currPort);
  168.     SetPort(textDialog);
  169.     DrawHilite(textDialog,textdoneBU) ;
  170.     HiliteControl(SnatchHandle(textDialog,textremoveBU),255) ;
  171.     HiliteControl(SnatchHandle(textDialog,texttransformsetBU),255) ;
  172.     do {
  173.         ModalDialog((ModalFilterProcPtr)NULL, &hitItem) ;
  174.         switch(hitItem) {
  175.             case textdoneBU:
  176.                 obj->texture = textarray[0];
  177.                 for(loop=0;loop < textsize; loop++)
  178.                     textarray[loop]->next = textarray[loop+1];
  179.                 textarray[textsize-1]->next =NULL;
  180.                 exit = 1 ;
  181.                 break ;
  182.             case textlistUI:
  183.                 GetMouse(&mousePoint);
  184.                 doubleClick = LClick(mousePoint,0,textList) ;
  185.                 myCell.v = myCell.h = 0;                
  186.                 if(LGetSelect(TRUE, &myCell, textList)) {
  187.                     chosentexture = textarray[myCell.v] ;
  188.                     HiliteControl(SnatchHandle(textDialog,textremoveBU),0) ;
  189.                     HiliteControl(SnatchHandle(textDialog,texttransformsetBU),0) ;
  190.                 }
  191.                 else {
  192.                     chosentexture = NULL ;
  193.                     HiliteControl(SnatchHandle(textDialog,textremoveBU),255) ;
  194.                     HiliteControl(SnatchHandle(textDialog,texttransformsetBU),255) ;
  195.                     for(loop = 0 ; loop < textsize ; loop++) {
  196.                         myCell.v = loop;
  197.                         LSetSelect(FALSE,myCell,textList);    
  198.                     }
  199.                 }
  200.                 break ;
  201.             case texttypeCI:
  202.                 itemHandle = SnatchHandle(textDialog,texttypeCI) ;
  203.                 type = GetCtlValue(itemHandle)-1 ;
  204.                 SetTextureText(textDialog,type) ;
  205.                 break ;
  206.             case textremoveBU:
  207.                 if(chosentexture) {
  208.                     loop =0;
  209.                     while((textarray[loop]!=chosentexture) && (loop < textsize)) loop++;
  210.                     LDelRow(1,(short)loop,textList);
  211.                     
  212.                     for(loop2 = loop; loop2 < textsize ;loop2++)
  213.                         textarray[loop2] = textarray[loop2+1];
  214.                     textsize--;
  215.                     
  216.                     obj->texture = textarray[0];
  217.                     for(loop=0;loop < textsize; loop++)
  218.                         textarray[loop]->next = textarray[loop+1];
  219.                     textarray[textsize-1]->next =NULL;
  220.                     DeleteTexture(chosentexture) ;
  221.                     chosentexture = 0 ;
  222.                     HiliteControl(SnatchHandle(textDialog,textremoveBU),255) ;
  223.                     HiliteControl(SnatchHandle(textDialog,texttransformsetBU),255) ;
  224.                 }
  225.                 break ;
  226.             case textcreateBU:
  227.                 if(textsize < 50) {
  228.                     itemHandle = SnatchHandle(textDialog,texttypeCI) ;
  229.                     type = GetCtlValue(itemHandle)-1 ;
  230.                     fail = 0 ;
  231.                     for(loop = 0 ; loop < 8 ; loop++) {
  232.                         if(templates[type].fieldnames[loop]) 
  233.                             if(!GetFloatEditText(textDialog,loop + textfield1ET,¶m[loop]))
  234.                                 fail = 1 ;
  235.                     
  236.                     }
  237.                     if(fail) break ;
  238.                     switch(type) {
  239.                         case T_BLOTCH:
  240.                             surf2 = SurfaceCreate();
  241.                             *surf2 = *surf;
  242.                             surf2->next = NULL;
  243.                             text = TextBlotchCreate(param[0],surf2) ;
  244.                             break ;
  245.                         case T_BUMP:
  246.                             text = TextBumpCreate(param[0]) ;
  247.                             break ;
  248.                         case T_CHECKER:
  249.                             surf2 = SurfaceCreate();
  250.                             *surf2 = *surf;
  251.                             surf2->next = NULL;
  252.                             text = TextCheckerCreate(surf2) ;
  253.                             break ;
  254.                         case T_CLOUD:
  255.                             text = TextCloudCreate(param[0],param[1],param[2],(int)param[3],param[4],param[5],param[6]) ;
  256.                             break ;
  257.                         case T_FBM:
  258.                             text = TextFBmCreate(param[0],param[1],param[2],param[3],(int)param[4],param[5],NULL) ;
  259.                             break ;
  260.                         case T_FBMBUMP:
  261.                             text = TextFBmBumpCreate(param[0],param[1],param[2],param[3],(int)param[4]) ;
  262.                             break ;
  263.                         case T_GLOSS:
  264.                             text = TextGlossCreate(param[0]) ;
  265.                             break ;
  266.                         case T_IMAGE:
  267.                             break ;
  268.                         case T_MARBLE:
  269.                             text = TextMarbleCreate(NULL) ;
  270.                             break ;
  271.                         case T_SKY:
  272.                             break ;
  273.                         case T_STRIPE:
  274.                             surf2 = SurfaceCreate();
  275.                             *surf2 = *surf;
  276.                             surf2->next = NULL;
  277.                             mapping = UVMappingCreate();
  278.                             text = TextStripeCreate(surf2,param[0],param[1],mapping) ;
  279.                             break ;
  280.                         case T_WOOD:
  281.                             text = TextWoodCreate() ;
  282.                             break ;    
  283.                     }
  284.                     if(text) {
  285.                         sprintf(buffer,"%s", templates[type].name) ;
  286.                         for(loop = strlen(buffer) ; loop < 255 ; loop++) buffer[loop] = ' ';
  287.                         textarray[textsize] = text ;
  288.                         text->next = NULL;
  289.                         cell.v = textsize;
  290.                         textsize++;
  291.                         LAddRow(1,cell.v,textList) ;
  292.                         LSetCell(buffer, 255, cell, textList);
  293.                     }
  294.                 }
  295.                 else
  296.                     SysBeep(1) ; /* Who needs 50 textures on an object anyway ? */
  297.                 break ;
  298.             case texttransformsetBU:
  299.                 if(chosentexture) {
  300.                     if(chosentexture->trans)
  301.                         DeleteTrans(chosentexture->trans);
  302.                     EditTransforms(&dummyobject);
  303.                     chosentexture->trans = dummyobject.trans;
  304.                     dummyobject.trans = NULL;                    
  305.                 }
  306.                 break;
  307.             case textsurfacepickBU:
  308.                 dummyobject.surf = surf;
  309.                 EditSurface(&dummyobject) ;
  310.                 break ; 
  311.         }
  312.     } while (!exit) ;
  313.     LDispose(textList) ;
  314.     SetPort(currPort);
  315.     DisposDialog(textDialog) ;
  316. }