item_list_editor_plugin.cpp 12 KB

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