visual_script_yield_nodes.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /*************************************************************************/
  2. /* visual_script_yield_nodes.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 "visual_script_yield_nodes.h"
  31. #include "core/os/os.h"
  32. #include "scene/main/node.h"
  33. #include "scene/main/scene_tree.h"
  34. #include "visual_script_nodes.h"
  35. //////////////////////////////////////////
  36. ////////////////YIELD///////////
  37. //////////////////////////////////////////
  38. int VisualScriptYield::get_output_sequence_port_count() const {
  39. return 1;
  40. }
  41. bool VisualScriptYield::has_input_sequence_port() const {
  42. return true;
  43. }
  44. int VisualScriptYield::get_input_value_port_count() const {
  45. return 0;
  46. }
  47. int VisualScriptYield::get_output_value_port_count() const {
  48. return 0;
  49. }
  50. String VisualScriptYield::get_output_sequence_port_text(int p_port) const {
  51. return String();
  52. }
  53. PropertyInfo VisualScriptYield::get_input_value_port_info(int p_idx) const {
  54. return PropertyInfo();
  55. }
  56. PropertyInfo VisualScriptYield::get_output_value_port_info(int p_idx) const {
  57. return PropertyInfo();
  58. }
  59. String VisualScriptYield::get_caption() const {
  60. return yield_mode == YIELD_RETURN ? "Yield" : "Wait";
  61. }
  62. String VisualScriptYield::get_text() const {
  63. switch (yield_mode) {
  64. case YIELD_RETURN: return ""; break;
  65. case YIELD_FRAME: return "Next Frame"; break;
  66. case YIELD_PHYSICS_FRAME: return "Next Physics Frame"; break;
  67. case YIELD_WAIT: return rtos(wait_time) + " sec(s)"; break;
  68. }
  69. return String();
  70. }
  71. class VisualScriptNodeInstanceYield : public VisualScriptNodeInstance {
  72. public:
  73. VisualScriptYield::YieldMode mode;
  74. float wait_time;
  75. virtual int get_working_memory_size() const { return 1; } //yield needs at least 1
  76. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  77. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return false; }
  78. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  79. if (p_start_mode == START_MODE_RESUME_YIELD) {
  80. return 0; //resuming yield
  81. } else {
  82. //yield
  83. SceneTree *tree = Object::cast_to<SceneTree>(OS::get_singleton()->get_main_loop());
  84. if (!tree) {
  85. r_error_str = "Main Loop is not SceneTree";
  86. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  87. return 0;
  88. }
  89. Ref<VisualScriptFunctionState> state;
  90. state.instance();
  91. int ret = STEP_YIELD_BIT;
  92. switch (mode) {
  93. case VisualScriptYield::YIELD_RETURN:
  94. ret = STEP_EXIT_FUNCTION_BIT;
  95. break; //return the yield
  96. case VisualScriptYield::YIELD_FRAME: state->connect_to_signal(tree, "idle_frame", Array()); break;
  97. case VisualScriptYield::YIELD_PHYSICS_FRAME: state->connect_to_signal(tree, "physics_frame", Array()); break;
  98. case VisualScriptYield::YIELD_WAIT: state->connect_to_signal(tree->create_timer(wait_time).ptr(), "timeout", Array()); break;
  99. }
  100. *p_working_mem = state;
  101. return ret;
  102. }
  103. }
  104. };
  105. VisualScriptNodeInstance *VisualScriptYield::instance(VisualScriptInstance *p_instance) {
  106. VisualScriptNodeInstanceYield *instance = memnew(VisualScriptNodeInstanceYield);
  107. //instance->instance=p_instance;
  108. instance->mode = yield_mode;
  109. instance->wait_time = wait_time;
  110. return instance;
  111. }
  112. void VisualScriptYield::set_yield_mode(YieldMode p_mode) {
  113. if (yield_mode == p_mode)
  114. return;
  115. yield_mode = p_mode;
  116. ports_changed_notify();
  117. _change_notify();
  118. }
  119. VisualScriptYield::YieldMode VisualScriptYield::get_yield_mode() {
  120. return yield_mode;
  121. }
  122. void VisualScriptYield::set_wait_time(float p_time) {
  123. if (wait_time == p_time)
  124. return;
  125. wait_time = p_time;
  126. ports_changed_notify();
  127. }
  128. float VisualScriptYield::get_wait_time() {
  129. return wait_time;
  130. }
  131. void VisualScriptYield::_validate_property(PropertyInfo &property) const {
  132. if (property.name == "wait_time") {
  133. if (yield_mode != YIELD_WAIT) {
  134. property.usage = 0;
  135. }
  136. }
  137. }
  138. void VisualScriptYield::_bind_methods() {
  139. ClassDB::bind_method(D_METHOD("set_yield_mode", "mode"), &VisualScriptYield::set_yield_mode);
  140. ClassDB::bind_method(D_METHOD("get_yield_mode"), &VisualScriptYield::get_yield_mode);
  141. ClassDB::bind_method(D_METHOD("set_wait_time", "sec"), &VisualScriptYield::set_wait_time);
  142. ClassDB::bind_method(D_METHOD("get_wait_time"), &VisualScriptYield::get_wait_time);
  143. ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Frame,Physics Frame,Time", PROPERTY_USAGE_NOEDITOR), "set_yield_mode", "get_yield_mode");
  144. ADD_PROPERTY(PropertyInfo(Variant::REAL, "wait_time"), "set_wait_time", "get_wait_time");
  145. BIND_ENUM_CONSTANT(YIELD_FRAME);
  146. BIND_ENUM_CONSTANT(YIELD_PHYSICS_FRAME);
  147. BIND_ENUM_CONSTANT(YIELD_WAIT);
  148. }
  149. VisualScriptYield::VisualScriptYield() {
  150. yield_mode = YIELD_FRAME;
  151. wait_time = 1;
  152. }
  153. template <VisualScriptYield::YieldMode MODE>
  154. static Ref<VisualScriptNode> create_yield_node(const String &p_name) {
  155. Ref<VisualScriptYield> node;
  156. node.instance();
  157. node->set_yield_mode(MODE);
  158. return node;
  159. }
  160. ///////////////////////////////////////////////////
  161. ////////////////YIELD SIGNAL//////////////////////
  162. //////////////////////////////////////////////////
  163. int VisualScriptYieldSignal::get_output_sequence_port_count() const {
  164. return 1;
  165. }
  166. bool VisualScriptYieldSignal::has_input_sequence_port() const {
  167. return true;
  168. }
  169. #ifdef TOOLS_ENABLED
  170. static Node *_find_script_node(Node *p_edited_scene, Node *p_current_node, const Ref<Script> &script) {
  171. if (p_edited_scene != p_current_node && p_current_node->get_owner() != p_edited_scene)
  172. return NULL;
  173. Ref<Script> scr = p_current_node->get_script();
  174. if (scr.is_valid() && scr == script)
  175. return p_current_node;
  176. for (int i = 0; i < p_current_node->get_child_count(); i++) {
  177. Node *n = _find_script_node(p_edited_scene, p_current_node->get_child(i), script);
  178. if (n)
  179. return n;
  180. }
  181. return NULL;
  182. }
  183. #endif
  184. Node *VisualScriptYieldSignal::_get_base_node() const {
  185. #ifdef TOOLS_ENABLED
  186. Ref<Script> script = get_visual_script();
  187. if (!script.is_valid())
  188. return NULL;
  189. MainLoop *main_loop = OS::get_singleton()->get_main_loop();
  190. SceneTree *scene_tree = Object::cast_to<SceneTree>(main_loop);
  191. if (!scene_tree)
  192. return NULL;
  193. Node *edited_scene = scene_tree->get_edited_scene_root();
  194. if (!edited_scene)
  195. return NULL;
  196. Node *script_node = _find_script_node(edited_scene, edited_scene, script);
  197. if (!script_node)
  198. return NULL;
  199. if (!script_node->has_node(base_path))
  200. return NULL;
  201. Node *path_to = script_node->get_node(base_path);
  202. return path_to;
  203. #else
  204. return NULL;
  205. #endif
  206. }
  207. StringName VisualScriptYieldSignal::_get_base_type() const {
  208. if (call_mode == CALL_MODE_SELF && get_visual_script().is_valid())
  209. return get_visual_script()->get_instance_base_type();
  210. else if (call_mode == CALL_MODE_NODE_PATH && get_visual_script().is_valid()) {
  211. Node *path = _get_base_node();
  212. if (path)
  213. return path->get_class();
  214. }
  215. return base_type;
  216. }
  217. int VisualScriptYieldSignal::get_input_value_port_count() const {
  218. if (call_mode == CALL_MODE_INSTANCE)
  219. return 1;
  220. else
  221. return 0;
  222. }
  223. int VisualScriptYieldSignal::get_output_value_port_count() const {
  224. MethodInfo sr;
  225. if (!ClassDB::get_signal(_get_base_type(), signal, &sr))
  226. return 0;
  227. return sr.arguments.size();
  228. }
  229. String VisualScriptYieldSignal::get_output_sequence_port_text(int p_port) const {
  230. return String();
  231. }
  232. PropertyInfo VisualScriptYieldSignal::get_input_value_port_info(int p_idx) const {
  233. if (call_mode == CALL_MODE_INSTANCE)
  234. return PropertyInfo(Variant::OBJECT, "instance");
  235. else
  236. return PropertyInfo();
  237. }
  238. PropertyInfo VisualScriptYieldSignal::get_output_value_port_info(int p_idx) const {
  239. MethodInfo sr;
  240. if (!ClassDB::get_signal(_get_base_type(), signal, &sr))
  241. return PropertyInfo(); //no signal
  242. ERR_FAIL_INDEX_V(p_idx, sr.arguments.size(), PropertyInfo());
  243. return sr.arguments[p_idx];
  244. }
  245. String VisualScriptYieldSignal::get_caption() const {
  246. static const char *cname[3] = {
  247. "WaitSignal",
  248. "WaitNodeSignal",
  249. "WaitInstanceSigna;",
  250. };
  251. return cname[call_mode];
  252. }
  253. String VisualScriptYieldSignal::get_text() const {
  254. if (call_mode == CALL_MODE_SELF)
  255. return " " + String(signal) + "()";
  256. else
  257. return " " + _get_base_type() + "." + String(signal) + "()";
  258. }
  259. void VisualScriptYieldSignal::set_base_type(const StringName &p_type) {
  260. if (base_type == p_type)
  261. return;
  262. base_type = p_type;
  263. _change_notify();
  264. ports_changed_notify();
  265. }
  266. StringName VisualScriptYieldSignal::get_base_type() const {
  267. return base_type;
  268. }
  269. void VisualScriptYieldSignal::set_signal(const StringName &p_type) {
  270. if (signal == p_type)
  271. return;
  272. signal = p_type;
  273. _change_notify();
  274. ports_changed_notify();
  275. }
  276. StringName VisualScriptYieldSignal::get_signal() const {
  277. return signal;
  278. }
  279. void VisualScriptYieldSignal::set_base_path(const NodePath &p_type) {
  280. if (base_path == p_type)
  281. return;
  282. base_path = p_type;
  283. _change_notify();
  284. ports_changed_notify();
  285. }
  286. NodePath VisualScriptYieldSignal::get_base_path() const {
  287. return base_path;
  288. }
  289. void VisualScriptYieldSignal::set_call_mode(CallMode p_mode) {
  290. if (call_mode == p_mode)
  291. return;
  292. call_mode = p_mode;
  293. _change_notify();
  294. ports_changed_notify();
  295. }
  296. VisualScriptYieldSignal::CallMode VisualScriptYieldSignal::get_call_mode() const {
  297. return call_mode;
  298. }
  299. void VisualScriptYieldSignal::_validate_property(PropertyInfo &property) const {
  300. if (property.name == "base_type") {
  301. if (call_mode != CALL_MODE_INSTANCE) {
  302. property.usage = PROPERTY_USAGE_NOEDITOR;
  303. }
  304. }
  305. if (property.name == "node_path") {
  306. if (call_mode != CALL_MODE_NODE_PATH) {
  307. property.usage = 0;
  308. } else {
  309. Node *bnode = _get_base_node();
  310. if (bnode) {
  311. property.hint_string = bnode->get_path(); //convert to loong string
  312. } else {
  313. }
  314. }
  315. }
  316. if (property.name == "signal") {
  317. property.hint = PROPERTY_HINT_ENUM;
  318. List<MethodInfo> methods;
  319. ClassDB::get_signal_list(_get_base_type(), &methods);
  320. List<String> mstring;
  321. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  322. if (E->get().name.begins_with("_"))
  323. continue;
  324. mstring.push_back(E->get().name.get_slice(":", 0));
  325. }
  326. mstring.sort();
  327. String ml;
  328. for (List<String>::Element *E = mstring.front(); E; E = E->next()) {
  329. if (ml != String())
  330. ml += ",";
  331. ml += E->get();
  332. }
  333. property.hint_string = ml;
  334. }
  335. }
  336. void VisualScriptYieldSignal::_bind_methods() {
  337. ClassDB::bind_method(D_METHOD("set_base_type", "base_type"), &VisualScriptYieldSignal::set_base_type);
  338. ClassDB::bind_method(D_METHOD("get_base_type"), &VisualScriptYieldSignal::get_base_type);
  339. ClassDB::bind_method(D_METHOD("set_signal", "signal"), &VisualScriptYieldSignal::set_signal);
  340. ClassDB::bind_method(D_METHOD("get_signal"), &VisualScriptYieldSignal::get_signal);
  341. ClassDB::bind_method(D_METHOD("set_call_mode", "mode"), &VisualScriptYieldSignal::set_call_mode);
  342. ClassDB::bind_method(D_METHOD("get_call_mode"), &VisualScriptYieldSignal::get_call_mode);
  343. ClassDB::bind_method(D_METHOD("set_base_path", "base_path"), &VisualScriptYieldSignal::set_base_path);
  344. ClassDB::bind_method(D_METHOD("get_base_path"), &VisualScriptYieldSignal::get_base_path);
  345. String bt;
  346. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  347. if (i > 0)
  348. bt += ",";
  349. bt += Variant::get_type_name(Variant::Type(i));
  350. }
  351. ADD_PROPERTY(PropertyInfo(Variant::INT, "call_mode", PROPERTY_HINT_ENUM, "Self,Node Path,Instance"), "set_call_mode", "get_call_mode");
  352. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_type", PROPERTY_HINT_TYPE_STRING, "Object"), "set_base_type", "get_base_type");
  353. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_path", PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE), "set_base_path", "get_base_path");
  354. ADD_PROPERTY(PropertyInfo(Variant::STRING, "signal"), "set_signal", "get_signal");
  355. BIND_ENUM_CONSTANT(CALL_MODE_SELF);
  356. BIND_ENUM_CONSTANT(CALL_MODE_NODE_PATH);
  357. BIND_ENUM_CONSTANT(CALL_MODE_INSTANCE);
  358. }
  359. class VisualScriptNodeInstanceYieldSignal : public VisualScriptNodeInstance {
  360. public:
  361. VisualScriptYieldSignal::CallMode call_mode;
  362. NodePath node_path;
  363. int output_args;
  364. StringName signal;
  365. VisualScriptYieldSignal *node;
  366. VisualScriptInstance *instance;
  367. virtual int get_working_memory_size() const { return 1; }
  368. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  369. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
  370. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  371. if (p_start_mode == START_MODE_RESUME_YIELD) {
  372. return 0; //resuming yield
  373. } else {
  374. //yield
  375. Object *object = NULL;
  376. switch (call_mode) {
  377. case VisualScriptYieldSignal::CALL_MODE_SELF: {
  378. object = instance->get_owner_ptr();
  379. } break;
  380. case VisualScriptYieldSignal::CALL_MODE_NODE_PATH: {
  381. Node *node = Object::cast_to<Node>(instance->get_owner_ptr());
  382. if (!node) {
  383. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  384. r_error_str = "Base object is not a Node!";
  385. return 0;
  386. }
  387. Node *another = node->get_node(node_path);
  388. if (!another) {
  389. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  390. r_error_str = "Path does not lead Node!";
  391. return 0;
  392. }
  393. object = another;
  394. } break;
  395. case VisualScriptYieldSignal::CALL_MODE_INSTANCE: {
  396. object = *p_inputs[0];
  397. if (!object) {
  398. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  399. r_error_str = "Supplied instance input is null.";
  400. return 0;
  401. }
  402. } break;
  403. }
  404. Ref<VisualScriptFunctionState> state;
  405. state.instance();
  406. state->connect_to_signal(object, signal, Array());
  407. *p_working_mem = state;
  408. return STEP_YIELD_BIT;
  409. }
  410. }
  411. };
  412. VisualScriptNodeInstance *VisualScriptYieldSignal::instance(VisualScriptInstance *p_instance) {
  413. VisualScriptNodeInstanceYieldSignal *instance = memnew(VisualScriptNodeInstanceYieldSignal);
  414. instance->node = this;
  415. instance->instance = p_instance;
  416. instance->signal = signal;
  417. instance->call_mode = call_mode;
  418. instance->node_path = base_path;
  419. instance->output_args = get_output_value_port_count();
  420. return instance;
  421. }
  422. VisualScriptYieldSignal::VisualScriptYieldSignal() {
  423. call_mode = CALL_MODE_SELF;
  424. base_type = "Object";
  425. }
  426. template <VisualScriptYieldSignal::CallMode cmode>
  427. static Ref<VisualScriptNode> create_yield_signal_node(const String &p_name) {
  428. Ref<VisualScriptYieldSignal> node;
  429. node.instance();
  430. node->set_call_mode(cmode);
  431. return node;
  432. }
  433. void register_visual_script_yield_nodes() {
  434. VisualScriptLanguage::singleton->add_register_func("functions/wait/wait_frame", create_yield_node<VisualScriptYield::YIELD_FRAME>);
  435. VisualScriptLanguage::singleton->add_register_func("functions/wait/wait_physics_frame", create_yield_node<VisualScriptYield::YIELD_PHYSICS_FRAME>);
  436. VisualScriptLanguage::singleton->add_register_func("functions/wait/wait_time", create_yield_node<VisualScriptYield::YIELD_WAIT>);
  437. VisualScriptLanguage::singleton->add_register_func("functions/yield", create_yield_node<VisualScriptYield::YIELD_RETURN>);
  438. VisualScriptLanguage::singleton->add_register_func("functions/yield_signal", create_node_generic<VisualScriptYieldSignal>);
  439. }