-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.py
217 lines (185 loc) · 8.43 KB
/
ui.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
from constants import *
import gamelib as gamelib
def show_title():
'''
Shows the title of the paint
'''
gamelib.draw_rectangle(0, 0, WINDOW_WIDTH, HEIGHT_TITLE, fill=TITLE_COLOR)
gamelib.draw_image('icons/logo.ppm', 182, 4)
gamelib.draw_text('ALGOPAINT', WIDTH_TEXT_TITLE,
HEIGHT_TEXT_TITLE, bold=True, fill='black')
def show_save_load_image_buttons(paint):
'''
Shows the buttons to load/save images in their respective positions of the paint
'''
gamelib.draw_rectangle(LOAD_PPM_BUTTON[0], HEIGHT_FILE_BUTTONS[0], LOAD_PPM_BUTTON[1],
HEIGHT_FILE_BUTTONS[1], fill='white', activeoutline='black', outline='black', activewidth=3)
gamelib.draw_rectangle(SAVE_PPM_BUTTON[0], HEIGHT_FILE_BUTTONS[0], SAVE_PPM_BUTTON[1],
HEIGHT_FILE_BUTTONS[1], fill='white', activeoutline='black', outline='black', activewidth=3)
gamelib.draw_rectangle(SAVE_PNG_BUTTON[0], HEIGHT_FILE_BUTTONS[0], SAVE_PNG_BUTTON[1],
HEIGHT_FILE_BUTTONS[1], fill='white', activeoutline='black', outline='black', activewidth=3)
image_height = HEIGHT_FILE_TEXT - 12
x_load_ppm = LOAD_PPM_BUTTON[0] + 2
x_save_ppm = SAVE_PPM_BUTTON[0] + 3
x_save_png = SAVE_PNG_BUTTON[0] + 3
gamelib.draw_image('icons/upload.ppm', x_load_ppm, image_height)
gamelib.draw_image('icons/save.ppm', x_save_ppm, image_height)
gamelib.draw_image('icons/save.ppm', x_save_png, image_height)
gamelib.draw_text('Upload PPM', X_LOAD_PPM_TEXT,
HEIGHT_FILE_TEXT, bold=True, fill='black')
gamelib.draw_text('Save as PPM', X_SAVE_PPM_TEXT,
HEIGHT_FILE_TEXT, bold=True, fill='black')
gamelib.draw_text('Save as PNG', X_SAVE_PNG_TEXT,
HEIGHT_FILE_TEXT, bold=True, fill='black')
def show_pixels(paint):
'''
Shows the pixels to draw in the interface.
'''
outline = 'black' if paint['pixeled'] else paint['selected color']
for pixel in paint['pixels'].values():
x1, y1, x2, y2 = pixel['pos']
color = pixel['color']
# either black (to see pixels) or the color of the pixel
outline = 'black' if paint['pixeled'] else color
gamelib.draw_rectangle(x1, y1, x2, y2, fill=color, outline=outline)
def show_shortcut_colors(paint):
'''
Shows the shortcut colors in the interface
'''
for i, color in enumerate(MAIN_COLORS):
distance_to_first_color_x1 = (
WIDTH_COLOR_BOX + SEPARATION_BETWEEN_COLORS) * i
x1 = X1_FIRST_COLOR + distance_to_first_color_x1
x2 = X1_FIRST_COLOR + WIDTH_COLOR_BOX + distance_to_first_color_x1
if color == paint['selected color']:
gamelib.draw_rectangle(
x1, HEIGHT_COLOR_BAR[0], x2, HEIGHT_COLOR_BAR[1], fill=color, outline='black', width=4)
else:
gamelib.draw_rectangle(
x1, HEIGHT_COLOR_BAR[0], x2, HEIGHT_COLOR_BAR[1], fill=color, activeoutline='black', activewidth=2)
def show_tool_bar(paint):
'''
Shows the tool bar in the interface
'''
show_undo_redo_buttons(paint)
show_bucket(paint)
show_eraser(paint)
show_pixeled_option()
show_trash()
show_input_color(paint)
def show_undo_redo_buttons(paint):
'''
Shows the 'UNDO' and 'REDO' buttons in their respective positions of the paint
'''
gamelib.draw_rectangle(UNDO[0], HEIGHT_TOOL_BAR[0], UNDO[1], HEIGHT_TOOL_BAR[1],
outline='black', activeoutline='black', activewidth=3)
x_undo_image = UNDO[0] + 6
x_redo_image = REDO[0] + 6
y_undo_image = y_redo_image = HEIGHT_TOOL_BAR[0] + 7
gamelib.draw_image('icons/undo1.ppm', x_undo_image, y_undo_image)
gamelib.draw_rectangle(REDO[0], HEIGHT_TOOL_BAR[0], REDO[1], HEIGHT_TOOL_BAR[1],
activeoutline='black', outline='black', activewidth=3)
gamelib.draw_image('icons/redo1.ppm', x_redo_image, y_redo_image)
def show_bucket(paint):
'''
Shows the bucket in the interface
'''
bucket_width = 4 if paint['bucket'] else 1
gamelib.draw_rectangle(BUCKET[0], HEIGHT_TOOL_BAR[0], BUCKET[1], HEIGHT_TOOL_BAR[1],
outline='black', width=bucket_width, activeoutline='black', activewidth=3)
gamelib.draw_image('icons/bucket.ppm',
BUCKET[0] + 6, HEIGHT_TOOL_BAR[0] + 7)
def show_eraser(paint):
'''
Shows the erase button in the interface
'''
if paint['eraser']:
gamelib.draw_rectangle(
ERASER[0], HEIGHT_TOOL_BAR[0], ERASER[1], HEIGHT_TOOL_BAR[1], outline='black', width=4)
else:
gamelib.draw_rectangle(ERASER[0], HEIGHT_TOOL_BAR[0], ERASER[1],
HEIGHT_TOOL_BAR[1], outline='black', activeoutline='black', activewidth=3)
gamelib.draw_image('icons/eraser.ppm',
ERASER[0] + 6, HEIGHT_TOOL_BAR[0] + 7)
def show_pixeled_option():
'''
Shows the pixeled/unpixeled button in the interface
'''
gamelib.draw_rectangle(PIXELED[0], HEIGHT_TOOL_BAR[0], PIXELED[1],
HEIGHT_TOOL_BAR[1], outline='black', activeoutline='black', activewidth=3)
gamelib.draw_image('icons/border.ppm',
PIXELED[0] + 6, HEIGHT_TOOL_BAR[0] + 7)
def show_trash():
'''
Shows the trash button in the interface
'''
gamelib.draw_rectangle(TRASH[0], HEIGHT_TOOL_BAR[0], TRASH[1], HEIGHT_TOOL_BAR[1],
outline='black', activeoutline='black', activewidth=3)
gamelib.draw_image('icons/trash.ppm',
TRASH[0] + 6, HEIGHT_TOOL_BAR[0] + 7)
def show_input_color(paint):
'''
Shows the input color option in the interface
'''
gamelib.draw_rectangle(INPUT_COLORS[0], HEIGHT_TOOL_BAR[0], INPUT_COLORS[1],
HEIGHT_TOOL_BAR[1], outline='black', activeoutline='black', activewidth=3)
gamelib.draw_image('icons/palette.ppm',
INPUT_COLORS[0] + 6, HEIGHT_TOOL_BAR[0] + 7)
def show_custom_colors(paint):
'''
Shows the custom colors in the interface
'''
show_custom_color1(paint)
show_custom_color2(paint)
show_custom_color3(paint)
def show_custom_color1(paint):
'''
Shows the custom color 1 in the interface
'''
custom_color_selected = paint['custom colors selected'][0]
custom_color = paint['custom colors'][0]
if custom_color_selected and custom_color != 'white':
gamelib.draw_rectangle(CUSTOM_COLOR_1[0], HEIGHT_TOOL_BAR[0], CUSTOM_COLOR_1[1],
HEIGHT_TOOL_BAR[1], fill=custom_color, outline='black', width=4)
else:
gamelib.draw_rectangle(CUSTOM_COLOR_1[0], HEIGHT_TOOL_BAR[0], CUSTOM_COLOR_1[1],
HEIGHT_TOOL_BAR[1], fill=custom_color, outline='black')
def show_custom_color2(paint):
'''
Shows the custom color 2 in the interface
'''
custom_color_selected = paint['custom colors selected'][1]
custom_color = paint['custom colors'][1]
if custom_color_selected and custom_color != 'white':
gamelib.draw_rectangle(CUSTOM_COLOR_2[0], HEIGHT_TOOL_BAR[0], CUSTOM_COLOR_2[1],
HEIGHT_TOOL_BAR[1], fill=custom_color, outline='black', width=4)
else:
gamelib.draw_rectangle(CUSTOM_COLOR_2[0], HEIGHT_TOOL_BAR[0], CUSTOM_COLOR_2[1],
HEIGHT_TOOL_BAR[1], fill=custom_color, outline='black')
def show_custom_color3(paint):
'''
Shows the custom color 3 in the interface
'''
custom_color_selected = paint['custom colors selected'][2]
custom_color = paint['custom colors'][2]
if custom_color_selected and custom_color != 'white':
gamelib.draw_rectangle(CUSTOM_COLOR_3[0], HEIGHT_TOOL_BAR[0], CUSTOM_COLOR_3[1],
HEIGHT_TOOL_BAR[1], fill=custom_color, outline='black', width=4)
else:
gamelib.draw_rectangle(CUSTOM_COLOR_3[0], HEIGHT_TOOL_BAR[0], CUSTOM_COLOR_3[1],
HEIGHT_TOOL_BAR[1], fill=custom_color, outline='black')
def show_paint(paint):
'''Shows all the elements in the UI'''
gamelib.draw_begin()
# background
gamelib.icon('icons/icon.ppm')
gamelib.draw_rectangle(
0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, fill=BACKGROUND_COLOR)
show_title()
# elements of UI
show_save_load_image_buttons(paint)
show_pixels(paint)
show_shortcut_colors(paint)
show_tool_bar(paint)
show_custom_colors(paint)
gamelib.draw_end()