grid_map_editor_plugin.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  1. /*************************************************************************/
  2. /* grid_map_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.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. if (!selection.active) {
  188. Transform xf;
  189. xf.basis.set_zero();
  190. VisualServer::get_singleton()->instance_set_transform(selection_instance, xf);
  191. return;
  192. }
  193. Transform xf;
  194. xf.scale(Vector3(1, 1, 1) * (Vector3(1, 1, 1) + (selection.end - selection.begin)) * node->get_cell_size());
  195. xf.origin = selection.begin * node->get_cell_size();
  196. VisualServer::get_singleton()->instance_set_transform(selection_instance, node->get_global_transform() * xf);
  197. }
  198. void GridMapEditor::_validate_selection() {
  199. if (!selection.active)
  200. return;
  201. selection.begin = selection.click;
  202. selection.end = selection.current;
  203. if (selection.begin.x > selection.end.x)
  204. SWAP(selection.begin.x, selection.end.x);
  205. if (selection.begin.y > selection.end.y)
  206. SWAP(selection.begin.y, selection.end.y);
  207. if (selection.begin.z > selection.end.z)
  208. SWAP(selection.begin.z, selection.end.z);
  209. _update_selection_transform();
  210. }
  211. bool GridMapEditor::do_input_action(Camera *p_camera, const Point2 &p_point, bool p_click) {
  212. if (!spatial_editor)
  213. return false;
  214. if (selected_pallete < 0 && input_action != INPUT_COPY && input_action != INPUT_SELECT && input_action != INPUT_DUPLICATE)
  215. return false;
  216. Ref<MeshLibrary> theme = node->get_theme();
  217. if (theme.is_null())
  218. return false;
  219. if (input_action != INPUT_COPY && input_action != INPUT_SELECT && input_action != INPUT_DUPLICATE && !theme->has_item(selected_pallete))
  220. return false;
  221. Camera *camera = p_camera;
  222. Vector3 from = camera->project_ray_origin(p_point);
  223. Vector3 normal = camera->project_ray_normal(p_point);
  224. Transform local_xform = node->get_global_transform().affine_inverse();
  225. Vector<Plane> planes = camera->get_frustum();
  226. from = local_xform.xform(from);
  227. normal = local_xform.basis.xform(normal).normalized();
  228. Plane p;
  229. p.normal[edit_axis] = 1.0;
  230. p.d = edit_floor[edit_axis] * node->get_cell_size();
  231. Vector3 inters;
  232. if (!p.intersects_segment(from, from + normal * settings_pick_distance->get_value(), &inters))
  233. return false;
  234. //make sure the intersection is inside the frustum planes, to avoid
  235. //painting on invisible regions
  236. for (int i = 0; i < planes.size(); i++) {
  237. Plane fp = local_xform.xform(planes[i]);
  238. if (fp.is_point_over(inters))
  239. return false;
  240. }
  241. int cell[3];
  242. float cell_size[3] = { node->get_cell_size(), node->get_cell_size(), node->get_cell_size() };
  243. last_mouseover = Vector3(-1, -1, -1);
  244. for (int i = 0; i < 3; i++) {
  245. if (i == edit_axis)
  246. cell[i] = edit_floor[i];
  247. else {
  248. cell[i] = inters[i] / node->get_cell_size();
  249. if (inters[i] < 0)
  250. cell[i] -= 1; //compensate negative
  251. grid_ofs[i] = cell[i] * cell_size[i];
  252. }
  253. /*if (cell[i]<0 || cell[i]>=grid_size[i]) {
  254. cursor_visible=false;
  255. _update_cursor_transform();
  256. return false;
  257. }*/
  258. }
  259. last_mouseover = Vector3(cell[0], cell[1], cell[2]);
  260. VS::get_singleton()->instance_set_transform(grid_instance[edit_axis], Transform(Basis(), grid_ofs));
  261. if (cursor_instance.is_valid()) {
  262. 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();
  263. cursor_visible = true;
  264. _update_cursor_transform();
  265. }
  266. if (input_action == INPUT_DUPLICATE) {
  267. selection.current = Vector3(cell[0], cell[1], cell[2]);
  268. _update_duplicate_indicator();
  269. } else if (input_action == INPUT_SELECT) {
  270. selection.current = Vector3(cell[0], cell[1], cell[2]);
  271. if (p_click)
  272. selection.click = selection.current;
  273. selection.active = true;
  274. _validate_selection();
  275. return true;
  276. } else if (input_action == INPUT_COPY) {
  277. int item = node->get_cell_item(cell[0], cell[1], cell[2]);
  278. if (item >= 0) {
  279. selected_pallete = item;
  280. theme_pallete->set_current(item);
  281. update_pallete();
  282. _update_cursor_instance();
  283. }
  284. return true;
  285. }
  286. if (input_action == INPUT_PAINT) {
  287. SetItem si;
  288. si.pos = Vector3(cell[0], cell[1], cell[2]);
  289. si.new_value = selected_pallete;
  290. si.new_orientation = cursor_rot;
  291. si.old_value = node->get_cell_item(cell[0], cell[1], cell[2]);
  292. si.old_orientation = node->get_cell_item_orientation(cell[0], cell[1], cell[2]);
  293. set_items.push_back(si);
  294. node->set_cell_item(cell[0], cell[1], cell[2], selected_pallete, cursor_rot);
  295. return true;
  296. } else if (input_action == INPUT_ERASE) {
  297. SetItem si;
  298. si.pos = Vector3(cell[0], cell[1], cell[2]);
  299. si.new_value = -1;
  300. si.new_orientation = 0;
  301. si.old_value = node->get_cell_item(cell[0], cell[1], cell[2]);
  302. si.old_orientation = node->get_cell_item_orientation(cell[0], cell[1], cell[2]);
  303. set_items.push_back(si);
  304. node->set_cell_item(cell[0], cell[1], cell[2], -1);
  305. return true;
  306. }
  307. return false;
  308. }
  309. void GridMapEditor::_delete_selection() {
  310. if (!selection.active)
  311. return;
  312. undo_redo->create_action(TTR("GridMap Delete Selection"));
  313. for (int i = selection.begin.x; i <= selection.end.x; i++) {
  314. for (int j = selection.begin.y; j <= selection.end.y; j++) {
  315. for (int k = selection.begin.z; k <= selection.end.z; k++) {
  316. undo_redo->add_do_method(node, "set_cell_item", i, j, k, GridMap::INVALID_CELL_ITEM);
  317. 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));
  318. }
  319. }
  320. }
  321. undo_redo->commit_action();
  322. selection.active = false;
  323. _validate_selection();
  324. }
  325. void GridMapEditor::_update_duplicate_indicator() {
  326. if (!selection.active || input_action != INPUT_DUPLICATE) {
  327. Transform xf;
  328. xf.basis.set_zero();
  329. VisualServer::get_singleton()->instance_set_transform(duplicate_instance, xf);
  330. return;
  331. }
  332. Transform xf;
  333. xf.scale(Vector3(1, 1, 1) * (Vector3(1, 1, 1) + (selection.end - selection.begin)) * node->get_cell_size());
  334. xf.origin = (selection.begin + (selection.current - selection.click)) * node->get_cell_size();
  335. Basis rot;
  336. rot.set_orthogonal_index(selection.duplicate_rot);
  337. xf.basis = rot * xf.basis;
  338. VisualServer::get_singleton()->instance_set_transform(duplicate_instance, node->get_global_transform() * xf);
  339. }
  340. struct __Item {
  341. Vector3 pos;
  342. int rot;
  343. int item;
  344. };
  345. void GridMapEditor::_duplicate_paste() {
  346. if (!selection.active)
  347. return;
  348. int idx = options->get_popup()->get_item_index(MENU_OPTION_DUPLICATE_SELECTS);
  349. bool reselect = options->get_popup()->is_item_checked(idx);
  350. List<__Item> items;
  351. Basis rot;
  352. rot.set_orthogonal_index(selection.duplicate_rot);
  353. for (int i = selection.begin.x; i <= selection.end.x; i++) {
  354. for (int j = selection.begin.y; j <= selection.end.y; j++) {
  355. for (int k = selection.begin.z; k <= selection.end.z; k++) {
  356. int itm = node->get_cell_item(i, j, k);
  357. if (itm == GridMap::INVALID_CELL_ITEM)
  358. continue;
  359. int orientation = node->get_cell_item_orientation(i, j, k);
  360. __Item item;
  361. Vector3 rel = Vector3(i, j, k) - selection.begin;
  362. rel = rot.xform(rel);
  363. Basis orm;
  364. orm.set_orthogonal_index(orientation);
  365. orm = rot * orm;
  366. item.pos = selection.begin + rel;
  367. item.item = itm;
  368. item.rot = orm.get_orthogonal_index();
  369. items.push_back(item);
  370. }
  371. }
  372. }
  373. Vector3 ofs = selection.current - selection.click;
  374. if (items.size()) {
  375. undo_redo->create_action(TTR("GridMap Duplicate Selection"));
  376. for (List<__Item>::Element *E = items.front(); E; E = E->next()) {
  377. __Item &it = E->get();
  378. Vector3 pos = it.pos + ofs;
  379. undo_redo->add_do_method(node, "set_cell_item", pos.x, pos.y, pos.z, it.item, it.rot);
  380. 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));
  381. }
  382. undo_redo->commit_action();
  383. }
  384. if (reselect) {
  385. selection.begin += ofs;
  386. selection.end += ofs;
  387. selection.click = selection.begin;
  388. selection.current = selection.end;
  389. _validate_selection();
  390. }
  391. }
  392. bool GridMapEditor::forward_spatial_input_event(Camera *p_camera, const Ref<InputEvent> &p_event) {
  393. if (!node) {
  394. return false;
  395. }
  396. Ref<InputEventMouseButton> mb = p_event;
  397. if (mb.is_valid()) {
  398. if (mb->get_button_index() == BUTTON_WHEEL_UP && (mb->get_command() || mb->get_shift())) {
  399. if (mb->is_pressed())
  400. floor->set_value(floor->get_value() + mb->get_factor());
  401. return true; //eaten
  402. } else if (mb->get_button_index() == BUTTON_WHEEL_DOWN && (mb->get_command() || mb->get_shift())) {
  403. if (mb->is_pressed())
  404. floor->set_value(floor->get_value() - mb->get_factor());
  405. return true;
  406. }
  407. if (mb->is_pressed()) {
  408. if (mb->get_button_index() == BUTTON_LEFT) {
  409. if (input_action == INPUT_DUPLICATE) {
  410. //paste
  411. _duplicate_paste();
  412. input_action = INPUT_NONE;
  413. _update_duplicate_indicator();
  414. } else if (mb->get_shift()) {
  415. input_action = INPUT_SELECT;
  416. } else if (mb->get_command())
  417. input_action = INPUT_COPY;
  418. else {
  419. input_action = INPUT_PAINT;
  420. set_items.clear();
  421. }
  422. } else if (mb->get_button_index() == BUTTON_RIGHT)
  423. if (input_action == INPUT_DUPLICATE) {
  424. input_action = INPUT_NONE;
  425. _update_duplicate_indicator();
  426. } else {
  427. input_action = INPUT_ERASE;
  428. set_items.clear();
  429. }
  430. else
  431. return false;
  432. return do_input_action(p_camera, Point2(mb->get_position().x, mb->get_position().y), true);
  433. } else {
  434. if (
  435. (mb->get_button_index() == BUTTON_RIGHT && input_action == INPUT_ERASE) ||
  436. (mb->get_button_index() == BUTTON_LEFT && input_action == INPUT_PAINT)) {
  437. if (set_items.size()) {
  438. undo_redo->create_action("GridMap Paint");
  439. for (List<SetItem>::Element *E = set_items.front(); E; E = E->next()) {
  440. const SetItem &si = E->get();
  441. undo_redo->add_do_method(node, "set_cell_item", si.pos.x, si.pos.y, si.pos.z, si.new_value, si.new_orientation);
  442. }
  443. for (List<SetItem>::Element *E = set_items.back(); E; E = E->prev()) {
  444. const SetItem &si = E->get();
  445. undo_redo->add_undo_method(node, "set_cell_item", si.pos.x, si.pos.y, si.pos.z, si.old_value, si.old_orientation);
  446. }
  447. undo_redo->commit_action();
  448. }
  449. set_items.clear();
  450. input_action = INPUT_NONE;
  451. return true;
  452. }
  453. if (mb->get_button_index() == BUTTON_LEFT && input_action != INPUT_NONE) {
  454. set_items.clear();
  455. input_action = INPUT_NONE;
  456. return true;
  457. }
  458. if (mb->get_button_index() == BUTTON_RIGHT && (input_action == INPUT_ERASE || input_action == INPUT_DUPLICATE)) {
  459. input_action = INPUT_NONE;
  460. return true;
  461. }
  462. }
  463. }
  464. Ref<InputEventMouseMotion> mm = p_event;
  465. if (mm.is_valid()) {
  466. return do_input_action(p_camera, mm->get_position(), false);
  467. }
  468. return false;
  469. }
  470. struct _CGMEItemSort {
  471. String name;
  472. int id;
  473. _FORCE_INLINE_ bool operator<(const _CGMEItemSort &r_it) const { return name < r_it.name; }
  474. };
  475. void GridMapEditor::_set_display_mode(int p_mode) {
  476. if (display_mode == p_mode) {
  477. return;
  478. }
  479. if (p_mode == DISPLAY_LIST) {
  480. mode_list->set_pressed(true);
  481. mode_thumbnail->set_pressed(false);
  482. } else if (p_mode == DISPLAY_THUMBNAIL) {
  483. mode_list->set_pressed(false);
  484. mode_thumbnail->set_pressed(true);
  485. }
  486. display_mode = p_mode;
  487. update_pallete();
  488. }
  489. void GridMapEditor::update_pallete() {
  490. int selected = theme_pallete->get_current();
  491. theme_pallete->clear();
  492. if (display_mode == DISPLAY_THUMBNAIL) {
  493. theme_pallete->set_max_columns(0);
  494. theme_pallete->set_icon_mode(ItemList::ICON_MODE_TOP);
  495. } else if (display_mode == DISPLAY_LIST) {
  496. theme_pallete->set_max_columns(1);
  497. theme_pallete->set_icon_mode(ItemList::ICON_MODE_LEFT);
  498. }
  499. float min_size = EDITOR_DEF("editors/grid_map/preview_size", 64);
  500. theme_pallete->set_fixed_icon_size(Size2(min_size, min_size));
  501. theme_pallete->set_fixed_column_width(min_size * 3 / 2);
  502. theme_pallete->set_max_text_lines(2);
  503. Ref<MeshLibrary> theme = node->get_theme();
  504. if (theme.is_null()) {
  505. last_theme = NULL;
  506. return;
  507. }
  508. Vector<int> ids;
  509. ids = theme->get_item_list();
  510. List<_CGMEItemSort> il;
  511. for (int i = 0; i < ids.size(); i++) {
  512. _CGMEItemSort is;
  513. is.id = ids[i];
  514. is.name = theme->get_item_name(ids[i]);
  515. il.push_back(is);
  516. }
  517. il.sort();
  518. int item = 0;
  519. for (List<_CGMEItemSort>::Element *E = il.front(); E; E = E->next()) {
  520. int id = E->get().id;
  521. theme_pallete->add_item("");
  522. String name = theme->get_item_name(id);
  523. Ref<Texture> preview = theme->get_item_preview(id);
  524. if (!preview.is_null()) {
  525. theme_pallete->set_item_icon(item, preview);
  526. theme_pallete->set_item_tooltip(item, name);
  527. }
  528. if (name != "") {
  529. theme_pallete->set_item_text(item, name);
  530. }
  531. theme_pallete->set_item_metadata(item, id);
  532. item++;
  533. }
  534. if (selected != -1) {
  535. theme_pallete->select(selected);
  536. }
  537. last_theme = theme.operator->();
  538. }
  539. void GridMapEditor::edit(GridMap *p_gridmap) {
  540. node = p_gridmap;
  541. VS *vs = VS::get_singleton();
  542. last_mouseover = Vector3(-1, -1, -1);
  543. input_action = INPUT_NONE;
  544. selection.active = false;
  545. _update_selection_transform();
  546. _update_duplicate_indicator();
  547. spatial_editor = Object::cast_to<SpatialEditorPlugin>(editor->get_editor_plugin_screen());
  548. if (!node) {
  549. set_process(false);
  550. for (int i = 0; i < 3; i++) {
  551. VisualServer::get_singleton()->instance_set_visible(grid_instance[i], false);
  552. }
  553. VisualServer::get_singleton()->instance_set_visible(cursor_instance, false);
  554. return;
  555. }
  556. update_pallete();
  557. set_process(true);
  558. Vector3 edited_floor = p_gridmap->get_meta("_editor_floor_");
  559. clip_mode = p_gridmap->has_meta("_editor_clip_") ? ClipMode(p_gridmap->get_meta("_editor_clip_").operator int()) : CLIP_DISABLED;
  560. for (int i = 0; i < 3; i++) {
  561. if (vs->mesh_get_surface_count(grid[i]) > 0)
  562. vs->mesh_remove_surface(grid[i], 0);
  563. edit_floor[i] = edited_floor[i];
  564. }
  565. {
  566. //update grids
  567. indicator_mat.instance();
  568. indicator_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  569. indicator_mat->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  570. indicator_mat->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  571. indicator_mat->set_albedo(Color(0.8, 0.5, 0.1));
  572. Vector<Vector3> grid_points[3];
  573. Vector<Color> grid_colors[3];
  574. float cell_size[3] = { p_gridmap->get_cell_size(), p_gridmap->get_cell_size(), p_gridmap->get_cell_size() };
  575. for (int i = 0; i < 3; i++) {
  576. Vector3 axis;
  577. axis[i] = 1;
  578. Vector3 axis_n1;
  579. axis_n1[(i + 1) % 3] = cell_size[(i + 1) % 3];
  580. Vector3 axis_n2;
  581. axis_n2[(i + 2) % 3] = cell_size[(i + 2) % 3];
  582. for (int j = -GRID_CURSOR_SIZE; j <= GRID_CURSOR_SIZE; j++) {
  583. for (int k = -GRID_CURSOR_SIZE; k <= GRID_CURSOR_SIZE; k++) {
  584. Vector3 p = axis_n1 * j + axis_n2 * k;
  585. float trans = Math::pow(MAX(0, 1.0 - (Vector2(j, k).length() / GRID_CURSOR_SIZE)), 2);
  586. Vector3 pj = axis_n1 * (j + 1) + axis_n2 * k;
  587. float transj = Math::pow(MAX(0, 1.0 - (Vector2(j + 1, k).length() / GRID_CURSOR_SIZE)), 2);
  588. Vector3 pk = axis_n1 * j + axis_n2 * (k + 1);
  589. float transk = Math::pow(MAX(0, 1.0 - (Vector2(j, k + 1).length() / GRID_CURSOR_SIZE)), 2);
  590. grid_points[i].push_back(p);
  591. grid_points[i].push_back(pk);
  592. grid_colors[i].push_back(Color(1, 1, 1, trans));
  593. grid_colors[i].push_back(Color(1, 1, 1, transk));
  594. grid_points[i].push_back(p);
  595. grid_points[i].push_back(pj);
  596. grid_colors[i].push_back(Color(1, 1, 1, trans));
  597. grid_colors[i].push_back(Color(1, 1, 1, transj));
  598. }
  599. }
  600. Array d;
  601. d.resize(VS::ARRAY_MAX);
  602. d[VS::ARRAY_VERTEX] = grid_points[i];
  603. d[VS::ARRAY_COLOR] = grid_colors[i];
  604. VisualServer::get_singleton()->mesh_add_surface_from_arrays(grid[i], VisualServer::PRIMITIVE_LINES, d);
  605. VisualServer::get_singleton()->mesh_surface_set_material(grid[i], 0, indicator_mat->get_rid());
  606. }
  607. }
  608. update_grid();
  609. _update_clip();
  610. }
  611. void GridMapEditor::_update_clip() {
  612. node->set_meta("_editor_clip_", clip_mode);
  613. if (clip_mode == CLIP_DISABLED)
  614. node->set_clip(false);
  615. else
  616. node->set_clip(true, clip_mode == CLIP_ABOVE, edit_floor[edit_axis], edit_axis);
  617. }
  618. void GridMapEditor::update_grid() {
  619. grid_xform.origin.x -= 1; //force update in hackish way.. what do i care
  620. //VS *vs = VS::get_singleton();
  621. grid_ofs[edit_axis] = edit_floor[edit_axis] * node->get_cell_size();
  622. edit_grid_xform.origin = grid_ofs;
  623. edit_grid_xform.basis = Basis();
  624. for (int i = 0; i < 3; i++) {
  625. VisualServer::get_singleton()->instance_set_visible(grid_instance[i], i == edit_axis);
  626. }
  627. updating = true;
  628. floor->set_value(edit_floor[edit_axis]);
  629. updating = false;
  630. }
  631. void GridMapEditor::_notification(int p_what) {
  632. if (p_what == NOTIFICATION_ENTER_TREE) {
  633. theme_pallete->connect("item_selected", this, "_item_selected_cbk");
  634. for (int i = 0; i < 3; i++) {
  635. grid[i] = VS::get_singleton()->mesh_create();
  636. grid_instance[i] = VS::get_singleton()->instance_create2(grid[i], get_tree()->get_root()->get_world()->get_scenario());
  637. }
  638. selection_instance = VisualServer::get_singleton()->instance_create2(selection_mesh, get_tree()->get_root()->get_world()->get_scenario());
  639. duplicate_instance = VisualServer::get_singleton()->instance_create2(duplicate_mesh, get_tree()->get_root()->get_world()->get_scenario());
  640. _update_selection_transform();
  641. _update_duplicate_indicator();
  642. } else if (p_what == NOTIFICATION_EXIT_TREE) {
  643. for (int i = 0; i < 3; i++) {
  644. VS::get_singleton()->free(grid_instance[i]);
  645. VS::get_singleton()->free(grid[i]);
  646. grid_instance[i] = RID();
  647. grid[i] = RID();
  648. }
  649. VisualServer::get_singleton()->free(selection_instance);
  650. VisualServer::get_singleton()->free(duplicate_instance);
  651. selection_instance = RID();
  652. duplicate_instance = RID();
  653. } else if (p_what == NOTIFICATION_PROCESS) {
  654. Transform xf = node->get_global_transform();
  655. if (xf != grid_xform) {
  656. for (int i = 0; i < 3; i++) {
  657. VS::get_singleton()->instance_set_transform(grid_instance[i], xf * edit_grid_xform);
  658. }
  659. grid_xform = xf;
  660. }
  661. Ref<MeshLibrary> cgmt = node->get_theme();
  662. if (cgmt.operator->() != last_theme)
  663. update_pallete();
  664. if (lock_view) {
  665. EditorNode *editor = Object::cast_to<EditorNode>(get_tree()->get_root()->get_child(0));
  666. Plane p;
  667. p.normal[edit_axis] = 1.0;
  668. p.d = edit_floor[edit_axis] * node->get_cell_size();
  669. p = node->get_transform().xform(p); // plane to snap
  670. SpatialEditorPlugin *sep = Object::cast_to<SpatialEditorPlugin>(editor->get_editor_plugin_screen());
  671. if (sep)
  672. sep->snap_cursor_to_plane(p);
  673. //editor->get_editor_plugin_screen()->call("snap_cursor_to_plane",p);
  674. }
  675. }
  676. }
  677. void GridMapEditor::_update_cursor_instance() {
  678. if (!node) {
  679. return;
  680. }
  681. if (cursor_instance.is_valid())
  682. VisualServer::get_singleton()->free(cursor_instance);
  683. cursor_instance = RID();
  684. if (selected_pallete >= 0) {
  685. if (node && !node->get_theme().is_null()) {
  686. Ref<Mesh> mesh = node->get_theme()->get_item_mesh(selected_pallete);
  687. if (!mesh.is_null() && mesh->get_rid().is_valid()) {
  688. cursor_instance = VisualServer::get_singleton()->instance_create2(mesh->get_rid(), get_tree()->get_root()->get_world()->get_scenario());
  689. VisualServer::get_singleton()->instance_set_transform(cursor_instance, cursor_transform);
  690. }
  691. }
  692. }
  693. }
  694. void GridMapEditor::_item_selected_cbk(int idx) {
  695. selected_pallete = theme_pallete->get_item_metadata(idx);
  696. _update_cursor_instance();
  697. }
  698. void GridMapEditor::_floor_changed(float p_value) {
  699. if (updating)
  700. return;
  701. edit_floor[edit_axis] = p_value;
  702. node->set_meta("_editor_floor_", Vector3(edit_floor[0], edit_floor[1], edit_floor[2]));
  703. update_grid();
  704. _update_clip();
  705. }
  706. void GridMapEditor::_bind_methods() {
  707. ClassDB::bind_method("_menu_option", &GridMapEditor::_menu_option);
  708. ClassDB::bind_method("_configure", &GridMapEditor::_configure);
  709. ClassDB::bind_method("_item_selected_cbk", &GridMapEditor::_item_selected_cbk);
  710. ClassDB::bind_method("_floor_changed", &GridMapEditor::_floor_changed);
  711. ClassDB::bind_method(D_METHOD("_set_display_mode", "mode"), &GridMapEditor::_set_display_mode);
  712. }
  713. GridMapEditor::GridMapEditor(EditorNode *p_editor) {
  714. input_action = INPUT_NONE;
  715. editor = p_editor;
  716. undo_redo = p_editor->get_undo_redo();
  717. int mw = EDITOR_DEF("editors/grid_map/palette_min_width", 230);
  718. Control *ec = memnew(Control);
  719. ec->set_custom_minimum_size(Size2(mw, 0));
  720. add_child(ec);
  721. spatial_editor_hb = memnew(HBoxContainer);
  722. SpatialEditor::get_singleton()->add_control_to_menu_panel(spatial_editor_hb);
  723. options = memnew(MenuButton);
  724. spatial_editor_hb->add_child(options);
  725. spatial_editor_hb->hide();
  726. options->set_text("Grid");
  727. options->get_popup()->add_check_item(TTR("Snap View"), MENU_OPTION_LOCK_VIEW);
  728. options->get_popup()->add_separator();
  729. options->get_popup()->add_item(vformat(TTR("Prev Level (%sDown Wheel)"), keycode_get_string(KEY_MASK_CMD)), MENU_OPTION_PREV_LEVEL);
  730. options->get_popup()->add_item(vformat(TTR("Next Level (%sUp Wheel)"), keycode_get_string(KEY_MASK_CMD)), MENU_OPTION_NEXT_LEVEL);
  731. options->get_popup()->add_separator();
  732. options->get_popup()->add_check_item(TTR("Clip Disabled"), MENU_OPTION_CLIP_DISABLED);
  733. options->get_popup()->set_item_checked(options->get_popup()->get_item_index(MENU_OPTION_CLIP_DISABLED), true);
  734. options->get_popup()->add_check_item(TTR("Clip Above"), MENU_OPTION_CLIP_ABOVE);
  735. options->get_popup()->add_check_item(TTR("Clip Below"), MENU_OPTION_CLIP_BELOW);
  736. options->get_popup()->add_separator();
  737. options->get_popup()->add_check_item(TTR("Edit X Axis"), MENU_OPTION_X_AXIS, KEY_Z);
  738. options->get_popup()->add_check_item(TTR("Edit Y Axis"), MENU_OPTION_Y_AXIS, KEY_X);
  739. options->get_popup()->add_check_item(TTR("Edit Z Axis"), MENU_OPTION_Z_AXIS, KEY_C);
  740. options->get_popup()->set_item_checked(options->get_popup()->get_item_index(MENU_OPTION_Y_AXIS), true);
  741. options->get_popup()->add_separator();
  742. options->get_popup()->add_item(TTR("Cursor Rotate X"), MENU_OPTION_CURSOR_ROTATE_X, KEY_A);
  743. options->get_popup()->add_item(TTR("Cursor Rotate Y"), MENU_OPTION_CURSOR_ROTATE_Y, KEY_S);
  744. options->get_popup()->add_item(TTR("Cursor Rotate Z"), MENU_OPTION_CURSOR_ROTATE_Z, KEY_D);
  745. options->get_popup()->add_item(TTR("Cursor Back Rotate X"), MENU_OPTION_CURSOR_BACK_ROTATE_X, KEY_MASK_SHIFT + KEY_A);
  746. options->get_popup()->add_item(TTR("Cursor Back Rotate Y"), MENU_OPTION_CURSOR_BACK_ROTATE_Y, KEY_MASK_SHIFT + KEY_S);
  747. options->get_popup()->add_item(TTR("Cursor Back Rotate Z"), MENU_OPTION_CURSOR_BACK_ROTATE_Z, KEY_MASK_SHIFT + KEY_D);
  748. options->get_popup()->add_item(TTR("Cursor Clear Rotation"), MENU_OPTION_CURSOR_CLEAR_ROTATION, KEY_W);
  749. options->get_popup()->add_separator();
  750. options->get_popup()->add_check_item("Duplicate Selects", MENU_OPTION_DUPLICATE_SELECTS);
  751. options->get_popup()->add_separator();
  752. options->get_popup()->add_item(TTR("Create Area"), MENU_OPTION_SELECTION_MAKE_AREA, KEY_CONTROL + KEY_C);
  753. options->get_popup()->add_item(TTR("Create Exterior Connector"), MENU_OPTION_SELECTION_MAKE_EXTERIOR_CONNECTOR);
  754. options->get_popup()->add_item(TTR("Erase Area"), MENU_OPTION_REMOVE_AREA);
  755. options->get_popup()->add_separator();
  756. options->get_popup()->add_item(TTR("Selection -> Duplicate"), MENU_OPTION_SELECTION_DUPLICATE, KEY_MASK_SHIFT + KEY_INSERT);
  757. options->get_popup()->add_item(TTR("Selection -> Clear"), MENU_OPTION_SELECTION_CLEAR, KEY_MASK_SHIFT + KEY_DELETE);
  758. options->get_popup()->add_separator();
  759. options->get_popup()->add_item(TTR("Settings"), MENU_OPTION_GRIDMAP_SETTINGS);
  760. settings_dialog = memnew(ConfirmationDialog);
  761. settings_dialog->set_title(TTR("GridMap Settings"));
  762. add_child(settings_dialog);
  763. settings_vbc = memnew(VBoxContainer);
  764. settings_vbc->set_custom_minimum_size(Size2(200, 0));
  765. settings_dialog->add_child(settings_vbc);
  766. settings_pick_distance = memnew(SpinBox);
  767. settings_pick_distance->set_max(10000.0f);
  768. settings_pick_distance->set_min(500.0f);
  769. settings_pick_distance->set_step(1.0f);
  770. settings_pick_distance->set_value(EDITOR_DEF("editors/grid_map/pick_distance", 5000.0));
  771. settings_vbc->add_margin_child(TTR("Pick Distance:"), settings_pick_distance);
  772. clip_mode = CLIP_DISABLED;
  773. options->get_popup()->connect("id_pressed", this, "_menu_option");
  774. HBoxContainer *hb = memnew(HBoxContainer);
  775. add_child(hb);
  776. hb->set_h_size_flags(SIZE_EXPAND_FILL);
  777. mode_thumbnail = memnew(ToolButton);
  778. mode_thumbnail->set_toggle_mode(true);
  779. mode_thumbnail->set_pressed(true);
  780. mode_thumbnail->set_icon(p_editor->get_gui_base()->get_icon("FileThumbnail", "EditorIcons"));
  781. hb->add_child(mode_thumbnail);
  782. mode_thumbnail->connect("pressed", this, "_set_display_mode", varray(DISPLAY_THUMBNAIL));
  783. mode_list = memnew(ToolButton);
  784. mode_list->set_toggle_mode(true);
  785. mode_list->set_pressed(false);
  786. mode_list->set_icon(p_editor->get_gui_base()->get_icon("FileList", "EditorIcons"));
  787. hb->add_child(mode_list);
  788. mode_list->connect("pressed", this, "_set_display_mode", varray(DISPLAY_LIST));
  789. EDITOR_DEF("editors/grid_map/preview_size", 64);
  790. display_mode = DISPLAY_THUMBNAIL;
  791. theme_pallete = memnew(ItemList);
  792. add_child(theme_pallete);
  793. theme_pallete->set_v_size_flags(SIZE_EXPAND_FILL);
  794. spatial_editor_hb->add_child(memnew(VSeparator));
  795. Label *fl = memnew(Label);
  796. fl->set_text(" Floor: ");
  797. spatial_editor_hb->add_child(fl);
  798. floor = memnew(SpinBox);
  799. floor->set_min(-32767);
  800. floor->set_max(32767);
  801. floor->set_step(1);
  802. floor->get_line_edit()->add_constant_override("minimum_spaces", 16);
  803. spatial_editor_hb->add_child(floor);
  804. floor->connect("value_changed", this, "_floor_changed");
  805. edit_axis = Vector3::AXIS_Y;
  806. edit_floor[0] = -1;
  807. edit_floor[1] = -1;
  808. edit_floor[2] = -1;
  809. cursor_visible = false;
  810. selected_pallete = -1;
  811. lock_view = false;
  812. cursor_rot = 0;
  813. last_mouseover = Vector3(-1, -1, -1);
  814. selection_mesh = VisualServer::get_singleton()->mesh_create();
  815. duplicate_mesh = VisualServer::get_singleton()->mesh_create();
  816. {
  817. //selection mesh create
  818. PoolVector<Vector3> lines;
  819. PoolVector<Vector3> triangles;
  820. for (int i = 0; i < 6; i++) {
  821. Vector3 face_points[4];
  822. for (int j = 0; j < 4; j++) {
  823. float v[3];
  824. v[0] = 1.0;
  825. v[1] = 1 - 2 * ((j >> 1) & 1);
  826. v[2] = v[1] * (1 - 2 * (j & 1));
  827. for (int k = 0; k < 3; k++) {
  828. if (i < 3)
  829. face_points[j][(i + k) % 3] = v[k] * (i >= 3 ? -1 : 1);
  830. else
  831. face_points[3 - j][(i + k) % 3] = v[k] * (i >= 3 ? -1 : 1);
  832. }
  833. }
  834. triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5));
  835. triangles.push_back(face_points[1] * 0.5 + Vector3(0.5, 0.5, 0.5));
  836. triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5));
  837. triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5));
  838. triangles.push_back(face_points[3] * 0.5 + Vector3(0.5, 0.5, 0.5));
  839. triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5));
  840. }
  841. for (int i = 0; i < 12; i++) {
  842. Rect3 base(Vector3(0, 0, 0), Vector3(1, 1, 1));
  843. Vector3 a, b;
  844. base.get_edge(i, a, b);
  845. lines.push_back(a);
  846. lines.push_back(b);
  847. }
  848. Array d;
  849. d.resize(VS::ARRAY_MAX);
  850. inner_mat.instance();
  851. inner_mat->set_albedo(Color(0.7, 0.7, 1.0, 0.3));
  852. inner_mat->set_flag(SpatialMaterial::FLAG_ONTOP, true);
  853. inner_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  854. inner_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  855. d[VS::ARRAY_VERTEX] = triangles;
  856. VisualServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, VS::PRIMITIVE_TRIANGLES, d);
  857. VisualServer::get_singleton()->mesh_surface_set_material(selection_mesh, 0, inner_mat->get_rid());
  858. outer_mat.instance();
  859. outer_mat->set_albedo(Color(0.7, 0.7, 1.0, 0.3));
  860. outer_mat->set_flag(SpatialMaterial::FLAG_ONTOP, true);
  861. outer_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  862. outer_mat->set_line_width(3.0);
  863. outer_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  864. d[VS::ARRAY_VERTEX] = lines;
  865. VisualServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, VS::PRIMITIVE_LINES, d);
  866. VisualServer::get_singleton()->mesh_surface_set_material(selection_mesh, 1, outer_mat->get_rid());
  867. d[VS::ARRAY_VERTEX] = triangles;
  868. VisualServer::get_singleton()->mesh_add_surface_from_arrays(duplicate_mesh, VS::PRIMITIVE_TRIANGLES, d);
  869. VisualServer::get_singleton()->mesh_surface_set_material(duplicate_mesh, 0, inner_mat->get_rid());
  870. d[VS::ARRAY_VERTEX] = lines;
  871. VisualServer::get_singleton()->mesh_add_surface_from_arrays(duplicate_mesh, VS::PRIMITIVE_LINES, d);
  872. VisualServer::get_singleton()->mesh_surface_set_material(duplicate_mesh, 1, outer_mat->get_rid());
  873. }
  874. selection.active = false;
  875. updating = false;
  876. }
  877. GridMapEditor::~GridMapEditor() {
  878. for (int i = 0; i < 3; i++) {
  879. if (grid[i].is_valid())
  880. VisualServer::get_singleton()->free(grid[i]);
  881. if (grid_instance[i].is_valid())
  882. VisualServer::get_singleton()->free(grid_instance[i]);
  883. if (cursor_instance.is_valid())
  884. VisualServer::get_singleton()->free(cursor_instance);
  885. }
  886. VisualServer::get_singleton()->free(selection_mesh);
  887. if (selection_instance.is_valid())
  888. VisualServer::get_singleton()->free(selection_instance);
  889. VisualServer::get_singleton()->free(duplicate_mesh);
  890. if (duplicate_instance.is_valid())
  891. VisualServer::get_singleton()->free(duplicate_instance);
  892. }
  893. void GridMapEditorPlugin::edit(Object *p_object) {
  894. gridmap_editor->edit(Object::cast_to<GridMap>(p_object));
  895. }
  896. bool GridMapEditorPlugin::handles(Object *p_object) const {
  897. return p_object->is_class("GridMap");
  898. }
  899. void GridMapEditorPlugin::make_visible(bool p_visible) {
  900. if (p_visible) {
  901. gridmap_editor->show();
  902. gridmap_editor->spatial_editor_hb->show();
  903. gridmap_editor->set_process(true);
  904. } else {
  905. gridmap_editor->spatial_editor_hb->hide();
  906. gridmap_editor->hide();
  907. gridmap_editor->edit(NULL);
  908. gridmap_editor->set_process(false);
  909. }
  910. }
  911. GridMapEditorPlugin::GridMapEditorPlugin(EditorNode *p_node) {
  912. editor = p_node;
  913. gridmap_editor = memnew(GridMapEditor(editor));
  914. SpatialEditor::get_singleton()->get_palette_split()->add_child(gridmap_editor);
  915. SpatialEditor::get_singleton()->get_palette_split()->move_child(gridmap_editor, 0);
  916. gridmap_editor->hide();
  917. }
  918. GridMapEditorPlugin::~GridMapEditorPlugin() {
  919. }