home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / icoons / source / knotinfo.c < prev    next >
C/C++ Source or Header  |  1992-10-09  |  15KB  |  498 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 "knotinfo.h"
  16.  
  17. /* Get Stringinfo buffer for the KnotInfo gadget with id 'G':    */
  18. #define Knot_Get_Gadget_String(G) (((struct StringInfo *) \
  19.                 (KnotInfoGadgets[G]->SpecialInfo))->Buffer)
  20.  
  21.     /* Set string to 'S' in string gadget 'G':    */
  22. #define Knot_Set_Gadget_String(G, S) \
  23.         GT_SetGadgetAttrs(KnotInfoGadgets[G], \
  24.             Windows[W_KnotInfo].Window, NULL, \
  25.             GTST_String, S)
  26.  
  27.     /* Set string to 'S' in TEXT gadget 'G':    */
  28. #define Knot_Set_Gadget_Text(G, S) \
  29.         GT_SetGadgetAttrs(KnotInfoGadgets[G], \
  30.             Windows[W_KnotInfo].Window, NULL, \
  31.             GTTX_Text, S)
  32.  
  33.     /* Set number to 'N' in number gadget 'G':    */
  34. #define Knot_Set_Gadget_Number(G, N) \
  35.         GT_SetGadgetAttrs(KnotInfoGadgets[G], \
  36.             Windows[W_KnotInfo].Window, NULL, \
  37.             GTNM_Number, (long) (N))
  38.  
  39. static char    Knot_Pos_X_Text[20];
  40. static char    Knot_Pos_Y_Text[20];
  41. static char    Knot_Pos_Z_Text[20];
  42.  
  43. static Boolean_T Modify_Active;
  44. static short      Modify_Mode = 0;    /* 0: tension, 1:bias, 2: cont.    */
  45. static short      Old_Modify_X;
  46. static double     Old_Value;
  47. static double     Current_Value;
  48.  
  49.  
  50. void KnotInfo_Show_Pos(Vector_T Pos)
  51. /************************************************************************/
  52. /*                                                                      */
  53. /*                                                                      */
  54. /************************************************************************/
  55. {
  56.     char Buffer[Buffer_Length+1];
  57.  
  58.     if (Windows[W_KnotInfo].Window == NULL) return;
  59.  
  60.     sprintf(Knot_Pos_X_Text, " %.2lf", Pos[0]);
  61.     Knot_Set_Gadget_Text(GDX_G_Knot_Pos_X, Knot_Pos_X_Text);
  62.  
  63.     sprintf(Knot_Pos_Y_Text, " %.2lf", Pos[1]);
  64.     Knot_Set_Gadget_Text(GDX_G_Knot_Pos_Y, Knot_Pos_Y_Text);
  65.  
  66.     sprintf(Knot_Pos_Z_Text, " %.2lf", Pos[2]);
  67.     Knot_Set_Gadget_Text(GDX_G_Knot_Pos_Z, Knot_Pos_Z_Text);
  68.  
  69.  
  70. } /* KnotInfo_Show_Pos */
  71.  
  72. void KnotInfo_Show_Spline_Info(double Tension, double Bias, double Continuity)
  73. /************************************************************************/
  74. /*                                                                      */
  75. /*                                                                      */
  76. /************************************************************************/
  77. {
  78.     char Buffer[Buffer_Length+1];
  79.     if (Windows[W_KnotInfo].Window == NULL) return;
  80.  
  81.     sprintf(Buffer, " %.2lf", Tension);
  82.     Knot_Set_Gadget_String(GDX_G_Tension_Value, Buffer);
  83.  
  84.     sprintf(Buffer, " %.2lf", Bias);
  85.     Knot_Set_Gadget_String(GDX_G_Bias_Value, Buffer);
  86.  
  87.     sprintf(Buffer, " %.2lf",  Continuity);
  88.     Knot_Set_Gadget_String(GDX_G_Continuity_Value, Buffer);
  89.  
  90. } /* KnotInfo_Show_Spline_Info */
  91.  
  92. void KnotInfo_Show_Info(Spline_T *Spline, Knot_T *Knot)
  93. /************************************************************************/
  94. /*                                                                      */
  95. /* Display spline/knot id and tension/bias/continuity info.        */
  96. /*                                                                      */
  97. /************************************************************************/
  98. {
  99.     double     Tension;
  100.     double     Bias;
  101.     double     Continuity;
  102.     short      Point_Id;
  103.     Vector_T    Pos;
  104.  
  105.     if (Windows[W_KnotInfo].Window == NULL) return;
  106.  
  107.     if (Knot != NULL) Point_Id = Knot->Point_Id;
  108.     else              Point_Id = -1;
  109.  
  110.     Knot_Set_Gadget_Number(GDX_G_Point_Id, Point_Id);
  111.  
  112.     if (Spline == NULL || Knot == NULL) {
  113.  
  114.         Tension    = 0.0;
  115.     Bias       = 0.0;
  116.     Continuity = 0.0;
  117.  
  118.     Pos[0] = Pos[1] = Pos[2] = 0.0;
  119.  
  120.     KnotInfo_Show_Pos(Pos);
  121.  
  122.     } else {
  123.  
  124.         Tension    = Knot->Tension;
  125.     Bias       = Knot->Bias;
  126.     Continuity = Knot->Continuity; 
  127.     Point_Id   = Knot->Point_Id;
  128.  
  129.     KnotInfo_Show_Pos(Points[Point_Id].Pos);
  130.  
  131.     } /* if .. else .. */
  132.  
  133.     KnotInfo_Show_Spline_Info(Tension, Bias, Continuity); 
  134.  
  135. } /* KnotInfo_Show_Info */
  136.  
  137. void Handle_G_Tension_Value()
  138. /************************************************************************/
  139. /*                                                                      */
  140. /*                                                                      */
  141. /************************************************************************/
  142. {
  143.     char Buffer[Buffer_Length+1];
  144.  
  145.     if (Windows[W_KnotInfo].Window == NULL) return;
  146.  
  147.     Current_Value = atof(Knot_Get_Gadget_String(GDX_G_Tension_Value));
  148.     sprintf(Buffer, " %.2lf",  Current_Value);
  149.     Current_Value = atof(Buffer);
  150.     Knot_Set_Gadget_String(GDX_G_Tension_Value, Buffer);
  151.  
  152.     if (Group_Mode) {
  153.  
  154.     Points_Set_Tension(Current_Value);
  155.     Compute_Splines();
  156.  
  157.     } else if (Select_Knot != NULL) {
  158.  
  159.     Select_Knot->Tension = Current_Value;
  160.     Compute_Marked_Segments();
  161.  
  162.     } /* if .. else .. */
  163.  
  164.     Redraw_Mask |= What_All;
  165.  
  166. } /* Handle_G_Tension_Value */
  167.  
  168. void Handle_G_Bias_Value()
  169. /************************************************************************/
  170. /*                                                                      */
  171. /*                                                                      */
  172. /************************************************************************/
  173. {
  174.     char Buffer[Buffer_Length+1];
  175.  
  176.     if (Windows[W_KnotInfo].Window == NULL) return;
  177.  
  178.     Current_Value = atof(Knot_Get_Gadget_String(GDX_G_Bias_Value));
  179.     sprintf(Buffer, " %.2lf",  Current_Value);
  180.     Current_Value = atof(Buffer);
  181.     Knot_Set_Gadget_String(GDX_G_Bias_Value, Buffer);
  182.  
  183.     if (Group_Mode) {
  184.  
  185.     Points_Set_Bias(Current_Value);
  186.     Compute_Splines();
  187.  
  188.     } else if (Select_Knot != NULL) {
  189.  
  190.     Select_Knot->Bias = Current_Value;
  191.     Compute_Marked_Segments();
  192.  
  193.     } /* if .. else .. */
  194.  
  195.     Redraw_Mask |= What_All;
  196.  
  197. } /* Handle_G_Bias_Value */
  198.  
  199. void Handle_G_Continuity_Value()
  200. /************************************************************************/
  201. /*                                                                      */
  202. /*                                                                      */
  203. /************************************************************************/
  204. {
  205.     char Buffer[Buffer_Length+1];
  206.  
  207.     if (Windows[W_KnotInfo].Window == NULL) return;
  208.  
  209.     Current_Value = atof(Knot_Get_Gadget_String(GDX_G_Continuity_Value));
  210.     sprintf(Buffer, " %.2lf",  Current_Value);
  211.     Current_Value = atof(Buffer);
  212.     Knot_Set_Gadget_String(GDX_G_Continuity_Value, Buffer);
  213.  
  214.  
  215.     if (Group_Mode) {
  216.  
  217.     Points_Set_Continuity(Current_Value);
  218.     Compute_Splines();
  219.  
  220.     } else if (Select_Knot != NULL) {
  221.  
  222.     Select_Knot->Continuity =  Current_Value;
  223.     Compute_Marked_Segments();
  224.  
  225.     } /* if .. else .. */
  226.  
  227.     Redraw_Mask |= What_All;
  228.  
  229. } /* Handle_G_Continuity_Value */
  230.  
  231. static
  232. void Modify_KnotInfo_Timeout()
  233. /************************************************************************/
  234. /*                                                                      */
  235. /* Function called when timer expires: Draw all splines.                */
  236. /*                                                                      */
  237. /************************************************************************/
  238. {
  239.     Compute_Splines();
  240.     Clear_All(What_All);
  241.     Draw_All(What_All);
  242.     Draw_Marked_Segments(DM_Erase, What_S);
  243.     Draw_Marked_Segments(DM_Plane, What_S);
  244.  
  245.     Stop_Timer();
  246.  
  247. } /* Modify_KnotInfo_Timeout */
  248.  
  249. static
  250. void Modify_KnotInfo_Redraw(long Mask)
  251. /************************************************************************/
  252. /*                                                                      */
  253. /* Function called to redraw the screen while changing spline parms.    */
  254. /*                                                                      */
  255. /************************************************************************/
  256. {
  257.     char    Buffer[Buffer_Length+1];
  258.  
  259.     switch (Modify_Mode) {
  260.  
  261.     case 0:    /* Tension */
  262.         sprintf(Buffer, " %.2lf",  Current_Value);
  263.     Knot_Set_Gadget_String(GDX_G_Tension_Value, Buffer);
  264.  
  265.     if (Group_Mode) {
  266.         Points_Set_Tension(Current_Value);
  267.     } else {
  268.         Select_Knot->Tension = Current_Value;
  269.      }
  270.     break;
  271.  
  272.     case 1:    /* Bias */
  273.         sprintf(Buffer, " %.2lf",  Current_Value);
  274.     Knot_Set_Gadget_String(GDX_G_Bias_Value, Buffer);
  275.     if (Group_Mode) {
  276.         Points_Set_Bias(Current_Value);
  277.     } else {
  278.         Select_Knot->Bias = Current_Value;
  279.     }
  280.     break;
  281.  
  282.     case 2:     /* Continuity */
  283.         sprintf(Buffer, " %.2lf",  Current_Value);
  284.     Knot_Set_Gadget_String(GDX_G_Continuity_Value, Buffer);
  285.     if (Group_Mode) {
  286.         Points_Set_Continuity(Current_Value);
  287.     } else {
  288.         Select_Knot->Continuity = Current_Value;
  289.     }
  290.     break;
  291.  
  292.     } /* switch */
  293.  
  294.     /* Only update selected knot until timeout, then draw all */
  295.  
  296.     Compute_Marked_Segments();
  297.     Clear_Plane(3, What_All);
  298.     Draw_Marked_Segments(DM_Plane, What_S);
  299.  
  300.     if (Group_Mode) Start_Timer(Delay_Draw_Seconds, Delay_Draw_Micros);
  301.  
  302.     Redraw_Mask = 0;
  303.  
  304. } /* Modify_KnotInfo_Redraw */
  305.  
  306. static
  307. void Modify_KnotInfo_Select_Up()
  308. /************************************************************************/
  309. /*                                                                      */
  310. /* Function called to redraw the screen while changing spline parms.    */
  311. /*                                                                      */
  312. /************************************************************************/
  313. {
  314.    
  315.     if (Group_Mode) {
  316.  
  317.     /* If the timer hasn't expired, we need to compute and redraw   */
  318.     /* all.                                */
  319.  
  320.     if (!Check_Timer()) {
  321.  
  322.             Compute_Splines();
  323.             Clear_All(What_All);
  324.             Draw_All(What_All);
  325.     }
  326.  
  327.     }
  328.  
  329.     Clear_Plane(3, What_All);
  330.     Draw_Marked_Segments(DM_Normal, What_S);
  331.  
  332.     Stop_Timer();
  333.     Set_Mode_Normal();
  334.  
  335. } /* Modify_KnotInfo_Select_Up */
  336.  
  337. static void  Modify_KnotInfo_Select_Down(short X, short Y)
  338. /************************************************************************/
  339. /*                                                                      */
  340. /* Function called when mouse is moved to modify tension, bias or cont.    */
  341. /* of the selected knot.                        */
  342. /* X, Y are the actual coordinates in the active window 'Win'.        */
  343. /*                                                                      */
  344. /************************************************************************/
  345. {
  346.     if (Select_Spline == NULL || Select_Knot == NULL) return;
  347.  
  348.  
  349.     Modify_Active = TRUE;
  350.  
  351.     Draw_Marked_Segments(DM_Erase, What_S);
  352.     Draw_Marked_Segments(DM_Plane, What_S);
  353.  
  354.     Old_Modify_X = X;
  355.  
  356.     switch (Modify_Mode) {
  357.  
  358.     case 0:     /* Tension     */
  359.      Old_Value = Select_Knot->Tension;
  360.     break;
  361.  
  362.     case 1:    /* Bias     */
  363.     Old_Value = Select_Knot->Bias;
  364.     break;
  365.  
  366.     case 2:     /* Continuity    */
  367.     Old_Value = Select_Knot->Continuity;
  368.     break;
  369.  
  370.     } /* switch */
  371.  
  372.     if (MQ_Size_KnotInfo > 0) {
  373.     SetMouseQueue(Windows[Id_Active_Window].Window, MQ_Size_KnotInfo); 
  374.     Redraw_Always = TRUE;
  375.     } else Redraw_Always = FALSE;
  376.  
  377. } /* Modify_KnotInfo_Select_Down */
  378.  
  379. static 
  380. void  Modify_KnotInfo_Move(short X, short Y)
  381. /************************************************************************/
  382. /*                                                                      */
  383. /* Function called when mouse is moved to modify tension, bias or cont.    */
  384. /* of the selected knot.                        */
  385. /* X, Y are the actual coordinates in the active window 'Win'.        */
  386. /*                                                                      */
  387. /************************************************************************/
  388. {
  389.     char Buffer[Buffer_Length+1];
  390.  
  391.     if (!Modify_Active) return;
  392.  
  393.     Current_Value = (X - Old_Modify_X);
  394.     Current_Value = Current_Value / 400.0 + Old_Value;
  395.  
  396.     if (Redraw_Always) Modify_KnotInfo_Redraw(What_All);
  397.     else            Redraw_Mask |= What_All;
  398.  
  399. } /* Modify_KnotInfo_Move */
  400.  
  401. static
  402. Boolean_T Modify_KnotInfo_Handle_Event(struct IntuiMessage *Msg)
  403. /************************************************************************/
  404. /*                                                                      */
  405. /*                                                                      */
  406. /************************************************************************/
  407. {
  408.  
  409.     switch (Msg->Class) {
  410.  
  411.  
  412.     case IDCMP_MOUSEBUTTONS:
  413.         /* Msg->Code contain id of button pressed         */
  414.         /* Msg->MouseX and Msg->MouseY contain mouse position     */
  415.  
  416.     switch (Msg->Code) {
  417.  
  418.     case SELECTDOWN:
  419.         Modify_KnotInfo_Select_Down(Msg->MouseX, Msg->MouseY);
  420.         return(TRUE);
  421.  
  422.     case SELECTUP:
  423.         Modify_KnotInfo_Select_Up();
  424.         return(TRUE);
  425.  
  426.     } /* switch (Msg->Code) */
  427.     break;
  428.  
  429.         case MOUSEMOVE:
  430.         Modify_KnotInfo_Move(Msg->MouseX, Msg->MouseY);
  431.         return(TRUE);
  432.  
  433.     } /* switch (Msg->Class) */
  434.  
  435.     return(FALSE);
  436.  
  437. } /* Modify_KnotInfo_Handle_Event */
  438.  
  439. void Handle_G_Tension()
  440. /************************************************************************/
  441. /*                                                                      */
  442. /*                                                                      */
  443. /************************************************************************/
  444. {
  445.     if (Windows[W_KnotInfo].Window == NULL) return;
  446.     if (Select_Spline == NULL || Select_Knot == NULL) 
  447.                 if (!Select_Knot_From_Group()) return;
  448.  
  449.     Modify_Mode            = 0;
  450.     Modify_Active       = FALSE;
  451.     State.Handle_Event  = Modify_KnotInfo_Handle_Event;
  452.     State.Timeout     = Modify_KnotInfo_Timeout;
  453.     State.Redraw     = Modify_KnotInfo_Redraw;
  454.  
  455.     Display_Status("Modify tension");
  456.  
  457. } /* Handle_G_Tension */
  458.  
  459. void Handle_G_Bias()
  460. /************************************************************************/
  461. /*                                                                      */
  462. /*                                                                      */
  463. /************************************************************************/
  464. {
  465.     if (Windows[W_KnotInfo].Window == NULL) return;
  466.     if (Select_Spline == NULL || Select_Knot == NULL) 
  467.                 if (!Select_Knot_From_Group()) return;
  468.  
  469.     Modify_Mode            = 1;
  470.     Modify_Active       = FALSE;
  471.     State.Handle_Event  = Modify_KnotInfo_Handle_Event;
  472.     State.Timeout     = Modify_KnotInfo_Timeout;
  473.     State.Redraw     = Modify_KnotInfo_Redraw;
  474.  
  475.     Display_Status("Modify bias");
  476.  
  477. } /* Handle_G_Bias */
  478.  
  479. void Handle_G_Continuity()
  480. /************************************************************************/
  481. /*                                                                      */
  482. /*                                                                      */
  483. /************************************************************************/
  484. {
  485.     if (Windows[W_KnotInfo].Window == NULL) return;
  486.     if (Select_Spline == NULL || Select_Knot == NULL) 
  487.                 if (!Select_Knot_From_Group()) return;
  488.  
  489.     Modify_Mode            = 2;
  490.     Modify_Active       = FALSE;
  491.     State.Handle_Event  = Modify_KnotInfo_Handle_Event;
  492.     State.Timeout     = Modify_KnotInfo_Timeout;
  493.     State.Redraw     = Modify_KnotInfo_Redraw;
  494.  
  495.     Display_Status("Modify continuity");
  496.  
  497. } /* Handle_G_Continuity */
  498.