home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 144.lha / Leach_v1.3 / ruler.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-21  |  7.8 KB  |  285 lines

  1. /******************************************************************************
  2.  *
  3.  *  THIS FILE USES 4 COLUMN TAB STOPS. 
  4.  *    
  5.  *
  6.  *    This file contains ruler management routines for Leach Ver 1.3
  7.  *
  8.  ******************************************************************************/
  9.  
  10. #include    <functions.h>
  11. #include    <exec/ports.h>
  12. #include    <exec/libraries.h>
  13. #include    <graphics/gfx.h>
  14. #include    <graphics/rastport.h>
  15. #include    <graphics/text.h>
  16. #include    <intuition/intuitionbase.h>
  17. #include     <stdio.h>
  18.  
  19. /*----------------------------------------------------------------------------*
  20.  * gad.h is a real mixed bag. It defines (allocates storage for) many gadget
  21.  * related variables. It also declares the char array "statustext".
  22.  *----------------------------------------------------------------------------*/
  23.  
  24. /* #include    "gad.h" */
  25. extern struct Gadget swind_gad[];
  26. extern UBYTE         statustext[];
  27.  
  28. #define SHOWGAD swind_gad[0]
  29. #define MOVEGAD swind_gad[1]
  30.  
  31. #define GLOBAL extern
  32. #include "globals.h"
  33.  
  34. extern short endpoints[];            /* End point coordinates for ruler    */
  35.  
  36. #define x1 0                        /* endpoints array indexes.            */
  37. #define y1 1                        /* Note LOWER CASE!                    */
  38. #define x2 2
  39. #define y2 3
  40.  
  41. extern struct    Border ruler;        /* Border struct for ruler.            */
  42.  
  43. /************  VARIABLES USED JUST INSIDE THIS FILE  ********************/
  44.  
  45. extern short mx, my;            /* Mouse coordinates.    */
  46. extern short update;            /* TRUE if status display needs refreshing.    */
  47. extern short ruler_color;
  48. extern short moving;            /* Nonzero if ruler should be tracking mouse*/
  49. extern short sizing;            /* 0 if the ruler is not being sized/moved    */    
  50.                                 /* 1 if the first endpoint is being moved.    */
  51.                                 /* 2 if the second endpoint is being moved.    */
  52.  
  53. /******************************************************************************
  54.  * This function will erase the ruler, deSELECT the show/hide gadget, deSELECT
  55.  * the move/freeze gadget and disable the move/freeze gadget.
  56.  ******************************************************************************/
  57.  
  58. hide_ruler()
  59. {
  60.     swap_gtext(&SHOWGAD);
  61.     SHOWGAD.Flags &= ~SELECTED;                    /* Deselect s/h gadget.    */
  62.     restore_line();                             /* Erase line.            */
  63.  
  64.     if (MOVEGAD.Flags & SELECTED)                /* If in move mode...    */
  65.     {
  66.         swap_gtext(&MOVEGAD);
  67.         MOVEGAD.Flags &= ~SELECTED;                /* Deselect m/f gadget.    */
  68.         HostIDCMPmods &= ~(MOUSEBUTTONS | MOUSEMOVE); 
  69.         ModifyIDCMP(HostWind, HostIDCMPflags | HostIDCMPmods);
  70.         restore_pointer();
  71.     }
  72.     OffGadget(&MOVEGAD, StatWind, NULL);
  73.     RefreshGadgets(swind_gad, StatWind, NULL);    /* Redraw with new text */
  74.  
  75. } /*  End of hide_ruler()  */
  76.  
  77.  
  78. /******************************************************************************
  79.  *  Move the ruler by erasing the old one, altering the endpoint coordinates
  80.  *  and redrawing the border. Looks at variables in the Host's window
  81.  *  structure and prevents the ruler from being run off the screen edges.
  82.  *    Returns TRUE if the ruler was moved, FALSE if it wasn't.
  83.  ******************************************************************************/
  84.  
  85. move_ruler()
  86. {
  87. short deltax, deltay;    /* Coordinates of first endpoint - mouse coodinates */
  88. short rc;                /* Return code: 1 = frame moved, 0 = not moved.        */
  89.  
  90. rc = 0;
  91. deltax = mx - endpoints[x1];    /* Neg deltas mean we're trying to move right */
  92. deltay = my - endpoints[y1];    /* Neg deltas mean we're trying to move up    */
  93.  
  94. /*----------------------------------------------------------------------*
  95.  * Check the boundry conditions. You never have to check the first    
  96.  * endpoint because the mouse can't be moved off the screen.
  97.  *----------------------------------------------------------------------*/
  98.  
  99. /* If ruler can move horizontally... Adjust X coordinates.    */
  100.  
  101. if (deltax    && ( endpoints[x2] + deltax < HostWind->LeftEdge + HostWind->Width )
  102.             && ( endpoints[x2] + deltax > HostWind->LeftEdge) )
  103.  
  104. {
  105.     endpoints[x1] = mx;
  106.     endpoints[x2] += deltax;
  107.     rc = 1;
  108. }
  109. /* If the ruler can move verically, adjust Y coordinates.        */
  110.  
  111. if ( deltay && ( endpoints[y2] + deltay < HostWind->TopEdge + HostWind->Height )
  112.             && ( endpoints[y2] + deltay > HostWind->TopEdge) )
  113. {
  114.     endpoints[y1] = my;
  115.     endpoints[y2] += deltay;
  116.     rc = 1;
  117. }
  118.  
  119. /* Restore current line from saved_line buffers. Then redraw line using */
  120. /* the coordinates that we just calculated.                                */
  121.  
  122. if (rc)
  123. {
  124.     restore_line();
  125.     draw_line(HostRPort, ruler_color, endpoints[x1], endpoints[y1],
  126.                                       endpoints[x2], endpoints[y2]);
  127. }
  128. update += rc;
  129.  
  130. return(rc);
  131.  
  132. } /*  End of move_ruler()  */
  133.  
  134.  
  135. /******************************************************************************
  136.  *  Resize the ruler by adjusting one of the endpoints.
  137.  *  Boundry checking is unnecessary because the mouse won't leave the screen.
  138.  *    Always returns TRUE.
  139.  ******************************************************************************/
  140.  
  141. size_ruler()
  142. {
  143.  
  144. if ( sizing == 1)
  145. {
  146.     endpoints[x1] = mx;
  147.     endpoints[y1] = my;
  148. }
  149. else if (sizing == 2)
  150. {
  151.     endpoints[x2] = mx;
  152.     endpoints[y2] = my;
  153. }
  154.  
  155. /* Erase the current ruler and redraw it.    */
  156. restore_line();                
  157. draw_line(HostRPort, ruler_color, endpoints[x1], endpoints[y1],
  158.                                   endpoints[x2], endpoints[y2]);
  159. update = 1;
  160.  
  161. return(1);
  162.  
  163. } /*  End of size_ruler()  */
  164.  
  165.  
  166. /******************************************************************************
  167.  *  Calculate ruler length and slope in degrees. Display all that plus the
  168.  *  coordinates of the ruler's endpoints.
  169.  *
  170.  *  This routine requires linking with a floating point math library.
  171.  ******************************************************************************/
  172.  
  173.  
  174. update_status()
  175. {
  176. short            i;
  177. double            len, sin;
  178. float            degrees;
  179. long            deltax, deltay;
  180.  
  181. extern double    sqrt(), asin();
  182.  
  183. extern UBYTE            stext[];
  184.  
  185.  
  186. /* Indexes of char data fields in the text string. These indexes must be    */
  187. /* kept in sync with the text"xxx" string initialization in main.c            */
  188.  
  189. #define X1         4
  190. #define Y1        13
  191. #define X2        22
  192. #define Y2        31
  193. #define LEN        41
  194. #define SLOPE    54
  195.  
  196. deltax  = endpoints[x2] - endpoints[x1];
  197. deltay  = endpoints[y2] - endpoints[y1];
  198.  
  199. /* I handle horizontal and vertical lines as a special case because they    */
  200. /* are used often and are easy.                                                */
  201.  
  202. if (deltay == 0)                        /* Horizontal    */
  203. {
  204.     if (deltax > 0)
  205.     {
  206.         len = deltax + 1;
  207.         degrees = 0.0;
  208.     }
  209.     else
  210.     {
  211.         degrees = 180.0;
  212.         len = -(deltax - 1);
  213.     }
  214. }
  215. else if (deltax == 0)                    /* Vertical        */
  216. {
  217.     if (deltay > 0)
  218.     {
  219.         len = deltay + 1;
  220.         degrees = 270.0;
  221.     }
  222.     else
  223.     {
  224.         degrees = 90.0;
  225.         len = -(deltay - 1);
  226.     }
  227. }
  228. else                                    /* Other        */
  229. {
  230.     /*------------------------------------------------------------------*
  231.      * The signs of the deltas don't matter for length but, the sign of
  232.      * deltay does make a difference in the slope calculation. The sign
  233.      * of deltay is wrong to begin with because it is from the screen
  234.      * coordinate system.
  235.      *------------------------------------------------------------------*/
  236.  
  237.     len = deltax * deltax + deltay * deltay;
  238.     len = sqrt(len);
  239.  
  240.     /* sine = opposite / hypotonuse or someting like that    */
  241.  
  242.     sin =  deltay / len;
  243.     if (deltax > 0)        /* If in 1st or 4th quadrant...        */
  244.     {
  245.         sin = -sin;        /* Fix sign of sine.                */
  246.     }
  247.     degrees = 57.296 * asin(sin);
  248.  
  249.     if (deltax > 0)
  250.     {
  251.         if ( deltay > 0)                /* 1st quadrant is ok as is    */
  252.             degrees += 360.0;            /* 4th quadrant                */
  253.     }
  254.     else
  255.         degrees += 180.0;                /* 2nd and 3rd quadrants    */
  256.  
  257. } /*  end of else "other"  */
  258.  
  259. /* Now that we have all this great data, we need to display it.    */
  260.  
  261. sprintf(&stext[X1], "%03d", endpoints[x1] );
  262. stext[X1 + 3] = ' ';
  263.  
  264. sprintf(&stext[Y1], "%03d", endpoints[y1] );
  265. stext[Y1 + 3] = ' ';
  266.  
  267. sprintf(&stext[X2], "%03d", endpoints[x2] );
  268. stext[X2 + 3] = ' ';
  269.  
  270. sprintf(&stext[Y2], "%03d", endpoints[y2] );
  271. stext[Y2 + 3] = ' ';
  272.  
  273. sprintf(&stext[LEN], "%04d", (short) (len + 0.5) );
  274. stext[LEN + 4] = ' ';
  275.  
  276. sprintf(&stext[SLOPE], "%05.1f", degrees);
  277.  
  278. PrintIText(StatRPort, &statustext, 0L, 0L);
  279.  
  280. return;
  281.  
  282. } /*  End of update_title()  */
  283.  
  284. /****************************  END OF RULER.C  ******************************/
  285.