Merge pull request #64626 from YuriSizov/control-farewell-meta

Replace meta properties with regular properties in `Control`
This commit is contained in:
Rémi Verschelde 2022-08-22 19:26:32 +02:00 committed by GitHub
commit acd8fb7bf0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 30 deletions

View File

@ -565,6 +565,26 @@ void Control::_validate_property(PropertyInfo &property) const {
}
}
bool Control::_property_can_revert(const StringName &p_name) const {
if (p_name == "layout_mode" || p_name == "anchors_preset") {
return true;
}
return false;
}
bool Control::_property_get_revert(const StringName &p_name, Variant &r_property) const {
if (p_name == "layout_mode") {
r_property = _get_default_layout_mode();
return true;
} else if (p_name == "anchors_preset") {
r_property = LayoutPreset::PRESET_TOP_LEFT;
return true;
}
return false;
}
// Global relations.
bool Control::is_top_level_control() const {
@ -794,25 +814,16 @@ void Control::_compute_offsets(Rect2 p_rect, const real_t p_anchors[4], real_t (
void Control::_set_layout_mode(LayoutMode p_mode) {
bool list_changed = false;
if (p_mode == LayoutMode::LAYOUT_MODE_POSITION || p_mode == LayoutMode::LAYOUT_MODE_ANCHORS) {
if ((int)get_meta("_edit_layout_mode", p_mode) != (int)p_mode) {
if (data.stored_layout_mode != p_mode) {
list_changed = true;
data.stored_layout_mode = p_mode;
}
set_meta("_edit_layout_mode", (int)p_mode);
if (p_mode == LayoutMode::LAYOUT_MODE_POSITION) {
remove_meta("_edit_layout_mode");
remove_meta("_edit_use_custom_anchors");
if (data.stored_layout_mode == LayoutMode::LAYOUT_MODE_POSITION) {
data.stored_use_custom_anchors = false;
set_anchors_and_offsets_preset(LayoutPreset::PRESET_TOP_LEFT, LayoutPresetMode::PRESET_MODE_KEEP_SIZE);
set_grow_direction_preset(LayoutPreset::PRESET_TOP_LEFT);
}
} else {
if (has_meta("_edit_layout_mode")) {
remove_meta("_edit_layout_mode");
list_changed = true;
}
}
if (list_changed) {
notify_property_list_changed();
@ -832,33 +843,43 @@ Control::LayoutMode Control::_get_layout_mode() const {
if (_get_anchors_layout_preset() != (int)LayoutPreset::PRESET_TOP_LEFT) {
return LayoutMode::LAYOUT_MODE_ANCHORS;
}
// Otherwise check what was saved.
if (has_meta("_edit_layout_mode")) {
return (LayoutMode)(int)get_meta("_edit_layout_mode");
// Otherwise fallback on what's stored.
return data.stored_layout_mode;
}
// Or fallback on default.
Control::LayoutMode Control::_get_default_layout_mode() const {
Node *parent_node = get_parent_control();
// In these modes the property is read-only.
if (!parent_node) {
return LayoutMode::LAYOUT_MODE_UNCONTROLLED;
} else if (Object::cast_to<Container>(parent_node)) {
return LayoutMode::LAYOUT_MODE_CONTAINER;
}
// Otherwise fallback on the position mode.
return LayoutMode::LAYOUT_MODE_POSITION;
}
void Control::_set_anchors_layout_preset(int p_preset) {
bool list_changed = false;
if (get_meta("_edit_layout_mode", LayoutMode::LAYOUT_MODE_ANCHORS).operator int() != LayoutMode::LAYOUT_MODE_ANCHORS) {
if (data.stored_layout_mode != LayoutMode::LAYOUT_MODE_ANCHORS) {
list_changed = true;
set_meta("_edit_layout_mode", LayoutMode::LAYOUT_MODE_ANCHORS);
data.stored_layout_mode = LayoutMode::LAYOUT_MODE_ANCHORS;
}
if (p_preset == -1) {
if (!get_meta("_edit_use_custom_anchors", false)) {
set_meta("_edit_use_custom_anchors", true);
if (!data.stored_use_custom_anchors) {
data.stored_use_custom_anchors = true;
notify_property_list_changed();
}
return; // Keep settings as is.
}
if (get_meta("_edit_use_custom_anchors", true)) {
if (data.stored_use_custom_anchors) {
list_changed = true;
remove_meta("_edit_use_custom_anchors");
data.stored_use_custom_anchors = false;
}
LayoutPreset preset = (LayoutPreset)p_preset;
@ -899,7 +920,7 @@ void Control::_set_anchors_layout_preset(int p_preset) {
int Control::_get_anchors_layout_preset() const {
// If the custom preset was selected by user, use it.
if ((bool)get_meta("_edit_use_custom_anchors", false)) {
if (data.stored_use_custom_anchors) {
return -1;
}
@ -3391,7 +3412,7 @@ void Control::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_contents"), "set_clip_contents", "is_clipping_contents");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "custom_minimum_size", PROPERTY_HINT_NONE, "suffix:px"), "set_custom_minimum_size", "get_custom_minimum_size");
ADD_PROPERTY(PropertyInfo(Variant::INT, "layout_direction", PROPERTY_HINT_ENUM, "Inherited,Locale,Left-to-Right,Right-to-Left"), "set_layout_direction", "get_layout_direction");
ADD_PROPERTY(PropertyInfo(Variant::INT, "layout_mode", PROPERTY_HINT_ENUM, "Position,Anchors,Container,Uncontrolled", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_layout_mode", "_get_layout_mode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "layout_mode", PROPERTY_HINT_ENUM, "Position,Anchors,Container,Uncontrolled", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL), "_set_layout_mode", "_get_layout_mode");
ADD_PROPERTY_DEFAULT("layout_mode", LayoutMode::LAYOUT_MODE_POSITION);
const String anchors_presets_options = "Custom:-1,PresetFullRect:15,"
@ -3399,7 +3420,7 @@ void Control::_bind_methods() {
"PresetCenterLeft:4,PresetCenterTop:5,PresetCenterRight:6,PresetCenterBottom:7,PresetCenter:8,"
"PresetLeftWide:9,PresetTopWide:10,PresetRightWide:11,PresetBottomWide:12,PresetVCenterWide:13,PresetHCenterWide:14";
ADD_PROPERTY(PropertyInfo(Variant::INT, "anchors_preset", PROPERTY_HINT_ENUM, anchors_presets_options, PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_anchors_layout_preset", "_get_anchors_layout_preset");
ADD_PROPERTY(PropertyInfo(Variant::INT, "anchors_preset", PROPERTY_HINT_ENUM, anchors_presets_options, PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL), "_set_anchors_layout_preset", "_get_anchors_layout_preset");
ADD_PROPERTY_DEFAULT("anchors_preset", -1);
ADD_SUBGROUP_INDENT("Anchor Points", "anchor_", 1);

View File

@ -170,6 +170,9 @@ private:
// Positioning and sizing.
LayoutMode stored_layout_mode = LayoutMode::LAYOUT_MODE_POSITION;
bool stored_use_custom_anchors = false;
real_t offset[4] = { 0.0, 0.0, 0.0, 0.0 };
real_t anchor[4] = { ANCHOR_BEGIN, ANCHOR_BEGIN, ANCHOR_BEGIN, ANCHOR_BEGIN };
FocusMode focus_mode = FOCUS_NONE;
@ -275,6 +278,7 @@ private:
void _set_layout_mode(LayoutMode p_mode);
LayoutMode _get_layout_mode() const;
LayoutMode _get_default_layout_mode() const;
void _set_anchors_layout_preset(int p_preset);
int _get_anchors_layout_preset() const;
@ -319,6 +323,9 @@ protected:
void _get_property_list(List<PropertyInfo> *p_list) const;
virtual void _validate_property(PropertyInfo &property) const override;
bool _property_can_revert(const StringName &p_name) const;
bool _property_get_revert(const StringName &p_name, Variant &r_property) const;
// Internationalization.
virtual Array structured_text_parser(TextServer::StructuredTextParser p_parser_type, const Array &p_args, const String &p_text) const;