props_2d/prop_2d_instance_job.cpp

280 lines
8.3 KiB
C++
Raw Normal View History

2021-11-23 16:53:47 +01:00
/*
Copyright (c) 2019-2022 Péter Magyar
2021-11-23 16:53:47 +01:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
2022-02-21 22:02:46 +01:00
#include "prop_2d_instance_job.h"
2021-11-23 16:53:47 +01:00
2022-02-21 22:02:46 +01:00
#include "prop_2d_instance.h"
2021-11-23 16:53:47 +01:00
2022-02-21 22:02:46 +01:00
#include "./props/prop_2d_data.h"
2021-11-23 16:53:47 +01:00
2022-02-21 22:12:25 +01:00
const String Prop2DInstanceJob::BINDING_STRING_ACTIVE_BUILD_PHASE_TYPE = "Normal,Process,Physics Process";
2021-11-23 16:53:47 +01:00
2022-02-21 22:12:25 +01:00
Prop2DInstanceJob::ActiveBuildPhaseType Prop2DInstanceJob::get_build_phase_type() {
2021-11-23 16:53:47 +01:00
return _build_phase_type;
}
2022-02-21 22:12:25 +01:00
void Prop2DInstanceJob::set_build_phase_type(Prop2DInstanceJob::ActiveBuildPhaseType build_phase_type) {
2021-11-23 16:53:47 +01:00
_build_phase_type = build_phase_type;
}
2022-02-21 22:12:25 +01:00
void Prop2DInstanceJob::set_prop(const Ref<Prop2DData> &prop) {
2021-11-23 16:53:47 +01:00
_prop = prop;
_in_tree = true;
}
2022-02-21 22:12:25 +01:00
void Prop2DInstanceJob::set_prop_instance(Prop2DInstance *instance) {
2021-11-23 16:53:47 +01:00
_instance = instance;
}
2022-02-21 22:12:25 +01:00
void Prop2DInstanceJob::set_prop_instance_bind(Node *instance) {
set_prop_instance(Object::cast_to<Prop2DInstance>(instance));
2021-11-23 16:53:47 +01:00
}
2022-02-21 22:12:25 +01:00
int Prop2DInstanceJob::get_phase() {
2021-11-23 16:53:47 +01:00
return _phase;
}
2022-02-21 22:12:25 +01:00
void Prop2DInstanceJob::set_phase(const int phase) {
2021-11-23 16:53:47 +01:00
_phase = phase;
}
2022-02-21 22:12:25 +01:00
void Prop2DInstanceJob::next_phase() {
2021-11-23 16:53:47 +01:00
++_phase;
}
2022-02-21 22:12:25 +01:00
bool Prop2DInstanceJob::get_build_done() {
2021-11-23 16:53:47 +01:00
return _build_done;
}
2022-02-21 22:12:25 +01:00
void Prop2DInstanceJob::set_build_done(const bool val) {
2021-11-23 16:53:47 +01:00
_build_done = val;
}
2022-02-21 22:12:25 +01:00
void Prop2DInstanceJob::finished() {
2021-11-23 16:53:47 +01:00
set_build_done(true);
if (_instance) {
_instance->build_finished();
}
}
2022-02-21 22:12:25 +01:00
void Prop2DInstanceJob::reset() {
2021-11-23 16:53:47 +01:00
call("_reset");
}
2022-02-21 22:12:25 +01:00
void Prop2DInstanceJob::_reset() {
2021-11-23 16:53:47 +01:00
_build_done = false;
_phase = 0;
}
2022-02-21 22:12:25 +01:00
void Prop2DInstanceJob::_execute() {
2021-11-23 16:53:47 +01:00
ActiveBuildPhaseType origpt = _build_phase_type;
while (!get_cancelled() && _in_tree && !_build_done && origpt == _build_phase_type && !should_return()) {
execute_phase();
}
}
2022-02-21 22:12:25 +01:00
void Prop2DInstanceJob::execute_phase() {
2021-11-23 16:53:47 +01:00
call("_execute_phase");
}
2022-02-21 22:12:25 +01:00
void Prop2DInstanceJob::_execute_phase() {
2021-11-23 16:53:47 +01:00
finished();
}
2022-02-21 22:12:25 +01:00
void Prop2DInstanceJob::process(const float delta) {
2021-11-23 16:53:47 +01:00
if (has_method("_process"))
call("_process", delta);
}
2022-02-21 22:12:25 +01:00
void Prop2DInstanceJob::physics_process(const float delta) {
2021-11-23 16:53:47 +01:00
if (has_method("_physics_process"))
call("_physics_process", delta);
}
2022-02-21 22:12:25 +01:00
void Prop2DInstanceJob::prop_instance_enter_tree() {
2021-11-23 16:53:47 +01:00
_in_tree = true;
}
2022-02-21 22:12:25 +01:00
void Prop2DInstanceJob::prop_instance_exit_tree() {
2021-11-23 16:53:47 +01:00
_in_tree = false;
if (get_complete()) {
_prop.unref();
} else {
set_cancelled(true);
}
}
2022-02-21 22:12:25 +01:00
Prop2DInstanceJob::Prop2DInstanceJob() {
2021-11-23 16:53:47 +01:00
_instance = NULL;
_in_tree = false;
_build_phase_type = BUILD_PHASE_TYPE_NORMAL;
_build_done = true;
_phase = 0;
#if !THREAD_POOL_PRESENT
_complete = true;
_cancelled = false;
_max_allocated_time = 0;
_start_time = 0;
_current_run_stage = 0;
_stage = 0;
#endif
}
2022-02-21 22:12:25 +01:00
Prop2DInstanceJob::~Prop2DInstanceJob() {
2021-11-23 16:53:47 +01:00
_prop.unref();
_instance = NULL;
}
2022-02-21 22:12:25 +01:00
void Prop2DInstanceJob::_bind_methods() {
2023-01-09 14:10:32 +01:00
//BIND_VMETHOD(MethodInfo("_process", PropertyInfo(Variant::REAL, "delta")));
//BIND_VMETHOD(MethodInfo("_physics_process", PropertyInfo(Variant::REAL, "delta")));
2021-11-23 16:53:47 +01:00
2022-02-21 22:12:25 +01:00
ClassDB::bind_method(D_METHOD("get_build_phase_type"), &Prop2DInstanceJob::get_build_phase_type);
ClassDB::bind_method(D_METHOD("set_build_phase_type", "value"), &Prop2DInstanceJob::set_build_phase_type);
2021-11-23 16:53:47 +01:00
ADD_PROPERTY(PropertyInfo(Variant::INT, "build_phase_type", PROPERTY_HINT_ENUM, BINDING_STRING_ACTIVE_BUILD_PHASE_TYPE), "set_build_phase_type", "get_build_phase_type");
2022-02-21 22:12:25 +01:00
ClassDB::bind_method(D_METHOD("set_prop", "prop"), &Prop2DInstanceJob::set_prop);
ClassDB::bind_method(D_METHOD("set_prop_instance", "instance"), &Prop2DInstanceJob::set_prop_instance_bind);
2021-11-23 16:53:47 +01:00
2022-02-21 22:12:25 +01:00
ClassDB::bind_method(D_METHOD("get_phase"), &Prop2DInstanceJob::get_phase);
ClassDB::bind_method(D_METHOD("set_phase", "phase"), &Prop2DInstanceJob::set_phase);
ClassDB::bind_method(D_METHOD("next_phase"), &Prop2DInstanceJob::next_phase);
2021-11-23 16:53:47 +01:00
2022-02-21 22:12:25 +01:00
ClassDB::bind_method(D_METHOD("get_build_done"), &Prop2DInstanceJob::get_build_done);
ClassDB::bind_method(D_METHOD("set_build_done", "val"), &Prop2DInstanceJob::set_build_done);
2021-11-23 16:53:47 +01:00
2022-02-21 22:12:25 +01:00
ClassDB::bind_method(D_METHOD("finished"), &Prop2DInstanceJob::finished);
2021-11-23 16:53:47 +01:00
2023-01-09 14:10:32 +01:00
//BIND_VMETHOD(MethodInfo("_reset"));
2021-11-23 16:53:47 +01:00
2022-02-21 22:12:25 +01:00
ClassDB::bind_method(D_METHOD("reset"), &Prop2DInstanceJob::reset);
ClassDB::bind_method(D_METHOD("_reset"), &Prop2DInstanceJob::_reset);
2021-11-23 16:53:47 +01:00
2022-02-21 22:12:25 +01:00
ClassDB::bind_method(D_METHOD("_execute"), &Prop2DInstanceJob::_execute);
2021-11-23 16:53:47 +01:00
2023-01-09 14:10:32 +01:00
//BIND_VMETHOD(MethodInfo("_execute_phase"));
2021-11-23 16:53:47 +01:00
2022-02-21 22:12:25 +01:00
ClassDB::bind_method(D_METHOD("execute_phase"), &Prop2DInstanceJob::execute_phase);
ClassDB::bind_method(D_METHOD("_execute_phase"), &Prop2DInstanceJob::_execute_phase);
2021-11-23 16:53:47 +01:00
2022-02-21 22:12:25 +01:00
ClassDB::bind_method(D_METHOD("prop_instance_exit_tree"), &Prop2DInstanceJob::prop_instance_exit_tree);
2021-11-23 16:53:47 +01:00
#if !THREAD_POOL_PRESENT
2022-02-21 22:12:25 +01:00
ClassDB::bind_method(D_METHOD("get_complete"), &Prop2DInstanceJob::get_complete);
ClassDB::bind_method(D_METHOD("set_complete", "value"), &Prop2DInstanceJob::set_complete);
2021-11-23 16:53:47 +01:00
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "complete"), "set_complete", "get_complete");
2022-02-21 22:12:25 +01:00
ClassDB::bind_method(D_METHOD("get_start_time"), &Prop2DInstanceJob::get_start_time);
ClassDB::bind_method(D_METHOD("set_start_time", "value"), &Prop2DInstanceJob::set_start_time);
2021-11-23 16:53:47 +01:00
ADD_PROPERTY(PropertyInfo(Variant::INT, "start_time"), "set_start_time", "get_start_time");
2022-02-21 22:12:25 +01:00
ClassDB::bind_method(D_METHOD("get_current_run_stage"), &Prop2DInstanceJob::get_current_run_stage);
ClassDB::bind_method(D_METHOD("set_current_run_stage", "value"), &Prop2DInstanceJob::set_current_run_stage);
2021-11-23 16:53:47 +01:00
ADD_PROPERTY(PropertyInfo(Variant::INT, "current_run_stage"), "set_current_run_stage", "get_current_run_stage");
2022-02-21 22:12:25 +01:00
ClassDB::bind_method(D_METHOD("get_stage"), &Prop2DInstanceJob::get_stage);
ClassDB::bind_method(D_METHOD("set_stage", "value"), &Prop2DInstanceJob::set_stage);
2021-11-23 16:53:47 +01:00
ADD_PROPERTY(PropertyInfo(Variant::INT, "stage"), "set_stage", "get_stage");
2022-02-21 22:12:25 +01:00
ClassDB::bind_method(D_METHOD("get_current_execution_time"), &Prop2DInstanceJob::get_current_execution_time);
2021-11-23 16:53:47 +01:00
2022-02-21 22:12:25 +01:00
ClassDB::bind_method(D_METHOD("should_do", "just_check"), &Prop2DInstanceJob::should_do, DEFVAL(false));
ClassDB::bind_method(D_METHOD("should_return"), &Prop2DInstanceJob::should_return);
2021-11-23 16:53:47 +01:00
2023-01-09 14:10:32 +01:00
//BIND_VMETHOD(MethodInfo("_execute"));
2022-02-21 22:12:25 +01:00
ClassDB::bind_method(D_METHOD("execute"), &Prop2DInstanceJob::execute);
2021-11-23 16:53:47 +01:00
ADD_SIGNAL(MethodInfo("completed"));
#endif
}
#if !THREAD_POOL_PRESENT
2022-02-21 22:12:25 +01:00
bool Prop2DInstanceJob::get_complete() const {
2021-11-23 16:53:47 +01:00
return _complete;
}
2022-02-21 22:12:25 +01:00
void Prop2DInstanceJob::set_complete(const bool value) {
2021-11-23 16:53:47 +01:00
_complete = value;
}
2022-02-21 22:12:25 +01:00
bool Prop2DInstanceJob::get_cancelled() const {
2021-11-23 16:53:47 +01:00
return _cancelled;
}
2022-02-21 22:12:25 +01:00
void Prop2DInstanceJob::set_cancelled(const bool value) {
2021-11-23 16:53:47 +01:00
_cancelled = value;
}
2022-02-21 22:12:25 +01:00
float Prop2DInstanceJob::get_max_allocated_time() const {
2021-11-23 16:53:47 +01:00
return _max_allocated_time;
}
2022-02-21 22:12:25 +01:00
void Prop2DInstanceJob::set_max_allocated_time(const float value) {
2021-11-23 16:53:47 +01:00
_max_allocated_time = value;
}
2022-02-21 22:12:25 +01:00
int Prop2DInstanceJob::get_start_time() const {
2021-11-23 16:53:47 +01:00
return _start_time;
}
2022-02-21 22:12:25 +01:00
void Prop2DInstanceJob::set_start_time(const int value) {
2021-11-23 16:53:47 +01:00
_start_time = value;
}
2022-02-21 22:12:25 +01:00
int Prop2DInstanceJob::get_current_run_stage() const {
2021-11-23 16:53:47 +01:00
return _current_run_stage;
}
2022-02-21 22:12:25 +01:00
void Prop2DInstanceJob::set_current_run_stage(const int value) {
2021-11-23 16:53:47 +01:00
_current_run_stage = value;
}
2022-02-21 22:12:25 +01:00
int Prop2DInstanceJob::get_stage() const {
2021-11-23 16:53:47 +01:00
return _stage;
}
2022-02-21 22:12:25 +01:00
void Prop2DInstanceJob::set_stage(const int value) {
2021-11-23 16:53:47 +01:00
_stage = value;
}
2022-02-21 22:12:25 +01:00
void Prop2DInstanceJob::reset_stages() {
2021-11-23 16:53:47 +01:00
_current_run_stage = 0;
_stage = 0;
}
2022-02-21 22:12:25 +01:00
float Prop2DInstanceJob::get_current_execution_time() {
2021-11-23 16:53:47 +01:00
return 0;
}
2022-02-21 22:12:25 +01:00
bool Prop2DInstanceJob::should_do(const bool just_check) {
2021-11-23 16:53:47 +01:00
return true;
}
2022-02-21 22:12:25 +01:00
bool Prop2DInstanceJob::should_return() {
2021-11-23 16:53:47 +01:00
if (_cancelled)
return true;
return false;
}
2022-02-21 22:12:25 +01:00
void Prop2DInstanceJob::execute() {
2021-11-23 16:53:47 +01:00
ERR_FAIL_COND(!has_method("_execute"));
call("_execute");
}
#endif