grid_map_editor_plugin.cpp 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  1. /*************************************************************************/
  2. /* grid_map_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "grid_map_editor_plugin.h"
  31. #include "editor/editor_settings.h"
  32. #include "editor/plugins/spatial_editor_plugin.h"
  33. #include "scene/3d/camera.h"
  34. #include "geometry.h"
  35. #include "os/keyboard.h"
  36. void GridMapEditor::_node_removed(Node *p_node) {
  37. if (p_node == node) {
  38. node = NULL;
  39. hide();
  40. theme_pallete->hide();
  41. }
  42. }
  43. void GridMapEditor::_configure() {
  44. if (!node)
  45. return;
  46. update_grid();
  47. }
  48. void GridMapEditor::_menu_option(int p_option) {
  49. switch (p_option) {
  50. case MENU_OPTION_CONFIGURE: {
  51. } break;
  52. case MENU_OPTION_LOCK_VIEW: {
  53. int index = options->get_popup()->get_item_index(MENU_OPTION_LOCK_VIEW);
  54. lock_view = !options->get_popup()->is_item_checked(index);
  55. options->get_popup()->set_item_checked(index, lock_view);
  56. } break;
  57. case MENU_OPTION_CLIP_DISABLED:
  58. case MENU_OPTION_CLIP_ABOVE:
  59. case MENU_OPTION_CLIP_BELOW: {
  60. clip_mode = ClipMode(p_option - MENU_OPTION_CLIP_DISABLED);
  61. for (int i = 0; i < 3; i++) {
  62. int index = options->get_popup()->get_item_index(MENU_OPTION_CLIP_DISABLED + i);
  63. options->get_popup()->set_item_checked(index, i == clip_mode);
  64. }
  65. _update_clip();
  66. } break;
  67. case MENU_OPTION_X_AXIS:
  68. case MENU_OPTION_Y_AXIS:
  69. case MENU_OPTION_Z_AXIS: {
  70. int new_axis = p_option - MENU_OPTION_X_AXIS;
  71. for (int i = 0; i < 3; i++) {
  72. int idx = options->get_popup()->get_item_index(MENU_OPTION_X_AXIS + i);
  73. options->get_popup()->set_item_checked(idx, i == new_axis);
  74. }
  75. edit_axis = Vector3::Axis(new_axis);
  76. update_grid();
  77. _update_clip();
  78. } break;
  79. case MENU_OPTION_CURSOR_ROTATE_Y: {
  80. Basis r;
  81. if (input_action == INPUT_DUPLICATE) {
  82. r.set_orthogonal_index(selection.duplicate_rot);
  83. r.rotate(Vector3(0, 1, 0), -Math_PI / 2.0);
  84. selection.duplicate_rot = r.get_orthogonal_index();
  85. _update_duplicate_indicator();
  86. break;
  87. }
  88. r.set_orthogonal_index(cursor_rot);
  89. r.rotate(Vector3(0, 1, 0), -Math_PI / 2.0);
  90. cursor_rot = r.get_orthogonal_index();
  91. _update_cursor_transform();
  92. } break;
  93. case MENU_OPTION_CURSOR_ROTATE_X: {
  94. Basis r;
  95. if (input_action == INPUT_DUPLICATE) {
  96. r.set_orthogonal_index(selection.duplicate_rot);
  97. r.rotate(Vector3(1, 0, 0), -Math_PI / 2.0);
  98. selection.duplicate_rot = r.get_orthogonal_index();
  99. _update_duplicate_indicator();
  100. break;
  101. }
  102. r.set_orthogonal_index(cursor_rot);
  103. r.rotate(Vector3(1, 0, 0), -Math_PI / 2.0);
  104. cursor_rot = r.get_orthogonal_index();
  105. _update_cursor_transform();
  106. } break;
  107. case MENU_OPTION_CURSOR_ROTATE_Z: {
  108. Basis r;
  109. if (input_action == INPUT_DUPLICATE) {
  110. r.set_orthogonal_index(selection.duplicate_rot);
  111. r.rotate(Vector3(0, 0, 1), -Math_PI / 2.0);
  112. selection.duplicate_rot = r.get_orthogonal_index();
  113. _update_duplicate_indicator();
  114. break;
  115. }
  116. r.set_orthogonal_index(cursor_rot);
  117. r.rotate(Vector3(0, 0, 1), -Math_PI / 2.0);
  118. cursor_rot = r.get_orthogonal_index();
  119. _update_cursor_transform();
  120. } break;
  121. case MENU_OPTION_CURSOR_BACK_ROTATE_Y: {
  122. Basis r;
  123. r.set_orthogonal_index(cursor_rot);
  124. r.rotate(Vector3(0, 1, 0), Math_PI / 2.0);
  125. cursor_rot = r.get_orthogonal_index();
  126. _update_cursor_transform();
  127. } break;
  128. case MENU_OPTION_CURSOR_BACK_ROTATE_X: {
  129. Basis r;
  130. r.set_orthogonal_index(cursor_rot);
  131. r.rotate(Vector3(1, 0, 0), Math_PI / 2.0);
  132. cursor_rot = r.get_orthogonal_index();
  133. _update_cursor_transform();
  134. } break;
  135. case MENU_OPTION_CURSOR_BACK_ROTATE_Z: {
  136. Basis r;
  137. r.set_orthogonal_index(cursor_rot);
  138. r.rotate(Vector3(0, 0, 1), Math_PI / 2.0);
  139. cursor_rot = r.get_orthogonal_index();
  140. _update_cursor_transform();
  141. } break;
  142. case MENU_OPTION_CURSOR_CLEAR_ROTATION: {
  143. if (input_action == INPUT_DUPLICATE) {
  144. selection.duplicate_rot = 0;
  145. _update_duplicate_indicator();
  146. break;
  147. }
  148. cursor_rot = 0;
  149. _update_cursor_transform();
  150. } break;
  151. case MENU_OPTION_DUPLICATE_SELECTS: {
  152. int idx = options->get_popup()->get_item_index(MENU_OPTION_DUPLICATE_SELECTS);
  153. options->get_popup()->set_item_checked(idx, !options->get_popup()->is_item_checked(idx));
  154. } break;
  155. case MENU_OPTION_SELECTION_DUPLICATE:
  156. if (!(selection.active && input_action == INPUT_NONE))
  157. return;
  158. if (last_mouseover == Vector3(-1, -1, -1)) //nono mouseovering anythin
  159. break;
  160. input_action = INPUT_DUPLICATE;
  161. selection.click = last_mouseover;
  162. selection.current = last_mouseover;
  163. selection.duplicate_rot = 0;
  164. _update_duplicate_indicator();
  165. break;
  166. case MENU_OPTION_SELECTION_CLEAR: {
  167. if (!selection.active)
  168. return;
  169. _delete_selection();
  170. } break;
  171. case MENU_OPTION_GRIDMAP_SETTINGS: {
  172. settings_dialog->popup_centered(settings_vbc->get_combined_minimum_size() + Size2(50, 50));
  173. } break;
  174. }
  175. }
  176. void GridMapEditor::_update_cursor_transform() {
  177. cursor_transform = Transform();
  178. cursor_transform.origin = cursor_origin;
  179. cursor_transform.basis.set_orthogonal_index(cursor_rot);
  180. cursor_transform = node->get_transform() * cursor_transform;
  181. if (cursor_instance.is_valid()) {
  182. VisualServer::get_singleton()->instance_set_transform(cursor_instance, cursor_transform);
  183. VisualServer::get_singleton()->instance_set_visible(cursor_instance, cursor_visible);
  184. }
  185. }
  186. void GridMapEditor::_update_selection_transform() {
  187. Transform xf_zero;
  188. xf_zero.basis.set_zero();
  189. if (!selection.active) {
  190. VisualServer::get_singleton()->instance_set_transform(selection_instance, xf_zero);
  191. for (int i = 0; i < 3; i++) {
  192. VisualServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf_zero);
  193. }
  194. return;
  195. }
  196. Transform xf;
  197. xf.scale(Vector3(1, 1, 1) * (Vector3(1, 1, 1) + (selection.end - selection.begin)) * node->get_cell_size());
  198. xf.origin = selection.begin * node->get_cell_size();
  199. VisualServer::get_singleton()->instance_set_transform(selection_instance, node->get_global_transform() * xf);
  200. for (int i = 0; i < 3; i++) {
  201. if (i != edit_axis || (edit_floor[edit_axis] < selection.begin[edit_axis]) || (edit_floor[edit_axis] > selection.end[edit_axis] + 1)) {
  202. VisualServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf_zero);
  203. } else {
  204. Vector3 scale = (selection.end - selection.begin + Vector3(1, 1, 1));
  205. scale[edit_axis] = 1.0;
  206. Vector3 pos = selection.begin;
  207. pos[edit_axis] = edit_floor[edit_axis];
  208. scale *= node->get_cell_size();
  209. pos *= node->get_cell_size();
  210. Transform xf;
  211. xf.basis.scale(scale);
  212. xf.origin = pos;
  213. VisualServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf);
  214. }
  215. }
  216. }
  217. void GridMapEditor::_validate_selection() {
  218. if (!selection.active)
  219. return;
  220. selection.begin = selection.click;
  221. selection.end = selection.current;
  222. if (selection.begin.x > selection.end.x)
  223. SWAP(selection.begin.x, selection.end.x);
  224. if (selection.begin.y > selection.end.y)
  225. SWAP(selection.begin.y, selection.end.y);
  226. if (selection.begin.z > selection.end.z)
  227. SWAP(selection.begin.z, selection.end.z);
  228. _update_selection_transform();
  229. }
  230. bool GridMapEditor::do_input_action(Camera *p_camera, const Point2 &p_point, bool p_click) {
  231. if (!spatial_editor)
  232. return false;
  233. if (selected_pallete < 0 && input_action != INPUT_COPY && input_action != INPUT_SELECT && input_action != INPUT_DUPLICATE)
  234. return false;
  235. Ref<MeshLibrary> theme = node->get_theme();
  236. if (theme.is_null())
  237. return false;
  238. if (input_action != INPUT_COPY && input_action != INPUT_SELECT && input_action != INPUT_DUPLICATE && !theme->has_item(selected_pallete))
  239. return false;
  240. Camera *camera = p_camera;
  241. Vector3 from = camera->project_ray_origin(p_point);
  242. Vector3 normal = camera->project_ray_normal(p_point);
  243. Transform local_xform = node->get_global_transform().affine_inverse();
  244. Vector<Plane> planes = camera->get_frustum();
  245. from = local_xform.xform(from);
  246. normal = local_xform.basis.xform(normal).normalized();
  247. Plane p;
  248. p.normal[edit_axis] = 1.0;
  249. p.d = edit_floor[edit_axis] * node->get_cell_size()[edit_axis];
  250. Vector3 inters;
  251. if (!p.intersects_segment(from, from + normal * settings_pick_distance->get_value(), &inters))
  252. return false;
  253. //make sure the intersection is inside the frustum planes, to avoid
  254. //painting on invisible regions
  255. for (int i = 0; i < planes.size(); i++) {
  256. Plane fp = local_xform.xform(planes[i]);
  257. if (fp.is_point_over(inters))
  258. return false;
  259. }
  260. int cell[3];
  261. float cell_size[3] = { node->get_cell_size().x, node->get_cell_size().y, node->get_cell_size().z };
  262. last_mouseover = Vector3(-1, -1, -1);
  263. for (int i = 0; i < 3; i++) {
  264. if (i == edit_axis)
  265. cell[i] = edit_floor[i];
  266. else {
  267. cell[i] = inters[i] / node->get_cell_size()[i];
  268. if (inters[i] < 0)
  269. cell[i] -= 1; //compensate negative
  270. grid_ofs[i] = cell[i] * cell_size[i];
  271. }
  272. /*if (cell[i]<0 || cell[i]>=grid_size[i]) {
  273. cursor_visible=false;
  274. _update_cursor_transform();
  275. return false;
  276. }*/
  277. }
  278. last_mouseover = Vector3(cell[0], cell[1], cell[2]);
  279. VS::get_singleton()->instance_set_transform(grid_instance[edit_axis], Transform(Basis(), grid_ofs));
  280. if (cursor_instance.is_valid()) {
  281. cursor_origin = (Vector3(cell[0], cell[1], cell[2]) + Vector3(0.5 * node->get_center_x(), 0.5 * node->get_center_y(), 0.5 * node->get_center_z())) * node->get_cell_size();
  282. cursor_visible = true;
  283. _update_cursor_transform();
  284. }
  285. if (input_action == INPUT_DUPLICATE) {
  286. selection.current = Vector3(cell[0], cell[1], cell[2]);
  287. _update_duplicate_indicator();
  288. } else if (input_action == INPUT_SELECT) {
  289. selection.current = Vector3(cell[0], cell[1], cell[2]);
  290. if (p_click)
  291. selection.click = selection.current;
  292. selection.active = true;
  293. _validate_selection();
  294. return true;
  295. } else if (input_action == INPUT_COPY) {
  296. int item = node->get_cell_item(cell[0], cell[1], cell[2]);
  297. if (item >= 0) {
  298. selected_pallete = item;
  299. theme_pallete->set_current(item);
  300. update_pallete();
  301. _update_cursor_instance();
  302. }
  303. return true;
  304. }
  305. if (input_action == INPUT_PAINT) {
  306. SetItem si;
  307. si.pos = Vector3(cell[0], cell[1], cell[2]);
  308. si.new_value = selected_pallete;
  309. si.new_orientation = cursor_rot;
  310. si.old_value = node->get_cell_item(cell[0], cell[1], cell[2]);
  311. si.old_orientation = node->get_cell_item_orientation(cell[0], cell[1], cell[2]);
  312. set_items.push_back(si);
  313. node->set_cell_item(cell[0], cell[1], cell[2], selected_pallete, cursor_rot);
  314. return true;
  315. } else if (input_action == INPUT_ERASE) {
  316. SetItem si;
  317. si.pos = Vector3(cell[0], cell[1], cell[2]);
  318. si.new_value = -1;
  319. si.new_orientation = 0;
  320. si.old_value = node->get_cell_item(cell[0], cell[1], cell[2]);
  321. si.old_orientation = node->get_cell_item_orientation(cell[0], cell[1], cell[2]);
  322. set_items.push_back(si);
  323. node->set_cell_item(cell[0], cell[1], cell[2], -1);
  324. return true;
  325. }
  326. return false;
  327. }
  328. void GridMapEditor::_delete_selection() {
  329. if (!selection.active)
  330. return;
  331. undo_redo->create_action(TTR("GridMap Delete Selection"));
  332. for (int i = selection.begin.x; i <= selection.end.x; i++) {
  333. for (int j = selection.begin.y; j <= selection.end.y; j++) {
  334. for (int k = selection.begin.z; k <= selection.end.z; k++) {
  335. undo_redo->add_do_method(node, "set_cell_item", i, j, k, GridMap::INVALID_CELL_ITEM);
  336. undo_redo->add_undo_method(node, "set_cell_item", i, j, k, node->get_cell_item(i, j, k), node->get_cell_item_orientation(i, j, k));
  337. }
  338. }
  339. }
  340. undo_redo->commit_action();
  341. selection.active = false;
  342. _validate_selection();
  343. }
  344. void GridMapEditor::_update_duplicate_indicator() {
  345. if (!selection.active || input_action != INPUT_DUPLICATE) {
  346. Transform xf;
  347. xf.basis.set_zero();
  348. VisualServer::get_singleton()->instance_set_transform(duplicate_instance, xf);
  349. return;
  350. }
  351. Transform xf;
  352. xf.scale(Vector3(1, 1, 1) * (Vector3(1, 1, 1) + (selection.end - selection.begin)) * node->get_cell_size());
  353. xf.origin = (selection.begin + (selection.current - selection.click)) * node->get_cell_size();
  354. Basis rot;
  355. rot.set_orthogonal_index(selection.duplicate_rot);
  356. xf.basis = rot * xf.basis;
  357. VisualServer::get_singleton()->instance_set_transform(duplicate_instance, node->get_global_transform() * xf);
  358. }
  359. struct __Item {
  360. Vector3 pos;
  361. int rot;
  362. int item;
  363. };
  364. void GridMapEditor::_duplicate_paste() {
  365. if (!selection.active)
  366. return;
  367. int idx = options->get_popup()->get_item_index(MENU_OPTION_DUPLICATE_SELECTS);
  368. bool reselect = options->get_popup()->is_item_checked(idx);
  369. List<__Item> items;
  370. Basis rot;
  371. rot.set_orthogonal_index(selection.duplicate_rot);
  372. for (int i = selection.begin.x; i <= selection.end.x; i++) {
  373. for (int j = selection.begin.y; j <= selection.end.y; j++) {
  374. for (int k = selection.begin.z; k <= selection.end.z; k++) {
  375. int itm = node->get_cell_item(i, j, k);
  376. if (itm == GridMap::INVALID_CELL_ITEM)
  377. continue;
  378. int orientation = node->get_cell_item_orientation(i, j, k);
  379. __Item item;
  380. Vector3 rel = Vector3(i, j, k) - selection.begin;
  381. rel = rot.xform(rel);
  382. Basis orm;
  383. orm.set_orthogonal_index(orientation);
  384. orm = rot * orm;
  385. item.pos = selection.begin + rel;
  386. item.item = itm;
  387. item.rot = orm.get_orthogonal_index();
  388. items.push_back(item);
  389. }
  390. }
  391. }
  392. Vector3 ofs = selection.current - selection.click;
  393. if (items.size()) {
  394. undo_redo->create_action(TTR("GridMap Duplicate Selection"));
  395. for (List<__Item>::Element *E = items.front(); E; E = E->next()) {
  396. __Item &it = E->get();
  397. Vector3 pos = it.pos + ofs;
  398. undo_redo->add_do_method(node, "set_cell_item", pos.x, pos.y, pos.z, it.item, it.rot);
  399. undo_redo->add_undo_method(node, "set_cell_item", pos.x, pos.y, pos.z, node->get_cell_item(pos.x, pos.y, pos.z), node->get_cell_item_orientation(pos.x, pos.y, pos.z));
  400. }
  401. undo_redo->commit_action();
  402. }
  403. if (reselect) {
  404. selection.begin += ofs;
  405. selection.end += ofs;
  406. selection.click = selection.begin;
  407. selection.current = selection.end;
  408. _validate_selection();
  409. }
  410. }
  411. bool GridMapEditor::forward_spatial_input_event(Camera *p_camera, const Ref<InputEvent> &p_event) {
  412. if (!node) {
  413. return false;
  414. }
  415. Ref<InputEventMouseButton> mb = p_event;
  416. if (mb.is_valid()) {
  417. if (mb->get_button_index() == BUTTON_WHEEL_UP && (mb->get_command() || mb->get_shift())) {
  418. if (mb->is_pressed())
  419. floor->set_value(floor->get_value() + mb->get_factor());
  420. return true; //eaten
  421. } else if (mb->get_button_index() == BUTTON_WHEEL_DOWN && (mb->get_command() || mb->get_shift())) {
  422. if (mb->is_pressed())
  423. floor->set_value(floor->get_value() - mb->get_factor());
  424. return true;
  425. }
  426. if (mb->is_pressed()) {
  427. if (mb->get_button_index() == BUTTON_LEFT) {
  428. if (input_action == INPUT_DUPLICATE) {
  429. //paste
  430. _duplicate_paste();
  431. input_action = INPUT_NONE;
  432. _update_duplicate_indicator();
  433. } else if (mb->get_shift()) {
  434. input_action = INPUT_SELECT;
  435. } else if (mb->get_command())
  436. input_action = INPUT_COPY;
  437. else {
  438. input_action = INPUT_PAINT;
  439. set_items.clear();
  440. }
  441. } else if (mb->get_button_index() == BUTTON_RIGHT)
  442. if (input_action == INPUT_DUPLICATE) {
  443. input_action = INPUT_NONE;
  444. _update_duplicate_indicator();
  445. } else {
  446. input_action = INPUT_ERASE;
  447. set_items.clear();
  448. }
  449. else
  450. return false;
  451. return do_input_action(p_camera, Point2(mb->get_position().x, mb->get_position().y), true);
  452. } else {
  453. if (
  454. (mb->get_button_index() == BUTTON_RIGHT && input_action == INPUT_ERASE) ||
  455. (mb->get_button_index() == BUTTON_LEFT && input_action == INPUT_PAINT)) {
  456. if (set_items.size()) {
  457. undo_redo->create_action("GridMap Paint");
  458. for (List<SetItem>::Element *E = set_items.front(); E; E = E->next()) {
  459. const SetItem &si = E->get();
  460. undo_redo->add_do_method(node, "set_cell_item", si.pos.x, si.pos.y, si.pos.z, si.new_value, si.new_orientation);
  461. }
  462. for (List<SetItem>::Element *E = set_items.back(); E; E = E->prev()) {
  463. const SetItem &si = E->get();
  464. undo_redo->add_undo_method(node, "set_cell_item", si.pos.x, si.pos.y, si.pos.z, si.old_value, si.old_orientation);
  465. }
  466. undo_redo->commit_action();
  467. }
  468. set_items.clear();
  469. input_action = INPUT_NONE;
  470. return true;
  471. }
  472. if (mb->get_button_index() == BUTTON_LEFT && input_action != INPUT_NONE) {
  473. set_items.clear();
  474. input_action = INPUT_NONE;
  475. return true;
  476. }
  477. if (mb->get_button_index() == BUTTON_RIGHT && (input_action == INPUT_ERASE || input_action == INPUT_DUPLICATE)) {
  478. input_action = INPUT_NONE;
  479. return true;
  480. }
  481. }
  482. }
  483. Ref<InputEventMouseMotion> mm = p_event;
  484. if (mm.is_valid()) {
  485. return do_input_action(p_camera, mm->get_position(), false);
  486. }
  487. return false;
  488. }
  489. struct _CGMEItemSort {
  490. String name;
  491. int id;
  492. _FORCE_INLINE_ bool operator<(const _CGMEItemSort &r_it) const { return name < r_it.name; }
  493. };
  494. void GridMapEditor::_set_display_mode(int p_mode) {
  495. if (display_mode == p_mode) {
  496. return;
  497. }
  498. if (p_mode == DISPLAY_LIST) {
  499. mode_list->set_pressed(true);
  500. mode_thumbnail->set_pressed(false);
  501. } else if (p_mode == DISPLAY_THUMBNAIL) {
  502. mode_list->set_pressed(false);
  503. mode_thumbnail->set_pressed(true);
  504. }
  505. display_mode = p_mode;
  506. update_pallete();
  507. }
  508. void GridMapEditor::update_pallete() {
  509. int selected = theme_pallete->get_current();
  510. theme_pallete->clear();
  511. if (display_mode == DISPLAY_THUMBNAIL) {
  512. theme_pallete->set_max_columns(0);
  513. theme_pallete->set_icon_mode(ItemList::ICON_MODE_TOP);
  514. } else if (display_mode == DISPLAY_LIST) {
  515. theme_pallete->set_max_columns(1);
  516. theme_pallete->set_icon_mode(ItemList::ICON_MODE_LEFT);
  517. }
  518. float min_size = EDITOR_DEF("editors/grid_map/preview_size", 64);
  519. theme_pallete->set_fixed_icon_size(Size2(min_size, min_size));
  520. theme_pallete->set_fixed_column_width(min_size * 3 / 2);
  521. theme_pallete->set_max_text_lines(2);
  522. Ref<MeshLibrary> theme = node->get_theme();
  523. if (theme.is_null()) {
  524. last_theme = NULL;
  525. return;
  526. }
  527. Vector<int> ids;
  528. ids = theme->get_item_list();
  529. List<_CGMEItemSort> il;
  530. for (int i = 0; i < ids.size(); i++) {
  531. _CGMEItemSort is;
  532. is.id = ids[i];
  533. is.name = theme->get_item_name(ids[i]);
  534. il.push_back(is);
  535. }
  536. il.sort();
  537. int item = 0;
  538. for (List<_CGMEItemSort>::Element *E = il.front(); E; E = E->next()) {
  539. int id = E->get().id;
  540. theme_pallete->add_item("");
  541. String name = theme->get_item_name(id);
  542. Ref<Texture> preview = theme->get_item_preview(id);
  543. if (!preview.is_null()) {
  544. theme_pallete->set_item_icon(item, preview);
  545. theme_pallete->set_item_tooltip(item, name);
  546. }
  547. if (name != "") {
  548. theme_pallete->set_item_text(item, name);
  549. }
  550. theme_pallete->set_item_metadata(item, id);
  551. item++;
  552. }
  553. if (selected != -1) {
  554. theme_pallete->select(selected);
  555. }
  556. last_theme = theme.operator->();
  557. }
  558. void GridMapEditor::edit(GridMap *p_gridmap) {
  559. node = p_gridmap;
  560. VS *vs = VS::get_singleton();
  561. last_mouseover = Vector3(-1, -1, -1);
  562. input_action = INPUT_NONE;
  563. selection.active = false;
  564. _update_selection_transform();
  565. _update_duplicate_indicator();
  566. spatial_editor = Object::cast_to<SpatialEditorPlugin>(editor->get_editor_plugin_screen());
  567. if (!node) {
  568. set_process(false);
  569. for (int i = 0; i < 3; i++) {
  570. VisualServer::get_singleton()->instance_set_visible(grid_instance[i], false);
  571. }
  572. VisualServer::get_singleton()->instance_set_visible(cursor_instance, false);
  573. return;
  574. }
  575. update_pallete();
  576. set_process(true);
  577. Vector3 edited_floor = p_gridmap->get_meta("_editor_floor_");
  578. clip_mode = p_gridmap->has_meta("_editor_clip_") ? ClipMode(p_gridmap->get_meta("_editor_clip_").operator int()) : CLIP_DISABLED;
  579. for (int i = 0; i < 3; i++) {
  580. if (vs->mesh_get_surface_count(grid[i]) > 0)
  581. vs->mesh_remove_surface(grid[i], 0);
  582. edit_floor[i] = edited_floor[i];
  583. }
  584. {
  585. //update grids
  586. indicator_mat.instance();
  587. indicator_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  588. indicator_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  589. indicator_mat->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  590. indicator_mat->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  591. indicator_mat->set_albedo(Color(0.8, 0.5, 0.1));
  592. Vector<Vector3> grid_points[3];
  593. Vector<Color> grid_colors[3];
  594. float cell_size[3] = { p_gridmap->get_cell_size().x, p_gridmap->get_cell_size().y, p_gridmap->get_cell_size().z };
  595. for (int i = 0; i < 3; i++) {
  596. Vector3 axis;
  597. axis[i] = 1;
  598. Vector3 axis_n1;
  599. axis_n1[(i + 1) % 3] = cell_size[(i + 1) % 3];
  600. Vector3 axis_n2;
  601. axis_n2[(i + 2) % 3] = cell_size[(i + 2) % 3];
  602. for (int j = -GRID_CURSOR_SIZE; j <= GRID_CURSOR_SIZE; j++) {
  603. for (int k = -GRID_CURSOR_SIZE; k <= GRID_CURSOR_SIZE; k++) {
  604. Vector3 p = axis_n1 * j + axis_n2 * k;
  605. float trans = Math::pow(MAX(0, 1.0 - (Vector2(j, k).length() / GRID_CURSOR_SIZE)), 2);
  606. Vector3 pj = axis_n1 * (j + 1) + axis_n2 * k;
  607. float transj = Math::pow(MAX(0, 1.0 - (Vector2(j + 1, k).length() / GRID_CURSOR_SIZE)), 2);
  608. Vector3 pk = axis_n1 * j + axis_n2 * (k + 1);
  609. float transk = Math::pow(MAX(0, 1.0 - (Vector2(j, k + 1).length() / GRID_CURSOR_SIZE)), 2);
  610. grid_points[i].push_back(p);
  611. grid_points[i].push_back(pk);
  612. grid_colors[i].push_back(Color(1, 1, 1, trans));
  613. grid_colors[i].push_back(Color(1, 1, 1, transk));
  614. grid_points[i].push_back(p);
  615. grid_points[i].push_back(pj);
  616. grid_colors[i].push_back(Color(1, 1, 1, trans));
  617. grid_colors[i].push_back(Color(1, 1, 1, transj));
  618. }
  619. }
  620. Array d;
  621. d.resize(VS::ARRAY_MAX);
  622. d[VS::ARRAY_VERTEX] = grid_points[i];
  623. d[VS::ARRAY_COLOR] = grid_colors[i];
  624. VisualServer::get_singleton()->mesh_add_surface_from_arrays(grid[i], VisualServer::PRIMITIVE_LINES, d);
  625. VisualServer::get_singleton()->mesh_surface_set_material(grid[i], 0, indicator_mat->get_rid());
  626. }
  627. }
  628. update_grid();
  629. _update_clip();
  630. }
  631. void GridMapEditor::_update_clip() {
  632. node->set_meta("_editor_clip_", clip_mode);
  633. if (clip_mode == CLIP_DISABLED)
  634. node->set_clip(false);
  635. else
  636. node->set_clip(true, clip_mode == CLIP_ABOVE, edit_floor[edit_axis], edit_axis);
  637. }
  638. void GridMapEditor::update_grid() {
  639. grid_xform.origin.x -= 1; //force update in hackish way.. what do i care
  640. //VS *vs = VS::get_singleton();
  641. grid_ofs[edit_axis] = edit_floor[edit_axis] * node->get_cell_size()[edit_axis];
  642. edit_grid_xform.origin = grid_ofs;
  643. edit_grid_xform.basis = Basis();
  644. for (int i = 0; i < 3; i++) {
  645. VisualServer::get_singleton()->instance_set_visible(grid_instance[i], i == edit_axis);
  646. }
  647. updating = true;
  648. floor->set_value(edit_floor[edit_axis]);
  649. updating = false;
  650. }
  651. void GridMapEditor::_notification(int p_what) {
  652. if (p_what == NOTIFICATION_ENTER_TREE) {
  653. theme_pallete->connect("item_selected", this, "_item_selected_cbk");
  654. for (int i = 0; i < 3; i++) {
  655. grid[i] = VS::get_singleton()->mesh_create();
  656. grid_instance[i] = VS::get_singleton()->instance_create2(grid[i], get_tree()->get_root()->get_world()->get_scenario());
  657. selection_level_instance[i] = VisualServer::get_singleton()->instance_create2(selection_level_mesh[i], get_tree()->get_root()->get_world()->get_scenario());
  658. }
  659. selection_instance = VisualServer::get_singleton()->instance_create2(selection_mesh, get_tree()->get_root()->get_world()->get_scenario());
  660. duplicate_instance = VisualServer::get_singleton()->instance_create2(duplicate_mesh, get_tree()->get_root()->get_world()->get_scenario());
  661. _update_selection_transform();
  662. _update_duplicate_indicator();
  663. } else if (p_what == NOTIFICATION_EXIT_TREE) {
  664. for (int i = 0; i < 3; i++) {
  665. VS::get_singleton()->free(grid_instance[i]);
  666. VS::get_singleton()->free(grid[i]);
  667. grid_instance[i] = RID();
  668. grid[i] = RID();
  669. VisualServer::get_singleton()->free(selection_level_instance[i]);
  670. }
  671. VisualServer::get_singleton()->free(selection_instance);
  672. VisualServer::get_singleton()->free(duplicate_instance);
  673. selection_instance = RID();
  674. duplicate_instance = RID();
  675. } else if (p_what == NOTIFICATION_PROCESS) {
  676. if (!node) {
  677. return;
  678. }
  679. Transform xf = node->get_global_transform();
  680. if (xf != grid_xform) {
  681. for (int i = 0; i < 3; i++) {
  682. VS::get_singleton()->instance_set_transform(grid_instance[i], xf * edit_grid_xform);
  683. }
  684. grid_xform = xf;
  685. }
  686. Ref<MeshLibrary> cgmt = node->get_theme();
  687. if (cgmt.operator->() != last_theme)
  688. update_pallete();
  689. if (lock_view) {
  690. EditorNode *editor = Object::cast_to<EditorNode>(get_tree()->get_root()->get_child(0));
  691. Plane p;
  692. p.normal[edit_axis] = 1.0;
  693. p.d = edit_floor[edit_axis] * node->get_cell_size()[edit_axis];
  694. p = node->get_transform().xform(p); // plane to snap
  695. SpatialEditorPlugin *sep = Object::cast_to<SpatialEditorPlugin>(editor->get_editor_plugin_screen());
  696. if (sep)
  697. sep->snap_cursor_to_plane(p);
  698. //editor->get_editor_plugin_screen()->call("snap_cursor_to_plane",p);
  699. }
  700. }
  701. }
  702. void GridMapEditor::_update_cursor_instance() {
  703. if (!node) {
  704. return;
  705. }
  706. if (cursor_instance.is_valid())
  707. VisualServer::get_singleton()->free(cursor_instance);
  708. cursor_instance = RID();
  709. if (selected_pallete >= 0) {
  710. if (node && !node->get_theme().is_null()) {
  711. Ref<Mesh> mesh = node->get_theme()->get_item_mesh(selected_pallete);
  712. if (!mesh.is_null() && mesh->get_rid().is_valid()) {
  713. cursor_instance = VisualServer::get_singleton()->instance_create2(mesh->get_rid(), get_tree()->get_root()->get_world()->get_scenario());
  714. VisualServer::get_singleton()->instance_set_transform(cursor_instance, cursor_transform);
  715. }
  716. }
  717. }
  718. }
  719. void GridMapEditor::_item_selected_cbk(int idx) {
  720. selected_pallete = theme_pallete->get_item_metadata(idx);
  721. _update_cursor_instance();
  722. }
  723. void GridMapEditor::_floor_changed(float p_value) {
  724. if (updating)
  725. return;
  726. edit_floor[edit_axis] = p_value;
  727. node->set_meta("_editor_floor_", Vector3(edit_floor[0], edit_floor[1], edit_floor[2]));
  728. update_grid();
  729. _update_clip();
  730. _update_selection_transform();
  731. }
  732. void GridMapEditor::_bind_methods() {
  733. ClassDB::bind_method("_menu_option", &GridMapEditor::_menu_option);
  734. ClassDB::bind_method("_configure", &GridMapEditor::_configure);
  735. ClassDB::bind_method("_item_selected_cbk", &GridMapEditor::_item_selected_cbk);
  736. ClassDB::bind_method("_floor_changed", &GridMapEditor::_floor_changed);
  737. ClassDB::bind_method(D_METHOD("_set_display_mode", "mode"), &GridMapEditor::_set_display_mode);
  738. }
  739. GridMapEditor::GridMapEditor(EditorNode *p_editor) {
  740. input_action = INPUT_NONE;
  741. editor = p_editor;
  742. undo_redo = p_editor->get_undo_redo();
  743. int mw = EDITOR_DEF("editors/grid_map/palette_min_width", 230);
  744. Control *ec = memnew(Control);
  745. ec->set_custom_minimum_size(Size2(mw, 0));
  746. add_child(ec);
  747. spatial_editor_hb = memnew(HBoxContainer);
  748. SpatialEditor::get_singleton()->add_control_to_menu_panel(spatial_editor_hb);
  749. options = memnew(MenuButton);
  750. spatial_editor_hb->add_child(options);
  751. spatial_editor_hb->hide();
  752. options->set_text("Grid");
  753. options->get_popup()->add_check_item(TTR("Snap View"), MENU_OPTION_LOCK_VIEW);
  754. options->get_popup()->add_separator();
  755. options->get_popup()->add_item(vformat(TTR("Prev Level (%sDown Wheel)"), keycode_get_string(KEY_MASK_CMD)), MENU_OPTION_PREV_LEVEL);
  756. options->get_popup()->add_item(vformat(TTR("Next Level (%sUp Wheel)"), keycode_get_string(KEY_MASK_CMD)), MENU_OPTION_NEXT_LEVEL);
  757. options->get_popup()->add_separator();
  758. options->get_popup()->add_check_item(TTR("Clip Disabled"), MENU_OPTION_CLIP_DISABLED);
  759. options->get_popup()->set_item_checked(options->get_popup()->get_item_index(MENU_OPTION_CLIP_DISABLED), true);
  760. options->get_popup()->add_check_item(TTR("Clip Above"), MENU_OPTION_CLIP_ABOVE);
  761. options->get_popup()->add_check_item(TTR("Clip Below"), MENU_OPTION_CLIP_BELOW);
  762. options->get_popup()->add_separator();
  763. options->get_popup()->add_check_item(TTR("Edit X Axis"), MENU_OPTION_X_AXIS, KEY_Z);
  764. options->get_popup()->add_check_item(TTR("Edit Y Axis"), MENU_OPTION_Y_AXIS, KEY_X);
  765. options->get_popup()->add_check_item(TTR("Edit Z Axis"), MENU_OPTION_Z_AXIS, KEY_C);
  766. options->get_popup()->set_item_checked(options->get_popup()->get_item_index(MENU_OPTION_Y_AXIS), true);
  767. options->get_popup()->add_separator();
  768. options->get_popup()->add_item(TTR("Cursor Rotate X"), MENU_OPTION_CURSOR_ROTATE_X, KEY_A);
  769. options->get_popup()->add_item(TTR("Cursor Rotate Y"), MENU_OPTION_CURSOR_ROTATE_Y, KEY_S);
  770. options->get_popup()->add_item(TTR("Cursor Rotate Z"), MENU_OPTION_CURSOR_ROTATE_Z, KEY_D);
  771. options->get_popup()->add_item(TTR("Cursor Back Rotate X"), MENU_OPTION_CURSOR_BACK_ROTATE_X, KEY_MASK_SHIFT + KEY_A);
  772. options->get_popup()->add_item(TTR("Cursor Back Rotate Y"), MENU_OPTION_CURSOR_BACK_ROTATE_Y, KEY_MASK_SHIFT + KEY_S);
  773. options->get_popup()->add_item(TTR("Cursor Back Rotate Z"), MENU_OPTION_CURSOR_BACK_ROTATE_Z, KEY_MASK_SHIFT + KEY_D);
  774. options->get_popup()->add_item(TTR("Cursor Clear Rotation"), MENU_OPTION_CURSOR_CLEAR_ROTATION, KEY_W);
  775. options->get_popup()->add_separator();
  776. options->get_popup()->add_check_item("Duplicate Selects", MENU_OPTION_DUPLICATE_SELECTS);
  777. options->get_popup()->add_separator();
  778. options->get_popup()->add_item(TTR("Create Area"), MENU_OPTION_SELECTION_MAKE_AREA, KEY_CONTROL + KEY_C);
  779. options->get_popup()->add_item(TTR("Create Exterior Connector"), MENU_OPTION_SELECTION_MAKE_EXTERIOR_CONNECTOR);
  780. options->get_popup()->add_item(TTR("Erase Area"), MENU_OPTION_REMOVE_AREA);
  781. options->get_popup()->add_separator();
  782. options->get_popup()->add_item(TTR("Selection -> Duplicate"), MENU_OPTION_SELECTION_DUPLICATE, KEY_MASK_SHIFT + KEY_INSERT);
  783. options->get_popup()->add_item(TTR("Selection -> Clear"), MENU_OPTION_SELECTION_CLEAR, KEY_MASK_SHIFT + KEY_DELETE);
  784. options->get_popup()->add_separator();
  785. options->get_popup()->add_item(TTR("Settings"), MENU_OPTION_GRIDMAP_SETTINGS);
  786. settings_dialog = memnew(ConfirmationDialog);
  787. settings_dialog->set_title(TTR("GridMap Settings"));
  788. add_child(settings_dialog);
  789. settings_vbc = memnew(VBoxContainer);
  790. settings_vbc->set_custom_minimum_size(Size2(200, 0));
  791. settings_dialog->add_child(settings_vbc);
  792. settings_pick_distance = memnew(SpinBox);
  793. settings_pick_distance->set_max(10000.0f);
  794. settings_pick_distance->set_min(500.0f);
  795. settings_pick_distance->set_step(1.0f);
  796. settings_pick_distance->set_value(EDITOR_DEF("editors/grid_map/pick_distance", 5000.0));
  797. settings_vbc->add_margin_child(TTR("Pick Distance:"), settings_pick_distance);
  798. clip_mode = CLIP_DISABLED;
  799. options->get_popup()->connect("id_pressed", this, "_menu_option");
  800. HBoxContainer *hb = memnew(HBoxContainer);
  801. add_child(hb);
  802. hb->set_h_size_flags(SIZE_EXPAND_FILL);
  803. mode_thumbnail = memnew(ToolButton);
  804. mode_thumbnail->set_toggle_mode(true);
  805. mode_thumbnail->set_pressed(true);
  806. mode_thumbnail->set_icon(p_editor->get_gui_base()->get_icon("FileThumbnail", "EditorIcons"));
  807. hb->add_child(mode_thumbnail);
  808. mode_thumbnail->connect("pressed", this, "_set_display_mode", varray(DISPLAY_THUMBNAIL));
  809. mode_list = memnew(ToolButton);
  810. mode_list->set_toggle_mode(true);
  811. mode_list->set_pressed(false);
  812. mode_list->set_icon(p_editor->get_gui_base()->get_icon("FileList", "EditorIcons"));
  813. hb->add_child(mode_list);
  814. mode_list->connect("pressed", this, "_set_display_mode", varray(DISPLAY_LIST));
  815. EDITOR_DEF("editors/grid_map/preview_size", 64);
  816. display_mode = DISPLAY_THUMBNAIL;
  817. theme_pallete = memnew(ItemList);
  818. add_child(theme_pallete);
  819. theme_pallete->set_v_size_flags(SIZE_EXPAND_FILL);
  820. spatial_editor_hb->add_child(memnew(VSeparator));
  821. Label *fl = memnew(Label);
  822. fl->set_text(" Floor: ");
  823. spatial_editor_hb->add_child(fl);
  824. floor = memnew(SpinBox);
  825. floor->set_min(-32767);
  826. floor->set_max(32767);
  827. floor->set_step(1);
  828. floor->get_line_edit()->add_constant_override("minimum_spaces", 16);
  829. spatial_editor_hb->add_child(floor);
  830. floor->connect("value_changed", this, "_floor_changed");
  831. edit_axis = Vector3::AXIS_Y;
  832. edit_floor[0] = -1;
  833. edit_floor[1] = -1;
  834. edit_floor[2] = -1;
  835. cursor_visible = false;
  836. selected_pallete = -1;
  837. lock_view = false;
  838. cursor_rot = 0;
  839. last_mouseover = Vector3(-1, -1, -1);
  840. selection_mesh = VisualServer::get_singleton()->mesh_create();
  841. duplicate_mesh = VisualServer::get_singleton()->mesh_create();
  842. {
  843. //selection mesh create
  844. PoolVector<Vector3> lines;
  845. PoolVector<Vector3> triangles;
  846. PoolVector<Vector3> square[3];
  847. for (int i = 0; i < 6; i++) {
  848. Vector3 face_points[4];
  849. for (int j = 0; j < 4; j++) {
  850. float v[3];
  851. v[0] = 1.0;
  852. v[1] = 1 - 2 * ((j >> 1) & 1);
  853. v[2] = v[1] * (1 - 2 * (j & 1));
  854. for (int k = 0; k < 3; k++) {
  855. if (i < 3)
  856. face_points[j][(i + k) % 3] = v[k] * (i >= 3 ? -1 : 1);
  857. else
  858. face_points[3 - j][(i + k) % 3] = v[k] * (i >= 3 ? -1 : 1);
  859. }
  860. }
  861. triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5));
  862. triangles.push_back(face_points[1] * 0.5 + Vector3(0.5, 0.5, 0.5));
  863. triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5));
  864. triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5));
  865. triangles.push_back(face_points[3] * 0.5 + Vector3(0.5, 0.5, 0.5));
  866. triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5));
  867. }
  868. for (int i = 0; i < 12; i++) {
  869. Rect3 base(Vector3(0, 0, 0), Vector3(1, 1, 1));
  870. Vector3 a, b;
  871. base.get_edge(i, a, b);
  872. lines.push_back(a);
  873. lines.push_back(b);
  874. }
  875. for (int i = 0; i < 3; i++) {
  876. Vector3 points[4];
  877. for (int j = 0; j < 4; j++) {
  878. static const bool orderx[4] = { 0, 1, 1, 0 };
  879. static const bool ordery[4] = { 0, 0, 1, 1 };
  880. Vector3 sp;
  881. if (orderx[j]) {
  882. sp[(i + 1) % 3] = 1.0;
  883. }
  884. if (ordery[j]) {
  885. sp[(i + 2) % 3] = 1.0;
  886. }
  887. points[j] = sp;
  888. }
  889. for (int j = 0; j < 4; j++) {
  890. Vector3 ofs;
  891. ofs[i] += 0.01;
  892. square[i].push_back(points[j] - ofs);
  893. square[i].push_back(points[(j + 1) % 4] - ofs);
  894. square[i].push_back(points[j] + ofs);
  895. square[i].push_back(points[(j + 1) % 4] + ofs);
  896. }
  897. }
  898. Array d;
  899. d.resize(VS::ARRAY_MAX);
  900. inner_mat.instance();
  901. inner_mat->set_albedo(Color(0.7, 0.7, 1.0, 0.2));
  902. //inner_mat->set_flag(SpatialMaterial::FLAG_ONTOP, true);
  903. inner_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  904. inner_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  905. d[VS::ARRAY_VERTEX] = triangles;
  906. VisualServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, VS::PRIMITIVE_TRIANGLES, d);
  907. VisualServer::get_singleton()->mesh_surface_set_material(selection_mesh, 0, inner_mat->get_rid());
  908. outer_mat.instance();
  909. outer_mat->set_albedo(Color(0.7, 0.7, 1.0, 0.8));
  910. outer_mat->set_on_top_of_alpha();
  911. outer_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  912. outer_mat->set_line_width(3.0);
  913. outer_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  914. selection_floor_mat.instance();
  915. selection_floor_mat->set_albedo(Color(0.80, 0.80, 1.0, 1));
  916. selection_floor_mat->set_on_top_of_alpha();
  917. selection_floor_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  918. selection_floor_mat->set_line_width(3.0);
  919. //selection_floor_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  920. d[VS::ARRAY_VERTEX] = lines;
  921. VisualServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, VS::PRIMITIVE_LINES, d);
  922. VisualServer::get_singleton()->mesh_surface_set_material(selection_mesh, 1, outer_mat->get_rid());
  923. d[VS::ARRAY_VERTEX] = triangles;
  924. VisualServer::get_singleton()->mesh_add_surface_from_arrays(duplicate_mesh, VS::PRIMITIVE_TRIANGLES, d);
  925. VisualServer::get_singleton()->mesh_surface_set_material(duplicate_mesh, 0, inner_mat->get_rid());
  926. d[VS::ARRAY_VERTEX] = lines;
  927. VisualServer::get_singleton()->mesh_add_surface_from_arrays(duplicate_mesh, VS::PRIMITIVE_LINES, d);
  928. VisualServer::get_singleton()->mesh_surface_set_material(duplicate_mesh, 1, outer_mat->get_rid());
  929. for (int i = 0; i < 3; i++) {
  930. d[VS::ARRAY_VERTEX] = square[i];
  931. selection_level_mesh[i] = VS::get_singleton()->mesh_create();
  932. VisualServer::get_singleton()->mesh_add_surface_from_arrays(selection_level_mesh[i], VS::PRIMITIVE_LINES, d);
  933. VisualServer::get_singleton()->mesh_surface_set_material(selection_level_mesh[i], 0, selection_floor_mat->get_rid());
  934. }
  935. }
  936. selection.active = false;
  937. updating = false;
  938. }
  939. GridMapEditor::~GridMapEditor() {
  940. for (int i = 0; i < 3; i++) {
  941. if (grid[i].is_valid())
  942. VisualServer::get_singleton()->free(grid[i]);
  943. if (grid_instance[i].is_valid())
  944. VisualServer::get_singleton()->free(grid_instance[i]);
  945. if (cursor_instance.is_valid())
  946. VisualServer::get_singleton()->free(cursor_instance);
  947. if (selection_level_instance[i].is_valid()) {
  948. VisualServer::get_singleton()->free(selection_level_instance[i]);
  949. }
  950. }
  951. VisualServer::get_singleton()->free(selection_mesh);
  952. if (selection_instance.is_valid())
  953. VisualServer::get_singleton()->free(selection_instance);
  954. VisualServer::get_singleton()->free(duplicate_mesh);
  955. if (duplicate_instance.is_valid())
  956. VisualServer::get_singleton()->free(duplicate_instance);
  957. }
  958. void GridMapEditorPlugin::edit(Object *p_object) {
  959. gridmap_editor->edit(Object::cast_to<GridMap>(p_object));
  960. }
  961. bool GridMapEditorPlugin::handles(Object *p_object) const {
  962. return p_object->is_class("GridMap");
  963. }
  964. void GridMapEditorPlugin::make_visible(bool p_visible) {
  965. if (p_visible) {
  966. gridmap_editor->show();
  967. gridmap_editor->spatial_editor_hb->show();
  968. gridmap_editor->set_process(true);
  969. } else {
  970. gridmap_editor->spatial_editor_hb->hide();
  971. gridmap_editor->hide();
  972. gridmap_editor->edit(NULL);
  973. gridmap_editor->set_process(false);
  974. }
  975. }
  976. GridMapEditorPlugin::GridMapEditorPlugin(EditorNode *p_node) {
  977. editor = p_node;
  978. gridmap_editor = memnew(GridMapEditor(editor));
  979. SpatialEditor::get_singleton()->get_palette_split()->add_child(gridmap_editor);
  980. SpatialEditor::get_singleton()->get_palette_split()->move_child(gridmap_editor, 0);
  981. gridmap_editor->hide();
  982. }
  983. GridMapEditorPlugin::~GridMapEditorPlugin() {
  984. }