home *** CD-ROM | disk | FTP | other *** search
- /*
- * Maceditor.c
- */
-
- #include <MacHeaders>
- #include <Windows.h>
- #include <QuickDraw.h>
- #include <Dialogs.h>
- #include <Lists.h>
- #include <Types.h>
-
- #include "geom.h"
- #include "list.h"
- #include "grid.h"
- #include "sampling.h"
- #include "csg.h"
- #include "instance.h"
-
- #include "light.h"
- #include "point.h"
- #include "infinite.h"
- #include "spot.h"
- #include "jittered.h"
- #include "extended.h"
-
- #include "surface.h"
- #include "texture.h"
- #include "image.h"
-
- #include "blotch.h"
- #include "bump.h"
- #include "checker.h"
- #include "cloud.h"
- #include "fbm.h"
- #include "fbmbump.h"
- #include "gloss.h"
- #include "imagetext.h"
- #include "marble.h"
- #include "mount.h"
- #include "sky.h"
- #include "stripe.h"
- #include "windy.h"
- #include "wood.h"
-
- #include "rotate.h"
- #include "scale.h"
- #include "translate.h"
-
- #include "maceditor.h"
- #include "macdialog.h"
-
- extern void HandleMouseDown (EventRecord *theEvent);
- extern void HandleEvent(void);
- extern void DisplayView() ;
- extern void DrawCoordinateInfo(Float x, Float y, Float z) ;
-
- Geom *GetCurrentParent();
-
- char *ObjectName(short type) ;
- void UpdateEditor() ;
- void InsertObjectList(Geom *obj) ;
- void DisplayObjectList() ;
- void ConstructAndDisplayObjectList() ;
- void InitObjectList() ;
- void DestroyObjectList() ;
- ControlHandle SnatchHandle(DialogPtr thebox, short theGetItem) ;
- pascal void DrawCoordInfo(WindowPtr window, short item) ;
- pascal void DoWorldList(WindowPtr window, short item) ;
- pascal void DrawPopUps(WindowPtr window, short item) ;
- pascal void DrawTopView(WindowPtr window, short item) ;
- pascal void DrawSideView(WindowPtr window, short item) ;
- pascal void DrawFrontView(WindowPtr window, short item) ;
-
- extern GeomList *Defstack;
- extern Geom *World ;
- extern Vector crosshair, old_crosshair ;
- extern Float zoom ;
-
- /* Exportable variables */
-
- DialogPtr editorDialog = NULL;
- Geom *hiliteobject = NULL ;
- unsigned long FilterFlags = 0 ;
-
- /* Some private ones */
-
- static ListHandle editorList ;
- static struct ObjectStack *objectstack;
- static Geom *chosen_object = NULL;
-
- /*
- * Since any modifications we make to a scene might fall outside bounds we have to recompute
- * everything.
- */
- void ReComputeBounds(Geom *obj)
- {
- short loop ;
-
- if(obj) GeomBounds(obj,obj->bounds) ;
- for(loop = objectstack->numparents ; loop >= 0 ; loop--) {
- if(objectstack->parentlist[loop])
- GeomBounds(objectstack->parentlist[loop],objectstack->parentlist[loop]->bounds) ;
- }
- }
-
- void SetButtons()
- {
- /* if(chosen_object != NULL) {
- HiliteControl(SnatchHandle(editorDialog,ewdeleteBU),0) ;
- HiliteControl(SnatchHandle(editorDialog,ewmodifyBU),0) ;
- }
- else {
- HiliteControl(SnatchHandle(editorDialog,ewdeleteBU),255) ;
- HiliteControl(SnatchHandle(editorDialog,ewmodifyBU),255) ;
- } */
- }
-
-
- /* Initialise all user items, so that the correct procedures are called */
- void OpenEditorDialog()
- {
- short itemType, loop ;
- Handle itemHandle ;
- Rect itemRect ;
- GeomRef ref ;
-
- if(editorDialog) return ; /* Something wierd has happened */
-
- objectstack = malloc((size_t)sizeof(struct ObjectStack));
- objectstack->parentlist[0] = World ;
- objectstack->numparents = 0 ;
- crosshair.x = crosshair.y = crosshair.z = 0 ;
-
- editorDialog = GetNewDialog(editordialogR, 0L, (WindowPtr)-1);
- SetPort(editorDialog) ;
- ShowWindow(editorDialog);
-
- GetDItem(editorDialog, worldlistUI, &itemType, &itemHandle, &itemRect);
- SetDItem(editorDialog, worldlistUI, itemType, (ProcPtr) DoWorldList, &itemRect);
- InitObjectList() ;
- GetDItem(editorDialog, topUI, &itemType, &itemHandle, &itemRect);
- SetDItem(editorDialog, topUI, itemType, (ProcPtr) DrawTopView, &itemRect);
- GetDItem(editorDialog, sideUI, &itemType, &itemHandle, &itemRect);
- SetDItem(editorDialog, sideUI, itemType, (ProcPtr) DrawSideView, &itemRect);
- GetDItem(editorDialog, frontUI, &itemType, &itemHandle, &itemRect);
- SetDItem(editorDialog, frontUI, itemType, (ProcPtr) DrawFrontView, &itemRect);
- GetDItem(editorDialog, coordinfoUI, &itemType, &itemHandle, &itemRect);
- SetDItem(editorDialog, coordinfoUI, itemType, (ProcPtr) DrawCoordInfo, &itemRect);
- InvalRect(&editorDialog->portRect) ;
- SetButtons();
- chosen_object = NULL;
- }
-
- void CloseEditorDialog()
- {
- if(editorDialog) {
- free(objectstack);
- DestroyObjectList() ;
- DisposDialog(editorDialog) ;
- editorDialog = NULL ;
- chosen_object = NULL ;
- }
- }
-
- void HandleEditorEvent(EventRecord *theEvent)
- {
- short theItem, itype ;
- DialogPtr theDialog ;
- ControlHandle itemHandle ;
- Rect itemRect ;
- GrafPtr currPort ;
- Point mousePoint ;
- Boolean doubleClick ;
- short ox,oy ;
- Cell theCell, myCell ;
-
- SetPort(editorDialog) ;
- if(DialogSelect(theEvent, &theDialog, &theItem)) {
- switch(theItem) {
- case worldlistUI:
- GetMouse(&mousePoint);
- doubleClick = LClick(mousePoint,theEvent->modifiers,editorList) ;
- myCell.v = myCell.h = 0;
- if(LGetSelect(TRUE, &myCell, editorList)) {
- chosen_object = objectstack->objectlist[theCell.v] ;
- }
- else {
- chosen_object = NULL;
- }
- if(doubleClick) {
- theCell = LLastClick(editorList) ;
- chosen_object = objectstack->objectlist[theCell.v] ;
- if(chosen_object && IsAggregate(chosen_object)) {
- DestroyObjectList() ;
- objectstack->numparents++ ;
- objectstack->parentlist[objectstack->numparents] = chosen_object ;
- InitObjectList() ;
- chosen_object = NULL;
- }
- else {
- doObjectModify(chosen_object) ;
- ReComputeBounds(chosen_object) ;
- }
- }
- SetButtons();
- break ;
- case backBU:
- DestroyObjectList() ;
- if(objectstack->numparents > 0) {
- chosen_object = NULL;
- objectstack->parentlist[objectstack->numparents] = NULL ;
- objectstack->numparents-- ;
- }
- InitObjectList() ;
- SetButtons();
- break ;
- case sphereselectIC:
- if(FILTER_SET(F_SPHERE))
- UNSET_FILTER(F_SPHERE) ;
- else
- SET_FILTER(F_SPHERE) ;
- InvalRect(&editorDialog->portRect) ;
- break ;
- case cylinderselectIC:
- if(FILTER_SET(F_CYLINDER))
- UNSET_FILTER(F_CYLINDER) ;
- else
- SET_FILTER(F_CYLINDER) ;
- InvalRect(&editorDialog->portRect) ;
- break ;
- case coneselectIC:
- if(FILTER_SET(F_CONE))
- UNSET_FILTER(F_CONE) ;
- else
- SET_FILTER(F_CONE) ;
- InvalRect(&editorDialog->portRect) ;
- break ;
- case triangleselectIC:
- if(FILTER_SET(F_TRIANGLE))
- UNSET_FILTER(F_TRIANGLE) ;
- else
- SET_FILTER(F_TRIANGLE) ;
- InvalRect(&editorDialog->portRect) ;
- break ;
- case torusselectIC:
- if(FILTER_SET(F_TORUS))
- UNSET_FILTER(F_TORUS) ;
- else
- SET_FILTER(F_TORUS) ;
- InvalRect(&editorDialog->portRect) ;
- break ;
- case blobselectIC:
- if(FILTER_SET(F_BLOB))
- UNSET_FILTER(F_BLOB) ;
- else
- SET_FILTER(F_BLOB) ;
- InvalRect(&editorDialog->portRect) ;
- break ;
- case boxselectIC:
- if(FILTER_SET(F_BOX))
- UNSET_FILTER(F_BOX) ;
- else
- SET_FILTER(F_BOX) ;
- InvalRect(&editorDialog->portRect) ;
- break ;
- case discselectIC:
- if(FILTER_SET(F_DISC))
- UNSET_FILTER(F_DISC) ;
- else
- SET_FILTER(F_DISC) ;
- InvalRect(&editorDialog->portRect) ;
- break ;
- case polyselectIC:
- if(FILTER_SET(F_POLYGON))
- UNSET_FILTER(F_POLYGON) ;
- else
- SET_FILTER(F_POLYGON) ;
- InvalRect(&editorDialog->portRect) ;
- break ;
- case csgselectIC:
- if(FILTER_SET(F_CSG))
- UNSET_FILTER(F_CSG) ;
- else
- SET_FILTER(F_CSG) ;
- InvalRect(&editorDialog->portRect) ;
- break ;
- case lightsselectIC:
- if(FILTER_SET(F_LIGHTS))
- UNSET_FILTER(F_LIGHTS) ;
- else
- SET_FILTER(F_LIGHTS) ;
- InvalRect(&editorDialog->portRect) ;
- break ;
- case cameraselectIC:
- if(FILTER_SET(F_CAMERA))
- UNSET_FILTER(F_CAMERA) ;
- else
- SET_FILTER(F_CAMERA) ;
- InvalRect(&editorDialog->portRect) ;
- break ;
- case topUI:
- GetMouse(&mousePoint) ;
- GetDItem(editorDialog,theItem,&itype,&itemHandle,&itemRect) ;
- ox = mousePoint.h - (itemRect.left+(itemRect.right-itemRect.left)/2) ;
- oy = (itemRect.top+(itemRect.bottom-itemRect.top)/2) - mousePoint.v;
- DrawCoordinateInfo((Float)ox/zoom,(Float)oy/zoom,crosshair.z) ;
- DrawCrossHair() ;
- break ;
- case sideUI:
- GetMouse(&mousePoint) ;
- GetDItem(editorDialog,theItem,&itype,&itemHandle,&itemRect) ;
- ox = mousePoint.h - (itemRect.left+(itemRect.right-itemRect.left)/2) ;
- oy = (itemRect.top+(itemRect.bottom-itemRect.top)/2) - mousePoint.v;
- DrawCoordinateInfo(crosshair.x,(Float)ox/zoom,(Float)oy/zoom) ;
- DrawCrossHair() ;
- break ;
- case frontUI:
- GetMouse(&mousePoint) ;
- GetDItem(editorDialog,theItem,&itype,&itemHandle,&itemRect) ;
- ox = mousePoint.h - (itemRect.left+(itemRect.right-itemRect.left)/2) ;
- oy = (itemRect.top+(itemRect.bottom-itemRect.top)/2) - mousePoint.v;
- DrawCoordinateInfo((Float)ox/zoom,crosshair.y,(Float)oy/zoom) ;
- DrawCrossHair() ;
- break ;
- case zoominBU:
- zoom /= 2.;
- crosshair = old_crosshair;
- InvalRect(&editorDialog->portRect) ;
- break ;
- case zoomoutBU:
- zoom *= 2.;
- crosshair = old_crosshair;
- InvalRect(&editorDialog->portRect) ;
- break ;
- case ewdeleteBU:
- theCell = LLastClick(editorList) ;
- myCell.v = myCell.h = 0;
- if(LGetSelect(TRUE, &myCell, editorList)) {
- chosen_object = objectstack->objectlist[theCell.v] ;
- }
- else
- chosen_object = NULL;
- if(chosen_object && UnlinkObject(chosen_object,GetCurrentParent())) {
- DeleteObject(chosen_object);
- DestroyObjectList() ;
- InitObjectList() ;
- chosen_object = NULL;
- SetPort(editorDialog);
- InvalRect(&editorDialog->portRect);
- }
- SetButtons();
- break;
- case ewmodifyBU:
- theCell = LLastClick(editorList) ;
- myCell.v = myCell.h = 0;
- if(LGetSelect(TRUE, &myCell, editorList)) {
- chosen_object = objectstack->objectlist[theCell.v] ;
- }
- else
- chosen_object = NULL;
- SetButtons();
- if(chosen_object) {
- doObjectModify(chosen_object) ;
- ReComputeBounds(chosen_object) ;
- }
- break ;
- case ewcreateBU:
- CreateNewObject() ;
- DestroyObjectList() ;
- InitObjectList() ;
- chosen_object = NULL;
- SetButtons();
- break ;
- case ewlightsBU:
- EditLights();
- break;
- }
- }
- else {
- switch(theEvent->what) {
- case mouseDown:
- HandleMouseDown(theEvent);
- break ;
- case updateEvt:
- UpdateWind() ;
- UpdateEditor() ;
- break ;
- case keyDown:
- case autoKey:
- if ((theEvent->modifiers & cmdKey) != 0) {
- AdjustMenus();
- HandleMenu(MenuKey((char) (theEvent->message & charCodeMask)));
- }
- break;
- }
- }
- }
-
- void DestroyObjectList()
- {
- LDispose(editorList) ;
- }
-
- void InitObjectList()
- {
- short itemType ;
- Handle itemHandle ;
- Rect itemRect, bounds ;
- Point cellSize = {0,0} ;
-
- SetRect(&bounds,0,0,1,0) ;
- GetDItem(editorDialog, worldlistUI, &itemType, &itemHandle, &itemRect);
- itemRect.right -= 15 ;
- editorList = LNew(&itemRect,&bounds,cellSize,0,editorDialog,TRUE,FALSE,FALSE,TRUE) ;
- ConstructAndDisplayObjectList() ;
- }
-
-
- pascal void DrawCoordInfo(WindowPtr window, short item)
- {
- DrawCoordinateInfo(crosshair.x,crosshair.y,crosshair.z) ;
- }
-
-
- pascal void DoWorldList(WindowPtr window, short item)
- {
- GrafPtr currPort ;
- PenState saveState;
- short itemType;
- Handle itemHandle;
- Rect itemBox;
-
- GetPort(&currPort) ;
- SetPort(editorDialog) ;
- GetPenState(&saveState);
- GetDItem(editorDialog, worldlistUI, &itemType, &itemHandle, &itemBox);
- PenNormal() ;
- InsetRect(&itemBox,-1,-1) ;
- FrameRect(&itemBox);
- LUpdate(editorDialog->visRgn, editorList) ;
- SetPenState(&saveState);
- SetPort(currPort) ;
- }
-
- /*
- * Filter out the object type INSTANCE
- */
- Geom *FilterInstances(Geom *obj)
- {
- Instance *inst ;
-
- if(ObjectType(obj) == INSTANCE) {
- inst = obj->obj ;
- return inst->obj ;
- }
- return obj ;
- }
-
- void ConstructAndDisplayObjectList()
- {
- Geom *listItem, *parent;
- List *listHead ;
- Csg *csgHead ;
- Instance *instance ;
- Grid *grid ;
-
- /* Note that the object list must be constructed from an aggregate object type ie
- * list,csg, grid. Instances should be caught by the filter routine, but I will check for
- * them here anyway.
- */
-
- HiliteControl(SnatchHandle(editorDialog,backBU),(objectstack->numparents)?0:255) ;
- objectstack->numobjects = 0 ;
- parent = objectstack->parentlist[objectstack->numparents] ;
- switch(ObjectType(parent)) {
- case LIST:
- listHead = parent->obj ;
- for(listItem = listHead->list ; listItem ; listItem = listItem->next) {
- objectstack->objectlist[objectstack->numobjects] = FilterInstances(listItem) ;
- objectstack->numobjects++ ;
- }
- for(listItem = listHead->unbounded ; listItem ; listItem = listItem->next) {
- objectstack->objectlist[objectstack->numobjects] = FilterInstances(listItem) ;
- objectstack->numobjects++ ;
- }
- break ;
- case CSG:
- csgHead = parent->obj ;
- objectstack->objectlist[0] = FilterInstances(csgHead->obj1) ;
- objectstack->objectlist[1] = FilterInstances(csgHead->obj2) ;
- objectstack->numobjects = 2 ;
- break ;
- case GRID:
- grid = parent->obj ;
- for(listItem = grid->unbounded ; listItem ; listItem = listItem->next) {
- objectstack->objectlist[objectstack->numobjects] = FilterInstances(listItem) ;
- objectstack->numobjects++ ;
- }
- for(listItem = grid->objects ; listItem ; listItem = listItem->next) {
- objectstack->objectlist[objectstack->numobjects] = FilterInstances(listItem) ;
- objectstack->numobjects++ ;
- }
- break ;
- case INSTANCE:
- instance = parent->obj ;
- objectstack->numobjects = 1 ;
- objectstack->objectlist[0] = FilterInstances(instance->obj) ;
- break ;
- }
- DisplayObjectList() ;
- }
-
- void DisplayObjectList()
- {
- GrafPtr currPort ;
- PenState saveState;
- short itemType;
- Handle itemHandle;
- Rect itemBox;
- WindowPtr temp ;
- int loop ;
-
- /* Want to draw a box around the list of items */
-
- GetPort(&currPort) ;
- SetPort(editorDialog) ;
- GetPenState(&saveState);
- PenNormal() ;
- GetDItem(editorDialog, worldlistUI, &itemType, &itemHandle, &itemBox);
- itemBox.right -= 15 ;
- EraseRect(&itemBox) ;
- itemBox.right += 15 ;
- InsetRect(&itemBox,-1,-1) ;
- FrameRect(&itemBox);
- LDoDraw(FALSE, editorList) ;
- for(loop = objectstack->numobjects-1 ; loop >=0 ; loop--) {
- InsertObjectList(objectstack->objectlist[loop]) ;
- }
- LDoDraw(TRUE,editorList) ;
- LUpdate(editorDialog->visRgn,editorList) ;
- SetPenState(&saveState);
- SetPort(currPort) ;
- }
-
- void InsertObjectList(Geom *obj)
- {
- char name[255] ;
- Cell cell = {0,0};
- short loop ;
-
- if(obj->name)
- sprintf(name,"%s(%s)%s", obj->name,obj->methods->name(),IsAggregate(obj)?"->":"") ;
- else
- sprintf(name,"%s%s",obj->methods->name(),IsAggregate(obj)?"->":"") ;
- for(loop = strlen(name) ; loop<255 ; loop++) name[loop] = ' ';
- LAddRow(1,cell.v,editorList) ;
- LSetCell(name, 255, cell, editorList);
- }
-
- /* Identify each light type by its intensity procedure */
- int LightType(Light *l)
- {
- extern LightMethods
- *iExtendedMethods, *iInfMethods, *iJitteredMethods, *iPointMethods,
- *iSpotMethods ;
-
- if((l == NULL) || (l->methods==NULL))
- return -1 ;
- else if(l->methods == iPointMethods)
- return L_POINT ;
- else if(l->methods == iInfMethods)
- return L_INFINITE ;
- else if(l->methods == iSpotMethods)
- return L_SPOT ;
- else if(l->methods == iJitteredMethods)
- return L_JITTERED ;
- else if(l->methods == iExtendedMethods)
- return L_EXTENDED ;
- else
- return -1 ;
- }
-
- /* Identify each texture type by the Application prodecure it uses */
- int TextureType(Texture *t)
- {
- if(t == NULL)
- return -1 ;
- else if(t->method == BlotchApply)
- return T_BLOTCH ;
- else if(t->method == BumpApply)
- return T_BUMP ;
- else if(t->method == CheckerApply)
- return T_CHECKER ;
- else if(t->method == CloudTextApply)
- return T_CLOUD ;
- else if(t->method == FBmApply)
- return T_FBM ;
- else if(t->method == FBmBumpApply)
- return T_FBMBUMP ;
- else if(t->method == GlossApply)
- return T_GLOSS ;
- else if(t->method == ImageTextApply)
- return T_IMAGE ;
- else if(t->method == MarbleApply)
- return T_MARBLE ;
- else if(t->method == SkyApply)
- return T_SKY ;
- else if(t->method == StripeApply)
- return T_STRIPE ;
- else if(t->method == WoodApply)
- return T_WOOD ;
- else
- return -1 ;
- }
-
-
- int TransformType(Trans *t)
- {
- extern TransMethods *iRotateMethods, *iScaleMethods, *iTranslateMethods ;
-
- if(t == NULL)
- return -1 ;
- else if(t->methods == iScaleMethods)
- return TR_SCALE ;
- else if(t->methods == iTranslateMethods)
- return TR_TRANSLATE ;
- else if(t->methods == iRotateMethods)
- return TR_ROTATE ;
- else
- return -1 ;
- }
-
- /* Identify each primitive/aggregate from its associated methods procedure */
- int ObjectType(Geom *o)
- {
- extern Methods *iBlobMethods, *iBoxMethods, *iConeMethods, *iCsgMethods,
- *iCylinderMethods, *iDiscMethods, *iGridMethods , *iHfMethods,
- *iListMethods,*iInstanceMethods, *iPlaneMethods, *iPolygonMethods,
- *iSphereMethods, *iTorusMethods, *iTriangleMethods ;
-
- if(o == NULL)
- return -1 ;
- else if(o->methods == iPlaneMethods)
- return PLANE ;
- else if(o->methods == iSphereMethods)
- return SPHERE ;
- else if(o->methods == iConeMethods)
- return CONE ;
- else if(o->methods == iBoxMethods)
- return BOX ;
- else if(o->methods == iTorusMethods)
- return TORUS ;
- else if(o->methods == iBlobMethods)
- return BLOB ;
- else if(o->methods == iDiscMethods)
- return DISC ;
- else if(o->methods == iTriangleMethods)
- return TRIANGLE ;
- else if(o->methods == iPolygonMethods)
- return POLYGON ;
- else if(o->methods == iHfMethods)
- return HF ;
- else if(o->methods == iCylinderMethods)
- return CYLINDER ;
- else if(o->methods == iCsgMethods)
- return CSG ;
- else if(o->methods == iListMethods)
- return LIST ;
- else if(o->methods == iGridMethods)
- return GRID ;
- else if(o->methods == iInstanceMethods)
- return INSTANCE ;
- else
- return -1 ;
- }
-
- Geom *GetCurrentParent()
- {
- return objectstack->parentlist[objectstack->numparents] ;
- }
-
- void dummy()
- {
- }
-
- void UpdateEditor()
- {
- GrafPtr currPort ;
- int invert ;
- short itype,loop ;
- ControlHandle itemHandle ;
- Rect itemRect ;
-
- if(editorDialog) {
- GetPort(&currPort) ;
- SetPort(editorDialog);
- BeginUpdate(editorDialog);
- UpdtDialog(editorDialog,editorDialog->visRgn);
- EndUpdate(editorDialog);
- for(loop = sphereselectIC ; loop <= cameraselectIC; loop++) {
- invert = 0 ;
- switch(loop) {
- case sphereselectIC: invert = FILTER_SET(F_SPHERE); break ;
- case cylinderselectIC: invert = FILTER_SET(F_CYLINDER); break ;
- case coneselectIC: invert = FILTER_SET(F_CONE); break ;
- case triangleselectIC: invert = FILTER_SET(F_TRIANGLE); break ;
- case torusselectIC: invert = FILTER_SET(F_TORUS); break ;
- case blobselectIC: invert = FILTER_SET(F_BLOB); break ;
- case boxselectIC: invert = FILTER_SET(F_BOX); break ;
- case discselectIC: invert = FILTER_SET(F_DISC); break ;
- case polyselectIC: invert = FILTER_SET(F_POLYGON); break ;
- case csgselectIC: invert = FILTER_SET(F_CSG); break ;
- case lightsselectIC: invert = FILTER_SET(F_LIGHTS); break ;
- case cameraselectIC: invert = FILTER_SET(F_CAMERA); break ;
- }
- if(invert) {
- GetDItem(editorDialog,loop,&itype,&itemHandle,&itemRect) ;
- InvertRect(&itemRect) ;
- }
- }
- SetPort(currPort);
- }
- }
-