call_dialog.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*************************************************************************/
  2. /* call_dialog.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "call_dialog.h"
  30. #include "scene/gui/label.h"
  31. #include "object_type_db.h"
  32. #include "print_string.h"
  33. class CallDialogParams : public Object {
  34. OBJ_TYPE( CallDialogParams, Object );
  35. public:
  36. bool _set(const StringName& p_name, const Variant& p_value) {
  37. values[p_name]=p_value;
  38. return true;
  39. }
  40. bool _get(const StringName& p_name,Variant &r_ret) const {
  41. if (values.has(p_name)) {
  42. r_ret=values[p_name];
  43. return true;
  44. }
  45. return false;
  46. }
  47. void _get_property_list( List<PropertyInfo> *p_list) const {
  48. for(int i=0;i<method.arguments.size();i++)
  49. p_list->push_back(method.arguments[i]);
  50. }
  51. MethodInfo method;
  52. HashMap<String,Variant> values;
  53. CallDialogParams() {}
  54. };
  55. void CallDialog::_notification(int p_what) {
  56. if (p_what==NOTIFICATION_READY) {
  57. call->connect("pressed", this,"_call");
  58. cancel->connect("pressed", this,"_cancel");
  59. //filter->get_path()->connect("text_changed", this,"_text_changed");
  60. _update_method_list();
  61. }
  62. if (p_what==NOTIFICATION_EXIT_SCENE) {
  63. call->disconnect("pressed", this,"_call");
  64. cancel->disconnect("pressed", this,"_cancel");
  65. //filter->get_path()->connect("text_changed", this,"_text_changed");
  66. _update_method_list();
  67. }
  68. if (p_what==NOTIFICATION_DRAW) {
  69. RID ci = get_canvas_item();
  70. get_stylebox("panel","PopupMenu")->draw(ci,Rect2(Point2(),get_size()));
  71. }
  72. }
  73. void CallDialog::_call() {
  74. if (!tree->get_selected())
  75. return;
  76. TreeItem* item=tree->get_selected();
  77. ERR_FAIL_COND(!item);
  78. int idx=item->get_metadata(0);
  79. ERR_FAIL_INDEX(idx,methods.size());
  80. MethodInfo &m = methods[idx];
  81. Variant args[VARIANT_ARG_MAX];
  82. for(int i=0;i<VARIANT_ARG_MAX;i++) {
  83. if (i>=m.arguments.size())
  84. continue;
  85. if (call_params->values.has(m.arguments[i].name))
  86. args[i]=call_params->values[m.arguments[i].name];
  87. }
  88. Variant ret = object->call(m.name,args[0],args[1],args[2],args[3],args[4]);
  89. if (ret.get_type()!=Variant::NIL)
  90. return_value->set_text(ret);
  91. else
  92. return_value->set_text("");
  93. }
  94. void CallDialog::_cancel() {
  95. hide();
  96. }
  97. void CallDialog::_item_selected() {
  98. TreeItem* item=tree->get_selected();
  99. ERR_FAIL_COND(!item);
  100. if (item->get_metadata(0).get_type()==Variant::NIL) {
  101. call->set_disabled(true);
  102. return;
  103. }
  104. call->set_disabled(false);
  105. int idx=item->get_metadata(0);
  106. ERR_FAIL_INDEX(idx,methods.size());
  107. MethodInfo &m = methods[idx];
  108. call_params->values.clear();
  109. call_params->method=m;
  110. property_editor->edit(call_params);
  111. property_editor->update_tree();
  112. }
  113. void CallDialog::_update_method_list() {
  114. tree->clear();
  115. if (!object)
  116. return;
  117. TreeItem *root = tree->create_item();
  118. List<MethodInfo> method_list;
  119. object->get_method_list(&method_list);
  120. method_list.sort();
  121. methods.clear();
  122. List<String> inheritance_list;
  123. String type = object->get_type();
  124. while(type!="") {
  125. inheritance_list.push_back( type );
  126. type=ObjectTypeDB::type_inherits_from(type);
  127. }
  128. TreeItem *selected_item=NULL;
  129. for(int i=0;i<inheritance_list.size();i++) {
  130. String type=inheritance_list[i];
  131. String parent_type=ObjectTypeDB::type_inherits_from(type);
  132. TreeItem *type_item=NULL;
  133. List<MethodInfo>::Element *N,*E=method_list.front();
  134. while(E) {
  135. N=E->next();
  136. if (parent_type!="" && ObjectTypeDB::get_method(parent_type,E->get().name)!=NULL) {
  137. E=N;
  138. continue;
  139. }
  140. if (!type_item) {
  141. type_item=tree->create_item(root);
  142. type_item->set_text(0,type);
  143. if (has_icon(type,"EditorIcons"))
  144. type_item->set_icon(0,get_icon(type,"EditorIcons"));
  145. }
  146. TreeItem *method_item = tree->create_item(type_item);
  147. method_item->set_text(0,E->get().name);
  148. method_item->set_metadata(0,methods.size());
  149. if (E->get().name==selected)
  150. selected_item=method_item;
  151. methods.push_back( E->get() );
  152. method_list.erase(E);
  153. E=N;
  154. }
  155. }
  156. if (selected_item)
  157. selected_item->select(0);
  158. }
  159. void CallDialog::_bind_methods() {
  160. ObjectTypeDB::bind_method("_call",&CallDialog::_call);
  161. ObjectTypeDB::bind_method("_cancel",&CallDialog::_cancel);
  162. ObjectTypeDB::bind_method("_item_selected", &CallDialog::_item_selected);
  163. }
  164. void CallDialog::set_object(Object *p_object,StringName p_selected) {
  165. object=p_object;
  166. selected=p_selected;
  167. property_editor->edit(NULL);
  168. call->set_disabled(true);
  169. return_value->clear();
  170. _update_method_list();
  171. method_label->set_text("Method List For ' "+p_object->get_type()+" ':");
  172. }
  173. CallDialog::CallDialog() {
  174. object=NULL;
  175. call = memnew( Button );
  176. call->set_anchor( MARGIN_LEFT, ANCHOR_END );
  177. call->set_anchor( MARGIN_TOP, ANCHOR_END );
  178. call->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  179. call->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  180. call->set_begin( Point2( 70, 29 ) );
  181. call->set_end( Point2( 15, 15 ) );
  182. call->set_text("Call");
  183. add_child(call);
  184. cancel = memnew( Button );
  185. cancel->set_anchor( MARGIN_TOP, ANCHOR_END );
  186. cancel->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  187. cancel->set_begin( Point2( 15, 29 ) );
  188. cancel->set_end( Point2( 70, 15 ) );
  189. cancel->set_text("Close");
  190. add_child(cancel);
  191. tree = memnew( Tree );
  192. tree->set_anchor( MARGIN_RIGHT, ANCHOR_RATIO );
  193. tree->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  194. tree->set_begin( Point2( 20,50 ) );
  195. tree->set_margin(MARGIN_BOTTOM, 44 );
  196. tree->set_margin(MARGIN_RIGHT, 0.5 );
  197. tree->set_select_mode( Tree::SELECT_ROW );
  198. add_child(tree);
  199. tree->connect("item_selected", this,"_item_selected");
  200. tree->set_hide_root(true);
  201. property_editor = memnew( PropertyEditor );
  202. property_editor->set_anchor_and_margin( MARGIN_RIGHT, ANCHOR_END, 15 );
  203. property_editor->set_anchor_and_margin( MARGIN_TOP, ANCHOR_BEGIN, 50 );
  204. property_editor->set_anchor_and_margin( MARGIN_LEFT, ANCHOR_RATIO, 0.55 );
  205. property_editor->set_anchor_and_margin( MARGIN_BOTTOM, ANCHOR_END, 90 );
  206. property_editor->get_tree()->set_hide_root( true );
  207. property_editor->hide_top_label();
  208. add_child(property_editor);
  209. method_label = memnew( Label );
  210. method_label->set_pos( Point2( 15,25) );
  211. method_label->set_text("Method List:");
  212. add_child(method_label);
  213. Label *label = memnew( Label );
  214. label->set_anchor_and_margin( MARGIN_LEFT, ANCHOR_RATIO, 0.53 );
  215. label->set_anchor_and_margin( MARGIN_TOP, ANCHOR_BEGIN, 25 );
  216. label->set_text("Arguments:");
  217. add_child(label);
  218. return_label = memnew( Label );
  219. return_label->set_anchor_and_margin( MARGIN_LEFT, ANCHOR_RATIO, 0.53 );
  220. return_label->set_anchor_and_margin( MARGIN_TOP, ANCHOR_END, 85 );
  221. return_label->set_text("Return:");
  222. add_child(return_label);
  223. return_value = memnew( LineEdit );
  224. return_value->set_anchor_and_margin( MARGIN_LEFT, ANCHOR_RATIO, 0.55 );
  225. return_value->set_anchor_and_margin( MARGIN_RIGHT, ANCHOR_END, 15 );
  226. return_value->set_anchor_and_margin( MARGIN_TOP, ANCHOR_END, 65 );
  227. add_child(return_value);
  228. /*
  229. label = memnew( Label );
  230. label->set_anchor( MARGIN_TOP, ANCHOR_END );
  231. label->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  232. label->set_begin( Point2( 15,54) );
  233. label->set_end( Point2( 16,44) );
  234. label->set_text("Parameters:");
  235. add_child(label);
  236. */
  237. call_params = memnew( CallDialogParams );
  238. set_as_toplevel(true);
  239. }
  240. CallDialog::~CallDialog()
  241. {
  242. memdelete(call_params);
  243. }