Subversion Repositories Games.Descent

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 pmbaty 1
/*
2
 * Portions of this file are copyright Rebirth contributors and licensed as
3
 * described in COPYING.txt.
4
 * Portions of this file are copyright Parallax Software and licensed
5
 * according to the Parallax license below.
6
 * See COPYING.txt for license details.
7
 
8
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
9
SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
10
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
11
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
12
IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
13
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
14
FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
15
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
16
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
17
COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
18
*/
19
 
20
 
21
#include <stdlib.h>
22
 
23
#include "maths.h"
24
#include "pstypes.h"
25
#include "event.h"
26
#include "gr.h"
27
#include "ui.h"
28
#include "mouse.h"
29
#include "key.h"
30
#include "timer.h"
31
 
32
namespace dcx {
33
 
34
void ui_draw_scrollbar( UI_DIALOG *dlg, UI_GADGET_SCROLLBAR * scrollbar )
35
{
36
#if 0  //ndef OGL
37
        if (scrollbar->status==0)
38
                return;
39
#endif
40
 
41
        scrollbar->status = 0;
42
        gr_set_current_canvas( scrollbar->canvas );
43
        auto &canvas = *grd_curcanv;
44
 
45
        const auto color = (dlg->keyboard_focus_gadget == scrollbar)
46
                ? CRED
47
                : CGREY;
48
 
49
        gr_rect(canvas, 0, 0, scrollbar->width - 1, scrollbar->fake_position - 1, color);
50
        gr_rect(canvas, 0, scrollbar->fake_position + scrollbar->fake_size, scrollbar->width - 1, scrollbar->height - 1, color);
51
        ui_draw_box_out(canvas, 0, scrollbar->fake_position, scrollbar->width - 1, scrollbar->fake_position + scrollbar->fake_size - 1);
52
}
53
 
54
std::unique_ptr<UI_GADGET_SCROLLBAR> ui_add_gadget_scrollbar(UI_DIALOG * dlg, short x, short y, short w, short h, int start, int stop, int position, int window_size)
55
{
56
        int tw;
57
 
58
        auto &up = "\x1e";
59
        auto &down = "\x1f";
60
 
61
        gr_get_string_size(*grd_curcanv->cv_font, up, &tw, nullptr, nullptr);
62
 
63
        w = tw + 10;
64
 
65
        if (stop < start ) stop = start;
66
 
67
        std::unique_ptr<UI_GADGET_SCROLLBAR> scrollbar{ui_gadget_add<UI_GADGET_SCROLLBAR>(dlg, x, y+w, x+w-1, y+h-w-1)};
68
 
69
        scrollbar->up_button = ui_add_gadget_button( dlg, x, y, w, w, up, NULL );
70
        scrollbar->up_button->parent = scrollbar.get();
71
 
72
        scrollbar->down_button =ui_add_gadget_button( dlg, x, y+h-w, w, w, down, NULL );
73
        scrollbar->down_button->parent = scrollbar.get();
74
 
75
        scrollbar->horz = 0;
76
        scrollbar->width = scrollbar->x2-scrollbar->x1+1;
77
        scrollbar->height = scrollbar->y2-scrollbar->y1+1;
78
        scrollbar->start = start;
79
        scrollbar->stop = stop;
80
        scrollbar->position = position;
81
        scrollbar->window_size = window_size;
82
        scrollbar->fake_length = scrollbar->height;
83
        scrollbar->fake_position =  0;
84
        if (stop!=start)
85
                scrollbar->fake_size = (window_size * scrollbar->height)/(stop-start+1+window_size);
86
        else
87
                scrollbar->fake_size = scrollbar->height;
88
 
89
        if (scrollbar->fake_size < 7) scrollbar->fake_size = 7;
90
        scrollbar->dragging = 0;
91
        scrollbar->moved=0;
92
        scrollbar->last_scrolled = 0;
93
        return scrollbar;
94
 
95
}
96
 
97
window_event_result ui_scrollbar_do( UI_DIALOG *dlg, UI_GADGET_SCROLLBAR * scrollbar,const d_event &event )
98
{
99
        int OnMe, OnSlider, keyfocus;
100
        int oldpos, op;
101
        int x, y, z;
102
        window_event_result rval = window_event_result::ignored;
103
 
104
        if (event.type == EVENT_WINDOW_DRAW)
105
        {
106
                ui_draw_scrollbar( dlg, scrollbar );
107
                return window_event_result::ignored;
108
        }
109
 
110
        keyfocus = 0;
111
 
112
        if (dlg->keyboard_focus_gadget==scrollbar)
113
                keyfocus = 1;
114
 
115
        if (scrollbar->start==scrollbar->stop)
116
        {
117
                scrollbar->position = 0;
118
                scrollbar->fake_position = 0;
119
                ui_draw_scrollbar( dlg, scrollbar );
120
                return window_event_result::ignored;
121
        }
122
 
123
        op = scrollbar->position;
124
 
125
        oldpos = scrollbar->fake_position;
126
 
127
        scrollbar->moved = 0;
128
 
129
 
130
        if (keyfocus && event.type == EVENT_KEY_COMMAND)
131
        {
132
                int key;
133
 
134
                key = event_key_get(event);
135
 
136
                if (key & KEY_UP)
137
                {
138
                        scrollbar->up_button->position = 2;
139
                        rval = window_event_result::handled;
140
                }
141
                else if (key & KEY_DOWN)
142
                {
143
                        scrollbar->down_button->position = 2;
144
                        rval = window_event_result::handled;
145
                }
146
        }
147
        else if (keyfocus && event.type == EVENT_KEY_RELEASE)
148
        {
149
                int key;
150
 
151
                key = event_key_get(event);
152
 
153
                if (key & KEY_UP)
154
                {
155
                        scrollbar->up_button->position = 0;
156
                        rval = window_event_result::handled;
157
                }
158
                else if (key & KEY_DOWN)
159
                {
160
                        scrollbar->down_button->position = 0;
161
                        rval = window_event_result::handled;
162
                }
163
        }
164
 
165
        if (scrollbar->up_button->position!=0)
166
        {
167
                if (timer_query() > scrollbar->last_scrolled + 1)
168
                {
169
                        scrollbar->last_scrolled = timer_query();
170
                        scrollbar->position--;
171
                        if (scrollbar->position < scrollbar->start )
172
                                scrollbar->position = scrollbar->start;
173
                        scrollbar->fake_position = scrollbar->position-scrollbar->start;
174
                        scrollbar->fake_position *= scrollbar->height-scrollbar->fake_size;
175
                        scrollbar->fake_position /= (scrollbar->stop-scrollbar->start);
176
                }
177
        }
178
 
179
        if (scrollbar->down_button->position!=0)
180
        {
181
                if (timer_query() > scrollbar->last_scrolled + 1)
182
                {
183
                        scrollbar->last_scrolled = timer_query();
184
                        scrollbar->position++;
185
                        if (scrollbar->position > scrollbar->stop )
186
                                scrollbar->position = scrollbar->stop;
187
                        scrollbar->fake_position = scrollbar->position-scrollbar->start;
188
                        scrollbar->fake_position *= scrollbar->height-scrollbar->fake_size;
189
                        scrollbar->fake_position /= (scrollbar->stop-scrollbar->start);
190
                }
191
        }
192
 
193
        OnMe = ui_mouse_on_gadget( scrollbar );
194
 
195
        //gr_ubox(0, scrollbar->fake_position, scrollbar->width-1, scrollbar->fake_position+scrollbar->fake_size-1 );
196
 
197
        if (B1_JUST_RELEASED)
198
                scrollbar->dragging = 0;
199
 
200
        //if (B1_PRESSED && OnMe )
201
        //    listbox->dragging = 1;
202
 
203
 
204
        mouse_get_pos(&x, &y, &z);
205
 
206
        OnSlider = 0;
207
        if ((y >= scrollbar->fake_position+scrollbar->y1) && \
208
                (y < scrollbar->fake_position+scrollbar->y1+scrollbar->fake_size) && OnMe )
209
                OnSlider = 1;
210
 
211
        if (B1_JUST_PRESSED && OnSlider )
212
        {
213
                scrollbar->dragging = 1;
214
                scrollbar->drag_x = x;
215
                scrollbar->drag_y = y;
216
                scrollbar->drag_starting = scrollbar->fake_position;
217
                rval = window_event_result::handled;
218
        }
219
        else if (B1_JUST_PRESSED && OnMe)
220
        {
221
                scrollbar->dragging = 2;        // outside the slider
222
                rval = window_event_result::handled;
223
        }
224
 
225
        if  ((scrollbar->dragging == 2) && OnMe && !OnSlider && (timer_query() > scrollbar->last_scrolled + 4))
226
        {
227
                scrollbar->last_scrolled = timer_query();
228
 
229
                if ( y < scrollbar->fake_position+scrollbar->y1 )
230
                {
231
                        // Page Up
232
                        scrollbar->position -= scrollbar->window_size;
233
                        if (scrollbar->position < scrollbar->start )
234
                                scrollbar->position = scrollbar->start;
235
 
236
                } else {
237
                        // Page Down
238
                        scrollbar->position += scrollbar->window_size;
239
                        if (scrollbar->position > scrollbar->stop )
240
                                scrollbar->position = scrollbar->stop;
241
                }
242
                scrollbar->fake_position = scrollbar->position-scrollbar->start;
243
                scrollbar->fake_position *= scrollbar->height-scrollbar->fake_size;
244
                scrollbar->fake_position /= (scrollbar->stop-scrollbar->start);
245
        }
246
 
247
        if ((selected_gadget==scrollbar) && (scrollbar->dragging == 1))
248
        {
249
                //x = scrollbar->drag_x;
250
                scrollbar->fake_position = scrollbar->drag_starting + (y - scrollbar->drag_y );
251
                if (scrollbar->fake_position<0)
252
                {
253
                        scrollbar->fake_position = 0;
254
                        //y = scrollbar->fake_position + scrollbar->drag_y - scrollbar->drag_starting;
255
                }
256
                if (scrollbar->fake_position > (scrollbar->height-scrollbar->fake_size))
257
                {
258
                        scrollbar->fake_position = (scrollbar->height-scrollbar->fake_size);
259
                        //y = scrollbar->fake_position + scrollbar->drag_y - scrollbar->drag_starting;
260
                }
261
 
262
                //mouse_set_pos( x, y );
263
 
264
                scrollbar->position = scrollbar->fake_position;
265
                scrollbar->position *= (scrollbar->stop-scrollbar->start);
266
                scrollbar->position /= ( scrollbar->height-scrollbar->fake_size ) ;
267
                scrollbar->position += scrollbar->start;
268
 
269
                if (scrollbar->position > scrollbar->stop )
270
                        scrollbar->position = scrollbar->stop;
271
 
272
                if (scrollbar->position < scrollbar->start )
273
                        scrollbar->position = scrollbar->start;
274
 
275
                //scrollbar->fake_position = scrollbar->position-scrollbar->start;
276
                //scrollbar->fake_position *= scrollbar->height-scrollbar->fake_size;
277
                //scrollbar->fake_position /= (scrollbar->stop-scrollbar->start);
278
 
279
        }
280
 
281
        if (op != scrollbar->position )
282
                scrollbar->moved = 1;
283
        if (scrollbar->moved)
284
        {
285
                rval = ui_gadget_send_event(dlg, EVENT_UI_GADGET_PRESSED, scrollbar);
286
                if (rval == window_event_result::ignored)
287
                        rval = window_event_result::handled;
288
        }
289
 
290
        if (oldpos != scrollbar->fake_position)
291
                scrollbar->status = 1;
292
 
293
        return rval;
294
}
295
 
296
}