class Channel: """ **pyjsdl.mixer.Channel** * Channel.play * Channel.stop * Channel.pause * Channel.unpause * Channel.set_volume * Channel.get_volume * Channel.get_busy * Channel.get_sound """ _mixer = None def __init__(self, id): self._id = id self._sound = None self._active = False self._pause = False self._loops = 0 self._volume = 1.0 self._lvolume = 1.0 self._rvolume = 1.0 self._mixer._register_channel(self) self._time = Time() self._nonimplemented_methods() def _set_sound(self, sound): if self._sound: self._sound._channel = None self._sound = sound def _play(self): self._volume = 1.0 self._lvolume = 1.0 self._rvolume = 1.0 self._active = True self._sound._sound_object.play() self._time.timeout(self._sound._sound_object.getDuration() * 1000, self) def _play_repeat(self, loops): if loops > 0: self._loops = loops else: self._loops = -1 self._play() def play(self, sound, loops=0, maxtime=0, fade_ms=0): """ Play sound on channel. Argument sound to play and loops is number of repeats or -1 for continuous. """ if self._sound: self.stop() self._set_sound(sound) if not loops: self._play() else: self._play_repeat(loops) return None def run(self): if not self._loops: self._active = False else: if self._loops > 0: self._loops -= 1 self._play() def stop(self): """ Stop sound on channel. """ if self._sound: self._sound._sound_object.pause() self._sound._sound_object.setCurrentTime(0) self._pause = False self._loops = 0 self._active = False return None def pause(self): """ Pause sound on channel. """ if self._sound: if not self._pause: self._sound._sound_object.pause() self._pause = True return None def unpause(self): """ Unpause sound on channel. """ if self._sound: if self._pause: self._sound._sound_object.play() self._pause = False return None def set_volume(self, volume): """ Set channel volume of sound playing. """ if volume < 0.0: volume = 0.0 elif volume > 1.0: volume = 1.0 self._volume = volume if self._sound: self._sound._sound_object.setVolume( self._volume * self._sound._sound_object._volume) else: self._volume = 1.0 return None def get_volume(self): """ Get channel volume for current sound. """ return self._volume def get_busy(self): """ Check if channel is processing sound. """ return self._active def get_sound(self): """ Get sound open by channel. """ return self._sound def _nonimplemented_methods(self): """ Non-implemented methods. """ self.fadeout = lambda *arg: None self.queue = lambda *arg: None self.get_queue = lambda *arg: None self.set_endevent = lambda *arg: None self.get_endevent = lambda *arg: 0
class Canvas(Surface, MouseWheelHandler): def __init__(self, size, buffered): Surface.__init__(self, size) MouseWheelHandler.__init__(self, True) if isinstance(buffered, bool): self._bufferedimage = buffered else: self._bufferedimage = True try: if self.impl.canvasContext: self._isCanvas = True except: self._isCanvas = False self._bufferedimage = False if self._bufferedimage: self.surface = Surface(size) else: self.surface = self self.resize(size[0], size[1]) self.images = {} self.image_list = [] self.function = None self.time_wait = 0 self.time = Time() self.event = pyjsdl.event self.addMouseListener(self) self.addMouseWheelListener(self) self.addKeyboardListener(self) self.sinkEvents(Event.ONMOUSEDOWN | Event.ONMOUSEUP| Event.ONMOUSEMOVE | Event.ONMOUSEOUT | Event.ONMOUSEWHEEL | Event.ONKEYDOWN | Event.ONKEYPRESS | Event.ONKEYUP) self.modKey = pyjsdl.event.modKey self.specialKey = pyjsdl.event.specialKey self._rect_list = [] self._rect_list.append(Rect(0,0,0,0)) self._rect_len = 1 self._rect_num = 0 self._rect_temp = Rect(0,0,0,0) _animationFrame = self._initAnimationFrame() if _animationFrame: self.time_hold_min = 0 else: self.time_hold_min = 1 self.time_hold = self.time_hold_min def _initAnimationFrame(self): JS(""" $wnd['requestAnimationFrame'] = $wnd['requestAnimationFrame'] || $wnd['mozRequestAnimationFrame'] || $wnd['webkitRequestAnimationFrame'] || $wnd['oRequestAnimationFrame']; """) if JS("""$wnd['requestAnimationFrame'] != undefined"""): _animationFrame = True else: JS("""$wnd['requestAnimationFrame'] = function(cb){cb()};""") _animationFrame = False return _animationFrame def onMouseMove(self, sender, x, y): event = DOM.eventGetCurrentEvent() event.pos = (x, y) self.event.mouseMove['x'], self.event.mouseMove['y'] = x, y self.event._updateQueue(event) def onMouseDown(self, sender, x, y): event = DOM.eventGetCurrentEvent() event.pos = (x, y) self.event.mousePress[event.button] = True self.event._updateQueue(event) def onMouseUp(self, sender, x, y): event = DOM.eventGetCurrentEvent() event.pos = (x, y) self.event.mousePress[event.button] = False self.event._updateQueue(event) def onMouseLeave(self, sender): self.event.mousePress[0], self.event.mousePress[1], self.event.mousePress[2] = False, False, False self.event.mouseMove['x'], self.event.mouseMove['y'] = -1, -1 self.event.mouseMoveRel['x'], self.event.mouseMoveRel['y'] = None, None for keycode in self.modKey: if self.event.keyPress[keycode]: self.event.keyPress[keycode] = False def onMouseWheel(self, sender, velocity): event = DOM.eventGetCurrentEvent() if event.type == 'mousewheel': #TODO: update for changes in mousewheel implementation if hasattr(event, 'wheelDeltaX'): self.onMouseWheel = self._onMouseWheel self._onMouseWheel(sender, velocity) else: self.onMouseWheel = self._onMouseWheelY DOM.eventGetMouseWheelVelocityY = eventGetMouseWheelVelocityY self._onMouseWheelY(sender, eventGetMouseWheelVelocityY(event)) else: #DOMMouseScroll self.onMouseWheel = self._onMouseScroll self._onMouseScroll(sender, velocity) def _onMouseWheel(self, sender, velocity): event = DOM.eventGetCurrentEvent() if not event.wheelDeltaX: if velocity < 0: button = 4 events = velocity / -3 else: button = 5 events = velocity / 3 else: if velocity < 0: button = 6 events = velocity / -3 else: button = 7 events = velocity / 3 event.btn = button event.pos = (self.event.mouseMove['x'], self.event.mouseMove['y']) for evt in range(events): self.event._updateQueue(event) def _onMouseWheelY(self, sender, velocity): event = DOM.eventGetCurrentEvent() if velocity < 0: button = 4 events = velocity / -3 else: button = 5 events = velocity / 3 event.btn = button event.pos = (self.event.mouseMove['x'], self.event.mouseMove['y']) for evt in range(events): self.event._updateQueue(event) def _onMouseScroll(self, sender, velocity): event = DOM.eventGetCurrentEvent() if velocity > 1 or velocity < -1: if velocity < 0: button = 4 else: button = 5 else: if velocity < 0: button = 6 else: button = 7 event.btn = button event.pos = (self.event.mouseMove['x'], self.event.mouseMove['y']) self.event._updateQueue(event) def onKeyDown(self, sender, keycode, modifiers): if keycode in self.modKey: event = DOM.eventGetCurrentEvent() self.event.keyPress[keycode] = True self.event._updateQueue(event) DOM.eventPreventDefault(event) elif keycode in self.specialKey: event = DOM.eventGetCurrentEvent() self.event._updateQueue(event) DOM.eventPreventDefault(event) def onKeyPress(self, sender, keycode, modifiers): event = DOM.eventGetCurrentEvent() if not (event.keyCode and event.keyCode in self.specialKey): self.event._updateQueue(event) DOM.eventPreventDefault(event) def onKeyUp(self, sender, keycode, modifiers): event = DOM.eventGetCurrentEvent() if keycode in self.modKey: self.event.keyPress[keycode] = False self.event._updateQueue(event) def resize(self, width, height): Surface.resize(self, width, height) self.surface.resize(width, height) try: self.surface._display.textbox.resize() except (TypeError, AttributeError): #pyjs-O:TypeError/-S:AttributeError pass try: self.surface._display.textarea.resize() except (TypeError, AttributeError): #pyjs-O:TypeError/-S:AttributeError pass def set_function(self, function): self.function = function def load_images(self, images): if images: image_list = [] for image in images: if isinstance(image, str): image_list.append(image) self.image_list.append(image) else: name = image[0] if isinstance(image[1], str): data = image[1] else: data = base64.b64encode(image[1].getvalue()) if not data.startswith('data:'): ext = name.strip().split('.')[-1] data = "data:%s;base64,%s" %(ext, data) #data:[<mediatype>][;base64],<data> image_list.append(data) self.image_list.append(name) loadImages(image_list, self) else: self.start() def set_timeout(self, change): self.time_hold += change if self.time_hold < self.time_hold_min: self.time_hold = self.time_hold_min def start(self): self.run = self._run self.time.timeout(self.time_hold, self) def stop(self): self.time_wait = 0 self.run = lambda: None def onImagesLoaded(self, images): for i, image in enumerate(self.image_list): self.images[image] = images[i] self.start() def set_timeWait(self, time): if time: self.time_wait = time self.run = lambda: None else: if self.time_wait: self.time_wait = 0 self.run = self._run self.run() def _get_rect(self): if self._rect_num < self._rect_len: return self._rect_list[self._rect_num] else: self._rect_list.append(Rect(0,0,0,0)) self._rect_len += 1 return self._rect_list[self._rect_num] def _run(self): self.function() JS("""$wnd['requestAnimationFrame'](@{{paint}});""") def rerun(self): if not self.time_hold: self.run() else: self.time.timeout(self.time_hold, self)
class Channel: """ **pyjsdl.mixer.Channel** * Channel.play * Channel.stop * Channel.pause * Channel.unpause * Channel.set_volume * Channel.get_volume * Channel.get_busy * Channel.get_sound """ _mixer = None def __init__(self, id): self._id = id self._sound = None self._active = False self._pause = False self._loops = 0 self._volume = 1.0 self._lvolume = 1.0 self._rvolume = 1.0 self._mixer._register_channel(self) self._time = Time() self._nonimplemented_methods() def _set_sound(self, sound): if self._sound: self._sound._channel = None self._sound = sound def _play(self): self._volume = 1.0 self._lvolume = 1.0 self._rvolume = 1.0 self._active = True self._sound._sound_object.play() self._time.timeout(self._sound._sound_object.getDuration()*1000, self) def _play_repeat(self, loops): if loops > 0: self._loops = loops else: self._loops = -1 self._play() def play(self, sound, loops=0, maxtime=0, fade_ms=0): """ Play sound on channel. Argument sound to play and loops is number of repeats or -1 for continuous. """ if self._sound: self.stop() self._set_sound(sound) if not loops: self._play() else: self._play_repeat(loops) return None def run(self): if not self._loops: self._active = False else: if self._loops > 0: self._loops -= 1 self._play() def stop(self): """ Stop sound on channel. """ if self._sound: self._sound._sound_object.pause() self._sound._sound_object.setCurrentTime(0) self._pause = False self._loops = 0 self._active = False return None def pause(self): """ Pause sound on channel. """ if self._sound: if not self._pause: self._sound._sound_object.pause() self._pause = True return None def unpause(self): """ Unpause sound on channel. """ if self._sound: if self._pause: self._sound._sound_object.play() self._pause = False return None def set_volume(self, volume): """ Set channel volume of sound playing. """ if volume < 0.0: volume = 0.0 elif volume > 1.0: volume = 1.0 self._volume = volume if self._sound: self._sound._sound_object.setVolume(self._volume * self._sound._sound_object._volume) else: self._volume = 1.0 return None def get_volume(self): """ Get channel volume for current sound. """ return self._volume def get_busy(self): """ Check if channel is processing sound. """ return self._active def get_sound(self): """ Get sound open by channel. """ return self._sound def _nonimplemented_methods(self): """ Non-implemented methods. """ self.fadeout = lambda *arg: None self.queue = lambda *arg: None self.get_queue = lambda *arg: None self.set_endevent = lambda *arg: None self.get_endevent = lambda *arg: 0