drag.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1997-2006 Id Software, Inc.
  4. This file is part of Quake 2 Tools source code.
  5. Quake 2 Tools source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake 2 Tools source code is distributed in the hope that it will be
  10. useful, 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. You should have received a copy of the GNU General Public License
  14. along with Quake 2 Tools source code; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. #include "qe3.h"
  19. /*
  20. drag either multiple brushes, or select plane points from
  21. a single brush.
  22. */
  23. qboolean drag_ok;
  24. vec3_t drag_xvec;
  25. vec3_t drag_yvec;
  26. static int buttonstate;
  27. static int pressx, pressy;
  28. static vec3_t pressdelta;
  29. static int buttonx, buttony;
  30. //int num_move_points;
  31. //float *move_points[1024];
  32. int lastx, lasty;
  33. qboolean drag_first;
  34. void AxializeVector (vec3_t v)
  35. {
  36. vec3_t a;
  37. float o;
  38. int i;
  39. if (!v[0] && !v[1])
  40. return;
  41. if (!v[1] && !v[2])
  42. return;
  43. if (!v[0] && !v[2])
  44. return;
  45. for (i=0 ; i<3 ; i++)
  46. a[i] = fabs(v[i]);
  47. if (a[0] > a[1] && a[0] > a[2])
  48. i = 0;
  49. else if (a[1] > a[0] && a[1] > a[2])
  50. i = 1;
  51. else
  52. i = 2;
  53. o = v[i];
  54. VectorCopy (vec3_origin, v);
  55. if (o<0)
  56. v[i] = -1;
  57. else
  58. v[i] = 1;
  59. }
  60. /*
  61. ===========
  62. Drag_Setup
  63. ===========
  64. */
  65. void Drag_Setup (int x, int y, int buttons,
  66. vec3_t xaxis, vec3_t yaxis,
  67. vec3_t origin, vec3_t dir)
  68. {
  69. trace_t t;
  70. face_t *f;
  71. if (selected_brushes.next == &selected_brushes)
  72. {
  73. Sys_Status("No selection to drag\n", 0);
  74. return;
  75. }
  76. drag_first = true;
  77. g_qeglobals.d_num_move_points = 0;
  78. VectorCopy (vec3_origin, pressdelta);
  79. pressx = x;
  80. pressy = y;
  81. VectorCopy (xaxis, drag_xvec);
  82. AxializeVector (drag_xvec);
  83. VectorCopy (yaxis, drag_yvec);
  84. AxializeVector (drag_yvec);
  85. if (g_qeglobals.d_select_mode == sel_vertex)
  86. {
  87. SelectVertexByRay (origin, dir);
  88. if (g_qeglobals.d_num_move_points)
  89. {
  90. drag_ok = true;
  91. return;
  92. }
  93. }
  94. if (g_qeglobals.d_select_mode == sel_edge)
  95. {
  96. SelectEdgeByRay (origin, dir);
  97. if (g_qeglobals.d_num_move_points)
  98. {
  99. drag_ok = true;
  100. return;
  101. }
  102. }
  103. //
  104. // check for direct hit first
  105. //
  106. t = Test_Ray (origin, dir, true);
  107. if (t.selected)
  108. {
  109. drag_ok = true;
  110. if (buttons == (MK_LBUTTON|MK_CONTROL) )
  111. {
  112. Sys_Printf ("Shear dragging face\n");
  113. Brush_SelectFaceForDragging (t.brush, t.face, true);
  114. }
  115. else if (buttons == (MK_LBUTTON|MK_CONTROL|MK_SHIFT) )
  116. {
  117. Sys_Printf ("Sticky dragging brush\n");
  118. for (f=t.brush->brush_faces ; f ; f=f->next)
  119. Brush_SelectFaceForDragging (t.brush, f, false);
  120. }
  121. else
  122. Sys_Printf ("Dragging entire selection\n");
  123. return;
  124. }
  125. if (g_qeglobals.d_select_mode == sel_vertex || g_qeglobals.d_select_mode == sel_edge)
  126. return;
  127. //
  128. // check for side hit
  129. //
  130. if (selected_brushes.next->next != &selected_brushes)
  131. {
  132. Sys_Printf ("Click isn't inside multiple selection\n");
  133. return;
  134. }
  135. if (selected_brushes.next->owner->eclass->fixedsize)
  136. {
  137. Sys_Printf ("Can't stretch fixed size entities\n");
  138. return;
  139. }
  140. if (buttons & MK_CONTROL)
  141. Brush_SideSelect (selected_brushes.next, origin, dir, true);
  142. else
  143. Brush_SideSelect (selected_brushes.next, origin, dir, false);
  144. Sys_Printf ("Side stretch\n");
  145. drag_ok = true;
  146. }
  147. entity_t *peLink;
  148. void UpdateTarget(vec3_t origin, vec3_t dir)
  149. {
  150. trace_t t;
  151. entity_t *pe;
  152. int i;
  153. char sz[128];
  154. t = Test_Ray (origin, dir, 0);
  155. if (!t.brush)
  156. return;
  157. pe = t.brush->owner;
  158. if (pe == NULL)
  159. return;
  160. // is this the first?
  161. if (peLink != NULL)
  162. {
  163. // Get the target id from out current target
  164. // if there is no id, make one
  165. i = IntForKey(pe, "target");
  166. if (i <= 0)
  167. {
  168. i = GetUniqueTargetId(1);
  169. sprintf(sz, "%d", i);
  170. SetKeyValue(pe, "target", sz);
  171. }
  172. // set the target # into our src
  173. sprintf(sz, "%d", i);
  174. SetKeyValue(peLink, "targetname", sz);
  175. Sys_UpdateWindows(W_ENTITY);
  176. }
  177. // promote the target to the src
  178. peLink = pe;
  179. }
  180. /*
  181. ===========
  182. Drag_Begin
  183. ===========
  184. */
  185. void Drag_Begin (int x, int y, int buttons,
  186. vec3_t xaxis, vec3_t yaxis,
  187. vec3_t origin, vec3_t dir)
  188. {
  189. trace_t t;
  190. drag_ok = false;
  191. VectorCopy (vec3_origin, pressdelta);
  192. drag_first = true;
  193. peLink = NULL;
  194. // shift LBUTTON = select entire brush
  195. if (buttons == (MK_LBUTTON | MK_SHIFT))
  196. {
  197. if (!dir[0] && !dir[1])
  198. Select_Ray (origin, dir, SF_ENTITIES_FIRST); // hack for XY
  199. else
  200. Select_Ray (origin, dir, 0);
  201. return;
  202. }
  203. // ctrl-shift LBUTTON = select single face
  204. if (buttons == (MK_LBUTTON | MK_CONTROL | MK_SHIFT))
  205. {
  206. Select_Deselect ();
  207. Select_Ray (origin, dir, SF_SINGLEFACE);
  208. return;
  209. }
  210. // LBUTTON + all other modifiers = manipulate selection
  211. if (buttons & MK_LBUTTON)
  212. {
  213. Drag_Setup (x, y, buttons, xaxis, yaxis, origin, dir);
  214. return;
  215. }
  216. // middle button = grab texture
  217. if (buttons == MK_MBUTTON)
  218. {
  219. t = Test_Ray (origin, dir, false);
  220. if (t.face)
  221. {
  222. g_qeglobals.d_new_brush_bottom_z = t.brush->mins[2];
  223. g_qeglobals.d_new_brush_top_z = t.brush->maxs[2];
  224. Texture_SetTexture (&t.face->texdef);
  225. }
  226. else
  227. Sys_Printf ("Did not select a texture\n");
  228. return;
  229. }
  230. // ctrl-middle button = set entire brush to texture
  231. if (buttons == (MK_MBUTTON|MK_CONTROL) )
  232. {
  233. t = Test_Ray (origin, dir, false);
  234. if (t.brush)
  235. {
  236. if (t.brush->brush_faces->texdef.name[0] == '(')
  237. Sys_Printf ("Can't change an entity texture\n");
  238. else
  239. {
  240. Brush_SetTexture (t.brush, &g_qeglobals.d_texturewin.texdef);
  241. Sys_UpdateWindows (W_ALL);
  242. }
  243. }
  244. else
  245. Sys_Printf ("Didn't hit a btrush\n");
  246. return;
  247. }
  248. // ctrl-shift-middle button = set single face to texture
  249. if (buttons == (MK_MBUTTON|MK_SHIFT|MK_CONTROL) )
  250. {
  251. t = Test_Ray (origin, dir, false);
  252. if (t.brush)
  253. {
  254. if (t.brush->brush_faces->texdef.name[0] == '(')
  255. Sys_Printf ("Can't change an entity texture\n");
  256. else
  257. {
  258. t.face->texdef = g_qeglobals.d_texturewin.texdef;
  259. Brush_Build( t.brush );
  260. Sys_UpdateWindows (W_ALL);
  261. }
  262. }
  263. else
  264. Sys_Printf ("Didn't hit a btrush\n");
  265. return;
  266. }
  267. }
  268. /*
  269. ===========
  270. MoveSelection
  271. ===========
  272. */
  273. void MoveSelection (vec3_t move)
  274. {
  275. int i;
  276. brush_t *b;
  277. if (!move[0] && !move[1] && !move[2])
  278. return;
  279. Sys_UpdateWindows (W_XY|W_CAMERA);
  280. //
  281. // dragging only a part of the selection
  282. //
  283. if (g_qeglobals.d_num_move_points)
  284. {
  285. for (i=0 ; i<g_qeglobals.d_num_move_points ; i++)
  286. VectorAdd (g_qeglobals.d_move_points[i], move, g_qeglobals.d_move_points[i]);
  287. for (b=selected_brushes.next ; b != &selected_brushes ; b=b->next)
  288. {
  289. Brush_Build( b );
  290. for (i=0 ; i<3 ; i++)
  291. if (b->mins[i] > b->maxs[i]
  292. || b->maxs[i] - b->mins[i] > 4096)
  293. break; // dragged backwards or fucked up
  294. if (i != 3)
  295. break;
  296. }
  297. // if any of the brushes were crushed out of existance
  298. // calcel the entire move
  299. if (b != &selected_brushes)
  300. {
  301. Sys_Printf ("Brush dragged backwards, move canceled\n");
  302. for (i=0 ; i<g_qeglobals.d_num_move_points ; i++)
  303. VectorSubtract (g_qeglobals.d_move_points[i], move, g_qeglobals.d_move_points[i]);
  304. for (b=selected_brushes.next ; b != &selected_brushes ; b=b->next)
  305. Brush_Build( b );
  306. }
  307. }
  308. else
  309. {
  310. //
  311. // if there are lots of brushes selected, just translate instead
  312. // of rebuilding the brushes
  313. //
  314. if (drag_yvec[2] == 0 && selected_brushes.next->next != &selected_brushes)
  315. {
  316. VectorAdd (g_qeglobals.d_select_translate, move, g_qeglobals.d_select_translate);
  317. }
  318. else
  319. {
  320. Select_Move (move);
  321. }
  322. }
  323. }
  324. /*
  325. ===========
  326. Drag_MouseMoved
  327. ===========
  328. */
  329. void Drag_MouseMoved (int x, int y, int buttons)
  330. {
  331. vec3_t move, delta;
  332. int i;
  333. char movestring[128];
  334. if (!buttons)
  335. {
  336. drag_ok = false;
  337. return;
  338. }
  339. if (!drag_ok)
  340. return;
  341. // clear along one axis
  342. if (buttons & MK_SHIFT)
  343. {
  344. drag_first = false;
  345. if (abs(x-pressx) > abs(y-pressy))
  346. y = pressy;
  347. else
  348. x = pressx;
  349. }
  350. for (i=0 ; i<3 ; i++)
  351. {
  352. move[i] = drag_xvec[i]*(x - pressx)
  353. + drag_yvec[i]*(y - pressy);
  354. move[i] = floor(move[i]/g_qeglobals.d_gridsize+0.5)*g_qeglobals.d_gridsize;
  355. }
  356. sprintf (movestring, "drag (%i %i %i)", (int)move[0], (int)move[1], (int)move[2]);
  357. Sys_Status (movestring, 0);
  358. VectorSubtract (move, pressdelta, delta);
  359. MoveSelection (delta);
  360. VectorCopy (move, pressdelta);
  361. }
  362. /*
  363. ===========
  364. Drag_MouseUp
  365. ===========
  366. */
  367. void Drag_MouseUp (void)
  368. {
  369. Sys_Status ("drag completed.", 0);
  370. if (g_qeglobals.d_select_translate[0] || g_qeglobals.d_select_translate[1] || g_qeglobals.d_select_translate[2])
  371. {
  372. Select_Move (g_qeglobals.d_select_translate);
  373. VectorCopy (vec3_origin, g_qeglobals.d_select_translate);
  374. Sys_UpdateWindows (W_CAMERA);
  375. }
  376. }