Pencil2D Animation
Download Community News Docs Contribute
  • Overview
  • Articles
  • Code
  •  
  • Class List
  • Class Index
  • Class Hierarchy
  • Class Members
  • File List
Loading...
Searching...
No Matches
  • core_lib
  • src
onionskinsubpainter.cpp
1/*
2
3Pencil2D - Traditional Animation Software
4Copyright (C) 2012-2020 Matthew Chiawen Chang
5
6This program is free software; you can redistribute it and/or
7modify it under the terms of the GNU General Public License
8as published by the Free Software Foundation; version 2 of the License.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15*/
16#include "onionskinsubpainter.h"
17
18#include <QPainter>
19
20#include "keyframe.h"
21#include "layer.h"
22#include "onionskinpainteroptions.h"
23
24OnionSkinSubPainter::OnionSkinSubPainter()
25{
26}
27
28void OnionSkinSubPainter::paint(QPainter& painter, const Layer* layer, const OnionSkinPainterOptions& options, int frameIndex, const std::function<void(OnionSkinPaintState, int)>& state) const
29{
30 if (!options.enabledWhilePlaying && options.isPlaying) { return; }
31
32 if (layer->visible() == false)
33 return;
34
35 if (layer->keyFrameCount() == 0)
36 return;
37
38 qreal minOpacity = static_cast<qreal>(options.minOpacity / 100);
39 qreal maxOpacity = static_cast<qreal>(options.maxOpacity / 100);
40
41 if (options.skinPrevFrames && frameIndex >= 1)
42 {
43 // Paint onion skin before current frame.
44 qreal prevOpacityIncrement = (maxOpacity - minOpacity) / options.framesToSkinPrev;
45 qreal opacity = maxOpacity;
46
47 int onionFrameNumber = layer->getPreviousFrameNumber(frameIndex, options.isAbsolute);
48 KeyFrame* currentAbsoluteFrame = layer->getLastKeyFrameAtPosition(frameIndex);
49 int currentAbsoluteFrameNumber = currentAbsoluteFrame ? currentAbsoluteFrame->pos() : -1;
50
51 int onionPosition = 0;
52 while (onionPosition < options.framesToSkinPrev)
53 {
54 // We've gone below the first frame, stop iterating
55 if (onionFrameNumber < 1) {
56 break;
57 }
58 painter.setOpacity(opacity);
59
60 // When in absolute mode, we don't skin the current absolute frame
61 // otherwise, if absolute is off and the current frame is in range, will be painted
62 if (!options.isAbsolute || onionFrameNumber != currentAbsoluteFrameNumber) {
63 state(OnionSkinPaintState::PREV, onionFrameNumber);
64 opacity = opacity - prevOpacityIncrement;
65 onionPosition++;
66 }
67
68 onionFrameNumber = layer->getPreviousFrameNumber(onionFrameNumber, options.isAbsolute);
69 }
70 }
71
72 state(OnionSkinPaintState::CURRENT, frameIndex);
73
74 if (options.skinNextFrames)
75 {
76 // Paint onion skin after current frame.
77 qreal nextOpacityIncrement = (maxOpacity - minOpacity) / options.framesToSkinNext;
78 qreal opacity = maxOpacity;
79
80 int onionFrameNumber = layer->getNextFrameNumber(frameIndex, options.isAbsolute);
81 int onionPosition = 0;
82
83 while (onionPosition < options.framesToSkinNext && onionFrameNumber > 0)
84 {
85 painter.setOpacity(opacity);
86
87 state(OnionSkinPaintState::NEXT, onionFrameNumber);
88 opacity = opacity - nextOpacityIncrement;
89
90 onionFrameNumber = layer->getNextFrameNumber(onionFrameNumber, options.isAbsolute);
91 onionPosition++;
92 }
93 }
94}
KeyFrame
Definition: keyframe.h:30
Layer
Definition: layer.h:33
QPainter
QPainter::setOpacity
void setOpacity(qreal opacity)
OnionSkinPainterOptions
Definition: onionskinpainteroptions.h:20
Generated on Thu May 8 2025 04:47:53 for Pencil2D by doxygen 1.9.6 based on revision 4513250b1d5b1a3676ec0e67b06b7a885ceaae39