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

  1. /*
  2.  * maclights.c
  3.  * Code for the light editor
  4.  */
  5.  
  6. #include <MacHeaders>
  7. #include <Windows.h>
  8. #include <QuickDraw.h>
  9. #include <Controls.h>
  10. #include <Dialogs.h>
  11. #include <Lists.h>
  12. #include <Types.h>
  13. #include "geom.h"
  14. #include "light.h"
  15.  
  16. #include "point.h"
  17. #include "infinite.h"
  18. #include "spot.h"
  19. #include "jittered.h"
  20. #include "extended.h"
  21.  
  22. #include "macmodify.h"
  23. #include "maceditor.h"
  24. #include "macdialog.h"
  25.  
  26. extern DialogPtr editorDialog ;
  27. extern Geom *Objects ;            /* Named objects */
  28. extern Geom *World;
  29. extern Light *Lights;
  30. extern Light *CreateProtoLight(short type);
  31.  
  32. static Light *lightarray[50], *chosenlight;
  33. static short numlights;
  34. static ListHandle lightList ;
  35.  
  36. pascal void DrawLightList(WindowPtr window, short item)
  37. {
  38.     GrafPtr        currPort ;
  39.     PenState    saveState;
  40.     short        itemType;
  41.     Handle        itemHandle;
  42.     Rect        itemBox;
  43.  
  44.     GetPort(&currPort) ;
  45.     SetPort(window) ;
  46.     GetPenState(&saveState);
  47.     GetDItem(window, item, &itemType, &itemHandle, &itemBox);
  48.     PenNormal() ;
  49.     InsetRect(&itemBox,-1,-1) ;
  50.     FrameRect(&itemBox);
  51.     LUpdate(window->visRgn, lightList) ;
  52.     SetPenState(&saveState);
  53.     SetPort(currPort) ;
  54. }
  55.  
  56. void AddLightToList(ListHandle list, Light *light)
  57. {
  58.     char buffer[255];
  59.     Cell cell = {0,0};
  60.     int type;
  61.     short loop;
  62.  
  63.     type = LightType(light) ;
  64.     switch(type) {
  65.         case L_POINT:        sprintf(buffer,"Point") ;        break;
  66.         case L_INFINITE:    sprintf(buffer,"Direction");    break;
  67.         case L_SPOT:        sprintf(buffer,"Spotlight");    break;
  68.         case L_JITTERED:    sprintf(buffer,"Area");            break;
  69.         case L_EXTENDED:    sprintf(buffer,"Extended");        break;
  70.         default:            sprintf(buffer,"Unknown") ;        break;
  71.     }
  72.     for(loop = strlen(buffer) ; loop < 255 ; loop++) buffer[loop] = ' ';
  73.     cell.v = numlights ;
  74.     LAddRow(1,cell.v,list) ;
  75.     LSetCell(buffer, 255, cell, list);
  76. }
  77.  
  78. void SetLightParameters(DialogPtr dlog,Light *light)
  79. {
  80.     Str255 name;
  81.     char *str[4];
  82.  
  83.     HideDItem(dlog,lightfield1ET);
  84.     HideDItem(dlog,lightfield2ET);
  85.     HideDItem(dlog,lightfield3ET);
  86.     HideDItem(dlog,lightfield4ET);
  87.     HideDItem(dlog,lightcolourUI);
  88.     HideDItem(dlog,lightfield1ST);
  89.     HideDItem(dlog,lightfield2ST);
  90.     HideDItem(dlog,lightfield3ST);
  91.     HideDItem(dlog,lightfield4ST);
  92.     HideDItem(dlog,lightcolourST);
  93.     if(light) {
  94.         ShowDItem(dlog,lightcolourUI);
  95.         ShowDItem(dlog,lightcolourST);
  96.         switch(LightType(light)) {
  97.             case L_POINT:
  98.                 ShowDItem(dlog,lightfield1ET);
  99.                 ShowDItem(dlog,lightfield1ST);
  100.                 ShowDItem(dlog,lightfield2ET);
  101.                 ShowDItem(dlog,lightfield2ST);
  102.                 ShowDItem(dlog,lightfield3ET);
  103.                 ShowDItem(dlog,lightfield3ST);
  104.                 SetIText(SnatchHandle(dlog,lightfield1ST),"\pPos X") ;
  105.                 SetIText(SnatchHandle(dlog,lightfield2ST),"\pPos Y") ;
  106.                 SetIText(SnatchHandle(dlog,lightfield3ST),"\pPos Z") ;
  107.                 SetFloatEditText(dlog,lightfield1ET,((Pointlight *)light->light)->pos.x);
  108.                 SetFloatEditText(dlog,lightfield2ET,((Pointlight *)light->light)->pos.y);
  109.                 SetFloatEditText(dlog,lightfield3ET,((Pointlight *)light->light)->pos.z);
  110.                 break;
  111.             case  L_INFINITE:
  112.                 ShowDItem(dlog,lightfield1ET);
  113.                 ShowDItem(dlog,lightfield1ST);
  114.                 ShowDItem(dlog,lightfield2ET);
  115.                 ShowDItem(dlog,lightfield2ST);
  116.                 ShowDItem(dlog,lightfield3ET);
  117.                 ShowDItem(dlog,lightfield3ST);
  118.                 SetIText(SnatchHandle(dlog,lightfield1ST),"\pDir X") ;
  119.                 SetIText(SnatchHandle(dlog,lightfield2ST),"\pDir Y") ;
  120.                 SetIText(SnatchHandle(dlog,lightfield3ST),"\pDir Z") ;
  121.                 SetFloatEditText(dlog,lightfield1ET,((Infinite *)light->light)->dir.x);
  122.                 SetFloatEditText(dlog,lightfield2ET,((Infinite *)light->light)->dir.y);
  123.                 SetFloatEditText(dlog,lightfield3ET,((Infinite *)light->light)->dir.z);
  124.                 break;
  125.             case L_EXTENDED:
  126.                 ShowDItem(dlog,lightfield1ET);
  127.                 ShowDItem(dlog,lightfield1ST);
  128.                 ShowDItem(dlog,lightfield2ET);
  129.                 ShowDItem(dlog,lightfield2ST);
  130.                 ShowDItem(dlog,lightfield3ET);
  131.                 ShowDItem(dlog,lightfield3ST);
  132.                 ShowDItem(dlog,lightfield4ET);
  133.                 ShowDItem(dlog,lightfield4ST);
  134.                 SetIText(SnatchHandle(dlog,lightfield1ST),"\pPos X") ;
  135.                 SetIText(SnatchHandle(dlog,lightfield2ST),"\pPos Y") ;
  136.                 SetIText(SnatchHandle(dlog,lightfield3ST),"\pPos Z") ;
  137.                 SetIText(SnatchHandle(dlog,lightfield4ST),"\pRadius") ;
  138.                 SetFloatEditText(dlog,lightfield1ET,((Extended *)light->light)->pos.x);
  139.                 SetFloatEditText(dlog,lightfield2ET,((Extended *)light->light)->pos.y);
  140.                 SetFloatEditText(dlog,lightfield3ET,((Extended *)light->light)->pos.z);
  141.                 SetFloatEditText(dlog,lightfield4ET,((Extended *)light->light)->radius);
  142.                 break;
  143.         }
  144.     }
  145. }
  146.  
  147. pascal void DrawLightColour(WindowPtr window, short itemno)
  148. {
  149.     ControlHandle itemHandle ;
  150.     Rect itemRect ;
  151.     short itemType ;
  152.     GrafPtr currPort ;
  153.     Color col ;
  154.     RGBColor maccol, oldcol ;
  155.     
  156.     if(!chosenlight) return;
  157.     col = chosenlight->color;
  158.     GetPort(&currPort) ;
  159.     SetPort(window) ;
  160.     GetForeColor(&oldcol) ;
  161.     GetDItem(window, itemno, &itemType, &itemHandle, &itemRect);
  162.     EraseRect(&itemRect) ;
  163.     FrameRect(&itemRect) ;
  164.     InsetRect(&itemRect,2,2) ;
  165.     maccol.red = (unsigned short) (65535. * col.r) ;
  166.     maccol.green = (unsigned short) (65535. * col.g) ;
  167.     maccol.blue = (unsigned short) (65535. * col.b) ;
  168.     RGBForeColor(&maccol) ;
  169.     PaintRect(&itemRect) ;
  170.     RGBForeColor(&oldcol) ;
  171.     SetPort(currPort) ;
  172. }
  173.  
  174. void ValidateLight(DialogPtr dlog,Light *light)
  175. {    
  176.     Float x,y,z,r;
  177.  
  178.     if(!light) return;
  179.     switch(LightType(light)) {
  180.         case L_POINT:
  181.             if(GetFloatEditText(dlog,lightfield1ET,&x) &&
  182.                     GetFloatEditText(dlog,lightfield2ET,&y) &&
  183.                     GetFloatEditText(dlog,lightfield3ET,&z)) {
  184.                 ((Pointlight *)light->light)->pos.x = x;
  185.                 ((Pointlight *)light->light)->pos.y = y;
  186.                 ((Pointlight *)light->light)->pos.z = z;
  187.                 return;
  188.             }
  189.             SysBeep(1);
  190.             return;
  191.         case L_INFINITE:
  192.             if(GetFloatEditText(dlog,lightfield1ET,&x) &&
  193.                     GetFloatEditText(dlog,lightfield2ET,&y) &&
  194.                     GetFloatEditText(dlog,lightfield3ET,&z)) {
  195.                 ((Infinite *)light->light)->dir.x = x;
  196.                 ((Infinite *)light->light)->dir.y = y;
  197.                 ((Infinite *)light->light)->dir.z = z;
  198.                 return;
  199.             }
  200.             SysBeep(1);
  201.             return;
  202.         case L_EXTENDED:
  203.             if(GetFloatEditText(dlog,lightfield1ET,&x) &&
  204.                     GetFloatEditText(dlog,lightfield2ET,&y) &&
  205.                     GetFloatEditText(dlog,lightfield3ET,&z) &&
  206.                     GetFloatEditText(dlog,lightfield4ET,&r)) {
  207.                 ((Extended *)light->light)->pos.x = x;
  208.                 ((Extended *)light->light)->pos.y = y;
  209.                 ((Extended *)light->light)->pos.z = z;
  210.                 ((Extended *)light->light)->radius = r;
  211.                 return;
  212.             }
  213.             SysBeep(1);
  214.             return;
  215.     }
  216. }
  217.  
  218. void EditLights()
  219. {
  220.     GrafPtr currPort;
  221.     ControlHandle itemHandle ;
  222.     Rect itemRect, bounds ;
  223.     DialogPtr lightDialog ;
  224.     short hitItem, itemType, loop, loop2 ;
  225.     char exit = 0, doubleClick, fail ;
  226.     Light *light, *newlight;
  227.     Point cellSize = {0,0}, mousePoint ;
  228.     Cell myCell;
  229.  
  230.     lightDialog = GetNewDialog(lightdialogR, 0L, (WindowPtr)-1);
  231.     
  232.     chosenlight = NULL;
  233.     SetLightParameters(lightDialog,chosenlight);
  234.     
  235.     GetDItem(lightDialog, lightcolourUI, &itemType, &itemHandle, &itemRect);
  236.     SetDItem(lightDialog, lightcolourUI, itemType, (ProcPtr) DrawLightColour, &itemRect);
  237.     GetDItem(lightDialog, lightlistUI, &itemType, &itemHandle, &itemRect);
  238.     SetDItem(lightDialog, lightlistUI, itemType, (ProcPtr) DrawLightList, &itemRect);
  239.     SetRect(&bounds,0,0,1,0) ;
  240.     itemRect.right -= 15 ;
  241.     lightList = LNew(&itemRect,&bounds,cellSize,0,lightDialog,TRUE,FALSE,FALSE,TRUE) ;
  242.     numlights = 0 ;
  243.     for(light = Lights ; light ; light = light->next) {
  244.         AddLightToList(lightList,light);
  245.         lightarray[numlights++] = light ;
  246.     }
  247.     
  248.     DrawDialog(lightDialog) ;
  249.     DrawHilite(lightDialog,lightdoneBU) ;
  250.     GetPort(&currPort);
  251.     SetPort(lightDialog);
  252.     
  253.     do {
  254.         ModalDialog((ModalFilterProcPtr)NULL, &hitItem) ;
  255.         switch(hitItem) {
  256.             case lightdeleteBU:
  257.                 if(chosenlight) {
  258.                     loop = 0 ;
  259.                     while((lightarray[loop]!=chosenlight) && (loop < numlights)) loop++;
  260.                     LDelRow(1,(short)loop,lightList);
  261.                     
  262.                     for(loop2 = loop ; loop2 < numlights ;loop2++)
  263.                         lightarray[loop2] = lightarray[loop2+1];
  264.                     numlights--; 
  265.                     Lights = lightarray[0];
  266.                     for(loop=0; loop < numlights;loop++)
  267.                         lightarray[loop]->next = lightarray[loop+1];
  268.                     lightarray[numlights-1]->next = NULL;
  269.                     DeleteLight(chosenlight);
  270.                     chosenlight=NULL;                    
  271.                     SetLightParameters(lightDialog,chosenlight);
  272.                 }
  273.                 break;
  274.             case lightcolourUI:
  275.                 if(chosenlight) {
  276.                     PickColour(&chosenlight->color) ;
  277.                     DrawHilite(lightDialog,lightdoneBU) ;
  278.                 }
  279.                 break;
  280.             case lightlistUI:
  281.                 ValidateLight(lightDialog,chosenlight);
  282.                 GetMouse(&mousePoint);
  283.                 doubleClick = LClick(mousePoint,0,lightList) ;
  284.                 myCell = LLastClick(lightList) ;
  285.                 if((myCell.v >=0) && (myCell.v < numlights)) {
  286.                     chosenlight = lightarray[myCell.v] ;
  287.                 }
  288.                 else {
  289.                     chosenlight = NULL ;
  290.                     for(loop = 0 ;loop <numlights; loop++) {
  291.                         myCell.v = loop;
  292.                         LSetSelect(FALSE,myCell,lightList);    
  293.                     }
  294.                 }
  295.                 SetLightParameters(lightDialog,chosenlight);
  296.                 break ;
  297.             case lightdoneBU:
  298.                 ValidateLight(lightDialog,chosenlight);
  299.                 exit = 1 ;
  300.                 break ;
  301.             case lightextendedcreateBU:
  302.                 newlight = CreateProtoLight(L_EXTENDED);
  303.                 if(newlight) {
  304.                     if(Lights) {
  305.                         for(light = Lights ; light->next ; light = light->next);
  306.                         light->next = newlight;
  307.                     }
  308.                     else
  309.                         Lights = newlight;
  310.                     AddLightToList(lightList,newlight);
  311.                     lightarray[numlights++] = newlight;
  312.                 }
  313.                 break ;
  314.             case lightdirectionalcreateBU:
  315.                 newlight = CreateProtoLight(L_INFINITE);
  316.                 if(newlight) {
  317.                     if(Lights) {
  318.                         for(light = Lights ; light->next ; light = light->next);
  319.                         light->next = newlight;
  320.                     }
  321.                     else
  322.                         Lights = newlight;
  323.                     AddLightToList(lightList,newlight);
  324.                     lightarray[numlights++] = newlight;
  325.                 }
  326.                 break ;
  327.             case lightpointcreateBU:
  328.                 newlight = CreateProtoLight(L_POINT);
  329.                 if(newlight) {
  330.                     if(Lights) {
  331.                         for(light = Lights ; light->next ; light = light->next);
  332.                         light->next = newlight;
  333.                     }
  334.                     else
  335.                         Lights = newlight;
  336.                     AddLightToList(lightList,newlight);
  337.                     lightarray[numlights++] = newlight;
  338.                 }
  339.                 break ;
  340.         }
  341.     } while (!exit);
  342.     LDispose(lightList) ;
  343.     DisposDialog(lightDialog) ;
  344.     SetPort(editorDialog);
  345.     InvalRect(&editorDialog->portRect) ;
  346.     SetPort(currPort);
  347. }
  348.