Skip to content

Commit

Permalink
Static color constants for gdscript
Browse files Browse the repository at this point in the history
  • Loading branch information
poke1024 committed Dec 17, 2017
1 parent 5232379 commit 27a7f42
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 21 deletions.
5 changes: 5 additions & 0 deletions core/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,11 @@ bool Color::html_is_valid(const String &p_color) {
return true;
}

const Map<String, Color> &Color::names() {
if (_named_colors.empty()) _populate_named_colors(); // from color_names.inc
return _named_colors;
}

Color Color::named(const String &p_name) {
if (_named_colors.empty()) _populate_named_colors(); // from color_names.inc
String name = p_name;
Expand Down
2 changes: 2 additions & 0 deletions core/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#ifndef COLOR_H
#define COLOR_H

#include "map.h"
#include "math_funcs.h"
#include "ustring.h"
/**
Expand Down Expand Up @@ -187,6 +188,7 @@ struct Color {
static Color hex(uint32_t p_hex);
static Color html(const String &p_color);
static bool html_is_valid(const String &p_color);
static const Map<String, Color> &names();
static Color named(const String &p_name);
String to_html(bool p_alpha = true) const;

Expand Down
6 changes: 3 additions & 3 deletions core/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,9 @@ class Variant {

void static_assign(const Variant &p_variant);
static void get_constructor_list(Variant::Type p_type, List<MethodInfo> *p_list);
static void get_numeric_constants_for_type(Variant::Type p_type, List<StringName> *p_constants);
static bool has_numeric_constant(Variant::Type p_type, const StringName &p_value);
static int get_numeric_constant_value(Variant::Type p_type, const StringName &p_value, bool *r_valid = NULL);
static void get_constants_for_type(Variant::Type p_type, List<StringName> *p_constants);
static bool has_constant(Variant::Type p_type, const StringName &p_value);
static Variant get_constant_value(Variant::Type p_type, const StringName &p_value, bool *r_valid = NULL);

typedef String (*ObjectDeConstruct)(const Variant &p_object, void *ud);
typedef void (*ObjectConstruct)(const String &p_text, void *ud, Variant &r_value);
Expand Down
53 changes: 49 additions & 4 deletions core/variant_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,7 @@ struct _VariantCall {
#ifdef DEBUG_ENABLED
List<StringName> value_ordered;
#endif
Map<StringName, Variant> variant_value;
};

static ConstantData *constant_data;
Expand All @@ -974,6 +975,11 @@ struct _VariantCall {
constant_data[p_type].value_ordered.push_back(p_constant_name);
#endif
}

static void add_variant_constant(int p_type, StringName p_constant_name, const Variant &p_constant_value) {

constant_data[p_type].variant_value[p_constant_name] = p_constant_value;
}
};

_VariantCall::TypeFunc *_VariantCall::type_funcs = NULL;
Expand Down Expand Up @@ -1326,7 +1332,7 @@ void Variant::get_constructor_list(Variant::Type p_type, List<MethodInfo> *p_lis
}
}

void Variant::get_numeric_constants_for_type(Variant::Type p_type, List<StringName> *p_constants) {
void Variant::get_constants_for_type(Variant::Type p_type, List<StringName> *p_constants) {

ERR_FAIL_INDEX(p_type, Variant::VARIANT_MAX);

Expand All @@ -1342,16 +1348,21 @@ void Variant::get_numeric_constants_for_type(Variant::Type p_type, List<StringNa
p_constants->push_back(E->key());
#endif
}

for (Map<StringName, Variant>::Element *E = cd.variant_value.front(); E; E = E->next()) {

p_constants->push_back(E->key());
}
}

bool Variant::has_numeric_constant(Variant::Type p_type, const StringName &p_value) {
bool Variant::has_constant(Variant::Type p_type, const StringName &p_value) {

ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, false);
_VariantCall::ConstantData &cd = _VariantCall::constant_data[p_type];
return cd.value.has(p_value);
return cd.value.has(p_value) || cd.variant_value.has(p_value);
}

int Variant::get_numeric_constant_value(Variant::Type p_type, const StringName &p_value, bool *r_valid) {
Variant Variant::get_constant_value(Variant::Type p_type, const StringName &p_value, bool *r_valid) {

if (r_valid)
*r_valid = false;
Expand All @@ -1361,6 +1372,28 @@ int Variant::get_numeric_constant_value(Variant::Type p_type, const StringName &

Map<StringName, int>::Element *E = cd.value.find(p_value);
if (!E) {
switch (p_type) {
case Variant::COLOR: {
const Map<String, Color>::Element *E = Color::names().find(p_value.operator String());
if (E) {

if (r_valid)
*r_valid = true;
return E->get();
}
} // fallthrough

default: {
Map<StringName, Variant>::Element *E = cd.variant_value.find(p_value);
if (E) {

if (r_valid)
*r_valid = true;
return E->get();
}
} break;
}

return -1;
}
if (r_valid)
Expand Down Expand Up @@ -1811,6 +1844,18 @@ void register_variant_methods() {
_VariantCall::add_constant(Variant::VECTOR3, "AXIS_X", Vector3::AXIS_X);
_VariantCall::add_constant(Variant::VECTOR3, "AXIS_Y", Vector3::AXIS_Y);
_VariantCall::add_constant(Variant::VECTOR3, "AXIS_Z", Vector3::AXIS_Z);

_VariantCall::add_variant_constant(Variant::VECTOR3, "LEFT", Vector3(-1, 0, 0));
_VariantCall::add_variant_constant(Variant::VECTOR3, "RIGHT", Vector3(1, 0, 0));
_VariantCall::add_variant_constant(Variant::VECTOR3, "UP", Vector3(0, 1, 0));
_VariantCall::add_variant_constant(Variant::VECTOR3, "DOWN", Vector3(0, -1, 0));
_VariantCall::add_variant_constant(Variant::VECTOR3, "FORWARD", Vector3(0, 0, 1));
_VariantCall::add_variant_constant(Variant::VECTOR3, "BACK", Vector3(0, 0, -1));

_VariantCall::add_variant_constant(Variant::VECTOR2, "LEFT", Vector2(-1, 0));
_VariantCall::add_variant_constant(Variant::VECTOR2, "RIGHT", Vector2(1, 0));
_VariantCall::add_variant_constant(Variant::VECTOR2, "UP", Vector2(0, 1));
_VariantCall::add_variant_constant(Variant::VECTOR2, "DOWN", Vector2(0, -1));
}

void unregister_variant_methods() {
Expand Down
1 change: 1 addition & 0 deletions doc/classes/Color.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</brief_description>
<description>
A color is represented as red, green and blue (r,g,b) components. Additionally, "a" represents the alpha component, often used for transparency. Values are in floating point and usually range from 0 to 1. Some methods (such as set_modulate(color)) may accept values &gt; 1.
A number of predefined colors can be accessed through static constants, i.e. Color.red, Color.green and so on.
</description>
<tutorials>
</tutorials>
Expand Down
5 changes: 3 additions & 2 deletions editor/doc/doc_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -529,13 +529,14 @@ void DocData::generate(bool p_basic_types) {
}

List<StringName> constants;
Variant::get_numeric_constants_for_type(Variant::Type(i), &constants);
Variant::get_constants_for_type(Variant::Type(i), &constants);

for (List<StringName>::Element *E = constants.front(); E; E = E->next()) {

ConstantDoc constant;
constant.name = E->get();
constant.value = itos(Variant::get_numeric_constant_value(Variant::Type(i), E->get()));
Variant value = Variant::get_constant_value(Variant::Type(i), E->get());
constant.value = value.get_type() == Variant::INT ? itos(value) : value.get_construct_string();
c.constants.push_back(constant);
}
}
Expand Down
5 changes: 2 additions & 3 deletions modules/gdscript/gdscript_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2146,11 +2146,10 @@ Error GDScriptLanguage::complete_code(const String &p_code, const String &p_base
} break;
case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT: {
List<StringName> constants;
Variant::get_numeric_constants_for_type(p.get_completion_built_in_constant(), &constants);
Variant::get_constants_for_type(p.get_completion_built_in_constant(), &constants);
for (List<StringName>::Element *E = constants.front(); E; E = E->next()) {
options.insert(E->get().operator String());
}

} break;
case GDScriptParser::COMPLETION_FUNCTION:
isfunction = true;
Expand Down Expand Up @@ -2936,7 +2935,7 @@ Error GDScriptLanguage::lookup_code(const String &p_code, const String &p_symbol
Variant v = Variant::construct(t.type, NULL, 0, ce);

bool valid;
v.get_numeric_constant_value(t.type, p_symbol, &valid);
v.get_constant_value(t.type, p_symbol, &valid);
if (valid) {
r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
r_result.class_name = Variant::get_type_name(t.type);
Expand Down
13 changes: 7 additions & 6 deletions modules/gdscript/gdscript_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,15 +580,16 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
_set_error("Built-in type constant expected after '.'");
return NULL;
}
if (!Variant::has_numeric_constant(bi_type, identifier)) {
if (!Variant::has_constant(bi_type, identifier)) {

_set_error("Static constant '" + identifier.operator String() + "' not present in built-in type " + Variant::get_type_name(bi_type) + ".");
_set_error("Static constant '" + identifier.operator String() + "' not present in built-in type " + Variant::get_type_name(bi_type) + ".");
return NULL;
}
} else {

ConstantNode *cn = alloc_node<ConstantNode>();
cn->value = Variant::get_numeric_constant_value(bi_type, identifier);
expr = cn;
ConstantNode *cn = alloc_node<ConstantNode>();
cn->value = Variant::get_constant_value(bi_type, identifier);
expr = cn;
}

} else if (tokenizer->get_token(1) == GDScriptTokenizer::TK_PARENTHESIS_OPEN && tokenizer->is_token_literal()) {
// We check with is_token_literal, as this allows us to use match/sync/etc. as a name
Expand Down
6 changes: 3 additions & 3 deletions modules/visual_script/visual_script_nodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1704,7 +1704,7 @@ Variant::Type VisualScriptBasicTypeConstant::get_basic_type() const {

class VisualScriptNodeInstanceBasicTypeConstant : public VisualScriptNodeInstance {
public:
int value;
Variant value;
bool valid;
//virtual int get_working_memory_size() const { return 0; }

Expand All @@ -1723,7 +1723,7 @@ class VisualScriptNodeInstanceBasicTypeConstant : public VisualScriptNodeInstanc
VisualScriptNodeInstance *VisualScriptBasicTypeConstant::instance(VisualScriptInstance *p_instance) {

VisualScriptNodeInstanceBasicTypeConstant *instance = memnew(VisualScriptNodeInstanceBasicTypeConstant);
instance->value = Variant::get_numeric_constant_value(type, name, &instance->valid);
instance->value = Variant::get_constant_value(type, name, &instance->valid);
return instance;
}

Expand All @@ -1732,7 +1732,7 @@ void VisualScriptBasicTypeConstant::_validate_property(PropertyInfo &property) c
if (property.name == "constant") {

List<StringName> constants;
Variant::get_numeric_constants_for_type(type, &constants);
Variant::get_constants_for_type(type, &constants);

if (constants.size() == 0) {
property.usage = 0;
Expand Down

0 comments on commit 27a7f42

Please sign in to comment.