summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/math/color.h4
-rw-r--r--core/variant/variant_call.cpp1
-rw-r--r--doc/classes/Color.xml7
-rw-r--r--doc/classes/Window.xml2
-rw-r--r--editor/editor_settings.cpp2
-rw-r--r--editor/import/resource_importer_texture.cpp6
-rw-r--r--editor/plugins/editor_preview_plugins.cpp4
-rw-r--r--main/main.cpp2
-rw-r--r--scene/main/window.cpp23
-rw-r--r--scene/main/window.h4
10 files changed, 50 insertions, 5 deletions
diff --git a/core/math/color.h b/core/math/color.h
index ffd0fd8f6e..815dd98d96 100644
--- a/core/math/color.h
+++ b/core/math/color.h
@@ -94,6 +94,10 @@ struct Color {
void invert();
Color inverted() const;
+ _FORCE_INLINE_ float get_luminance() const {
+ return 0.2126 * r + 0.7152 * g + 0.0722 * b;
+ }
+
_FORCE_INLINE_ Color lerp(const Color &p_to, float p_weight) const {
Color res = *this;
diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp
index 51b9119933..d2bda34818 100644
--- a/core/variant/variant_call.cpp
+++ b/core/variant/variant_call.cpp
@@ -1644,6 +1644,7 @@ static void _register_variant_builtin_methods() {
bind_method(Color, lightened, sarray("amount"), varray());
bind_method(Color, darkened, sarray("amount"), varray());
bind_method(Color, blend, sarray("over"), varray());
+ bind_method(Color, get_luminance, sarray(), varray());
bind_method(Color, is_equal_approx, sarray("to"), varray());
diff --git a/doc/classes/Color.xml b/doc/classes/Color.xml
index 22fb853b40..21d17969e2 100644
--- a/doc/classes/Color.xml
+++ b/doc/classes/Color.xml
@@ -178,6 +178,13 @@
<description>
</description>
</method>
+ <method name="get_luminance" qualifiers="const">
+ <return type="float" />
+ <description>
+ Returns the luminance of the color in the [code][0.0, 1.0][/code] range.
+ This is useful when determining light or dark color. Colors with a luminance smaller than 0.5 can be generally considered dark.
+ </description>
+ </method>
<method name="get_named_color" qualifiers="static">
<return type="Color" />
<argument index="0" name="idx" type="int" />
diff --git a/doc/classes/Window.xml b/doc/classes/Window.xml
index c7ca6a20be..75161d3c5b 100644
--- a/doc/classes/Window.xml
+++ b/doc/classes/Window.xml
@@ -287,6 +287,8 @@
</member>
<member name="content_scale_aspect" type="int" setter="set_content_scale_aspect" getter="get_content_scale_aspect" enum="Window.ContentScaleAspect" default="0">
</member>
+ <member name="content_scale_factor" type="float" setter="set_content_scale_factor" getter="get_content_scale_factor" default="1.0">
+ </member>
<member name="content_scale_mode" type="int" setter="set_content_scale_mode" getter="get_content_scale_mode" enum="Window.ContentScaleMode" default="0">
</member>
<member name="content_scale_size" type="Vector2i" setter="set_content_scale_size" getter="get_content_scale_size" default="Vector2i(0, 0)">
diff --git a/editor/editor_settings.cpp b/editor/editor_settings.cpp
index 68ac122e63..1ac1d6f048 100644
--- a/editor/editor_settings.cpp
+++ b/editor/editor_settings.cpp
@@ -1190,7 +1190,7 @@ bool EditorSettings::is_dark_theme() {
int LIGHT_COLOR = 2;
Color base_color = get("interface/theme/base_color");
int icon_font_color_setting = get("interface/theme/icon_and_font_color");
- return (icon_font_color_setting == AUTO_COLOR && ((base_color.r + base_color.g + base_color.b) / 3.0) < 0.5) || icon_font_color_setting == LIGHT_COLOR;
+ return (icon_font_color_setting == AUTO_COLOR && base_color.get_luminance() < 0.5) || icon_font_color_setting == LIGHT_COLOR;
}
void EditorSettings::list_text_editor_themes() {
diff --git a/editor/import/resource_importer_texture.cpp b/editor/import/resource_importer_texture.cpp
index b1fa2eda28..5ca61e4c39 100644
--- a/editor/import/resource_importer_texture.cpp
+++ b/editor/import/resource_importer_texture.cpp
@@ -37,6 +37,8 @@
#include "editor/editor_node.h"
void ResourceImporterTexture::_texture_reimport_roughness(const Ref<StreamTexture2D> &p_tex, const String &p_normal_path, RS::TextureDetectRoughnessChannel p_channel) {
+ ERR_FAIL_COND(p_tex.is_null());
+
MutexLock lock(singleton->mutex);
StringName path = p_tex->get_path();
@@ -51,6 +53,8 @@ void ResourceImporterTexture::_texture_reimport_roughness(const Ref<StreamTextur
}
void ResourceImporterTexture::_texture_reimport_3d(const Ref<StreamTexture2D> &p_tex) {
+ ERR_FAIL_COND(p_tex.is_null());
+
MutexLock lock(singleton->mutex);
StringName path = p_tex->get_path();
@@ -63,6 +67,8 @@ void ResourceImporterTexture::_texture_reimport_3d(const Ref<StreamTexture2D> &p
}
void ResourceImporterTexture::_texture_reimport_normal(const Ref<StreamTexture2D> &p_tex) {
+ ERR_FAIL_COND(p_tex.is_null());
+
MutexLock lock(singleton->mutex);
StringName path = p_tex->get_path();
diff --git a/editor/plugins/editor_preview_plugins.cpp b/editor/plugins/editor_preview_plugins.cpp
index 871b72d9c3..f7ec0f8c5c 100644
--- a/editor/plugins/editor_preview_plugins.cpp
+++ b/editor/plugins/editor_preview_plugins.cpp
@@ -852,7 +852,9 @@ Ref<Texture2D> EditorFontPreviewPlugin::generate_from_path(const String &p_path,
Ref<Font> font = sampled_font;
- font->draw_string(canvas_item, pos, sample, HORIZONTAL_ALIGNMENT_LEFT, -1.f, 50, Color(1, 1, 1));
+ const Color c = GLOBAL_GET("rendering/environment/defaults/default_clear_color");
+ const float fg = c.get_luminance() < 0.5 ? 1.0 : 0.0;
+ font->draw_string(canvas_item, pos, sample, HORIZONTAL_ALIGNMENT_LEFT, -1.f, 50, Color(fg, fg, fg));
RS::get_singleton()->connect(SNAME("frame_pre_draw"), callable_mp(const_cast<EditorFontPreviewPlugin *>(this), &EditorFontPreviewPlugin::_generate_frame_started), Vector<Variant>(), Object::CONNECT_ONESHOT);
diff --git a/main/main.cpp b/main/main.cpp
index 21e5935adc..805c8c2c84 100644
--- a/main/main.cpp
+++ b/main/main.cpp
@@ -2321,6 +2321,7 @@ bool Main::start() {
String stretch_aspect = GLOBAL_DEF_BASIC("display/window/stretch/aspect", "keep");
Size2i stretch_size = Size2i(GLOBAL_DEF_BASIC("display/window/size/width", 0),
GLOBAL_DEF_BASIC("display/window/size/height", 0));
+ real_t stretch_scale = GLOBAL_DEF_BASIC("display/window/stretch/scale", 1.0);
Window::ContentScaleMode cs_sm = Window::CONTENT_SCALE_MODE_DISABLED;
if (stretch_mode == "canvas_items") {
@@ -2343,6 +2344,7 @@ bool Main::start() {
sml->get_root()->set_content_scale_mode(cs_sm);
sml->get_root()->set_content_scale_aspect(cs_aspect);
sml->get_root()->set_content_scale_size(stretch_size);
+ sml->get_root()->set_content_scale_factor(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));
diff --git a/scene/main/window.cpp b/scene/main/window.cpp
index 20f8b30dc6..7784fa1094 100644
--- a/scene/main/window.cpp
+++ b/scene/main/window.cpp
@@ -560,9 +560,12 @@ void Window::_update_viewport_size() {
float font_oversampling = 1.0;
if (content_scale_mode == CONTENT_SCALE_MODE_DISABLED || content_scale_size.x == 0 || content_scale_size.y == 0) {
- stretch_transform = Transform2D();
+ font_oversampling = content_scale_factor;
final_size = size;
+ final_size_override = Size2(size) / content_scale_factor;
+ stretch_transform = Transform2D();
+ stretch_transform.scale(Size2(content_scale_factor, content_scale_factor));
} else {
//actual screen video mode
Size2 video_mode = size;
@@ -634,9 +637,9 @@ void Window::_update_viewport_size() {
} break;
case CONTENT_SCALE_MODE_CANVAS_ITEMS: {
final_size = screen_size;
- final_size_override = viewport_size;
+ final_size_override = viewport_size / content_scale_factor;
attach_to_screen_rect = Rect2(margin, screen_size);
- font_oversampling = screen_size.x / viewport_size.x;
+ font_oversampling = (screen_size.x / viewport_size.x) * content_scale_factor;
Size2 scale = Vector2(screen_size) / Vector2(final_size_override);
stretch_transform.scale(scale);
@@ -825,6 +828,16 @@ Window::ContentScaleAspect Window::get_content_scale_aspect() const {
return content_scale_aspect;
}
+void Window::set_content_scale_factor(real_t p_factor) {
+ ERR_FAIL_COND(p_factor <= 0);
+ content_scale_factor = p_factor;
+ _update_viewport_size();
+}
+
+real_t Window::get_content_scale_factor() const {
+ return content_scale_factor;
+}
+
void Window::set_use_font_oversampling(bool p_oversampling) {
if (is_inside_tree() && window_id != DisplayServer::MAIN_WINDOW_ID) {
ERR_FAIL_MSG("Only the root window can set and use font oversampling.");
@@ -1468,6 +1481,9 @@ void Window::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_content_scale_aspect", "aspect"), &Window::set_content_scale_aspect);
ClassDB::bind_method(D_METHOD("get_content_scale_aspect"), &Window::get_content_scale_aspect);
+ ClassDB::bind_method(D_METHOD("set_content_scale_factor", "factor"), &Window::set_content_scale_factor);
+ ClassDB::bind_method(D_METHOD("get_content_scale_factor"), &Window::get_content_scale_factor);
+
ClassDB::bind_method(D_METHOD("set_use_font_oversampling", "enable"), &Window::set_use_font_oversampling);
ClassDB::bind_method(D_METHOD("is_using_font_oversampling"), &Window::is_using_font_oversampling);
@@ -1539,6 +1555,7 @@ void Window::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "content_scale_size"), "set_content_scale_size", "get_content_scale_size");
ADD_PROPERTY(PropertyInfo(Variant::INT, "content_scale_mode", PROPERTY_HINT_ENUM, "Disabled,Canvas Items,Viewport"), "set_content_scale_mode", "get_content_scale_mode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "content_scale_aspect", PROPERTY_HINT_ENUM, "Ignore,Keep,Keep Width,Keep Height,Expand"), "set_content_scale_aspect", "get_content_scale_aspect");
+ ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "content_scale_factor"), "set_content_scale_factor", "get_content_scale_factor");
ADD_GROUP("Theme", "theme_");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "theme", PROPERTY_HINT_RESOURCE_TYPE, "Theme"), "set_theme", "get_theme");
diff --git a/scene/main/window.h b/scene/main/window.h
index 0b1075ff76..f511f6df29 100644
--- a/scene/main/window.h
+++ b/scene/main/window.h
@@ -112,6 +112,7 @@ private:
Size2i content_scale_size;
ContentScaleMode content_scale_mode = CONTENT_SCALE_MODE_DISABLED;
ContentScaleAspect content_scale_aspect = CONTENT_SCALE_ASPECT_IGNORE;
+ real_t content_scale_factor = 1.0;
void _make_window();
void _clear_window();
@@ -230,6 +231,9 @@ public:
void set_content_scale_aspect(ContentScaleAspect p_aspect);
ContentScaleAspect get_content_scale_aspect() const;
+ void set_content_scale_factor(real_t p_factor);
+ real_t get_content_scale_factor() const;
+
void set_use_font_oversampling(bool p_oversampling);
bool is_using_font_oversampling() const;