add margin, div hover, sizing improvements

This commit is contained in:
Face
2025-08-03 13:23:02 +03:00
parent 233afbfe1a
commit 823149057a
7 changed files with 270 additions and 53 deletions

View File

@@ -155,17 +155,51 @@ static func is_background_panel(node: Node) -> bool:
return node.name == "BackgroundPanel" and node is Panel
# for any other tag
static func create_panel_container_with_background(styles: Dictionary) -> PanelContainer:
static func create_panel_container_with_background(styles: Dictionary, hover_styles: Dictionary = {}) -> PanelContainer:
var panel_container = PanelContainer.new()
panel_container.name = "Div"
var vbox = VBoxContainer.new()
vbox.name = "VBoxContainer"
# Allow mouse events to pass through to the parent PanelContainer
vbox.mouse_filter = Control.MOUSE_FILTER_IGNORE
panel_container.add_child(vbox)
var style_box = create_stylebox_from_styles(styles)
panel_container.add_theme_stylebox_override("panel", style_box)
# Add hover support if hover styles exist
if hover_styles.size() > 0:
setup_panel_hover_support(panel_container, styles, hover_styles)
return panel_container
static func setup_panel_hover_support(panel: PanelContainer, normal_styles: Dictionary, hover_styles: Dictionary):
var normal_stylebox = create_stylebox_from_styles(normal_styles)
# Merge normal styles with hover styles for the hover state
var merged_hover_styles = normal_styles.duplicate()
for key in hover_styles:
merged_hover_styles[key] = hover_styles[key]
var hover_stylebox = create_stylebox_from_styles(merged_hover_styles)
# Store references for the hover handlers
panel.set_meta("normal_stylebox", normal_stylebox)
panel.set_meta("hover_stylebox", hover_stylebox)
# Connect mouse events
panel.mouse_entered.connect(_on_panel_mouse_entered.bind(panel))
panel.mouse_exited.connect(_on_panel_mouse_exited.bind(panel))
static func _on_panel_mouse_entered(panel: PanelContainer):
if panel.has_meta("hover_stylebox"):
var hover_stylebox = panel.get_meta("hover_stylebox")
panel.add_theme_stylebox_override("panel", hover_stylebox)
static func _on_panel_mouse_exited(panel: PanelContainer):
if panel.has_meta("normal_stylebox"):
var normal_stylebox = panel.get_meta("normal_stylebox")
panel.add_theme_stylebox_override("panel", normal_stylebox)
static func needs_background_wrapper(styles: Dictionary) -> bool:
return styles.has("background-color") or styles.has("border-radius") or styles.has("padding") or styles.has("padding-top") or styles.has("padding-right") or styles.has("padding-bottom") or styles.has("padding-left") or styles.has("border-width") or styles.has("border-top-width") or styles.has("border-right-width") or styles.has("border-bottom-width") or styles.has("border-left-width") or styles.has("border-color") or styles.has("border-style") or styles.has("border-top-color") or styles.has("border-right-color") or styles.has("border-bottom-color") or styles.has("border-left-color")

View File

@@ -48,12 +48,12 @@ static func should_skip_sizing(node: Control, element, parser) -> bool:
return false
static func apply_container_dimension_sizing(node: Control, width, height) -> void:
static func apply_container_dimension_sizing(node: Control, width, height, styles: Dictionary = {}) -> void:
if width != null:
if is_percentage(width):
node.set_meta("container_percentage_width", width)
node.size_flags_horizontal = Control.SIZE_EXPAND_FILL
apply_container_percentage_sizing(node)
# Don't call immediately - will be called later when parent is available
else:
node.custom_minimum_size.x = width
node.size_flags_horizontal = Control.SIZE_SHRINK_BEGIN
@@ -66,8 +66,10 @@ static func apply_container_dimension_sizing(node: Control, width, height) -> vo
else:
node.custom_minimum_size.y = height
node.size_flags_vertical = Control.SIZE_SHRINK_BEGIN
apply_size_constraints_and_flags(node, styles)
static func apply_regular_control_sizing(node: Control, width, height) -> void:
static func apply_regular_control_sizing(node: Control, width, height, styles: Dictionary = {}) -> void:
if width != null:
if is_percentage(width):
var estimated_width = calculate_percentage_size(width, DEFAULT_VIEWPORT_WIDTH)
@@ -85,6 +87,8 @@ static func apply_regular_control_sizing(node: Control, width, height) -> void:
else:
node.custom_minimum_size.y = height
node.size_flags_vertical = Control.SIZE_SHRINK_BEGIN
apply_size_constraints_and_flags(node, styles)
static func is_percentage(value) -> bool:
return typeof(value) == TYPE_STRING and value.ends_with("%")
@@ -103,16 +107,64 @@ static func apply_container_percentage_sizing(node: Control) -> void:
if node.has_meta("container_percentage_width"):
var percentage_str = node.get_meta("container_percentage_width")
var parent_width = get_parent_dimension(parent, true, DEFAULT_VIEWPORT_WIDTH)
new_min_size.x = calculate_percentage_size(percentage_str, parent_width)
var parent_width = get_parent_dimension(parent, true, 0)
if parent_width > 0:
new_min_size.x = calculate_percentage_size(percentage_str, parent_width)
node.size_flags_horizontal = Control.SIZE_SHRINK_BEGIN
else:
# Parent size not available yet, defer sizing by using SIZE_EXPAND_FILL
node.size_flags_horizontal = Control.SIZE_EXPAND_FILL
var percentage_value = float(percentage_str.replace("%", "")) / 100.0
node.size_flags_stretch_ratio = percentage_value
if node.has_meta("container_percentage_height"):
var percentage_str = node.get_meta("container_percentage_height")
var parent_height = get_parent_dimension(parent, false, DEFAULT_VIEWPORT_HEIGHT)
new_min_size.y = calculate_percentage_size(percentage_str, parent_height)
var parent_height = get_parent_dimension(parent, false, 0)
if parent_height > 0:
new_min_size.y = calculate_percentage_size(percentage_str, parent_height)
else:
# Parent size not available yet, defer sizing by using SIZE_EXPAND_FILL
node.size_flags_vertical = Control.SIZE_EXPAND_FILL
node.custom_minimum_size = new_min_size
static func apply_size_constraints_and_flags(node: Control, styles: Dictionary) -> void:
# Apply min/max width constraints
if styles.has("min-width"):
var min_width = parse_size_value(styles["min-width"])
if min_width != null and typeof(min_width) != TYPE_STRING:
node.custom_minimum_size.x = max(node.custom_minimum_size.x, min_width)
if styles.has("max-width"):
var max_width = parse_size_value(styles["max-width"])
if max_width != null and typeof(max_width) != TYPE_STRING:
if node.custom_minimum_size.x > max_width:
node.custom_minimum_size.x = max_width
# Prevent expansion beyond max width
if node.size_flags_horizontal == Control.SIZE_EXPAND_FILL:
node.size_flags_horizontal = Control.SIZE_SHRINK_BEGIN
# Apply min/max height constraints
if styles.has("min-height"):
var min_height = parse_size_value(styles["min-height"])
if min_height != null and typeof(min_height) != TYPE_STRING:
node.custom_minimum_size.y = max(node.custom_minimum_size.y, min_height)
if styles.has("max-height"):
var max_height = parse_size_value(styles["max-height"])
if max_height != null and typeof(max_height) != TYPE_STRING:
if node.custom_minimum_size.y > max_height:
node.custom_minimum_size.y = max_height
# Prevent expansion beyond max height
if node.size_flags_vertical == Control.SIZE_EXPAND_FILL:
node.size_flags_vertical = Control.SIZE_SHRINK_BEGIN
# Handle intrinsic sizing for elements with constraints but no explicit size
if not styles.has("width") and (styles.has("min-width") or styles.has("max-width")):
node.size_flags_horizontal = Control.SIZE_SHRINK_BEGIN
if not styles.has("height") and (styles.has("min-height") or styles.has("max-height")):
node.size_flags_vertical = Control.SIZE_SHRINK_BEGIN
static func get_parent_dimension(parent: Control, is_width: bool, fallback: float) -> float:
var size_value = parent.size.x if is_width else parent.size.y
if size_value > 0: