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 <QObject>
22 #include <QTransform>
23 #include <QPainter>
24 #include "log.h"
25 #include "pencildef.h"
26 
27 #include "layer.h"
28 
29 class Object;
30 class BitmapImage;
31 class ViewManager;
32 
34 {
35  bool bPrevOnionSkin = false;
36  bool bNextOnionSkin = false;
37  int nPrevOnionSkinCount = 3;
38  int nNextOnionSkinCount = 3;
39  float fOnionSkinMaxOpacity = 0.5f;
40  float fOnionSkinMinOpacity = 0.1f;
41  bool bColorizePrevOnion = false;
42  bool bColorizeNextOnion = false;
43  bool bAntiAlias = false;
44  bool bGrid = false;
45  int nGridSizeW = 50; /* This is the grid Width IN PIXELS. The grid will scale with the image, though */
46  int nGridSizeH = 50; /* This is the grid Height IN PIXELS. The grid will scale with the image, though */
47  bool bCenter = false;
48  bool bThirds = false;
49  bool bGoldenRatio = false;
50  bool bActionSafe = true;
51  int nActionSafe = 5;
52  bool bSafeArea = false;
53  bool bTitleSafe = true;
54  int nTitleSafe = 10;
55  bool bShowSafeAreaHelperText = true;
56  bool bAxis = false;
57  bool bThinLines = false;
58  bool bOutlines = false;
59  bool bIsOnionAbsolute = false;
60  LayerVisibility eLayerVisibility = LayerVisibility::RELATED;
61  float fLayerVisibilityThreshold = 0.f;
62  float scaling = 1.0f;
63  bool isPlaying = false;
64  bool onionWhilePlayback = false;
66 };
67 
69 {
70 public:
71  explicit CanvasPainter();
72  virtual ~CanvasPainter();
73 
74  void setCanvas(QPixmap* canvas);
75  void setViewTransform(const QTransform view, const QTransform viewInverse);
76  void setOptions(const CanvasPainterOptions& p) { mOptions = p; }
77  void setTransformedSelection(QRect selection, QTransform transform);
78  void ignoreTransformedSelection();
79  QRect getCameraRect();
80 
81  void setPaintSettings(const Object* object, int currentLayer, int frame, QRect rect, BitmapImage* buffer);
82  void paint();
83  void paintCached();
84  void renderGrid(QPainter& painter);
85  void renderOverlays(QPainter& painter);
86  void resetLayerCache();
87 
88 private:
89 
96  void initializePainter(QPainter& painter, QPixmap& pixmap);
97 
98  void renderPreLayers(QPainter& painter);
99  void renderCurLayer(QPainter& painter);
100  void renderPostLayers(QPainter& painter);
101 
102  void paintBackground();
103  void paintOnionSkin(QPainter& painter);
104 
105  void renderPostLayers(QPixmap* pixmap);
106  void renderCurLayer(QPixmap* pixmap);
107  void renderPreLayers(QPixmap* pixmap);
108 
109  void paintCurrentFrame(QPainter& painter, int startLayer, int endLayer);
110 
111  void paintBitmapFrame(QPainter&, Layer* layer, int nFrame, bool colorize, bool useLastKeyFrame, bool isCurrentFrame);
112  void paintVectorFrame(QPainter&, Layer* layer, int nFrame, bool colorize, bool useLastKeyFrame, bool isCurrentFrame);
113 
114  void paintTransformedSelection(QPainter& painter);
115  void paintGrid(QPainter& painter);
116  void paintOverlayCenter(QPainter& painter);
117  void paintOverlayThirds(QPainter& painter);
118  void paintOverlayGolden(QPainter& painter);
119  void paintOverlaySafeAreas(QPainter& painter);
120  void paintCameraBorder(QPainter& painter);
121  void paintAxis(QPainter& painter);
122  void prescale(BitmapImage* bitmapImage);
123 
125  qreal calculateRelativeOpacityForLayer(int layerIndex) const;
126 
127 private:
128  CanvasPainterOptions mOptions;
129 
130  const Object* mObject = nullptr;
131  QPixmap* mCanvas = nullptr;
132  QTransform mViewTransform;
133  QTransform mViewInverse;
134 
135  QRect mCameraRect;
136 
137  int mCurrentLayerIndex = 0;
138  int mFrameNumber = 0;
139  BitmapImage* mBuffer = nullptr;
140 
141  QImage mScaledBitmap;
142 
143  bool bMultiLayerOnionSkin = false;
144 
145  // Handle selection transformation
146  bool mRenderTransform = false;
147  QRect mSelection;
148  QTransform mSelectionTransform;
149 
150  // Caches specifically for when drawing on the canvas
151  std::unique_ptr<QPixmap> mPreLayersCache, mPostLayersCache;
152 
153  const static int OVERLAY_SAFE_CENTER_CROSS_SIZE = 25;
154 };
155 
156 #endif // CANVASRENDERER_H
void paintCurrentFrame(QPainter &painter, int startLayer, int endLayer)
Paints layers within the specified range for the current frame.
Definition: layer.h:39
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:54