-
-
Notifications
You must be signed in to change notification settings - Fork 892
/
Copy pathcustom_elements.py
171 lines (137 loc) Β· 4.12 KB
/
custom_elements.py
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
from .vector_elements import Vector, Rectangle
TEXT_INPUT_ELEMENT_TYPES = {
"TextArea": "Text",
"TextBox": "Entry"
}
class Button(Rectangle):
def __init__(self, node, frame, image_path, *, id_):
super().__init__(node, frame)
self.image_path = image_path
self.id_ = id_
self.frame = frame
def to_code(self):
return f"""
button_image_{self.id_} = PhotoImage(
file=relative_to_assets("{self.image_path}"))
button_{self.id_} = Button(
image=button_image_{self.id_},
borderwidth=0,
highlightthickness=0,
activebackground="{self.frame.color()}",
command=lambda: print("button_{self.id_} clicked"),
relief="flat"
)
button_{self.id_}.place(
x={self.x},
y={self.y},
width={self.width},
height={self.height}
)
"""
class Text(Vector):
def __init__(self, node, frame):
super().__init__(node)
self.x, self.y = self.position(frame)
self.width, self.height = self.size()
self.text_color = self.color()
self.font, self.font_size = self.font_property()
self.text = self.characters.replace("\n", "\\n")
@property
def characters(self) -> str:
string: str = self.node.get("characters")
text_case: str = self.style.get("textCase", "ORIGINAL")
if text_case == "UPPER":
string = string.upper()
elif text_case == "LOWER":
string = string.lower()
elif text_case == "TITLE":
string = string.title()
return string
@property
def style(self):
# TODO: Native conversion
return self.node.get("style")
@property
def character_style_overrides(self):
return self.node.get("characterStyleOverrides")
@property
def style_override_table(self):
# TODO: Native conversion
return self.node.get("styleOverrideTable")
def font_property(self):
style = self.node.get("style")
font_name = style.get("fontPostScriptName")
if font_name is None:
font_name = style["fontFamily"]
font_name = font_name.replace('-', ' ')
font_size = style["fontSize"]
return font_name, font_size
def to_code(self):
return f"""
canvas.create_text(
{self.x},
{self.y},
anchor="nw",
text="{self.text}",
fill="{self.text_color}",
font=("{self.font}", {int(self.font_size)} * -1)
)
"""
class Image(Vector):
def __init__(self, node, frame, image_path, *, id_):
super().__init__(node)
self.x, self.y = self.position(frame)
width, height = self.size()
self.x += width // 2
self.y += height // 2
self.image_path = image_path
self.id_ = id_
def to_code(self):
return f"""
image_image_{self.id_} = PhotoImage(
file=relative_to_assets("{self.image_path}"))
image_{self.id_} = canvas.create_image(
{self.x},
{self.y},
image=image_image_{self.id_}
)
"""
class TextEntry(Vector):
def __init__(self, node, frame, image_path, *, id_):
super().__init__(node)
self.id_ = id_
self.image_path = image_path
self.x, self.y = self.position(frame)
width, height = self.size()
self.x += width / 2
self.y += height / 2
self.bg_color = self.color()
corner_radius = self.get("cornerRadius", 0)
if corner_radius > height / 2:
corner_radius = height / 2
self.entry_width = width - (corner_radius * 2)
self.entry_height = height - 2
self.entry_x, self.entry_y = self.position(frame)
self.entry_x += corner_radius
self.entry_type = TEXT_INPUT_ELEMENT_TYPES.get(self.get("name"))
def to_code(self):
return f"""
entry_image_{self.id_} = PhotoImage(
file=relative_to_assets("{self.image_path}"))
entry_bg_{self.id_} = canvas.create_image(
{self.x},
{self.y},
image=entry_image_{self.id_}
)
entry_{self.id_} = {self.entry_type}(
bd=0,
bg="{self.bg_color}",
highlightthickness=0
)
entry_{self.id_}.place(
x={self.entry_x},
y={self.entry_y},
width={self.entry_width},
height={self.entry_height}
)
"""