Mercurial > fife-parpg
comparison clients/editor/scripts/gui/resizablebase.py @ 255:51cc05d862f2
Merged editor_rewrite branch to trunk.
This contains changes that may break compatibility against existing clients.
For a list of changes that may affect your client, see: http://wiki.fifengine.de/Changes_to_pychan_and_FIFE_in_editor_rewrite_branch
author | cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Mon, 08 Jun 2009 16:00:02 +0000 |
parents | |
children | f2195628947b |
comparison
equal
deleted
inserted
replaced
254:10b5f7f36dd4 | 255:51cc05d862f2 |
---|---|
1 import fife | |
2 import scripts | |
3 import pychan | |
4 | |
5 class ResizableBase(object): | |
6 def __init__(self, resizable=True, *args, **kwargs): | |
7 self._engine = scripts.editor.getEditor().getEngine() | |
8 | |
9 self.resizable = resizable | |
10 self.resizable_top = True | |
11 self.resizable_left = True | |
12 self.resizable_right = True | |
13 self.resizable_bottom = True | |
14 | |
15 self._resize = False | |
16 | |
17 self.capture(self.mouseEntered, "mouseEntered", "ResizableBase") | |
18 self.capture(self.mouseExited, "mouseExited", "ResizableBase") | |
19 self.capture(self.mouseMoved, "mouseMoved", "ResizableBase") | |
20 self.capture(self.mouseDragged, "mouseDragged", "ResizableBase") | |
21 self.capture(self.mousePressed, "mousePressed", "ResizableBase") | |
22 self.capture(self.mouseReleased, "mouseReleased", "ResizableBase") | |
23 | |
24 self.cursor_id = 0 | |
25 self.cursor_type = 1 | |
26 | |
27 | |
28 def mouseEntered(self, event): | |
29 # Save cursor id | |
30 if self.resizable and self._resize is False: | |
31 cursor = self._engine.getCursor() | |
32 self.cursor_id = cursor.getId() | |
33 self.cursor_type = cursor.getType() | |
34 | |
35 def mouseMoved(self, event): | |
36 if self.resizable is False: | |
37 return | |
38 | |
39 # Hack to allow consuming mousemove events | |
40 key = (event.getX(), event.getY()) | |
41 if _mousemoveevent.has_key( key ) is False: | |
42 _mousemoveevent.clear() | |
43 _mousemoveevent[key] = event | |
44 elif _mousemoveevent[key].isConsumed(): | |
45 return | |
46 | |
47 | |
48 titleheight = 0 | |
49 if hasattr(self.real_widget, "getTitleBarHeight"): | |
50 titleheight = self.real_widget.getTitleBarHeight() | |
51 | |
52 cursor = self._engine.getCursor() | |
53 cursor.set(fife.CURSOR_NATIVE, fife.NC_RESIZENW) | |
54 | |
55 left = event.getX() < 5 and self.resizable_left | |
56 right = event.getX() > self.width-5 and self.resizable_right | |
57 top = event.getY() < 5 and self.resizable_top | |
58 bottom = event.getY() - titleheight > self.height-5 and self.resizable_bottom | |
59 | |
60 if left and top: | |
61 cursor.set(fife.CURSOR_NATIVE, fife.NC_RESIZENW) | |
62 elif right and top: | |
63 cursor.set(fife.CURSOR_NATIVE, fife.NC_RESIZENE) | |
64 elif left and bottom: | |
65 cursor.set(fife.CURSOR_NATIVE, fife.NC_RESIZESW) | |
66 elif right and bottom: | |
67 cursor.set(fife.CURSOR_NATIVE, fife.NC_RESIZESE) | |
68 elif left: | |
69 cursor.set(fife.CURSOR_NATIVE, fife.NC_RESIZEW) | |
70 elif right: | |
71 cursor.set(fife.CURSOR_NATIVE, fife.NC_RESIZEE) | |
72 elif top: | |
73 cursor.set(fife.CURSOR_NATIVE, fife.NC_RESIZEN) | |
74 elif bottom: | |
75 cursor.set(fife.CURSOR_NATIVE, fife.NC_RESIZES) | |
76 else: | |
77 cursor.set(self.cursor_type, self.cursor_id) | |
78 return | |
79 | |
80 event.consume() | |
81 _mousemoveevent[key].consume() | |
82 | |
83 def mouseExited(self, event): | |
84 # Reset cursor to whatever it was before it entered this window | |
85 if self.resizable and self._resize is False: | |
86 cursor = self._engine.getCursor() | |
87 cursor.set(self.cursor_type, self.cursor_id) | |
88 | |
89 def mouseDragged(self, event): | |
90 if self.resizable and self._resize: | |
91 diffX = event.getX() | |
92 diffY = event.getY() | |
93 | |
94 # Resize horizontally | |
95 if self._rLeft: | |
96 self.x += diffX | |
97 self.width -= diffX | |
98 elif self._rRight: | |
99 self.width = diffX | |
100 | |
101 # Resize vertically | |
102 if self._rTop: | |
103 self.y += diffY | |
104 self.height -= diffY | |
105 elif self._rBottom: | |
106 titleheight = 0 | |
107 if hasattr(self.real_widget, "getTitleBarHeight"): | |
108 titleheight = self.real_widget.getTitleBarHeight() | |
109 self.height = diffY-titleheight | |
110 | |
111 def mousePressed(self, event): | |
112 if self.resizable is False: | |
113 return | |
114 | |
115 titleheight = 0 | |
116 if hasattr(self.real_widget, "getTitleBarHeight"): | |
117 titleheight = self.real_widget.getTitleBarHeight() | |
118 | |
119 self._rLeft = event.getX() < 5 and self.resizable_left | |
120 self._rRight = event.getX() > self.width-5 and self.resizable_right | |
121 self._rTop = event.getY() < 5 and self.resizable_top | |
122 self._rBottom = event.getY() - titleheight > self.height-5 and self.resizable_bottom | |
123 | |
124 if self._rLeft or self._rRight or self._rTop or self._rBottom: | |
125 self._resize = True | |
126 self.min_size = (30, 30) | |
127 event.consume() | |
128 | |
129 def mouseReleased(self, event): | |
130 if self._resize: | |
131 self.min_size = (self.width, self.height) | |
132 self.adaptLayout() | |
133 event.consume() | |
134 | |
135 titleheight = 0 | |
136 if hasattr(self.real_widget, "getTitleBarHeight"): | |
137 titleheight = self.real_widget.getTitleBarHeight() | |
138 | |
139 self._resize = False | |
140 if event.getX() <= 0 or event.getX() >= self.width \ | |
141 or event.getY() <= 0 or event.getY() >= self.height+titleheight: | |
142 self.mouseExited(event) | |
143 | |
144 # Ugly hack to allow consumption of mousemove events | |
145 _mousemoveevent = {} | |
146 |