item_list_editor_plugin.cpp 12 KB

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