create_dialog.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /*************************************************************************/
  2. /* create_dialog.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 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 "create_dialog.h"
  30. #include "object_type_db.h"
  31. #include "print_string.h"
  32. #include "scene/gui/box_container.h"
  33. #include "editor_node.h"
  34. #if 1
  35. #include "os/keyboard.h"
  36. void CreateDialog::popup(bool p_dontclear) {
  37. popup_centered_ratio(0.6);
  38. if (p_dontclear)
  39. search_box->select_all();
  40. else
  41. search_box->clear();
  42. search_box->grab_focus();
  43. _update_search();
  44. }
  45. void CreateDialog::_text_changed(const String& p_newtext) {
  46. _update_search();
  47. }
  48. void CreateDialog::_sbox_input(const InputEvent& p_ie) {
  49. if (p_ie.type==InputEvent::KEY && (
  50. p_ie.key.scancode == KEY_UP ||
  51. p_ie.key.scancode == KEY_DOWN ||
  52. p_ie.key.scancode == KEY_PAGEUP ||
  53. p_ie.key.scancode == KEY_PAGEDOWN ) ) {
  54. search_options->call("_input_event",p_ie);
  55. search_box->accept_event();
  56. }
  57. }
  58. void CreateDialog::add_type(const String& p_type,HashMap<String,TreeItem*>& p_types,TreeItem *p_root,TreeItem **to_select) {
  59. if (p_types.has(p_type))
  60. return;
  61. if (!ObjectTypeDB::is_type(p_type,base_type) || p_type==base_type)
  62. return;
  63. String inherits=ObjectTypeDB::type_inherits_from(p_type);
  64. TreeItem *parent=p_root;
  65. if (inherits.length()) {
  66. if (!p_types.has(inherits)) {
  67. add_type(inherits,p_types,p_root,to_select);
  68. }
  69. if (p_types.has(inherits) )
  70. parent=p_types[inherits];
  71. }
  72. TreeItem *item = search_options->create_item(parent);
  73. item->set_text(0,p_type);
  74. if (!ObjectTypeDB::can_instance(p_type)) {
  75. item->set_custom_color(0, Color(0.5,0.5,0.5) );
  76. item->set_selectable(0,false);
  77. } else {
  78. if (!*to_select && (search_box->get_text()=="" || p_type.findn(search_box->get_text())!=-1)) {
  79. *to_select=item;
  80. }
  81. }
  82. if (has_icon(p_type,"EditorIcons")) {
  83. item->set_icon(0, get_icon(p_type,"EditorIcons"));
  84. }
  85. p_types[p_type]=item;
  86. }
  87. void CreateDialog::_update_search() {
  88. search_options->clear();
  89. /*
  90. TreeItem *root = search_options->create_item();
  91. _parse_fs(EditorFileSystem::get_singleton()->get_filesystem());
  92. */
  93. List<String> type_list;
  94. ObjectTypeDB::get_type_list(&type_list);
  95. HashMap<String,TreeItem*> types;
  96. TreeItem *root = search_options->create_item();
  97. root->set_text(0,base_type);
  98. List<String>::Element *I=type_list.front();
  99. TreeItem *to_select=NULL;
  100. for(;I;I=I->next()) {
  101. String type=I->get();
  102. if (!ObjectTypeDB::can_instance(type))
  103. continue; // cant create what can't be instanced
  104. if (search_box->get_text()=="")
  105. add_type(type,types,root,&to_select);
  106. else {
  107. bool found=false;
  108. String type=I->get();
  109. while(type!="" && ObjectTypeDB::is_type(type,base_type) && type!=base_type) {
  110. if (type.findn(search_box->get_text())!=-1) {
  111. found=true;
  112. break;
  113. }
  114. type=ObjectTypeDB::type_inherits_from(type);
  115. }
  116. if (found)
  117. add_type(I->get(),types,root,&to_select);
  118. }
  119. if (EditorNode::get_editor_data().get_custom_types().has(type)) {
  120. //there are custom types based on this... cool.
  121. const Vector<EditorData::CustomType> &ct = EditorNode::get_editor_data().get_custom_types()[type];
  122. for(int i=0;i<ct.size();i++) {
  123. bool show = search_box->get_text()=="" || ct[i].name.findn(search_box->get_text())!=-1;
  124. if (!show)
  125. continue;
  126. if (!types.has(type))
  127. add_type(type,types,root,&to_select);
  128. TreeItem *ti;
  129. if (types.has(type) )
  130. ti=types[type];
  131. else
  132. ti=search_options->get_root();
  133. TreeItem *item = search_options->create_item(ti);
  134. item->set_metadata(0,type);
  135. item->set_text(0,ct[i].name);
  136. if (ct[i].icon.is_valid()) {
  137. item->set_icon(0,ct[i].icon);
  138. }
  139. if (!to_select && (search_box->get_text()=="" || ct[i].name.findn(search_box->get_text())!=-1)) {
  140. to_select=item;
  141. }
  142. }
  143. }
  144. }
  145. if (to_select)
  146. to_select->select(0);
  147. get_ok()->set_disabled(root->get_children()==NULL);
  148. }
  149. void CreateDialog::_confirmed() {
  150. TreeItem *ti = search_options->get_selected();
  151. if (!ti)
  152. return;
  153. emit_signal("create");
  154. hide();
  155. }
  156. void CreateDialog::_notification(int p_what) {
  157. if (p_what==NOTIFICATION_ENTER_SCENE) {
  158. connect("confirmed",this,"_confirmed");
  159. _update_search();
  160. }
  161. if (p_what==NOTIFICATION_VISIBILITY_CHANGED) {
  162. if (is_visible()) {
  163. search_box->call_deferred("grab_focus"); // still not visible
  164. search_box->select_all();
  165. }
  166. }
  167. }
  168. void CreateDialog::set_base_type(const String& p_base) {
  169. base_type=p_base;
  170. set_title("Create New "+p_base);
  171. _update_search();
  172. }
  173. Object *CreateDialog::instance_selected() {
  174. TreeItem *selected = search_options->get_selected();
  175. if (selected) {
  176. return ObjectTypeDB::instance(selected->get_text(0));
  177. }
  178. return NULL;
  179. }
  180. String CreateDialog::get_base_type() const {
  181. return base_type;
  182. }
  183. void CreateDialog::_bind_methods() {
  184. ObjectTypeDB::bind_method(_MD("_text_changed"),&CreateDialog::_text_changed);
  185. ObjectTypeDB::bind_method(_MD("_confirmed"),&CreateDialog::_confirmed);
  186. ObjectTypeDB::bind_method(_MD("_sbox_input"),&CreateDialog::_sbox_input);
  187. ADD_SIGNAL(MethodInfo("create"));
  188. }
  189. CreateDialog::CreateDialog() {
  190. VBoxContainer *vbc = memnew( VBoxContainer );
  191. add_child(vbc);
  192. set_child_rect(vbc);
  193. search_box = memnew( LineEdit );
  194. vbc->add_margin_child("Search:",search_box);
  195. search_box->connect("text_changed",this,"_text_changed");
  196. search_box->connect("input_event",this,"_sbox_input");
  197. search_options = memnew( Tree );
  198. vbc->add_margin_child("Matches:",search_options,true);
  199. get_ok()->set_text("Open");
  200. get_ok()->set_disabled(true);
  201. register_text_enter(search_box);
  202. set_hide_on_ok(false);
  203. search_options->connect("item_activated",this,"_confirmed");
  204. // search_options->set_hide_root(true);
  205. base_type="Object";
  206. }
  207. #else
  208. //old create dialog, disabled
  209. void CreateDialog::_notification(int p_what) {
  210. if (p_what==NOTIFICATION_READY) {
  211. connect("confirmed",this,"_create");
  212. update_tree();
  213. }
  214. if (p_what==NOTIFICATION_DRAW) {
  215. //RID ci = get_canvas_item();
  216. //get_stylebox("panel","PopupMenu")->draw(ci,Rect2(Point2(),get_size()));
  217. }
  218. }
  219. void CreateDialog::_create() {
  220. if (tree->get_selected())
  221. emit_signal("create");
  222. hide();
  223. }
  224. void CreateDialog::_cancel() {
  225. hide();
  226. }
  227. void CreateDialog::_text_changed(String p_text) {
  228. update_tree();
  229. }
  230. void CreateDialog::add_type(const String& p_type,HashMap<String,TreeItem*>& p_types,TreeItem *p_root) {
  231. if (p_types.has(p_type))
  232. return;
  233. if (!ObjectTypeDB::is_type(p_type,base) || p_type==base)
  234. return;
  235. String inherits=ObjectTypeDB::type_inherits_from(p_type);
  236. TreeItem *parent=p_root;
  237. if (inherits.length()) {
  238. if (!p_types.has(inherits)) {
  239. add_type(inherits,p_types,p_root);
  240. }
  241. if (p_types.has(inherits) )
  242. parent=p_types[inherits];
  243. }
  244. TreeItem *item = tree->create_item(parent);
  245. item->set_text(0,p_type);
  246. if (!ObjectTypeDB::can_instance(p_type)) {
  247. item->set_custom_color(0, Color(0.5,0.5,0.5) );
  248. item->set_selectable(0,false);
  249. }
  250. if (has_icon(p_type,"EditorIcons")) {
  251. item->set_icon(0, get_icon(p_type,"EditorIcons"));
  252. }
  253. p_types[p_type]=item;
  254. }
  255. void CreateDialog::update_tree() {
  256. tree->clear();
  257. List<String> type_list;
  258. ObjectTypeDB::get_type_list(&type_list);
  259. HashMap<String,TreeItem*> types;
  260. TreeItem *root = tree->create_item();
  261. root->set_text(0,base);
  262. List<String>::Element *I=type_list.front();
  263. for(;I;I=I->next()) {
  264. String type=I->get();
  265. if (!ObjectTypeDB::can_instance(type))
  266. continue; // cant create what can't be instanced
  267. if (filter->get_text()=="")
  268. add_type(type,types,root);
  269. else {
  270. bool found=false;
  271. String type=I->get();
  272. while(type!="" && ObjectTypeDB::is_type(type,base) && type!=base) {
  273. if (type.findn(filter->get_text())!=-1) {
  274. found=true;
  275. break;
  276. }
  277. type=ObjectTypeDB::type_inherits_from(type);
  278. }
  279. if (found)
  280. add_type(I->get(),types,root);
  281. }
  282. if (EditorNode::get_editor_data().get_custom_types().has(type)) {
  283. //there are custom types based on this... cool.
  284. const Vector<EditorData::CustomType> &ct = EditorNode::get_editor_data().get_custom_types()[type];
  285. for(int i=0;i<ct.size();i++) {
  286. bool show = filter->get_text()=="" || ct[i].name.findn(filter->get_text())!=-1;
  287. if (!show)
  288. continue;
  289. if (!types.has(type))
  290. add_type(type,types,root);
  291. TreeItem *ti;
  292. if (types.has(type) )
  293. ti=types[type];
  294. else
  295. ti=tree->get_root();
  296. TreeItem *item = tree->create_item(ti);
  297. item->set_metadata(0,type);
  298. item->set_text(0,ct[i].name);
  299. if (ct[i].icon.is_valid()) {
  300. item->set_icon(0,ct[i].icon);
  301. }
  302. }
  303. }
  304. }
  305. }
  306. Object *CreateDialog::instance_selected() {
  307. if (!tree->get_selected())
  308. return NULL;
  309. String base = String(tree->get_selected()->get_metadata(0));
  310. if (base!="") {
  311. String name = tree->get_selected()->get_text(0);
  312. if (EditorNode::get_editor_data().get_custom_types().has(base)) {
  313. const Vector<EditorData::CustomType> &ct = EditorNode::get_editor_data().get_custom_types()[base];
  314. for(int i=0;i<ct.size();i++) {
  315. if (ct[i].name==name) {
  316. Object* obj = ObjectTypeDB::instance(base);
  317. ERR_FAIL_COND_V(!obj,NULL);
  318. obj->set_script(ct[i].script.get_ref_ptr());
  319. if (ct[i].icon.is_valid())
  320. obj->set_meta("_editor_icon",ct[i].icon);
  321. return obj;
  322. }
  323. }
  324. }
  325. ERR_FAIL_V(NULL);
  326. }
  327. return ObjectTypeDB::instance(tree->get_selected()->get_text(0));
  328. }
  329. void CreateDialog::_bind_methods() {
  330. ObjectTypeDB::bind_method("_create",&CreateDialog::_create);
  331. ObjectTypeDB::bind_method("_cancel",&CreateDialog::_cancel);
  332. ObjectTypeDB::bind_method("_text_changed", &CreateDialog::_text_changed);
  333. ADD_SIGNAL( MethodInfo("create"));
  334. }
  335. void CreateDialog::set_base_type(const String& p_base) {
  336. set_title("Create "+p_base+" Type");
  337. if (base==p_base)
  338. return;
  339. base=p_base;
  340. if (is_inside_scene())
  341. update_tree();
  342. }
  343. String CreateDialog::get_base_type() const {
  344. return base;
  345. }
  346. CreateDialog::CreateDialog() {
  347. VBoxContainer *vbc = memnew( VBoxContainer );
  348. add_child(vbc);
  349. set_child_rect(vbc);
  350. get_ok()->set_text("Create");
  351. tree = memnew( Tree );
  352. vbc->add_margin_child("Type:",tree,true);
  353. //tree->set_hide_root(true);
  354. filter = memnew( LineEdit );
  355. vbc->add_margin_child("Filter:",filter);
  356. base="Node";
  357. set_as_toplevel(true);
  358. tree->connect("item_activated", this, "_create");
  359. filter->connect("text_changed", this,"_text_changed");
  360. }
  361. CreateDialog::~CreateDialog()
  362. {
  363. }
  364. #endif