item_list_editor_plugin.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*************************************************************************/
  2. /* item_list_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "item_list_editor_plugin.h"
  31. #include "core/io/resource_loader.h"
  32. bool ItemListPlugin::_set(const StringName &p_name, const Variant &p_value) {
  33. String name = p_name;
  34. int idx = name.get_slice("/", 0).to_int();
  35. String what = name.get_slice("/", 1);
  36. if (what == "text")
  37. set_item_text(idx, p_value);
  38. else if (what == "icon")
  39. set_item_icon(idx, p_value);
  40. else if (what == "checkable") {
  41. // This keeps compatibility to/from versions where this property was a boolean, before radio buttons
  42. switch ((int)p_value) {
  43. case 0:
  44. case 1:
  45. set_item_checkable(idx, p_value);
  46. break;
  47. case 2:
  48. set_item_radio_checkable(idx, true);
  49. break;
  50. }
  51. } else if (what == "checked")
  52. set_item_checked(idx, p_value);
  53. else if (what == "id")
  54. set_item_id(idx, p_value);
  55. else if (what == "enabled")
  56. set_item_enabled(idx, p_value);
  57. else if (what == "separator")
  58. set_item_separator(idx, p_value);
  59. else
  60. return false;
  61. return true;
  62. }
  63. bool ItemListPlugin::_get(const StringName &p_name, Variant &r_ret) const {
  64. String name = p_name;
  65. int idx = name.get_slice("/", 0).to_int();
  66. String what = name.get_slice("/", 1);
  67. if (what == "text")
  68. r_ret = get_item_text(idx);
  69. else if (what == "icon")
  70. r_ret = get_item_icon(idx);
  71. else if (what == "checkable") {
  72. // This keeps compatibility to/from versions where this property was a boolean, before radio buttons
  73. if (!is_item_checkable(idx)) {
  74. r_ret = 0;
  75. } else {
  76. r_ret = is_item_radio_checkable(idx) ? 2 : 1;
  77. }
  78. } else if (what == "checked")
  79. r_ret = is_item_checked(idx);
  80. else if (what == "id")
  81. r_ret = get_item_id(idx);
  82. else if (what == "enabled")
  83. r_ret = is_item_enabled(idx);
  84. else if (what == "separator")
  85. r_ret = is_item_separator(idx);
  86. else
  87. return false;
  88. return true;
  89. }
  90. void ItemListPlugin::_get_property_list(List<PropertyInfo> *p_list) const {
  91. for (int i = 0; i < get_item_count(); i++) {
  92. String base = itos(i) + "/";
  93. p_list->push_back(PropertyInfo(Variant::STRING, base + "text"));
  94. p_list->push_back(PropertyInfo(Variant::OBJECT, base + "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture"));
  95. int flags = get_flags();
  96. if (flags & FLAG_CHECKABLE) {
  97. p_list->push_back(PropertyInfo(Variant::BOOL, base + "checkable", PROPERTY_HINT_ENUM, "No,As checkbox,As radio button"));
  98. p_list->push_back(PropertyInfo(Variant::BOOL, base + "checked"));
  99. }
  100. if (flags & FLAG_ID)
  101. p_list->push_back(PropertyInfo(Variant::INT, base + "id", PROPERTY_HINT_RANGE, "-1,4096"));
  102. if (flags & FLAG_ENABLE)
  103. p_list->push_back(PropertyInfo(Variant::BOOL, base + "enabled"));
  104. if (flags & FLAG_SEPARATOR)
  105. p_list->push_back(PropertyInfo(Variant::BOOL, base + "separator"));
  106. }
  107. }
  108. ///////////////////////////////////////////////////////////////
  109. ///////////////////////// PLUGINS /////////////////////////////
  110. ///////////////////////////////////////////////////////////////
  111. void ItemListOptionButtonPlugin::set_object(Object *p_object) {
  112. ob = Object::cast_to<OptionButton>(p_object);
  113. }
  114. bool ItemListOptionButtonPlugin::handles(Object *p_object) const {
  115. return p_object->is_class("OptionButton");
  116. }
  117. int ItemListOptionButtonPlugin::get_flags() const {
  118. return FLAG_ICON | FLAG_ID | FLAG_ENABLE;
  119. }
  120. void ItemListOptionButtonPlugin::add_item() {
  121. ob->add_item(vformat(TTR("Item %d"), ob->get_item_count()));
  122. _change_notify();
  123. }
  124. int ItemListOptionButtonPlugin::get_item_count() const {
  125. return ob->get_item_count();
  126. }
  127. void ItemListOptionButtonPlugin::erase(int p_idx) {
  128. ob->remove_item(p_idx);
  129. _change_notify();
  130. }
  131. ItemListOptionButtonPlugin::ItemListOptionButtonPlugin() {
  132. ob = NULL;
  133. }
  134. ///////////////////////////////////////////////////////////////
  135. void ItemListPopupMenuPlugin::set_object(Object *p_object) {
  136. if (p_object->is_class("MenuButton"))
  137. pp = Object::cast_to<MenuButton>(p_object)->get_popup();
  138. else
  139. pp = Object::cast_to<PopupMenu>(p_object);
  140. }
  141. bool ItemListPopupMenuPlugin::handles(Object *p_object) const {
  142. return p_object->is_class("PopupMenu") || p_object->is_class("MenuButton");
  143. }
  144. int ItemListPopupMenuPlugin::get_flags() const {
  145. return FLAG_ICON | FLAG_CHECKABLE | FLAG_ID | FLAG_ENABLE | FLAG_SEPARATOR;
  146. }
  147. void ItemListPopupMenuPlugin::add_item() {
  148. pp->add_item(vformat(TTR("Item %d"), pp->get_item_count()));
  149. _change_notify();
  150. }
  151. int ItemListPopupMenuPlugin::get_item_count() const {
  152. return pp->get_item_count();
  153. }
  154. void ItemListPopupMenuPlugin::erase(int p_idx) {
  155. pp->remove_item(p_idx);
  156. _change_notify();
  157. }
  158. ItemListPopupMenuPlugin::ItemListPopupMenuPlugin() {
  159. pp = NULL;
  160. }
  161. ///////////////////////////////////////////////////////////////
  162. void ItemListItemListPlugin::set_object(Object *p_object) {
  163. pp = Object::cast_to<ItemList>(p_object);
  164. }
  165. bool ItemListItemListPlugin::handles(Object *p_object) const {
  166. return p_object->is_class("ItemList");
  167. }
  168. int ItemListItemListPlugin::get_flags() const {
  169. return FLAG_ICON | FLAG_ENABLE;
  170. }
  171. void ItemListItemListPlugin::add_item() {
  172. pp->add_item(vformat(TTR("Item %d"), pp->get_item_count()));
  173. _change_notify();
  174. }
  175. int ItemListItemListPlugin::get_item_count() const {
  176. return pp->get_item_count();
  177. }
  178. void ItemListItemListPlugin::erase(int p_idx) {
  179. pp->remove_item(p_idx);
  180. _change_notify();
  181. }
  182. ItemListItemListPlugin::ItemListItemListPlugin() {
  183. pp = NULL;
  184. }
  185. ///////////////////////////////////////////////////////////////
  186. ///////////////////////////////////////////////////////////////
  187. ///////////////////////////////////////////////////////////////
  188. void ItemListEditor::_node_removed(Node *p_node) {
  189. if (p_node == item_list) {
  190. item_list = NULL;
  191. hide();
  192. dialog->hide();
  193. }
  194. }
  195. void ItemListEditor::_notification(int p_notification) {
  196. if (p_notification == NOTIFICATION_ENTER_TREE || p_notification == NOTIFICATION_THEME_CHANGED) {
  197. add_button->set_icon(get_icon("Add", "EditorIcons"));
  198. del_button->set_icon(get_icon("Remove", "EditorIcons"));
  199. } else if (p_notification == NOTIFICATION_READY) {
  200. get_tree()->connect("node_removed", this, "_node_removed");
  201. }
  202. }
  203. void ItemListEditor::_add_pressed() {
  204. if (selected_idx == -1)
  205. return;
  206. item_plugins[selected_idx]->add_item();
  207. }
  208. void ItemListEditor::_delete_pressed() {
  209. if (selected_idx == -1)
  210. return;
  211. String current_selected = (String)property_editor->get_selected_path();
  212. if (current_selected == "")
  213. return;
  214. // FIXME: Currently relying on selecting a *property* to derive what item to delete
  215. // e.g. you select "1/enabled" to delete item 1.
  216. // This should be fixed so that you can delete by selecting the item section header,
  217. // or a delete button on that header.
  218. int idx = current_selected.get_slice("/", 0).to_int();
  219. item_plugins[selected_idx]->erase(idx);
  220. }
  221. void ItemListEditor::_edit_items() {
  222. dialog->popup_centered(Vector2(300, 400) * EDSCALE);
  223. }
  224. void ItemListEditor::edit(Node *p_item_list) {
  225. item_list = p_item_list;
  226. if (!item_list) {
  227. selected_idx = -1;
  228. property_editor->edit(NULL);
  229. return;
  230. }
  231. for (int i = 0; i < item_plugins.size(); i++) {
  232. if (item_plugins[i]->handles(p_item_list)) {
  233. item_plugins[i]->set_object(p_item_list);
  234. property_editor->edit(item_plugins[i]);
  235. toolbar_button->set_icon(EditorNode::get_singleton()->get_object_icon(item_list, ""));
  236. selected_idx = i;
  237. return;
  238. }
  239. }
  240. selected_idx = -1;
  241. property_editor->edit(NULL);
  242. }
  243. bool ItemListEditor::handles(Object *p_object) const {
  244. for (int i = 0; i < item_plugins.size(); i++) {
  245. if (item_plugins[i]->handles(p_object)) {
  246. return true;
  247. }
  248. }
  249. return false;
  250. }
  251. void ItemListEditor::_bind_methods() {
  252. ClassDB::bind_method("_node_removed", &ItemListEditor::_node_removed);
  253. ClassDB::bind_method("_edit_items", &ItemListEditor::_edit_items);
  254. ClassDB::bind_method("_add_button", &ItemListEditor::_add_pressed);
  255. ClassDB::bind_method("_delete_button", &ItemListEditor::_delete_pressed);
  256. }
  257. ItemListEditor::ItemListEditor() {
  258. selected_idx = -1;
  259. toolbar_button = memnew(ToolButton);
  260. toolbar_button->set_text(TTR("Items"));
  261. add_child(toolbar_button);
  262. toolbar_button->connect("pressed", this, "_edit_items");
  263. dialog = memnew(AcceptDialog);
  264. dialog->set_title(TTR("Item List Editor"));
  265. add_child(dialog);
  266. VBoxContainer *vbc = memnew(VBoxContainer);
  267. dialog->add_child(vbc);
  268. //dialog->set_child_rect(vbc);
  269. HBoxContainer *hbc = memnew(HBoxContainer);
  270. hbc->set_h_size_flags(SIZE_EXPAND_FILL);
  271. vbc->add_child(hbc);
  272. add_button = memnew(Button);
  273. add_button->set_text(TTR("Add"));
  274. hbc->add_child(add_button);
  275. add_button->connect("pressed", this, "_add_button");
  276. hbc->add_spacer();
  277. del_button = memnew(Button);
  278. del_button->set_text(TTR("Delete"));
  279. hbc->add_child(del_button);
  280. del_button->connect("pressed", this, "_delete_button");
  281. property_editor = memnew(EditorInspector);
  282. vbc->add_child(property_editor);
  283. property_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  284. }
  285. ItemListEditor::~ItemListEditor() {
  286. for (int i = 0; i < item_plugins.size(); i++)
  287. memdelete(item_plugins[i]);
  288. }
  289. void ItemListEditorPlugin::edit(Object *p_object) {
  290. item_list_editor->edit(Object::cast_to<Node>(p_object));
  291. }
  292. bool ItemListEditorPlugin::handles(Object *p_object) const {
  293. return item_list_editor->handles(p_object);
  294. }
  295. void ItemListEditorPlugin::make_visible(bool p_visible) {
  296. if (p_visible) {
  297. item_list_editor->show();
  298. } else {
  299. item_list_editor->hide();
  300. item_list_editor->edit(NULL);
  301. }
  302. }
  303. ItemListEditorPlugin::ItemListEditorPlugin(EditorNode *p_node) {
  304. editor = p_node;
  305. item_list_editor = memnew(ItemListEditor);
  306. CanvasItemEditor::get_singleton()->add_control_to_menu_panel(item_list_editor);
  307. item_list_editor->hide();
  308. item_list_editor->add_plugin(memnew(ItemListOptionButtonPlugin));
  309. item_list_editor->add_plugin(memnew(ItemListPopupMenuPlugin));
  310. item_list_editor->add_plugin(memnew(ItemListItemListPlugin));
  311. }
  312. ItemListEditorPlugin::~ItemListEditorPlugin() {
  313. }