select.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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. // select.c
  19. #include "qe3.h"
  20. /*
  21. ===========
  22. Test_Ray
  23. ===========
  24. */
  25. #define DIST_START 999999
  26. trace_t Test_Ray (vec3_t origin, vec3_t dir, int flags)
  27. {
  28. brush_t *brush;
  29. face_t *face;
  30. float dist;
  31. trace_t t;
  32. memset (&t, 0, sizeof(t));
  33. t.dist = DIST_START;
  34. if (! (flags & SF_SELECTED_ONLY) )
  35. for (brush = active_brushes.next ; brush != &active_brushes ; brush=brush->next)
  36. {
  37. if ( (flags & SF_ENTITIES_FIRST) && brush->owner == world_entity)
  38. continue;
  39. if (FilterBrush (brush))
  40. continue;
  41. face = Brush_Ray (origin, dir, brush, &dist);
  42. if (dist > 0 && dist < t.dist)
  43. {
  44. t.dist = dist;
  45. t.brush = brush;
  46. t.face = face;
  47. t.selected = false;
  48. }
  49. }
  50. for (brush = selected_brushes.next ; brush != &selected_brushes ; brush=brush->next)
  51. {
  52. if ( (flags & SF_ENTITIES_FIRST) && brush->owner == world_entity)
  53. continue;
  54. if (FilterBrush (brush))
  55. continue;
  56. face = Brush_Ray (origin, dir, brush, &dist);
  57. if (dist > 0 && dist < t.dist)
  58. {
  59. t.dist = dist;
  60. t.brush = brush;
  61. t.face = face;
  62. t.selected = true;
  63. }
  64. }
  65. // if entites first, but didn't find any, check regular
  66. if ( (flags & SF_ENTITIES_FIRST) && t.brush == NULL)
  67. return Test_Ray (origin, dir, flags - SF_ENTITIES_FIRST);
  68. return t;
  69. }
  70. /*
  71. ============
  72. Select_Brush
  73. ============
  74. */
  75. void Select_Brush (brush_t *brush)
  76. {
  77. brush_t *b;
  78. entity_t *e;
  79. selected_face = NULL;
  80. if (g_qeglobals.d_select_count < 2)
  81. g_qeglobals.d_select_order[g_qeglobals.d_select_count] = brush;
  82. g_qeglobals.d_select_count++;
  83. e = brush->owner;
  84. if (e)
  85. {
  86. // select complete entity on first click
  87. if (e != world_entity)
  88. {
  89. for (b=selected_brushes.next ; b != &selected_brushes ; b=b->next)
  90. if (b->owner == e)
  91. goto singleselect;
  92. for (b=e->brushes.onext ; b != &e->brushes ; b=b->onext)
  93. {
  94. Brush_RemoveFromList (b);
  95. Brush_AddToList (b, &selected_brushes);
  96. }
  97. }
  98. else
  99. {
  100. singleselect:
  101. Brush_RemoveFromList (brush);
  102. Brush_AddToList (brush, &selected_brushes);
  103. }
  104. if (e->eclass)
  105. {
  106. UpdateEntitySel(brush->owner->eclass);
  107. }
  108. }
  109. }
  110. /*
  111. ============
  112. Select_Ray
  113. If the origin is inside a brush, that brush will be ignored.
  114. ============
  115. */
  116. void Select_Ray (vec3_t origin, vec3_t dir, int flags)
  117. {
  118. trace_t t;
  119. t = Test_Ray (origin, dir, flags);
  120. if (!t.brush)
  121. return;
  122. if (flags == SF_SINGLEFACE)
  123. {
  124. selected_face = t.face;
  125. selected_face_brush = t.brush;
  126. Sys_UpdateWindows (W_ALL);
  127. g_qeglobals.d_select_mode = sel_brush;
  128. return;
  129. }
  130. // move the brush to the other list
  131. g_qeglobals.d_select_mode = sel_brush;
  132. if (t.selected)
  133. {
  134. Brush_RemoveFromList (t.brush);
  135. Brush_AddToList (t.brush, &active_brushes);
  136. } else
  137. {
  138. Select_Brush (t.brush);
  139. }
  140. Sys_UpdateWindows (W_ALL);
  141. }
  142. void Select_Delete (void)
  143. {
  144. brush_t *brush;
  145. selected_face = NULL;
  146. g_qeglobals.d_select_mode = sel_brush;
  147. g_qeglobals.d_select_count = 0;
  148. g_qeglobals.d_num_move_points = 0;
  149. while (selected_brushes.next != &selected_brushes)
  150. {
  151. brush = selected_brushes.next;
  152. Brush_Free (brush);
  153. }
  154. // FIXME: remove any entities with no brushes
  155. Sys_UpdateWindows (W_ALL);
  156. }
  157. void Select_Deselect (void)
  158. {
  159. brush_t *b;
  160. g_qeglobals.d_workcount++;
  161. g_qeglobals.d_select_count = 0;
  162. g_qeglobals.d_num_move_points = 0;
  163. b = selected_brushes.next;
  164. if (b == &selected_brushes)
  165. {
  166. if (selected_face)
  167. {
  168. selected_face = NULL;
  169. Sys_UpdateWindows (W_ALL);
  170. }
  171. return;
  172. }
  173. selected_face = NULL;
  174. g_qeglobals.d_select_mode = sel_brush;
  175. // grab top / bottom height for new brushes
  176. if (b->mins[2] < b->maxs[2])
  177. {
  178. g_qeglobals.d_new_brush_bottom_z = b->mins[2];
  179. g_qeglobals.d_new_brush_top_z = b->maxs[2];
  180. }
  181. selected_brushes.next->prev = &active_brushes;
  182. selected_brushes.prev->next = active_brushes.next;
  183. active_brushes.next->prev = selected_brushes.prev;
  184. active_brushes.next = selected_brushes.next;
  185. selected_brushes.prev = selected_brushes.next = &selected_brushes;
  186. Sys_UpdateWindows (W_ALL);
  187. }
  188. /*
  189. ============
  190. Select_Move
  191. ============
  192. */
  193. void Select_Move (vec3_t delta)
  194. {
  195. brush_t *b;
  196. // actually move the selected brushes
  197. for (b = selected_brushes.next ; b != &selected_brushes ; b=b->next)
  198. Brush_Move (b, delta);
  199. // Sys_UpdateWindows (W_ALL);
  200. }
  201. /*
  202. ============
  203. Select_Clone
  204. Creates an exact duplicate of the selection in place, then moves
  205. the selected brushes off of their old positions
  206. ============
  207. */
  208. void Select_Clone (void)
  209. {
  210. brush_t *b, *b2, *n, *next, *next2;
  211. vec3_t delta;
  212. entity_t *e;
  213. g_qeglobals.d_workcount++;
  214. g_qeglobals.d_select_mode = sel_brush;
  215. delta[0] = g_qeglobals.d_gridsize;
  216. delta[1] = g_qeglobals.d_gridsize;
  217. delta[2] = 0;
  218. for (b=selected_brushes.next ; b != &selected_brushes ; b=next)
  219. {
  220. next = b->next;
  221. // if the brush is a world brush, handle simply
  222. if (b->owner == world_entity)
  223. {
  224. n = Brush_Clone (b);
  225. Brush_AddToList (n, &active_brushes);
  226. Entity_LinkBrush (world_entity, n);
  227. Brush_Build( n );
  228. Brush_Move (b, delta);
  229. continue;
  230. }
  231. e = Entity_Clone (b->owner);
  232. // clear the target / targetname
  233. DeleteKey (e, "target");
  234. DeleteKey (e, "targetname");
  235. // if the brush is a fixed size entity, create a new entity
  236. if (b->owner->eclass->fixedsize)
  237. {
  238. n = Brush_Clone (b);
  239. Brush_AddToList (n, &active_brushes);
  240. Entity_LinkBrush (e, n);
  241. Brush_Build( n );
  242. Brush_Move (b, delta);
  243. continue;
  244. }
  245. // brush is a complex entity, grab all the other ones now
  246. next = &selected_brushes;
  247. for ( b2 = b ; b2 != &selected_brushes ; b2=next2)
  248. {
  249. next2 = b2->next;
  250. if (b2->owner != b->owner)
  251. {
  252. if (next == &selected_brushes)
  253. next = b2;
  254. continue;
  255. }
  256. // move b2 to the start of selected_brushes,
  257. // so it won't be hit again
  258. Brush_RemoveFromList (b2);
  259. Brush_AddToList (b2, &selected_brushes);
  260. n = Brush_Clone (b2);
  261. Brush_AddToList (n, &active_brushes);
  262. Entity_LinkBrush (e, n);
  263. Brush_Build( n );
  264. Brush_Move (b2, delta);
  265. }
  266. }
  267. Sys_UpdateWindows (W_ALL);
  268. }
  269. /*
  270. ============
  271. Select_SetTexture
  272. ============
  273. */
  274. void Select_SetTexture (texdef_t *texdef)
  275. {
  276. brush_t *b;
  277. if (selected_face)
  278. {
  279. selected_face->texdef = *texdef;
  280. Brush_Build(selected_face_brush);
  281. }
  282. else
  283. {
  284. for (b=selected_brushes.next ; b != &selected_brushes ; b=b->next)
  285. if (!b->owner->eclass->fixedsize)
  286. Brush_SetTexture (b, texdef);
  287. }
  288. Sys_UpdateWindows (W_ALL);
  289. }
  290. /*
  291. ================================================================
  292. TRANSFORMATIONS
  293. ================================================================
  294. */
  295. void Select_GetBounds (vec3_t mins, vec3_t maxs)
  296. {
  297. brush_t *b;
  298. int i;
  299. for (i=0 ; i<3 ; i++)
  300. {
  301. mins[i] = 99999;
  302. maxs[i] = -99999;
  303. }
  304. for (b=selected_brushes.next ; b != &selected_brushes ; b=b->next)
  305. for (i=0 ; i<3 ; i++)
  306. {
  307. if (b->mins[i] < mins[i])
  308. mins[i] = b->mins[i];
  309. if (b->maxs[i] > maxs[i])
  310. maxs[i] = b->maxs[i];
  311. }
  312. }
  313. void Select_GetMid (vec3_t mid)
  314. {
  315. vec3_t mins, maxs;
  316. int i;
  317. Select_GetBounds (mins, maxs);
  318. for (i=0 ; i<3 ; i++)
  319. mid[i] = g_qeglobals.d_gridsize*floor ( ( (mins[i] + maxs[i])*0.5 )/g_qeglobals.d_gridsize );
  320. }
  321. vec3_t select_origin;
  322. vec3_t select_matrix[3];
  323. qboolean select_fliporder;
  324. void Select_AplyMatrix (void)
  325. {
  326. brush_t *b;
  327. face_t *f;
  328. int i, j;
  329. vec3_t temp;
  330. for (b=selected_brushes.next ; b != &selected_brushes ; b=b->next)
  331. {
  332. for (f=b->brush_faces ; f ; f=f->next)
  333. {
  334. for (i=0 ; i<3 ; i++)
  335. {
  336. VectorSubtract (f->planepts[i], select_origin, temp);
  337. for (j=0 ; j<3 ; j++)
  338. f->planepts[i][j] = DotProduct(temp, select_matrix[j])
  339. + select_origin[j];
  340. }
  341. if (select_fliporder)
  342. {
  343. VectorCopy (f->planepts[0], temp);
  344. VectorCopy (f->planepts[2], f->planepts[0]);
  345. VectorCopy (temp, f->planepts[2]);
  346. }
  347. }
  348. Brush_Build( b );
  349. }
  350. Sys_UpdateWindows (W_ALL);
  351. }
  352. void Select_FlipAxis (int axis)
  353. {
  354. int i;
  355. Select_GetMid (select_origin);
  356. for (i=0 ; i<3 ; i++)
  357. {
  358. VectorCopy (vec3_origin, select_matrix[i]);
  359. select_matrix[i][i] = 1;
  360. }
  361. select_matrix[axis][axis] = -1;
  362. select_fliporder = true;
  363. Select_AplyMatrix ();
  364. }
  365. void Select_RotateAxis (int axis, float deg)
  366. {
  367. vec3_t temp;
  368. int i, j;
  369. vec_t c, s;
  370. if (deg == 0)
  371. return;
  372. Select_GetMid (select_origin);
  373. select_fliporder = false;
  374. if (deg == 90)
  375. {
  376. for (i=0 ; i<3 ; i++)
  377. {
  378. VectorCopy (vec3_origin, select_matrix[i]);
  379. select_matrix[i][i] = 1;
  380. }
  381. i = (axis+1)%3;
  382. j = (axis+2)%3;
  383. VectorCopy (select_matrix[i], temp);
  384. VectorCopy (select_matrix[j], select_matrix[i]);
  385. VectorSubtract (vec3_origin, temp, select_matrix[j]);
  386. }
  387. else
  388. {
  389. deg = -deg;
  390. if (deg == -180)
  391. {
  392. c = -1;
  393. s = 0;
  394. }
  395. else if (deg == -270)
  396. {
  397. c = 0;
  398. s = -1;
  399. }
  400. else
  401. {
  402. c = cos(deg/180*3.14159);
  403. s = sin (deg/180*3.14159);
  404. }
  405. for (i=0 ; i<3 ; i++)
  406. {
  407. VectorCopy (vec3_origin, select_matrix[i]);
  408. select_matrix[i][i] = 1;
  409. }
  410. switch (axis)
  411. {
  412. case 0:
  413. select_matrix[1][1] = c;
  414. select_matrix[1][2] = -s;
  415. select_matrix[2][1] = s;
  416. select_matrix[2][2] = c;
  417. break;
  418. case 1:
  419. select_matrix[0][0] = c;
  420. select_matrix[0][2] = s;
  421. select_matrix[2][0] = -s;
  422. select_matrix[2][2] = c;
  423. break;
  424. case 2:
  425. select_matrix[0][0] = c;
  426. select_matrix[0][1] = -s;
  427. select_matrix[1][0] = s;
  428. select_matrix[1][1] = c;
  429. break;
  430. }
  431. }
  432. Select_AplyMatrix ();
  433. }
  434. /*
  435. ================================================================
  436. GROUP SELECTIONS
  437. ================================================================
  438. */
  439. void Select_CompleteTall (void)
  440. {
  441. brush_t *b, *next;
  442. int i;
  443. vec3_t mins, maxs;
  444. if (!QE_SingleBrush ())
  445. return;
  446. g_qeglobals.d_select_mode = sel_brush;
  447. VectorCopy (selected_brushes.next->mins, mins);
  448. VectorCopy (selected_brushes.next->maxs, maxs);
  449. Select_Delete ();
  450. for (b=active_brushes.next ; b != &active_brushes ; b=next)
  451. {
  452. next = b->next;
  453. for (i=0 ; i<2 ; i++)
  454. if (b->maxs[i] > maxs[i] || b->mins[i] < mins[i])
  455. break;
  456. if (i == 2)
  457. {
  458. Brush_RemoveFromList (b);
  459. Brush_AddToList (b, &selected_brushes);
  460. }
  461. }
  462. Sys_UpdateWindows (W_ALL);
  463. }
  464. void Select_PartialTall (void)
  465. {
  466. brush_t *b, *next;
  467. int i;
  468. vec3_t mins, maxs;
  469. if (!QE_SingleBrush ())
  470. return;
  471. g_qeglobals.d_select_mode = sel_brush;
  472. VectorCopy (selected_brushes.next->mins, mins);
  473. VectorCopy (selected_brushes.next->maxs, maxs);
  474. Select_Delete ();
  475. for (b=active_brushes.next ; b != &active_brushes ; b=next)
  476. {
  477. next = b->next;
  478. for (i=0 ; i<2 ; i++)
  479. if (b->mins[i] > maxs[i] || b->maxs[i] < mins[i])
  480. break;
  481. if (i == 2)
  482. {
  483. Brush_RemoveFromList (b);
  484. Brush_AddToList (b, &selected_brushes);
  485. }
  486. }
  487. Sys_UpdateWindows (W_ALL);
  488. }
  489. void Select_Touching (void)
  490. {
  491. brush_t *b, *next;
  492. int i;
  493. vec3_t mins, maxs;
  494. if (!QE_SingleBrush ())
  495. return;
  496. g_qeglobals.d_select_mode = sel_brush;
  497. VectorCopy (selected_brushes.next->mins, mins);
  498. VectorCopy (selected_brushes.next->maxs, maxs);
  499. for (b=active_brushes.next ; b != &active_brushes ; b=next)
  500. {
  501. next = b->next;
  502. for (i=0 ; i<3 ; i++)
  503. if (b->mins[i] > maxs[i]+1 || b->maxs[i] < mins[i]-1)
  504. break;
  505. if (i == 3)
  506. {
  507. Brush_RemoveFromList (b);
  508. Brush_AddToList (b, &selected_brushes);
  509. }
  510. }
  511. Sys_UpdateWindows (W_ALL);
  512. }
  513. void Select_Inside (void)
  514. {
  515. brush_t *b, *next;
  516. int i;
  517. vec3_t mins, maxs;
  518. if (!QE_SingleBrush ())
  519. return;
  520. g_qeglobals.d_select_mode = sel_brush;
  521. VectorCopy (selected_brushes.next->mins, mins);
  522. VectorCopy (selected_brushes.next->maxs, maxs);
  523. Select_Delete ();
  524. for (b=active_brushes.next ; b != &active_brushes ; b=next)
  525. {
  526. next = b->next;
  527. for (i=0 ; i<3 ; i++)
  528. if (b->maxs[i] > maxs[i] || b->mins[i] < mins[i])
  529. break;
  530. if (i == 3)
  531. {
  532. Brush_RemoveFromList (b);
  533. Brush_AddToList (b, &selected_brushes);
  534. }
  535. }
  536. Sys_UpdateWindows (W_ALL);
  537. }
  538. /*
  539. =============
  540. Select_Ungroup
  541. Turn the currently selected entity back into normal brushes
  542. =============
  543. */
  544. void Select_Ungroup (void)
  545. {
  546. entity_t *e;
  547. brush_t *b;
  548. e = selected_brushes.next->owner;
  549. if (!e || e == world_entity || e->eclass->fixedsize)
  550. {
  551. Sys_Status ("Not a grouped entity.", 0);
  552. return;
  553. }
  554. for (b=e->brushes.onext ; b != &e->brushes ; b=e->brushes.onext)
  555. {
  556. Brush_RemoveFromList (b);
  557. Brush_AddToList (b, &active_brushes);
  558. Entity_UnlinkBrush (b);
  559. Entity_LinkBrush (world_entity, b);
  560. Brush_Build( b );
  561. b->owner = world_entity;
  562. }
  563. Entity_Free (e);
  564. Sys_UpdateWindows (W_ALL);
  565. }
  566. /*
  567. ====================
  568. Select_MakeStructural
  569. ====================
  570. */
  571. void Select_MakeStructural (void)
  572. {
  573. brush_t *b;
  574. face_t *f;
  575. for (b=selected_brushes.next ; b != &selected_brushes ; b=b->next)
  576. for (f=b->brush_faces ; f ; f=f->next)
  577. f->texdef.contents &= ~CONTENTS_DETAIL;
  578. Select_Deselect ();
  579. Sys_UpdateWindows (W_ALL);
  580. }
  581. void Select_MakeDetail (void)
  582. {
  583. brush_t *b;
  584. face_t *f;
  585. for (b=selected_brushes.next ; b != &selected_brushes ; b=b->next)
  586. for (f=b->brush_faces ; f ; f=f->next)
  587. f->texdef.contents |= CONTENTS_DETAIL;
  588. Select_Deselect ();
  589. Sys_UpdateWindows (W_ALL);
  590. }