This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
GUI_scrollbar.cpp
174 lines (148 loc) · 3.87 KB
/
GUI_scrollbar.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
GUILIB: An example GUI framework library for use with SDL
*/
/* This is a very generic scrollbar widget */
#include "GUI_scrollbar.h"
#define SCROLL_INTERVAL 100 /* Scroll every 100 ms */
#define SCROLL_WAIT 2 /* Intervals to wait before scrolling */
/* Passed the sensitive areas, orientation, and initial widget */
GUI_ScrollBar:: GUI_ScrollBar(SDL_Rect &first, SDL_Rect &middle, SDL_Rect &last,
orientation direction, GUI_Scrollable *widget)
: GUI_Widget(NULL)
{
/* Set the sensitive areas */
sensitive_neg = first;
sensitive_mid = middle;
sensitive_pos = last;
/* Set the orientation and next scrolling time */
whichxy = direction;
next_scroll = 0;
/* Set the default scrollable widget */
target = widget;
}
GUI_ScrollBar:: GUI_ScrollBar(orientation direction, GUI_Scrollable *widget)
: GUI_Widget(NULL)
{
/* Set the orientation */
whichxy = direction;
/* Set the default scrollable widget */
target = widget;
}
void
GUI_ScrollBar:: FindBounds(void)
{
SDL_Rect *rects[4];
/* Find the bounds of this widget */
rects[0] = &sensitive_neg;
rects[1] = &sensitive_mid;
rects[2] = &sensitive_pos;
rects[3] = NULL;
SetRect(rects);
}
/* Link a scrollable widget to this scrollbar */
int
GUI_ScrollBar:: AddScrollable(GUI_Scrollable *widget)
{
target = widget;
return(0);
}
/* Continue scrolling while mouse is held down */
GUI_status
GUI_ScrollBar:: Idle(void)
{
GUI_status status;
status = GUI_PASS;
if ( next_scroll && (next_scroll <= SDL_GetTicks()) ) {
int x, y;
Uint8 state;
state = SDL_GetMouseState(&x, &y);
if ( (state & SDL_BUTTON(1)) == SDL_BUTTON(1) ) {
status = MouseDown(x, y, 1);
next_scroll /= SCROLL_WAIT;
} else {
next_scroll = 0;
}
}
return(status);
}
/* Mouse hits activate us */
GUI_status
GUI_ScrollBar:: MouseDown(int x, int y, int button)
{
GUI_status status;
/* Don't do anything if we're not linked to a widget */
if ( target == NULL ) {
return(GUI_PASS);
}
/* Scroll depending on where we were hit */
status = GUI_REDRAW;
if ( HitRect(x, y, sensitive_neg) ) {
Scroll(-1);
} else
if ( HitRect(x, y, sensitive_pos) ) {
Scroll(1);
} else
if ( HitRect(x, y, sensitive_mid) ) {
int position;
int first, last;
float percentage;
/* Scale the position by the hit coordinate */
if ( whichxy == SCROLLBAR_HORIZONTAL ) {
percentage = (float)(x-sensitive_mid.x)/sensitive_mid.w;
} else {
percentage = (float)(y-sensitive_mid.y)/sensitive_mid.h;
}
target->Range(first, last);
position = (int)(first+percentage*(last-first)+0.5);
ScrollTo(position);
} else {
status = GUI_PASS;
}
if ( status == GUI_REDRAW ) {
next_scroll = SDL_GetTicks()+SCROLL_WAIT*SCROLL_INTERVAL;
} else {
next_scroll = 0;
}
return(status);
}
/* The functions to activate the scrolling */
void
GUI_ScrollBar:: Scroll(int amount)
{
target->Scroll(amount);
}
void
GUI_ScrollBar:: ScrollTo(int position)
{
target->Scroll(position-target->Scroll(0));
}
/* Passed the sensitive images, orientation, and initial widget */
GUI_ScrollButtons:: GUI_ScrollButtons(int x1, int y1, SDL_Surface *image1,
SDL_Rect &middle, int x2, int y2, SDL_Surface *image2,
orientation direction, GUI_Scrollable *widget)
: GUI_ScrollBar(direction, widget)
{
/* Set the image and rectangle of the negative button */
image_neg = image1;
sensitive_neg.x = x1;
sensitive_neg.y = y1;
sensitive_neg.w = image1->w;
sensitive_neg.h = image1->h;
/* Set the image and rectangle of the positive button */
image_pos = image2;
sensitive_pos.x = x2;
sensitive_pos.y = y2;
sensitive_pos.w = image2->w;
sensitive_pos.h = image2->h;
/* Set the middle sensitive rectangle */
sensitive_mid = middle;
/* Find the bounds of this widget */
FindBounds();
}
/* Show the widget */
void
GUI_ScrollButtons:: Display(void)
{
SDL_BlitSurface(image_neg, NULL, screen, &sensitive_neg);
SDL_BlitSurface(image_pos, NULL, screen, &sensitive_pos);
}