comparison tools/editor/scripts/gui/resizablebase.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 from fife import fife
25 import scripts
26 from fife.extensions import pychan
27
28 class ResizableBase(object):
29 def __init__(self, resizable=True, *args, **kwargs):
30 self._engine = scripts.editor.getEditor().getEngine()
31
32 self.resizable = resizable
33 self.resizable_top = True
34 self.resizable_left = True
35 self.resizable_right = True
36 self.resizable_bottom = True
37
38 self._resize = False
39
40 self.capture(self.mouseEntered, "mouseEntered", "ResizableBase")
41 self.capture(self.mouseExited, "mouseExited", "ResizableBase")
42 self.capture(self.mouseMoved, "mouseMoved", "ResizableBase")
43 self.capture(self.mouseDragged, "mouseDragged", "ResizableBase")
44 self.capture(self.mousePressed, "mousePressed", "ResizableBase")
45 self.capture(self.mouseReleased, "mouseReleased", "ResizableBase")
46
47 self.cursor_id = 0
48 self.cursor_type = 1
49
50
51 def mouseEntered(self, event):
52 # Save cursor id
53 if self.resizable and self._resize is False:
54 cursor = self._engine.getCursor()
55 self.cursor_id = cursor.getId()
56 self.cursor_type = cursor.getType()
57
58 def mouseMoved(self, event):
59 if self.resizable is False:
60 return
61
62 # Hack to allow consuming mousemove events
63 key = (event.getX(), event.getY())
64 if _mousemoveevent.has_key( key ) is False:
65 _mousemoveevent.clear()
66 _mousemoveevent[key] = event
67 elif _mousemoveevent[key].isConsumed():
68 return
69
70
71 titleheight = 0
72 if hasattr(self.real_widget, "getTitleBarHeight"):
73 titleheight = self.real_widget.getTitleBarHeight()
74
75 cursor = self._engine.getCursor()
76
77 left = event.getX() < 5 and self.resizable_left
78 right = event.getX() > self.width-5 and self.resizable_right
79 top = event.getY() < 5 and self.resizable_top
80 bottom = event.getY() - titleheight > self.height-5 and self.resizable_bottom
81
82 if left and top:
83 cursor.set(fife.CURSOR_NATIVE, fife.NC_RESIZENW)
84 elif right and top:
85 cursor.set(fife.CURSOR_NATIVE, fife.NC_RESIZENE)
86 elif left and bottom:
87 cursor.set(fife.CURSOR_NATIVE, fife.NC_RESIZESW)
88 elif right and bottom:
89 cursor.set(fife.CURSOR_NATIVE, fife.NC_RESIZESE)
90 elif left:
91 cursor.set(fife.CURSOR_NATIVE, fife.NC_RESIZEW)
92 elif right:
93 cursor.set(fife.CURSOR_NATIVE, fife.NC_RESIZEE)
94 elif top:
95 cursor.set(fife.CURSOR_NATIVE, fife.NC_RESIZEN)
96 elif bottom:
97 cursor.set(fife.CURSOR_NATIVE, fife.NC_RESIZES)
98 else:
99 cursor.set(self.cursor_type, self.cursor_id)
100 return
101
102 event.consume()
103 _mousemoveevent[key].consume()
104
105 def mouseExited(self, event):
106 # Reset cursor to whatever it was before it entered this window
107 if self.resizable and self._resize is False:
108 cursor = self._engine.getCursor()
109 cursor.set(self.cursor_type, self.cursor_id)
110
111 def mouseDragged(self, event):
112 if self.resizable and self._resize:
113 diffX = event.getX()
114 diffY = event.getY()
115
116 # Resize horizontally
117 if self._rLeft:
118 self.x += diffX
119 self.width -= diffX
120 elif self._rRight:
121 self.width = diffX
122
123 # Resize vertically
124 if self._rTop:
125 self.y += diffY
126 self.height -= diffY
127 elif self._rBottom:
128 titleheight = 0
129 if hasattr(self.real_widget, "getTitleBarHeight"):
130 titleheight = self.real_widget.getTitleBarHeight()
131 self.height = diffY-titleheight
132
133 def mousePressed(self, event):
134 if self.resizable is False:
135 return
136
137 titleheight = 0
138 if hasattr(self.real_widget, "getTitleBarHeight"):
139 titleheight = self.real_widget.getTitleBarHeight()
140
141 self._rLeft = event.getX() < 5 and self.resizable_left
142 self._rRight = event.getX() > self.width-5 and self.resizable_right
143 self._rTop = event.getY() < 5 and self.resizable_top
144 self._rBottom = event.getY() - titleheight > self.height-5 and self.resizable_bottom
145
146 if self._rLeft or self._rRight or self._rTop or self._rBottom:
147 self._resize = True
148 self.min_size = (30, 30)
149 event.consume()
150
151 def mouseReleased(self, event):
152 if self._resize:
153 self.min_size = (self.width, self.height)
154 self.adaptLayout()
155 event.consume()
156
157 titleheight = 0
158 if hasattr(self.real_widget, "getTitleBarHeight"):
159 titleheight = self.real_widget.getTitleBarHeight()
160
161 self._resize = False
162 if event.getX() <= 0 or event.getX() >= self.width \
163 or event.getY() <= 0 or event.getY() >= self.height+titleheight:
164 self.mouseExited(event)
165
166 # Ugly hack to allow consumption of mousemove events
167 _mousemoveevent = {}
168