Debian Codegraph @

ui/UISlider.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/ui/UISlider.cpp $
21 * $Revision: 10 $
22 * $Date: 4/14/99 1:53a $
23 * $Author: Jeff $
24 *
25 * Slider code
26 *
27 * $Log: /DescentIII/Main/ui/UISlider.cpp $
28 *
29 * 10 4/14/99 1:53a Jeff
30 * fixed case mismatched #includes
31 *
32 * 9 12/02/98 11:45a Samir
33 * small fixes to slider control.
34 *
35 * 8 10/19/98 6:30p Jeff
36 * changes made for detail variables. Put in preset values. Preset
37 * options. Removed terrain_cast from detail. Put new callbacks in
38 * UIListBox and UISlider
39 *
40 * 7 9/01/98 4:33p Samir
41 * improved mouse handling of sliders.
42 *
43 * 6 3/10/98 7:25p Samir
44 * Allow customization of visual slider.
45 *
46 * 5 3/04/98 12:08p Samir
47 * Slider fixed.
48 *
49 * 4 1/30/98 7:05p Samir
50 * Each gadget now has their own coordinate system.
51 *
52 * 3 1/18/98 4:24p Samir
53 * Fixed up scrolling sliders.
54 *
55 * 2 1/13/98 4:28p Samir
56 * Added sliders.
57 *
58 * 1 1/09/98 12:00p Samir
59 * Initial revision
60 *
61 * $NoKeywords: $
62 */
63
64 #include "UIlib.h"
65
66 // Construction and destruction
67
68 UISlider::UISlider() {
69 switch_item = slider_item = NULL;
70 selectchange_fn = NULL;
71 selectchange_id_fn = NULL;
72 m_callbackptr = NULL;
73 }
74
75 UISlider::~UISlider() {}
76
77 #define SLIDER_BUTTON_WIDTH (switch_item->width() ? switch_item->width() : 16)
78 #define SLIDER_BUTTON_HEIGHT (switch_item->height() ? switch_item->height() : 16)
79
80 void UISlider::Create(UIWindow *parent, int id, int x, int y, int w, int h, int flags) {
81 ASSERT(w > 0);
82
83 m_Pos = 0;
84 m_Range = 1;
85 selectchange_fn = NULL;
86 selectchange_id_fn = NULL;
87 m_callbackptr = NULL;
88
89 switch_item = UIPrimativeItem(GR_DARKGRAY).CopyUIItem();
90 slider_item = UIPrimativeItem(GR_DARKGRAY).CopyUIItem();
91
92 UIGadget::Create(parent, id, x, y, w, h, flags);
93 }
94
95 // sets visual characteristics of slider.
96 void UISlider::SetSliderSwitchItem(UIItem *item) {
97 if (switch_item)
98 delete switch_item;
99
100 if (item) {
101 switch_item = item->CopyUIItem();
102 } else
103 switch_item = UIPrimativeItem(GR_DARKGRAY).CopyUIItem();
104 }
105
106 void UISlider::SetSliderItem(UIItem *item) {
107 if (slider_item)
108 delete slider_item;
109
110 if (item) {
111 slider_item = item->CopyUIItem();
112 } else
113 slider_item = UIPrimativeItem(GR_DARKGRAY).CopyUIItem();
114 }
115
116 // settings
117
118 void UISlider::SetRange(int range) {
119 ASSERT(range > 0);
120
121 m_Range = range;
122 m_IntervalWidth = ((float)m_PixelWidth) / ((float)m_Range);
123 }
124
125 // called from outside gadget hierarchy.
126
127 // override: called when resized or before drawing.
128 void UISlider::OnFormat() {
129 if (slider_item->width() == 0)
130 m_W = GetSavedW();
131 else
132 m_W = slider_item->width();
133
134 if (switch_item->height() == 0)
135 m_H = GetSavedH();
136 else
137 m_H = switch_item->height();
138
139 m_PixelWidth = m_W - SLIDER_BUTTON_WIDTH;
140 m_IntervalWidth = ((float)m_PixelWidth) / ((float)m_Range);
141 switch_w = SLIDER_BUTTON_WIDTH;
142 switch_h = SLIDER_BUTTON_HEIGHT;
143
144 UIGadget::OnFormat();
145 }
146
147 // behavior when gadget is being destroyed.
148 void UISlider::OnDestroy() {
149 if (slider_item)
150 delete slider_item;
151 if (switch_item)
152 delete switch_item;
153 }
154
155 // behavior when gadget is being drawn.
156 void UISlider::OnDraw() {
157 // draw the slider region and the slider switch
158 switch_x = (int)((float)m_Pos * (float)m_IntervalWidth);
159 switch_y = 0;
160
161 if (slider_item->width())
162 slider_item->draw(0, switch_y + (SLIDER_BUTTON_HEIGHT / 2) - slider_item->height() / 2);
163 else
164 slider_item->draw(0, switch_y + (SLIDER_BUTTON_HEIGHT / 2) - 1, m_W, 3);
165
166 if (switch_item->width())
167 switch_item->draw(switch_x, switch_y);
168 else
169 switch_item->draw(switch_x, switch_y, SLIDER_BUTTON_WIDTH, SLIDER_BUTTON_HEIGHT);
170 }
171
172 // behavior when key is pressed.
173 void UISlider::OnKeyDown(int key) {
174 if (key == KEY_LEFT && m_Pos > 0) {
175 m_Pos--;
176 OnSelect();
177 } else if (key == KEY_RIGHT && m_Pos < m_Range) {
178 m_Pos++;
179 OnSelect();
180 }
181 }
182
183 // behavior when key is released.
184 void UISlider::OnKeyUp(int key) {}
185
186 // behavior when mouse button is pressed.
187 void UISlider::OnMouseBtnDown(int btn) {
188 if (btn == UILMSEBTN) {
189 int x, new_pos; // within gadget.
190 // determine where in slider the new switch should go.
191 LOCK_FOCUS(this);
192 x = SCREEN_TO_GAD_X(this, UI_input.mx);
193 if (x < 0)
194 x = 0;
195 if (x > m_W)
196 x = m_W;
197 new_pos = (int)(((float)x) / ((float)m_IntervalWidth));
198 if (new_pos < 0)
199 new_pos = 0;
200 else if (new_pos >= m_Range)
201 new_pos = m_Range - 1;
202 if (new_pos != m_Pos) {
203 // mprintf(0, "x=%d, interval=%d, pos=%d\n", x, m_IntervalWidth, new_pos);
204 OnSelect();
205 }
206 m_Pos = new_pos;
207 }
208 }
209
210 // behavior when mouse button is released.
211 void UISlider::OnMouseBtnUp(int btn) {
212 if (btn == UILMSEBTN)
213 UNLOCK_FOCUS(this);
214 }
215
216 void UISlider::OnSelect() {
217 if (selectchange_fn) {
218 (*selectchange_fn)(m_Pos);
219 }
220
221 if (selectchange_id_fn) {
222 (*selectchange_id_fn)(GetID(), m_callbackptr);
223 }
224
225 UIGadget::OnSelect();
226 }
227
228 void UISlider::SetSelectChangeCallback(void (*fn)(int)) { selectchange_fn = fn; }
229
230 void UISlider::SetSelectChangeCallback(void (*fn)(int, void *), void *ptr) {
231 selectchange_id_fn = fn;
232 m_callbackptr = ptr;
233 }