Mercurial > fife-parpg
changeset 600:427150724fe1
- added new pychan demo: gui animations
FEATURES:
- shows (very basic) animations like resizing, moving and changing colors of a pychan.widget by using FIFE TimeEvents
author | chewie@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Fri, 27 Aug 2010 16:59:54 +0000 |
parents | a2024b994ca3 |
children | 6856cdb22351 |
files | demos/pychan_demo/gui/poc_guianimation.xml demos/pychan_demo/poc_gui_animation.py demos/pychan_demo/pychan_test.py |
diffstat | 3 files changed, 238 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/demos/pychan_demo/gui/poc_guianimation.xml Fri Aug 27 16:59:54 2010 +0000 @@ -0,0 +1,51 @@ +<Window title="Proof-of-concept: Gui animations" size="300,415" min_size="300,415" max_size="300,415"> + <VBox> + <HBox> + <Button name="example_move" text="Move it!"/> + <Button name="example_color" text="Tint it!" /> + <Button name="example_resize" text="Crush it!" /> + <Button name="example_all" text="All together now..." /> + </HBox> + + <HBox> + <Label text="Timer delay" /> + <Slider name="delay" scale_start="0" scale_end="50" size="100,10" min_size="100,10" max_size="100,10"/> + <Label name="delay_label" /> + </HBox> + </VBox> + + <Container name="move_wrapper" size="200,105" min_size="200,105" max_size="200,105"> + <Icon name="move" image="gui/icons/pychan_logo.png"/> + </Container> + + <Container name="color_wrapper" size="200,105" min_size="200,105" max_size="200,105"> + <HBox> + <VBox name="color" size="100,100" min_size="100,100" max_size="100,100" base_color="255,255,255,100"> + </VBox> + <HBox padding="0" size="100,100" min_size="100,100" max_size="100,100"> + <VBox padding="0" size="33,99" max_size="33,99" min_size="33,99"> + <Label text=" " name="color_1_1" background_color="130,130,0" size="33,33" min_size="33,33" max_size="33,33"/> + <Label text=" " name="color_1_2" background_color="130,130,0" size="33,33" min_size="33,33" max_size="33,33"/> + <Label text=" " name="color_1_3" background_color="130,130,0" size="33,33" min_size="33,33" max_size="33,33"/> + </VBox> + <VBox padding="0"> + <Label text=" " name="color_2_1" background_color="130,130,0" size="33,33" min_size="33,33" max_size="33,33"/> + <Label text=" " name="color_2_2" background_color="130,130,0" size="33,33" min_size="33,33" max_size="33,33"/> + <Label text=" " name="color_2_3" background_color="130,130,0" size="33,33" min_size="33,33" max_size="33,33"/> + </VBox> + <VBox padding="0"> + <Label text=" " name="color_3_1" background_color="130,130,0" size="33,33" min_size="33,33" max_size="33,33"/> + <Label text=" " name="color_3_2" background_color="130,130,0" size="33,33" min_size="33,33" max_size="33,33"/> + <Label text=" " name="color_3_3" background_color="130,130,0" size="33,33" min_size="33,33" max_size="33,33"/> + </VBox> + </HBox> + </HBox> + </Container> + + <Container name="resize_wrapper" size="200,105" min_size="200,105" max_size="200,105"> + <VBox name="resize" size="100,100" min_size="100,100" max_size="100,100" base_color="255,255,255,100"> + </VBox> + </Container> + + <Button name="closeButton" text="Close"/> +</Window>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/demos/pychan_demo/poc_gui_animation.py Fri Aug 27 16:59:54 2010 +0000 @@ -0,0 +1,179 @@ +# -*- coding: utf-8 -*- +# #################################################################### +# Copyright (C) 2005-2010 by the FIFE team +# http://www.fifengine.de +# This file is part of FIFE. +# +# FIFE is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the +# Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +# #################################################################### + +""" proof-of-concept pychan demo app to test gui animations """ + +from pychan_test import PyChanExample +from fife.extensions import pychan + +from fife.extensions.pychan.tools import callbackWithArguments as cbwa +from fife.extensions.fife_timer import Timer + +import time +import random + +ACTION_MOVE = 1 +ACTION_RESIZE = 2 +ACTION_COLOR = 3 +ACTIONS = [ACTION_MOVE, ACTION_RESIZE, ACTION_COLOR] + +DEFAULT_DELAY = 10 + +class PocAnimations(PyChanExample): + """ a small app (^^) to show how gui animations ~could~ be + made by using B{fife.TimeEvent}s + """ + def __init__(self): + super(PocAnimations,self).__init__('gui/poc_guianimation.xml') + + self._move_timer = None + self._resize_timer = None + self._color_timer = None + + def start(self): + """ + load XML file and setup callbacks + """ + self.widget = pychan.loadXML(self.xmlFile) + + self.widget.mapEvents({ + 'closeButton' : self.stop, + + 'example_move' : cbwa(self._start_anim, type=ACTION_MOVE), + 'example_color' : cbwa(self._start_anim, type=ACTION_COLOR), + 'example_resize': cbwa(self._start_anim, type=ACTION_RESIZE), + 'example_all' : self._anim_all, + 'delay' : self._set_delay_display, + }) + + self.move_example_widget = self.widget.findChild(name="move") + self.mew = self.move_example_widget + + self.resize_example_widget = self.widget.findChild(name="resize") + self.rew = self.resize_example_widget + + self.color_example_widget = self.widget.findChild(name="color") + self.cew = self.color_example_widget + + self.delay_slider = self.widget.findChild(name="delay") + self.delay_slider.setValue(float(DEFAULT_DELAY)) + + self.delay_display = self.widget.findChild(name="delay_label") + self.delay_display.text = unicode(str(DEFAULT_DELAY)) + + self.little_matrix = [] + for x in range(1,4): + for y in range(1,4): + name = "color_%s_%s" % (x, y) + widget = self.widget.findChild(name=name) + self.little_matrix.append(widget) + + self.widget.adaptLayout(True) + self.widget.show() + + def _set_delay_display(self): + """ set delay display according to slider value """ + value = self.delay_slider.getValue() + self.delay_display.text = unicode(str(int(value))) + + def _anim_all(self): + """ fire all animations """ + for action in ACTIONS: + self._start_anim(type=action) + + def _start_anim(self, type=None): + """ start the animation of the given type """ + self._reset_anim(type) + kwargs = { + 'delay' : int(self.delay_slider.getValue()), + 'callback' : None, + 'repeat' : 0, + } + if type == ACTION_MOVE: + kwargs['callback'] = self._move + self._move_timer = Timer(**kwargs) + self._move_timer.start() + elif type == ACTION_RESIZE: + kwargs['callback'] = self._resize + self._resize_timer = Timer(**kwargs) + self._resize_timer.start() + elif type == ACTION_COLOR: + kwargs['callback'] = self._color + self._color_timer = Timer(**kwargs) + self._color_timer.start() + + def _reset_anim(self, type=None): + """ undo changes made by the animation (but leave alone disco matrix ^^) """ + if type == ACTION_MOVE: + if self._move_timer: + self._move_timer.stop() + self.mew.position = 0, 0 + elif type == ACTION_RESIZE: + if self._resize_timer: + self._resize_timer.stop() + SIZE = 100, 100 + self.rew.size = SIZE + self.rew.min_size = SIZE + self.rew.max_size = SIZE + self.widget.findChild(name="resize_wrapper").adaptLayout() + elif type == ACTION_COLOR: + if self._color_timer: + self._color_timer.stop() + COLOR = 255, 255, 255, 100 + self.cew.base_color = COLOR + + def _move(self): + """ move the mew widget """ + position = list(self.mew.position) + if position[0] < 100: + position[0] += 1 + self.mew.position = position + else: + self._reset_anim(ACTION_MOVE) + + def _resize(self): + """ resize the rew widget """ + size = list(self.rew.size) + if size[0] > 0: + size[0] -= 1 + size[1] -= 1 + self.rew.size = size + self.rew.min_size = size + self.rew.max_size = size + else: + self._reset_anim(ACTION_RESIZE) + + def _color(self): + """ tint the cew widgets """ + color = self.cew.base_color + red = color.r + if red > 1: + red -= 1 + self.cew.base_color = (red, 255, 255, 100) + + # disco! + for widget in self.little_matrix: + color = tuple(random.randint(1,255) for i in range(1,5)) + widget.background_color = color + else: + self._reset_anim(ACTION_COLOR) +
--- a/demos/pychan_demo/pychan_test.py Fri Aug 27 12:59:50 2010 +0000 +++ b/demos/pychan_demo/pychan_test.py Fri Aug 27 16:59:54 2010 +0000 @@ -111,13 +111,17 @@ # it later. credits.capture(lambda : credits._setText(u"Credits"), event_name="mouseExited") - # Our list of examples - # We keep a dictionary of these and fill - # the ListBox on the left with its names. + + # import example modules from dynamic import DynamicExample from styling import StylingExample from sliders import SliderExample from colortester import ColorExample + from poc_gui_animation import PocAnimations + + # Our list of examples + # We keep a dictionary of these and fill + # the ListBox on the left with its names. self.examples = { 'Absolute Positioning' : PyChanExample('gui/absolute.xml'), 'All Widgets' : PyChanExample('gui/all_widgets.xml'), @@ -126,6 +130,7 @@ 'Sliders' : SliderExample(), 'ScrollArea' : PyChanExample('gui/scrollarea.xml'), 'Colortester': ColorExample(), + 'GuiAnimations' : PocAnimations(), } self.demoList = self.gui.findChild(name='demoList') self.demoList.items = sorted(self.examples.keys())