FS2_Open
Open source remastering of the Freespace 2 engine
checkbox.cpp
Go to the documentation of this file.
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 the
6  * source.
7  *
8 */
9 
10 
11 
12 #include "globalincs/alphacolors.h"
13 #include "ui/ui.h"
14 #include "ui/uidefs.h"
15 
16 void UI_CHECKBOX::create(UI_WINDOW *wnd, char *_text, int _x, int _y, int _state )
17 {
18  int _w, _h;
19 
20 // gr_get_string_size( &_w, &_h, "X" );
21  _w = 18;
22  _h = 18;
23 
24  if ( _text )
25  text = vm_strdup( _text );
26  else
27  text = NULL;
28 
29  base_create( wnd, UI_KIND_CHECKBOX, _x, _y, _w, _h );
30 
31  position = 0;
32  pressed_down = 0;
33  flag = _state;
34 }
35 
36 void UI_CHECKBOX::destroy()
37 {
38  if (text)
39  vm_free(text);
40 
42 }
43 
44 
45 void UI_CHECKBOX::draw()
46 {
47  int offset;
48 
49  if ( uses_bmaps ) {
50 
51  if ( disabled_flag ) {
52  if ( flag ) {
53  if ( bmap_ids[CBOX_DISABLED_MARKED] != -1 ) {
54  gr_set_bitmap(bmap_ids[CBOX_DISABLED_MARKED]);
56  }
57  }
58  else {
59  if ( bmap_ids[CBOX_DISABLED_CLEAR] != -1 ) {
60  gr_set_bitmap(bmap_ids[CBOX_DISABLED_CLEAR]);
62  }
63  }
64  }
65  else { // not disabled
66  if ( position == 0 ) { // up
67  if ( flag ) { // marked
68  if ( bmap_ids[CBOX_UP_MARKED] != -1 ) {
69  gr_set_bitmap(bmap_ids[CBOX_UP_MARKED]);
71  }
72  }
73  else { // not marked
74  if ( bmap_ids[CBOX_UP_CLEAR] != -1 ) {
75  gr_set_bitmap(bmap_ids[CBOX_UP_CLEAR]);
77  }
78  }
79  }
80  else { // down
81  if ( flag ) { // marked
82  if ( bmap_ids[CBOX_DOWN_MARKED] != -1 ) {
83  gr_set_bitmap(bmap_ids[CBOX_DOWN_MARKED]);
85  }
86  }
87  else { // not marked
88  if ( bmap_ids[CBOX_DOWN_CLEAR] != -1 ) {
89  gr_set_bitmap(bmap_ids[CBOX_DOWN_CLEAR]);
91  }
92  }
93  }
94  }
95 
96  } else {
99 
100  if (position == 0 ) {
101  ui_draw_box_out( 0, 0, w-1, h-1 );
102  offset = 0;
103 
104  } else {
105  ui_draw_box_in( 0, 0, w-1, h-1 );
106  offset = 1;
107  }
108 
109  if (disabled_flag)
111  else if (my_wnd->selected_gadget == this)
113  else
115 
116  if (flag)
117  ui_string_centered( Middle(w) + offset, Middle(h) + offset, "X" );
118  else
119  ui_string_centered( Middle(w) + offset, Middle(h) + offset, " " );
120 
121  if (text) {
122  gr_reset_clip();
123  gr_string( x+w+4, y+2, text, GR_RESIZE_MENU );
124  }
125  }
126 }
127 
128 void UI_CHECKBOX::process(int focus)
129 {
130  int OnMe, oldposition;
131 
132  if (disabled_flag) {
133  position = 0;
134  return;
135  }
136 
137  if (my_wnd->selected_gadget == this)
138  focus = 1;
139 
140  OnMe = is_mouse_on();
141 
142  oldposition = position;
143 
144  if ( B1_PRESSED && OnMe ) {
145  position = 1;
146  } else {
147  position = 0;
148  }
149 
150  if (my_wnd->keypress == hotkey ) {
151  position = 2;
152  my_wnd->last_keypress = 0;
153  }
154 
155  if ( focus && ((my_wnd->keypress == KEY_SPACEBAR) || (my_wnd->keypress == KEY_ENTER)) )
156  position = 2;
157 
158  if (focus)
159  if ( (oldposition == 2) && (keyd_pressed[KEY_SPACEBAR] || keyd_pressed[KEY_ENTER]) )
160  position = 2;
161 
162  pressed_down = 0;
163 
164  if (position == 0) {
165  if ( (oldposition == 1) && OnMe ){
166  pressed_down = 1;
167  }
168  if ( (oldposition == 2) && focus ){
169  pressed_down = 1;
170  }
171  }
172 
173  if (pressed_down && user_function ) {
174  user_function();
175  }
176 
177  if (pressed_down)
178  flag = !flag;
179 }
180 
182 {
183  return pressed_down;
184 }
185 
187 {
188  return flag;
189 }
190 
191 void UI_CHECKBOX::set_state(int _state)
192 {
193  flag = _state;
194 }
#define vm_free(ptr)
Definition: pstypes.h:548
int hotkey
Definition: ui.h:78
GLfloat GLfloat GLfloat GLfloat h
Definition: Glext.h:7280
int is_mouse_on()
Definition: gadget.cpp:371
#define GR_RESIZE_MENU
Definition: 2d.h:684
UI_WINDOW * my_wnd
Definition: ui.h:111
int bmap_ids[MAX_BMAPS_PER_GADGET]
Definition: ui.h:118
__inline void gr_string(int x, int y, const char *string, int resize_mode=GR_RESIZE_FULL)
Definition: 2d.h:769
void base_create(UI_WINDOW *wnd, int _kind, int _x, int _y, int _w, int _h)
Definition: gadget.cpp:244
int checked()
Definition: checkbox.cpp:186
void gr_set_color_fast(color *dst)
Definition: 2d.cpp:1197
void ui_draw_box_in(int x1, int y1, int x2, int y2)
Definition: uidraw.cpp:80
void gr_set_bitmap(int bitmap_num, int alphablend_mode, int bitblt_mode, float alpha)
Definition: 2d.cpp:2105
void ui_draw_box_out(int x1, int y1, int x2, int y2)
Definition: uidraw.cpp:69
void set_state(int _state)
Definition: checkbox.cpp:191
__inline void gr_set_clip(int x, int y, int w, int h, int resize_mode=GR_RESIZE_FULL)
Definition: 2d.h:741
#define gr_reset_clip
Definition: 2d.h:745
virtual void destroy()
Definition: gadget.cpp:160
GLintptr offset
Definition: Glext.h:5497
#define KEY_ENTER
Definition: key.h:125
#define CGRAY
Definition: uidefs.h:23
#define vm_strdup(ptr)
Definition: pstypes.h:549
GLint GLint GLint GLint GLint x
Definition: Glext.h:5182
int uses_bmaps
Definition: ui.h:93
#define CBRIGHT_GREEN
Definition: uidefs.h:22
#define B1_PRESSED
Definition: uidefs.h:47
void create(UI_WINDOW *wnd, char *_text, int _x, int _y, int _state)
Definition: checkbox.cpp:16
int disabled_flag
Definition: ui.h:82
GLubyte GLubyte GLubyte GLubyte w
Definition: Glext.h:5679
int last_keypress
Definition: ui.h:650
Definition: ui.h:584
#define UI_KIND_CHECKBOX
Definition: ui.h:20
ubyte keyd_pressed[NUM_KEYS]
Definition: key.cpp:42
#define KEY_SPACEBAR
Definition: key.h:128
void gr_bitmap(int _x, int _y, int resize_mode)
Definition: 2d.cpp:1303
#define CGREEN
Definition: uidefs.h:21
void ui_string_centered(int x, int y, char *s)
Definition: uidraw.cpp:28
void(* user_function)(void)
Definition: ui.h:81
void gr_set_font(int fontnum)
Definition: font.cpp:566
int keypress
Definition: ui.h:622
int f_id
Definition: ui.h:603
int changed()
Definition: checkbox.cpp:181
GLint y
Definition: Gl.h:1505
#define Middle(x)
Definition: uidefs.h:74
UI_GADGET * selected_gadget
Definition: ui.h:610