1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-05 12:10:55 +00:00

Merge pull request #37020 from aaronfranke/range

Allow using integer vectors for iteration and make range() use them
This commit is contained in:
Rémi Verschelde
2020-05-10 13:11:07 +02:00
committed by GitHub
3 changed files with 105 additions and 49 deletions

View File

@@ -3458,17 +3458,25 @@ bool Variant::iter_init(Variant &r_iter, bool &valid) const {
return _data._float > 0.0; return _data._float > 0.0;
} break; } break;
case VECTOR2: { case VECTOR2: {
int64_t from = reinterpret_cast<const Vector2 *>(_data._mem)->x; double from = reinterpret_cast<const Vector2 *>(_data._mem)->x;
int64_t to = reinterpret_cast<const Vector2 *>(_data._mem)->y; double to = reinterpret_cast<const Vector2 *>(_data._mem)->y;
r_iter = from;
return from < to;
} break;
case VECTOR2I: {
int64_t from = reinterpret_cast<const Vector2i *>(_data._mem)->x;
int64_t to = reinterpret_cast<const Vector2i *>(_data._mem)->y;
r_iter = from; r_iter = from;
return from < to; return from < to;
} break; } break;
case VECTOR3: { case VECTOR3: {
int64_t from = reinterpret_cast<const Vector3 *>(_data._mem)->x; double from = reinterpret_cast<const Vector3 *>(_data._mem)->x;
int64_t to = reinterpret_cast<const Vector3 *>(_data._mem)->y; double to = reinterpret_cast<const Vector3 *>(_data._mem)->y;
int64_t step = reinterpret_cast<const Vector3 *>(_data._mem)->z; double step = reinterpret_cast<const Vector3 *>(_data._mem)->z;
r_iter = from; r_iter = from;
@@ -3476,10 +3484,22 @@ bool Variant::iter_init(Variant &r_iter, bool &valid) const {
return false; return false;
} else if (from < to) { } else if (from < to) {
return step > 0; return step > 0;
} else {
return step < 0;
} }
//return true; return step < 0;
} break;
case VECTOR3I: {
int64_t from = reinterpret_cast<const Vector3i *>(_data._mem)->x;
int64_t to = reinterpret_cast<const Vector3i *>(_data._mem)->y;
int64_t step = reinterpret_cast<const Vector3i *>(_data._mem)->z;
r_iter = from;
if (from == to) {
return false;
} else if (from < to) {
return step > 0;
}
return step < 0;
} break; } break;
case OBJECT: { case OBJECT: {
@@ -3640,7 +3660,19 @@ bool Variant::iter_next(Variant &r_iter, bool &valid) const {
return true; return true;
} break; } break;
case VECTOR2: { case VECTOR2: {
int64_t to = reinterpret_cast<const Vector2 *>(_data._mem)->y; double to = reinterpret_cast<const Vector2 *>(_data._mem)->y;
double idx = r_iter;
idx++;
if (idx >= to)
return false;
r_iter = idx;
return true;
} break;
case VECTOR2I: {
int64_t to = reinterpret_cast<const Vector2i *>(_data._mem)->y;
int64_t idx = r_iter; int64_t idx = r_iter;
idx++; idx++;
@@ -3652,8 +3684,24 @@ bool Variant::iter_next(Variant &r_iter, bool &valid) const {
return true; return true;
} break; } break;
case VECTOR3: { case VECTOR3: {
int64_t to = reinterpret_cast<const Vector3 *>(_data._mem)->y; double to = reinterpret_cast<const Vector3 *>(_data._mem)->y;
int64_t step = reinterpret_cast<const Vector3 *>(_data._mem)->z; double step = reinterpret_cast<const Vector3 *>(_data._mem)->z;
double idx = r_iter;
idx += step;
if (step < 0 && idx <= to)
return false;
if (step > 0 && idx >= to)
return false;
r_iter = idx;
return true;
} break;
case VECTOR3I: {
int64_t to = reinterpret_cast<const Vector3i *>(_data._mem)->y;
int64_t step = reinterpret_cast<const Vector3i *>(_data._mem)->z;
int64_t idx = r_iter; int64_t idx = r_iter;
idx += step; idx += step;
@@ -3844,10 +3892,18 @@ Variant Variant::iter_get(const Variant &r_iter, bool &r_valid) const {
return r_iter; return r_iter;
} break; } break;
case VECTOR2I: {
return r_iter;
} break;
case VECTOR3: { case VECTOR3: {
return r_iter; return r_iter;
} break; } break;
case VECTOR3I: {
return r_iter;
} break;
case OBJECT: { case OBJECT: {
if (!_get_obj().obj) { if (!_get_obj().obj) {

View File

@@ -3182,9 +3182,9 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
ConstantNode *cn = alloc_node<ConstantNode>(); ConstantNode *cn = alloc_node<ConstantNode>();
switch (args.size()) { switch (args.size()) {
case 1: cn->value = (int)constants[0]; break; case 1: cn->value = (int64_t)constants[0]; break;
case 2: cn->value = Vector2(constants[0], constants[1]); break; case 2: cn->value = Vector2i(constants[0], constants[1]); break;
case 3: cn->value = Vector3(constants[0], constants[1], constants[2]); break; case 3: cn->value = Vector3i(constants[0], constants[1], constants[2]); break;
} }
cn->datatype = _type_from_variant(cn->value); cn->datatype = _type_from_variant(cn->value);
container = cn; container = cn;
@@ -3197,8 +3197,8 @@ void GDScriptParser::_parse_block(BlockNode *p_block, bool p_static) {
switch (args.size()) { switch (args.size()) {
case 1: tn->vtype = Variant::INT; break; case 1: tn->vtype = Variant::INT; break;
case 2: tn->vtype = Variant::VECTOR2; break; case 2: tn->vtype = Variant::VECTOR2I; break;
case 3: tn->vtype = Variant::VECTOR3; break; case 3: tn->vtype = Variant::VECTOR3I; break;
} }
for (int i = 0; i < args.size(); i++) { for (int i = 0; i < args.size(); i++) {
@@ -7820,7 +7820,7 @@ void GDScriptParser::_check_class_level_types(ClassNode *p_class) {
ConstantNode *tgt_type = alloc_node<ConstantNode>(); ConstantNode *tgt_type = alloc_node<ConstantNode>();
tgt_type->line = v.line; tgt_type->line = v.line;
tgt_type->value = (int)v.data_type.builtin_type; tgt_type->value = (int64_t)v.data_type.builtin_type;
OperatorNode *convert_call = alloc_node<OperatorNode>(); OperatorNode *convert_call = alloc_node<OperatorNode>();
convert_call->line = v.line; convert_call->line = v.line;
@@ -8197,7 +8197,7 @@ void GDScriptParser::_check_block_types(BlockNode *p_block) {
ConstantNode *tgt_type = alloc_node<ConstantNode>(); ConstantNode *tgt_type = alloc_node<ConstantNode>();
tgt_type->line = lv->line; tgt_type->line = lv->line;
tgt_type->value = (int)lv->datatype.builtin_type; tgt_type->value = (int64_t)lv->datatype.builtin_type;
tgt_type->datatype = _type_from_variant(tgt_type->value); tgt_type->datatype = _type_from_variant(tgt_type->value);
OperatorNode *convert_call = alloc_node<OperatorNode>(); OperatorNode *convert_call = alloc_node<OperatorNode>();
@@ -8802,7 +8802,7 @@ int GDScriptParser::get_completion_argument_index() {
return completion_argument; return completion_argument;
} }
int GDScriptParser::get_completion_identifier_is_function() { bool GDScriptParser::get_completion_identifier_is_function() {
return completion_ident_is_call; return completion_ident_is_call;
} }

View File

@@ -684,7 +684,7 @@ public:
BlockNode *get_completion_block(); BlockNode *get_completion_block();
FunctionNode *get_completion_function(); FunctionNode *get_completion_function();
int get_completion_argument_index(); int get_completion_argument_index();
int get_completion_identifier_is_function(); bool get_completion_identifier_is_function();
const List<String> &get_dependencies() const { return dependencies; } const List<String> &get_dependencies() const { return dependencies; }