]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/camera/listener.cpp
Merge branch 'fix-fast' into 'master'
[xonotic/netradiant.git] / contrib / camera / listener.cpp
1 /*
2    Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3    For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 /*
23    Camera plugin for GtkRadiant
24    Copyright (C) 2002 Splash Damage Ltd.
25  */
26
27 #include "camera.h"
28
29 CListener::CListener()
30 {
31     refCount = 1;
32
33     m_bHooked = FALSE;
34
35     m_bLeftMBPressed = m_bRightMBPressed = m_bMiddleMBPressed = false;
36
37     oldValid = false;
38
39     Register();
40 }
41
42 CListener::~CListener()
43 {
44     UnRegister();
45 }
46
47 void CListener::Register()
48 {
49     g_UITable.m_pfnHookWindow(this);
50     g_pXYWndWrapper = g_UITable.m_pfnGetXYWndWrapper();
51     m_bHooked = TRUE;
52 }
53
54 void CListener::UnRegister()
55 {
56     if (m_bHooked) {
57         g_UITable.m_pfnUnHookWindow(this);
58         g_pXYWndWrapper = NULL;
59         m_bHooked = FALSE;
60     }
61 }
62
63 bool CListener::OnMouseMove(unsigned int nFlags, double x, double y)
64 {
65     SetViewType(g_pXYWndWrapper->GetViewType());
66
67     if (m_bLeftMBPressed && oldValid && g_iEditMode == 0) {
68         vec3_t click, delta;
69
70         g_pXYWndWrapper->SnapToGrid((int) x, (int) y, click);
71
72         switch (m_vt) {
73             case XY:
74                 VectorSet(delta, click[0] - old_x, click[1] - old_y, 0);
75                 old_x = click[0];
76                 old_y = click[1];
77                 break;
78             case XZ:
79                 VectorSet(delta, click[0] - old_x, 0, click[2] - old_y);
80                 old_x = click[0];
81                 old_y = click[2];
82                 break;
83             case YZ:
84                 VectorSet(delta, 0, click[1] - old_x, click[2] - old_y);
85                 old_x = click[1];
86                 old_y = click[2];
87                 break;
88         }
89
90         if (g_iActiveTarget < 0) {
91             GetCurrentCam()->GetCam()->getPositionObj()->updateSelection(delta[0], delta[1], delta[2]);
92         } else {
93             GetCurrentCam()->GetCam()->getActiveTarget(g_iActiveTarget)->updateSelection(delta[0], delta[1], delta[2]);
94         }
95
96         GetCurrentCam()->HasBeenModified();
97
98         g_FuncTable.m_pfnSysUpdateWindows(W_XY_OVERLAY | W_CAMERA);
99
100         return true;
101     }
102
103     return false;
104 }
105
106 bool CListener::OnLButtonDown(unsigned int nFlags, double x, double y)
107 {
108     SetViewType(g_pXYWndWrapper->GetViewType());
109
110     m_bLeftMBPressed = true;
111     oldValid = true;
112
113     vec3_t org, delta;
114
115     g_pXYWndWrapper->SnapToGrid((int) x, (int) y, org);
116
117     switch (m_vt) {
118         case XY:
119             old_x = org[0];
120             old_y = org[1];
121             org[2] = 64 * 1024;
122             VectorSet(delta, 0, 0, -1);
123             break;
124         case XZ:
125             old_x = org[0];
126             old_y = org[2];
127             org[1] = 64 * 1024;
128             VectorSet(delta, 0, -1, 0);
129             break;
130         case YZ:
131             old_x = org[1];
132             old_y = org[2];
133             org[0] = 64 * 1024;
134             VectorSet(delta, -1, 0, 0);
135             break;
136     }
137
138     if (g_iEditMode == 0) {
139         if (g_iActiveTarget < 0) {
140             GetCurrentCam()->GetCam()->getPositionObj()->selectPointByRay(org[0], org[1], org[2], delta[0], delta[1],
141                                                                           delta[2], true);
142         } else {
143             GetCurrentCam()->GetCam()->getActiveTarget(g_iActiveTarget)->selectPointByRay(org[0], org[1], org[2],
144                                                                                           delta[0], delta[1], delta[2],
145                                                                                           true);
146         }
147     } else if (g_iEditMode == 1) {
148         idVec3 *lastcoord;
149         idCameraPosition *camera;
150
151         if (g_iActiveTarget < 0) {
152             camera = GetCurrentCam()->GetCam()->getPositionObj();
153         } else {
154             camera = GetCurrentCam()->GetCam()->getActiveTarget(g_iActiveTarget);
155         }
156
157         if (camera->numPoints()) {
158             lastcoord = camera->getPoint(camera->numPoints() - 1);
159             switch (m_vt) {
160                 case XY:
161                     camera->addPoint(org[0], org[1], lastcoord->z);
162                     break;
163                 case XZ:
164                     camera->addPoint(org[0], lastcoord->y, org[2]);
165                     break;
166                 case YZ:
167                     camera->addPoint(lastcoord->x, org[1], org[2]);
168                     break;
169             }
170         } else {
171             switch (m_vt) {
172                 case XY:
173                     camera->addPoint(org[0], org[1], 0);
174                     break;
175                 case XZ:
176                     camera->addPoint(org[0], 0, org[2]);
177                     break;
178                 case YZ:
179                     camera->addPoint(0, org[1], org[2]);
180                     break;
181             }
182         }
183
184         GetCurrentCam()->HasBeenModified();
185     }
186
187     g_FuncTable.m_pfnSysUpdateWindows(W_XY_OVERLAY | W_CAMERA);
188
189     return true;
190
191     //return false;
192 }
193
194 bool CListener::OnLButtonUp(unsigned int nFlags, double x, double y)
195 {
196     SetViewType(g_pXYWndWrapper->GetViewType());
197
198     m_bLeftMBPressed = false;
199     oldValid = false;
200
201     if (g_iEditMode == 0) {
202         if (g_iActiveTarget < 0) {
203             GetCurrentCam()->GetCam()->getPositionObj()->deselectAll();
204         } else {
205             GetCurrentCam()->GetCam()->getActiveTarget(g_iActiveTarget)->deselectAll();
206         }
207
208         g_FuncTable.m_pfnSysUpdateWindows(W_XY_OVERLAY | W_CAMERA);
209     }
210
211     return false;
212 }
213
214 bool CListener::OnRButtonDown(unsigned int nFlags, double x, double y)
215 {
216     SetViewType(g_pXYWndWrapper->GetViewType());
217
218     m_bRightMBPressed = true;
219
220     return false;
221 }
222
223 bool CListener::OnRButtonUp(unsigned int nFlags, double x, double y)
224 {
225     SetViewType(g_pXYWndWrapper->GetViewType());
226
227     m_bRightMBPressed = false;
228
229     return false;
230 }
231
232 bool CListener::OnMButtonDown(unsigned int nFlags, double x, double y)
233 {
234     SetViewType(g_pXYWndWrapper->GetViewType());
235
236     m_bMiddleMBPressed = true;
237
238     return false;
239 }
240
241 bool CListener::OnMButtonUp(unsigned int nFlags, double x, double y)
242 {
243     SetViewType(g_pXYWndWrapper->GetViewType());
244
245     m_bMiddleMBPressed = false;
246
247     return false;
248 }