grid_map_editor_plugin.cpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. Matrix3 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. Matrix3 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. Matrix3 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. Matrix3 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. Matrix3 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. Matrix3 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_MAKE_AREA:
  156. case MENU_OPTION_SELECTION_MAKE_EXTERIOR_CONNECTOR: {
  157. if (!selection.active)
  158. break;
  159. int area = node->get_unused_area_id();
  160. Error err = node->create_area(area, AABB(selection.begin, selection.end - selection.begin + Vector3(1, 1, 1)));
  161. if (err != OK) {
  162. }
  163. if (p_option == MENU_OPTION_SELECTION_MAKE_EXTERIOR_CONNECTOR) {
  164. node->area_set_exterior_portal(area, true);
  165. }
  166. _update_areas_display();
  167. update_areas();
  168. } break;
  169. case MENU_OPTION_REMOVE_AREA: {
  170. if (selected_area < 1)
  171. return;
  172. node->erase_area(selected_area);
  173. _update_areas_display();
  174. update_areas();
  175. } break;
  176. case MENU_OPTION_SELECTION_DUPLICATE:
  177. if (!(selection.active && input_action == INPUT_NONE))
  178. return;
  179. if (last_mouseover == Vector3(-1, -1, -1)) //nono mouseovering anythin
  180. break;
  181. input_action = INPUT_DUPLICATE;
  182. selection.click = last_mouseover;
  183. selection.current = last_mouseover;
  184. selection.duplicate_rot = 0;
  185. _update_duplicate_indicator();
  186. break;
  187. case MENU_OPTION_SELECTION_CLEAR: {
  188. if (!selection.active)
  189. return;
  190. _delete_selection();
  191. } break;
  192. case MENU_OPTION_GRIDMAP_SETTINGS: {
  193. settings_dialog->popup_centered(settings_vbc->get_combined_minimum_size() + Size2(50, 50));
  194. } break;
  195. }
  196. }
  197. void GridMapEditor::_update_cursor_transform() {
  198. cursor_transform = Transform();
  199. cursor_transform.origin = cursor_origin;
  200. cursor_transform.basis.set_orthogonal_index(cursor_rot);
  201. cursor_transform = node->get_transform() * cursor_transform;
  202. if (cursor_instance.is_valid()) {
  203. VisualServer::get_singleton()->instance_set_transform(cursor_instance, cursor_transform);
  204. VisualServer::get_singleton()->instance_geometry_set_flag(cursor_instance, VS::INSTANCE_FLAG_VISIBLE, cursor_visible);
  205. }
  206. }
  207. void GridMapEditor::_update_selection_transform() {
  208. if (!selection.active) {
  209. Transform xf;
  210. xf.basis.set_zero();
  211. VisualServer::get_singleton()->instance_set_transform(selection_instance, xf);
  212. return;
  213. }
  214. Transform xf;
  215. xf.scale(Vector3(1, 1, 1) * (Vector3(1, 1, 1) + (selection.end - selection.begin)) * node->get_cell_size());
  216. xf.origin = selection.begin * node->get_cell_size();
  217. VisualServer::get_singleton()->instance_set_transform(selection_instance, node->get_global_transform() * xf);
  218. }
  219. void GridMapEditor::_validate_selection() {
  220. if (!selection.active)
  221. return;
  222. selection.begin = selection.click;
  223. selection.end = selection.current;
  224. if (selection.begin.x > selection.end.x)
  225. SWAP(selection.begin.x, selection.end.x);
  226. if (selection.begin.y > selection.end.y)
  227. SWAP(selection.begin.y, selection.end.y);
  228. if (selection.begin.z > selection.end.z)
  229. SWAP(selection.begin.z, selection.end.z);
  230. _update_selection_transform();
  231. }
  232. bool GridMapEditor::do_input_action(Camera *p_camera, const Point2 &p_point, bool p_click) {
  233. if (!spatial_editor)
  234. return false;
  235. if (selected_pallete < 0 && input_action != INPUT_COPY && input_action != INPUT_SELECT && input_action != INPUT_DUPLICATE)
  236. return false;
  237. Ref<MeshLibrary> theme = node->get_theme();
  238. if (theme.is_null())
  239. return false;
  240. if (input_action != INPUT_COPY && input_action != INPUT_SELECT && input_action != INPUT_DUPLICATE && !theme->has_item(selected_pallete))
  241. return false;
  242. Camera *camera = p_camera;
  243. Vector3 from = camera->project_ray_origin(p_point);
  244. Vector3 normal = camera->project_ray_normal(p_point);
  245. Transform local_xform = node->get_global_transform().affine_inverse();
  246. Vector<Plane> planes = camera->get_frustum();
  247. from = local_xform.xform(from);
  248. normal = local_xform.basis.xform(normal).normalized();
  249. Plane p;
  250. p.normal[edit_axis] = 1.0;
  251. p.d = edit_floor[edit_axis] * node->get_cell_size();
  252. Vector3 inters;
  253. if (!p.intersects_segment(from, from + normal * settings_pick_distance->get_val(), &inters))
  254. return false;
  255. //make sure the intersection is inside the frustum planes, to avoid
  256. //painting on invisible regions
  257. for (int i = 0; i < planes.size(); i++) {
  258. Plane fp = local_xform.xform(planes[i]);
  259. if (fp.is_point_over(inters))
  260. return false;
  261. }
  262. int cell[3];
  263. float cell_size[3] = { node->get_cell_size(), node->get_cell_size(), node->get_cell_size() };
  264. last_mouseover = Vector3(-1, -1, -1);
  265. for (int i = 0; i < 3; i++) {
  266. if (i == edit_axis)
  267. cell[i] = edit_floor[i];
  268. else {
  269. cell[i] = inters[i] / node->get_cell_size();
  270. if (inters[i] < 0)
  271. cell[i] -= 1; //compensate negative
  272. grid_ofs[i] = cell[i] * cell_size[i];
  273. }
  274. /*if (cell[i]<0 || cell[i]>=grid_size[i]) {
  275. cursor_visible=false;
  276. _update_cursor_transform();
  277. return false;
  278. }*/
  279. }
  280. last_mouseover = Vector3(cell[0], cell[1], cell[2]);
  281. VS::get_singleton()->instance_set_transform(grid_instance[edit_axis], Transform(Matrix3(), grid_ofs));
  282. if (cursor_instance.is_valid()) {
  283. 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();
  284. cursor_visible = true;
  285. _update_cursor_transform();
  286. }
  287. if (input_action == INPUT_DUPLICATE) {
  288. selection.current = Vector3(cell[0], cell[1], cell[2]);
  289. _update_duplicate_indicator();
  290. } else if (input_action == INPUT_SELECT) {
  291. selection.current = Vector3(cell[0], cell[1], cell[2]);
  292. if (p_click)
  293. selection.click = selection.current;
  294. selection.active = true;
  295. _validate_selection();
  296. return true;
  297. } else if (input_action == INPUT_COPY) {
  298. int item = node->get_cell_item(cell[0], cell[1], cell[2]);
  299. if (item >= 0) {
  300. selected_pallete = item;
  301. theme_pallete->set_current(item);
  302. update_pallete();
  303. _update_cursor_instance();
  304. }
  305. return true;
  306. }
  307. if (input_action == INPUT_PAINT) {
  308. SetItem si;
  309. si.pos = Vector3(cell[0], cell[1], cell[2]);
  310. si.new_value = selected_pallete;
  311. si.new_orientation = cursor_rot;
  312. si.old_value = node->get_cell_item(cell[0], cell[1], cell[2]);
  313. si.old_orientation = node->get_cell_item_orientation(cell[0], cell[1], cell[2]);
  314. set_items.push_back(si);
  315. node->set_cell_item(cell[0], cell[1], cell[2], selected_pallete, cursor_rot);
  316. return true;
  317. } else if (input_action == INPUT_ERASE) {
  318. SetItem si;
  319. si.pos = Vector3(cell[0], cell[1], cell[2]);
  320. si.new_value = -1;
  321. si.new_orientation = 0;
  322. si.old_value = node->get_cell_item(cell[0], cell[1], cell[2]);
  323. si.old_orientation = node->get_cell_item_orientation(cell[0], cell[1], cell[2]);
  324. set_items.push_back(si);
  325. node->set_cell_item(cell[0], cell[1], cell[2], -1);
  326. return true;
  327. }
  328. return false;
  329. }
  330. void GridMapEditor::_delete_selection() {
  331. if (!selection.active)
  332. return;
  333. undo_redo->create_action("GridMap Delete Selection");
  334. for (int i = selection.begin.x; i <= selection.end.x; i++) {
  335. for (int j = selection.begin.y; j <= selection.end.y; j++) {
  336. for (int k = selection.begin.z; k <= selection.end.z; k++) {
  337. undo_redo->add_do_method(node, "set_cell_item", i, j, k, GridMap::INVALID_CELL_ITEM);
  338. 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));
  339. }
  340. }
  341. }
  342. undo_redo->commit_action();
  343. selection.active = false;
  344. _validate_selection();
  345. }
  346. void GridMapEditor::_update_duplicate_indicator() {
  347. if (!selection.active || input_action != INPUT_DUPLICATE) {
  348. Transform xf;
  349. xf.basis.set_zero();
  350. VisualServer::get_singleton()->instance_set_transform(duplicate_instance, xf);
  351. return;
  352. }
  353. Transform xf;
  354. xf.scale(Vector3(1, 1, 1) * (Vector3(1, 1, 1) + (selection.end - selection.begin)) * node->get_cell_size());
  355. xf.origin = (selection.begin + (selection.current - selection.click)) * node->get_cell_size();
  356. Matrix3 rot;
  357. rot.set_orthogonal_index(selection.duplicate_rot);
  358. xf.basis = rot * xf.basis;
  359. VisualServer::get_singleton()->instance_set_transform(duplicate_instance, node->get_global_transform() * xf);
  360. }
  361. struct __Item {
  362. Vector3 pos;
  363. int rot;
  364. int item;
  365. };
  366. void GridMapEditor::_duplicate_paste() {
  367. if (!selection.active)
  368. return;
  369. int idx = options->get_popup()->get_item_index(MENU_OPTION_DUPLICATE_SELECTS);
  370. bool reselect = options->get_popup()->is_item_checked(idx);
  371. List<__Item> items;
  372. Matrix3 rot;
  373. rot.set_orthogonal_index(selection.duplicate_rot);
  374. for (int i = selection.begin.x; i <= selection.end.x; i++) {
  375. for (int j = selection.begin.y; j <= selection.end.y; j++) {
  376. for (int k = selection.begin.z; k <= selection.end.z; k++) {
  377. int itm = node->get_cell_item(i, j, k);
  378. if (itm == GridMap::INVALID_CELL_ITEM)
  379. continue;
  380. int orientation = node->get_cell_item_orientation(i, j, k);
  381. __Item item;
  382. Vector3 rel = Vector3(i, j, k) - selection.begin;
  383. rel = rot.xform(rel);
  384. Matrix3 orm;
  385. orm.set_orthogonal_index(orientation);
  386. orm = rot * orm;
  387. item.pos = selection.begin + rel;
  388. item.item = itm;
  389. item.rot = orm.get_orthogonal_index();
  390. items.push_back(item);
  391. }
  392. }
  393. }
  394. Vector3 ofs = selection.current - selection.click;
  395. if (items.size()) {
  396. undo_redo->create_action("GridMap Duplicate Selection");
  397. for (List<__Item>::Element *E = items.front(); E; E = E->next()) {
  398. __Item &it = E->get();
  399. Vector3 pos = it.pos + ofs;
  400. undo_redo->add_do_method(node, "set_cell_item", pos.x, pos.y, pos.z, it.item, it.rot);
  401. 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));
  402. }
  403. undo_redo->commit_action();
  404. }
  405. if (reselect) {
  406. selection.begin += ofs;
  407. selection.end += ofs;
  408. selection.click = selection.begin;
  409. selection.current = selection.end;
  410. _validate_selection();
  411. }
  412. }
  413. bool GridMapEditor::forward_spatial_input_event(Camera *p_camera, const InputEvent &p_event) {
  414. if (!node) {
  415. return false;
  416. }
  417. if (edit_mode->get_selected() == 0) { // regular click
  418. switch (p_event.type) {
  419. case InputEvent::MOUSE_BUTTON: {
  420. if (p_event.mouse_button.button_index == BUTTON_WHEEL_UP && (p_event.mouse_button.mod.command || p_event.mouse_button.mod.shift)) {
  421. if (p_event.mouse_button.pressed)
  422. floor->set_val(floor->get_val() + p_event.mouse_button.factor);
  423. return true; //eaten
  424. } else if (p_event.mouse_button.button_index == BUTTON_WHEEL_DOWN && (p_event.mouse_button.mod.command || p_event.mouse_button.mod.shift)) {
  425. if (p_event.mouse_button.pressed)
  426. floor->set_val(floor->get_val() - p_event.mouse_button.factor);
  427. return true;
  428. }
  429. if (p_event.mouse_button.pressed) {
  430. if (p_event.mouse_button.button_index == BUTTON_LEFT) {
  431. if (input_action == INPUT_DUPLICATE) {
  432. //paste
  433. _duplicate_paste();
  434. input_action = INPUT_NONE;
  435. _update_duplicate_indicator();
  436. } else if (p_event.mouse_button.mod.shift) {
  437. input_action = INPUT_SELECT;
  438. } else if (p_event.mouse_button.mod.command)
  439. input_action = INPUT_COPY;
  440. else {
  441. input_action = INPUT_PAINT;
  442. set_items.clear();
  443. }
  444. } else if (p_event.mouse_button.button_index == BUTTON_RIGHT)
  445. if (input_action == INPUT_DUPLICATE) {
  446. input_action = INPUT_NONE;
  447. _update_duplicate_indicator();
  448. } else {
  449. input_action = INPUT_ERASE;
  450. set_items.clear();
  451. }
  452. else
  453. return false;
  454. return do_input_action(p_camera, Point2(p_event.mouse_button.x, p_event.mouse_button.y), true);
  455. } else {
  456. if (
  457. (p_event.mouse_button.button_index == BUTTON_RIGHT && input_action == INPUT_ERASE) ||
  458. (p_event.mouse_button.button_index == BUTTON_LEFT && input_action == INPUT_PAINT)) {
  459. if (set_items.size()) {
  460. undo_redo->create_action("GridMap Paint");
  461. for (List<SetItem>::Element *E = set_items.front(); E; E = E->next()) {
  462. const SetItem &si = E->get();
  463. undo_redo->add_do_method(node, "set_cell_item", si.pos.x, si.pos.y, si.pos.z, si.new_value, si.new_orientation);
  464. }
  465. for (List<SetItem>::Element *E = set_items.back(); E; E = E->prev()) {
  466. const SetItem &si = E->get();
  467. undo_redo->add_undo_method(node, "set_cell_item", si.pos.x, si.pos.y, si.pos.z, si.old_value, si.old_orientation);
  468. }
  469. undo_redo->commit_action();
  470. }
  471. set_items.clear();
  472. input_action = INPUT_NONE;
  473. return true;
  474. }
  475. if (p_event.mouse_button.button_index == BUTTON_LEFT && input_action != INPUT_NONE) {
  476. set_items.clear();
  477. input_action = INPUT_NONE;
  478. return true;
  479. }
  480. if (p_event.mouse_button.button_index == BUTTON_RIGHT && (input_action == INPUT_ERASE || input_action == INPUT_DUPLICATE)) {
  481. input_action = INPUT_NONE;
  482. return true;
  483. }
  484. }
  485. } break;
  486. case InputEvent::MOUSE_MOTION: {
  487. return do_input_action(p_camera, Point2(p_event.mouse_motion.x, p_event.mouse_motion.y), false);
  488. } break;
  489. }
  490. } else if (edit_mode->get_selected() == 1) {
  491. //area mode, select an area
  492. switch (p_event.type) {
  493. case InputEvent::MOUSE_BUTTON: {
  494. if (p_event.mouse_button.button_index == BUTTON_LEFT && p_event.mouse_button.pressed) {
  495. Point2 point = Point2(p_event.mouse_motion.x, p_event.mouse_motion.y);
  496. Camera *camera = p_camera;
  497. Vector3 from = camera->project_ray_origin(point);
  498. Vector3 normal = camera->project_ray_normal(point);
  499. Transform local_xform = node->get_global_transform().affine_inverse();
  500. from = local_xform.xform(from);
  501. normal = local_xform.basis.xform(normal).normalized();
  502. List<int> areas;
  503. node->get_area_list(&areas);
  504. float min_d = 1e10;
  505. int min_area = -1;
  506. for (List<int>::Element *E = areas.front(); E; E = E->next()) {
  507. int area = E->get();
  508. AABB aabb = node->area_get_bounds(area);
  509. aabb.pos *= node->get_cell_size();
  510. aabb.size *= node->get_cell_size();
  511. Vector3 rclip, rnormal;
  512. if (!aabb.intersects_segment(from, from + normal * 10000, &rclip, &rnormal))
  513. continue;
  514. float d = normal.dot(rclip);
  515. if (d < min_d) {
  516. min_d = d;
  517. min_area = area;
  518. }
  519. }
  520. selected_area = min_area;
  521. update_areas();
  522. }
  523. } break;
  524. }
  525. }
  526. return false;
  527. }
  528. struct _CGMEItemSort {
  529. String name;
  530. int id;
  531. _FORCE_INLINE_ bool operator<(const _CGMEItemSort &r_it) const { return name < r_it.name; }
  532. };
  533. void GridMapEditor::_set_display_mode(int p_mode) {
  534. if (display_mode == p_mode) {
  535. return;
  536. }
  537. if (p_mode == DISPLAY_LIST) {
  538. mode_list->set_pressed(true);
  539. mode_thumbnail->set_pressed(false);
  540. } else if (p_mode == DISPLAY_THUMBNAIL) {
  541. mode_list->set_pressed(false);
  542. mode_thumbnail->set_pressed(true);
  543. }
  544. display_mode = p_mode;
  545. update_pallete();
  546. }
  547. void GridMapEditor::update_pallete() {
  548. int selected = theme_pallete->get_current();
  549. theme_pallete->clear();
  550. if (display_mode == DISPLAY_THUMBNAIL) {
  551. theme_pallete->set_max_columns(0);
  552. theme_pallete->set_icon_mode(ItemList::ICON_MODE_TOP);
  553. } else if (display_mode == DISPLAY_LIST) {
  554. theme_pallete->set_max_columns(1);
  555. theme_pallete->set_icon_mode(ItemList::ICON_MODE_LEFT);
  556. }
  557. float min_size = EDITOR_DEF("grid_map/preview_size", 64);
  558. theme_pallete->set_fixed_icon_size(Size2(min_size, min_size));
  559. theme_pallete->set_fixed_column_width(min_size * 3 / 2);
  560. theme_pallete->set_max_text_lines(2);
  561. Ref<MeshLibrary> theme = node->get_theme();
  562. if (theme.is_null()) {
  563. last_theme = NULL;
  564. return;
  565. }
  566. Vector<int> ids;
  567. ids = theme->get_item_list();
  568. List<_CGMEItemSort> il;
  569. for (int i = 0; i < ids.size(); i++) {
  570. _CGMEItemSort is;
  571. is.id = ids[i];
  572. is.name = theme->get_item_name(ids[i]);
  573. il.push_back(is);
  574. }
  575. il.sort();
  576. int item = 0;
  577. for (List<_CGMEItemSort>::Element *E = il.front(); E; E = E->next()) {
  578. int id = E->get().id;
  579. theme_pallete->add_item("");
  580. String name = theme->get_item_name(id);
  581. Ref<Texture> preview = theme->get_item_preview(id);
  582. if (!preview.is_null()) {
  583. theme_pallete->set_item_icon(item, preview);
  584. theme_pallete->set_item_tooltip(item, name);
  585. }
  586. if (name != "") {
  587. theme_pallete->set_item_text(item, name);
  588. }
  589. theme_pallete->set_item_metadata(item, id);
  590. item++;
  591. }
  592. if (selected != -1) {
  593. theme_pallete->select(selected);
  594. }
  595. last_theme = theme.operator->();
  596. }
  597. void GridMapEditor::_area_renamed() {
  598. TreeItem *it = area_list->get_selected();
  599. int area = it->get_metadata(0);
  600. if (area < 1)
  601. return;
  602. node->area_set_name(area, it->get_text(0));
  603. }
  604. void GridMapEditor::_area_selected() {
  605. TreeItem *it = area_list->get_selected();
  606. int area = it->get_metadata(0);
  607. if (area < 1)
  608. return;
  609. selected_area = area;
  610. }
  611. void GridMapEditor::update_areas() {
  612. area_list->clear();
  613. List<int> areas;
  614. node->get_area_list(&areas);
  615. TreeItem *root = area_list->create_item(NULL);
  616. area_list->set_hide_root(true);
  617. TreeItem *selected = NULL;
  618. for (List<int>::Element *E = areas.front(); E; E = E->next()) {
  619. int area = E->get();
  620. TreeItem *ti = area_list->create_item(root);
  621. String name = node->area_get_name(area);
  622. ti->set_metadata(0, area);
  623. ti->set_text(0, name);
  624. ti->set_editable(0, true);
  625. if (area == selected_area)
  626. selected = ti;
  627. }
  628. if (selected)
  629. selected->select(0);
  630. }
  631. void GridMapEditor::edit(GridMap *p_gridmap) {
  632. node = p_gridmap;
  633. VS *vs = VS::get_singleton();
  634. last_mouseover = Vector3(-1, -1, -1);
  635. input_action = INPUT_NONE;
  636. selection.active = false;
  637. _update_selection_transform();
  638. _update_duplicate_indicator();
  639. spatial_editor = editor->get_editor_plugin_screen()->cast_to<SpatialEditorPlugin>();
  640. if (!node) {
  641. set_process(false);
  642. for (int i = 0; i < 3; i++) {
  643. VisualServer::get_singleton()->instance_geometry_set_flag(grid_instance[i], VS::INSTANCE_FLAG_VISIBLE, false);
  644. }
  645. VisualServer::get_singleton()->instance_geometry_set_flag(cursor_instance, VS::INSTANCE_FLAG_VISIBLE, false);
  646. _clear_areas();
  647. return;
  648. }
  649. update_pallete();
  650. update_areas();
  651. set_process(true);
  652. Vector3 edited_floor = p_gridmap->get_meta("_editor_floor_");
  653. clip_mode = p_gridmap->has_meta("_editor_clip_") ? ClipMode(p_gridmap->get_meta("_editor_clip_").operator int()) : CLIP_DISABLED;
  654. for (int i = 0; i < 3; i++) {
  655. if (vs->mesh_get_surface_count(grid[i]) > 0)
  656. vs->mesh_remove_surface(grid[i], 0);
  657. edit_floor[i] = edited_floor[i];
  658. }
  659. {
  660. //update grids
  661. indicator_mat = VisualServer::get_singleton()->fixed_material_create();
  662. VisualServer::get_singleton()->material_set_flag(indicator_mat, VisualServer::MATERIAL_FLAG_UNSHADED, true);
  663. VisualServer::get_singleton()->material_set_flag(indicator_mat, VisualServer::MATERIAL_FLAG_ONTOP, false);
  664. VisualServer::get_singleton()->fixed_material_set_param(indicator_mat, VisualServer::FIXED_MATERIAL_PARAM_DIFFUSE, Color(0.8, 0.5, 0.1));
  665. VisualServer::get_singleton()->fixed_material_set_flag(indicator_mat, VisualServer::FIXED_MATERIAL_FLAG_USE_ALPHA, true);
  666. VisualServer::get_singleton()->fixed_material_set_flag(indicator_mat, VisualServer::FIXED_MATERIAL_FLAG_USE_COLOR_ARRAY, true);
  667. Vector<Vector3> grid_points[3];
  668. Vector<Color> grid_colors[3];
  669. float cell_size[3] = { p_gridmap->get_cell_size(), p_gridmap->get_cell_size(), p_gridmap->get_cell_size() };
  670. for (int i = 0; i < 3; i++) {
  671. Vector3 axis;
  672. axis[i] = 1;
  673. Vector3 axis_n1;
  674. axis_n1[(i + 1) % 3] = cell_size[(i + 1) % 3];
  675. Vector3 axis_n2;
  676. axis_n2[(i + 2) % 3] = cell_size[(i + 2) % 3];
  677. for (int j = -GRID_CURSOR_SIZE; j <= GRID_CURSOR_SIZE; j++) {
  678. for (int k = -GRID_CURSOR_SIZE; k <= GRID_CURSOR_SIZE; k++) {
  679. Vector3 p = axis_n1 * j + axis_n2 * k;
  680. float trans = Math::pow(MAX(0, 1.0 - (Vector2(j, k).length() / GRID_CURSOR_SIZE)), 2);
  681. Vector3 pj = axis_n1 * (j + 1) + axis_n2 * k;
  682. float transj = Math::pow(MAX(0, 1.0 - (Vector2(j + 1, k).length() / GRID_CURSOR_SIZE)), 2);
  683. Vector3 pk = axis_n1 * j + axis_n2 * (k + 1);
  684. float transk = Math::pow(MAX(0, 1.0 - (Vector2(j, k + 1).length() / GRID_CURSOR_SIZE)), 2);
  685. grid_points[i].push_back(p);
  686. grid_points[i].push_back(pk);
  687. grid_colors[i].push_back(Color(1, 1, 1, trans));
  688. grid_colors[i].push_back(Color(1, 1, 1, transk));
  689. grid_points[i].push_back(p);
  690. grid_points[i].push_back(pj);
  691. grid_colors[i].push_back(Color(1, 1, 1, trans));
  692. grid_colors[i].push_back(Color(1, 1, 1, transj));
  693. }
  694. }
  695. Array d;
  696. d.resize(VS::ARRAY_MAX);
  697. d[VS::ARRAY_VERTEX] = grid_points[i];
  698. d[VS::ARRAY_COLOR] = grid_colors[i];
  699. VisualServer::get_singleton()->mesh_add_surface(grid[i], VisualServer::PRIMITIVE_LINES, d);
  700. VisualServer::get_singleton()->mesh_surface_set_material(grid[i], 0, indicator_mat);
  701. }
  702. }
  703. update_grid();
  704. _update_clip();
  705. _update_areas_display();
  706. }
  707. void GridMapEditor::_update_clip() {
  708. node->set_meta("_editor_clip_", clip_mode);
  709. if (clip_mode == CLIP_DISABLED)
  710. node->set_clip(false);
  711. else
  712. node->set_clip(true, clip_mode == CLIP_ABOVE, edit_floor[edit_axis], edit_axis);
  713. }
  714. void GridMapEditor::update_grid() {
  715. grid_xform.origin.x -= 1; //force update in hackish way.. what do i care
  716. //VS *vs = VS::get_singleton();
  717. grid_ofs[edit_axis] = edit_floor[edit_axis] * node->get_cell_size();
  718. edit_grid_xform.origin = grid_ofs;
  719. edit_grid_xform.basis = Matrix3();
  720. for (int i = 0; i < 3; i++) {
  721. VisualServer::get_singleton()->instance_geometry_set_flag(grid_instance[i], VS::INSTANCE_FLAG_VISIBLE, i == edit_axis);
  722. }
  723. updating = true;
  724. floor->set_val(edit_floor[edit_axis]);
  725. updating = false;
  726. }
  727. void GridMapEditor::_notification(int p_what) {
  728. if (p_what == NOTIFICATION_ENTER_TREE) {
  729. theme_pallete->connect("item_selected", this, "_item_selected_cbk");
  730. edit_mode->connect("item_selected", this, "_edit_mode_changed");
  731. area_list->connect("item_edited", this, "_area_renamed");
  732. area_list->connect("item_selected", this, "_area_selected");
  733. for (int i = 0; i < 3; i++) {
  734. grid[i] = VS::get_singleton()->mesh_create();
  735. grid_instance[i] = VS::get_singleton()->instance_create2(grid[i], get_tree()->get_root()->get_world()->get_scenario());
  736. }
  737. selection_instance = VisualServer::get_singleton()->instance_create2(selection_mesh, get_tree()->get_root()->get_world()->get_scenario());
  738. duplicate_instance = VisualServer::get_singleton()->instance_create2(duplicate_mesh, get_tree()->get_root()->get_world()->get_scenario());
  739. _update_selection_transform();
  740. _update_duplicate_indicator();
  741. } else if (p_what == NOTIFICATION_EXIT_TREE) {
  742. for (int i = 0; i < 3; i++) {
  743. VS::get_singleton()->free(grid_instance[i]);
  744. VS::get_singleton()->free(grid[i]);
  745. grid_instance[i] = RID();
  746. grid[i] = RID();
  747. }
  748. VisualServer::get_singleton()->free(selection_instance);
  749. VisualServer::get_singleton()->free(duplicate_instance);
  750. selection_instance = RID();
  751. duplicate_instance = RID();
  752. } else if (p_what == NOTIFICATION_PROCESS) {
  753. Transform xf = node->get_global_transform();
  754. if (xf != grid_xform) {
  755. for (int i = 0; i < 3; i++) {
  756. VS::get_singleton()->instance_set_transform(grid_instance[i], xf * edit_grid_xform);
  757. }
  758. grid_xform = xf;
  759. }
  760. Ref<MeshLibrary> cgmt = node->get_theme();
  761. if (cgmt.operator->() != last_theme)
  762. update_pallete();
  763. if (lock_view) {
  764. EditorNode *editor = get_tree()->get_root()->get_child(0)->cast_to<EditorNode>();
  765. Plane p;
  766. p.normal[edit_axis] = 1.0;
  767. p.d = edit_floor[edit_axis] * node->get_cell_size();
  768. p = node->get_transform().xform(p); // plane to snap
  769. SpatialEditorPlugin *sep = editor->get_editor_plugin_screen()->cast_to<SpatialEditorPlugin>();
  770. if (sep)
  771. sep->snap_cursor_to_plane(p);
  772. //editor->get_editor_plugin_screen()->call("snap_cursor_to_plane",p);
  773. }
  774. }
  775. }
  776. void GridMapEditor::_update_cursor_instance() {
  777. if (!node) {
  778. return;
  779. }
  780. if (cursor_instance.is_valid())
  781. VisualServer::get_singleton()->free(cursor_instance);
  782. cursor_instance = RID();
  783. if (selected_pallete >= 0) {
  784. if (node && !node->get_theme().is_null()) {
  785. Ref<Mesh> mesh = node->get_theme()->get_item_mesh(selected_pallete);
  786. if (!mesh.is_null() && mesh->get_rid().is_valid()) {
  787. cursor_instance = VisualServer::get_singleton()->instance_create2(mesh->get_rid(), get_tree()->get_root()->get_world()->get_scenario());
  788. VisualServer::get_singleton()->instance_set_transform(cursor_instance, cursor_transform);
  789. }
  790. }
  791. }
  792. }
  793. void GridMapEditor::_item_selected_cbk(int idx) {
  794. selected_pallete = theme_pallete->get_item_metadata(idx);
  795. _update_cursor_instance();
  796. }
  797. void GridMapEditor::_clear_areas() {
  798. for (int i = 0; i < areas.size(); i++) {
  799. VisualServer::get_singleton()->free(areas[i].instance);
  800. VisualServer::get_singleton()->free(areas[i].mesh);
  801. }
  802. areas.clear();
  803. }
  804. void GridMapEditor::_update_areas_display() {
  805. if (!node) {
  806. return;
  807. }
  808. _clear_areas();
  809. List<int> areas;
  810. node->get_area_list(&areas);
  811. Transform global_xf = node->get_global_transform();
  812. for (List<int>::Element *E = areas.front(); E; E = E->next()) {
  813. int area = E->get();
  814. Color color;
  815. if (node->area_is_exterior_portal(area))
  816. color = Color(1, 1, 1, 0.2);
  817. else
  818. color.set_hsv(Math::fmod(area * 0.37, 1), Math::fmod(area * 0.75, 1), 1.0, 0.2);
  819. RID material = VisualServer::get_singleton()->fixed_material_create();
  820. VisualServer::get_singleton()->fixed_material_set_param(material, VS::FIXED_MATERIAL_PARAM_DIFFUSE, color);
  821. VisualServer::get_singleton()->fixed_material_set_param(material, VS::FIXED_MATERIAL_PARAM_EMISSION, 0.5);
  822. VisualServer::get_singleton()->fixed_material_set_flag(material, VisualServer::FIXED_MATERIAL_FLAG_USE_ALPHA, true);
  823. RID mesh = VisualServer::get_singleton()->mesh_create();
  824. DVector<Plane> planes;
  825. for (int i = 0; i < 3; i++) {
  826. Vector3 axis;
  827. axis[i] = 1.0;
  828. planes.push_back(Plane(axis, 1));
  829. planes.push_back(Plane(-axis, 0));
  830. }
  831. VisualServer::get_singleton()->mesh_add_surface_from_planes(mesh, planes);
  832. VisualServer::get_singleton()->mesh_surface_set_material(mesh, 0, material, true);
  833. AreaDisplay ad;
  834. ad.mesh = mesh;
  835. ad.instance = VisualServer::get_singleton()->instance_create2(mesh, node->get_world()->get_scenario());
  836. Transform xform;
  837. AABB aabb = node->area_get_bounds(area);
  838. xform.origin = aabb.pos * node->get_cell_size();
  839. xform.basis.scale(aabb.size * node->get_cell_size());
  840. VisualServer::get_singleton()->instance_set_transform(ad.instance, global_xf * xform);
  841. this->areas.push_back(ad);
  842. }
  843. }
  844. void GridMapEditor::_edit_mode_changed(int p_what) {
  845. if (p_what == 0) {
  846. theme_pallete->show();
  847. area_list->hide();
  848. } else {
  849. theme_pallete->hide();
  850. area_list->show();
  851. }
  852. }
  853. void GridMapEditor::_floor_changed(float p_value) {
  854. if (updating)
  855. return;
  856. edit_floor[edit_axis] = p_value;
  857. node->set_meta("_editor_floor_", Vector3(edit_floor[0], edit_floor[1], edit_floor[2]));
  858. update_grid();
  859. _update_clip();
  860. }
  861. void GridMapEditor::_bind_methods() {
  862. ObjectTypeDB::bind_method("_menu_option", &GridMapEditor::_menu_option);
  863. ObjectTypeDB::bind_method("_configure", &GridMapEditor::_configure);
  864. ObjectTypeDB::bind_method("_item_selected_cbk", &GridMapEditor::_item_selected_cbk);
  865. ObjectTypeDB::bind_method("_edit_mode_changed", &GridMapEditor::_edit_mode_changed);
  866. ObjectTypeDB::bind_method("_area_renamed", &GridMapEditor::_area_renamed);
  867. ObjectTypeDB::bind_method("_area_selected", &GridMapEditor::_area_selected);
  868. ObjectTypeDB::bind_method("_floor_changed", &GridMapEditor::_floor_changed);
  869. ObjectTypeDB::bind_method(_MD("_set_display_mode", "mode"), &GridMapEditor::_set_display_mode);
  870. }
  871. GridMapEditor::GridMapEditor(EditorNode *p_editor) {
  872. input_action = INPUT_NONE;
  873. editor = p_editor;
  874. undo_redo = p_editor->get_undo_redo();
  875. int mw = EDITOR_DEF("grid_map/palette_min_width", 230);
  876. Control *ec = memnew(Control);
  877. ec->set_custom_minimum_size(Size2(mw, 0));
  878. add_child(ec);
  879. spatial_editor_hb = memnew(HBoxContainer);
  880. SpatialEditor::get_singleton()->add_control_to_menu_panel(spatial_editor_hb);
  881. options = memnew(MenuButton);
  882. spatial_editor_hb->add_child(options);
  883. spatial_editor_hb->hide();
  884. options->set_text("Grid");
  885. options->get_popup()->add_check_item("Snap View", MENU_OPTION_LOCK_VIEW);
  886. options->get_popup()->add_separator();
  887. options->get_popup()->add_item("Prev Level (" + keycode_get_string(KEY_MASK_CMD) + "Down Wheel)", MENU_OPTION_PREV_LEVEL);
  888. options->get_popup()->add_item("Next Level (" + keycode_get_string(KEY_MASK_CMD) + "Up Wheel)", MENU_OPTION_NEXT_LEVEL);
  889. options->get_popup()->add_separator();
  890. options->get_popup()->add_check_item("Clip Disabled", MENU_OPTION_CLIP_DISABLED);
  891. options->get_popup()->set_item_checked(options->get_popup()->get_item_index(MENU_OPTION_CLIP_DISABLED), true);
  892. options->get_popup()->add_check_item("Clip Above", MENU_OPTION_CLIP_ABOVE);
  893. options->get_popup()->add_check_item("Clip Below", MENU_OPTION_CLIP_BELOW);
  894. options->get_popup()->add_separator();
  895. options->get_popup()->add_check_item("Edit X Axis", MENU_OPTION_X_AXIS, KEY_Z);
  896. options->get_popup()->add_check_item("Edit Y Axis", MENU_OPTION_Y_AXIS, KEY_X);
  897. options->get_popup()->add_check_item("Edit Z Axis", MENU_OPTION_Z_AXIS, KEY_C);
  898. options->get_popup()->set_item_checked(options->get_popup()->get_item_index(MENU_OPTION_Y_AXIS), true);
  899. options->get_popup()->add_separator();
  900. options->get_popup()->add_item("Cursor Rotate X", MENU_OPTION_CURSOR_ROTATE_X, KEY_A);
  901. options->get_popup()->add_item("Cursor Rotate Y", MENU_OPTION_CURSOR_ROTATE_Y, KEY_S);
  902. options->get_popup()->add_item("Cursor Rotate Z", MENU_OPTION_CURSOR_ROTATE_Z, KEY_D);
  903. options->get_popup()->add_item("Cursor Back Rotate X", MENU_OPTION_CURSOR_BACK_ROTATE_X, KEY_MASK_SHIFT + KEY_A);
  904. options->get_popup()->add_item("Cursor Back Rotate Y", MENU_OPTION_CURSOR_BACK_ROTATE_Y, KEY_MASK_SHIFT + KEY_S);
  905. options->get_popup()->add_item("Cursor Back Rotate Z", MENU_OPTION_CURSOR_BACK_ROTATE_Z, KEY_MASK_SHIFT + KEY_D);
  906. options->get_popup()->add_item("Cursor Clear Rotation", MENU_OPTION_CURSOR_CLEAR_ROTATION, KEY_W);
  907. options->get_popup()->add_separator();
  908. options->get_popup()->add_check_item("Duplicate Selects", MENU_OPTION_DUPLICATE_SELECTS);
  909. options->get_popup()->add_separator();
  910. options->get_popup()->add_item("Create Area", MENU_OPTION_SELECTION_MAKE_AREA, KEY_CONTROL + KEY_C);
  911. options->get_popup()->add_item("Create Exterior Connector", MENU_OPTION_SELECTION_MAKE_EXTERIOR_CONNECTOR);
  912. options->get_popup()->add_item("Erase Area", MENU_OPTION_REMOVE_AREA);
  913. options->get_popup()->add_separator();
  914. options->get_popup()->add_item("Selection -> Duplicate", MENU_OPTION_SELECTION_DUPLICATE, KEY_MASK_SHIFT + KEY_INSERT);
  915. options->get_popup()->add_item("Selection -> Clear", MENU_OPTION_SELECTION_CLEAR, KEY_MASK_SHIFT + KEY_DELETE);
  916. //options->get_popup()->add_separator();
  917. //options->get_popup()->add_item("Configure",MENU_OPTION_CONFIGURE);
  918. options->get_popup()->add_separator();
  919. options->get_popup()->add_item("Settings", MENU_OPTION_GRIDMAP_SETTINGS);
  920. settings_dialog = memnew(ConfirmationDialog);
  921. settings_dialog->set_title("GridMap Settings");
  922. add_child(settings_dialog);
  923. settings_vbc = memnew(VBoxContainer);
  924. settings_vbc->set_custom_minimum_size(Size2(200, 0));
  925. settings_dialog->add_child(settings_vbc);
  926. settings_dialog->set_child_rect(settings_vbc);
  927. settings_pick_distance = memnew(SpinBox);
  928. settings_pick_distance->set_max(10000.0f);
  929. settings_pick_distance->set_min(500.0f);
  930. settings_pick_distance->set_step(1.0f);
  931. settings_pick_distance->set_val(EDITOR_DEF("grid_map/pick_distance", 5000.0));
  932. settings_vbc->add_margin_child("Pick Distance:", settings_pick_distance);
  933. clip_mode = CLIP_DISABLED;
  934. options->get_popup()->connect("item_pressed", this, "_menu_option");
  935. HBoxContainer *hb = memnew(HBoxContainer);
  936. add_child(hb);
  937. hb->set_h_size_flags(SIZE_EXPAND_FILL);
  938. edit_mode = memnew(OptionButton);
  939. edit_mode->set_area_as_parent_rect();
  940. edit_mode->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_BEGIN, 24);
  941. edit_mode->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 14);
  942. edit_mode->add_item("Tiles");
  943. edit_mode->add_item("Areas");
  944. hb->add_child(edit_mode);
  945. edit_mode->set_h_size_flags(SIZE_EXPAND_FILL);
  946. mode_thumbnail = memnew(ToolButton);
  947. mode_thumbnail->set_toggle_mode(true);
  948. mode_thumbnail->set_pressed(true);
  949. mode_thumbnail->set_icon(p_editor->get_gui_base()->get_icon("FileThumbnail", "EditorIcons"));
  950. hb->add_child(mode_thumbnail);
  951. mode_thumbnail->connect("pressed", this, "_set_display_mode", varray(DISPLAY_THUMBNAIL));
  952. mode_list = memnew(ToolButton);
  953. mode_list->set_toggle_mode(true);
  954. mode_list->set_pressed(false);
  955. mode_list->set_icon(p_editor->get_gui_base()->get_icon("FileList", "EditorIcons"));
  956. hb->add_child(mode_list);
  957. mode_list->connect("pressed", this, "_set_display_mode", varray(DISPLAY_LIST));
  958. display_mode = DISPLAY_THUMBNAIL;
  959. selected_area = -1;
  960. theme_pallete = memnew(ItemList);
  961. add_child(theme_pallete);
  962. theme_pallete->set_v_size_flags(SIZE_EXPAND_FILL);
  963. area_list = memnew(Tree);
  964. add_child(area_list);
  965. area_list->set_v_size_flags(SIZE_EXPAND_FILL);
  966. area_list->hide();
  967. spatial_editor_hb->add_child(memnew(VSeparator));
  968. Label *fl = memnew(Label);
  969. fl->set_text(" Floor: ");
  970. spatial_editor_hb->add_child(fl);
  971. floor = memnew(SpinBox);
  972. floor->set_min(-32767);
  973. floor->set_max(32767);
  974. floor->set_step(1);
  975. floor->get_line_edit()->add_constant_override("minimum_spaces", 16);
  976. spatial_editor_hb->add_child(floor);
  977. floor->connect("value_changed", this, "_floor_changed");
  978. edit_axis = Vector3::AXIS_Y;
  979. edit_floor[0] = -1;
  980. edit_floor[1] = -1;
  981. edit_floor[2] = -1;
  982. cursor_visible = false;
  983. selected_pallete = -1;
  984. lock_view = false;
  985. cursor_rot = 0;
  986. last_mouseover = Vector3(-1, -1, -1);
  987. selection_mesh = VisualServer::get_singleton()->mesh_create();
  988. duplicate_mesh = VisualServer::get_singleton()->mesh_create();
  989. {
  990. //selection mesh create
  991. DVector<Vector3> lines;
  992. DVector<Vector3> triangles;
  993. for (int i = 0; i < 6; i++) {
  994. Vector3 face_points[4];
  995. for (int j = 0; j < 4; j++) {
  996. float v[3];
  997. v[0] = 1.0;
  998. v[1] = 1 - 2 * ((j >> 1) & 1);
  999. v[2] = v[1] * (1 - 2 * (j & 1));
  1000. for (int k = 0; k < 3; k++) {
  1001. if (i < 3)
  1002. face_points[j][(i + k) % 3] = v[k] * (i >= 3 ? -1 : 1);
  1003. else
  1004. face_points[3 - j][(i + k) % 3] = v[k] * (i >= 3 ? -1 : 1);
  1005. }
  1006. }
  1007. triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1008. triangles.push_back(face_points[1] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1009. triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1010. triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1011. triangles.push_back(face_points[3] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1012. triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1013. }
  1014. for (int i = 0; i < 12; i++) {
  1015. AABB base(Vector3(0, 0, 0), Vector3(1, 1, 1));
  1016. Vector3 a, b;
  1017. base.get_edge(i, a, b);
  1018. lines.push_back(a);
  1019. lines.push_back(b);
  1020. }
  1021. Array d;
  1022. d.resize(VS::ARRAY_MAX);
  1023. inner_mat = VisualServer::get_singleton()->fixed_material_create();
  1024. VisualServer::get_singleton()->fixed_material_set_param(inner_mat, VS::FIXED_MATERIAL_PARAM_DIFFUSE, Color(0.7, 0.7, 1.0, 0.3));
  1025. VisualServer::get_singleton()->material_set_flag(inner_mat, VS::MATERIAL_FLAG_ONTOP, true);
  1026. VisualServer::get_singleton()->material_set_flag(inner_mat, VS::MATERIAL_FLAG_UNSHADED, true);
  1027. VisualServer::get_singleton()->fixed_material_set_flag(inner_mat, VisualServer::FIXED_MATERIAL_FLAG_USE_ALPHA, true);
  1028. d[VS::ARRAY_VERTEX] = triangles;
  1029. VisualServer::get_singleton()->mesh_add_surface(selection_mesh, VS::PRIMITIVE_TRIANGLES, d);
  1030. VisualServer::get_singleton()->mesh_surface_set_material(selection_mesh, 0, inner_mat);
  1031. outer_mat = VisualServer::get_singleton()->fixed_material_create();
  1032. VisualServer::get_singleton()->fixed_material_set_param(outer_mat, VS::FIXED_MATERIAL_PARAM_DIFFUSE, Color(0.7, 0.7, 1.0, 0.8));
  1033. VisualServer::get_singleton()->material_set_line_width(outer_mat, 3.0);
  1034. VisualServer::get_singleton()->material_set_flag(outer_mat, VS::MATERIAL_FLAG_ONTOP, true);
  1035. VisualServer::get_singleton()->material_set_flag(outer_mat, VS::MATERIAL_FLAG_UNSHADED, true);
  1036. VisualServer::get_singleton()->fixed_material_set_flag(outer_mat, VisualServer::FIXED_MATERIAL_FLAG_USE_ALPHA, true);
  1037. d[VS::ARRAY_VERTEX] = lines;
  1038. VisualServer::get_singleton()->mesh_add_surface(selection_mesh, VS::PRIMITIVE_LINES, d);
  1039. VisualServer::get_singleton()->mesh_surface_set_material(selection_mesh, 1, outer_mat);
  1040. inner_mat_dup = VisualServer::get_singleton()->fixed_material_create();
  1041. VisualServer::get_singleton()->fixed_material_set_param(inner_mat_dup, VS::FIXED_MATERIAL_PARAM_DIFFUSE, Color(1.0, 0.7, 0.7, 0.3));
  1042. VisualServer::get_singleton()->material_set_flag(inner_mat_dup, VS::MATERIAL_FLAG_ONTOP, true);
  1043. VisualServer::get_singleton()->material_set_flag(inner_mat_dup, VS::MATERIAL_FLAG_UNSHADED, true);
  1044. VisualServer::get_singleton()->fixed_material_set_flag(inner_mat_dup, VisualServer::FIXED_MATERIAL_FLAG_USE_ALPHA, true);
  1045. d[VS::ARRAY_VERTEX] = triangles;
  1046. VisualServer::get_singleton()->mesh_add_surface(duplicate_mesh, VS::PRIMITIVE_TRIANGLES, d);
  1047. VisualServer::get_singleton()->mesh_surface_set_material(duplicate_mesh, 0, inner_mat_dup);
  1048. outer_mat_dup = VisualServer::get_singleton()->fixed_material_create();
  1049. VisualServer::get_singleton()->fixed_material_set_param(outer_mat_dup, VS::FIXED_MATERIAL_PARAM_DIFFUSE, Color(1.0, 0.7, 0.7, 0.8));
  1050. VisualServer::get_singleton()->material_set_line_width(outer_mat_dup, 3.0);
  1051. VisualServer::get_singleton()->material_set_flag(outer_mat_dup, VS::MATERIAL_FLAG_ONTOP, true);
  1052. VisualServer::get_singleton()->material_set_flag(outer_mat_dup, VS::MATERIAL_FLAG_UNSHADED, true);
  1053. VisualServer::get_singleton()->fixed_material_set_flag(outer_mat_dup, VisualServer::FIXED_MATERIAL_FLAG_USE_ALPHA, true);
  1054. d[VS::ARRAY_VERTEX] = lines;
  1055. VisualServer::get_singleton()->mesh_add_surface(duplicate_mesh, VS::PRIMITIVE_LINES, d);
  1056. VisualServer::get_singleton()->mesh_surface_set_material(duplicate_mesh, 1, outer_mat_dup);
  1057. }
  1058. selection.active = false;
  1059. updating = false;
  1060. }
  1061. GridMapEditor::~GridMapEditor() {
  1062. for (int i = 0; i < 3; i++) {
  1063. if (grid[i].is_valid())
  1064. VisualServer::get_singleton()->free(grid[i]);
  1065. if (grid_instance[i].is_valid())
  1066. VisualServer::get_singleton()->free(grid_instance[i]);
  1067. if (cursor_instance)
  1068. VisualServer::get_singleton()->free(cursor_instance);
  1069. }
  1070. VisualServer::get_singleton()->free(inner_mat);
  1071. VisualServer::get_singleton()->free(outer_mat);
  1072. VisualServer::get_singleton()->free(inner_mat_dup);
  1073. VisualServer::get_singleton()->free(outer_mat_dup);
  1074. VisualServer::get_singleton()->free(selection_mesh);
  1075. if (selection_instance.is_valid())
  1076. VisualServer::get_singleton()->free(selection_instance);
  1077. VisualServer::get_singleton()->free(duplicate_mesh);
  1078. if (duplicate_instance.is_valid())
  1079. VisualServer::get_singleton()->free(duplicate_instance);
  1080. _clear_areas();
  1081. }
  1082. void GridMapEditorPlugin::edit(Object *p_object) {
  1083. gridmap_editor->edit(p_object ? p_object->cast_to<GridMap>() : NULL);
  1084. }
  1085. bool GridMapEditorPlugin::handles(Object *p_object) const {
  1086. return p_object->is_type("GridMap");
  1087. }
  1088. void GridMapEditorPlugin::make_visible(bool p_visible) {
  1089. if (p_visible) {
  1090. gridmap_editor->show();
  1091. gridmap_editor->spatial_editor_hb->show();
  1092. gridmap_editor->set_process(true);
  1093. } else {
  1094. gridmap_editor->spatial_editor_hb->hide();
  1095. gridmap_editor->hide();
  1096. gridmap_editor->edit(NULL);
  1097. gridmap_editor->set_process(false);
  1098. }
  1099. }
  1100. GridMapEditorPlugin::GridMapEditorPlugin(EditorNode *p_node) {
  1101. editor = p_node;
  1102. gridmap_editor = memnew(GridMapEditor(editor));
  1103. SpatialEditor::get_singleton()->get_palette_split()->add_child(gridmap_editor);
  1104. SpatialEditor::get_singleton()->get_palette_split()->move_child(gridmap_editor, 0);
  1105. gridmap_editor->hide();
  1106. }
  1107. GridMapEditorPlugin::~GridMapEditorPlugin() {
  1108. }