-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphics.py
135 lines (115 loc) · 4.68 KB
/
graphics.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
import tkinter
import time
class graphics:
#import ipdb; ipdb.set_trace()
def __init__(self, w, h, title):
''' Initialize the graphics object.
Creates a new tkinter Tk object,
and a tkinter Canvas object,
placed insize the Tk object.
'''
self.primary = tkinter.Tk()
self.primary.title(title)
self.primary.geometry('%dx%d+%d+%d' % (w, h, 50, 100))
self.canvas = tkinter.Canvas(self.primary, width=w, height=h, highlightthickness=0)
self.canvas.focus_set()
self.canvas.pack()
self.mouse_x = 0
self.mouse_y = 0
self.__handle_motion()
'''
BEGIN PRIVATE FUNCTION(S)
'''
def __handle_motion(self):
''' Ensure mouse x and y coordinates are updated when mouse moves.
'''
def motion_action(event):
self.mouse_x = event.x
self.mouse_y = event.y
self.canvas.bind('<Motion>', motion_action)
'''
END PRIVATE FUNCTION(S)
'''
def text(self, x, y, content, fill='black', size=17):
''' Draw text on the canvas.
Must always specify the text, x, y position.
Can optionally specify the fill color and size.
'''
text = self.canvas.create_text(x, y, text=content, fill=fill, font=('Arial', size), anchor='nw')
self.canvas.move(text, 0, 0)
def set_left_click_action(self, callee):
''' Call the callee function whenever the left click happens.
callee should take two parameters, the mouse x and mouse y coordinates.
'''
def left_click_action(event):
callee(self, event.x, event.y)
''' <Button-1> is the left-most mouse button '''
self.canvas.bind('<Button-1>', left_click_action)
def set_right_click_action(self, callee):
''' Call the callee function whenever the right click happens.
callee should take two parameters, the mouse x and mouse y coordinates.
'''
def right_click_action(event):
callee(self, event.x, event.y)
''' <Button-2> or <Button-3> is the right-most mouse button.
Both are set just in case '''
self.canvas.bind('<Button-2>', right_click_action)
self.canvas.bind('<Button-3>', right_click_action)
def set_keyboard_action(self, callee):
''' Call the callee function whenever a keyboard key is pressed.
callee should take one parameter, a char representing the key.
'''
def keyboard_action(event):
callee(self, event.char)
self.canvas.bind('<KeyPress>', keyboard_action)
def get_color_string(self, red, green, blue):
''' accepts three ints that should represent and RGB color.
Returns a hex string'''
hex_string = hex(red)[2:].rjust(2, '0') + \
hex(green)[2:].rjust(2, '0') + \
hex(blue)[2:].rjust(2, '0')
return '#' + hex_string
def triangle(self, x1, y1, x2, y2, x3, y3, fill='black'):
''' Draw a triangle.
The three corners of the triangle are specified with the parameter coordinates.
'''
r = self.canvas.create_polygon(x1, y1, x2, y2, x3, y3, fill=fill)
self.canvas.move(r, 0, 0)
def line(self, x1, y1, x2, y2, fill='black', width=3):
''' Draw a line.
The two ends of the line are specified with the parameter coordinates.
'''
r = self.canvas.create_line(x1, y1, x2, y2, fill=fill, width=width)
self.canvas.move(r, 0, 0)
def ellipse(self, x, y, w, h, fill='black'):
''' Draw an ellipse on the canvas.
Specify x, y (center of ellipse) and width / height.
'''
r = self.canvas.create_oval(x-(w/2), y-(h/2), x+(w/2), y+(h/2), fill=fill)
self.canvas.move(r, 0, 0)
def rectangle(self, x, y, w, h, fill='black'):
''' Draw a rectangle on the canvas.
Specify x, y (top-left corner) and width / height.
'''
r = self.canvas.create_rectangle(x, y, x+w, h+y, fill=fill)
self.canvas.move(r, 0, 0)
def update(self):
''' Does an idle task update and regular update.
'''
self.primary.update_idletasks()
self.primary.update()
def frame_space(self, frame_rate):
''' Sleeps for a time that corresponds to the provided frame rate.
'''
sleep_ms = 1.0 / float(frame_rate)
time.sleep(sleep_ms)
def update_frame(self, frame_rate):
''' Updates and sleeps.
This should be called at the end of each iteration of a users draw loop.
'''
self.update()
self.frame_space(frame_rate)
def clear(self):
''' Clears the canvas.
'''
self.canvas.delete('all')