Skip to content

Commit

Permalink
Merge pull request #53341 from pycbouh/gui-editor-scale-encapsulation
Browse files Browse the repository at this point in the history
  • Loading branch information
akien-mga authored Oct 4, 2021
2 parents 2a09e11 + bdbb7b3 commit 865b62b
Show file tree
Hide file tree
Showing 22 changed files with 965 additions and 690 deletions.
21 changes: 21 additions & 0 deletions doc/classes/Control.xml
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,27 @@
See [method get_theme_color] for details.
</description>
</method>
<method name="get_theme_default_base_scale" qualifiers="const">
<return type="float" />
<description>
Returns the default base scale value from the first matching [Theme] in the tree if that [Theme] has a valid [member Theme.default_base_scale] value.
See [method get_theme_color] for details.
</description>
</method>
<method name="get_theme_default_font" qualifiers="const">
<return type="Font" />
<description>
Returns the default font from the first matching [Theme] in the tree if that [Theme] has a valid [member Theme.default_font] value.
See [method get_theme_color] for details.
</description>
</method>
<method name="get_theme_default_font_size" qualifiers="const">
<return type="int" />
<description>
Returns the default font size value from the first matching [Theme] in the tree if that [Theme] has a valid [member Theme.default_font_size] value.
See [method get_theme_color] for details.
</description>
</method>
<method name="get_theme_font" qualifiers="const">
<return type="Font" />
<argument index="0" name="name" type="StringName" />
Expand Down
28 changes: 26 additions & 2 deletions doc/classes/Theme.xml
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,24 @@
Returns [code]false[/code] if the theme does not have [code]theme_type[/code].
</description>
</method>
<method name="has_default_base_scale" qualifiers="const">
<return type="bool" />
<description>
Returns [code]true[/code] if this theme has a valid [member default_base_scale] value.
</description>
</method>
<method name="has_default_font" qualifiers="const">
<return type="bool" />
<description>
Returns [code]true[/code] if this theme has a valid [member default_font] value.
</description>
</method>
<method name="has_default_font_size" qualifiers="const">
<return type="bool" />
<description>
Returns [code]true[/code] if this theme has a valid [member default_font_size] value.
</description>
</method>
<method name="has_font" qualifiers="const">
<return type="bool" />
<argument index="0" name="name" type="StringName" />
Expand Down Expand Up @@ -484,11 +502,17 @@
</method>
</methods>
<members>
<member name="default_base_scale" type="float" setter="set_default_base_scale" getter="get_default_base_scale" default="0.0">
The default base scale factor of this [Theme] resource. Used by some controls to scale their visual properties based on a global scale factor. If this value is set to [code]0.0[/code], the global scale factor is used.
Use [method has_default_base_scale] to check if this value is valid.
</member>
<member name="default_font" type="Font" setter="set_default_font" getter="get_default_font">
The theme's default font.
The default font of this [Theme] resource. Used as a fallback value for font items defined in this theme, but having invalid values. If this value is also invalid, the global default value is used.
Use [method has_default_font] to check if this value is valid.
</member>
<member name="default_font_size" type="int" setter="set_default_font_size" getter="get_default_font_size" default="-1">
The theme's default font size. Set to [code]-1[/code] to ignore and use global default.
The default font size of this [Theme] resource. Used as a fallback value for font size items defined in this theme, but having invalid values. If this value is set to [code]-1[/code], the global default value is used.
Use [method has_default_font_size] to check if this value is valid.
</member>
</members>
<constants>
Expand Down
15 changes: 15 additions & 0 deletions doc/classes/Window.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@
<description>
</description>
</method>
<method name="get_theme_default_base_scale" qualifiers="const">
<return type="float" />
<description>
</description>
</method>
<method name="get_theme_default_font" qualifiers="const">
<return type="Font" />
<description>
</description>
</method>
<method name="get_theme_default_font_size" qualifiers="const">
<return type="int" />
<description>
</description>
</method>
<method name="get_theme_font" qualifiers="const">
<return type="Font" />
<argument index="0" name="name" type="StringName" />
Expand Down
5 changes: 4 additions & 1 deletion editor/editor_themes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ void editor_register_and_generate_icons(Ref<Theme> p_theme, bool p_dark_theme =
Ref<Theme> create_editor_theme(const Ref<Theme> p_theme) {
Ref<Theme> theme = Ref<Theme>(memnew(Theme));

const float default_contrast = 0.3;
// Controls may rely on the scale for their internal drawing logic.
theme->set_default_theme_base_scale(EDSCALE);

// Theme settings
Color accent_color = EDITOR_GET("interface/theme/accent_color");
Expand All @@ -310,6 +311,8 @@ Ref<Theme> create_editor_theme(const Ref<Theme> p_theme) {
Color preset_base_color;
float preset_contrast = 0;

const float default_contrast = 0.3;

// Please use alphabetical order if you're adding a new theme here
// (after "Custom")

Expand Down
53 changes: 24 additions & 29 deletions scene/gui/color_picker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#include "core/os/os.h"

#ifdef TOOLS_ENABLED
#include "editor/editor_scale.h"
#include "editor/editor_settings.h"
#endif
#include "scene/main/window.h"
Expand All @@ -44,17 +43,7 @@ List<Color> ColorPicker::preset_cache;

void ColorPicker::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_THEME_CHANGED: {
btn_pick->set_icon(get_theme_icon(SNAME("screen_picker"), SNAME("ColorPicker")));
btn_add_preset->set_icon(get_theme_icon(SNAME("add_preset")));
_update_presets();
_update_controls();
} break;
case NOTIFICATION_ENTER_TREE: {
btn_pick->set_icon(get_theme_icon(SNAME("screen_picker"), SNAME("ColorPicker")));
btn_add_preset->set_icon(get_theme_icon(SNAME("add_preset")));

_update_controls();
_update_color();

#ifdef TOOLS_ENABLED
Expand All @@ -71,18 +60,39 @@ void ColorPicker::_notification(int p_what) {
}
}
#endif
} break;
case NOTIFICATION_PARENTED: {
[[fallthrough]];
}
case NOTIFICATION_THEME_CHANGED: {
btn_pick->set_icon(get_theme_icon(SNAME("screen_picker"), SNAME("ColorPicker")));
btn_add_preset->set_icon(get_theme_icon(SNAME("add_preset")));

uv_edit->set_custom_minimum_size(Size2(get_theme_constant(SNAME("sv_width")), get_theme_constant(SNAME("sv_height"))));
w_edit->set_custom_minimum_size(Size2(get_theme_constant(SNAME("h_width")), 0));

wheel_edit->set_custom_minimum_size(Size2(get_theme_constant(SNAME("sv_width")), get_theme_constant(SNAME("sv_height"))));
wheel_margin->add_theme_constant_override("margin_bottom", 8 * get_theme_default_base_scale());

for (int i = 0; i < 4; i++) {
labels[i]->set_custom_minimum_size(Size2(get_theme_constant(SNAME("label_width")), 0));
set_offset((Side)i, get_offset((Side)i) + get_theme_constant(SNAME("margin")));
}

if (Engine::get_singleton()->is_editor_hint()) {
// Adjust for the width of the "Script" icon.
text_type->set_custom_minimum_size(Size2(28 * get_theme_default_base_scale(), 0));
}

_update_presets();
_update_controls();
} break;

case NOTIFICATION_VISIBILITY_CHANGED: {
Popup *p = Object::cast_to<Popup>(get_parent());
if (p) {
p->set_size(Size2(get_combined_minimum_size().width + get_theme_constant(SNAME("margin")) * 2, get_combined_minimum_size().height + get_theme_constant(SNAME("margin")) * 2));
}
} break;

case NOTIFICATION_WM_CLOSE_REQUEST: {
if (screen != nullptr && screen->is_visible()) {
screen->hide();
Expand Down Expand Up @@ -762,11 +772,7 @@ void ColorPicker::_slider_draw(int p_which) {
Size2 size = scroll[p_which]->get_size();
Color left_color;
Color right_color;
#ifdef TOOLS_ENABLED
const real_t margin = 4 * EDSCALE;
#else
const real_t margin = 4;
#endif
const real_t margin = 4 * get_theme_default_base_scale();

if (p_which == 3) {
scroll[p_which]->draw_texture_rect(get_theme_icon(SNAME("sample_bg"), SNAME("ColorPicker")), Rect2(Point2(0, margin), Size2(size.x, margin)), true);
Expand Down Expand Up @@ -1147,7 +1153,6 @@ ColorPicker::ColorPicker() :
uv_edit->set_mouse_filter(MOUSE_FILTER_PASS);
uv_edit->set_h_size_flags(SIZE_EXPAND_FILL);
uv_edit->set_v_size_flags(SIZE_EXPAND_FILL);
uv_edit->set_custom_minimum_size(Size2(get_theme_constant(SNAME("sv_width")), get_theme_constant(SNAME("sv_height"))));
uv_edit->connect("draw", callable_mp(this, &ColorPicker::_hsv_draw), make_binds(0, uv_edit));

HBoxContainer *hb_smpl = memnew(HBoxContainer);
Expand Down Expand Up @@ -1219,9 +1224,6 @@ ColorPicker::ColorPicker() :
text_type->set_text("#");
text_type->set_tooltip(TTR("Switch between hexadecimal and code values."));
if (Engine::get_singleton()->is_editor_hint()) {
#ifdef TOOLS_ENABLED
text_type->set_custom_minimum_size(Size2(28 * EDSCALE, 0)); // Adjust for the width of the "Script" icon.
#endif
text_type->connect("pressed", callable_mp(this, &ColorPicker::_text_type_toggled));
} else {
text_type->set_flat(true);
Expand All @@ -1236,20 +1238,14 @@ ColorPicker::ColorPicker() :

wheel_edit->set_h_size_flags(SIZE_EXPAND_FILL);
wheel_edit->set_v_size_flags(SIZE_EXPAND_FILL);
wheel_edit->set_custom_minimum_size(Size2(get_theme_constant(SNAME("sv_width")), get_theme_constant(SNAME("sv_height"))));
hb_edit->add_child(wheel_edit);

wheel_mat.instantiate();
wheel_mat->set_shader(wheel_shader);
circle_mat.instantiate();
circle_mat->set_shader(circle_shader);

MarginContainer *wheel_margin(memnew(MarginContainer));
#ifdef TOOLS_ENABLED
wheel_margin->add_theme_constant_override("margin_bottom", 8 * EDSCALE);
#else
wheel_margin->add_theme_constant_override("margin_bottom", 8);
#endif
wheel_edit->add_child(wheel_margin);

wheel_margin->add_child(wheel);
Expand All @@ -1261,7 +1257,6 @@ ColorPicker::ColorPicker() :
wheel_uv->connect("draw", callable_mp(this, &ColorPicker::_hsv_draw), make_binds(0, wheel_uv));

hb_edit->add_child(w_edit);
w_edit->set_custom_minimum_size(Size2(get_theme_constant(SNAME("h_width")), 0));
w_edit->set_h_size_flags(SIZE_FILL);
w_edit->set_v_size_flags(SIZE_EXPAND_FILL);
w_edit->connect("gui_input", callable_mp(this, &ColorPicker::_w_input));
Expand Down
1 change: 1 addition & 0 deletions scene/gui/color_picker.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class ColorPicker : public BoxContainer {
Control *uv_edit = memnew(Control);
Control *w_edit = memnew(Control);
AspectRatioContainer *wheel_edit = memnew(AspectRatioContainer);
MarginContainer *wheel_margin = memnew(MarginContainer);
Ref<ShaderMaterial> wheel_mat;
Ref<ShaderMaterial> circle_mat;
Control *wheel = memnew(Control);
Expand Down
Loading

0 comments on commit 865b62b

Please sign in to comment.