Feat : a good start

This commit is contained in:
2024-07-01 01:42:33 +02:00
parent 857dff5d42
commit ae825d7f5d
122 changed files with 8419 additions and 249 deletions
+86 -121
View File
@@ -1,34 +1,40 @@
import drawsvg as dw
import json
from typing import List
import os
from draw_functions import create_gold_icon
from typing import cast
from pathlib import Path
from draw_functions import create_gold_icon, create_effects, draw_champion_shield
from card_types import Card, SkinData, SkinFile, Translation
from drawsvg.drawing import Drawing
with open("src/data/translation_fr.json", encoding="utf-8") as f:
translation_fr: dict = json.load(f)
translation_fr: Translation = json.load(f)
with open("src/data/heron_skin.json", encoding="utf-8") as f:
all_skin_data: dict = json.load(f)
all_skin_data: SkinFile = json.load(f)
cards: list[Card] = []
for p in Path('src/data/cards').glob('*.json'):
with open(p, encoding="utf-8") as f:
temp: list[Card] = json.load(f)["cards"]
cards.extend(temp)
# Params
x_width = 298
y_height = 417
border_width_x = 12
border_width_y = 17
x_internal_width = x_width - (border_width_x*2)
border_upper_offset = 5
title_y_offset = 24
title_font_size = 19
title_font_size_small = 16
subtitle_font_size = 13
cost_right_offset = 38
effect_text_size = 14
row_1_base_y = 280
row_2_base_y = 375
row_1_to_2_separator_y = 345
row_1_to_2_separator_shorten_by = 20
background_colors = {
"blue": "lightblue",
@@ -38,28 +44,22 @@ background_colors = {
"base": "lightgrey",
}
gradient_colors = {"red": "red", "blue": "blue", "yellow": "yellow", "green": "green"}
icon_colors = {"yellow": ["#ff9935", "#ffe744"]}
with open("src/data/cards_data.json", encoding="utf-8") as f:
cards: List = json.load(f)["cards"]
gradient_colors = {"red": "red", "blue": "blue", "yellow": "yellow", "green": "green", "base":"gray"}
icon_colors = {
"yellow": ["#ff9935", "#ffe744"],
"blue": ["#ff9935", "#ffe744"],
"red": ["#ff9935", "#ffe744"],
"green": ["#ff9935", "#ffe744"],
"base": ["#ff9935", "#ffe744"]
}
shadowFilter = dw.Filter(id="drop-shadow")
shadowFilter.append(dw.FilterItem("feDropShadow", dx=1, dy=1, stdDeviation=0))
# Draw faction icon
faction_icon = dw.Group(id="faction_icon")
faction_icon.append(
dw.Circle(
cx=1,
cy=1,
r=20,
fill="black",
filter=shadowFilter
)
)
faction_icon.append(dw.Circle(cx=1, cy=1, r=20, fill="black", filter=shadowFilter))
faction_icon.append(
dw.Circle(
cx=0,
@@ -72,64 +72,70 @@ faction_icon.append(
for card_data in cards:
# Get card skin data
try:
card_skin_data = all_skin_data[str(card_data["id"])]
card_skin_data = all_skin_data["cards"][str(card_data["id"])]
except KeyError:
print(
"Missing skin data for card id:", card_data["id"], "using default skin data"
print("Missing skin data for card id:", card_data["id"], "using default skin data")
card_skin_data = SkinData(
title=card_data["name"],
image_path="../../img/missing.png",
flavor_text_type=None,
)
card_skin_data = {
"title": card_data["name"],
"image_path": "../../img/missing.png",
}
# Create a new SVG drawing
output_dir = "output/" + all_skin_data["skin_name"]
os.makedirs(output_dir, exist_ok=True)
d = dw.Drawing(x_width, y_height)
d = Drawing(x_width, y_height)
d.append(shadowFilter)
# Define linear colored gradient
gradient = dw.LinearGradient(0, 0, 1, 0, gradientUnits="objectBoundingBox",id="grad1")
gradient = dw.LinearGradient(
0, 0, 1, 0, gradientUnits="objectBoundingBox", id="grad1"
)
gradient.add_stop(0, gradient_colors[card_data["faction"]], opacity=1)
gradient.add_stop(1, gradient_colors[card_data["faction"]], opacity=0)
d.append(dw.Use(gradient, None, None))
d.append_def(gradient)
# Define linear colored gradient for faction icon
gradient_faction_icon = dw.LinearGradient(0, 1, 0, 0, gradientUnits="objectBoundingBox",id="grad2")
gradient_faction_icon = dw.LinearGradient(
0, 1, 0, 0, gradientUnits="objectBoundingBox", id="grad2"
)
gradient_faction_icon.add_stop(0, icon_colors[card_data["faction"]][0], opacity=1)
gradient_faction_icon.add_stop(1, icon_colors[card_data["faction"]][1], opacity=1)
d.append(dw.Use(gradient_faction_icon, None, None))
d.append_def(gradient_faction_icon)
# Draw card background
d.append(dw.Rectangle(0, 0, x_width, y_height, rx=15, ry=18, fill="black"))
d.append(
background = dw.Group(id="background")
background.append(dw.Rectangle(0, 0, x_width, y_height, rx=15, ry=18, fill="black"))
background.append(
dw.Rectangle(
border_width_x,
border_width_y - border_upper_offset,
x_width - border_width_x * 2,
x_internal_width,
y_height - border_width_y * 2 + border_upper_offset,
rx=0,
ry=0,
fill=background_colors[card_data["faction"]],
)
)
d.append(background)
# Draw title area
# dwg.add(dwg.rect(insert=(0, 0), size=(str(x_width) + 'px', '50px'), fill='white'))
title = dw.Group(id="title")
card_title = card_skin_data["title"]
TEMP_TITLE_FONT_SIZE = title_font_size
if len(card_title) > 20:
TEMP_TITLE_FONT_SIZE = title_font_size_small
if len(card_title) > 23:
TEMP_TITLE_FONT_SIZE = title_font_size_small - 2
d.append(
temp_title_font_size = title_font_size_small
elif len(card_title) > 23:
temp_title_font_size = title_font_size_small - 2
else:
temp_title_font_size = title_font_size
title.append(
dw.Text(
card_title,
x=x_width / 2,
y=title_y_offset + border_width_y - border_upper_offset,
text_anchor="middle",
fill="black",
font_size=TEMP_TITLE_FONT_SIZE,
font_size=temp_title_font_size,
font_weight="bold",
font_family="Helvetica",
)
@@ -138,9 +144,9 @@ for card_data in cards:
card_type_text = translation_fr["card_type"].get(
card_data["card_type"], card_data["card_type"]
)
if "flavor_text_type" in card_skin_data.keys():
card_type_text += ", " + card_skin_data["flavor_text_type"]
d.append(
if card_skin_data.get("flavor_text_type") is not None:
card_type_text += ", " + str(card_skin_data["flavor_text_type"])
title.append(
dw.Text(
card_type_text,
font_size=subtitle_font_size,
@@ -151,86 +157,45 @@ for card_data in cards:
font_family="Helvetica",
)
)
d.append(title)
d.append(dw.Use(faction_icon, border_width_x + 23 + 1,border_width_y - border_upper_offset + 20 + 4 + 1 ))
# dwg.add(dwg.image(card_data['faction_icon'], insert=(x_width - 30 - 15, 30 - 15), size=(30, 30)))
# Draw cost circle
# dwg.add(
# dwg.circle(
# center=(x_width - cost_right_offset + 2, 19 + border_width_y + 2),
# r=21,
# fill="rgb(202, 129, 25)",
# )
# )
# dwg.add(
# dwg.circle(
# center=(x_width - cost_right_offset, 19 + border_width_y),
# r=20,
# fill="yellow",
# )
# )
# dwg.add(
# dwg.text(
# str(card_data["cost"]),
# insert=(x_width - cost_right_offset, 26 + border_width_y),
# text_anchor="middle",
# fill="black",
# font_size="24px",
# font_weight="bold",
# font_family="Helvetica",
# )
# )
d.append(dw.Use(create_gold_icon(card_data["cost"]), x_width - cost_right_offset, 19 + border_width_y))
d.save_svg(
"output/"
+ all_skin_data["skin_name"]
+ "/"
+ str(card_data["id"]).zfill(3)
+ ".svg"
)
continue
# Draw image area
dwg.add(dwg.rect(insert=(0, 65), size=(str(x_width) + "px", "200px"), fill="black"))
dwg.add(
dwg.image(card_skin_data["image_path"], insert=(0, 65), size=(x_width, 200))
)
dwg.add(
dwg.rect(
insert=(border_width_x, 265),
size=(str(30) + "px", str(y_height - 265 - border_width_y) + "px"),
fill="url(#grad1)",
# Draw faction icon
d.append(
dw.Use(
faction_icon,
border_width_x + 23 + 1,
border_width_y - border_upper_offset + 20 + 4 + 1,
)
)
# If champion, draw champion action icon
if card_data["card_type"] == "champion":
row_1_icon_x = border_width_x + 35
dwg.add(
dwg.circle(
center=(row_1_icon_x + 1, row_1_base_y + 20 + 1), r=15, fill="#111"
# Draw card cost
if card_data.get("cost") is not None:
d.append(
dw.Use(
create_gold_icon(cast(int,card_data.get("cost"))),
x_width - cost_right_offset,
19 + border_width_y,
)
)
dwg.add(dwg.circle(center=(row_1_icon_x, row_1_base_y + 20), r=15, fill="#555"))
draw_effects(
dwg=dwg,
card_effects=card_data["effects"],
border_width_x=border_width_x,
effect_text_size=effect_text_size,
row_1_base_y=row_1_base_y,
row_1_to_2_separator_shorten_by=row_1_to_2_separator_shorten_by,
row_1_to_2_separator_y=row_1_to_2_separator_y,
row_2_base_y=row_2_base_y,
x_width=x_width,
)
d.append(dw.Rectangle(x=0, y=65, width=x_width, height=200))
d.append(dw.Image(x=0, y=65, width=x_width, height=200,path=card_skin_data["image_path"]))
# Draw effects
effects = card_data.get("effects")
if effects is not None:
effects = create_effects(
effects=effects,
height=135,
width=x_internal_width
)
d.append(dw.Use(effects, border_width_x,265))
# If champion, draw champion shield with defense
if card_data["card_type"] == "champion":
draw_champion_shield(dwg, card_data, x_width, y_height, border_width_x)
shield = draw_champion_shield(card_data, border_width_x)
d.append(dw.Use(shield,x_width-30, y_height))
# Save the SVG file
dwg.save()
d.save_svg(f"output/{all_skin_data['skin_name']}/{str(card_data["id"]).zfill(3)}.svg")