]> icculus.org git repositories - taylor/freespace2.git/blob - src/ui/gadget.cpp
get rid of some platform specific stuff
[taylor/freespace2.git] / src / ui / gadget.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell 
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/Ui/GADGET.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Functions for the base gadget class
16  *
17  * $Log$
18  * Revision 1.8  2005/10/01 21:55:00  taylor
19  * fix a small bug in UI_GADGET that could leave a control animation loaded in memory without a way to unload it
20  * allow a slider with no defined hotspot to function properly (fixes the FS1 skill slider mask problem)
21  *
22  * Revision 1.7  2003/05/25 02:30:44  taylor
23  * Freespace 1 support
24  *
25  * Revision 1.6  2002/06/09 04:41:29  relnev
26  * added copyright header
27  *
28  * Revision 1.5  2002/05/28 21:03:38  relnev
29  * implemented stub
30  *
31  * Revision 1.4  2002/05/26 20:22:48  theoddone33
32  * Most of network/ works
33  *
34  * Revision 1.3  2002/05/07 03:16:53  theoddone33
35  * The Great Newline Fix
36  *
37  * Revision 1.2  2002/05/04 04:36:56  theoddone33
38  * More changes, took out a lot of the sound stuff which will bite later but
39  * I don't care.
40  *
41  * Revision 1.1.1.1  2002/05/03 03:28:11  root
42  * Initial import.
43  *
44  * 
45  * 18    8/11/99 3:21p Jefff
46  * set_bmaps clarification by daveb
47  * 
48  * 17    8/10/99 6:54p Dave
49  * Mad optimizations. Added paging to the nebula effect.
50  * 
51  * 16    7/16/99 1:50p Dave
52  * 8 bit aabitmaps. yay.
53  * 
54  * 15    6/25/99 11:59a Dave
55  * Multi options screen.
56  * 
57  * 14    5/21/99 6:45p Dave
58  * Sped up ui loading a bit. Sped up localization disk access stuff. Multi
59  * start game screen, multi password, and multi pxo-help screen.
60  * 
61  * 13    5/03/99 8:33p Dave
62  * New version of multi host options screen.
63  * 
64  * 12    4/16/99 5:23p Neilk
65  * Removed bitmap mprintf's to make debugging faster
66  * 
67  * 11    2/11/99 3:08p Dave
68  * PXO refresh button. Very preliminary squad war support.
69  * 
70  * 10    2/05/99 4:09p Anoop
71  * Hopefully the last fix for this file :)
72  * 
73  * 9     2/05/99 3:26p Enricco
74  * Oops. Some .ani's can have 4 frames.
75  * 
76  * 8     2/05/99 3:07p Davidg
77  * Fixed braindead bmap setting code.
78  * 
79  * 7     2/01/99 5:55p Dave
80  * Removed the idea of explicit bitmaps for buttons. Fixed text
81  * highlighting for disabled gadgets.
82  * 
83  * 6     12/21/98 9:05a Dave
84  * Changed UI code so it doesn't require 3 bitmaps for a 3 state button.
85  * 
86  * 5     12/18/98 1:13a Dave
87  * Rough 1024x768 support for Direct3D. Proper detection and usage through
88  * the launcher.
89  * 
90  * 4     12/02/98 5:47p Dave
91  * Put in interface xstr code. Converted barracks screen to new format.
92  * 
93  * 3     11/30/98 1:07p Dave
94  * 16 bit conversion, first run.
95  * 
96  * 2     10/07/98 10:54a Dave
97  * Initial checkin.
98  * 
99  * 1     10/07/98 10:51a Dave
100  * 
101  * 22    5/11/98 5:29p Hoffoss
102  * Added mouse button mapped to joystick button support.
103  * 
104  * 21    4/12/98 2:09p Dave
105  * Make main hall door text less stupid. Make sure inputbox focus in the
106  * multi host options screen is managed more intelligently.
107  * 
108  * 20    4/09/98 7:14p Hoffoss
109  * Did some cool changes for tooltips.
110  * 
111  * 19    2/11/98 6:24p Hoffoss
112  * Fixed bug where disabled and hidden buttons give failed sound when
113  * pressed.  Shouldn't happen when they are hidden.
114  * 
115  * 18    1/15/98 11:54a Hoffoss
116  * Embelished file with nifty comments.
117  * 
118  * 17    1/14/98 6:43p Hoffoss
119  * Massive changes to UI code.  A lot cleaner and better now.  Did all
120  * this to get the new UI_DOT_SLIDER to work properly, which the old code
121  * wasn't flexible enough to handle.
122  * 
123  * 16    12/30/97 4:26p Lawrance
124  * Disable right-click movement of ui controls
125  * 
126  * 15    10/24/97 10:58p Hoffoss
127  * Made some changes to the UI code to do some things I need it to do.
128  * 
129  * 14    10/12/97 3:45p Lawrance
130  * only allow movement of UI controls in debug
131  * 
132  * 13    10/09/97 4:56p Lawrance
133  * init GADGET stuff in base_create(), not the constructor
134  * 
135  * 12    9/07/97 10:05p Lawrance
136  * don't set hotspot_num, done in gadget constructor
137  * 
138  * 11    8/29/97 7:34p Lawrance
139  * use bmpman for storing bitmaps for ui controls
140  * 
141  * 10    8/18/97 5:28p Lawrance
142  * integrating sounds for when mouse goes over an active control
143  * 
144  * 9     7/20/97 7:00p Lawrance
145  * changed name of some anim_ functions to be more consistent
146  * 
147  * 8     5/26/97 10:26a Lawrance
148  * get slider control working 100%
149  * 
150  * 7     5/22/97 5:36p Lawrance
151  * allowing custom art for scrollbars
152  * 
153  * 6     5/21/97 11:07a Lawrance
154  * integrate masks and custom bitmaps
155  * 
156  * 5     4/28/97 2:19p Lawrance
157  * added clear_focus() function
158  * 
159  * 4     1/28/97 4:58p Lawrance
160  * allowing hidden UI components
161  * 
162  * 3     12/02/96 2:17p John
163  * Made right button drag UI gadgets around and
164  * Ctrl+Shift+Alt+F12 dumps out where they are.
165  * 
166  * 2     11/21/96 10:58a John
167  * Started adding code to drag buttons.
168  * 
169  * 1     11/14/96 6:55p John
170  *
171  * $NoKeywords: $
172  */
173
174 #include "uidefs.h"
175 #include "ui.h"
176 #include "bmpman.h"
177 #include "animplay.h"
178
179 // constructor
180 UI_GADGET::UI_GADGET()
181 {
182 }
183
184 // destructor
185 UI_GADGET::~UI_GADGET()
186 {
187 }
188
189 int UI_GADGET::get_hotspot()
190 {
191         if (!linked_to_hotspot)
192                 return -1;
193
194         return hotspot_num;
195 }
196
197 void UI_GADGET::reset()
198 {
199         m_flags = 0;
200 }
201
202 // --------------------------------------------------------------------
203 // Links a hotspot (palette index in mask) to the given gadget.
204 //
205 void UI_GADGET::link_hotspot(int num)
206 {
207         hotspot_num = num;
208         linked_to_hotspot = 1;
209 }
210
211 // --------------------------------------------------------------------
212 // Extract MAX_BMAPS_PER_CONTROL bitmaps for the different states of the control.
213 //
214 // The bitmap ids are stored in the bmap_ids[] array.  If you don't want to store
215 // from the zero index, fill in the offset parameter.  offset is a default parameter
216 // with a default value of zero.
217 //
218 // NOTE:  The bitmaps stored in a .ani file.  What each frame is used for
219 //                       is left up to the component to decide.
220 //
221
222 // loads nframes bitmaps, starting at index start_frame.
223 // anything < start_frame will not be loaded.
224 // this keeps the loading code from trying to load bitmaps which don't exist
225 // and taking an unnecessary disk hit.          
226 int UI_GADGET::set_bmaps(const char *ani_fname, int nframes, int start_frame)
227 {
228         int i;  
229 #ifndef MAKE_FS1
230         char full_name[MAX_FILENAME_LEN] = "";
231         char tmp[10];
232         int num_digits;
233         int its_all_good = 0;
234         int s_idx;
235 #endif
236         int idx;        
237         
238         // clear out all frames
239         for(idx=0; idx<MAX_BMAPS_PER_GADGET; idx++){
240                 bmap_ids[idx] = -1;
241         }
242         
243         // load all the bitmaps
244
245         SDL_assert(nframes < MAX_BMAPS_PER_GADGET);             
246         m_num_frames = nframes;         
247 #ifndef MAKE_FS1
248         // FS1 uses real anis instead of frame based pcxs so this code just slows down
249         // searching and therefore loading
250         for(idx=start_frame; idx<nframes; idx++){
251                 // clear the string
252                 strcpy(full_name, "");
253
254                 // get the # of digits for this index
255                 num_digits = (idx < 10) ? 1 : (idx < 100) ? 2 : (idx < 1000) ? 3 : 4;
256
257                 // build the actual filename
258                 strcpy(full_name, ani_fname);           
259                 for(s_idx=0; s_idx<(4-num_digits); s_idx++){
260                         strcat(full_name, NOX("0"));
261                 }
262 #ifdef PLAT_UNIX
263                 sprintf(tmp, "%d", idx);
264                 strcat(full_name, tmp);
265 #else
266                 strcat(full_name, itoa(idx, tmp, 10));          
267 #endif
268
269                 // try and load the bitmap                              
270                 bmap_ids[idx] = bm_load(full_name);     
271                 if(bmap_ids[idx] != -1){                
272                         // bm_lock(bmap_ids[idx], 16, 0);
273                         // bm_unlock(bmap_ids[idx]);
274                         
275                         its_all_good = 1;
276                 } else {
277                         // mprintf(("Skipping %s\n", full_name));
278                         // its_all_good = 0;
279                 }
280         }
281
282         // done
283         if(its_all_good){
284                 uses_bmaps = 1;         
285                 return 0;
286         }
287 #endif
288
289         // no go, so try and load as an ani. try and load as an .ani    
290         bmap_ids[0] = bm_load_animation(ani_fname, &m_num_frames);      
291         if((bmap_ids[0] >= 0) && (m_num_frames <= MAX_BMAPS_PER_GADGET)){                                       
292                 // seems pretty stupid that we didn't just use a variable for the first frame and access all
293                 // other frames offset from it instead of accessing this bmap_ids[] array, but probably too
294                 // much trouble to go through and change this anymore.  How sad..
295                 for ( i=1; i<m_num_frames; i++ ) {
296                         bmap_ids[i] = bmap_ids[0] + i;
297                 }       
298         }       
299
300         // flag that this control is using bitmaps for art      
301         uses_bmaps = 1;
302         return 0;
303 }
304
305 // Handle drawing of all children of the gadget.  Since this the base of all other gadgets,
306 // it doesn't have any look to it (kind of like a soul.  Can you see someone's soul?) and thus
307 // doesn't actually render itself.
308 //
309 void UI_GADGET::draw()
310 {
311         UI_GADGET *cur;
312
313         // if hidden, hide children as well
314         if (hidden){
315                 return;
316         }
317
318         if (children) {
319                 cur = children;
320                 do {
321                         cur->draw();
322                         cur = cur->next;
323
324                 } while (cur != children);
325         }
326 }
327
328 //      Free up bitmaps used by the gadget, and call children to destroy themselves as well.
329 //
330 void UI_GADGET::destroy()
331 {
332         int i;
333         UI_GADGET *cur;
334
335         for ( i=0; i<m_num_frames; i++ ) {
336                 if (bmap_ids[i] != -1) {
337                         bm_release(bmap_ids[i]);
338                         bmap_ids[i] = -1;
339                 }
340         }
341
342         if (children) {
343                 cur = children;
344                 do {
345                         cur->destroy();
346                         cur = cur->next;
347
348                 } while (cur != children);
349         }
350 }
351
352 // Use this if you need to change the size and position of a gadget after you have created it.
353 //
354 void UI_GADGET::update_dimensions(int _x, int _y, int _w, int _h)
355 {
356         if ( _x != -1 ) x = _x;
357         if ( _y != -1 ) y = _y;
358         if ( _w != -1 ) w = _w;
359         if ( _h != -1 ) h = _h; 
360 }
361
362 void UI_GADGET::get_dimensions(int *x_, int *y_, int *w_, int *h_)
363 {
364         *x_ = x;
365         *y_ = y;
366         *w_ = w;
367         *h_ = h;        
368 }
369
370 // Hide (or show) a gadget.
371 //  n != 0: Hide gadget
372 //  n == 0: Show gadget
373 //
374 void UI_GADGET::hide(int n)
375 {
376         hidden = n ? 1 : 0;
377 }
378
379 void UI_GADGET::unhide()
380 {
381         hidden = 0;
382 }
383
384 // Capture mouse with this gadget, which basically means only this gadget will get process()
385 // called on it until the mouse button 1 is released.
386 //
387 void UI_GADGET::capture_mouse()
388 {
389         my_wnd->capture_mouse(this);
390 }
391
392 // Check if (return true if):
393 //   mouse_captured():  this gadget has the mouse captured.
394 //   mouse_captured(x): gadget x has the mouse captured.
395 //
396 int UI_GADGET::mouse_captured(UI_GADGET *gadget)
397 {
398         if (!gadget)
399                 gadget = this;
400
401         return (my_wnd->mouse_captured_gadget == gadget);
402 }
403
404 // Set up the gadget and registers it with the UI window
405 //
406 void UI_GADGET::base_create(UI_WINDOW *wnd, int _kind, int _x, int _y, int _w, int _h)
407 {
408         int i;
409
410         // initialize data with passed values
411         kind = _kind;
412         x = _x;
413         y = _y;
414         w = _w;
415         h = _h;
416
417         // set up reference to UI window and initialize as having no family
418         my_wnd = wnd;
419         parent = NULL;
420         children = NULL;
421         next = prev = this;
422
423         // this actually links the gadget into the UI window's top level family (as the youngest gadget)
424         set_parent(NULL);
425
426         // initialize variables
427         hotkey = -1;
428         user_function = NULL;
429         disabled_flag = 0;
430         base_dragging = 0;
431         base_drag_x = base_drag_y = 0;
432         hotspot_num = -1;
433         hidden = 0;
434         linked_to_hotspot = 0;
435         uses_bmaps = 0;
436         m_num_frames = 0;
437         for ( i=0; i<MAX_BMAPS_PER_GADGET; i++ ) {
438                 bmap_ids[i] = -1;
439         }
440 }
441
442 void UI_GADGET::set_hotkey(int key)
443 {
444         hotkey = key;
445 }
446
447 // Far as I can tell, the callback function is handled differently for each gadget type, if
448 // handled by a given gadget type at all.
449 //
450 void UI_GADGET::set_callback(void (*_user_function)(void))
451 {
452         user_function = _user_function;
453 }
454
455 // get the next enabled gadget in sibling list or self if none
456 //
457 UI_GADGET *UI_GADGET::get_next()
458 {
459         UI_GADGET *tmp;
460
461         tmp = next;
462         while ((tmp != this) && tmp->disabled_flag)
463                 tmp = tmp->next;
464
465         return tmp;
466 }
467
468 // get the previous enabled gadget in sibling list or self if none
469 //
470 UI_GADGET *UI_GADGET::get_prev()
471 {
472         UI_GADGET *tmp;
473
474         tmp = prev;
475         while ((tmp != this) && tmp->disabled_flag)
476                 tmp = tmp->prev;
477
478         return tmp;
479 }
480
481 // Set this gadget as the focus (selected gadget) of the UI window
482 //
483 void UI_GADGET::set_focus()
484 {
485         my_wnd->selected_gadget = this;
486 }
487
488 // Make no gadget have focus in the UI window.
489 //
490 void UI_GADGET::clear_focus()
491 {
492         my_wnd->selected_gadget = NULL;
493 }
494
495 // Return true or false if this gadget currently has the focus
496 int UI_GADGET::has_focus()
497 {
498         return my_wnd->selected_gadget == this ? 1 : 0;
499 }
500
501 // get mouse pointer position relative to gadget's origin (UL corner)
502 //
503 void UI_GADGET::get_mouse_pos(int *xx, int *yy)
504 {
505         if (xx)
506                 *xx = ui_mouse.x - x;
507         if (yy)
508                 *yy = ui_mouse.y - y;
509 }
510
511 // Call process() for all children of gadget.  As a base gadget for all other gadget types,
512 // it doesn't actually do anything for itself.
513 //
514 void UI_GADGET::process(int focus)
515 {
516         UI_GADGET *tmp;
517
518         if (disabled_flag)
519                 return;
520
521         tmp = children;  // process all children of gadget
522         if (tmp) {
523                 do {
524                         tmp->process();
525                         tmp = tmp->next;
526
527                 } while (tmp != children);
528         }
529 }
530
531 // Check if the mouse is over the gadget's area or not,
532 //
533 int UI_GADGET::is_mouse_on()
534 {
535         int offset, pixel_val;
536         ubyte *mask_data;
537         int mask_w, mask_h;
538
539         // if linked to a hotspot, use the mask for determination
540         if (linked_to_hotspot) {
541                 mask_data = (ubyte*)my_wnd->get_mask_data(&mask_w, &mask_h);
542                 if ( mask_data == NULL ) {
543                         nprintf(("Warning", "No mask defined, but control is linked to hotspot\n"));
544                         Int3();
545                         return 0;
546                 }
547
548                 // if the mouse values are out of range of the bitmap
549                 // NOTE : this happens when using smaller mask bitmaps than the screen resolution (during development)
550                 if((ui_mouse.x >= mask_w) || (ui_mouse.y >= mask_h)){
551                         return 0;
552                 }
553
554                 // check the pixel value under the mouse
555                 offset = ui_mouse.y * mask_w + ui_mouse.x;
556                 pixel_val = *(mask_data + offset);
557                 if (pixel_val == hotspot_num){
558                         return 1;
559                 } else {
560                         return 0;
561                 }
562         // otherwise, we just check the bounding box area
563         } else {
564                 if ((ui_mouse.x >= x) && (ui_mouse.x < x + w) && (ui_mouse.y >= y) && (ui_mouse.y < y + h) ){
565                         return 1;
566                 } else {
567                         return 0;
568                 }
569         }
570 }
571
572 // check if mouse is over any child of this gadget
573 //
574 int UI_GADGET::is_mouse_on_children()
575 {
576         UI_GADGET *tmp;
577         
578         tmp = children;
579         if (tmp) {
580                 do {
581                         if (tmp->is_mouse_on())
582                                 return 1;
583                         if (tmp->is_mouse_on_children())
584                                 return 1;
585
586                         tmp = tmp->next;
587
588                 } while (tmp != children);
589         }
590
591         return 0;       
592 }
593
594 void UI_GADGET::disable()
595 {
596         disabled_flag = 1;
597 }
598
599 // Enables (or possibly disables) the gadget.  n is an optional argument.  If not supplied,
600 // enables the garget.  If supplied, enables garget if n is true, disables it if n is false.
601 //
602 void UI_GADGET::enable(int n)
603 {
604         disabled_flag = n ? 0 : 1;
605 }
606
607 // Check if gadget is disabled
608 //
609 int UI_GADGET::disabled()
610 {
611         return disabled_flag;
612 }
613
614 // Check if gadget is enabled
615 //
616 int UI_GADGET::enabled()
617 {
618         return !disabled_flag;
619 }
620
621 void UI_GADGET::drag_with_children( int dx, int dy )
622 {
623         UI_GADGET *tmp;
624
625         x = dx + base_start_x;
626         y = dy + base_start_y;
627         
628         tmp = children;
629         if (tmp) {
630                 do {
631                         tmp->drag_with_children(dx, dy);
632                         tmp = tmp->next;
633
634                 } while (tmp != children);
635         }
636 }
637
638 void UI_GADGET::start_drag_with_children()
639 {
640         UI_GADGET *tmp;
641
642         base_dragging = 1;
643         base_start_x = x;
644         base_start_y = y;
645         
646         tmp = children;
647         if (tmp) {
648                 do {
649                         tmp->start_drag_with_children();
650                         tmp = tmp->next;
651
652                 } while (tmp != children);
653         }
654 }
655
656 void UI_GADGET::stop_drag_with_children()
657 {
658         UI_GADGET *tmp;
659
660         base_dragging = 0;
661         tmp = children;
662         if (tmp) {
663                 do {
664                         tmp->stop_drag_with_children();
665                         tmp = tmp->next;
666
667                 } while (tmp != children);
668         }
669 }
670
671 // Returns 1 if moving
672 int UI_GADGET::check_move()
673 {
674         #if 0
675                 if ( parent != NULL ) return base_dragging;
676
677                 if ( !base_dragging )   {
678
679                         if ( B2_JUST_PRESSED )  {
680                                 if ( is_mouse_on() || is_mouse_on_children() ) {
681                                         start_drag_with_children();
682                                         base_drag_x = ui_mouse.x;
683                                         base_drag_y = ui_mouse.y;
684                                         return 1;
685                                 } else {
686                                         return 0;
687                                 }
688                         } else 
689                                 return 0;
690                 } else {
691                         drag_with_children(ui_mouse.x - base_drag_x,ui_mouse.y - base_drag_y);
692                         nprintf(( "UI", "UI: X=%d, Y=%d, Delta=(%d,%d)\n", x, y, (ui_mouse.x - base_drag_x), (ui_mouse.y - base_drag_y) ));
693                         if (B2_RELEASED)        {
694                                 stop_drag_with_children();
695                         }
696                         return 1;
697                 }
698         #endif
699         return 0;
700 }
701
702 // Take gadget out of family.  Children of this gadget stay with gadget, though.
703 //
704 // A family is basically the whole parent/sibling/children hierarchy for gadgets.  Any gadget
705 // that is linked to another gadget by one of these pointers is said to be in the same family
706 // as that gadget.  This function, therefore, caused all references to a gadget by it's
707 // family's gadgets' sibling or children pointers to be broken, and all references to any of them
708 // by this gadget's parent or sibling pointers to also be broken.  This isolates the gadget and
709 // it's children into a new family.
710 //
711 void UI_GADGET::remove_from_family()
712 {
713         if (parent) {
714                 if (parent->children == this) {
715                         if (next == this)  // an only child?
716                                 parent->children = NULL;  // if so, parent now has no children
717                         else
718                                 parent->children = next;  // next sibling is now the eldest
719                 }
720
721         } else {
722                 if (my_wnd->first_gadget == this) {
723                         if (next == this)  // an only child?
724                                 my_wnd->first_gadget = NULL;  // if so, parent now has no children
725                         else
726                                 my_wnd->first_gadget = next;  // next sibling is now the eldest
727                 }
728         }
729
730         parent = NULL;
731         if (next != this) {  // does gadget have siblings?
732                 next->prev = prev;
733                 prev->next = next;
734         }
735
736         next = prev = this;
737 }
738
739 // Put gadget into a new family (removing from old one if needed first).
740 // See remove_from_family() for definition of what a family is.
741 //
742 void UI_GADGET::set_parent(UI_GADGET *daddy)
743 {
744         remove_from_family();
745         parent = daddy;
746
747         if (!daddy) {
748                 if (!my_wnd->first_gadget) {
749                         my_wnd->first_gadget = this;
750
751                 } else {
752                         UI_GADGET *eldest_sibling, *youngest_sibling;
753
754                         eldest_sibling = my_wnd->first_gadget;
755                         youngest_sibling = eldest_sibling->prev;
756
757                         next = eldest_sibling;
758                         prev = youngest_sibling;
759
760                         eldest_sibling->prev = this;
761                         youngest_sibling->next = this;
762                 }
763
764                 return;
765         }
766
767         if (!daddy->children) {
768                 daddy->children = this;
769
770         } else {
771                 UI_GADGET *eldest_sibling, *youngest_sibling;
772
773                 eldest_sibling = daddy->children;
774                 youngest_sibling = eldest_sibling->prev;
775
776                 next = eldest_sibling;
777                 prev = youngest_sibling->prev;
778
779                 eldest_sibling->prev = this;
780                 youngest_sibling->next = this;
781         }
782 }
783