FS2_Open
Open source remastering of the Freespace 2 engine
scroll.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 "io/timer.h"
14 #include "ui/ui.h"
15 #include "ui/uidefs.h"
16 
17 
18 
19 // --------------------------------------------------------------------
20 // UI_SCROLLBAR::link_hotspot
21 //
22 //
23 void UI_SCROLLBAR::link_hotspot(int up_button_num, int down_button_num)
24 {
25  up_button.link_hotspot(up_button_num);
26  down_button.link_hotspot(down_button_num);
27 }
28 
29 // --------------------------------------------------------------------
30 // UI_SCROLLBAR::set_bmaps
31 //
32 // Call the UI_GADGET::set_bmaps() function for the child components
33 // of a scroll bar (the up and down button). Set up the bmaps for the
34 // line itself.
35 //
36 // We also need to get the dimensions of the bitmap button so we can update
37 // the dimensions of the scrollbar.
38 //
39 // returns: -1 ==> error
40 // 0 ==> success
41 //
42 int UI_SCROLLBAR::set_bmaps(char *up_button_fname, char *down_button_fname, char *line_fname)
43 {
44  int bx, by, bh, bw;
45 
46  up_button.set_bmaps(up_button_fname);
47  down_button.set_bmaps(down_button_fname);
48  up_button.get_dimensions(&bx, &by, &bw, &bh);
49 
50  // set the bitmaps for the rectangle that is the scrollbar itself
51  ((UI_GADGET*)this)->set_bmaps(line_fname);
52  uses_bmaps = 1;
53 
54  update_dimensions(x,y+bw,bw,h-bw*2);
55 
56  return 0;
57 }
58 
60 {
61  hidden = 1;
62  up_button.hide();
63  down_button.hide();
64 }
65 
67 {
68  hidden = 0;
69  up_button.unhide();
70  down_button.unhide();
71 }
72 
73 void UI_SCROLLBAR::create(UI_WINDOW *wnd, int _x, int _y, int _h, int _start, int _stop, int _position, int _window_size)
74 {
75  char *up = "^";
76  char *down = "v";
77  int bw = 20;
78 
79  base_create( wnd, UI_KIND_SCROLLBAR, _x, _y + bw, bw, _h - bw * 2 );
80 
81  up_button.create( wnd, up, _x, _y, bw, bw, 1 );
82  up_button.set_parent(this);
83  up_button.set_hotkey_if_focus(KEY_UP);
84 
85  down_button.create( wnd, down, _x, _y + _h - bw, bw, bw, 1 );
86  down_button.set_parent(this);
87  down_button.set_hotkey_if_focus(KEY_DOWN);
88 
89  horz = 0;
90  start = _start;
91  stop = _stop;
92  position = _position;
93  window_size = _window_size;
94  bar_length = h;
95  bar_position = 0;
96 
97  Assert( stop >= 0 );
98 
99  if (stop != start)
100  bar_size = (window_size * h) / (stop - start + window_size + 1);
101  else
102  bar_size = h;
103 
104  if (bar_size < 7)
105  bar_size = 7;
106 
107  bar_position = position - start;
108  bar_position *= h - bar_size;
109  bar_position /= stop - start;
110 
111  dragging = 0;
112  last_scrolled = 0;
113  moved = 1;
114 }
115 
116 void UI_SCROLLBAR::draw()
117 {
118  UI_GADGET::draw();
119 
120  if (uses_bmaps) {
121  gr_reset_clip();
122  if (disabled_flag) {
123  if ( bmap_ids[SB_DISABLED] != -1 ) {
124  gr_set_bitmap(bmap_ids[SB_DISABLED]);
126  }
127 
128  } else {
129  if ( bmap_ids[SB_NORMAL] != -1 ) {
130  gr_set_bitmap(bmap_ids[SB_NORMAL]);
132  }
133  }
134 
135  gr_set_clip( x, y, w, h, GR_RESIZE_MENU );
136  ui_draw_box_out( 0, bar_position, w - 1, bar_position + bar_size - 1 );
137 
138  } else {
140  gr_set_clip( x, y, w, h, GR_RESIZE_MENU );
141 
142  if (my_wnd->selected_gadget == this)
144  else
146 
147  ui_rect( 0, 0, w-1, h-1 );
148  ui_draw_box_out( 0, bar_position, w - 1, bar_position + bar_size - 1 );
149  }
150 }
151 
152 void UI_SCROLLBAR::process(int focus)
153 {
154  int OnMe, OnSlider;
155  int op;
156 
157  moved = 0;
158  if (disabled_flag) {
159  return;
160  }
161 
162  if (my_wnd->selected_gadget == this)
163  focus = 1;
164 
165  up_button.process(focus);
166  down_button.process(focus);
167 
168  if (start == stop) {
169  position = 0;
170  bar_position = 0;
171  return;
172  }
173 
174  op = position;
175 
176  if (up_button.pressed()) {
177  position--;
178  if (position < start)
179  position = start;
180 
181  bar_position = position - start;
182  bar_position *= h - bar_size;
183  bar_position /= stop - start;
184  set_focus();
185  }
186 
187  if (down_button.pressed()) {
188  position++;
189  if (position > stop)
190  position = stop;
191 
192  bar_position = position - start;
193  bar_position *= h - bar_size;
194  bar_position /= stop - start;
195  set_focus();
196  }
197 
198  OnMe = is_mouse_on();
199 
200  if (!B1_PRESSED)
201  dragging = 0;
202 
203  OnSlider = 0;
204  if ( (ui_mouse.y >= bar_position + y) && (ui_mouse.y < bar_position + y + bar_size) && OnMe )
205  OnSlider = 1;
206 
207  if (B1_JUST_PRESSED && OnSlider) {
208  dragging = 1;
209  drag_x = ui_mouse.x;
210  drag_y = ui_mouse.y;
211  drag_starting = bar_position;
212  set_focus();
213  }
214 
215  if ( B1_PRESSED && OnMe && !OnSlider && (timer_get_milliseconds() > last_scrolled + 1000 / (18*4)) ) {
216  last_scrolled = timer_get_milliseconds();
217 
218  if ( ui_mouse.y < bar_position+y ) {
219  // Page Up
220  position -= window_size;
221  if (position < start)
222  position = start;
223 
224  } else {
225  // Page Down
226  position += window_size;
227  if (position > stop)
228  position = stop;
229  }
230 
231  bar_position = position - start;
232  bar_position *= h - bar_size;
233  bar_position /= stop - start;
234  set_focus();
235  }
236 
237  if (B1_PRESSED && dragging) {
238  bar_position = drag_starting + ui_mouse.y - drag_y;
239 
240  if (bar_position < 0) {
241  bar_position = 0;
242  }
243 
244  if (bar_position > h - bar_size) {
245  bar_position = h - bar_size;
246  }
247 
248  position = bar_position;
249  position *= stop - start;
250  position /= h - bar_size;
251  position += start;
252 
253  if (position > stop)
254  position = stop;
255 
256  if (position < start)
257  position = start;
258 
259  set_focus();
260  }
261 
262  if (op != position)
263  moved = 1;
264  else
265  moved = 0;
266 }
267 
269 {
270  return position;
271 }
272 
274 {
275  return moved;
276 }
#define KEY_DOWN
Definition: key.h:180
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
int changed()
Definition: scroll.cpp:273
UI_WINDOW * my_wnd
Definition: ui.h:111
int bmap_ids[MAX_BMAPS_PER_GADGET]
Definition: ui.h:118
virtual void unhide()
Definition: gadget.cpp:217
void hide()
Definition: scroll.cpp:59
Assert(pm!=NULL)
Definition: ui.h:61
void set_focus()
Definition: gadget.cpp:321
void base_create(UI_WINDOW *wnd, int _kind, int _x, int _y, int _w, int _h)
Definition: gadget.cpp:244
int h
Definition: ui.h:79
virtual void hide(int n)
Definition: gadget.cpp:207
void gr_set_color_fast(color *dst)
Definition: 2d.cpp:1197
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 create(UI_WINDOW *wnd, int _x, int _y, int _h, int _start, int _stop, int _position, int _window_size)
Definition: scroll.cpp:73
UI_MOUSE ui_mouse
Definition: uimouse.cpp:17
#define B1_JUST_PRESSED
Definition: uidefs.h:49
__inline void gr_set_clip(int x, int y, int w, int h, int resize_mode=GR_RESIZE_FULL)
Definition: 2d.h:741
int y
Definition: uidefs.h:59
#define gr_reset_clip
Definition: 2d.h:745
int set_bmaps(char *ani_filename, int nframes=3, int start_frame=1)
Definition: gadget.cpp:71
void set_hotkey_if_focus(int key)
Definition: button.cpp:73
void get_dimensions(int *_x, int *_y, int *_w, int *_h)
Definition: gadget.cpp:195
void link_hotspot(int up_button_num, int down_button_num)
Definition: scroll.cpp:23
int pressed()
Definition: button.cpp:325
#define CGRAY
Definition: uidefs.h:23
#define UI_KIND_SCROLLBAR
Definition: ui.h:22
GLint GLint GLint GLint GLint x
Definition: Glext.h:5182
int uses_bmaps
Definition: ui.h:93
#define CBRIGHT_GREEN
Definition: uidefs.h:22
int x
Definition: uidefs.h:59
GLuint start
Definition: Gl.h:1502
#define B1_PRESSED
Definition: uidefs.h:47
int hidden
Definition: ui.h:86
GLbyte by
Definition: Glext.h:8237
int disabled_flag
Definition: ui.h:82
void update_dimensions(int _x, int _y, int _w, int _h)
Definition: gadget.cpp:187
int set_bmaps(char *up_button_fname, char *down_button_fname, char *line_fname)
Definition: scroll.cpp:42
void set_parent(UI_GADGET *_parent)
Definition: gadget.cpp:580
void link_hotspot(int num)
Definition: gadget.cpp:50
void create(UI_WINDOW *wnd, char *_text, int _x, int _y, int _w, int _h, int do_repeat=0, int ignore_focus=0)
Definition: button.cpp:26
GLubyte GLubyte GLubyte GLubyte w
Definition: Glext.h:5679
Definition: ui.h:584
void ui_rect(int x1, int y1, int x2, int y2)
Definition: uidraw.cpp:64
void gr_bitmap(int _x, int _y, int resize_mode)
Definition: 2d.cpp:1303
#define KEY_UP
Definition: key.h:179
void unhide()
Definition: scroll.cpp:66
void gr_set_font(int fontnum)
Definition: font.cpp:566
int getpos()
Definition: scroll.cpp:268
int timer_get_milliseconds()
Definition: timer.cpp:150
int f_id
Definition: ui.h:603
virtual void draw()
Definition: gadget.cpp:139
GLint y
Definition: Gl.h:1505
UI_GADGET * selected_gadget
Definition: ui.h:610