-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_handler.py
178 lines (144 loc) · 5.01 KB
/
event_handler.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
import gamelib as gamelib
from constants import *
import logic as logic
def save_ppm_clicked(x, y):
'''
Returns True if the save ppm button in the window was clicked.
False otherwise
'''
return SAVE_PPM_BUTTON[0] < x < SAVE_PPM_BUTTON[1] and HEIGHT_FILE_BUTTONS[0] < y < HEIGHT_FILE_BUTTONS[1]
def save_png_clicked(x, y):
'''
Returns True if the save png button in the window was clicked.
False otherwise
'''
return SAVE_PNG_BUTTON[0] < x < SAVE_PNG_BUTTON[1] and HEIGHT_FILE_BUTTONS[0] < y < HEIGHT_FILE_BUTTONS[1]
def upload_ppm_clicked(x, y):
'''
Returns True if the upload ppm button in the window was clicked.
False otherwise
'''
return LOAD_PPM_BUTTON[0] < x < LOAD_PPM_BUTTON[1] and HEIGHT_FILE_BUTTONS[0] < y < HEIGHT_FILE_BUTTONS[1]
def pixel_clicked(x, y):
'''
Returns True if any pixel in the window was clicked.
False otherwise
'''
x2 = PIXEL_ZONE[0] + PIXEL_SIZE * WIDTH_INITIAL_IMAGE
y2 = PIXEL_ZONE[1] + PIXEL_SIZE * HEIGHT_INITIAL_IMAGE
return PIXEL_ZONE[0] <= x <= x2 and PIXEL_ZONE[1] <= y <= y2
def drag_painting(paint, x, y, ev):
'''
If the user is dragging the mouse with a selected color and the bucket is not activated, returns True.
False otherwise
'''
return pixel_clicked(x, y) and paint['selected color'] != '' and not paint['bucket'] and ev.type == gamelib.EventType.Motion
def tool_bar_clicked(x, y):
'''
Returns True if any tool of the tool bar in the window was clicked.
False otherwise
'''
return HEIGHT_TOOL_BAR[0] <= y <= HEIGHT_TOOL_BAR[1] and UNDO[0] <= x <= CUSTOM_COLOR_3[1]
def shortcut_color_clicked(x, y):
'''
Returns True if any shortcut color in the window was clicked.
False otherwise
'''
return HEIGHT_COLOR_BAR[0] <= y <= HEIGHT_COLOR_BAR[1] and WIDTH_COLOR_BAR[0] <= x <= WIDTH_COLOR_BAR[1]
def undo_clicked(x):
'''
Returns True if the undo button in the window was clicked.
False otherwise
'''
return UNDO[0] < x < UNDO[1]
def redo_clicked(x):
'''
Returns True if the redo button in the window was clicked.
False otherwise
'''
return REDO[0] < x < REDO[1]
def bucket_clicked(x):
'''
Returns True if the bucket button in the window was clicked.
False otherwise
'''
return BUCKET[0] < x < BUCKET[1]
def eraser_clicked(x):
'''
Returns True if the eraser button in the window was clicked.
False otherwise
'''
return ERASER[0] < x < ERASER[1]
def trash_clicked(x):
'''
Returns True if the trash button in the window was clicked.
False otherwise
'''
return TRASH[0] < x < TRASH[1]
def pixeled_clicked(x):
'''
Returns True if the pixeled/unpixeled button in the window was clicked.
False otherwise
'''
return PIXELED[0] < x < PIXELED[1]
def input_colors_clicked(x):
'''
Returns True if the button to selecte a custom color in the window was clicked.
False otherwise
'''
return INPUT_COLORS[0] < x < INPUT_COLORS[1]
def custom_color1_clicked(x):
'''
Returns True if the custom color button in the window was clicked.
False otherwise
'''
return CUSTOM_COLOR_1[0] < x < CUSTOM_COLOR_1[1]
def custom_color2_clicked(x):
'''
Returns True if the custom color button in the window was clicked.
False otherwise
'''
return CUSTOM_COLOR_2[0] < x < CUSTOM_COLOR_2[1]
def custom_color3_clicked(x):
'''
Returns True if the custom color button in the window was clicked.
False otherwise
'''
return CUSTOM_COLOR_3[0] < x < CUSTOM_COLOR_3[1]
def handle_tool_clicked(paint, x):
'''
Handles the event of a tool being clicked in the window according to the x coordinate
if none of the tools was clicked, nothing happens
'''
if undo_clicked(x):
logic.undo_last_action(paint)
elif redo_clicked(x):
logic.redo_last_action(paint)
elif bucket_clicked(x):
logic.activate_bucket(paint)
# if bucket is clicked, the redo stack is cleared
paint['undone actions'].clear()
elif eraser_clicked(x):
logic.activate_eraser(paint)
# if a eraser is clicked, the redo stack is cleared
paint['undone actions'].clear()
elif trash_clicked(x):
logic.clear_paint(paint)
# if trash is clicked, the redo stack is cleared
paint['undone actions'].clear()
elif pixeled_clicked(x):
paint['pixeled'] = not paint['pixeled']
elif input_colors_clicked(x):
logic.select_custom_color(paint)
# if a color is clicked, the redo stack is cleared
paint['undone actions'].clear()
elif custom_color1_clicked(x):
logic.change_color_to_custom(paint, 0)
# if a color is clicked, the redo stack is cleared
paint['undone actions'].clear()
elif custom_color2_clicked(x):
logic.change_color_to_custom(paint, 1)
paint['undone actions'].clear()
elif custom_color3_clicked(x):
logic.change_color_to_custom(paint, 2)
paint['undone actions'].clear()