def animate(self): FPS = 2 clock = Clock(float(1) / FPS).activate() clock.link((clock, "outbox"), (self, "newframe")) for x in self.layers: if not self.dataReady("newframe"): self.pause() self.recv("newframe") print("now")
def main(self): """Main event loop, also handles input from other components""" displayservice = PygameDisplay.getDisplayService() self.link((self, "display_signal"), displayservice) self.send(self.disprequest, "display_signal") for _ in self.waitBox("callback"): yield 1 self.display = self.recv("callback") self.layers.append(self.display) # f = os.path.join('', "pennyarcade.gif") # x = pygame.image.load(f) # colorkey = x.get_at((0, 0)) # if colorkey is True: # x.set_colorkey(colorkey, pygame.RLEACCEL) # self.layers.append(x) # self.display = x # self.activeLayIn = len(self.layers)-1 # self.activeLayer = self.layers[self.activeLayIn] # self.display.blit( x, (0,0) ) layerDisp = TextDisplayer(size=(20, 20), position=(520, 10)).activate() self.link((self, "laynum"), (layerDisp, "inbox")) self.send( { "ADDLISTENEVENT": pygame.MOUSEBUTTONDOWN, "surface": self.display }, "display_signal") self.send( { "ADDLISTENEVENT": pygame.MOUSEBUTTONUP, "surface": self.display }, "display_signal") self.send( { "ADDLISTENEVENT": pygame.MOUSEMOTION, "surface": self.display }, "display_signal") self.send({ "ADDLISTENEVENT": pygame.KEYDOWN, "surface": self.display }, "display_signal") self.activeLayer = self.layers[self.activeLayIn] self.send(self.activeLayIn, "laynum") image = pygame.Surface((20, 20)) pygame.draw.circle(image, (1, 1, 1), (10, 10), self.toolSize, 0) # pygame.draw.circle(image, self.selectedColour, (10, 10), 8, 2) image.set_at((9, 9), (255, 255, 255)) image.set_colorkey(0, pygame.RLEACCEL) cursor = GfxCursor(self.display, image, (10, 10)) self.drawBG(True) self.blitToSurface() FPS = 1 clock = Clock(float(1) / FPS).activate() clock.link((clock, "outbox"), (self, "newframe")) done = False while not done: dirtyrects = [] dirtyrects.extend([cursor.hide()]) if not self.anyReady(): self.pause() yield 1 while self.dataReady("control"): cmsg = self.recv("control") if isinstance(cmsg, producerFinished) or isinstance( cmsg, shutdownMicroprocess): self.send(cmsg, "signal") done = True while self.dataReady("newframe") and self.playing: self.recv("newframe") if self.currentFrame != 0: self.layers[self.currentFrame].set_alpha(0) self.currentFrame = self.currentFrame + 1 if self.currentFrame == len(self.layers) - 1: self.playing = False self.layers[self.currentFrame].set_alpha(255) self.blitToSurface() while self.dataReady("inbox"): for event in self.recv("inbox"): if isinstance(event, tuple): self.send((event, ), "outbox") if event[0] == "Layer": if event[1] == "Add": yield WaitComplete(self.addLayer()) self.activeLayIn = len(self.layers) - 1 self.activeLayer = self.layers[ self.activeLayIn] self.drawBG() if self.animator and len(self.layers) - 2 >= 1: for x in self.layers: s.set_alpha = 0 self.activeLayer.blit( self.layers[len(self.layers) - 2], (0, 0)) self.blitToSurface() elif event[1] == "Delete": self.send( producerFinished(message=self.activeLayer), "display_signal") self.layers.remove(self.activeLayer) self.activeLayIn = 0 self.activeLayer = self.layers[ self.activeLayIn] # print (self.layers) if event[1] == "Next": if self.animator and self.activeLayIn != 0: self.activeLayer.set_alpha(0) if self.activeLayIn == len(self.layers) - 1: self.activeLayIn = 0 self.activeLayer = self.layers[ self.activeLayIn] else: self.activeLayIn += 1 self.activeLayer = self.layers[ self.activeLayIn] if self.animator: self.activeLayer.set_alpha(255) elif event[1] == "Prev": if self.animator and self.activeLayIn != 0: self.activeLayer.set_alpha(0) if self.activeLayIn == 0: self.activeLayIn = len(self.layers) - 1 self.activeLayer = self.layers[ self.activeLayIn] else: self.activeLayIn -= 1 self.activeLayer = self.layers[ self.activeLayIn] if self.animator: self.activeLayer.set_alpha(255) self.send(self.activeLayIn, "laynum") elif event[0] == "Tool": self.tool = event[1] elif event[0] == "Size": self.toolSize = event[1] / 3 pygame.draw.circle(image, self.selectedColour, (10, 10), self.toolSize / 3, 0) image.set_at((9, 9), (255, 255, 255)) image.set_colorkey(0, pygame.RLEACCEL) cursor = GfxCursor(self.display, image, (10, 10)) elif event[0] == "Alpha": self.layers[self.activeLayIn].set_alpha(event[1]) self.blitToSurface() # print (self.activeLayer.get_alpha()) elif event[0] == 'Colour': self.selectedColour = event[1] pygame.draw.circle(image, self.selectedColour, (10, 10), self.toolSize / 3, 0) image.set_at((9, 9), (255, 255, 255)) image.set_colorkey(0, pygame.RLEACCEL) cursor = GfxCursor(self.display, image, (10, 10)) elif event[0] == 'Save': self.save(event[1]) break if event.type == pygame.MOUSEBUTTONDOWN: cursor.disable() if self.tool == "Circle": if event.button == 1: self.oldpos = event.pos self.drawing = True if self.tool == "Eraser": self.selectedColour = self.backgroundColour self.tool = "Line" if self.tool == "Line": if event.button == 1: self.drawing = True if self.tool == "Bucket": self.floodFill(event.pos[0], event.pos[1], self.selectedColour, self.activeLayer.get_at(event.pos)) if self.tool == "Eyedropper": self.selectedColour = self.activeLayer.get_at( event.pos) if event.button == 3: self.addLayer() #self.oldpos = None #self.drawBG() #self.blitToSurface() #self.send(("clear",), "outbox") elif event.type == (pygame.KEYDOWN): if event.key == pygame.K_c: image = pygame.image.load( os.path.join('', 'pennyarcade.gif')) yield WaitComplete(self.addLayer()) self.activeLayIn = len(self.layers) - 1 self.activeLayer = self.layers[self.activeLayIn] self.drawBG() self.activeLayer.blit(image, (10, 10)) self.blitToSurface() self.send(self.activeLayIn, "laynum") elif event.key == pygame.K_s: """Testing a different brushing technique, bliting a brush""" print(self.size, self.toolSize) yield WaitComplete(self.addBrush()) self.activeBrush = self.brushes[len(self.brushes) - 1] self.activeBrush.fill(self.backgroundColour) pygame.draw.circle( self.activeBrush, self.selectedColour, (self.toolSize / 2, self.toolSize / 2), self.toolSize, 0) elif event.key == pygame.K_a: self.animator = True elif event.key == pygame.K_l: self.layers[1].blit(self.layers[1], (100, 100)) # temp = self.layers[1] # self.layers[1].fill(self.backgroundColour) # self.layers[1].blit( temp, (100,100) ) elif event.key == pygame.K_o: for x in self.layers: x.set_alpha(0) self.layers[0].set_alpha(255) self.currentFrame = 0 while self.dataReady( "newframe" ): # empty if there's anything there self.recv("newframe") self.playing = True self.blitToSurface() elif event.type == pygame.MOUSEBUTTONUP and event.button == 1: cursor.enable() if self.tool == "Circle": rad = int( math.sqrt(( (event.pos[0] - self.oldpos[0])**2) + ( (event.pos[1] - self.oldpos[1])**2))) pygame.draw.circle(self.activeLayer, self.selectedColour, self.oldpos, rad, 0) circle = ("circle", self.oldpos, rad) # self.send((circle,), "outbox") self.blitToSurface() self.drawing = False self.oldpos = None elif event.type == pygame.MOUSEMOTION: cursor.update(event) if self.tool == "Line": if self.drawing and self.innerRect.collidepoint( *event.pos): if self.oldpos == None: self.oldpos = event.pos else: # pygame.draw.circle(self.activeLayer, self.selectedColour, self.oldpos, self.toolSize, 0) r = pygame.draw.line( self.activeLayer, self.selectedColour, self.oldpos, event.pos, self.toolSize) dirtyrects.append(r) # self.activeLayer.blit(self.activeBrush, event.pos) FAILED TECHNIQUE line = ("line", self.oldpos, event.pos) self.send((line, ), "outbox") self.oldpos = event.pos self.blitToSurface() self.pause() pygame.time.delay(5) dirtyrects.extend([cursor.show()]) pygame.display.update(dirtyrects) yield 1
if (isinstance(msg, producerFinished) or isinstance(msg, shutdownMicroprocess)): self.send(msg, "signal") break if not self.anyReady(): self.pause() yield 1 if __name__ == "__main__": from Kamaelia.Util.Clock import CheapAndCheerfulClock as Clock from Kamaelia.Util.Console import ConsoleEchoer FPS = 60 clock = Clock(float(1) / FPS).activate() clock2 = Clock(float(1) / FPS).activate() xyPad = XYPad().activate() xyPad2 = XYPad(size=(200, 200), bouncingPuck=False, position=(210, 0), bgcolour=(0, 0, 0), fgcolour=(255, 255, 255), positionMsg="p2").activate() ce = ConsoleEchoer().activate() clock.link((clock, "outbox"), (xyPad, "newframe")) clock2.link((clock2, "outbox"), (xyPad2, "newframe")) xyPad.link((xyPad, "outbox"), (ce, "inbox")) xyPad2.link((xyPad2, "outbox"), (ce, "inbox")) Axon.Scheduler.scheduler.run.runThreads()
from Kamaelia.Apps.Jam.Protocol.Osc import Osc from Kamaelia.Apps.Jam.Internet.UDP import SimplePeer from Kamaelia.Apps.Jam.UI.XYPad import XYPad from Kamaelia.Apps.Jam.Util.SendQuantizer import SendQuantizer if __name__ == "__main__": FPS = 60 class CollisionFilter(): def filter(self, input): if input[0] != "position": return input else: return None Graphline( clock=Clock(float(1) / FPS), xyPad=XYPad(), quantizer=SendQuantizer(beatQuantize=1), # Filter does not pause - cpu munch-a-rama filter=Filter(filter=CollisionFilter()), osc=Osc("/UITest"), peer=SimplePeer(receiver_addr="127.0.0.1", receiver_port=2000), linkages={ ("clock", "outbox"): ("xyPad", "newframe"), ("xyPad", "outbox"): ("filter", "inbox"), ("filter", "outbox"): ("quantizer", "inbox"), ("quantizer", "outbox"): ("osc", "inbox"), ("osc", "outbox"): ("peer", "inbox") }).run()
# http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from Kamaelia.Util.Clock import CheapAndCheerfulClock as Clock from Kamaelia.Chassis.Graphline import Graphline from Kamaelia.Util.Console import ConsoleEchoer from Kamaelia.Apps.Jam.UI.XYPad import XYPad from Kamaelia.Apps.Jam.Protocol.Osc import Osc from Kamaelia.Apps.Jam.Internet.UDP import SimplePeer if __name__ == "__main__": FPS = 60 Graphline(clock = Clock(float(1)/FPS), xyPad = XYPad(), osc = Osc("/UITest"), peer = SimplePeer(receiver_addr="127.0.0.1", receiver_port=2000), linkages={("clock", "outbox"):("xyPad", "newframe"), ("xyPad", "outbox"):("osc", "inbox"), ("osc", "outbox"):("peer", "inbox"), } ).run()
def main(self): """Main loop.""" # pgd = PygameDisplay( width=300, height=550 ).activate() # PygameDisplay.setDisplayService(pgd) displayservice = PygameDisplay.getDisplayService() self.link((self, "display_signal"), displayservice) self.send(self.dispRequest, "display_signal") for _ in self.waitBox("callback"): yield 1 self.display = self.recv("callback") # colour buttons if self.colourSelector: rgbutton = Button(caption="Red/Green", position=(10, 170), msg=("Colour", "RG")).activate() rbbutton = Button(caption="Red/Blue", position=(80, 170), msg=("Colour", "RB")).activate() gbbutton = Button(caption="Green/Blue", position=(145, 170), msg=("Colour", "GB")).activate() self.link((rgbutton, "outbox"), (self, "buttons")) self.link((rbbutton, "outbox"), (self, "buttons")) self.link((gbbutton, "outbox"), (self, "buttons")) # tool buttons circleb = Button(caption="Circle", position=(10, 10), msg=(("Tool", "Circle"), )).activate() eraseb = Button(caption="Eraser", position=(100, 10), msg=(("Tool", "Eraser"), )).activate() lineb = Button(caption="Line", position=(10, 50), msg=(("Tool", "Line"), )).activate() bucketb = Button(caption="Bucket", position=(10, 90), msg=(("Tool", "Bucket"), )).activate() eyeb = Button(caption="Eyedropper", position=(10, 130), msg=(("Tool", "Eyedropper"), )).activate() addlayerb = Button(caption="Add Layer", position=(10, 540), msg=(("Layer", "Add"), )).activate() prevlayerb = Button(caption="<-", position=(80, 540), msg=(("Layer", "Prev"), )).activate() nextlayerb = Button(caption="->", position=(110, 540), msg=(("Layer", "Next"), )).activate() dellayerb = Button(caption="Delete", position=(140, 540), msg=(("Layer", "Delete"), )).activate() self.link((circleb, "outbox"), (self, "outbox"), passthrough=2) self.link((eraseb, "outbox"), (self, "outbox"), passthrough=2) self.link((lineb, "outbox"), (self, "outbox"), passthrough=2) self.link((bucketb, "outbox"), (self, "outbox"), passthrough=2) self.link((eyeb, "outbox"), (self, "outbox"), passthrough=2) self.link((addlayerb, "outbox"), (self, "outbox"), passthrough=2) self.link((prevlayerb, "outbox"), (self, "outbox"), passthrough=2) self.link((nextlayerb, "outbox"), (self, "outbox"), passthrough=2) self.link((dellayerb, "outbox"), (self, "outbox"), passthrough=2) SizePicker = XYPad(size=(255, 50), bouncingPuck=False, position=(10, 480), bgcolour=(0, 0, 0), fgcolour=(255, 255, 255), slider=True).activate() self.link((SizePicker, "outbox"), (self, "outbox"), passthrough=2) AlphaPicker = XYPad(size=(255, 20), bouncingPuck=False, position=(10, 575), bgcolour=(0, 0, 0), fgcolour=(255, 255, 255), slider=True, alpha=True).activate() self.link((AlphaPicker, "outbox"), (self, "outbox"), passthrough=2) #clock - don't really need this FPS = 60 clock = Clock(float(1) / FPS).activate() self.link((clock, "outbox"), (self, "newframe")) # Initial render so we don't see a blank screen self.drawBG() # self.render() if self.editable: self.send( { "ADDLISTENEVENT": pygame.MOUSEBUTTONDOWN, "surface": self.display }, "display_signal") self.send( { "ADDLISTENEVENT": pygame.MOUSEBUTTONUP, "surface": self.display }, "display_signal") self.send( { "ADDLISTENEVENT": pygame.MOUSEMOTION, "surface": self.display }, "display_signal") done = False while not done: if not self.anyReady(): self.pause() yield 1 while self.dataReady("buttons"): bmsg = self.recv("buttons") if bmsg[0] == "Colour": self.colours = bmsg[1] self.drawBG() while self.dataReady("control"): cmsg = self.recv("control") if (isinstance(cmsg, producerFinished)): self.send(cmsg, "signal") done = True while self.dataReady("inbox"): for event in self.recv("inbox"): if event.type == pygame.MOUSEBUTTONDOWN: self.clickTime = time.time() if self.slider: self.sliderPos = event.pos[0] self.drawBG() if self.display.get_rect().collidepoint(*event.pos): self.mouseDown = True self.isBouncing = False self.mousePositions = [] self.puckVel = [0, 0] self.puckPos = list(event.pos) self.lastMousePos = event.pos self.send( (self.messagePrefix + self.positionMsg, (float(self.puckPos[0]) / self.size[0], float(self.puckPos[1]) / self.size[1])), "localChanges") self.send((self.messagePrefix + "Velocity", self.puckVel), "localChanges") if event.type == pygame.MOUSEBUTTONUP: if self.mouseDown: if self.slider: self.sliderPos = event.pos[0] self.drawBG() if (self.bouncingPuck and time.time() - self.clickTime > 0.1): # Click and drag self.isBouncing = True if len(self.mousePositions): for i in xrange(2): # Use the average of the last 50 # relative mouse positions positions = [ x[i] for x in self.mousePositions ] self.puckVel[i] = sum(positions) self.puckVel[i] /= float( len(positions)) else: # Just a click self.puckVel = [0, 0] self.render() self.send((self.messagePrefix + "Velocity", self.puckVel), "localChanges") self.mouseDown = False if event.type == pygame.MOUSEMOTION and self.mouseDown: if self.slider: self.sliderPos = event.pos[0] self.drawBG() if self.display.get_rect().collidepoint(*event.pos): # We are dragging inside the display # Keep a buffer of 50 mouse positions if len(self.mousePositions) > 50: del self.mousePositions[0] relPos = [] for i in xrange(2): relPos.append(event.pos[i] - self.lastMousePos[i]) self.mousePositions.append(relPos) # Move the puck to where the mouse is and remember # where it is self.puckPos = list(event.pos) self.lastMousePos = event.pos self.send( (self.messagePrefix + self.positionMsg, (float(self.puckPos[0]) / self.size[0], float(self.puckPos[1]) / self.size[1])), "localChanges") self.render() if self.dataReady("remoteChanges"): bundle = self.recv("remoteChanges") # The action to take is given by the last section of the # OSC address - this should maybe be done by a component and # we just listen for ("Velocity", (xVel, yVel)) tuples action = bundle[0].split("/")[-1] if action == "Velocity": if self.bouncingPuck: self.puckVel = bundle[1] self.isBouncing = 1 elif action == "Position": for i in xrange(2): self.puckPos[i] = self.size[i] * bundle[1][i] self.render() if self.dataReady("newframe"): # Time to render a new frame # Clear any backlog of render messages while self.dataReady("newframe"): self.recv("newframe") # Change the direction of the puck if it hits a wall if self.isBouncing: self.processCollisions() if self.isBouncing: # Update the position for i in xrange(2): self.puckPos[i] += self.puckVel[i] self.render()