Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Add AspectRatioContainer class #45129

Merged
merged 1 commit into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions doc/classes/AspectRatioContainer.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="AspectRatioContainer" inherits="Container" version="4.0">
<brief_description>
Container that preserves its child controls' aspect ratio.
</brief_description>
<description>
Arranges child controls in a way to preserve their aspect ratio automatically whenever the container is resized. Solves the problem where the container size is dynamic and the contents' size needs to adjust accordingly without losing proportions.
</description>
<tutorials>
</tutorials>
<methods>
</methods>
<members>
<member name="alignment_horizontal" type="int" setter="set_alignment_horizontal" getter="get_alignment_horizontal" enum="AspectRatioContainer.AlignMode" default="1">
Specifies the horizontal relative position of child controls.
</member>
<member name="alignment_vertical" type="int" setter="set_alignment_vertical" getter="get_alignment_vertical" enum="AspectRatioContainer.AlignMode" default="1">
Specifies the vertical relative position of child controls.
</member>
<member name="ratio" type="float" setter="set_ratio" getter="get_ratio" default="1.0">
The aspect ratio to enforce on child controls. This is the width divided by the height. The ratio depends on the [member stretch_mode].
</member>
<member name="stretch_mode" type="int" setter="set_stretch_mode" getter="get_stretch_mode" enum="AspectRatioContainer.StretchMode" default="2">
The stretch mode used to align child controls.
</member>
</members>
<constants>
<constant name="STRETCH_WIDTH_CONTROLS_HEIGHT" value="0" enum="StretchMode">
The height of child controls is automatically adjusted based on the width of the container.
</constant>
<constant name="STRETCH_HEIGHT_CONTROLS_WIDTH" value="1" enum="StretchMode">
The width of child controls is automatically adjusted based on the height of the container.
</constant>
<constant name="STRETCH_FIT" value="2" enum="StretchMode">
The bounding rectangle of child controls is automatically adjusted to fit inside the container while keeping the aspect ratio.
</constant>
<constant name="STRETCH_COVER" value="3" enum="StretchMode">
The width and height of child controls is automatically adjusted to make their bounding rectangle cover the entire area of the container while keeping the aspect ratio.
When the bounding rectangle of child controls exceed the container's size and [member Control.rect_clip_content] is enabled, this allows to show only the container's area restricted by its own bounding rectangle.
</constant>
<constant name="ALIGN_BEGIN" value="0" enum="AlignMode">
Aligns child controls with the beginning (left or top) of the container.
</constant>
<constant name="ALIGN_CENTER" value="1" enum="AlignMode">
Aligns child controls with the center of the container.
</constant>
<constant name="ALIGN_END" value="2" enum="AlignMode">
Aligns child controls with the end (right or bottom) of the container.
</constant>
</constants>
</class>
1 change: 1 addition & 0 deletions editor/icons/icon_aspect_ratio_container.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
167 changes: 167 additions & 0 deletions scene/gui/aspect_ratio_container.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*************************************************************************/
/* aspect_ratio_container.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/

#include "aspect_ratio_container.h"

Size2 AspectRatioContainer::get_minimum_size() const {
Size2 ms;
for (int i = 0; i < get_child_count(); i++) {
Control *c = Object::cast_to<Control>(get_child(i));
if (!c) {
continue;
}
if (c->is_set_as_toplevel()) {
continue;
}
if (!c->is_visible()) {
continue;
}
Size2 minsize = c->get_combined_minimum_size();
ms.width = MAX(ms.width, minsize.width);
ms.height = MAX(ms.height, minsize.height);
}
return ms;
}

void AspectRatioContainer::set_ratio(float p_ratio) {
ratio = p_ratio;
queue_sort();
}

void AspectRatioContainer::set_stretch_mode(StretchMode p_mode) {
stretch_mode = p_mode;
queue_sort();
}

void AspectRatioContainer::set_alignment_horizontal(AlignMode p_alignment_horizontal) {
alignment_horizontal = p_alignment_horizontal;
queue_sort();
}

void AspectRatioContainer::set_alignment_vertical(AlignMode p_alignment_vertical) {
alignment_vertical = p_alignment_vertical;
queue_sort();
}

void AspectRatioContainer::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_SORT_CHILDREN: {
Size2 size = get_size();
for (int i = 0; i < get_child_count(); i++) {
Control *c = Object::cast_to<Control>(get_child(i));
if (!c) {
continue;
}
if (c->is_set_as_toplevel()) {
continue;
}
Size2 child_minsize = c->get_combined_minimum_size();
Size2 child_size = Size2(ratio, 1.0);
float scale_factor = 1.0;

switch (stretch_mode) {
case STRETCH_WIDTH_CONTROLS_HEIGHT: {
scale_factor = size.x / child_size.x;
} break;
case STRETCH_HEIGHT_CONTROLS_WIDTH: {
scale_factor = size.y / child_size.y;
} break;
case STRETCH_FIT: {
scale_factor = MIN(size.x / child_size.x, size.y / child_size.y);
} break;
case STRETCH_COVER: {
scale_factor = MAX(size.x / child_size.x, size.y / child_size.y);
} break;
}
child_size *= scale_factor;
child_size.x = MAX(child_size.x, child_minsize.x);
child_size.y = MAX(child_size.y, child_minsize.y);

float align_x = 0.5;
switch (alignment_horizontal) {
case ALIGN_BEGIN: {
align_x = 0.0;
} break;
case ALIGN_CENTER: {
align_x = 0.5;
} break;
case ALIGN_END: {
align_x = 1.0;
} break;
}
float align_y = 0.5;
switch (alignment_vertical) {
case ALIGN_BEGIN: {
align_y = 0.0;
} break;
case ALIGN_CENTER: {
align_y = 0.5;
} break;
case ALIGN_END: {
align_y = 1.0;
} break;
}
Vector2 offset = (size - child_size) * Vector2(align_x, align_y);

fit_child_in_rect(c, Rect2(offset, child_size));
}
} break;
}
}

void AspectRatioContainer::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_ratio", "ratio"), &AspectRatioContainer::set_ratio);
ClassDB::bind_method(D_METHOD("get_ratio"), &AspectRatioContainer::get_ratio);

ClassDB::bind_method(D_METHOD("set_stretch_mode", "stretch_mode"), &AspectRatioContainer::set_stretch_mode);
ClassDB::bind_method(D_METHOD("get_stretch_mode"), &AspectRatioContainer::get_stretch_mode);

ClassDB::bind_method(D_METHOD("set_alignment_horizontal", "alignment_horizontal"), &AspectRatioContainer::set_alignment_horizontal);
ClassDB::bind_method(D_METHOD("get_alignment_horizontal"), &AspectRatioContainer::get_alignment_horizontal);

ClassDB::bind_method(D_METHOD("set_alignment_vertical", "alignment_vertical"), &AspectRatioContainer::set_alignment_vertical);
ClassDB::bind_method(D_METHOD("get_alignment_vertical"), &AspectRatioContainer::get_alignment_vertical);

ADD_PROPERTY(PropertyInfo(Variant::REAL, "ratio"), "set_ratio", "get_ratio");
ADD_PROPERTY(PropertyInfo(Variant::INT, "stretch_mode", PROPERTY_HINT_ENUM, "Width controls height,Height controls width,Fit,Cover"), "set_stretch_mode", "get_stretch_mode");

ADD_GROUP("Alignment", "alignment_");
ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment_horizontal", PROPERTY_HINT_ENUM, "Begin,Center,End"), "set_alignment_horizontal", "get_alignment_horizontal");
ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment_vertical", PROPERTY_HINT_ENUM, "Begin,Center,End"), "set_alignment_vertical", "get_alignment_vertical");

BIND_ENUM_CONSTANT(STRETCH_WIDTH_CONTROLS_HEIGHT);
BIND_ENUM_CONSTANT(STRETCH_HEIGHT_CONTROLS_WIDTH);
BIND_ENUM_CONSTANT(STRETCH_FIT);
BIND_ENUM_CONSTANT(STRETCH_COVER);

BIND_ENUM_CONSTANT(ALIGN_BEGIN);
BIND_ENUM_CONSTANT(ALIGN_CENTER);
BIND_ENUM_CONSTANT(ALIGN_END);
}
80 changes: 80 additions & 0 deletions scene/gui/aspect_ratio_container.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*************************************************************************/
/* aspect_ratio_container.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/

#ifndef ASPECT_RATIO_CONTAINER_H
#define ASPECT_RATIO_CONTAINER_H

#include "scene/gui/container.h"

class AspectRatioContainer : public Container {
GDCLASS(AspectRatioContainer, Container);

protected:
void _notification(int p_what);
static void _bind_methods();
virtual Size2 get_minimum_size() const;

public:
enum StretchMode {
STRETCH_WIDTH_CONTROLS_HEIGHT,
STRETCH_HEIGHT_CONTROLS_WIDTH,
STRETCH_FIT,
STRETCH_COVER,
};
enum AlignMode {
ALIGN_BEGIN,
ALIGN_CENTER,
ALIGN_END,
};

private:
float ratio = 1.0;
StretchMode stretch_mode = STRETCH_FIT;
AlignMode alignment_horizontal = ALIGN_CENTER;
AlignMode alignment_vertical = ALIGN_CENTER;

public:
void set_ratio(float p_ratio);
float get_ratio() const { return ratio; }

void set_stretch_mode(StretchMode p_mode);
StretchMode get_stretch_mode() const { return stretch_mode; }

void set_alignment_horizontal(AlignMode p_alignment_horizontal);
AlignMode get_alignment_horizontal() const { return alignment_horizontal; }

void set_alignment_vertical(AlignMode p_alignment_vertical);
AlignMode get_alignment_vertical() const { return alignment_vertical; }
};

VARIANT_ENUM_CAST(AspectRatioContainer::StretchMode);
VARIANT_ENUM_CAST(AspectRatioContainer::AlignMode);

#endif // ASPECT_RATIO_CONTAINER_H
2 changes: 2 additions & 0 deletions scene/register_scene_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
#include "scene/animation/root_motion_view.h"
#include "scene/animation/tween.h"
#include "scene/audio/audio_stream_player.h"
#include "scene/gui/aspect_ratio_container.h"
#include "scene/gui/box_container.h"
#include "scene/gui/button.h"
#include "scene/gui/center_container.h"
Expand Down Expand Up @@ -303,6 +304,7 @@ void register_scene_types() {
ClassDB::register_class<ColorRect>();
ClassDB::register_class<NinePatchRect>();
ClassDB::register_class<ReferenceRect>();
ClassDB::register_class<AspectRatioContainer>();
ClassDB::register_class<TabContainer>();
ClassDB::register_class<Tabs>();
ClassDB::register_virtual_class<Separator>();
Expand Down