-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.py
131 lines (104 loc) · 3.99 KB
/
client.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
import time
from threading import Thread
from connection import Connection
from whiteboard import WhiteBoard
from UserDialog import UserDialog
class Client(Thread,WhiteBoard):
Objects = {'line': 'L', 'oval': 'O', 'circle': 'C', 'rectangle': 'R', 'square': 'S', 'drag': 'DR'}
def __init__(self):
self.conn = Connection()
Thread.__init__(self)
WhiteBoard.__init__(self)
self._init_mouse_event()
self.setDaemon(True)
self.isMouseDown = False
self.x_pos = None
self.y_pos = None
self.last_time = None
self.last_click_obj = None
self.line_x1,self.line_y1,self.line_x2,self.line_y2 = None, None, None, None
def _init_mouse_event(self):
self.drawing_area.bind("<Motion>", self.motion)
self.drawing_area.bind("<ButtonPress-1>", self.left_but_down)
self.drawing_area.bind("<ButtonRelease-1>", self.left_but_up)
def send_del_msg(self,event):
canvas_item_tuple = self.drawing_area.find_overlapping(event.x + 2, event.y + 2, event.x - 2, event.y - 2)
print(canvas_item_tuple)
if len(canvas_item_tuple) > 0:
to_delete_id = max(canvas_item_tuple)
tags = self.drawing_area.gettags(to_delete_id)
msgid = tags[0]
msg = ('Z', msgid)
self.conn.send_message(msg)
#(tpye,startx,starty,endx,endy,color)
#('D',startx,starty,endx,endy,self.color)
def left_but_down(self,event=None):
self.isMouseDown = True
self.x_pos = event.x
self.y_pos = event.y
self.last_time = time.time()
self.line_x1, self.line_y1 = event.x,event.y
if self.isMouseDown == True and self.drawing_tool == 'eraser':
self.send_del_msg(event)
try:
self.last_click_obj = self.drawing_area.gettags('current')[0]
print(self.last_click_obj)
except Exception:
pass
def left_but_up(self,event=None):
self.isMouseDown = False
print(event.x,event.y)
self.last_time = None
self.line_x2, self.line_y2 = event.x, event.y
if self.drawing_tool == 'text':
self.draw_text()
elif self.drawing_tool == 'drag':
self.do_drag()
else:
self.draw_one_obj()
def do_drag(self):
if self.last_click_obj != None:
msg = ('DR', self.last_click_obj, self.line_x2 - self.line_x1, self.line_y2 - self.line_y1)
self.conn.send_message(msg)
def draw_text(self):
text_to_draw = UserDialog._Text
msg = ('T', self.line_x1, self.line_y1, self.color,text_to_draw)
self.conn.send_message(msg)
def draw_one_obj(self):
tool = self.drawing_tool
if tool not in Client.Objects.keys():
return
else:
cmd_type = Client.Objects[tool]
msg = (cmd_type, self.line_x1, self.line_y1, self.line_x2, self.line_y2, self.color)
self.conn.send_message(msg)
# (tpye,startx,starty,endx,endy,color)
# ('D',startx,starty,endx,endy,self.color)
def motion(self,event=None):
if self.isMouseDown == True and self.drawing_tool == 'pencil':
now = time.time()
if now - self.last_time < 0.02:
print('too fast')
return
self.last_time = now
msg = ('D',self.x_pos,self.y_pos,event.x,event.y,self.color)
self.conn.send_message(msg)
self.x_pos = event.x
self.y_pos = event.y
elif self.isMouseDown == True and self.drawing_tool == 'eraser':
self.send_del_msg(event)
def run(self):
# print('run')aa
while True:
msg = self.conn.receive_msg()
self.draw_from_msg(msg)
print(msg)
if msg == 'xxx':
pass
# print('i am running')
# time.sleep(0.1)
if __name__ == '__main__':
client = Client()
print('startt')
client.start()
client.show_window()