Subversion Repositories DIN Is Noise

Rev

Rev 416 | Rev 699 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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



#include "plugin.h"
#include "curve_editor.h"
#include "vector2d.h"
#include "ui_list.h"
#include "field.h"
#include "viewwin.h"

#include <vector>
#include <fstream>
using namespace std;

extern ofstream dlog;

plugin::plugin () {
  num_ctrls = 0;
  name = "from_plugin";
  ss.str (name);
  shapeform = 1;
  pts = 0;
  n_pts = 0;
  mix = 1;
  undo = 1;
  num_ctrls = 0;
}

void plugin::setup () {

  widget* _ctrls [] = {&cb_auto_apply, &b_apply};
  for (int i = 0; i < 2; ++i) ctrls.push_back (_ctrls[i]);
  num_ctrls = ctrls.size ();

  b_apply.set_label ("Apply");
  cb_auto_apply.set_label ("Auto apply");

  b_apply.set_listener (this);
  cb_auto_apply.set_listener (this);

  ss.precision (10);

}

void plugin::update () {
  for (int i = 0, j = num_ctrls; i < j; ++i) ctrls[i]->update ();
}

int plugin::handle_input () {
  int r = 0;
  if (!_folded) for (int i = 0, j = num_ctrls; i < j; ++i) r |= ctrls[i]->handle_input ();
  return r;
}

void plugin::draw () {
  if (!_folded) for (int i = 0, j = num_ctrls; i < j; ++i) ctrls[i]->draw ();
}

int plugin::apply (multi_curve& crv) {

  int npts = points.size ();
  if (npts == 0) return 0;

  crv.clear (0);
  if (change_curve_name) crv.set_name (ss.str());

  /*if (resample && npts > 2) {
    points_iterator i = points.begin ();
    points_iterator i0 = i;
    points_iterator j = ++i;
    points_iterator k = ++i;
    i = i0;
    while (1) {
      point<float>& p0 = *i;
      point<float>& p1 = *j;
      point<float>& p2 = *k;
      point<float> va; direction (va, p0, p1);
      point<float> vb; direction (vb, p0, p2);
      float dot = va.x * vb.x + va.y * vb.y;
      float proja = dot / magnitude (vb);

      point<float> uvb; unit_vector (uvb, vb);
      point<float> base; base.x = p0.x + proja * uvb.x; base.y = p0.y + proja * uvb.y;
      point<float> base2p1; direction (base2p1, base, p1);
      double distance = magnitude (base2p1);
      if (distance < limit) {
        j = points.erase (j);
      } else {
        i = j;
        j = k;
      }
      if (++k == points.end()) break;
    }
  }*/


  for (points_iterator i = points.begin (), j = points.end (); i != j; ++i) {
    point<float>& p = *i;
    crv.add_vertex (p.x, p.y);
    crv.add_left_tangent (p.x, p.y);
    crv.add_right_tangent (p.x, p.y);
  }

  if (shapeform)
    crv.set_shapeform (1);
  else
    crv.evaluate ();

  return 1;

}

void plugin::clicked (button& b) {
  uis.crved->apply_plugin (this);
}

void plugin::changed (field& f) {
  render ();
  try_auto_apply ();
}

void plugin::changed (checkbutton& cb) {
  if (&cb == &cb_auto_apply) {
    if (cb.state) {
      uis.crved->apply_plugin (this);
      mix = undo = 0;
    } else mix = undo = 1;
  }
}

void plugin::try_auto_apply () {
  if (cb_auto_apply.state) uis.crved->apply_plugin (this);
}

void plugin::unfold () {
  for (int i = 0, j = num_ctrls; i < j; ++i) ctrls[i]->show ();
  _folded = 0;
}

void plugin::fold () {
  for (int i = 0, j = num_ctrls; i < j; ++i) ctrls[i]->hide ();
  _folded = 1;
}

void plugin::draw (curve_editor* ed) {
  glColor3f (clr.r, clr.g, clr.b);
  float wx, wy;
  int n = points.size ();
  if (n_pts < n) {
    if (pts) delete[] pts;
    pts = new float [2 * n];
    n_pts = n;
  }

  int k = 0;
  for (points_iterator i = points.begin (), j = points.end (); i != j; ++i) {
    point<float>& p = *i;
    ed->obj2win (p.x, p.y, wx, wy);
    pts[k++]=wx; pts[k++]=wy;
  }
  glVertexPointer (2, GL_FLOAT, 0, pts);
  glDrawArrays (GL_LINE_STRIP, 0, n);
}

plugin::~plugin () {
  if (pts) delete[] pts;
}

string plugin::make_fname () {
  extern string user_data_dir;
  return (user_data_dir + "p_" + name);
}