]> icculus.org git repositories - taylor/freespace2.git/blob - src/ui/gadget.cpp
fix a small bug in UI_GADGET that could leave a control animation loaded in memory...
[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         bm_filename = NULL;
183 }
184
185 // destructor
186 UI_GADGET::~UI_GADGET()
187 {
188 }
189
190 int UI_GADGET::get_hotspot()
191 {
192         if (!linked_to_hotspot)
193                 return -1;
194
195         return hotspot_num;
196 }
197
198 void UI_GADGET::reset()
199 {
200         m_flags = 0;
201 }
202
203 // --------------------------------------------------------------------
204 // Links a hotspot (palette index in mask) to the given gadget.
205 //
206 void UI_GADGET::link_hotspot(int num)
207 {
208         hotspot_num = num;
209         linked_to_hotspot = 1;
210 }
211
212 // --------------------------------------------------------------------
213 // Extract MAX_BMAPS_PER_CONTROL bitmaps for the different states of the control.
214 //
215 // The bitmap ids are stored in the bmap_ids[] array.  If you don't want to store
216 // from the zero index, fill in the offset parameter.  offset is a default parameter
217 // with a default value of zero.
218 //
219 // NOTE:  The bitmaps stored in a .ani file.  What each frame is used for
220 //                       is left up to the component to decide.
221 //
222
223 // loads nframes bitmaps, starting at index start_frame.
224 // anything < start_frame will not be loaded.
225 // this keeps the loading code from trying to load bitmaps which don't exist
226 // and taking an unnecessary disk hit.          
227 int UI_GADGET::set_bmaps(char *ani_fname, int nframes, int start_frame)
228 {
229         int i;  
230 #ifndef MAKE_FS1
231         char full_name[MAX_FILENAME_LEN] = "";
232         char tmp[10];
233         int num_digits;
234         int its_all_good = 0;
235         int s_idx;
236 #endif
237         int idx;        
238         
239         // clear out all frames
240         for(idx=0; idx<MAX_BMAPS_PER_GADGET; idx++){
241                 bmap_ids[idx] = -1;
242         }
243         
244         // load all the bitmaps
245         bm_filename = ani_fname;
246
247         Assert(nframes < MAX_BMAPS_PER_GADGET);         
248         m_num_frames = nframes;         
249 #ifndef MAKE_FS1
250         // FS1 uses real anis instead of frame based pcxs so this code just slows down
251         // searching and therefore loading
252         for(idx=start_frame; idx<nframes; idx++){
253                 // clear the string
254                 strcpy(full_name, "");
255
256                 // get the # of digits for this index
257                 num_digits = (idx < 10) ? 1 : (idx < 100) ? 2 : (idx < 1000) ? 3 : 4;
258
259                 // build the actual filename
260                 strcpy(full_name, ani_fname);           
261                 for(s_idx=0; s_idx<(4-num_digits); s_idx++){
262                         strcat(full_name, NOX("0"));
263                 }
264 #ifdef PLAT_UNIX
265                 sprintf(tmp, "%d", idx);
266                 strcat(full_name, tmp);
267 #else
268                 strcat(full_name, itoa(idx, tmp, 10));          
269 #endif
270
271                 // try and load the bitmap                              
272                 bmap_ids[idx] = bm_load(full_name);     
273                 if(bmap_ids[idx] != -1){                
274                         // bm_lock(bmap_ids[idx], 16, 0);
275                         // bm_unlock(bmap_ids[idx]);
276                         
277                         its_all_good = 1;
278                 } else {
279                         // mprintf(("Skipping %s\n", full_name));
280                         // its_all_good = 0;
281                 }
282         }
283
284         // done
285         if(its_all_good){
286                 uses_bmaps = 1;         
287                 return 0;
288         }
289 #endif
290
291         // no go, so try and load as an ani. try and load as an .ani    
292         bmap_ids[0] = bm_load_animation(ani_fname, &m_num_frames);      
293         if((bmap_ids[0] >= 0) && (m_num_frames <= MAX_BMAPS_PER_GADGET)){                                       
294                 // seems pretty stupid that we didn't just use a variable for the first frame and access all
295                 // other frames offset from it instead of accessing this bmap_ids[] array, but probably too
296                 // much trouble to go through and change this anymore.  How sad..
297                 for ( i=1; i<m_num_frames; i++ ) {
298                         bmap_ids[i] = bmap_ids[0] + i;
299                 }       
300         }       
301
302         // flag that this control is using bitmaps for art      
303         uses_bmaps = 1;
304         return 0;
305 }
306
307 // Handle drawing of all children of the gadget.  Since this the base of all other gadgets,
308 // it doesn't have any look to it (kind of like a soul.  Can you see someone's soul?) and thus
309 // doesn't actually render itself.
310 //
311 void UI_GADGET::draw()
312 {
313         UI_GADGET *cur;
314
315         // if hidden, hide children as well
316         if (hidden){
317                 return;
318         }
319
320         if (children) {
321                 cur = children;
322                 do {
323                         cur->draw();
324                         cur = cur->next;
325
326                 } while (cur != children);
327         }
328 }
329
330 //      Free up bitmaps used by the gadget, and call children to destroy themselves as well.
331 //
332 void UI_GADGET::destroy()
333 {
334         int i;
335         UI_GADGET *cur;
336
337         for ( i=0; i<m_num_frames; i++ ) {
338                 if (bmap_ids[i] != -1) {
339                         bm_release(bmap_ids[i]);
340                         bmap_ids[i] = -1;
341                 }
342         }
343
344         if (children) {
345                 cur = children;
346                 do {
347                         cur->destroy();
348                         cur = cur->next;
349
350                 } while (cur != children);
351         }
352 }
353
354 // Use this if you need to change the size and position of a gadget after you have created it.
355 //
356 void UI_GADGET::update_dimensions(int _x, int _y, int _w, int _h)
357 {
358         if ( _x != -1 ) x = _x;
359         if ( _y != -1 ) y = _y;
360         if ( _w != -1 ) w = _w;
361         if ( _h != -1 ) h = _h; 
362 }
363
364 void UI_GADGET::get_dimensions(int *x_, int *y_, int *w_, int *h_)
365 {
366         *x_ = x;
367         *y_ = y;
368         *w_ = w;
369         *h_ = h;        
370 }
371
372 // Hide (or show) a gadget.
373 //  n != 0: Hide gadget
374 //  n == 0: Show gadget
375 //
376 void UI_GADGET::hide(int n)
377 {
378         hidden = n ? 1 : 0;
379 }
380
381 void UI_GADGET::unhide()
382 {
383         hidden = 0;
384 }
385
386 // Capture mouse with this gadget, which basically means only this gadget will get process()
387 // called on it until the mouse button 1 is released.
388 //
389 void UI_GADGET::capture_mouse()
390 {
391         my_wnd->capture_mouse(this);
392 }
393
394 // Check if (return true if):
395 //   mouse_captured():  this gadget has the mouse captured.
396 //   mouse_captured(x): gadget x has the mouse captured.
397 //
398 int UI_GADGET::mouse_captured(UI_GADGET *gadget)
399 {
400         if (!gadget)
401                 gadget = this;
402
403         return (my_wnd->mouse_captured_gadget == gadget);
404 }
405
406 // Set up the gadget and registers it with the UI window
407 //
408 void UI_GADGET::base_create(UI_WINDOW *wnd, int _kind, int _x, int _y, int _w, int _h)
409 {
410         int i;
411
412         // initialize data with passed values
413         kind = _kind;
414         x = _x;
415         y = _y;
416         w = _w;
417         h = _h;
418
419         // set up reference to UI window and initialize as having no family
420         my_wnd = wnd;
421         parent = NULL;
422         children = NULL;
423         next = prev = this;
424
425         // this actually links the gadget into the UI window's top level family (as the youngest gadget)
426         set_parent(NULL);
427
428         // initialize variables
429         hotkey = -1;
430         user_function = NULL;
431         disabled_flag = 0;
432         base_dragging = 0;
433         base_drag_x = base_drag_y = 0;
434         hotspot_num = -1;
435         hidden = 0;
436         linked_to_hotspot = 0;
437         uses_bmaps = 0;
438         m_num_frames = 0;
439         for ( i=0; i<MAX_BMAPS_PER_GADGET; i++ ) {
440                 bmap_ids[i] = -1;
441         }
442 }
443
444 void UI_GADGET::set_hotkey(int key)
445 {
446         hotkey = key;
447 }
448
449 // Far as I can tell, the callback function is handled differently for each gadget type, if
450 // handled by a given gadget type at all.
451 //
452 void UI_GADGET::set_callback(void (*_user_function)(void))
453 {
454         user_function = _user_function;
455 }
456
457 // get the next enabled gadget in sibling list or self if none
458 //
459 UI_GADGET *UI_GADGET::get_next()
460 {
461         UI_GADGET *tmp;
462
463         tmp = next;
464         while ((tmp != this) && tmp->disabled_flag)
465                 tmp = tmp->next;
466
467         return tmp;
468 }
469
470 // get the previous enabled gadget in sibling list or self if none
471 //
472 UI_GADGET *UI_GADGET::get_prev()
473 {
474         UI_GADGET *tmp;
475
476         tmp = prev;
477         while ((tmp != this) && tmp->disabled_flag)
478                 tmp = tmp->prev;
479
480         return tmp;
481 }
482
483 // Set this gadget as the focus (selected gadget) of the UI window
484 //
485 void UI_GADGET::set_focus()
486 {
487         my_wnd->selected_gadget = this;
488 }
489
490 // Make no gadget have focus in the UI window.
491 //
492 void UI_GADGET::clear_focus()
493 {
494         my_wnd->selected_gadget = NULL;
495 }
496
497 // Return true or false if this gadget currently has the focus
498 int UI_GADGET::has_focus()
499 {
500         return my_wnd->selected_gadget == this ? 1 : 0;
501 }
502
503 // get mouse pointer position relative to gadget's origin (UL corner)
504 //
505 void UI_GADGET::get_mouse_pos(int *xx, int *yy)
506 {
507         if (xx)
508                 *xx = ui_mouse.x - x;
509         if (yy)
510                 *yy = ui_mouse.y - y;
511 }
512
513 // Call process() for all children of gadget.  As a base gadget for all other gadget types,
514 // it doesn't actually do anything for itself.
515 //
516 void UI_GADGET::process(int focus)
517 {
518         UI_GADGET *tmp;
519
520         if (disabled_flag)
521                 return;
522
523         tmp = children;  // process all children of gadget
524         if (tmp) {
525                 do {
526                         tmp->process();
527                         tmp = tmp->next;
528
529                 } while (tmp != children);
530         }
531 }
532
533 // Check if the mouse is over the gadget's area or not,
534 //
535 int UI_GADGET::is_mouse_on()
536 {
537         int offset, pixel_val;
538         ubyte *mask_data;
539         int mask_w, mask_h;
540
541         // if linked to a hotspot, use the mask for determination
542         if (linked_to_hotspot) {
543                 mask_data = (ubyte*)my_wnd->get_mask_data(&mask_w, &mask_h);
544                 if ( mask_data == NULL ) {
545                         nprintf(("Warning", "No mask defined, but control is linked to hotspot\n"));
546                         Int3();
547                         return 0;
548                 }
549
550                 // if the mouse values are out of range of the bitmap
551                 // NOTE : this happens when using smaller mask bitmaps than the screen resolution (during development)
552                 if((ui_mouse.x >= mask_w) || (ui_mouse.y >= mask_h)){
553                         return 0;
554                 }
555
556                 // check the pixel value under the mouse
557                 offset = ui_mouse.y * mask_w + ui_mouse.x;
558                 pixel_val = *(mask_data + offset);
559                 if (pixel_val == hotspot_num){
560                         return 1;
561                 } else {
562                         return 0;
563                 }
564         // otherwise, we just check the bounding box area
565         } else {
566                 if ((ui_mouse.x >= x) && (ui_mouse.x < x + w) && (ui_mouse.y >= y) && (ui_mouse.y < y + h) ){
567                         return 1;
568                 } else {
569                         return 0;
570                 }
571         }
572 }
573
574 // check if mouse is over any child of this gadget
575 //
576 int UI_GADGET::is_mouse_on_children()
577 {
578         UI_GADGET *tmp;
579         
580         tmp = children;
581         if (tmp) {
582                 do {
583                         if (tmp->is_mouse_on())
584                                 return 1;
585                         if (tmp->is_mouse_on_children())
586                                 return 1;
587
588                         tmp = tmp->next;
589
590                 } while (tmp != children);
591         }
592
593         return 0;       
594 }
595
596 void UI_GADGET::disable()
597 {
598         disabled_flag = 1;
599 }
600
601 // Enables (or possibly disables) the gadget.  n is an optional argument.  If not supplied,
602 // enables the garget.  If supplied, enables garget if n is true, disables it if n is false.
603 //
604 void UI_GADGET::enable(int n)
605 {
606         disabled_flag = n ? 0 : 1;
607 }
608
609 // Check if gadget is disabled
610 //
611 int UI_GADGET::disabled()
612 {
613         return disabled_flag;
614 }
615
616 // Check if gadget is enabled
617 //
618 int UI_GADGET::enabled()
619 {
620         return !disabled_flag;
621 }
622
623 void UI_GADGET::drag_with_children( int dx, int dy )
624 {
625         UI_GADGET *tmp;
626
627         x = dx + base_start_x;
628         y = dy + base_start_y;
629         
630         tmp = children;
631         if (tmp) {
632                 do {
633                         tmp->drag_with_children(dx, dy);
634                         tmp = tmp->next;
635
636                 } while (tmp != children);
637         }
638 }
639
640 void UI_GADGET::start_drag_with_children()
641 {
642         UI_GADGET *tmp;
643
644         base_dragging = 1;
645         base_start_x = x;
646         base_start_y = y;
647         
648         tmp = children;
649         if (tmp) {
650                 do {
651                         tmp->start_drag_with_children();
652                         tmp = tmp->next;
653
654                 } while (tmp != children);
655         }
656 }
657
658 void UI_GADGET::stop_drag_with_children()
659 {
660         UI_GADGET *tmp;
661
662         base_dragging = 0;
663         tmp = children;
664         if (tmp) {
665                 do {
666                         tmp->stop_drag_with_children();
667                         tmp = tmp->next;
668
669                 } while (tmp != children);
670         }
671 }
672
673 // Returns 1 if moving
674 int UI_GADGET::check_move()
675 {
676         #if 0
677                 if ( parent != NULL ) return base_dragging;
678
679                 if ( !base_dragging )   {
680
681                         if ( B2_JUST_PRESSED )  {
682                                 if ( is_mouse_on() || is_mouse_on_children() ) {
683                                         start_drag_with_children();
684                                         base_drag_x = ui_mouse.x;
685                                         base_drag_y = ui_mouse.y;
686                                         return 1;
687                                 } else {
688                                         return 0;
689                                 }
690                         } else 
691                                 return 0;
692                 } else {
693                         drag_with_children(ui_mouse.x - base_drag_x,ui_mouse.y - base_drag_y);
694                         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) ));
695                         if (B2_RELEASED)        {
696                                 stop_drag_with_children();
697                         }
698                         return 1;
699                 }
700         #endif
701         return 0;
702 }
703
704 // Take gadget out of family.  Children of this gadget stay with gadget, though.
705 //
706 // A family is basically the whole parent/sibling/children hierarchy for gadgets.  Any gadget
707 // that is linked to another gadget by one of these pointers is said to be in the same family
708 // as that gadget.  This function, therefore, caused all references to a gadget by it's
709 // family's gadgets' sibling or children pointers to be broken, and all references to any of them
710 // by this gadget's parent or sibling pointers to also be broken.  This isolates the gadget and
711 // it's children into a new family.
712 //
713 void UI_GADGET::remove_from_family()
714 {
715         if (parent) {
716                 if (parent->children == this) {
717                         if (next == this)  // an only child?
718                                 parent->children = NULL;  // if so, parent now has no children
719                         else
720                                 parent->children = next;  // next sibling is now the eldest
721                 }
722
723         } else {
724                 if (my_wnd->first_gadget == this) {
725                         if (next == this)  // an only child?
726                                 my_wnd->first_gadget = NULL;  // if so, parent now has no children
727                         else
728                                 my_wnd->first_gadget = next;  // next sibling is now the eldest
729                 }
730         }
731
732         parent = NULL;
733         if (next != this) {  // does gadget have siblings?
734                 next->prev = prev;
735                 prev->next = next;
736         }
737
738         next = prev = this;
739 }
740
741 // Put gadget into a new family (removing from old one if needed first).
742 // See remove_from_family() for definition of what a family is.
743 //
744 void UI_GADGET::set_parent(UI_GADGET *daddy)
745 {
746         remove_from_family();
747         parent = daddy;
748
749         if (!daddy) {
750                 if (!my_wnd->first_gadget) {
751                         my_wnd->first_gadget = this;
752
753                 } else {
754                         UI_GADGET *eldest_sibling, *youngest_sibling;
755
756                         eldest_sibling = my_wnd->first_gadget;
757                         youngest_sibling = eldest_sibling->prev;
758
759                         next = eldest_sibling;
760                         prev = youngest_sibling;
761
762                         eldest_sibling->prev = this;
763                         youngest_sibling->next = this;
764                 }
765
766                 return;
767         }
768
769         if (!daddy->children) {
770                 daddy->children = this;
771
772         } else {
773                 UI_GADGET *eldest_sibling, *youngest_sibling;
774
775                 eldest_sibling = daddy->children;
776                 youngest_sibling = eldest_sibling->prev;
777
778                 next = eldest_sibling;
779                 prev = youngest_sibling->prev;
780
781                 eldest_sibling->prev = this;
782                 youngest_sibling->next = this;
783         }
784 }
785