Here's the improved code with dynamic text coloring based on comparing current values to original values:
screen card_preview(card, selected=False, playable=False, size=(150, 210), hoverable=False, on_field=False, no_enlarge=False):
default outline_color = "#00ff0047" if playable else ("#ffff0047" if selected else "#00000000")
default hover_state = [False]
if on_field or no_enlarge:
$ enlarged = None
else:
$ enlarged = selected or (hoverable and hover_state[0])
if selected and not hover_state[0]:
$ zoom_factor = 1.2 if enlarged else 1.0
else:
$ zoom_factor = 1.8 if enlarged else 1.0
$ actual_size = (int(size[0] * zoom_factor), int(size[1] * zoom_factor))
$ offset_x = -((actual_size[0] - size[0]) // 2) if enlarged else 0
$ offset_y = -((actual_size[1] - size[1]) // 0.68) if enlarged else 0
$ outline_thickness = max(1, int(size[0] * 0.015 * zoom_factor))
# Color determination functions
python:
def get_stat_color(current_value, original_value):
if current_value < original_value:
return "#FF0000" # Red for lower
elif current_value > original_value:
return "#00FF00" # Green for higher
else:
return "#FFFFFF" # White for normal
# Get colors for each stat
mana_color = get_stat_color(card.mana, card.original_mana) if hasattr(card, 'original_mana') else "#FFFFFF"
attack_color = get_stat_color(card.attack, card.original_attack) if hasattr(card, 'original_attack') and isinstance(card, CreatureCard) else "#FFFFFF"
health_color = get_stat_color(card.health, card.original_health) if hasattr(card, 'original_health') and isinstance(card, CreatureCard) else "#FFFFFF"
frame:
xysize actual_size
background None
padding (0, 0)
xoffset offset_x
yoffset offset_y
if hoverable:
mousearea:
area (0, 0, actual_size[0], actual_size[1])
unhovered SetDict(hover_state, 0, False)
hovered [SetDict(hover_state, 0, True), SetVariable("hovered_card", card)]
add Solid(outline_color) xpos -10 ypos -10 xsize actual_size[0] + 20 ysize actual_size[1] + 20
fixed:
xysize actual_size
# Card Art
if on_field and isinstance(card, CreatureCard):
add "images/[card.card_id]-creature.png" xysize actual_size
else:
add "images/[card.card_id].png" xysize actual_size
# Mana
if not on_field:
fixed:
xpos int(size[0] * 0.02 * zoom_factor)
ypos int(size[1] * 0.02 * zoom_factor)
xsize int(size[0] * 0.25 * zoom_factor)
ysize int(size[0] * 0.25 * zoom_factor)
text "[card.mana]" size int(size[0] * 0.12 * zoom_factor) xalign 0.5 yalign 0.5 color mana_color outlines [(outline_thickness, "#000000", 0, 0)]
# Attack
if on_field:
fixed:
xpos int(size[0] * 0.02 * zoom_factor)
ypos actual_size[1] - int(size[1] * 0.25 * zoom_factor)
xsize int(size[0] * 0.25 * zoom_factor)
ysize int(size[0] * 0.25 * zoom_factor)
if isinstance(card, CreatureCard):
text "[card.attack]" size int(size[0] * 0.12 * zoom_factor) xalign 0.5 yalign 0.5 color attack_color outlines [(outline_thickness, "#000000", 0, 0)]
else:
fixed:
xpos int(size[0] * 0.02 * zoom_factor)
ypos actual_size[1] - int(size[1] * 0.18 * zoom_factor)
xsize int(size[0] * 0.25 * zoom_factor)
ysize int(size[0] * 0.25 * zoom_factor)
if isinstance(card, CreatureCard):
text "[card.attack]" size int(size[0] * 0.12 * zoom_factor) xalign 0.5 yalign 0.5 color attack_color outlines [(outline_thickness, "#000000", 0, 0)]
# Health
if on_field:
fixed:
xpos actual_size[0] - int(size[0] * 0.27 * zoom_factor)
ypos actual_size[1] - int(size[1] * 0.25 * zoom_factor)
xsize int(size[0] * 0.25 * zoom_factor)
ysize int(size[0] * 0.25 * zoom_factor)
if isinstance(card, CreatureCard):
text "[card.health]" size int(size[0] * 0.12 * zoom_factor) xalign 0.5 yalign 0.5 color health_color outlines [(outline_thickness, "#000000", 0, 0)]
else:
fixed:
xpos actual_size[0] - int(size[0] * 0.27 * zoom_factor)
ypos actual_size[1] - int(size[1] * 0.18 * zoom_factor)
xsize int(size[0] * 0.25 * zoom_factor)
ysize int(size[0] * 0.25 * zoom_factor)
if isinstance(card, CreatureCard):
text "[card.health]" size int(size[0] * 0.12 * zoom_factor) xalign 0.5 yalign 0.5 color health_color outlines [(outline_thickness, "#000000", 0, 0)]