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