Mercurial > fife-parpg
comparison engine/python/fife/extensions/pychan/autoposition.py @ 378:64738befdf3b
bringing in the changes from the build_system_rework branch in preparation for the 0.3.0 release. This commit will require the Jan2010 devkit. Clients will also need to be modified to the new way to import fife.
author | vtchill@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Mon, 11 Jan 2010 23:34:52 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
377:fe6fb0e0ed23 | 378:64738befdf3b |
---|---|
1 # -*- coding: utf-8 -*- | |
2 | |
3 # #################################################################### | |
4 # Copyright (C) 2005-2009 by the FIFE team | |
5 # http://www.fifengine.de | |
6 # This file is part of FIFE. | |
7 # | |
8 # FIFE is free software; you can redistribute it and/or | |
9 # modify it under the terms of the GNU Lesser General Public | |
10 # License as published by the Free Software Foundation; either | |
11 # version 2.1 of the License, or (at your option) any later version. | |
12 # | |
13 # This library is distributed in the hope that it will be useful, | |
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 # Lesser General Public License for more details. | |
17 # | |
18 # You should have received a copy of the GNU Lesser General Public | |
19 # License along with this library; if not, write to the | |
20 # Free Software Foundation, Inc., | |
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
22 # #################################################################### | |
23 | |
24 """ | |
25 Automatic widget positioning | |
26 ============================ | |
27 | |
28 You can use the C{position_techinque} attribute | |
29 on top level widgets which can also be set from xml. | |
30 | |
31 For direct use call L{placeWidget}. | |
32 """ | |
33 | |
34 from internal import screen_width, screen_height | |
35 from exceptions import PyChanException | |
36 | |
37 EXPLICIT = "explicit" | |
38 AUTOMATIC = "automatic" | |
39 | |
40 TOP = "top" | |
41 LEFT = "left" | |
42 RIGHT = "right" | |
43 CENTER = "center" | |
44 BOTTOM = "bottom" | |
45 | |
46 | |
47 def _splicePosition(p): | |
48 if "+" in p: | |
49 technique,delta = p.split("+") | |
50 elif "-" in p: | |
51 technique,delta = p.split("-") | |
52 delta = '-' + delta | |
53 else: | |
54 technique,delta = p,0 | |
55 delta = int(delta) | |
56 return technique,delta | |
57 | |
58 def _parsePosition(position): | |
59 try: | |
60 if position == AUTOMATIC: | |
61 position = "center+0:center+0" | |
62 | |
63 x_pos,y_pos = position.split(":") | |
64 x_pos, x_delta = _splicePosition(x_pos) | |
65 y_pos, y_delta = _splicePosition(y_pos) | |
66 | |
67 if x_pos not in [EXPLICIT,LEFT,CENTER,RIGHT]: | |
68 raise | |
69 if y_pos not in [EXPLICIT,TOP,CENTER,BOTTOM]: | |
70 raise | |
71 except: | |
72 raise PyChanException("Malformed position definition: " + repr(position)) | |
73 return x_pos,x_delta,y_pos,y_delta | |
74 | |
75 def placeWidget(widget,position): | |
76 """ | |
77 Place a widget according to a string defining relative coordinates to screen borders. | |
78 | |
79 The position definition has to be of the form: C{"<x_pos><x_delta>:<y_pos><y_delta>"} | |
80 | |
81 C{<x_pos>} may be one of: | |
82 - left | |
83 - right | |
84 - center | |
85 - explicit | |
86 | |
87 C{<y_pos>} may be one of: | |
88 - top | |
89 - bottom | |
90 - center | |
91 - explicit | |
92 | |
93 C{explicit} means that the widgets x or y position will not be touched. The rest should be | |
94 self explanatory. | |
95 | |
96 C{<x_delta>} and C{<y_delta>} must be of the form: +pixel_number or -pixel_number. Or comletely | |
97 omitted. Note that the sign has to be there for for positive deltas, too. | |
98 | |
99 For brevity two shortcuts exist: | |
100 - "explict" -> "explict:explict" | |
101 - "automatic" -> "center:center" | |
102 | |
103 A few examples:: | |
104 "right-20:top" | |
105 "center:top+10" | |
106 "center:center" | |
107 | |
108 @param widget: The PyChan widget. | |
109 @param position: A position definition. | |
110 | |
111 If the position cannot be parsed a L{PyChanException} is thrown. | |
112 | |
113 """ | |
114 if position == EXPLICIT: | |
115 return | |
116 x_pos,x_delta,y_pos,y_delta = _parsePosition(position) | |
117 | |
118 x,y = widget.position | |
119 w,h = widget.size | |
120 | |
121 if x_pos == CENTER: | |
122 x = (screen_width()-w)/2 + x_delta | |
123 | |
124 if y_pos == CENTER: | |
125 y = (screen_height()-h)/2 + y_delta | |
126 | |
127 if x_pos == LEFT: | |
128 x = x_delta | |
129 | |
130 if y_pos == TOP: | |
131 y = y_delta | |
132 | |
133 if x_pos == RIGHT: | |
134 x = screen_width() - w + x_delta | |
135 | |
136 if y_pos == BOTTOM: | |
137 y = screen_height() - h + y_delta | |
138 | |
139 widget.position = x,y | |
140 | |
141 | |
142 |