1 /*
2  Copyright (C) 2010-2014 Kristian Duske
3 
4  This file is part of TrenchBroom.
5 
6  TrenchBroom is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  TrenchBroom is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with TrenchBroom. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "UVCameraTool.h"
21 
22 #include "VecMath.h"
23 #include "View/InputState.h"
24 #include "Renderer/OrthographicCamera.h"
25 
26 namespace TrenchBroom {
27     namespace View {
UVCameraTool(Renderer::OrthographicCamera & camera)28         UVCameraTool::UVCameraTool(Renderer::OrthographicCamera& camera) :
29         ToolControllerBase(),
30         Tool(true),
31         m_camera(camera) {}
32 
doGetTool()33         Tool* UVCameraTool::doGetTool() {
34             return this;
35         }
36 
doMouseScroll(const InputState & inputState)37         void UVCameraTool::doMouseScroll(const InputState& inputState) {
38             const Vec3f oldWorldPos = m_camera.unproject(static_cast<float>(inputState.mouseX()),
39                                                          static_cast<float>(inputState.mouseY()),
40                                                          0.0f);
41 
42             if (inputState.scrollY() > 0)
43                 m_camera.zoom(1.1f);
44             else
45                 m_camera.zoom(1.0f / 1.1f);
46 
47             const Vec3f newWorldPos = m_camera.unproject(static_cast<float>(inputState.mouseX()),
48                                                          static_cast<float>(inputState.mouseY()),
49                                                          0.0f);
50 
51             const Vec3f delta = oldWorldPos - newWorldPos;
52             m_camera.moveBy(delta);
53         }
54 
doStartMouseDrag(const InputState & inputState)55         bool UVCameraTool::doStartMouseDrag(const InputState& inputState) {
56             return inputState.mouseButtonsPressed(MouseButtons::MBRight) || inputState.mouseButtonsPressed(MouseButtons::MBMiddle);
57         }
58 
doMouseDrag(const InputState & inputState)59         bool UVCameraTool::doMouseDrag(const InputState& inputState) {
60             const int oldX = inputState.mouseX() - inputState.mouseDX();
61             const int oldY = inputState.mouseY() - inputState.mouseDY();
62 
63             const Vec3f oldWorldPos = m_camera.unproject(static_cast<float>(oldX),
64                                                          static_cast<float>(oldY),
65                                                          0.0f);
66             const Vec3f newWorldPos = m_camera.unproject(static_cast<float>(inputState.mouseX()),
67                                                          static_cast<float>(inputState.mouseY()),
68                                                          0.0f);
69             const Vec3f delta = oldWorldPos - newWorldPos;
70             m_camera.moveBy(delta);
71             return true;
72         }
73 
doEndMouseDrag(const InputState & inputState)74         void UVCameraTool::doEndMouseDrag(const InputState& inputState) {}
75 
doCancelMouseDrag()76         void UVCameraTool::doCancelMouseDrag() {}
77 
doCancel()78         bool UVCameraTool::doCancel() {
79             return false;
80         }
81     }
82 }
83