visual_script_yield_nodes.cpp 17 KB

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