Browse Source

Fix TreeItem's button being rendered under Selected highlight

Dawid Marzec 2 years ago
parent
commit
b05572b7d0
1 changed files with 56 additions and 49 deletions
  1. 56 49
      scene/gui/tree.cpp

+ 56 - 49
scene/gui/tree.cpp

@@ -1930,72 +1930,54 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
 				continue;
 			}
 
-			int w = get_column_width(i);
+			int item_width = get_column_width(i);
 
 			if (i == 0) {
-				w -= ofs;
+				item_width -= ofs;
 
-				if (w <= 0) {
+				if (item_width <= 0) {
 					ofs = get_column_width(0);
 					continue;
 				}
 			} else {
 				ofs += theme_cache.h_separation;
-				w -= theme_cache.h_separation;
+				item_width -= theme_cache.h_separation;
 			}
 
 			if (p_item->cells[i].expand_right) {
 				int plus = 1;
 				while (i + plus < columns.size() && !p_item->cells[i + plus].editable && p_item->cells[i + plus].mode == TreeItem::CELL_MODE_STRING && p_item->cells[i + plus].text.is_empty() && p_item->cells[i + plus].icon.is_null()) {
-					w += get_column_width(i + plus);
+					item_width += get_column_width(i + plus);
 					plus++;
 					skip2++;
 				}
 			}
 
 			if (!rtl && p_item->cells[i].buttons.size()) {
-				int button_w = 0;
+				int buttons_width = 0;
 				for (int j = p_item->cells[i].buttons.size() - 1; j >= 0; j--) {
-					Ref<Texture2D> b = p_item->cells[i].buttons[j].texture;
-					button_w += b->get_size().width + theme_cache.button_pressed->get_minimum_size().width + theme_cache.button_margin;
+					Ref<Texture2D> button_texture = p_item->cells[i].buttons[j].texture;
+					buttons_width += button_texture->get_size().width + theme_cache.button_pressed->get_minimum_size().width + theme_cache.button_margin;
 				}
 
 				int total_ofs = ofs - theme_cache.offset.x;
 
-				if (total_ofs + w > p_draw_size.width) {
-					w = MAX(button_w, p_draw_size.width - total_ofs);
+				if (total_ofs + item_width > p_draw_size.width) {
+					item_width = MAX(buttons_width, p_draw_size.width - total_ofs);
 				}
 			}
 
-			int bw = 0;
+			int item_width_with_buttons = item_width; // used later for drawing buttons
+			int buttons_width = 0;
 			for (int j = p_item->cells[i].buttons.size() - 1; j >= 0; j--) {
-				Ref<Texture2D> b = p_item->cells[i].buttons[j].texture;
-				Size2 s = b->get_size() + theme_cache.button_pressed->get_minimum_size();
+				Ref<Texture2D> button_texture = p_item->cells[i].buttons[j].texture;
+				Size2 button_size = button_texture->get_size() + theme_cache.button_pressed->get_minimum_size();
 
-				Point2i o = Point2i(ofs + w - s.width, p_pos.y) - theme_cache.offset + p_draw_ofs;
-
-				if (cache.click_type == Cache::CLICK_BUTTON && cache.click_item == p_item && cache.click_column == i && cache.click_index == j && !p_item->cells[i].buttons[j].disabled) {
-					// Being pressed.
-					Point2 od = o;
-					if (rtl) {
-						od.x = get_size().width - od.x - s.x;
-					}
-					theme_cache.button_pressed->draw(get_canvas_item(), Rect2(od.x, od.y, s.width, MAX(s.height, label_h)));
-				}
-
-				o.y += (label_h - s.height) / 2;
-				o += theme_cache.button_pressed->get_offset();
-
-				if (rtl) {
-					o.x = get_size().width - o.x - b->get_width();
-				}
-
-				b->draw(ci, o, p_item->cells[i].buttons[j].disabled ? Color(1, 1, 1, 0.5) : p_item->cells[i].buttons[j].color);
-				w -= s.width + theme_cache.button_margin;
-				bw += s.width + theme_cache.button_margin;
+				item_width -= button_size.width + theme_cache.button_margin;
+				buttons_width += button_size.width + theme_cache.button_margin;
 			}
 
-			Rect2i item_rect = Rect2i(Point2i(ofs, p_pos.y) - theme_cache.offset + p_draw_ofs, Size2i(w, label_h));
+			Rect2i item_rect = Rect2i(Point2i(ofs, p_pos.y) - theme_cache.offset + p_draw_ofs, Size2i(item_width, label_h));
 			Rect2i cell_rect = item_rect;
 			if (i != 0) {
 				cell_rect.position.x -= theme_cache.h_separation;
@@ -2049,7 +2031,7 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
 				Rect2 r = cell_rect;
 				if (i == 0) {
 					r.position.x = p_draw_ofs.x;
-					r.size.x = w + ofs;
+					r.size.x = item_width + ofs;
 				} else {
 					r.position.x -= theme_cache.h_separation;
 					r.size.x += theme_cache.h_separation;
@@ -2094,11 +2076,11 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
 				}
 			}
 
-			Color col;
+			Color cell_color;
 			if (p_item->cells[i].custom_color) {
-				col = p_item->cells[i].color;
+				cell_color = p_item->cells[i].color;
 			} else {
-				col = p_item->cells[i].selected ? theme_cache.font_selected_color : theme_cache.font_color;
+				cell_color = p_item->cells[i].selected ? theme_cache.font_selected_color : theme_cache.font_color;
 			}
 
 			Color font_outline_color = theme_cache.font_outline_color;
@@ -2119,7 +2101,7 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
 
 			switch (p_item->cells[i].mode) {
 				case TreeItem::CELL_MODE_STRING: {
-					draw_item_rect(p_item->cells.write[i], item_rect, col, icon_col, outline_size, font_outline_color);
+					draw_item_rect(p_item->cells.write[i], item_rect, cell_color, icon_col, outline_size, font_outline_color);
 				} break;
 				case TreeItem::CELL_MODE_CHECK: {
 					Ref<Texture2D> checked = theme_cache.checked;
@@ -2143,7 +2125,7 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
 					item_rect.size.x -= check_w;
 					item_rect.position.x += check_w;
 
-					draw_item_rect(p_item->cells.write[i], item_rect, col, icon_col, outline_size, font_outline_color);
+					draw_item_rect(p_item->cells.write[i], item_rect, cell_color, icon_col, outline_size, font_outline_color);
 
 				} break;
 				case TreeItem::CELL_MODE_RANGE: {
@@ -2160,12 +2142,12 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
 							if (outline_size > 0 && font_outline_color.a > 0) {
 								p_item->cells[i].text_buf->draw_outline(ci, text_pos + Vector2(cell_width - text_width, 0), outline_size, font_outline_color);
 							}
-							p_item->cells[i].text_buf->draw(ci, text_pos + Vector2(cell_width - text_width, 0), col);
+							p_item->cells[i].text_buf->draw(ci, text_pos + Vector2(cell_width - text_width, 0), cell_color);
 						} else {
 							if (outline_size > 0 && font_outline_color.a > 0) {
 								p_item->cells[i].text_buf->draw_outline(ci, text_pos, outline_size, font_outline_color);
 							}
-							p_item->cells[i].text_buf->draw(ci, text_pos, col);
+							p_item->cells[i].text_buf->draw(ci, text_pos, cell_color);
 						}
 
 						Point2i arrow_pos = item_rect.position;
@@ -2182,12 +2164,12 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
 							if (outline_size > 0 && font_outline_color.a > 0) {
 								p_item->cells[i].text_buf->draw_outline(ci, text_pos + Vector2(cell_width - text_width, 0), outline_size, font_outline_color);
 							}
-							p_item->cells[i].text_buf->draw(ci, text_pos + Vector2(cell_width - text_width, 0), col);
+							p_item->cells[i].text_buf->draw(ci, text_pos + Vector2(cell_width - text_width, 0), cell_color);
 						} else {
 							if (outline_size > 0 && font_outline_color.a > 0) {
 								p_item->cells[i].text_buf->draw_outline(ci, text_pos, outline_size, font_outline_color);
 							}
-							p_item->cells[i].text_buf->draw(ci, text_pos, col);
+							p_item->cells[i].text_buf->draw(ci, text_pos, cell_color);
 						}
 
 						if (!p_item->cells[i].editable) {
@@ -2227,7 +2209,7 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
 					}
 
 					if (!p_item->cells[i].editable) {
-						draw_item_rect(p_item->cells.write[i], item_rect, col, icon_col, outline_size, font_outline_color);
+						draw_item_rect(p_item->cells.write[i], item_rect, cell_color, icon_col, outline_size, font_outline_color);
 						break;
 					}
 
@@ -2246,7 +2228,7 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
 								draw_style_box(theme_cache.custom_button_pressed, ir);
 							} else {
 								draw_style_box(theme_cache.custom_button_hover, ir);
-								col = theme_cache.custom_button_font_highlight;
+								cell_color = theme_cache.custom_button_font_highlight;
 							}
 						} else {
 							draw_style_box(theme_cache.custom_button, ir);
@@ -2255,17 +2237,42 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2
 						ir.position += theme_cache.custom_button->get_offset();
 					}
 
-					draw_item_rect(p_item->cells.write[i], ir, col, icon_col, outline_size, font_outline_color);
+					draw_item_rect(p_item->cells.write[i], ir, cell_color, icon_col, outline_size, font_outline_color);
 
 					downarrow->draw(ci, arrow_pos);
 
 				} break;
 			}
 
+			for (int j = p_item->cells[i].buttons.size() - 1; j >= 0; j--) {
+				Ref<Texture2D> button_texture = p_item->cells[i].buttons[j].texture;
+				Size2 button_size = button_texture->get_size() + theme_cache.button_pressed->get_minimum_size();
+
+				Point2i button_ofs = Point2i(ofs + item_width_with_buttons - button_size.width, p_pos.y) - theme_cache.offset + p_draw_ofs;
+
+				if (cache.click_type == Cache::CLICK_BUTTON && cache.click_item == p_item && cache.click_column == i && cache.click_index == j && !p_item->cells[i].buttons[j].disabled) {
+					// Being pressed.
+					Point2 od = button_ofs;
+					if (rtl) {
+						od.x = get_size().width - od.x - button_size.x;
+					}
+					theme_cache.button_pressed->draw(get_canvas_item(), Rect2(od.x, od.y, button_size.width, MAX(button_size.height, label_h)));
+				}
+
+				button_ofs.y += (label_h - button_size.height) / 2;
+				button_ofs += theme_cache.button_pressed->get_offset();
+
+				if (rtl) {
+					button_ofs.x = get_size().width - button_ofs.x - button_texture->get_width();
+				}
+				button_texture->draw(ci, button_ofs, p_item->cells[i].buttons[j].disabled ? Color(1, 1, 1, 0.5) : p_item->cells[i].buttons[j].color);
+				item_width_with_buttons -= button_size.width + theme_cache.button_margin;
+			}
+
 			if (i == 0) {
 				ofs = get_column_width(0);
 			} else {
-				ofs += w + bw;
+				ofs += item_width + buttons_width;
 			}
 
 			if (select_mode == SELECT_MULTI && selected_item == p_item && selected_col == i) {