Shinya Kitaoka 810553
#pragma once
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef PLANE_VIEWER_H
Toshihiro Shimizu 890ddd
#define PLANE_VIEWER_H
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzCore includes
Toshihiro Shimizu 890ddd
#include "tcommon.h"
Toshihiro Shimizu 890ddd
#include "traster.h"
Toshihiro Shimizu 890ddd
#include "timage.h"
Toshihiro Shimizu 890ddd
shun_iwasawa 96fddb
// TnzQt includes
shun_iwasawa 96fddb
#include "toonzqt/glwidget_for_highdpi.h"
shun_iwasawa 96fddb
Toshihiro Shimizu 890ddd
// Qt includes
Jeremy Bullock 1ef2a2
#include <qopenglwidget></qopenglwidget>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#undef DVAPI
Toshihiro Shimizu 890ddd
#undef DVVAR
Toshihiro Shimizu 890ddd
#ifdef TOONZQT_EXPORTS
Toshihiro Shimizu 890ddd
#define DVAPI DV_EXPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_EXPORT_VAR
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#define DVAPI DV_IMPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_IMPORT_VAR
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//  Forward declarations
Toshihiro Shimizu 890ddd
class TRasterImageP;
Toshihiro Shimizu 890ddd
class TToonzImageP;
Toshihiro Shimizu 890ddd
class TVectorImageP;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Shinya Kitaoka 120a6e
  \brief    The PlaneViewer class implements a basic OpenGL widget showing a
Shinya Kitaoka 120a6e
  plane in a
Shinya Kitaoka 120a6e
            standard top-down view, and supports image drawing from the Toonz
Shinya Kitaoka 120a6e
  images API.
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  \details  A PlaneViewer instance is designed to view bidimensional objects
Shinya Kitaoka 120a6e
  layed-out
Shinya Kitaoka 120a6e
            on a plane, providing standard mouse interaction functions and some
Shinya Kitaoka 120a6e
  built-in
Toshihiro Shimizu 890ddd
            keyboard shortcuts to provide view navigation.
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
            The class implements the necessary methods to draw objects on the
Shinya Kitaoka 120a6e
  plane -
Shinya Kitaoka 120a6e
            prominently, functions to push both world and widget references for
Shinya Kitaoka 120a6e
  standard
Shinya Kitaoka 120a6e
            OpenGL drawing, conversions between world and widget coordinates,
Shinya Kitaoka 120a6e
  and
Toshihiro Shimizu 890ddd
            efficient image-drawing functions for all Toonz image types.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
shun_iwasawa 2bdc04
/*
Shinya Kitaoka d1f6c4
CAUTION : Changing PlaneViewer to inherit QOpenGLWidget causes crash bug with
Shinya Kitaoka d1f6c4
shader fx for some unknown reasons. So I will reluctantly keep using the
shun_iwasawa 2bdc04
obsolete class until the shader fx being overhauled. 2016/6/22 Shun
shun_iwasawa 2bdc04
*/
shun_iwasawa 2bdc04
shun_iwasawa 96fddb
class DVAPI PlaneViewer : public GLWidgetForHighDpi {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  PlaneViewer(QWidget *parent);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // Background functions
Shinya Kitaoka 120a6e
  void setBgColor(const TPixel32 &color1, const TPixel32 &color2);
Shinya Kitaoka 120a6e
  void getBgColor(TPixel32 &color1, TPixel32 &color2) const;
Shinya Kitaoka 120a6e
  void setChessSize(double size);
Shinya Kitaoka 120a6e
  void drawBackground();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // Image drawing functions
Shinya Kitaoka 120a6e
  void draw(TRasterP ras, double dpiX, double dpiY, TPalette *palette = 0);
Shinya Kitaoka 120a6e
  void draw(TRasterImageP ri);
Shinya Kitaoka 120a6e
  void draw(TToonzImageP ti);
Shinya Kitaoka 120a6e
  void draw(TVectorImageP vi);
Shinya Kitaoka 120a6e
  void draw(TImageP img);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // World-Widget conversion functions
Shinya Kitaoka 120a6e
  TPointD winToWorld(int x, int y) { return m_aff.inv() * TPointD(x, y); }
Shinya Kitaoka 120a6e
  TPointD worldToWin(double x, double y) { return m_aff * TPointD(x, y); }
Shinya Kitaoka 120a6e
  TPoint qtWinToWin(int x, int y) { return TPoint(x, height() - y); }
Shinya Kitaoka 120a6e
  TPointD qtWinToWorld(int x, int y) {
Shinya Kitaoka 120a6e
    return m_aff.inv() * TPointD(x, height() - y);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  TPoint winToQtWin(int x, int y) { return qtWinToWin(x, y); }
Shinya Kitaoka 120a6e
  TPointD worldToQtWin(double x, double y) {
Shinya Kitaoka 120a6e
    TPointD res(worldToWin(x, y));
Shinya Kitaoka 120a6e
    return TPointD(res.x, height() - res.y);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // World-Widget OpenGL references
Shinya Kitaoka 120a6e
  void pushGLWorldCoordinates();
Shinya Kitaoka 120a6e
  void pushGLWinCoordinates();
Shinya Kitaoka 120a6e
  void popGLCoordinates();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // View functions
Shinya Kitaoka 120a6e
  TAffine &viewAff() { return m_aff; }
Shinya Kitaoka 120a6e
  const TAffine &viewAff() const { return m_aff; }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void resetView();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void zoomIn();
Shinya Kitaoka 120a6e
  void zoomOut();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void setViewPos(double x, double y);
Shinya Kitaoka 120a6e
  void setViewZoom(double x, double y, double zoom);
Shinya Kitaoka 120a6e
  void setViewZoom(double zoom) {
Shinya Kitaoka 120a6e
    setViewZoom(0.5 * width(), 0.5 * height(), zoom);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void moveView(double dx, double dy) {
Shinya Kitaoka 120a6e
    setViewPos(m_aff.a13 + dx, m_aff.a23 + dy);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  void zoomView(double x, double y, double delta) {
Shinya Kitaoka 120a6e
    setViewZoom(x, y, m_aff.a11 * delta);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void setZoomRange(double zoomMin, double zoomMax);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // Auxiliary functions
Shinya Kitaoka 120a6e
  TRaster32P rasterBuffer();
Shinya Kitaoka 120a6e
  void flushRasterBuffer();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
protected:
Shinya Kitaoka 120a6e
  int m_xpos, m_ypos;  //!< Mouse position on mouse operations.
Shinya Kitaoka 120a6e
  TAffine m_aff;       //!< Affine transform from world to widget coords.
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  float m_bgColorF[6];  //!< Widget bg color cast in the [0, 1] interval.
Shinya Kitaoka 120a6e
  double m_chessSize;   //!< Size of the chessboard squares (default is 40).
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TRaster32P m_rasterBuffer;  //!< Auxiliary buffer used to draw on the widget
Shinya Kitaoka 38fd86
                              //! directly.
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  double m_zoomRange[2];  //!< Viewport zoom range (default: [-1024, 1024]).
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
protected:
Shinya Kitaoka 120a6e
  virtual void mouseMoveEvent(QMouseEvent *event) override;
Shinya Kitaoka 120a6e
  virtual void mousePressEvent(QMouseEvent *event) override;
Shinya Kitaoka 120a6e
  virtual void wheelEvent(QWheelEvent *event) override;
Shinya Kitaoka 120a6e
  virtual void keyPressEvent(QKeyEvent *event) override;
Shinya Kitaoka 120a6e
  virtual void hideEvent(QHideEvent *event) override;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void initializeGL() override final;
Shinya Kitaoka 120a6e
  void resizeGL(int width, int height) override final;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  GLdouble m_matrix[16];
Shinya Kitaoka 120a6e
  bool m_firstResize;
Shinya Kitaoka 120a6e
  int m_width;
Shinya Kitaoka 120a6e
  int m_height;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#endif  // PLANE_VIEWER_H