File size: 4,052 Bytes
a184f51
 
 
1dc4501
 
 
 
a184f51
 
1dc4501
a184f51
 
 
5d36cc3
a184f51
1dc4501
 
6329a16
 
5d36cc3
6329a16
 
 
 
 
668a3f5
a184f51
 
 
 
6329a16
1dc4501
 
 
 
 
 
a184f51
1dc4501
5d36cc3
1dc4501
 
a184f51
1dc4501
 
 
 
 
a184f51
 
ac64ec0
 
a184f51
 
 
 
 
 
 
ac64ec0
 
a184f51
 
9e96640
b03b8e6
5d36cc3
 
a184f51
5d36cc3
 
a184f51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d36cc3
9e96640
ac64ec0
8512428
 
 
 
 
a184f51
8512428
5d36cc3
 
8512428
 
a184f51
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import os
from io import BytesIO

import streamlit as st
import svgwrite
from PIL import Image

DEFAULT_CARD_WIDTH = 75
DEFAULT_CARD_HEIGHT = 100

PARTS = {"background": ["white", "black", "red", "blue", "green", "yellow"],
         "suit": ["clubs", "diamonds", "hearts", "spades"],
         "value": ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]}

LEVELS = ["common", "uncommon", "rare", "epic", "legendary", "mythic", "godlike"]


def draw_card(background, suit, value, level, card_width, card_height):
    dwg = svgwrite.Drawing(size=(f"{card_width}px", f"{card_height}px"))
    dwg.add(dwg.rect((0, 0), (card_width, card_height), rx=10, ry=10, fill=background, stroke="black", stroke_width=2))

    suit = svgwrite.text.Text(suit.upper(), insert=(5, 15), fill="black", font_size="16px", font_weight="bold")
    dwg.add(suit)
    value = svgwrite.text.Text(value, insert=(5, card_height - 10), fill="black", font_size="16px", font_weight="bold")
    dwg.add(value)
    level = svgwrite.text.Text(level.upper(), insert=(card_width - 50, card_height - 10), fill="black", font_size="12px", font_weight="bold")
    dwg.add(level)

    return dwg.tostring()


def display_parts_selection():
    selected_parts = {}
    for part, options in PARTS.items():
        selected_parts[part] = st.sidebar.selectbox(f"Select {part}", options)
    return selected_parts


def display_level_selection():
    selected_level = st.sidebar.selectbox("Select Level", LEVELS)
    return selected_level


def display_size_selection(default_width, default_height):
    card_width = st.sidebar.slider("Card Width", min_value=50, max_value=150, value=default_width, step=5)
    card_height = st.sidebar.slider("Card Height", min_value=75, max_value=200, value=default_height, step=5)
    return card_width, card_height


def display_image_upload(card_width, card_height):
    uploaded_file = st.sidebar.file_uploader("Upload Card Background Image", type=["png", "jpg", "jpeg"])
    if uploaded_file is not None:
        image = Image.open(uploaded_file)
        image = image.resize((card_width, card_height))
        img_bytes = BytesIO()
        image.save(img_bytes, format="PNG")
        img_data = img_bytes.getvalue()
        img_href = "data:image/png;base64," + img_data.hex()
        return img_href
    else:
        return None


def display_card(selected_parts, selected_level, card_width, card_height, background):
    if background and os.path.exists(background):
        background_name = os.path.basename(background)
        st.sidebar.markdown(f"**Background Image:** {background_name}")
        background_color = "white"
    else:
        st.sidebar.markdown(f"**Background Image:** {selected_parts['background'].capitalize()}")
        background_color = selected_parts["background"]

    card_svg = draw_card(background_color,
                         selected_parts["suit"],
                         selected_parts["value"],
                         selected_level,
                         card_width,
                         card_height)

    # Add background
    if background and os.path.exists(background):
        img = svgwrite.image.Image(href=background, size=(f"{card_width}px", f"{card_height}px"))
        dwg = svgwrite.fromstring(card_svg)
        dwg.add(img)
        card_svg = dwg.tostring()

    st.write(f'<svg viewBox="0 0 {card_width} {card_height}">{card_svg}</svg>', unsafe_allow_html=True)

    # Create a download link for the card SVG
    svg_content = '<?xml version="1.0" encoding="utf-8" ?>' + card_svg
    st.download_button("Download Card as SVG", svg_content, "card.svg", "text/plain")

    # Create button to level up card
    if selected_level != "godlike":
        if st.button("Level Up"):
            current_level_index = LEVELS.index(selected_level)
            new_level = LEVELS[current_level_index + 1]
            selected_level = new_level
            st.sidebar.warning(f"Card has been leveled up to {selected_level}")
    else:
        st.sidebar.warning("This is the highest level")