home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d502 / cells.lha / CELLS / CELLSSource.lzh / cSlider.c < prev    next >
C/C++ Source or Header  |  1991-04-20  |  7KB  |  267 lines

  1. /*
  2.  *  CELLS       An Implementation of the WireWorld cellular automata
  3.  *              as described in Scientific American, Jan 1990.
  4.  *
  5.  *              Copyright 1990 by Davide P. Cervone.
  6.  *  You may use this code, provided this copyright notice is kept intact.
  7.  *  See the CELLS.HELP file for complete information on distribution conditions.
  8.  */
  9.  
  10. /*
  11.  *  File:  cSlider.c            Handles the main grid sliders and zoom gadgets.
  12.  */
  13.  
  14.  
  15. #include "cGadget.h"
  16.  
  17.  
  18. /*
  19.  *  UpdateSliders()
  20.  *
  21.  *  Find the new pot and body values for the sliders and update the slider's
  22.  *  gadgets if necessary.
  23.  */
  24.  
  25. void UpdateSliders()
  26. {
  27.    int Pot,Body;
  28.  
  29.    Body = 0xFFFF * GridW / MAXGRIDW;
  30.    if (GridW != MAXGRIDW)
  31.       Pot = 0xFFFF * GridX / (MAXGRIDW - GridW);
  32.      else
  33.       Pot = 0;
  34.    if (Body != cSlider[PROP_SLIDEH].HorizBody ||
  35.        Pot  != cSlider[PROP_SLIDEH].HorizPot)
  36.           NewModifyProp(&cGadget[ID_SLIDEH],myWindow,NULL,
  37.              cSlider[PROP_SLIDEH].Flags,Pot,0,Body,0,1);
  38.  
  39.    Body = 0xFFFF * GridH / MAXGRIDH;
  40.    if (GridH != MAXGRIDH)
  41.       Pot = 0xFFFF * GridY / (MAXGRIDH - GridH);
  42.      else
  43.       Pot = 0;
  44.    if (Body != cSlider[PROP_SLIDEV].VertBody ||
  45.        Pot  != cSlider[PROP_SLIDEV].VertPot)
  46.           NewModifyProp(&cGadget[ID_SLIDEV],myWindow,NULL,
  47.              cSlider[PROP_SLIDEV].Flags,0,Pot,0,Body,1);
  48. }
  49.  
  50.  
  51. /*
  52.  *  DoSlideH()
  53.  *
  54.  *  Find the new grid position determined by the horizontal slider, and
  55.  *  If it is different from the existing position, then
  56.  *    If the existing position does not overlap the new position, 
  57.  *      clear the screen abd refresh it completely,
  58.  *    Otherwise
  59.  *      calculate the offsets from the current position
  60.  *      and scroll the grid the desired amount.
  61.  *      Clear the newly uncovered area, and refresh it
  62.  *      then update the screen from the double buffer
  63.  */
  64.  
  65. void DoSlideH()
  66. {
  67.    short x,dx;
  68.    
  69.    x = ((MAXGRIDW - GridW) * cSlider[PROP_SLIDEH].HorizPot + 0x7FFF) / 0xFFFF;
  70.    if (x >= MAXGRIDW) x = MAXGRIDW - 1;
  71.  
  72.    if (x != GridX)
  73.    {
  74.       if (x + GridW <= GridX || x >= GridX + GridW)
  75.       {
  76.          GridX = x;
  77.          ClearScreen(FALSE);
  78.          RefreshScreen(CurGen);
  79.       } else {
  80.          dx = GridX - x; GridX = x; x = 0;
  81.          ScrollScreen(-dx,0);
  82.          if (dx < 0) {x = dx + GridW; dx = -dx;}
  83.          ClearPatch(x,0,dx,GridH);
  84.          RefreshPatch(x,0,dx,GridH,CurGen);
  85.          UpdateScreen();
  86.       }
  87.    }
  88. }
  89.  
  90.  
  91. /*
  92.  *  DoSlideV()
  93.  *
  94.  *  Find the new grid position determined by the vertical slider, and
  95.  *  If it is different from the existing position, then
  96.  *    If the existing position does not overlap the new position, 
  97.  *      clear the screen abd refresh it completely,
  98.  *    Otherwise
  99.  *      calculate the offsets from the current position
  100.  *      and scroll the grid the desired amount.
  101.  *      Clear the newly uncovered area, and refresh it
  102.  *      then update the screen from the double buffer
  103.  */
  104.  
  105. void DoSlideV()
  106. {
  107.    short y,dy;
  108.    
  109.    y = ((MAXGRIDH - GridH) * cSlider[PROP_SLIDEV].VertPot + 0x7FFF) / 0xFFFF;
  110.    if (y >= MAXGRIDH) y = MAXGRIDH - 1;
  111.  
  112.    if (y != GridY)
  113.    {
  114.       if (y + GridH <= GridY || y >= GridY + GridH)
  115.       {
  116.          GridY = y;
  117.          ClearScreen(FALSE);
  118.          RefreshScreen(CurGen);
  119.       } else {
  120.          dy = GridY - y; GridY = y; y = 0;
  121.          ScrollScreen(0,-dy);
  122.          if (dy < 0) {y = dy + GridH; dy = -dy;}
  123.          ClearPatch(0,y,GridW,dy);
  124.          RefreshPatch(0,y,GridW,dy,CurGen);
  125.          UpdateScreen();
  126.       }
  127.    }
  128. }
  129.  
  130.  
  131. /*
  132.  *  DoScrollLeft()
  133.  *
  134.  *  While there's still more room to scroll and the arrow is selected,
  135.  *    update the current position and scroll the screen by one cell
  136.  *    clear the uncovered patch and refresh it, then update the screen
  137.  *    from the double buffer.  Update the sliders to reflect the new
  138.  *    grid position.
  139.  */
  140.  
  141. void DoScrollLeft(theGadget)
  142. struct Gadget *theGadget;
  143. {
  144.    while (GridX > 0 && (theGadget->Flags & SELECTED))
  145.    {
  146.       GridX--; ScrollScreen(-1,0);
  147.       ClearPatch(0,0,1,GridH);
  148.       RefreshPatch(0,0,1,GridH,CurGen);
  149.       UpdateScreen();
  150.       UpdateSliders();
  151.    }
  152. }
  153.  
  154.  
  155. /*
  156.  *  DoScrollRight()
  157.  *
  158.  *  While there's still more room to scroll and the arrow is selected,
  159.  *    update the current position and scroll the screen by one cell
  160.  *    clear the uncovered patch and refresh it, then update the screen
  161.  *    from the double buffer.  Update the sliders to reflect the new
  162.  *    grid position.
  163.  */
  164.  
  165. void DoScrollRight(theGadget)
  166. struct Gadget *theGadget;
  167. {
  168.    while (GridX < MAXGRIDW-GridW && (theGadget->Flags & SELECTED))
  169.    {
  170.       GridX++; ScrollScreen(1,0);
  171.       ClearPatch(GridW-1,0,1,GridH);
  172.       RefreshPatch(GridW-1,0,1,GridH,CurGen);
  173.       UpdateScreen();
  174.       UpdateSliders();
  175.    }
  176. }
  177.  
  178.  
  179. /*
  180.  *  DoScrollUp()
  181.  *
  182.  *  While there's still more room to scroll and the arrow is selected,
  183.  *    update the current position and scroll the screen by one cell
  184.  *    clear the uncovered patch and refresh it, then update the screen
  185.  *    from the double buffer.  Update the sliders to reflect the new
  186.  *    grid position.
  187.  */
  188.  
  189. void DoScrollUp(theGadget)
  190. struct Gadget *theGadget;
  191. {
  192.    while (GridY > 0 && (theGadget->Flags & SELECTED))
  193.    {
  194.       GridY--; ScrollScreen(0,-1);
  195.       ClearPatch(0,0,GridW,1);
  196.       RefreshPatch(0,0,GridW,1,CurGen);
  197.       UpdateScreen();
  198.       UpdateSliders();
  199.    }
  200. }
  201.  
  202.  
  203. /*
  204.  *  DoScrollDown()
  205.  *
  206.  *  While there's still more room to scroll and the arrow is selected,
  207.  *    update the current position and scroll the screen by one cell
  208.  *    clear the uncovered patch and refresh it, then update the screen
  209.  *    from the double buffer.  Update the sliders to reflect the new
  210.  *    grid position.
  211.  */
  212.  
  213. void DoScrollDown(theGadget)
  214. struct Gadget *theGadget;
  215. {
  216.    while (GridY < MAXGRIDH-GridH && (theGadget->Flags & SELECTED))
  217.    {
  218.       GridY++; ScrollScreen(0,1);
  219.       ClearPatch(0,GridH-1,GridW,1);
  220.       RefreshPatch(0,GridH-1,GridW,1,CurGen);
  221.       UpdateScreen();
  222.       UpdateSliders();
  223.    }
  224. }
  225.  
  226.  
  227. /*
  228.  *  DoZoom()
  229.  *
  230.  *  If the cellsize can still be increased,
  231.  *    Set the board variables to the new size,
  232.  *    Clear the screen and refresh it in the new size.
  233.  *    Update the sliders to their new positions and sizes.
  234.  */
  235.  
  236. void DoZoom()
  237. {
  238.    if (CellSize < MAXCELLSIZE)
  239.    {
  240.       SetBoardSize(CellSize+1);
  241.       ClearScreen(FALSE);
  242.       RefreshScreen(CurGen);
  243.       UpdateSliders();
  244.    }
  245. }
  246.  
  247.  
  248. /*
  249.  *  DoShrink()
  250.  *
  251.  *  If the cell size can still get smaller,
  252.  *    Set the board variables to the new size,
  253.  *    Clear the screen and refresh it in the new size.
  254.  *    Update the sliders to their new positions and sizes.
  255.  */
  256.  
  257. void DoShrink()
  258. {
  259.    if (CellSize > MINCELLSIZE)
  260.    {
  261.       SetBoardSize(CellSize-1);
  262.       ClearScreen(FALSE);
  263.       RefreshScreen(CurGen);
  264.       UpdateSliders();
  265.    }
  266. }
  267.