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 a 2D scale factor property #52137

Merged
merged 1 commit into from
Oct 5, 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
4 changes: 2 additions & 2 deletions doc/classes/SceneTree.xml
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,9 @@
<argument index="0" name="mode" type="int" enum="SceneTree.StretchMode" />
<argument index="1" name="aspect" type="int" enum="SceneTree.StretchAspect" />
<argument index="2" name="minsize" type="Vector2" />
<argument index="3" name="shrink" type="float" default="1" />
<argument index="3" name="scale" type="float" default="1" />
<description>
Configures screen stretching to the given [enum StretchMode], [enum StretchAspect], minimum size and [code]shrink[/code] ratio.
Configures screen stretching to the given [enum StretchMode], [enum StretchAspect], minimum size and [code]scale[/code].
</description>
</method>
</methods>
Expand Down
7 changes: 4 additions & 3 deletions main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1843,7 +1843,8 @@ bool Main::start() {
String stretch_mode = GLOBAL_DEF("display/window/stretch/mode", "disabled");
String stretch_aspect = GLOBAL_DEF("display/window/stretch/aspect", "ignore");
Size2i stretch_size = Size2(GLOBAL_DEF("display/window/size/width", 0), GLOBAL_DEF("display/window/size/height", 0));
real_t stretch_shrink = GLOBAL_DEF("display/window/stretch/shrink", 1.0);
// out of compatability reasons stretch_scale is called shrink when exposed to the user.
real_t stretch_scale = GLOBAL_DEF("display/window/stretch/shrink", 1.0);

SceneTree::StretchMode sml_sm = SceneTree::STRETCH_MODE_DISABLED;
if (stretch_mode == "2d") {
Expand All @@ -1863,7 +1864,7 @@ bool Main::start() {
sml_aspect = SceneTree::STRETCH_ASPECT_EXPAND;
}

sml->set_screen_stretch(sml_sm, sml_aspect, stretch_size, stretch_shrink);
sml->set_screen_stretch(sml_sm, sml_aspect, stretch_size, stretch_scale);

sml->set_auto_accept_quit(GLOBAL_DEF("application/config/auto_accept_quit", true));
sml->set_quit_on_go_back(GLOBAL_DEF("application/config/quit_on_go_back", true));
Expand Down Expand Up @@ -1908,7 +1909,7 @@ bool Main::start() {
GLOBAL_DEF("display/window/stretch/aspect", "ignore");
ProjectSettings::get_singleton()->set_custom_property_info("display/window/stretch/aspect", PropertyInfo(Variant::STRING, "display/window/stretch/aspect", PROPERTY_HINT_ENUM, "ignore,keep,keep_width,keep_height,expand"));
GLOBAL_DEF("display/window/stretch/shrink", 1.0);
ProjectSettings::get_singleton()->set_custom_property_info("display/window/stretch/shrink", PropertyInfo(Variant::REAL, "display/window/stretch/shrink", PROPERTY_HINT_RANGE, "1.0,8.0,0.1"));
ProjectSettings::get_singleton()->set_custom_property_info("display/window/stretch/shrink", PropertyInfo(Variant::REAL, "display/window/stretch/shrink", PROPERTY_HINT_RANGE, "0.1,8,0.01,or_greater"));
sml->set_auto_accept_quit(GLOBAL_DEF("application/config/auto_accept_quit", true));
sml->set_quit_on_go_back(GLOBAL_DEF("application/config/quit_on_go_back", true));
GLOBAL_DEF("gui/common/snap_controls_to_pixels", true);
Expand Down
25 changes: 12 additions & 13 deletions scene/main/scene_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1106,11 +1106,11 @@ int SceneTree::get_node_count() const {

void SceneTree::_update_root_rect() {
if (stretch_mode == STRETCH_MODE_DISABLED) {
_update_font_oversampling(1.0);
root->set_size((last_screen_size / stretch_shrink).floor());
_update_font_oversampling(stretch_scale);
root->set_size(last_screen_size.floor());
root->set_attach_to_screen_rect(Rect2(Point2(), last_screen_size));
root->set_size_override_stretch(false);
root->set_size_override(false, Size2());
root->set_size_override_stretch(true);
root->set_size_override(true, (last_screen_size / stretch_scale).floor());
root->update_canvas_items();
return; //user will take care
}
Expand Down Expand Up @@ -1185,20 +1185,19 @@ void SceneTree::_update_root_rect() {
switch (stretch_mode) {
case STRETCH_MODE_DISABLED: {
// Already handled above
_update_font_oversampling(1.0);
} break;
case STRETCH_MODE_2D: {
_update_font_oversampling(screen_size.x / viewport_size.x); //screen / viewport radio drives oversampling
root->set_size((screen_size / stretch_shrink).floor());
_update_font_oversampling((screen_size.x / viewport_size.x) * stretch_scale); //screen / viewport ratio drives oversampling
root->set_size(screen_size.floor());
root->set_attach_to_screen_rect(Rect2(margin, screen_size));
root->set_size_override_stretch(true);
root->set_size_override(true, (viewport_size / stretch_shrink).floor());
root->set_size_override(true, (viewport_size / stretch_scale).floor());
root->update_canvas_items(); //force them to update just in case

} break;
case STRETCH_MODE_VIEWPORT: {
_update_font_oversampling(1.0);
root->set_size((viewport_size / stretch_shrink).floor());
root->set_size((viewport_size / stretch_scale).floor());
root->set_attach_to_screen_rect(Rect2(margin, screen_size));
root->set_size_override_stretch(false);
root->set_size_override(false, Size2());
Expand All @@ -1212,11 +1211,11 @@ void SceneTree::_update_root_rect() {
}
}

void SceneTree::set_screen_stretch(StretchMode p_mode, StretchAspect p_aspect, const Size2 &p_minsize, real_t p_shrink) {
void SceneTree::set_screen_stretch(StretchMode p_mode, StretchAspect p_aspect, const Size2 &p_minsize, real_t p_scale) {
stretch_mode = p_mode;
stretch_aspect = p_aspect;
stretch_min = p_minsize;
stretch_shrink = p_shrink;
stretch_scale = p_scale;
_update_root_rect();
}

Expand Down Expand Up @@ -1826,7 +1825,7 @@ void SceneTree::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_frame"), &SceneTree::get_frame);
ClassDB::bind_method(D_METHOD("quit", "exit_code"), &SceneTree::quit, DEFVAL(-1));

ClassDB::bind_method(D_METHOD("set_screen_stretch", "mode", "aspect", "minsize", "shrink"), &SceneTree::set_screen_stretch, DEFVAL(1));
ClassDB::bind_method(D_METHOD("set_screen_stretch", "mode", "aspect", "minsize", "scale"), &SceneTree::set_screen_stretch, DEFVAL(1));

ClassDB::bind_method(D_METHOD("queue_delete", "obj"), &SceneTree::queue_delete);

Expand Down Expand Up @@ -2111,7 +2110,7 @@ SceneTree::SceneTree() {

stretch_mode = STRETCH_MODE_DISABLED;
stretch_aspect = STRETCH_ASPECT_IGNORE;
stretch_shrink = 1;
stretch_scale = 1.0;

last_screen_size = Size2(OS::get_singleton()->get_window_size().width, OS::get_singleton()->get_window_size().height);
_update_root_rect();
Expand Down
4 changes: 2 additions & 2 deletions scene/main/scene_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class SceneTree : public MainLoop {
StretchMode stretch_mode;
StretchAspect stretch_aspect;
Size2i stretch_min;
real_t stretch_shrink;
real_t stretch_scale;

void _update_font_oversampling(float p_ratio);
void _update_root_rect();
Expand Down Expand Up @@ -362,7 +362,7 @@ class SceneTree : public MainLoop {
void get_nodes_in_group(const StringName &p_group, List<Node *> *p_list);
bool has_group(const StringName &p_identifier) const;

void set_screen_stretch(StretchMode p_mode, StretchAspect p_aspect, const Size2 &p_minsize, real_t p_shrink = 1);
void set_screen_stretch(StretchMode p_mode, StretchAspect p_aspect, const Size2 &p_minsize, real_t p_scale = 1.0);

void set_use_font_oversampling(bool p_oversampling);
bool is_using_font_oversampling() const;
Expand Down