Subversion Repositories DIN Is Noise

Rev

Rev 295 | Rev 484 | 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-2017 Jagannathan Sampath
* For more information, please visit http://dinisnoise.org/
*/


#include "mouse_slider.h"
#include "console.h"
#include "viewwin.h"
#include "container.h"
#include "ui_list.h"

extern int lmb;
extern int mousex, mousey, mouseyy;
extern void warp_mouse (int x, int y);
extern viewport view;

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 = lowx = lowy = midx = midy = highx = highy = prevx = prevy = nmslx = nmsly = lmb_clicked = 0;
}

void mouse_slider::draw () {
  glColor3f (0.5, 0.5, 0.5);
  glBegin (GL_LINES);
    glVertex2f (initx, inity);
    glVertex2f (mousex, mouseyy);
  glEnd ();
}

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;
  }

  if (nmslx) {
    int dx = mousex - prevx;
    if (dx != 0) {
      for (mouse_slider_listener_iterator i = mslx.begin (), j = mslx.end (); i != j; ++i) (*i)->moused (dx);
    }
  }

  if (nmsly) {
    int dy = mouseyy - prevy;
    if (dy != 0) {
      for (mouse_slider_listener_iterator i = msly.begin (), j = msly.end (); i != j; ++i) (*i)->moused (dy);
    }
  }

  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;

  const int margin = 5;
  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;

  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 + ", ";
    }
  }

  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 + ", ";
    }
  }

  active = 1;
  lmb_clicked = 0;
  is_lmb.tie = this;

  cons << GREEN << "Just move";
  if (xchange != "") cons << " in X to " << xchange;
  if (ychange != "") cons << " in Y to " << ychange;
  cons << "ESC or Right 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)->finished_mousing(); }}
  if (nmsly) {for (mouse_slider_listener_iterator i = msly.begin (), j = msly.end (); i != j; ++i) { (*i)->finished_mousing(); }}
  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);
  cons << RED << "Stopped mouse slider" << eol;
}

void activate_mouse_slider () {
  if (uis.main_menu.show) uis.main_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 is_mouse_slider_active () {
  return mouse_slider0.active;
}

void cant_mouse_slide () {
  cons << RED << "Sorry, mouse slider is already active!" << eol;
}