Scotsman Site Admin
Joined: 03 Aug 2004 Posts: 705 Location: MadWolf Software
|
Posted: Fri Jul 14, 2006 9:17 pm Post subject: Room drawing script |
|
|
Code: | import manor
global draw_on, lines, numLines, remLines, I_Send, red, green, blue
draw_on = 0
numLines = 0
lines = []
remLines = []
I_Send = 0
red = 0
green = 0
blue = 0
class remRecv:
def __init__(self, userID, lineNum):
self.userID = userID
self.lineNum = lineNum
class stroke:
def __init__(self, red, green, blue, x, y):
self.red = red
self.green = green
self.blue = blue
self.points = shape()
self.points.move(x, y)
def point(self, x, y):
self.points.line(x, y)
def bounds(self):
return self.points.bounds().inset(-1, -1)
def draw(self, gworld):
gworld.foreColor(0xFF, self.red, self.green, self.blue)
self.points.draw(gworld, 0, 0)
def newStroke(userID, data, params):
global lines, numLines, remLines
r, g, b, x, y = data
lines.append(stroke(r, g, b, x, y))
count = 0
for i in remLines:
if i.userID == userID:
del remLines[count]
count += 1
remLines.append(remRecv(userID, numLines))
numLines += 1
def newPoint(userID, data, params):
global lines, remLines
x,y = data
for i in remLines:
if i.userID == userID:
lines[i.lineNum].point(x, y)
manor.dirtyRect(lines[i.lineNum].bounds())
def fullStroke(userID, data, params):
global lines, numLines
r, g, b, points = data
lines.append(stroke(r, g, b, 0, 0))
lines[numLines].points = shape(points);
manor.dirtyRect(lines[numLines].bounds())
numLines += 1;
def clearStrokes(userID, data, params):
global lines, numLines
for i in lines:
manor.dirtyRect(i.bounds())
lines = []
numLines = 0
def sendStrokes(userID, data, params):
global lines, I_Send
if I_Send == 1:
for i in lines:
manor.sendData("stroke", [userID], [i.red, i.green, i.blue, str(i.points)])
def mouseTracker(params):
global lines, currLine, lx, ly
if manor.stillDown() != 0:
x,y = manor.getMouseRmPos()
if x != lx or y != ly:
lx = x
ly = y
lines[currLine].point(x,y)
manor.broadcastData("point", 0, [x, y])
manor.dirtyRect(lines[currLine].bounds())
manor.setTimer(0, mouseTracker, 0)
def mnr_mousedown(x, y):
global lines, draw_on, currLine, numLines, red, green, blue, lx, ly
handled = 0
if draw_on == 1:
lx = x
ly = y
lines.append(stroke(red, green, blue, x, y))
manor.broadcastData("new", 0, [red, green, blue, x, y])
currLine = numLines
numLines += 1
#print x,y
handled = 1
manor.setTimer(0, mouseTracker, 0)
return handled
def mnr_render(gworld, layer):
global lines
if layer == 2:
gworld.penSize(2, 2)
for i in lines:
i.draw(gworld)
return 0
def mnr_keydown(char, key):
global draw_on, red, green, blue
handled = 0
#print key
if key == 122 or key == 112:
handled = 1
if draw_on == 0:
draw_on = 1
manor.statusMsg("Draw mode on", 0)
else:
manor.statusMsg("", 0)
draw_on = 0
if draw_on == 1:
if key == 120 or key == 113:
red = 0xFF
green = 0
blue = 0
handled = 1
if key == 99 or key == 114:
red = 0
green = 0xFF
blue = 0
handled = 1
if key == 118 or key == 115:
red = 0
green = 0
blue = 0xFF
handled = 1
if key == 96 or key == 116:
red = 0
green = 0
blue = 0
handled = 1
return handled
def mnr_outchat(chatText):
global lines, numLines
if chatText == "'clean":
manor.broadcastData("clean", 0, 0)
for i in lines:
manor.dirtyRect(i.bounds())
lines = []
numLines = 0
return chatText
def mnr_enter(userID):
global I_Send
if userID == manor.myID() or manor.myID() == 0:
manor.logstr("")
manor.logstr("Drawing available")
manor.logstr("")
manor.logstr("F1 Toggles draw mode on and off")
manor.logstr("F2 Red pen")
manor.logstr("F3 Green pen")
manor.logstr("F4 Blue pen")
manor.logstr("F5 Black pen")
#print manor.getRoomUserCount()
if manor.getRoomUserCount() == 1:
I_Send = 1
def mnr_leave(userID):
global I_Send
if userID != manor.myID():
if (manor.getIdxRoomUser(0) == userID and manor.getIdxRoomUser(1) == manor.myID()) or (manor.getIdxRoomUser(0) == manor.myID()):
I_Send = 1
manor.regDataReceive("new", newStroke, 0)
manor.regDataReceive("point", newPoint, 0)
manor.regDataReceive("stroke", fullStroke, 0)
manor.regDataReceive("reqStroke", sendStrokes, 0)
manor.regDataReceive("clean", clearStrokes, 0)
manor.broadcastData("reqStroke", 0, 0) |
|
|