1 /* 2 Copyright (c) 2017-2018 Timur Gafarov 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 Permission is hereby granted, free of charge, to any person or organization 6 obtaining a copy of the software and accompanying documentation covered by 7 this license (the "Software") to use, reproduce, display, distribute, 8 execute, and transmit the Software, and to prepare derivative works of the 9 Software, and to permit third-parties to whom the Software is furnished to 10 do so, all subject to the following: 11 12 The copyright notices in the Software and this entire statement, including 13 the above license grant, this restriction and the following disclaimer, 14 must be included in all copies of the Software, in whole or in part, and 15 all derivative works of the Software, unless such copies or derivative 16 works are solely in the form of machine-executable object code generated by 17 a source language processor. 18 19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 22 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 23 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 DEALINGS IN THE SOFTWARE. 26 */ 27 28 module dagon.graphics.freeview; 29 30 import dlib.core.memory; 31 import dlib.math.vector; 32 import dlib.math.matrix; 33 import dlib.math.quaternion; 34 35 import dagon.core.libs; 36 import dagon.core.ownership; 37 import dagon.core.event; 38 import dagon.core.keycodes; 39 import dagon.graphics.tbcamera; 40 import dagon.graphics.view; 41 42 class Freeview: EventListener, View 43 { 44 TrackballCamera camera; 45 int prevMouseX; 46 int prevMouseY; 47 48 this(EventManager emngr, Owner owner) 49 { 50 super(emngr, owner); 51 camera = New!TrackballCamera(); 52 camera.pitch(45.0f); 53 camera.turn(45.0f); 54 camera.setZoom(20.0f); 55 } 56 57 void reset() 58 { 59 camera.reset(); 60 camera.pitch(45.0f); 61 camera.turn(45.0f); 62 camera.setZoom(20.0f); 63 camera.update(1.0 / 60.0); 64 } 65 66 ~this() 67 { 68 Delete(camera); 69 } 70 71 override void onMouseButtonDown(int button) 72 { 73 if (button == MB_LEFT) 74 { 75 prevMouseX = eventManager.mouseX; 76 prevMouseY = eventManager.mouseY; 77 } 78 } 79 80 override void onMouseWheel(int x, int y) 81 { 82 camera.zoom(cast(float)y * 0.2f); 83 } 84 85 void setZoom(float z) 86 { 87 camera.setZoom(z); 88 } 89 90 void update(double dt) 91 { 92 processEvents(); 93 94 if (eventManager.mouseButtonPressed[MB_RIGHT]) 95 { 96 float shift_x = (eventManager.mouseX - prevMouseX) * 0.1f; 97 float shift_y = -(eventManager.mouseY - prevMouseY) * 0.1f; 98 Vector3f trans = camera.getUpVector * shift_y + camera.getRightVector * shift_x; 99 camera.translateTarget(trans); 100 } 101 else if (eventManager.mouseButtonPressed[MB_LEFT] && eventManager.keyPressed[KEY_LCTRL]) 102 { 103 float shift_x = (eventManager.mouseX - prevMouseX); 104 float shift_y = (eventManager.mouseY - prevMouseY); 105 camera.zoom((shift_x + shift_y) * 0.1f); 106 } 107 else if (eventManager.mouseButtonPressed[MB_LEFT]) 108 { 109 float turn_m = (eventManager.mouseX - prevMouseX); 110 float pitch_m = (eventManager.mouseY - prevMouseY); 111 camera.pitch(pitch_m); 112 camera.turn(turn_m); 113 } 114 115 prevMouseX = eventManager.mouseX; 116 prevMouseY = eventManager.mouseY; 117 118 camera.update(dt); 119 } 120 121 Matrix4x4f viewMatrix() 122 { 123 return camera.viewMatrix(); 124 } 125 126 Matrix4x4f invViewMatrix() 127 { 128 return camera.invViewMatrix(); 129 } 130 131 132 Vector3f cameraPosition() 133 { 134 return camera.getPosition(); 135 } 136 } 137