home *** CD-ROM | disk | FTP | other *** search
- /*
- * maclights.c
- * Code for the light editor
- */
-
- #include <MacHeaders>
- #include <Windows.h>
- #include <QuickDraw.h>
- #include <Controls.h>
- #include <Dialogs.h>
- #include <Lists.h>
- #include <Types.h>
- #include "geom.h"
- #include "light.h"
-
- #include "point.h"
- #include "infinite.h"
- #include "spot.h"
- #include "jittered.h"
- #include "extended.h"
-
- #include "macmodify.h"
- #include "maceditor.h"
- #include "macdialog.h"
-
- extern DialogPtr editorDialog ;
- extern Geom *Objects ; /* Named objects */
- extern Geom *World;
- extern Light *Lights;
- extern Light *CreateProtoLight(short type);
-
- static Light *lightarray[50], *chosenlight;
- static short numlights;
- static ListHandle lightList ;
-
- pascal void DrawLightList(WindowPtr window, short item)
- {
- GrafPtr currPort ;
- PenState saveState;
- short itemType;
- Handle itemHandle;
- Rect itemBox;
-
- GetPort(&currPort) ;
- SetPort(window) ;
- GetPenState(&saveState);
- GetDItem(window, item, &itemType, &itemHandle, &itemBox);
- PenNormal() ;
- InsetRect(&itemBox,-1,-1) ;
- FrameRect(&itemBox);
- LUpdate(window->visRgn, lightList) ;
- SetPenState(&saveState);
- SetPort(currPort) ;
- }
-
- void AddLightToList(ListHandle list, Light *light)
- {
- char buffer[255];
- Cell cell = {0,0};
- int type;
- short loop;
-
- type = LightType(light) ;
- switch(type) {
- case L_POINT: sprintf(buffer,"Point") ; break;
- case L_INFINITE: sprintf(buffer,"Direction"); break;
- case L_SPOT: sprintf(buffer,"Spotlight"); break;
- case L_JITTERED: sprintf(buffer,"Area"); break;
- case L_EXTENDED: sprintf(buffer,"Extended"); break;
- default: sprintf(buffer,"Unknown") ; break;
- }
- for(loop = strlen(buffer) ; loop < 255 ; loop++) buffer[loop] = ' ';
- cell.v = numlights ;
- LAddRow(1,cell.v,list) ;
- LSetCell(buffer, 255, cell, list);
- }
-
- void SetLightParameters(DialogPtr dlog,Light *light)
- {
- Str255 name;
- char *str[4];
-
- HideDItem(dlog,lightfield1ET);
- HideDItem(dlog,lightfield2ET);
- HideDItem(dlog,lightfield3ET);
- HideDItem(dlog,lightfield4ET);
- HideDItem(dlog,lightcolourUI);
- HideDItem(dlog,lightfield1ST);
- HideDItem(dlog,lightfield2ST);
- HideDItem(dlog,lightfield3ST);
- HideDItem(dlog,lightfield4ST);
- HideDItem(dlog,lightcolourST);
- if(light) {
- ShowDItem(dlog,lightcolourUI);
- ShowDItem(dlog,lightcolourST);
- switch(LightType(light)) {
- case L_POINT:
- ShowDItem(dlog,lightfield1ET);
- ShowDItem(dlog,lightfield1ST);
- ShowDItem(dlog,lightfield2ET);
- ShowDItem(dlog,lightfield2ST);
- ShowDItem(dlog,lightfield3ET);
- ShowDItem(dlog,lightfield3ST);
- SetIText(SnatchHandle(dlog,lightfield1ST),"\pPos X") ;
- SetIText(SnatchHandle(dlog,lightfield2ST),"\pPos Y") ;
- SetIText(SnatchHandle(dlog,lightfield3ST),"\pPos Z") ;
- SetFloatEditText(dlog,lightfield1ET,((Pointlight *)light->light)->pos.x);
- SetFloatEditText(dlog,lightfield2ET,((Pointlight *)light->light)->pos.y);
- SetFloatEditText(dlog,lightfield3ET,((Pointlight *)light->light)->pos.z);
- break;
- case L_INFINITE:
- ShowDItem(dlog,lightfield1ET);
- ShowDItem(dlog,lightfield1ST);
- ShowDItem(dlog,lightfield2ET);
- ShowDItem(dlog,lightfield2ST);
- ShowDItem(dlog,lightfield3ET);
- ShowDItem(dlog,lightfield3ST);
- SetIText(SnatchHandle(dlog,lightfield1ST),"\pDir X") ;
- SetIText(SnatchHandle(dlog,lightfield2ST),"\pDir Y") ;
- SetIText(SnatchHandle(dlog,lightfield3ST),"\pDir Z") ;
- SetFloatEditText(dlog,lightfield1ET,((Infinite *)light->light)->dir.x);
- SetFloatEditText(dlog,lightfield2ET,((Infinite *)light->light)->dir.y);
- SetFloatEditText(dlog,lightfield3ET,((Infinite *)light->light)->dir.z);
- break;
- case L_EXTENDED:
- ShowDItem(dlog,lightfield1ET);
- ShowDItem(dlog,lightfield1ST);
- ShowDItem(dlog,lightfield2ET);
- ShowDItem(dlog,lightfield2ST);
- ShowDItem(dlog,lightfield3ET);
- ShowDItem(dlog,lightfield3ST);
- ShowDItem(dlog,lightfield4ET);
- ShowDItem(dlog,lightfield4ST);
- SetIText(SnatchHandle(dlog,lightfield1ST),"\pPos X") ;
- SetIText(SnatchHandle(dlog,lightfield2ST),"\pPos Y") ;
- SetIText(SnatchHandle(dlog,lightfield3ST),"\pPos Z") ;
- SetIText(SnatchHandle(dlog,lightfield4ST),"\pRadius") ;
- SetFloatEditText(dlog,lightfield1ET,((Extended *)light->light)->pos.x);
- SetFloatEditText(dlog,lightfield2ET,((Extended *)light->light)->pos.y);
- SetFloatEditText(dlog,lightfield3ET,((Extended *)light->light)->pos.z);
- SetFloatEditText(dlog,lightfield4ET,((Extended *)light->light)->radius);
- break;
- }
- }
- }
-
- pascal void DrawLightColour(WindowPtr window, short itemno)
- {
- ControlHandle itemHandle ;
- Rect itemRect ;
- short itemType ;
- GrafPtr currPort ;
- Color col ;
- RGBColor maccol, oldcol ;
-
- if(!chosenlight) return;
- col = chosenlight->color;
- GetPort(&currPort) ;
- SetPort(window) ;
- GetForeColor(&oldcol) ;
- GetDItem(window, itemno, &itemType, &itemHandle, &itemRect);
- EraseRect(&itemRect) ;
- FrameRect(&itemRect) ;
- InsetRect(&itemRect,2,2) ;
- maccol.red = (unsigned short) (65535. * col.r) ;
- maccol.green = (unsigned short) (65535. * col.g) ;
- maccol.blue = (unsigned short) (65535. * col.b) ;
- RGBForeColor(&maccol) ;
- PaintRect(&itemRect) ;
- RGBForeColor(&oldcol) ;
- SetPort(currPort) ;
- }
-
- void ValidateLight(DialogPtr dlog,Light *light)
- {
- Float x,y,z,r;
-
- if(!light) return;
- switch(LightType(light)) {
- case L_POINT:
- if(GetFloatEditText(dlog,lightfield1ET,&x) &&
- GetFloatEditText(dlog,lightfield2ET,&y) &&
- GetFloatEditText(dlog,lightfield3ET,&z)) {
- ((Pointlight *)light->light)->pos.x = x;
- ((Pointlight *)light->light)->pos.y = y;
- ((Pointlight *)light->light)->pos.z = z;
- return;
- }
- SysBeep(1);
- return;
- case L_INFINITE:
- if(GetFloatEditText(dlog,lightfield1ET,&x) &&
- GetFloatEditText(dlog,lightfield2ET,&y) &&
- GetFloatEditText(dlog,lightfield3ET,&z)) {
- ((Infinite *)light->light)->dir.x = x;
- ((Infinite *)light->light)->dir.y = y;
- ((Infinite *)light->light)->dir.z = z;
- return;
- }
- SysBeep(1);
- return;
- case L_EXTENDED:
- if(GetFloatEditText(dlog,lightfield1ET,&x) &&
- GetFloatEditText(dlog,lightfield2ET,&y) &&
- GetFloatEditText(dlog,lightfield3ET,&z) &&
- GetFloatEditText(dlog,lightfield4ET,&r)) {
- ((Extended *)light->light)->pos.x = x;
- ((Extended *)light->light)->pos.y = y;
- ((Extended *)light->light)->pos.z = z;
- ((Extended *)light->light)->radius = r;
- return;
- }
- SysBeep(1);
- return;
- }
- }
-
- void EditLights()
- {
- GrafPtr currPort;
- ControlHandle itemHandle ;
- Rect itemRect, bounds ;
- DialogPtr lightDialog ;
- short hitItem, itemType, loop, loop2 ;
- char exit = 0, doubleClick, fail ;
- Light *light, *newlight;
- Point cellSize = {0,0}, mousePoint ;
- Cell myCell;
-
- lightDialog = GetNewDialog(lightdialogR, 0L, (WindowPtr)-1);
-
- chosenlight = NULL;
- SetLightParameters(lightDialog,chosenlight);
-
- GetDItem(lightDialog, lightcolourUI, &itemType, &itemHandle, &itemRect);
- SetDItem(lightDialog, lightcolourUI, itemType, (ProcPtr) DrawLightColour, &itemRect);
- GetDItem(lightDialog, lightlistUI, &itemType, &itemHandle, &itemRect);
- SetDItem(lightDialog, lightlistUI, itemType, (ProcPtr) DrawLightList, &itemRect);
- SetRect(&bounds,0,0,1,0) ;
- itemRect.right -= 15 ;
- lightList = LNew(&itemRect,&bounds,cellSize,0,lightDialog,TRUE,FALSE,FALSE,TRUE) ;
- numlights = 0 ;
- for(light = Lights ; light ; light = light->next) {
- AddLightToList(lightList,light);
- lightarray[numlights++] = light ;
- }
-
- DrawDialog(lightDialog) ;
- DrawHilite(lightDialog,lightdoneBU) ;
- GetPort(&currPort);
- SetPort(lightDialog);
-
- do {
- ModalDialog((ModalFilterProcPtr)NULL, &hitItem) ;
- switch(hitItem) {
- case lightdeleteBU:
- if(chosenlight) {
- loop = 0 ;
- while((lightarray[loop]!=chosenlight) && (loop < numlights)) loop++;
- LDelRow(1,(short)loop,lightList);
-
- for(loop2 = loop ; loop2 < numlights ;loop2++)
- lightarray[loop2] = lightarray[loop2+1];
- numlights--;
- Lights = lightarray[0];
- for(loop=0; loop < numlights;loop++)
- lightarray[loop]->next = lightarray[loop+1];
- lightarray[numlights-1]->next = NULL;
- DeleteLight(chosenlight);
- chosenlight=NULL;
- SetLightParameters(lightDialog,chosenlight);
- }
- break;
- case lightcolourUI:
- if(chosenlight) {
- PickColour(&chosenlight->color) ;
- DrawHilite(lightDialog,lightdoneBU) ;
- }
- break;
- case lightlistUI:
- ValidateLight(lightDialog,chosenlight);
- GetMouse(&mousePoint);
- doubleClick = LClick(mousePoint,0,lightList) ;
- myCell = LLastClick(lightList) ;
- if((myCell.v >=0) && (myCell.v < numlights)) {
- chosenlight = lightarray[myCell.v] ;
- }
- else {
- chosenlight = NULL ;
- for(loop = 0 ;loop <numlights; loop++) {
- myCell.v = loop;
- LSetSelect(FALSE,myCell,lightList);
- }
- }
- SetLightParameters(lightDialog,chosenlight);
- break ;
- case lightdoneBU:
- ValidateLight(lightDialog,chosenlight);
- exit = 1 ;
- break ;
- case lightextendedcreateBU:
- newlight = CreateProtoLight(L_EXTENDED);
- if(newlight) {
- if(Lights) {
- for(light = Lights ; light->next ; light = light->next);
- light->next = newlight;
- }
- else
- Lights = newlight;
- AddLightToList(lightList,newlight);
- lightarray[numlights++] = newlight;
- }
- break ;
- case lightdirectionalcreateBU:
- newlight = CreateProtoLight(L_INFINITE);
- if(newlight) {
- if(Lights) {
- for(light = Lights ; light->next ; light = light->next);
- light->next = newlight;
- }
- else
- Lights = newlight;
- AddLightToList(lightList,newlight);
- lightarray[numlights++] = newlight;
- }
- break ;
- case lightpointcreateBU:
- newlight = CreateProtoLight(L_POINT);
- if(newlight) {
- if(Lights) {
- for(light = Lights ; light->next ; light = light->next);
- light->next = newlight;
- }
- else
- Lights = newlight;
- AddLightToList(lightList,newlight);
- lightarray[numlights++] = newlight;
- }
- break ;
- }
- } while (!exit);
- LDispose(lightList) ;
- DisposDialog(lightDialog) ;
- SetPort(editorDialog);
- InvalRect(&editorDialog->portRect) ;
- SetPort(currPort);
- }
-