From 37f2222dd77d4ebb9c4df1af598635ac73f0bcf6 Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Thu, 31 Dec 2015 18:26:49 -0300 Subject: [PATCH] -Added Color8(r8,g8,b8,a8) function as well as .r8,.g8,.b8,.a8 members to Color, to deal with colors in the 0-255 range. Closes #2345 --- core/variant_op.cpp | 32 ++++++++++++++++++++++++++++ modules/gdscript/gd_functions.cpp | 35 +++++++++++++++++++++++++++++++ modules/gdscript/gd_functions.h | 1 + 3 files changed, 68 insertions(+) diff --git a/core/variant_op.cpp b/core/variant_op.cpp index e33b79e63c7..59cc32afda1 100644 --- a/core/variant_op.cpp +++ b/core/variant_op.cpp @@ -1352,6 +1352,22 @@ void Variant::set(const Variant& p_index, const Variant& p_value, bool *r_valid) valid=true; v->set_hsv(v->get_h(),v->get_s(),p_value); return; + } else if (*str=="r8" ) { + valid=true; + v->g=float(p_value)/255.0; + return; + } else if (*str=="g8" ) { + valid=true; + v->g=float(p_value)/255.0; + return; + } else if (*str=="b8" ) { + valid=true; + v->b=float(p_value)/255.0; + return; + } else if (*str=="a8" ) { + valid=true; + v->a=float(p_value)/255.0; + return; } } else if (p_index.get_type()==Variant::INT) { @@ -2195,6 +2211,18 @@ Variant Variant::get(const Variant& p_index, bool *r_valid) const { } else if (*str=="v" ) { valid=true; return v->get_v(); + } else if (*str=="r8") { + valid=true; + return v->r*255.0; + } else if (*str=="g8" ) { + valid=true; + return v->g*255.0; + } else if (*str=="b8" ) { + valid=true; + return v->b*255.0; + } else if (*str=="a8" ) { + valid=true; + return v->a*255.0; } } else if (p_index.get_type()==Variant::INT) { @@ -2866,6 +2894,10 @@ void Variant::get_property_list(List *p_list) const { p_list->push_back( PropertyInfo(Variant::REAL,"h")); p_list->push_back( PropertyInfo(Variant::REAL,"s")); p_list->push_back( PropertyInfo(Variant::REAL,"v")); + p_list->push_back( PropertyInfo(Variant::INT,"r8")); + p_list->push_back( PropertyInfo(Variant::INT,"g8")); + p_list->push_back( PropertyInfo(Variant::INT,"b8")); + p_list->push_back( PropertyInfo(Variant::INT,"a8")); } break; case IMAGE: { } break; diff --git a/modules/gdscript/gd_functions.cpp b/modules/gdscript/gd_functions.cpp index 251b0ae3925..1990afb7875 100644 --- a/modules/gdscript/gd_functions.cpp +++ b/modules/gdscript/gd_functions.cpp @@ -98,6 +98,7 @@ const char *GDFunctions::get_func_name(Function p_func) { "load", "inst2dict", "dict2inst", + "Color8", "hash", "print_stack", "instance_from_id", @@ -937,6 +938,33 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va VALIDATE_ARG_COUNT(1); r_ret=p_args[0]->hash(); + } break; + case COLOR8: { + + if (p_arg_count<3) { + r_error.error=Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.argument=3; + return; + } + if (p_arg_count>4) { + r_error.error=Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS; + r_error.argument=4; + return; + } + + VALIDATE_ARG_NUM(0); + VALIDATE_ARG_NUM(1); + VALIDATE_ARG_NUM(2); + + Color color(*p_args[0],*p_args[1],*p_args[2]); + + if (p_arg_count==4) { + VALIDATE_ARG_NUM(3); + color.a=*p_args[3]; + } + + r_ret=color; + } break; case PRINT_STACK: { @@ -1017,6 +1045,7 @@ bool GDFunctions::is_deterministic(Function p_func) { case TYPE_CONVERT: case TYPE_OF: case TEXT_STR: + case COLOR8: // enable for debug only, otherwise not desirable - case GEN_RANGE: return true; default: @@ -1360,6 +1389,12 @@ MethodInfo GDFunctions::get_info(Function p_func) { mi.return_val.type=Variant::INT; return mi; } break; + case COLOR8: { + + MethodInfo mi("Color8",PropertyInfo(Variant::INT,"r8"),PropertyInfo(Variant::INT,"g8"),PropertyInfo(Variant::INT,"b8"),PropertyInfo(Variant::INT,"a8")); + mi.return_val.type=Variant::COLOR; + return mi; + } break; case PRINT_STACK: { MethodInfo mi("print_stack"); diff --git a/modules/gdscript/gd_functions.h b/modules/gdscript/gd_functions.h index ad35a628d54..e348689550d 100644 --- a/modules/gdscript/gd_functions.h +++ b/modules/gdscript/gd_functions.h @@ -94,6 +94,7 @@ public: INST2DICT, DICT2INST, HASH, + COLOR8, PRINT_STACK, INSTANCE_FROM_ID, FUNC_MAX