All Classes Namespaces Functions Variables Enumerations Properties Pages
canvaspainter.h
1 /*
2 
3 Pencil2D - Traditional Animation Software
4 Copyright (C) 2012-2020 Matthew Chiawen Chang
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; version 2 of the License.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 */
16 
17 #ifndef CANVASPAINTER_H
18 #define CANVASPAINTER_H
19 
20 #include <memory>
21 #include <QCoreApplication>
22 #include <QObject>
23 #include <QTransform>
24 #include <QPainter>
25 #include "log.h"
26 #include "pencildef.h"
27 
28 #include "layer.h"
29 
30 class Object;
31 class BitmapImage;
32 class ViewManager;
33 
35 {
36  bool bPrevOnionSkin = false;
37  bool bNextOnionSkin = false;
38  int nPrevOnionSkinCount = 3;
39  int nNextOnionSkinCount = 3;
40  float fOnionSkinMaxOpacity = 0.5f;
41  float fOnionSkinMinOpacity = 0.1f;
42  bool bColorizePrevOnion = false;
43  bool bColorizeNextOnion = false;
44  bool bAntiAlias = false;
45  bool bGrid = false;
46  int nGridSizeW = 50; /* This is the grid Width IN PIXELS. The grid will scale with the image, though */
47  int nGridSizeH = 50; /* This is the grid Height IN PIXELS. The grid will scale with the image, though */
48  bool bAxis = false;
49  bool bThinLines = false;
50  bool bOutlines = false;
51  bool bIsOnionAbsolute = false;
52  LayerVisibility eLayerVisibility = LayerVisibility::RELATED;
53  float fLayerVisibilityThreshold = 0.f;
54  float scaling = 1.0f;
55  bool isPlaying = false;
56  bool onionWhilePlayback = false;
58 };
59 
61 {
62  Q_DECLARE_TR_FUNCTIONS(CanvasPainter)
63 public:
64  explicit CanvasPainter();
65  virtual ~CanvasPainter();
66 
67  void setCanvas(QPixmap* canvas);
68  void setViewTransform(const QTransform view, const QTransform viewInverse);
69  void setOptions(const CanvasPainterOptions& p) { mOptions = p; }
70  void setTransformedSelection(QRect selection, QTransform transform);
71  void ignoreTransformedSelection();
72  QRect getCameraRect();
73 
74  void setPaintSettings(const Object* object, int currentLayer, int frame, QRect rect, BitmapImage* buffer);
75  void paint();
76  void paintCached();
77  void renderGrid(QPainter& painter);
78  void resetLayerCache();
79 
80 private:
81 
88  void initializePainter(QPainter& painter, QPixmap& pixmap);
89 
90  void renderPreLayers(QPainter& painter);
91  void renderCurLayer(QPainter& painter);
92  void renderPostLayers(QPainter& painter);
93 
94  void paintBackground();
95  void paintOnionSkin(QPainter& painter);
96 
97  void renderPostLayers(QPixmap* pixmap);
98  void renderCurLayer(QPixmap* pixmap);
99  void renderPreLayers(QPixmap* pixmap);
100 
101  void paintCurrentFrame(QPainter& painter, int startLayer, int endLayer);
102 
103  void paintBitmapFrame(QPainter&, Layer* layer, int nFrame, bool colorize, bool useLastKeyFrame, bool isCurrentFrame);
104  void paintVectorFrame(QPainter&, Layer* layer, int nFrame, bool colorize, bool useLastKeyFrame, bool isCurrentFrame);
105 
106  void paintTransformedSelection(QPainter& painter);
107  void paintGrid(QPainter& painter);
108  void paintCameraBorder(QPainter& painter);
109  void paintAxis(QPainter& painter);
110  void prescale(BitmapImage* bitmapImage);
111 
113  qreal calculateRelativeOpacityForLayer(int layerIndex) const;
114 
115 private:
116  CanvasPainterOptions mOptions;
117 
118  const Object* mObject = nullptr;
119  QPixmap* mCanvas = nullptr;
120  QTransform mViewTransform;
121  QTransform mViewInverse;
122 
123  QRect mCameraRect;
124 
125  int mCurrentLayerIndex = 0;
126  int mFrameNumber = 0;
127  BitmapImage* mBuffer = nullptr;
128 
129  QImage mScaledBitmap;
130 
131  // Handle selection transformation
132  bool mRenderTransform = false;
133  QRect mSelection;
134  QTransform mSelectionTransform;
135 
136  // Caches specifically for when drawing on the canvas
137  std::unique_ptr<QPixmap> mPreLayersCache, mPostLayersCache;
138 
139  const static int OVERLAY_SAFE_CENTER_CROSS_SIZE = 25;
140 };
141 
142 #endif // CANVASRENDERER_H
void paintCurrentFrame(QPainter &painter, int startLayer, int endLayer)
Paints layers within the specified range for the current frame.
Definition: layer.h:38
void initializePainter(QPainter &painter, QPixmap &pixmap)
CanvasPainter::initializePainter Enriches the painter with a context and sets it's initial matrix...
qreal calculateRelativeOpacityForLayer(int layerIndex) const
Calculate layer opacity based on current layer offset.
Definition: object.h:41