home *** CD-ROM | disk | FTP | other *** search
/ Virtual Reality Homebrewer's Handbook / vr.iso / vr386 / cursor2d.c < prev    next >
C/C++ Source or Header  |  1996-03-19  |  6KB  |  243 lines

  1. /* 2D cursor handling  */
  2. // Mouse cursor show, move
  3. // Click-on-screen->object selection
  4. // Mouse handler
  5. // Rewritten for VR-386 by Dave Stampe 9/1/94
  6.  
  7. /* Original written by Dave Stampe, Aug. 1992 */
  8.  
  9.  
  10. /*
  11.  This code is part of the VR-386 project, created by Dave Stampe.
  12.  VR-386 is a desendent of REND386, created by Dave Stampe and
  13.  Bernie Roehl.  Almost all the code has been rewritten by Dave
  14.  Stampre for VR-386.
  15.  
  16.  Copyright (c) 1994 by Dave Stampe:
  17.  May be freely used to write software for release into the public domain
  18.  or for educational use; all commercial endeavours MUST contact Dave Stampe
  19.  (dstampe@psych.toronto.edu) for permission to incorporate any part of
  20.  this software or source code into their products!  Usually there is no
  21.  charge for under 50-100 items for low-cost or shareware products, and terms
  22.  are reasonable.  Any royalties are used for development, so equipment is
  23.  often acceptable payment.
  24.  
  25.  ATTRIBUTION:  If you use any part of this source code or the libraries
  26.  in your projects, you must give attribution to VR-386 and Dave Stampe,
  27.  and any other authors in your documentation, source code, and at startup
  28.  of your program.  Let's keep the freeware ball rolling!
  29.  
  30.  DEVELOPMENT: VR-386 is a effort to develop the process started by
  31.  REND386, improving programmer access by rewriting the code and supplying
  32.  a standard API.  If you write improvements, add new functions rather
  33.  than rewriting current functions.  This will make it possible to
  34.  include you improved code in the next API release.  YOU can help advance
  35.  VR-386.  Comments on the API are welcome.
  36.  
  37.  CONTACT: dstampe@psych.toronto.edu
  38. */
  39.  
  40.  
  41.  
  42. #include <stdio.h>
  43. #include <dos.h>
  44.  
  45. #include "pointer.h"
  46. #include "vr_api.h"
  47. #include "f3dkitd.h"
  48. #include "renderer.h"    // for screen monitor
  49. #include "intmath.h"
  50.  
  51. extern int manip_2D_avail;
  52.  
  53. /***************** 2D (screen) cursor handling ************/
  54.  
  55. static SWORD oldx = 160, oldy = 100;
  56. static BOOL cursor_visible = FALSE;
  57. static BOOL enable_2D;
  58.  
  59. void cursor_enable(BOOL state)   /* hardwire on/off */
  60. {
  61.   if(!state && cursor_visible && in_graphics)
  62.         cursor_hide();
  63.   enable_2D = state;
  64.   if(state && !cursor_visible && in_graphics)
  65.         cursor_show();
  66. }
  67.  
  68. void cursor_move(WORD x, WORD y) /* move cursor if visible */
  69. {
  70.   oldx = x;
  71.   oldy = y;
  72.   if (cursor_visible==TRUE && enable_2D==TRUE)
  73.     {
  74.       set_drawpage(current_video_page);
  75.       erase_cursor(current_video_page);
  76.       draw_cursor(x,y,-(screeninfo->colors-1),current_video_page);
  77.     }
  78. }
  79.  
  80. BOOL cursor_hide() /* erase cursor, tell if it was shown */
  81. {
  82.   BOOL i = cursor_visible;
  83.  
  84.   if (cursor_visible==TRUE && enable_2D)
  85.     {
  86.       erase_cursor(current_video_page);
  87.       cursor_visible = FALSE;
  88.     }
  89.   return i;
  90. }
  91.  
  92.  
  93. BOOL last_cursor_hide(WORD page) /* erase cursor, tell if it was shown */
  94. {                       /* for non-current page */
  95.   BOOL i = cursor_visible;
  96.  
  97.   if (cursor_visible==TRUE && enable_2D)
  98.     {
  99.       erase_cursor(page);
  100.       cursor_visible = FALSE;
  101.     }
  102.   return i;
  103. }
  104.  
  105.  
  106. void cursor_show() /* redisplay cursor */
  107. {
  108.   if (cursor_visible==FALSE && enable_2D)
  109.     {
  110.       set_drawpage(current_video_page);
  111.       draw_cursor(oldx, oldy, -(screeninfo->colors-1), current_video_page);
  112.       cursor_visible = TRUE;
  113.     }
  114. }
  115.  
  116.  
  117. WORD move_2D(PDRIVER *d, WORD *x, WORD *y, WORD *b)
  118. {
  119.   int c;
  120.  
  121.   if (d == NULL) return 0;
  122.   if (PNEW_POS & (c = mouse_read(d, x, y, b)))
  123.     cursor_move(*x, *y);
  124.   return c;
  125. }
  126.  
  127.  
  128. WORD move_till_click(PDRIVER *d, WORD b, WORD *x, WORD *y) /* b is button mask */
  129. {
  130.   int s;
  131.   unsigned c;
  132.  
  133.   if (d == NULL) return 0;
  134.   while(1)
  135.     {
  136.       if (((s = move_2D(d, x, y, &c)) & PNEW_BUT) && (c & b)) break;
  137.     }
  138.   return s;
  139. }
  140.  
  141. extern int mouse_nav;
  142.  
  143. BOOL can_point_2D()
  144. {
  145.   if (!manip_2D_avail || mouse_nav)
  146.     {
  147.       if(in_graphics)
  148.     {
  149.       save_screen();
  150.       popmsg("Mouse not available");
  151.       tdelay(500);
  152.       restore_screen();
  153.     }
  154.       return 0;
  155.     }
  156.   return 1;
  157. }
  158.  
  159.  
  160. ////// USE report_screen_object() <objsppt.c>
  161. ////// to get rest of data on object
  162.  
  163.  
  164. OBJECT *find_object_on_screen(WORD x, WORD y)
  165. {
  166.   OBJECT *obj;               // ACTUALLY REDRAWS SCREEN TO GET DATA
  167.   set_screen_monitor(x, y);
  168.   screen_refresh(current_camera);
  169.   clear_screen_monitor();
  170.   process_screen_monitor();
  171.   return screen_monitor_object();
  172. }
  173.  
  174.  
  175.  
  176. OBJECT *move_and_find_object_2D(PDRIVER *d, WORD *buttons)    // to find object etc
  177. {
  178.   int x, y, c;
  179.   OBJECT *obj;
  180.   int b;
  181.  
  182.   if(!buttons) buttons = &b;
  183.  
  184.   if (!(c = move_2D(d, &x, &y, buttons))) return NULL; /* no changes */
  185.  
  186.   if (*buttons != 0 && (c & PNEW_BUT)) /* new button: DOWN event */
  187.     {
  188.       obj = find_object_on_screen(x, y);
  189.       if (obj && is_object_selectable(obj)) return obj;
  190.     }
  191.   return NULL;
  192. }
  193.  
  194.  
  195. // the "screen idle" call
  196. // returns 0 if no action
  197. // returns -1 if menu-area click
  198. // returns 1 if new object selected
  199.  
  200. int move_and_select_2D(PDRIVER *d)
  201. {
  202.   int x, y, c;
  203.   unsigned b;
  204.  
  205.   if (!(c = move_2D(d, &x, &y, &b))) return 0; /* no changes */
  206.  
  207.   b &= 1; /* only left button wanted */
  208.   if(b==1 && y==0) return -1; /* menu callup */
  209.  
  210.   if (b != 0 && (c & PNEW_BUT)) /* new button: DOWN event */
  211.     {
  212.       OBJECT *obj;
  213.  
  214.       obj = find_object_on_screen(x, y);
  215.       if (obj && is_object_selectable(obj))
  216.     {
  217.       if(is_object_selected(obj))
  218.          unhighlight_object(obj);
  219.       else
  220.          highlight_object(obj);
  221.       world_changed++;
  222.       return 1;
  223.     }
  224.       else
  225.     {
  226.       save_screen();
  227.       popmsg("Not on (selectable) object");
  228.       tdelay(300);
  229.       restore_screen();
  230.     }
  231.     }
  232.   return 0;
  233. }
  234.  
  235. extern PDRIVER *cursor_device;
  236.  
  237. void mouse_process()
  238. {
  239.   int i;
  240.   if(mouse_nav || !cursor_device) return;
  241.   i = move_and_select_2D(cursor_device);
  242.   if(i==-1) process_a_key('X');    // menu callup
  243. }