home *** CD-ROM | disk | FTP | other *** search
- /*---------------------------------------------------------------------------
- PatchTool
- ---------
-
- Patch tool class
-
- (C) Silicon Dream Ltd 1994
-
- ---------------------------------------------------------------------------
-
- Changes: Date:
- * Created file 27/12/94
- */
-
- #include <tool.h>
-
- #include "resource.h"
-
- #define POINT_BUFFER_SIZE 40
-
- class PatchTool: public Tool
- {
- private:
- Vec avecPoint[POINT_BUFFER_SIZE];
- ushort usNumPoints;
- char szErrTitle[20];
- HanObject hobj;
-
- Redraw _cppdyn OnSelect(Vec &vec);
- Redraw _cppdyn OnSet(Vec &vec);
- Redraw _cppdyn OnUndo();
- Redraw _cppdyn OnDo(Vec &vec);
- void _cppdyn DrawSoFar(HDC hdc, HanCoorSys hcsys, void *pvViewData, bool bInvalid);
-
- public:
- _cppdyn ~PatchTool();
- };
-
- IMPLEMENT_OBJECT(PatchTool)
-
- //---------------------------------------------------------------------------
- // Destructor
-
- _cppdyn PatchTool::~PatchTool()
- {
- }
-
- //---------------------------------------------------------------------------
- // OnSelect - Called when the tool is selected from the control bar's
- // button box
-
- Redraw _cppdyn PatchTool::OnSelect(Vec &vec)
- {
- /* Initially no points */
-
- usNumPoints=0;
-
- /* Write message (resource with id IDS_ONSELECT) in status bar */
-
- WriteMessage(IDS_ONSELECT);
- return REDRAW_NONE;
- }
-
- //---------------------------------------------------------------------------
- // OnSet - Called when the control bar's 'Set' button is pressed
-
- Redraw _cppdyn PatchTool::OnSet(Vec &vec)
- {
- char szName[MAX_NAME_BUFF_LEN];
-
- /* Find currently selected object */
-
- if (usNumPoints==0)
- {
- GetSelectedObjects(szName, MAX_NAME_BUFF_LEN);
- if ((hobj=GetObjectHandle(szName))==NULL_HANDLE)
- {
- MessageBox(IDS_ERR_TITLE, IDS_NO_OBJECT_SELECTED, MB_ICONEXCLAMATION);
- return REDRAW_NONE;
- }
- }
-
- /* Do not allow user to create more points than buffer will hold */
-
- if (usNumPoints==POINT_BUFFER_SIZE)
- {
- MessageBox(IDS_ERR_TITLE, IDS_TOO_MANY_POINTS, MB_ICONEXCLAMATION);
- return REDRAW_NONE;
- }
-
- /* Add point to list */
-
- avecPoint[usNumPoints++]=vec;
- return REDRAW_TOOL;
- }
-
- //---------------------------------------------------------------------------
- // OnUndo - Called when 'Undo' is selected from the 'Edit' menu and
- // this tool is in the middle of constructing, or if it performed
- // the last editing action
-
- Redraw _cppdyn PatchTool::OnUndo()
- {
- ushort usNumPats, us;
- HanObject hobj;
- HanPatch ahpat[POINT_BUFFER_SIZE-1];
-
- /* If we are in the middle of constructing a patch, remove the last
- point */
-
- if (usNumPoints>0)
- {
- usNumPoints--;
- return REDRAW_TOOL;
- }
-
- /* Undo the effect of the entire session */
-
- // GetUndo(&hobj, sizeof(HanObject));
- // GetUndo(&usNumPats, sizeof(ushort));
- // GetUndo(ahpat, usNumPats*sizeof(HanPatch));
- // for (us=usNumPats; us--;)
- // DelPatch(hobj, ahpat[us]);
-
- return REDRAW_ALL;
- }
-
- //---------------------------------------------------------------------------
- // OnDo - Called when the control bar's 'Do' button is pressed
-
- Redraw _cppdyn PatchTool::OnDo(Vec &vec)
- {
- HanVertex ahver[POINT_BUFFER_SIZE];
- HanPatch ahpat[POINT_BUFFER_SIZE-1];
- ushort us, usNumPats;
- GeomErr gerr;
- HanSurf hsur;
-
- /* Check to see if a surface is selected */
-
- hsur=GetCurrentSurf();
- if (hsur==NULL_HANDLE)
- {
- MessageBox(IDS_ERR_TITLE, IDS_ERR_NO_SURF);
- return REDRAW_TOOL;
- }
-
- /* Create a Geometry vertex at each point */
-
- for (us=0; us<usNumPoints; us++)
- {
- gerr=::AddVertex(hobj, avecPoint[us], ahver+us);
- if (gerr!=GERR_OK)
- {
- /* Many errors can occur during patch creation. Rather than worrying
- about creating our own error messages for each case, let Geometry
- build the error string and stick it in a message box */
-
- usNumPoints=0;
- MessageBox(IDS_ERR_TITLE, gerr);
- return REDRAW_TOOL;
- }
- }
-
- /* Define patch on Genesis, using info from current settings */
-
- usNumPats=40;
- gerr=::DefPatch(hobj,
- usNumPoints,
- 0,
- 0.0,
- ahver,
- NULL,
- hsur,
- &usNumPats,
- ahpat);
- if (gerr!=GERR_OK)
- {
- /* Remove verticies */
-
- for (us=usNumPoints; us--;)
- DelVertex(hobj, ahver[us]);
-
- /* Get description of error from Geometry, and put it in
- a message box */
-
- MessageBox(IDS_ERR_TITLE, gerr);
- }
- usNumPoints=0;
-
- /* All we need to 'undo' is the patch handle(s), as deleting a patch will also
- delete its verticies if no other patch is using them. NB. There could be
- more than one if Geometry had to split up the patch drawn */
-
- // SaveUndo(&hobj, sizeof(HanObject));
- // SaveUndo(&usNumPats, sizeof(ushort));
- // SaveUndo(ahpat, sizeof(HanPatch)*usNumPats);
-
- return REDRAW_ALL;
- }
-
- //---------------------------------------------------------------------------
- // DrawSoFar - Called when the view gets repainted and we're in the middle of
- // constructing, so we can draw what we have so far
-
- void _cppdyn PatchTool::DrawSoFar(HDC hdc, HanCoorSys hcsys, void *pvViewData, bool bInvalid)
- {
- HPEN hpen, hpenOld;
- float fStartX, fStartY, fEndX, fEndY;
- ushort us;
- int iOldMode;
-
- if (usNumPoints==0)
- return;
-
- /* Create solid and dotted green pens with which to draw the patch */
-
- hpen=CreatePen(PS_DOT, 1, RGB(0, 200, 0));
- hpenOld=(HPEN) SelectObject(hdc, (HGDIOBJ) hpen);
- iOldMode=SetBkMode(hdc, TRANSPARENT);
-
- /* Draw each edge of the patch so far */
-
- for (us=1; us<usNumPoints; us++)
- {
- Get3DLine(hcsysOnSelect, avecPoint[us], avecPoint[us-1],
- &fStartX, &fStartY, &fEndX, &fEndY);
- MoveTo(hdc, (int) fStartX, (int) fStartY);
- LineTo(hdc, (int) fEndX, (int) fEndY);
- }
-
- /* Draw endpoint marker */
-
- if (Get3DPoint(hcsysOnSelect, avecPoint[usNumPoints-1],
- &fStartX, &fStartY)==GERR_OK)
- DrawMarker(hdc, (int) fStartX, (int) fStartY, MT_BOX);
-
- /* We must restore the state of the DC before returning */
-
- SetBkMode(hdc, iOldMode);
- SelectObject(hdc, hpenOld);
- DeleteObject(hpen);
- return;
- }
-