(root)/wip/src/mouse_slider.cc - Rev 1532
Rev 1479 |
Rev 1569 |
Go to most recent revision |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
/*
* mouse_slider.cc
* DIN Is Noise is copyright (c) 2006-2020 Jagannathan Sampath
* For more information, please visit https://dinisnoise.org/
*/
#include "mouse_slider.h"
#include "console.h"
#include "viewwin.h"
#include "container.h"
#include "ui_list.h"
#include "log.h"
extern void warp_mouse (int x, int y);
//extern char BUFFER[];
typedef std::list<mouse_slider_listener*>::iterator mouse_slider_listener_iterator;
mouse_slider_listener::mouse_slider_listener () {orient = Y;}
mouse_slider::mouse_slider () {
initx = inity = inityy = lowx = lowy = midx = midy = highx = highy = prevx = prevy = nmslx = nmsly = lmb_clicked = 0;
int dirs [] = {arrow_button::left, arrow_button::right, arrow_button::down, arrow_button::up};
arrow_button* abs [] = {&axl, &axr, &ayb, &ayt};
for (int i = 0; i < 4; ++i) abs[i]->set (22, dirs[i], 0.5f, 0.5f, 0.5f);
sz = axl.size;
sz_2 = sz / 2;
szn = sz;
szn_1 = szn + sz;
}
void mouse_slider::update_x_arrows () {
int py = mouseyy - sz_2;
axl.set_pos (mousex - szn_1, py);
axr.set_pos (mousex + szn, py);
}
void mouse_slider::update_y_arrows () {
int px = mousex - sz_2;
ayb.set_pos (px, mouseyy - szn_1);
ayt.set_pos (px, mouseyy + szn);
}
int mouse_slider::y_only () {
return (nmsly && SHIFT);
}
int mouse_slider::x_only () {
return (nmslx && CTRL);
}
void mouse_slider::color_arrows (int d, arrow_button& a1, arrow_button& a2) {
static const float low = 0.5f, high = 1.0f;
/*if (d == 0) {
a1.set_color (low, low, low);
a2.set_color (low, low, low);
} else*/ if (d > 0) {
a2.set_color (low, low, high);
a1.set_color (low, low, low);
} else if (d < 0) {
a1.set_color (low, low, high);
a2.set_color (low, low, low);
}
}
void mouse_slider::draw () {
glColor3f (0.5, 0.5, 0.5);
pts[0]=initx; pts[1]=inity;
pts[2]=mousex; pts[3]=mouseyy;
glVertexPointer (2, GL_INT, 0, pts);
glDrawArrays (GL_LINES, 0, 2);
if (nmslx && !y_only() ) {
//color_arrows (dx, axl, axr);
axl.draw ();
axr.draw ();
}
if (nmsly && !x_only()) {
//color_arrows (dy, ayb, ayt);
ayb.draw ();
ayt.draw ();
}
}
int mouse_slider::handle_input () {
if (mousex < lowx || mousex > highx) {
warp_mouse (midx, mousey);
initx = prevx = mousex;
}
if (mousey < lowy || mousey > highy) {
warp_mouse (mousex, midy);
inity = prevy = mouseyy;
}
int ua = 0;
dx = dy = 0;
if ( nmslx && !y_only() ) {
dx = mousex - prevx;
dx += wheel;
HOVER = 1;
if (dx != 0) {
for (mouse_slider_listener_iterator i = mslx.begin (), j = mslx.end (); i != j; ++i) (*i)->moused (dx);
ua = 1;
}
}
if ( nmsly && !x_only() ) {
dy = mouseyy - prevy;
dy += wheel;
HOVER = 1;
if (dy != 0) {
for (mouse_slider_listener_iterator i = msly.begin (), j = msly.end (); i != j; ++i) (*i)->moused (dy);
ua = 1;
}
}
if (ua) {
update_x_arrows ();
update_y_arrows ();
}
prevx = mousex;
prevy = mouseyy;
if (is_lmb (this)) {
lmb_clicked = 1;
} else {
if (lmb_clicked) {
deactivate ();
lmb_clicked = 0;
}
}
return 1;
}
void mouse_slider::add (mouse_slider_listener* msl) {
if (msl->orient == mouse_slider_listener::X) { push_back (mslx, msl); ++nmslx; } else { push_back (msly, msl); ++nmsly; }
}
void mouse_slider::remove (mouse_slider_listener* msl) {
if (msl->orient == mouse_slider_listener::X) { erase (mslx, msl); --nmslx; } else { erase (msly, msl); --nmsly; }
if (nmslx == 0 && nmsly == 0) deactivate ();
}
int mouse_slider::activate () {
if (nmslx == 0 && nmsly == 0) return active;
lowx = margin; highx = view.xmax - lowx;
lowy = margin; highy = view.ymax - lowy;
midx = view.xmax / 2;
midy = view.ymax / 2;
prevx = mousex;
prevy = mouseyy;
initx = mousex;
inity = mouseyy;
inityy = mousey;
string xchange, ychange;
if (nmslx) {
for (mouse_slider_listener_iterator i = mslx.begin (), j = mslx.end (); i != j; ++i) {
mouse_slider_listener* msl = *i;
xchange = xchange + msl->name + ", ";
}
update_x_arrows ();
}
if (nmsly) {
for (mouse_slider_listener_iterator i = msly.begin (), j = msly.end (); i != j; ++i) {
mouse_slider_listener* msl = *i;
ychange = ychange + msl->name + ", ";
}
update_y_arrows ();
}
active = 1;
lmb_clicked = 0;
is_lmb.tie = this;
cons << GREEN << "Move";
if (xchange != "") cons << " Left/Right or Wheel to change " << xchange;
if (ychange != "") cons << " Up/Down or Wheel to change " << ychange;
cons << "ESC/Click to stop" << eol;
return active;
}
extern void draw_slit_cutter (int);
void mouse_slider::deactivate () {
if (nmslx) {for (mouse_slider_listener_iterator i = mslx.begin (), j = mslx.end (); i != j; ++i) { (*i)->after_slide(); }}
if (nmsly) {for (mouse_slider_listener_iterator i = msly.begin (), j = msly.end (); i != j; ++i) { (*i)->after_slide(); }}
mslx.clear ();
msly.clear ();
nmslx = nmsly = 0;
erase (uis.widgets_of[uis.current], &mouse_slider0);
active = 0;
lmb_clicked = 0;
is_lmb.clear (this);
draw_slit_cutter (0);
warp_mouse (initx, inityy);
cons << RED << "Stopped mouse slider" << eol;
}
void activate_mouse_slider () {
if (MENU.show) MENU.toggle ();
if (mouse_slider0.activate ()) {
if (uis.is_widget_on_screen (&mouse_slider0, uis.current) == 0)
uis.widgets_of [uis.current].push_back (&mouse_slider0);
}
}
int mouse_slider_active () {
return mouse_slider0.active;
}
void deactivate_mouse_slider () {
mouse_slider0.deactivate ();
}
void cant_mouse_slide () {
cons << RED << "Mouse slider is already active!" << eol;
}
mouse_slider::~mouse_slider () {}