call_dialog.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*************************************************************************/
  2. /* call_dialog.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 "call_dialog.h"
  31. #include "object_type_db.h"
  32. #include "print_string.h"
  33. #include "scene/gui/label.h"
  34. class CallDialogParams : public Object {
  35. OBJ_TYPE(CallDialogParams, Object);
  36. public:
  37. bool _set(const StringName &p_name, const Variant &p_value) {
  38. values[p_name] = p_value;
  39. return true;
  40. }
  41. bool _get(const StringName &p_name, Variant &r_ret) const {
  42. if (values.has(p_name)) {
  43. r_ret = values[p_name];
  44. return true;
  45. }
  46. return false;
  47. }
  48. void _get_property_list(List<PropertyInfo> *p_list) const {
  49. for (int i = 0; i < method.arguments.size(); i++)
  50. p_list->push_back(method.arguments[i]);
  51. }
  52. MethodInfo method;
  53. HashMap<String, Variant> values;
  54. CallDialogParams() {}
  55. };
  56. void CallDialog::_notification(int p_what) {
  57. if (p_what == NOTIFICATION_READY) {
  58. call->connect("pressed", this, "_call");
  59. cancel->connect("pressed", this, "_cancel");
  60. //filter->get_path()->connect("text_changed", this,"_text_changed");
  61. _update_method_list();
  62. }
  63. if (p_what == NOTIFICATION_EXIT_TREE) {
  64. call->disconnect("pressed", this, "_call");
  65. cancel->disconnect("pressed", this, "_cancel");
  66. //filter->get_path()->connect("text_changed", this,"_text_changed");
  67. _update_method_list();
  68. }
  69. if (p_what == NOTIFICATION_DRAW) {
  70. RID ci = get_canvas_item();
  71. get_stylebox("panel", "PopupMenu")->draw(ci, Rect2(Point2(), get_size()));
  72. }
  73. }
  74. void CallDialog::_call() {
  75. if (!tree->get_selected())
  76. return;
  77. TreeItem *item = tree->get_selected();
  78. ERR_FAIL_COND(!item);
  79. int idx = item->get_metadata(0);
  80. ERR_FAIL_INDEX(idx, methods.size());
  81. MethodInfo &m = methods[idx];
  82. Variant args[VARIANT_ARG_MAX];
  83. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  84. if (i >= m.arguments.size())
  85. continue;
  86. if (call_params->values.has(m.arguments[i].name))
  87. args[i] = call_params->values[m.arguments[i].name];
  88. }
  89. Variant ret = object->call(m.name, args[0], args[1], args[2], args[3], args[4]);
  90. if (ret.get_type() != Variant::NIL)
  91. return_value->set_text(ret);
  92. else
  93. return_value->set_text("");
  94. }
  95. void CallDialog::_cancel() {
  96. hide();
  97. }
  98. void CallDialog::_item_selected() {
  99. TreeItem *item = tree->get_selected();
  100. ERR_FAIL_COND(!item);
  101. if (item->get_metadata(0).get_type() == Variant::NIL) {
  102. call->set_disabled(true);
  103. return;
  104. }
  105. call->set_disabled(false);
  106. int idx = item->get_metadata(0);
  107. ERR_FAIL_INDEX(idx, methods.size());
  108. MethodInfo &m = methods[idx];
  109. call_params->values.clear();
  110. call_params->method = m;
  111. property_editor->edit(call_params);
  112. property_editor->update_tree();
  113. }
  114. void CallDialog::_update_method_list() {
  115. tree->clear();
  116. if (!object)
  117. return;
  118. TreeItem *root = tree->create_item();
  119. List<MethodInfo> method_list;
  120. object->get_method_list(&method_list);
  121. method_list.sort();
  122. methods.clear();
  123. List<String> inheritance_list;
  124. String type = object->get_type();
  125. while (type != "") {
  126. inheritance_list.push_back(type);
  127. type = ObjectTypeDB::type_inherits_from(type);
  128. }
  129. TreeItem *selected_item = NULL;
  130. for (int i = 0; i < inheritance_list.size(); i++) {
  131. String type = inheritance_list[i];
  132. String parent_type = ObjectTypeDB::type_inherits_from(type);
  133. TreeItem *type_item = NULL;
  134. List<MethodInfo>::Element *N, *E = method_list.front();
  135. while (E) {
  136. N = E->next();
  137. if (parent_type != "" && ObjectTypeDB::get_method(parent_type, E->get().name) != NULL) {
  138. E = N;
  139. continue;
  140. }
  141. if (!type_item) {
  142. type_item = tree->create_item(root);
  143. type_item->set_text(0, type);
  144. if (has_icon(type, "EditorIcons"))
  145. type_item->set_icon(0, get_icon(type, "EditorIcons"));
  146. }
  147. TreeItem *method_item = tree->create_item(type_item);
  148. method_item->set_text(0, E->get().name);
  149. method_item->set_metadata(0, methods.size());
  150. if (E->get().name == selected)
  151. selected_item = method_item;
  152. methods.push_back(E->get());
  153. method_list.erase(E);
  154. E = N;
  155. }
  156. }
  157. if (selected_item)
  158. selected_item->select(0);
  159. }
  160. void CallDialog::_bind_methods() {
  161. ObjectTypeDB::bind_method("_call", &CallDialog::_call);
  162. ObjectTypeDB::bind_method("_cancel", &CallDialog::_cancel);
  163. ObjectTypeDB::bind_method("_item_selected", &CallDialog::_item_selected);
  164. }
  165. void CallDialog::set_object(Object *p_object, StringName p_selected) {
  166. object = p_object;
  167. selected = p_selected;
  168. property_editor->edit(NULL);
  169. call->set_disabled(true);
  170. return_value->clear();
  171. _update_method_list();
  172. method_label->set_text(vformat(TTR("Method List For '%s':"), p_object->get_type()));
  173. }
  174. CallDialog::CallDialog() {
  175. object = NULL;
  176. call = memnew(Button);
  177. call->set_anchor(MARGIN_LEFT, ANCHOR_END);
  178. call->set_anchor(MARGIN_TOP, ANCHOR_END);
  179. call->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  180. call->set_anchor(MARGIN_BOTTOM, ANCHOR_END);
  181. call->set_begin(Point2(70, 29));
  182. call->set_end(Point2(15, 15));
  183. call->set_text(TTR("Call"));
  184. add_child(call);
  185. cancel = memnew(Button);
  186. cancel->set_anchor(MARGIN_TOP, ANCHOR_END);
  187. cancel->set_anchor(MARGIN_BOTTOM, ANCHOR_END);
  188. cancel->set_begin(Point2(15, 29));
  189. cancel->set_end(Point2(70, 15));
  190. cancel->set_text(TTR("Close"));
  191. add_child(cancel);
  192. tree = memnew(Tree);
  193. tree->set_anchor(MARGIN_RIGHT, ANCHOR_RATIO);
  194. tree->set_anchor(MARGIN_BOTTOM, ANCHOR_END);
  195. tree->set_begin(Point2(20, 50));
  196. tree->set_margin(MARGIN_BOTTOM, 44);
  197. tree->set_margin(MARGIN_RIGHT, 0.5);
  198. tree->set_select_mode(Tree::SELECT_ROW);
  199. add_child(tree);
  200. tree->connect("item_selected", this, "_item_selected");
  201. tree->set_hide_root(true);
  202. property_editor = memnew(PropertyEditor);
  203. property_editor->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 15);
  204. property_editor->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 50);
  205. property_editor->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_RATIO, 0.55);
  206. property_editor->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 90);
  207. property_editor->get_scene_tree()->set_hide_root(true);
  208. property_editor->hide_top_label();
  209. add_child(property_editor);
  210. method_label = memnew(Label);
  211. method_label->set_pos(Point2(15, 25));
  212. method_label->set_text(TTR("Method List:"));
  213. add_child(method_label);
  214. Label *label = memnew(Label);
  215. label->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_RATIO, 0.53);
  216. label->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 25);
  217. label->set_text(TTR("Arguments:"));
  218. add_child(label);
  219. return_label = memnew(Label);
  220. return_label->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_RATIO, 0.53);
  221. return_label->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, 85);
  222. return_label->set_text(TTR("Return:"));
  223. add_child(return_label);
  224. return_value = memnew(LineEdit);
  225. return_value->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_RATIO, 0.55);
  226. return_value->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 15);
  227. return_value->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, 65);
  228. add_child(return_value);
  229. /*
  230. label = memnew( Label );
  231. label->set_anchor( MARGIN_TOP, ANCHOR_END );
  232. label->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  233. label->set_begin( Point2( 15,54) );
  234. label->set_end( Point2( 16,44) );
  235. label->set_text("Parameters:");
  236. add_child(label);
  237. */
  238. call_params = memnew(CallDialogParams);
  239. set_as_toplevel(true);
  240. }
  241. CallDialog::~CallDialog() {
  242. memdelete(call_params);
  243. }