1 /*
2 * mouse_slider.cc
3 * DIN Is Noise is copyright (c) 2006-2021 Jagannathan Sampath
4 * DIN Is Noise is released under GNU Public License 2.0
5 * For more information, please visit https://dinisnoise.org/
6 */
7
8 #include "mouse_slider.h"
9 #include "console.h"
10 #include "viewwin.h"
11 #include "container.h"
12 #include "ui_list.h"
13
14 extern char BUFFER[];
15 extern void warp_mouse (int x, int y);
16 extern int line_height;
17
18 typedef std::list<mouse_slider_listener*>::iterator mouse_slider_listener_iterator;
19
mouse_slider_listener()20 mouse_slider_listener::mouse_slider_listener () {
21 orient = Y;
22 delta0 = 1.0;
23 delta = 0.0;
24 }
25
mouse_slider()26 mouse_slider::mouse_slider () {
27
28 initx = inity = inityy = lowx = lowy = midx = midy = highx = highy = prevx = prevy = dx = dy = nmslx = nmsly = lmb_clicked = active = 0;
29
30 int dirs [] = {arrow_button::left, arrow_button::right, arrow_button::down, arrow_button::up};
31 arrow_button* abs [] = {&axl, &axr, &ayb, &ayt};
32 for (int i = 0; i < 4; ++i) abs[i]->set (22, dirs[i], 0.5f, 0.5f, 0.5f);
33
34 sz = axl.size;
35 sz_2 = sz / 2;
36 szn = sz;
37 szn_1 = szn + sz;
38
39 warp = 1;
40
41 shift = 0;
42
43 }
44
update_x_arrows()45 void mouse_slider::update_x_arrows () {
46 int py = mouseyy - sz_2;
47 axl.set_pos (mousex - szn_1, py);
48 axr.set_pos (mousex + szn, py);
49 }
50
update_y_arrows()51 void mouse_slider::update_y_arrows () {
52 int px = mousex - sz_2;
53 ayb.set_pos (px, mouseyy - szn_1);
54 ayt.set_pos (px, mouseyy + szn);
55 }
56
justx()57 int mouse_slider::justx () {
58 return (nmslx && CTRL);
59 }
60
justy()61 int mouse_slider::justy () {
62 return (nmsly && SHIFT);
63 }
64
draw()65 void mouse_slider::draw () {
66 glColor3f (0.5, 0.5, 0.75);
67 pts[0]=initx; pts[1]=inity;
68 pts[2]=mousex; pts[3]=mouseyy;
69 glVertexPointer (2, GL_INT, 0, pts);
70 glDrawArrays (GL_LINES, 0, 2);
71
72 /*glEnable (GL_LINE_STIPPLE);
73 glLineStipple (1, 0xa0b0);
74 if (nmslx) {
75 pts[0]=lowx; pts[1] = 0;
76 pts[2]=lowx; pts[3] = view.ymax;
77 pts[4]=highx; pts[5] = 0;
78 pts[6]=highx; pts[7] = view.ymax;
79 glDrawArrays (GL_LINES, 0, 4);
80 }
81 if (nmsly) {
82 pts[0]=0; pts[1] = lowy;
83 pts[2]=view.xmax; pts[3] = lowy;
84 pts[4]=0; pts[5] = highy;
85 pts[6]=view.xmax; pts[7] = highy;
86 glDrawArrays (GL_LINES, 0, 4);
87 }
88 glDisable (GL_LINE_STIPPLE);*/
89
90 if (nmslx && !justy() ) {
91 axl.draw ();
92 axr.draw ();
93 //printdx ();
94 }
95 if (nmsly && !justx()) {
96 ayb.draw ();
97 ayt.draw ();
98 //printdy ();
99 }
100 }
101
102 /*void mouse_slider::printdx () {
103 glColor3f (0, 1, 1);
104 for (mouse_slider_listener_iterator i = mslx.begin (), j = mslx.end (); i != j; ++i) {
105 mouse_slider_listener* mslx = *i;
106 sprintf (BUFFER, "%0.3f", scale.value * mslx->delta0);
107 draw_string (BUFFER, axr.extents.right + 5, axr.extents.midy);
108 }
109 }
110
111 void mouse_slider::printdy () {
112 glColor3f (0, 1, 1);
113 int yt = ayt.extents.top, yb = ayb.extents.bottom - line_height;
114 static const int dt = 3;
115 for (mouse_slider_listener_iterator i = msly.begin (), j = msly.end (); i != j; ++i, yt += line_height, yb -= line_height) {
116 mouse_slider_listener* msly = *i;
117 double v = scale.value * msly->delta0;
118 sprintf (BUFFER, "+%0.3f", v);
119 draw_string (BUFFER, ayt.extents.midx + dt, ayt.extents.top);
120 BUFFER[0]='-';
121 draw_string (BUFFER, ayt.extents.midx + dt, ayb.extents.bottom - line_height);
122 }
123 }*/
124
print(list<mouse_slider_listener * > & msll)125 void mouse_slider::print (list<mouse_slider_listener*>& msll) {
126 for (mouse_slider_listener_iterator i = msll.begin (), j = msll.end (); i != j; ++i) {
127 mouse_slider_listener* msli = *i;
128 cons << "Base = " << base << ", +- = " << (scale.value * msli->delta0) << eol;
129 }
130 }
131
print(list<mouse_slider_listener * > & l1,list<mouse_slider_listener * > & l2)132 void mouse_slider::print (list<mouse_slider_listener*>& l1, list<mouse_slider_listener*>& l2) {
133 print (l1);
134 print (l2);
135 }
136
handle_input()137 int mouse_slider::handle_input () {
138
139 if (keypressed (SDLK_0)) {
140 base = 10;
141 scale.calcvalue (base, shift);
142 print (mslx, msly);
143 }
144 else {
145 for (Uint8 i = SDLK_1; i < SDLK_COLON; ++i) {
146 if (keypressed(i)) {
147 base = i - SDLK_0;
148 scale.calcvalue (base, shift);
149 print (mslx, msly);
150 break;
151 }
152 }
153 }
154
155 if (keypressed (SDLK_q)) {
156 scale /= base;
157 --shift;
158 print (mslx, msly);
159 }
160 else if (keypressed (SDLK_e)) {
161 scale *= base;
162 ++shift;
163 print (mslx, msly);
164 }
165
166 if (keypressed (SDLK_SLASH)) {
167 warp = !warp;
168 if (warp) cons << GREEN << "Will warp mouse at boundary" << eol; else cons << RED << "Will not warp mouse at boundary" << eol;
169 }
170
171 if (warp) {
172
173 if (mousex < lowx || mousex > highx) {
174 warp_mouse (midx, mousey);
175 initx = prevx = mousex;
176 }
177
178 if (mousey < lowy || mousey > highy) {
179 warp_mouse (mousex, midy);
180 inity = prevy = mouseyy;
181 }
182
183 }
184
185
186 int ua = 0;
187 dx = dy = 0;
188 if ( nmslx && !justy() ) {
189 if (wheel) {
190 mousex += wheel;
191 warp_mouse (mousex, mousey);
192 }
193 dx = mousex - prevx;
194 HOVER = 1;
195 if (dx != 0) {
196 for (mouse_slider_listener_iterator i = mslx.begin (), j = mslx.end (); i != j; ++i) (*i)->moused (dx, scale.value);
197 ua = 1;
198 }
199 }
200
201 if ( nmsly && !justx() ) {
202 if (wheel) {
203 mousey -= wheel;
204 warp_mouse (mousex, mousey);
205 }
206 dy = mouseyy - prevy;
207 HOVER = 1;
208 if (dy != 0) {
209 for (mouse_slider_listener_iterator i = msly.begin (), j = msly.end (); i != j; ++i) (*i)->moused (dy, scale.value);
210 ua = 1;
211 }
212 }
213
214 if (ua) {
215 update_x_arrows ();
216 update_y_arrows ();
217 }
218
219 prevx = mousex;
220 prevy = mouseyy;
221
222 if (is_lmb (this)) {
223 lmb_clicked = 1;
224 } else {
225 if (lmb_clicked) deactivate ();
226 }
227
228 return 1;
229
230 }
231
add(mouse_slider_listener * msl)232 void mouse_slider::add (mouse_slider_listener* msl) {
233 if (msl->orient == mouse_slider_listener::X) { push_back (mslx, msl); ++nmslx; } else { push_back (msly, msl); ++nmsly; }
234 }
235
remove(mouse_slider_listener * msl)236 void mouse_slider::remove (mouse_slider_listener* msl) {
237 if (msl->orient == mouse_slider_listener::X) { erase (mslx, msl); --nmslx; } else { erase (msly, msl); --nmsly; }
238 }
239
activate(int shft,int scalestyle)240 int mouse_slider::activate (int shft, int scalestyle) {
241
242 if (nmslx == 0 && nmsly == 0) return active;
243
244 lowx = margin; highx = view.xmax - lowx;
245 lowy = margin; highy = view.ymax - lowy;
246 midx = view.xmax / 2;
247 midy = view.ymax / 2;
248 prevx = mousex;
249 prevy = mouseyy;
250 initx = mousex;
251 inity = mouseyy;
252 inityy = mousey;
253
254 string xchange, ychange;
255 if (nmslx) {
256 for (mouse_slider_listener_iterator i = mslx.begin (), j = mslx.end (); i != j; ++i) {
257 mouse_slider_listener* msl = *i;
258 xchange = xchange + msl->name + ", ";
259 }
260 update_x_arrows ();
261 }
262
263 if (nmsly) {
264 for (mouse_slider_listener_iterator i = msly.begin (), j = msly.end (); i != j; ++i) {
265 mouse_slider_listener* msl = *i;
266 ychange = ychange + msl->name + ", ";
267 }
268 update_y_arrows ();
269 }
270
271 if (!active) {
272 uis.widgets_of [uis.current].push_back (&mouse_slider0);
273 active = 1;
274 warp = 1;
275 lmb_clicked = 0;
276 is_lmb.tie = this;
277 }
278
279 cons << GREEN << "Just move";
280 if (xchange != "") cons << " Left/Right or Wheel to change " << xchange;
281 if (ychange != "") cons << " Up/Down or Wheel to change " << ychange;
282 cons << "ESC/Click to stop" << eol;
283
284 shift = shft;
285 if (scalestyle == scalet::CHANGING) base = 10.0; else base = 1.0;
286 scale.style = scalestyle;
287 scale.calcvalue (base, shift);
288
289 return active;
290
291 }
292
293
deactivate()294 int mouse_slider::deactivate () {
295
296 if (active) {
297
298 if (nmslx) {
299 for (mouse_slider_listener_iterator i = mslx.begin (), j = mslx.end (); i != j; ++i) (*i)->after_slide();
300 mslx.clear ();
301 nmslx = 0;
302 }
303
304 if (nmsly) {
305 for (mouse_slider_listener_iterator i = msly.begin (), j = msly.end (); i != j; ++i) (*i)->after_slide();
306 msly.clear ();
307 nmsly = 0;
308 }
309
310 active = 0;
311 erase (uis.widgets_of[uis.current], &mouse_slider0);
312 warp_mouse (initx, inityy);
313 lmb_clicked = 0;
314 is_lmb.clear (this);
315
316 extern void draw_slit_cutter (int);
317 draw_slit_cutter (0);
318
319 cons << RED << "Stopped mouse slider" << eol;
320
321 return 1;
322
323 }
324
325 scale.style = 1;
326
327 return 0;
328
329 }
330
activate_mouse_slider(int shift,int scalestyle)331 void activate_mouse_slider (int shift, int scalestyle) {
332 if (MENU.show) MENU.toggle ();
333 mouse_slider0.activate (shift, scalestyle);
334 }
335
cant_mouse_slide()336 void cant_mouse_slide () {
337 cons << RED << "Mouse slider is already active!" << eol;
338 }
339
~mouse_slider()340 mouse_slider::~mouse_slider () {}
341
calcvalue(double & b,int s)342 void mouse_slider::scalet::calcvalue (double& b, int s) {
343 if (style == CHANGING) {
344 value = 1.0;
345 if (s != 0) value *= pow (b, s);
346 } else {
347 value = b;
348 }
349 }
350
351