Toshihiro Shimizu 890ddd
shun-iwasawa 86dc52
#include "viewtools.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tools/cursors.h"
Toshihiro Shimizu 890ddd
#include "tgeometry.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <math.h></math.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tgl.h"
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// Zoom Tool
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class ZoomTool final : public TTool {
Shinya Kitaoka 120a6e
  int m_oldY;
Shinya Kitaoka 120a6e
  TPointD m_center;
Shinya Kitaoka 120a6e
  bool m_dragging;
Shinya Kitaoka 120a6e
  double m_factor;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  ZoomTool() : TTool("T_Zoom"), m_dragging(false), m_oldY(0), m_factor(1) {
Shinya Kitaoka 120a6e
    bind(TTool::AllTargets);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  ToolType getToolType() const override { return TTool::GenericTool; }
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  void updateMatrix() override { return setMatrix(TAffine()); }
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  void leftButtonDown(const TPointD &pos, const TMouseEvent &e) override {
Shinya Kitaoka 120a6e
    if (!m_viewer) return;
Shinya Kitaoka 120a6e
    m_dragging              = true;
Shinya Kitaoka 120a6e
    int v                   = 1;
Shinya Kitaoka 120a6e
    if (e.isAltPressed()) v = -1;
Shinya Kitaoka 120a6e
    m_oldY                  = e.m_pos.y;
Shinya Kitaoka 120a6e
    // m_center = getViewer()->winToWorld(e.m_pos);
Shinya Kitaoka 120a6e
    m_center = TPointD(e.m_pos.x, e.m_pos.y);
Shinya Kitaoka 120a6e
    m_factor = 1;
Shinya Kitaoka 120a6e
    invalidate();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 473e70
  void leftButtonDrag(const TPointD &pos, const TMouseEvent &e) override {
Shinya Kitaoka 120a6e
    int d    = m_oldY - e.m_pos.y;
Shinya Kitaoka 120a6e
    m_oldY   = e.m_pos.y;
Shinya Kitaoka 120a6e
    double f = exp(-d * 0.01);
Shinya Kitaoka 120a6e
    m_factor = f;
Shinya Kitaoka 120a6e
    m_viewer->zoom(m_center, f);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 473e70
  void leftButtonUp(const TPointD &pos, const TMouseEvent &e) override {
Shinya Kitaoka 120a6e
    m_dragging = false;
Shinya Kitaoka 120a6e
    invalidate();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  void rightButtonDown(const TPointD &, const TMouseEvent &e) override {
Shinya Kitaoka 120a6e
    if (!m_viewer) return;
Shinya Kitaoka 120a6e
    invalidate();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  void draw() override {
Shinya Kitaoka 120a6e
    if (!m_dragging) return;
Shinya Kitaoka 120a6e
shun_iwasawa bc352c
    TPointD center   = m_viewer->winToWorld(m_center);
Shinya Kitaoka 120a6e
    double pixelSize = getPixelSize();
Shinya Kitaoka 120a6e
    double unit      = pixelSize;
Shinya Kitaoka 120a6e
    glPushMatrix();
Shinya Kitaoka 120a6e
    glTranslated(center.x, center.y, 0);
Shinya Kitaoka 120a6e
    glScaled(unit, unit, unit);
Shinya Kitaoka 120a6e
    glColor3f(1, 0, 0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    double u = 4;
Shinya Kitaoka 120a6e
    glBegin(GL_LINES);
Shinya Kitaoka 120a6e
    glVertex2d(0, -10);
Shinya Kitaoka 120a6e
    glVertex2d(0, 10);
Shinya Kitaoka 120a6e
    glVertex2d(-10, 0);
Shinya Kitaoka 120a6e
    glVertex2d(10, 0);
Shinya Kitaoka 120a6e
    glEnd();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    glPopMatrix();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  int getCursorId() const override { return ToolCursor::ZoomCursor; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} zoomTool;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// Hand Tool
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class HandTool final : public TTool {
Shinya Kitaoka 120a6e
  TStopWatch m_sw;
shun_iwasawa bc352c
  TPointD m_oldPos;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  HandTool() : TTool("T_Hand") { bind(TTool::AllTargets); }
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  ToolType getToolType() const override { return TTool::GenericTool; }
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  void updateMatrix() override { return setMatrix(TAffine()); }
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  void leftButtonDown(const TPointD &pos, const TMouseEvent &e) override {
Shinya Kitaoka 120a6e
    if (!m_viewer) return;
Shinya Kitaoka 120a6e
    m_oldPos = e.m_pos;
Shinya Kitaoka 120a6e
    m_sw.start(true);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  void leftButtonDrag(const TPointD &pos, const TMouseEvent &e) override {
Shinya Kitaoka 120a6e
    if (!m_viewer) return;
Shinya Kitaoka 120a6e
    if (m_sw.getTotalTime() < 10) return;
Shinya Kitaoka 120a6e
    m_sw.stop();
Shinya Kitaoka 120a6e
    m_sw.start(true);
shun_iwasawa bc352c
    TPointD delta = e.m_pos - m_oldPos;
shun_iwasawa bc352c
    delta.y       = -delta.y;
Shinya Kitaoka 120a6e
    m_viewer->pan(delta);
Shinya Kitaoka 120a6e
    m_oldPos = e.m_pos;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  void leftButtonUp(const TPointD &pos, const TMouseEvent &e) override {
Shinya Kitaoka 120a6e
    if (!m_viewer) return;
Shinya Kitaoka 120a6e
    m_sw.stop();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  int getCursorId() const override { return ToolCursor::PanCursor; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} handTool;
Toshihiro Shimizu 890ddd
shun-iwasawa 86dc52
}  // namespace
shun-iwasawa 86dc52
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// Rotate Tool
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
shun-iwasawa 86dc52
RotateTool::RotateTool()
shun-iwasawa 86dc52
    : TTool("T_Rotate")
shun-iwasawa 86dc52
    , m_dragging(false)
shun-iwasawa 86dc52
    , m_cameraCentered("Rotate On Camera Center", false)
shun-iwasawa 86dc52
    , m_angle(0) {
shun-iwasawa 86dc52
  bind(TTool::AllTargets);
shun-iwasawa 86dc52
  m_prop.bind(m_cameraCentered);
shun-iwasawa 86dc52
}
shun-iwasawa 86dc52
shun-iwasawa 86dc52
void RotateTool::leftButtonDown(const TPointD &pos, const TMouseEvent &e) {
shun-iwasawa 86dc52
  if (!m_viewer) return;
shun-iwasawa 86dc52
shun-iwasawa 86dc52
  m_angle       = 0.0;
shun-iwasawa 86dc52
  m_dragging    = true;
shun-iwasawa 86dc52
  m_oldPos      = pos;
shun-iwasawa 86dc52
  m_oldMousePos = e.m_pos;
shun-iwasawa 86dc52
  // m_center = TPointD(0,0);
shun-iwasawa 86dc52
  m_sw.start(true);
shun-iwasawa 86dc52
  invalidate();
shun-iwasawa 86dc52
shun-iwasawa 86dc52
  // m_center =
shun-iwasawa 86dc52
  // viewAffine.inv()*TPointD(0,0);//m_viewer->winToWorld(m_viewer);
shun-iwasawa 86dc52
  // virtual TPointD winToWorld(const TPoint &winPos) const = 0;
shun-iwasawa 86dc52
}
shun-iwasawa 86dc52
shun-iwasawa 86dc52
void RotateTool::leftButtonDrag(const TPointD &pos, const TMouseEvent &e) {
shun-iwasawa 86dc52
  if (!m_viewer) return;
shun-iwasawa 86dc52
  if (m_sw.getTotalTime() < 50) return;
shun-iwasawa 86dc52
  m_sw.stop();
shun-iwasawa 86dc52
  m_sw.start(true);
shun-iwasawa 86dc52
  TPointD p = pos;
shun-iwasawa 86dc52
  if (m_viewer->is3DView()) {
shun-iwasawa 86dc52
    TPointD d     = e.m_pos - m_oldMousePos;
Shinya Kitaoka 120a6e
    m_oldMousePos = e.m_pos;
shun-iwasawa 86dc52
    double factor = 0.5;
shun-iwasawa 86dc52
    m_viewer->rotate3D(factor * d.x, -factor * d.y);
shun-iwasawa 86dc52
  } else {
shun-iwasawa 86dc52
    TPointD a = p - m_center;
shun-iwasawa 86dc52
    TPointD b = m_oldPos - m_center;
shun-iwasawa 86dc52
    if (norm2(a) > 0 && norm2(b) > 0) {
shun-iwasawa 86dc52
      double ang = asin(cross(b, a) / (norm(a) * norm(b))) * M_180_PI;
shun-iwasawa 86dc52
      m_angle    = m_angle + ang;
shun-iwasawa 86dc52
      m_viewer->rotate(m_center, m_angle);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
shun-iwasawa 86dc52
  m_oldPos = p;
shun-iwasawa 86dc52
}
shun-iwasawa 86dc52
shun-iwasawa 86dc52
void RotateTool::leftButtonUp(const TPointD &pos, const TMouseEvent &e) {
shun-iwasawa 86dc52
  m_dragging = false;
shun-iwasawa 86dc52
  invalidate();
shun-iwasawa 86dc52
  m_sw.stop();
shun-iwasawa 86dc52
}
shun-iwasawa 86dc52
shun-iwasawa 86dc52
void RotateTool::draw() {
shun-iwasawa 86dc52
  glColor3f(1, 0, 0);
shun-iwasawa 86dc52
  double u = 50;
shun-iwasawa 86dc52
  if (m_cameraCentered.getValue())
shun-iwasawa 86dc52
    m_center = TPointD(0, 0);
shun-iwasawa 86dc52
  else {
shun-iwasawa 86dc52
    TAffine aff                        = m_viewer->getViewMatrix().inv();
shun-iwasawa 86dc52
    if (m_viewer->getIsFlippedX()) aff = aff * TScale(-1, 1);
shun-iwasawa 86dc52
    if (m_viewer->getIsFlippedY()) aff = aff * TScale(1, -1);
shun-iwasawa 86dc52
    u                                  = u * sqrt(aff.det());
shun-iwasawa 86dc52
    m_center                           = aff * TPointD(0, 0);
Shinya Kitaoka 120a6e
  }
shun-iwasawa 86dc52
  tglDrawSegment(TPointD(-u + m_center.x, m_center.y),
shun-iwasawa 86dc52
                 TPointD(u + m_center.x, m_center.y));
shun-iwasawa 86dc52
  tglDrawSegment(TPointD(m_center.x, -u + m_center.y),
shun-iwasawa 86dc52
                 TPointD(m_center.x, u + m_center.y));
shun-iwasawa 86dc52
}
Shinya Kitaoka 120a6e
shun-iwasawa 86dc52
int RotateTool::getCursorId() const { return ToolCursor::RotateCursor; }
shun-iwasawa e87e08
shun-iwasawa 86dc52
RotateTool rotateTool;