Mercurial > fife-parpg
comparison demos/pychan_demo/poc_gui_animation.py @ 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 | |
children | 8c9cdcc9bc4f |
comparison
equal
deleted
inserted
replaced
599:a2024b994ca3 | 600:427150724fe1 |
---|---|
1 # -*- coding: utf-8 -*- | |
2 # #################################################################### | |
3 # Copyright (C) 2005-2010 by the FIFE team | |
4 # http://www.fifengine.de | |
5 # This file is part of FIFE. | |
6 # | |
7 # FIFE is free software; you can redistribute it and/or | |
8 # modify it under the terms of the GNU Lesser General Public | |
9 # License as published by the Free Software Foundation; either | |
10 # version 2.1 of the License, or (at your option) any later version. | |
11 # | |
12 # This library is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 # Lesser General Public License for more details. | |
16 # | |
17 # You should have received a copy of the GNU Lesser General Public | |
18 # License along with this library; if not, write to the | |
19 # Free Software Foundation, Inc., | |
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
21 # #################################################################### | |
22 | |
23 """ proof-of-concept pychan demo app to test gui animations """ | |
24 | |
25 from pychan_test import PyChanExample | |
26 from fife.extensions import pychan | |
27 | |
28 from fife.extensions.pychan.tools import callbackWithArguments as cbwa | |
29 from fife.extensions.fife_timer import Timer | |
30 | |
31 import time | |
32 import random | |
33 | |
34 ACTION_MOVE = 1 | |
35 ACTION_RESIZE = 2 | |
36 ACTION_COLOR = 3 | |
37 ACTIONS = [ACTION_MOVE, ACTION_RESIZE, ACTION_COLOR] | |
38 | |
39 DEFAULT_DELAY = 10 | |
40 | |
41 class PocAnimations(PyChanExample): | |
42 """ a small app (^^) to show how gui animations ~could~ be | |
43 made by using B{fife.TimeEvent}s | |
44 """ | |
45 def __init__(self): | |
46 super(PocAnimations,self).__init__('gui/poc_guianimation.xml') | |
47 | |
48 self._move_timer = None | |
49 self._resize_timer = None | |
50 self._color_timer = None | |
51 | |
52 def start(self): | |
53 """ | |
54 load XML file and setup callbacks | |
55 """ | |
56 self.widget = pychan.loadXML(self.xmlFile) | |
57 | |
58 self.widget.mapEvents({ | |
59 'closeButton' : self.stop, | |
60 | |
61 'example_move' : cbwa(self._start_anim, type=ACTION_MOVE), | |
62 'example_color' : cbwa(self._start_anim, type=ACTION_COLOR), | |
63 'example_resize': cbwa(self._start_anim, type=ACTION_RESIZE), | |
64 'example_all' : self._anim_all, | |
65 'delay' : self._set_delay_display, | |
66 }) | |
67 | |
68 self.move_example_widget = self.widget.findChild(name="move") | |
69 self.mew = self.move_example_widget | |
70 | |
71 self.resize_example_widget = self.widget.findChild(name="resize") | |
72 self.rew = self.resize_example_widget | |
73 | |
74 self.color_example_widget = self.widget.findChild(name="color") | |
75 self.cew = self.color_example_widget | |
76 | |
77 self.delay_slider = self.widget.findChild(name="delay") | |
78 self.delay_slider.setValue(float(DEFAULT_DELAY)) | |
79 | |
80 self.delay_display = self.widget.findChild(name="delay_label") | |
81 self.delay_display.text = unicode(str(DEFAULT_DELAY)) | |
82 | |
83 self.little_matrix = [] | |
84 for x in range(1,4): | |
85 for y in range(1,4): | |
86 name = "color_%s_%s" % (x, y) | |
87 widget = self.widget.findChild(name=name) | |
88 self.little_matrix.append(widget) | |
89 | |
90 self.widget.adaptLayout(True) | |
91 self.widget.show() | |
92 | |
93 def _set_delay_display(self): | |
94 """ set delay display according to slider value """ | |
95 value = self.delay_slider.getValue() | |
96 self.delay_display.text = unicode(str(int(value))) | |
97 | |
98 def _anim_all(self): | |
99 """ fire all animations """ | |
100 for action in ACTIONS: | |
101 self._start_anim(type=action) | |
102 | |
103 def _start_anim(self, type=None): | |
104 """ start the animation of the given type """ | |
105 self._reset_anim(type) | |
106 kwargs = { | |
107 'delay' : int(self.delay_slider.getValue()), | |
108 'callback' : None, | |
109 'repeat' : 0, | |
110 } | |
111 if type == ACTION_MOVE: | |
112 kwargs['callback'] = self._move | |
113 self._move_timer = Timer(**kwargs) | |
114 self._move_timer.start() | |
115 elif type == ACTION_RESIZE: | |
116 kwargs['callback'] = self._resize | |
117 self._resize_timer = Timer(**kwargs) | |
118 self._resize_timer.start() | |
119 elif type == ACTION_COLOR: | |
120 kwargs['callback'] = self._color | |
121 self._color_timer = Timer(**kwargs) | |
122 self._color_timer.start() | |
123 | |
124 def _reset_anim(self, type=None): | |
125 """ undo changes made by the animation (but leave alone disco matrix ^^) """ | |
126 if type == ACTION_MOVE: | |
127 if self._move_timer: | |
128 self._move_timer.stop() | |
129 self.mew.position = 0, 0 | |
130 elif type == ACTION_RESIZE: | |
131 if self._resize_timer: | |
132 self._resize_timer.stop() | |
133 SIZE = 100, 100 | |
134 self.rew.size = SIZE | |
135 self.rew.min_size = SIZE | |
136 self.rew.max_size = SIZE | |
137 self.widget.findChild(name="resize_wrapper").adaptLayout() | |
138 elif type == ACTION_COLOR: | |
139 if self._color_timer: | |
140 self._color_timer.stop() | |
141 COLOR = 255, 255, 255, 100 | |
142 self.cew.base_color = COLOR | |
143 | |
144 def _move(self): | |
145 """ move the mew widget """ | |
146 position = list(self.mew.position) | |
147 if position[0] < 100: | |
148 position[0] += 1 | |
149 self.mew.position = position | |
150 else: | |
151 self._reset_anim(ACTION_MOVE) | |
152 | |
153 def _resize(self): | |
154 """ resize the rew widget """ | |
155 size = list(self.rew.size) | |
156 if size[0] > 0: | |
157 size[0] -= 1 | |
158 size[1] -= 1 | |
159 self.rew.size = size | |
160 self.rew.min_size = size | |
161 self.rew.max_size = size | |
162 else: | |
163 self._reset_anim(ACTION_RESIZE) | |
164 | |
165 def _color(self): | |
166 """ tint the cew widgets """ | |
167 color = self.cew.base_color | |
168 red = color.r | |
169 if red > 1: | |
170 red -= 1 | |
171 self.cew.base_color = (red, 255, 255, 100) | |
172 | |
173 # disco! | |
174 for widget in self.little_matrix: | |
175 color = tuple(random.randint(1,255) for i in range(1,5)) | |
176 widget.background_color = color | |
177 else: | |
178 self._reset_anim(ACTION_COLOR) | |
179 |