All Classes Namespaces Functions Variables Enumerations Properties Pages
camera.cpp
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 #include "camera.h"
18 
19 Camera::Camera()
20 {
21 }
22 
23 Camera::Camera(QPointF translation, qreal rotation, qreal scaling, CameraEasingType type)
24 {
25  Q_ASSERT(scaling > 0);
26  mTranslate = translation;
27  mRotate = rotation;
28  mScale = scaling;
29  mEasingType = static_cast<CameraEasingType>(type);
30  updateViewTransform();
31 }
32 
33 Camera::Camera(const Camera& c2) : KeyFrame(c2)
34 {
35  mTranslate = c2.mTranslate;
36  mRotate = c2.mRotate;
37  mScale = c2.mScale;
38  mNeedUpdateView = true;
39 }
40 
41 Camera::~Camera()
42 {
43 }
44 
45 Camera* Camera::clone() const
46 {
47  return new Camera(*this);
48 }
49 
50 void Camera::assign(const Camera& rhs)
51 {
52  mTranslate = rhs.mTranslate;
53  mRotate = rhs.mRotate;
54  mScale = rhs.mScale;
55  mNeedUpdateView = true;
56  updateViewTransform();
57  modification();
58 }
59 
60 QTransform Camera::getView()
61 {
62  if (mNeedUpdateView)
63  updateViewTransform();
64  return view;
65 }
66 
67 void Camera::reset()
68 {
69  mTranslate = QPointF(0, 0);
70  mRotate = 0.;
71  mScale = 1.;
72  mNeedUpdateView = true;
73  modification();
74 }
75 
76 void Camera::updateViewTransform()
77 {
78  if (mNeedUpdateView)
79  {
80  QTransform t;
81  t.translate(mTranslate.x(), mTranslate.y());
82 
83  QTransform r;
84  r.rotate(mRotate);
85 
86  QTransform s;
87  s.scale(mScale, mScale);
88 
89  view = t * r * s;
90  }
91  mNeedUpdateView = false;
92 }
93 
94 void Camera::translate(qreal dx, qreal dy)
95 {
96  mTranslate.setX(dx);
97  mTranslate.setY(dy);
98 
99  mNeedUpdateView = true;
100  modification();
101 }
102 
103 void Camera::translate(const QPointF pt)
104 {
105  translate(pt.x(), pt.y());
106 }
107 
108 void Camera::rotate(qreal degree)
109 {
110  mRotate = degree;
111  if (mRotate > 360)
112  {
113  mRotate = mRotate - 360;
114  }
115  else if (mRotate < 0)
116  {
117  mRotate = mRotate + 360;
118  }
119  mRotate = degree;
120 
121  mNeedUpdateView = true;
122  modification();
123 }
124 
125 void Camera::scale(qreal scaleValue)
126 {
127  mScale = scaleValue;
128 
129  mNeedUpdateView = true;
130  modification();
131 }
132 
133 void Camera::scaleWithOffset(qreal scaleValue, QPointF offset)
134 {
135  mTranslate = (mTranslate + offset) * mScale / scaleValue - offset;
136  scale(scaleValue);
137 }
138 
139 bool Camera::operator==(const Camera& rhs) const
140 {
141  bool b = (mTranslate == rhs.mTranslate)
142  && qFuzzyCompare(mRotate, rhs.mRotate)
143  && qFuzzyCompare(mScale, rhs.mScale);
144 
145  return b;
146 }
Definition: camera.h:24
QTransform & translate(qreal dx, qreal dy)
qreal x() const const
qreal y() const const
QTransform & scale(qreal sx, qreal sy)
QTransform & rotate(qreal angle, Qt::Axis axis)
void setX(qreal x)
void setY(qreal y)