item_list_editor_plugin.cpp 11 KB

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