1 /* 2 Copyright (c) 2014-2015 Timur Gafarov 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 6 Permission is hereby granted, free of charge, to any person or organization 7 obtaining a copy of the software and accompanying documentation covered by 8 this license (the "Software") to use, reproduce, display, distribute, 9 execute, and transmit the Software, and to prepare derivative works of the 10 Software, and to permit third-parties to whom the Software is furnished to 11 do so, all subject to the following: 12 13 The copyright notices in the Software and this entire statement, including 14 the above license grant, this restriction and the following disclaimer, 15 must be included in all copies of the Software, in whole or in part, and 16 all derivative works of the Software, unless such copies or derivative 17 works are solely in the form of machine-executable object code generated by 18 a source language processor. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 DEALINGS IN THE SOFTWARE. 27 */ 28 29 module dgl.templates.freeview; 30 31 import derelict.sdl.sdl; 32 import derelict.opengl.gl; 33 34 import dlib.core.memory; 35 import dlib.image.color; 36 37 import dgl.core.event; 38 import dgl.core.layer; 39 import dgl.graphics.tbcamera; 40 41 class FreeviewLayer: Layer 42 { 43 TrackballCamera camera; 44 int tempMouseX = 0; 45 int tempMouseY = 0; 46 47 bool grabMouse = true; 48 49 this(EventManager emngr) 50 { 51 super(emngr, LayerType.Layer3D); 52 53 camera = New!TrackballCamera(); 54 camera.pitch(45.0f); 55 camera.turn(45.0f); 56 camera.setZoom(20.0f); 57 addModifier(camera); 58 } 59 60 override void free() 61 { 62 Delete(this); 63 } 64 65 ~this() 66 { 67 camera.free(); 68 } 69 70 override void onMouseButtonDown(int button) 71 { 72 if (grabMouse) 73 { 74 if (button == SDL_BUTTON_RIGHT) 75 { 76 tempMouseX = eventManager.mouseX; 77 tempMouseY = eventManager.mouseY; 78 eventManager.setMouseToCenter(); 79 } 80 else if (button == SDL_BUTTON_MIDDLE) 81 { 82 tempMouseX = eventManager.mouseX; 83 tempMouseY = eventManager.mouseY; 84 eventManager.setMouseToCenter(); 85 } 86 else if (button == SDL_BUTTON_WHEELUP) 87 { 88 camera.zoomSmooth(-2.0f, 16.0f); 89 } 90 else if (button == SDL_BUTTON_WHEELDOWN) 91 { 92 camera.zoomSmooth(2.0f, 16.0f); 93 } 94 } 95 } 96 97 override void onMouseButtonUp(int button) 98 { 99 if (grabMouse) 100 { 101 if (button == SDL_BUTTON_RIGHT) 102 { 103 eventManager.setMouse(tempMouseX, tempMouseY); 104 } 105 else if (button == SDL_BUTTON_MIDDLE) 106 { 107 eventManager.setMouse(tempMouseX, tempMouseY); 108 } 109 } 110 } 111 112 override void draw(double dt) 113 { 114 if (grabMouse) 115 { 116 if (eventManager.mouseButtonPressed[SDL_BUTTON_RIGHT]) 117 { 118 float turn_m = (cast(float)eventManager.windowWidth/2 - eventManager.mouseX)/8.0f; 119 float pitch_m = -(cast(float)eventManager.windowHeight/2 - eventManager.mouseY)/8.0f; 120 camera.pitchSmooth(pitch_m, 16.0f); 121 camera.turnSmooth(turn_m, 16.0f); 122 eventManager.setMouseToCenter(); 123 eventManager.showCursor(false); 124 } 125 else if (eventManager.mouseButtonPressed[SDL_BUTTON_MIDDLE] || 126 (eventManager.mouseButtonPressed[SDL_BUTTON_LEFT] && eventManager.keyPressed[SDLK_LSHIFT])) 127 { 128 float shift_x = (cast(float)eventManager.windowWidth/2 - eventManager.mouseX)/16.0f; 129 float shift_y = (cast(float)eventManager.windowHeight/2 - eventManager.mouseY)/16.0f; 130 camera.moveSmooth(shift_y, 16.0f); 131 camera.strafeSmooth(-shift_x, 16.0f); 132 eventManager.setMouseToCenter(); 133 eventManager.showCursor(false); 134 } 135 else 136 eventManager.showCursor(true); 137 } 138 139 super.draw(dt); 140 } 141 }