Subversion Repositories DIN Is Noise

Rev

Rev 13 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
* warper.cc
* DIN Is Noise is copyright (c) 2006-2017 Jagannathan Sampath
* For more information, please visit http://dinisnoise.org/
*/



#include "warper.h"
#include "vector2d.h"
#include "ui_list.h"

#include <vector>
using namespace std;

extern const int MILLION;

extern ui_list uis;


warper::warper () : x ("warpx.crv"), y ("warpy.crv"), ed ("warp.ed") {
  ed.add (&x, &lis);
  ed.add (&y, &lis);
  back_ed = 0;
  name = "XY Warper";
}

warper::~warper () {
  x.save ("warpx.crv");
  y.save ("warpy.crv");
  widget_save ("d_warper", ctrls);
}

void warper::setup () {

  plugin::setup ();

  ctrls.push_back (&b_edit);
  num_ctrls = ctrls.size ();

  b_edit.set_label ("Edit");

  widget_load ("d_warper", ctrls);

  b_edit.set_listener (this);

  for (int i = 0; i < num_ctrls; ++i) {
    widget* ci = ctrls[i];
    if (ci != &cb_auto_apply) ci->set_color (clr.r, clr.g, clr.b);
  }

  //for (int i = 0; i < num_ctrls; ++i) ctrls[i]->set_moveable (1);

  solx (&x);
  soly (&y);

  lis.wp = this;

}

void warper::clicked (button& b) {
  if (&b == &b_edit) {
    if (b_edit.label == "Edit") {
      b_edit.set_label ("Go back");
      back_ed = uis.crved;
      uis.set_current (&ed);
    } else {
      uis.set_current (back_ed);
      b_edit.set_label ("Edit");
    }
  } else plugin::clicked (b);
}

void warper::render () {
  // warp all segments of input curve
  //
  multi_curve& in = *uis.crved->get_picked_curve (1); // get picked curve
  box<float> bbox; in.calc_bbox (bbox); // get bounding box

  vector<crvpt> in_pts; in.get_profile_points (in_pts);

  int n = in_pts.size ();
  if (n < 2) return;  // no segment so bail out

  points.clear ();
  // warp every point of all segments
  float bwi2 = bbox.width_1 * 2, bhi2 = bbox.height_1 * 2;
  for (int i = 0; i < n; ++i) {
    crvpt& vi = in_pts [i];
    float x = -1 + (vi.x - bbox.left) * bwi2; // map x to -1 to 1 ie normalise
    float y = -1 + (vi.y - bbox.bottom) * bhi2; // map y to -1 to 1 ie normalise
    float wx = solx (x); // warp x
    float wy = soly (y); // warp y
    x = bbox.left + (wx + 1) / 2.0 * bbox.width; // map warped x back to original to get new x
    y = bbox.bottom + (wy + 1) / 2.0 * bbox.height; // map warped y back to original to get new y
    points.push_back (point<float>(x, y)); // add new x,y
  }  
  ss.str("");
  ss << in.name;

}

int warper::apply (multi_curve& crv) {
  render ();
  return plugin::apply (crv);
}

void warper::draw (curve_editor* ed) {}

void warp_listener::edited (curve_editor* ed, int i) {
  multi_curve* crv = ed->get_curve (i);
  if (crv == &wp->x) wp->solx (&wp->x); else wp->soly (&wp->y);
  if (wp->cb_auto_apply.get_state ()) {
    multi_curve& in = *wp->back_ed->get_picked_curve (1);
    wp->apply(in);
  }
}