grid_map_editor_plugin.cpp 41 KB

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