home *** CD-ROM | disk | FTP | other *** search
/ Dream 41 / Amiga_Dream_41.iso / Amiga / Pro / 3d / ICoons1_0.lzh / icoons / source / move.c < prev    next >
C/C++ Source or Header  |  1992-10-12  |  13KB  |  446 lines

  1. /* :ts=8 */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include <math.h>
  7.  
  8. #include "general.h"
  9. #include "globals.h"
  10. #include "intui.h"
  11. #include "timer.h"
  12. #include "spl_math.h"
  13. #include "spl_util.h"
  14. #include "spl_gfx.h"
  15. #include "move.h"
  16.  
  17. /* Get Stringinfo buffer for the Move gadget with id 'G':    */
  18. #define Get_Gadget_String(G) (((struct StringInfo *) \
  19.                 (Move_GroupGadgets[G]->SpecialInfo))->Buffer)
  20.  
  21.     /* Set string to 'S' in string gadget 'G':    */
  22. #define Set_Gadget_String(G, S) \
  23.         GT_SetGadgetAttrs(Move_GroupGadgets[G], \
  24.             Windows[W_Move_Group].Window, NULL, \
  25.             GTST_String, S)
  26.  
  27. static Boolean_T    Move_Group_Active;
  28. static short        Old_X;
  29. static short        Old_Y;
  30. static Vector_T        Orig_Pos;
  31. static Vector_T        Box_Min, Box_Max;
  32.  
  33. void Set_Move_Group_Position(Vector_T Pos)
  34. /************************************************************************/
  35. /*                                                                      */
  36. /* Set move position gadgets to 'Pos'.                    */
  37. /*                                                                      */
  38. /************************************************************************/
  39. {
  40.     char Buffer[Buffer_Length+1];
  41.  
  42.     if (Windows[W_Move_Group].Window == NULL) return;
  43.  
  44.     sprintf(Buffer, " %.2lf", Pos[0]);
  45.     Set_Gadget_String(GDX_Move_Group_X_Value, Buffer);
  46.     sprintf(Buffer, " %.2lf", Pos[1]);
  47.     Set_Gadget_String(GDX_Move_Group_Y_Value, Buffer);
  48.     sprintf(Buffer, " %.2lf", Pos[2]);
  49.     Set_Gadget_String(GDX_Move_Group_Z_Value, Buffer);
  50.  
  51. } /* Set_Move_Group_Position */
  52.  
  53. static
  54. void Move_Timeout()
  55. /************************************************************************/
  56. /*                                                                      */
  57. /* Function called when timer expires: Draw all splines.        */
  58. /*                                                                      */
  59. /************************************************************************/
  60. {    
  61.     Vector_T    Distance;
  62.  
  63.     if (Group_Mode) {
  64.  
  65.         Distance[0] = Current_Pos[0] - Orig_Pos[0];
  66.         Distance[1] = Current_Pos[1] - Orig_Pos[1];
  67.         Distance[2] = Current_Pos[2] - Orig_Pos[2];
  68.  
  69.         Points_Move(Distance);
  70.     }
  71.     Compute_Splines();
  72.     Clear_All(What_All);
  73.     Draw_All(What_All);
  74.  
  75.     Redraw_Mask = 0;
  76.  
  77.     Stop_Timer();
  78.  
  79. } /* Move_Timeout */
  80.  
  81. static 
  82. void Move_Redraw(long Mask)
  83. /************************************************************************/
  84. /*                                                                      */
  85. /* Function called to redraw screen while moving points.        */
  86. /*                                                                      */
  87. /************************************************************************/
  88. {    
  89.     Vector_T B_Min, B_Max;
  90.  
  91.     if (Select_Point_Id >= 0) {
  92.  
  93.         Set_Move_Group_Position(Points[Select_Point_Id].Pos);
  94.     KnotInfo_Show_Pos(Points[Select_Point_Id].Pos);
  95.  
  96.     } /* if */
  97.  
  98.     if (Group_Mode) {
  99.  
  100.         B_Min[0] = Box_Min[0] + Current_Pos[0] - Orig_Pos[0];
  101.         B_Min[1] = Box_Min[1] + Current_Pos[1] - Orig_Pos[1];
  102.         B_Min[2] = Box_Min[2] + Current_Pos[2] - Orig_Pos[2];
  103.         B_Max[0] = Box_Max[0] + Current_Pos[0] - Orig_Pos[0];
  104.         B_Max[1] = Box_Max[1] + Current_Pos[1] - Orig_Pos[1];
  105.         B_Max[2] = Box_Max[2] + Current_Pos[2] - Orig_Pos[2];
  106.  
  107.         Clear_Plane(3, What_All);
  108.         Draw_Box(B_Min, B_Max, NULL, DM_Plane, What_All);
  109.  
  110.         Start_Timer(Delay_Draw_Seconds, Delay_Draw_Micros);
  111.  
  112.     } else {
  113.     
  114.     Point_Move(Select_Point_Id, Current_Pos);
  115.  
  116.     Compute_Marked_Segments();
  117.     Clear_Plane(3, What_All);
  118.     Draw_Marked_Segments(DM_Plane, What_All);
  119.  
  120.     } /* if .. else .. */
  121.  
  122.     Redraw_Mask = 0;
  123.  
  124. } /* Move_Redraw */
  125.  
  126. static
  127. void Move_Select_Up(short X, short Y)
  128. /************************************************************************/
  129. /*                                                                      */
  130. /* X, Y are the actual coordinates in the active window.        */
  131. /*                                                                      */
  132. /************************************************************************/
  133. {
  134.     if (Group_Mode) {
  135.  
  136.     /* If the timer hasn't expired, then call the timeout handler to*/
  137.     /* compute and draw the splines.                 */
  138.  
  139.         if (!Check_Timer()) Move_Timeout();
  140.  
  141.     } else {
  142.  
  143.     if (Grid_Snap_Active) {
  144.  
  145.         Current_Pos[0] = Points[Select_Point_Id].Pos[0];
  146.         Current_Pos[1] = Points[Select_Point_Id].Pos[1];
  147.         Current_Pos[2] = Points[Select_Point_Id].Pos[2];
  148.  
  149.         Snap_To_Grid(Current_Pos);
  150.  
  151.  
  152.     } /* if */
  153.     Point_Move(Select_Point_Id, Current_Pos);
  154.  
  155.     Compute_Marked_Segments();
  156.  
  157.     Clear_Plane(3, What_All);
  158.  
  159.     Draw_Marked_Segments(DM_Normal, What_All);
  160.     Draw_Knots(DM_Normal, What_All);
  161.  
  162.     Set_RMBTrap(FALSE);
  163.  
  164.     } /* if .. else .. */
  165.  
  166.     Set_Mode_Normal();
  167.  
  168. } /* Move_Select_Up */
  169.  
  170. static 
  171. void Move_Select_Down(short X, short Y)
  172. /************************************************************************/
  173. /*                                                                      */
  174. /* X, Y are the actual coordinates in the active window.        */
  175. /*                                                                      */
  176. /************************************************************************/
  177. {
  178.     Select_View_Id = Screen_To_World(X, Y, Current_Pos);
  179.     if (Select_View_Id < 0) return;
  180.  
  181.     if (Get_Select_Bounding_Box(Box_Min, Box_Max)) {
  182.     Display_Message("No points selected");
  183.     return;
  184.     }
  185.     Draw_Box(Box_Min, Box_Max, NULL, DM_Plane, What_All);
  186.  
  187.  
  188.     Orig_Pos[0] = Current_Pos[0];
  189.     Orig_Pos[1] = Current_Pos[1];
  190.     Orig_Pos[2] = Current_Pos[2];
  191.  
  192.     Old_X = X;
  193.     Old_Y = Y;
  194.  
  195.     Move_Group_Active = TRUE;
  196.  
  197. } /* Move_Select_Down */
  198.  
  199. static 
  200. void  Move_Move(short X, short Y)
  201. /************************************************************************/
  202. /*                                                                      */
  203. /* Function called when mouse is moved to move points.            */
  204. /* X, Y are the actual coordinates in the active window.        */
  205. /*                                                                      */
  206. /************************************************************************/
  207. {
  208.     short View_Id;
  209.  
  210.     if (Group_Mode && !Move_Group_Active) return;
  211.  
  212.     View_Id = Screen_To_World(X, Y, Current_Pos);
  213.  
  214.     if (View_Id != Select_View_Id) return;
  215.  
  216.     if (Old_X == X && Old_Y == Y) return;
  217.  
  218.     Old_X = X;
  219.     Old_Y = Y;
  220.  
  221.     if (Redraw_Always) Move_Redraw(What_All);
  222.     else               Redraw_Mask = What_All;
  223.  
  224. } /* Move_Move */
  225.  
  226. static
  227. Boolean_T Move_Handle_Event(struct IntuiMessage *Msg)
  228. /************************************************************************/
  229. /*                                                                      */
  230. /* Event handler routine for the 'MOVE GROUP' mode.            */
  231. /* Events handled:                            */
  232. /*    Select down: Start move.                    */
  233. /*    Select up: Stop move.                        */
  234. /*    Mouse move: Change position.                    */
  235. /*                                                                      */
  236. /************************************************************************/
  237. {
  238.  
  239.     switch (Msg->Class) {
  240.  
  241.  
  242.     case IDCMP_MOUSEBUTTONS:
  243.         /* Msg->Code contain id of button pressed         */
  244.         /* Msg->MouseX and Msg->MouseY contain mouse position             */
  245.  
  246.     switch (Msg->Code) {
  247.  
  248.     case SELECTDOWN:
  249.         Move_Select_Down(Msg->MouseX, Msg->MouseY);
  250.         return(TRUE);
  251.  
  252.     case SELECTUP:
  253.         Move_Select_Up(Msg->MouseX, Msg->MouseY);
  254.         return(TRUE);
  255.  
  256.     case MENUDOWN:
  257.         if (!Group_Mode) Command_Connect(); /* May return to normal mode */
  258.     } /* switch (Msg->Code) */
  259.     break;
  260.  
  261.     case IDCMP_MOUSEMOVE:
  262.     Move_Move(Msg->MouseX, Msg->MouseY);
  263.     return(TRUE);
  264.  
  265.     case IDCMP_VANILLAKEY:
  266.     switch(Msg->Code) {
  267.  
  268.     case 'C':                    /* C    */
  269.         if (!Group_Mode) Command_Combine();
  270.         return(TRUE);
  271.  
  272.     } /* switch (Msg->Code) */
  273.     break;
  274.  
  275.     } /* switch (Msg->Class) */
  276.  
  277.     return(FALSE);
  278.  
  279. } /* Move_Handle_Event */
  280.  
  281. void Set_Mode_Move(short X, short Y)
  282. /************************************************************************/
  283. /*                                                                      */
  284. /* Start moving the currently selected point, or if GROUP MODE, then    */
  285. /* move all selected points.                        */
  286. /* X, Y is the current mouse position.                    */
  287. /*                                                                      */
  288. /************************************************************************/
  289. {
  290.     short    Point_Id;
  291.     short    Tmp1, Tmp2;
  292.     Vector_T    Current_Pos;
  293.  
  294.     if (Select_Point_Id < 0 && !Group_Mode) return;
  295.  
  296.     if (Select_Spline != NULL)
  297.     Set_Move_Group_Position(Points[Select_Point_Id].Pos);
  298.  
  299.     KnotInfo_Show_Info(Select_Spline, Select_Knot);
  300.     Set_Window_Title(NULL);
  301.     Display_Status(NULL);
  302.  
  303.     if (Group_Mode) {
  304.  
  305.         Move_Group_Active    = FALSE;
  306.         State.Handle_Event    = Move_Handle_Event;
  307.         State.Timeout        = Move_Timeout;
  308.         State.Redraw        = Move_Redraw;
  309.  
  310.         if (MQ_Size_Move_G > 0) {
  311.         SetMouseQueue(Windows[Id_Active_Window].Window, MQ_Size_Move_G); 
  312.         Redraw_Always = TRUE;
  313.         } else Redraw_Always = FALSE;
  314.  
  315.         Points_Save();
  316.  
  317.         sprintf(Error_Msg, "Move group");
  318.         Display_Status(Error_Msg);
  319.  
  320.     Move_Select_Down(X, Y);
  321.  
  322.     } else {
  323.  
  324.  
  325.     State.Handle_Event    = Move_Handle_Event;
  326.     State.Redraw        = Move_Redraw;
  327.  
  328.     Mark_Connected_Segments(Select_Point_Id);
  329.     
  330.     Draw_Marked_Segments(DM_Erase, What_All);
  331.     Draw_Marked_Segments(DM_Plane, What_All);
  332.     
  333.     Old_X = X; Old_Y = Y;
  334.  
  335.         if (MQ_Size_Move > 0) {
  336.         SetMouseQueue(Windows[Id_Active_Window].Window, MQ_Size_Move); 
  337.         Redraw_Always = TRUE;
  338.         } else Redraw_Always = FALSE;
  339.     
  340.         /* Disable menu and receive RMBTraps */
  341.     Set_RMBTrap(TRUE);
  342.  
  343.         sprintf(Error_Msg, "Move");
  344.         Display_Status(Error_Msg);
  345.  
  346.     } /* if .. else .. */
  347.  
  348. } /* Set_Mode_Move */
  349.  
  350. void Handle_Move_Group_Move()
  351. /************************************************************************/
  352. /*                                                                      */
  353. /* Change to MOVE GROUP mode.                        */
  354. /*                                                                      */
  355. /************************************************************************/
  356. {
  357.     if (Select_Point_Id < 0) return;
  358.  
  359.     Move_Group_Active        = FALSE;
  360.     State.Handle_Event        = Move_Handle_Event;
  361.     State.Timeout        = Move_Timeout;
  362.     State.Redraw        = Move_Redraw;
  363.  
  364.     sprintf(Error_Msg, "Move group");
  365.     Display_Status(Error_Msg);
  366.  
  367.     Points_Save();
  368.  
  369. } /* Handle_Move_Group_Move */
  370.  
  371. void Handle_Move_Group_X_Value()
  372. /************************************************************************/
  373. /*                                                                      */
  374. /* Handle a Move_Group_X_Value gadget event.                */
  375. /*                                                                      */
  376. /************************************************************************/
  377. {
  378.     Vector_T Distance;
  379.  
  380.     if (Windows[W_Move_Group].Window == NULL) return;
  381.     if (Select_Point_Id < 0) return;
  382.  
  383.     Points_Save();
  384.  
  385.  
  386.     Distance[0] = atof(Get_Gadget_String(GDX_Move_Group_X_Value)) -
  387.           Points[Select_Point_Id].Pos[0];
  388.     Distance[1] = 0.0;
  389.     Distance[2] = 0.0;
  390.  
  391.     Points_Move(Distance);
  392.  
  393.     Move_Timeout(); /* Compute and draw splines */
  394.  
  395. } /* Handle_Move_Group_X_Value */
  396.  
  397. void Handle_Move_Group_Y_Value()
  398. /************************************************************************/
  399. /*                                                                      */
  400. /* Handle a Move_Group_Y_Value gadget event.                */
  401. /*                                                                      */
  402. /************************************************************************/
  403. {
  404.     Vector_T    Distance;
  405.  
  406.     if (Windows[W_Move_Group].Window == NULL) return;
  407.     if (Select_Point_Id < 0) return;
  408.  
  409.     Points_Save();
  410.  
  411.     Distance[0] = 0.0;
  412.     Distance[1] = atof(Get_Gadget_String(GDX_Move_Group_Y_Value)) -
  413.           Points[Select_Point_Id].Pos[1];
  414.     Distance[2] = 0.0;
  415.  
  416.     Points_Move(Distance);
  417.  
  418.     Move_Timeout(); /* Compute and draw splines */
  419.  
  420. } /* Handle_Move_Group_Y_Value */
  421.  
  422. void Handle_Move_Group_Z_Value()
  423. /************************************************************************/
  424. /*                                                                      */
  425. /* Handle a Move_Group_Z_Value gadget event.                */
  426. /*                                                                      */
  427. /************************************************************************/
  428. {
  429.     Vector_T Distance;
  430.  
  431.     if (Windows[W_Move_Group].Window == NULL) return;
  432.     if (Select_Point_Id < 0) return;
  433.  
  434.     Points_Save();
  435.  
  436.     Distance[0] = 0.0;
  437.     Distance[1] = 0.0;
  438.     Distance[2] = atof(Get_Gadget_String(GDX_Move_Group_Z_Value)) -
  439.           Points[Select_Point_Id].Pos[2];
  440.  
  441.     Points_Move(Distance);
  442.  
  443.     Move_Timeout(); /* Compute and draw splines */
  444.  
  445. } /* Handle_Move_Group_Z_Value */
  446.