Power System Platform  2026w10a-beta
Loading...
Searching...
No Matches
Camera.cpp
1/*
2 * Copyright (C) 2017 Thales Lima Oliveira <thales@ufu.br>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#include "Camera.h"
19
20Camera::Camera()
21{
22 m_translation = wxPoint2DDouble(0, 0);
23 m_scale = 1.0;
24}
25
26Camera::~Camera() {}
27wxPoint2DDouble Camera::ScreenToWorld(wxPoint2DDouble screenCoords) const
28{
29 return wxPoint2DDouble(screenCoords.m_x / m_scale - m_translation.m_x,
30 screenCoords.m_y / m_scale - m_translation.m_y);
31}
32
33void Camera::SetTranslation(wxPoint2DDouble screenPoint)
34{
35 m_translation = screenPoint / m_scale - m_translationStartPt;
36}
37
38void Camera::SetScale(wxPoint2DDouble screenPoint, double delta)
39{
40 m_translation -= screenPoint * (1.0 - m_scale) / m_scale;
41
42 m_scale += delta;
43
44 // Limits: 5% - 300%
45 if(m_scale < m_zoomMin) m_scale = m_zoomMin;
46 if(m_scale > m_zoomMax) m_scale = m_zoomMax;
47
48 m_translation += screenPoint * (1.0 - m_scale) / m_scale;
49}
50
51wxPoint2DDouble Camera::GetMousePosition(bool worldCoords) const
52{
53 if(worldCoords) return ScreenToWorld(m_mousePosition);
54 return m_mousePosition;
55}