Skip to content

Commit

Permalink
Merge pull request #88577 from davthedev/itemlist-light-refactor
Browse files Browse the repository at this point in the history
Fix item positioning, text alignment & unwanted clipping of ItemList items
  • Loading branch information
akien-mga committed Feb 29, 2024
2 parents 7462b1a + f63728c commit da91622
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 23 deletions.
2 changes: 1 addition & 1 deletion doc/classes/ItemList.xml
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@
The size of the item text outline.
[b]Note:[/b] If using a font with [member FontFile.multichannel_signed_distance_field] enabled, its [member FontFile.msdf_pixel_range] must be set to at least [i]twice[/i] the value of [theme_item outline_size] for outline rendering to look correct. Otherwise, the outline may appear to be cut off earlier than intended.
</theme_item>
<theme_item name="v_separation" data_type="constant" type="int" default="2">
<theme_item name="v_separation" data_type="constant" type="int" default="4">
The vertical spacing between items.
</theme_item>
<theme_item name="font" data_type="font" type="Font">
Expand Down
2 changes: 1 addition & 1 deletion editor/themes/editor_theme_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,7 @@ void EditorThemeManager::_populate_standard_styles(const Ref<EditorTheme> &p_the
p_theme->set_color("font_selected_color", "ItemList", p_config.mono_color);
p_theme->set_color("font_outline_color", "ItemList", p_config.font_outline_color);
p_theme->set_color("guide_color", "ItemList", Color(1, 1, 1, 0));
p_theme->set_constant("v_separation", "ItemList", p_config.forced_even_separation * 0.5 * EDSCALE);
p_theme->set_constant("v_separation", "ItemList", p_config.forced_even_separation * EDSCALE);
p_theme->set_constant("h_separation", "ItemList", (p_config.increased_margin + 2) * EDSCALE);
p_theme->set_constant("icon_margin", "ItemList", (p_config.increased_margin + 2) * EDSCALE);
p_theme->set_constant("line_separation", "ItemList", p_config.separation_margin);
Expand Down
44 changes: 24 additions & 20 deletions scene/gui/item_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1150,10 +1150,6 @@ void ItemList::_notification(int p_what) {
if (should_draw_selected_bg || should_draw_hovered_bg || should_draw_custom_bg) {
Rect2 r = rcache;
r.position += base_ofs;
r.position.y -= theme_cache.v_separation / 2;
r.size.y += theme_cache.v_separation;
r.position.x -= theme_cache.h_separation / 2;
r.size.x += theme_cache.h_separation;

if (rtl) {
r.position.x = size.width - r.position.x - r.size.x;
Expand Down Expand Up @@ -1185,6 +1181,12 @@ void ItemList::_notification(int p_what) {

Point2 pos = items[i].rect_cache.position + icon_ofs + base_ofs;

if (icon_mode == ICON_MODE_TOP) {
pos.y += theme_cache.v_separation / 2;
} else {
pos.x += theme_cache.h_separation / 2;
}

if (icon_mode == ICON_MODE_TOP) {
pos.x += Math::floor((items[i].rect_cache.size.width - icon_size.width) / 2);
pos.y += theme_cache.icon_margin;
Expand Down Expand Up @@ -1224,6 +1226,8 @@ void ItemList::_notification(int p_what) {

if (items[i].tag_icon.is_valid()) {
Point2 draw_pos = items[i].rect_cache.position;
draw_pos.x += theme_cache.h_separation / 2;
draw_pos.y += theme_cache.v_separation / 2;
if (rtl) {
draw_pos.x = size.width - draw_pos.x - items[i].tag_icon->get_width();
}
Expand Down Expand Up @@ -1261,12 +1265,18 @@ void ItemList::_notification(int p_what) {
text_ofs += base_ofs;
text_ofs += items[i].rect_cache.position;

text_ofs.x += theme_cache.h_separation / 2;
text_ofs.y += theme_cache.v_separation / 2;

if (rtl) {
text_ofs.x = size.width - text_ofs.x - max_len;
}

items.write[i].text_buf->set_alignment(HORIZONTAL_ALIGNMENT_CENTER);

float text_w = items[i].rect_cache.size.width - theme_cache.h_separation;
items.write[i].text_buf->set_width(text_w);

if (theme_cache.font_outline_size > 0 && theme_cache.font_outline_color.a > 0) {
items[i].text_buf->draw_outline(get_canvas_item(), text_ofs, theme_cache.font_outline_size, theme_cache.font_outline_color);
}
Expand All @@ -1279,14 +1289,17 @@ void ItemList::_notification(int p_what) {

if (icon_mode == ICON_MODE_TOP) {
text_ofs.x += (items[i].rect_cache.size.width - size2.x) / 2;
text_ofs.x += theme_cache.h_separation / 2;
text_ofs.y += theme_cache.v_separation / 2;
} else {
text_ofs.y += (items[i].rect_cache.size.height - size2.y) / 2;
text_ofs.x += theme_cache.h_separation / 2;
}

text_ofs += base_ofs;
text_ofs += items[i].rect_cache.position;

float text_w = width - text_ofs.x;
float text_w = width - text_ofs.x - theme_cache.h_separation;
items.write[i].text_buf->set_width(text_w);

if (rtl) {
Expand All @@ -1309,10 +1322,6 @@ void ItemList::_notification(int p_what) {
if (select_mode == SELECT_MULTI && i == current) {
Rect2 r = rcache;
r.position += base_ofs;
r.position.y -= theme_cache.v_separation / 2;
r.size.y += theme_cache.v_separation;
r.position.x -= theme_cache.h_separation / 2;
r.size.x += theme_cache.h_separation;

if (rtl) {
r.position.x = size.width - r.position.x - r.size.x;
Expand Down Expand Up @@ -1382,9 +1391,10 @@ void ItemList::force_update_list_size() {
}
max_column_width = MAX(max_column_width, minsize.x);

// elements need to adapt to the selected size
// Elements need to adapt to the selected size.
minsize.y += theme_cache.v_separation;
minsize.x += theme_cache.h_separation;

items.write[i].rect_cache.size = minsize;
items.write[i].min_rect_cache.size = minsize;
}
Expand Down Expand Up @@ -1415,26 +1425,26 @@ void ItemList::force_update_list_size() {
}

if (same_column_width) {
items.write[i].rect_cache.size.x = max_column_width;
items.write[i].rect_cache.size.x = max_column_width + theme_cache.h_separation;
}
items.write[i].rect_cache.position = ofs;

max_h = MAX(max_h, items[i].rect_cache.size.y);
ofs.x += items[i].rect_cache.size.x + theme_cache.h_separation;
ofs.x += items[i].rect_cache.size.x;

items.write[i].column = col;
col++;
if (col == current_columns) {
if (i < items.size() - 1) {
separators.push_back(ofs.y + max_h + theme_cache.v_separation / 2);
separators.push_back(ofs.y + max_h);
}

for (int j = i; j >= 0 && col > 0; j--, col--) {
items.write[j].rect_cache.size.y = max_h;
}

ofs.x = 0;
ofs.y += max_h + theme_cache.v_separation;
ofs.y += max_h;
col = 0;
max_h = 0;
}
Expand Down Expand Up @@ -1496,12 +1506,6 @@ int ItemList::get_item_at_position(const Point2 &p_pos, bool p_exact) const {
for (int i = 0; i < items.size(); i++) {
Rect2 rc = items[i].rect_cache;

// Grow the detection rectangle to match the grown selection indicator.
rc.position.y -= theme_cache.v_separation / 2;
rc.size.y += theme_cache.v_separation;
rc.position.x -= theme_cache.h_separation / 2;
rc.size.x += theme_cache.h_separation;

if (i % current_columns == current_columns - 1) {
rc.size.width = get_size().width - rc.position.x; // Make sure you can still select the last item when clicking past the column.
}
Expand Down
2 changes: 1 addition & 1 deletion scene/theme/default_theme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ void fill_default_theme(Ref<Theme> &theme, const Ref<Font> &default_font, const
theme->set_stylebox("panel", "ItemList", make_flat_stylebox(style_normal_color));
theme->set_stylebox("focus", "ItemList", focus);
theme->set_constant("h_separation", "ItemList", Math::round(4 * scale));
theme->set_constant("v_separation", "ItemList", Math::round(2 * scale));
theme->set_constant("v_separation", "ItemList", Math::round(4 * scale));
theme->set_constant("icon_margin", "ItemList", Math::round(4 * scale));
theme->set_constant("line_separation", "ItemList", Math::round(2 * scale));

Expand Down

0 comments on commit da91622

Please sign in to comment.