Support explicit values in flag properties, add C# flags support

- Add support for explicit values in properties using `PROPERTY_HINT_FLAGS`
that works the same way it does for enums.
- Fix enums and flags in VisualScriptEditor (it wasn't considering the
explicit value).
- Use `PROPERTY_HINT_FLAGS` for C# enums with the FlagsAttribute instead
of `PROPERTY_HINT_ENUM`.
This commit is contained in:
Raul Santos
2022-03-06 18:04:29 +01:00
parent cec7fc4ffe
commit 862691a9f7
10 changed files with 101 additions and 36 deletions

View File

@@ -627,14 +627,12 @@ EditorPropertyEnum::EditorPropertyEnum() {
///////////////////// FLAGS /////////////////////////
void EditorPropertyFlags::_flag_toggled() {
uint32_t value = 0;
for (int i = 0; i < flags.size(); i++) {
if (flags[i]->is_pressed()) {
uint32_t val = 1;
val <<= flag_indices[i];
value |= val;
}
void EditorPropertyFlags::_flag_toggled(int p_index) {
uint32_t value = get_edited_object()->get(get_edited_property());
if (flags[p_index]->is_pressed()) {
value |= flag_values[p_index];
} else {
value &= ~flag_values[p_index];
}
emit_changed(get_edited_property(), value);
@@ -644,13 +642,7 @@ void EditorPropertyFlags::update_property() {
uint32_t value = get_edited_object()->get(get_edited_property());
for (int i = 0; i < flags.size(); i++) {
uint32_t val = 1;
val <<= flag_indices[i];
if (value & val) {
flags[i]->set_pressed(true);
} else {
flags[i]->set_pressed(false);
}
flags[i]->set_pressed((value & flag_values[i]) == flag_values[i]);
}
}
@@ -658,17 +650,24 @@ void EditorPropertyFlags::setup(const Vector<String> &p_options) {
ERR_FAIL_COND(flags.size());
bool first = true;
uint32_t current_val;
for (int i = 0; i < p_options.size(); i++) {
String option = p_options[i].strip_edges();
if (option != "") {
CheckBox *cb = memnew(CheckBox);
cb->set_text(option);
cb->set_clip_text(true);
cb->connect("pressed", this, "_flag_toggled");
cb->connect("pressed", this, "_flag_toggled", varray(i));
add_focusable(cb);
vbox->add_child(cb);
flags.push_back(cb);
flag_indices.push_back(i);
Vector<String> text_split = p_options[i].split(":");
if (text_split.size() != 1) {
current_val = text_split[1].to_int();
} else {
current_val = 1 << i;
}
flag_values.push_back(current_val);
if (first) {
set_label_reference(cb);
first = false;