/
DirectWindow.py
398 lines (356 loc) · 16.7 KB
/
DirectWindow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# original code by Hypnos, also includes code by Croixis
# http://www.panda3d.org/phpbb2/viewtopic.php?t=4861
# requires boxes.py by chombee
# http://www.panda3d.org/phpbb2/viewtopic.php?p=22420#22420
# Stephen Lujan, March 08 2010
from pandac.PandaModules import TextNode, Vec3
from direct.gui.DirectGui import DirectFrame,DirectButton,DirectScrolledFrame,DGG,DirectLabel
import boxes
#from direct.directbase.DirectStart import *
if __name__ == "__main__":
from direct.directbase.DirectStart import *
# a task that keeps a node at the position of the mouse-cursor
def mouseNodeTask( task ):
if base.mouseWatcherNode.hasMouse():
x=base.mouseWatcherNode.getMouseX()
y=base.mouseWatcherNode.getMouseY()
# the mouse position is read relative to render2d, so set it accordingly
aspect2dMouseNode.setPos( render2d, x, 0, y )
#render2dMouseNode.setPos( render2d, x, 0, y )
return task.cont
# maybe we should check if aspect2d doesnt already contain the aspect2dMouseNode
aspect2dMouseNode = aspect2d.attachNewNode( 'aspect2dMouseNode', sort = 999999 )
#render2dMouseNode = render2d.attachNewNode( 'render2dMouseNode', sort = 999999 )
taskMgr.add( mouseNodeTask, 'mouseNodeTask' )
DIRECTORY = 'windowBorders/'
DEFAULT_TITLE_GEOM_LEFT = loader.loadTexture( DIRECTORY+'titleLeft.png' )
DEFAULT_TITLE_GEOM_CENTER = loader.loadTexture( DIRECTORY+'titleCenter.png' )
DEFAULT_TITLE_GEOM_RIGHT = loader.loadTexture( DIRECTORY+'titleRight.png' )
DEFAULT_TITLE_GEOM_RIGHT_CLOSE = loader.loadTexture( DIRECTORY+'titleRightClose.png' )
DEFAULT_RESIZE_GEOM = loader.loadTexture( DIRECTORY+'resize.png' )
VERTICALSCROLL_FRAMETEXTURE = loader.loadTexture( DIRECTORY+'scrollVerticalBorder.png' )
VERTICALSCROLL_INCBUTTON_FRAMETEXTURE = loader.loadTexture( DIRECTORY+'scrollVerticalDown.png' )
VERTICALSCROLL_DECBUTTON_FRAMETEXTURE = loader.loadTexture( DIRECTORY+'scrollVerticalUp.png' )
VERTICALSCROLL_TUMB_FRAMETEXTURE = loader.loadTexture( DIRECTORY+'scrollVerticalBar.png' )
HORIZONTALSCROLL_FRAMETEXTURE = loader.loadTexture( DIRECTORY+'scrollHorizontalBorder.png' )
HORIZONTALSCROLL_INCBUTTON_FRAMETEXTURE = loader.loadTexture( DIRECTORY+'scrollHorizontalRight.png' )
HORIZONTALSCROLL_DECBUTTON_FRAMETEXTURE = loader.loadTexture( DIRECTORY+'scrollHorizontalLeft.png' )
HORIZONTALSCROLL_TUMB_FRAMETEXTURE = loader.loadTexture( DIRECTORY+'scrollHorizontalBar.png' )
class DirectWindow( DirectFrame ):
def __init__( self,
pos = ( -.5, .5),
title = 'Title',
curSize = ( 1, 1),
maxSize = ( 1, 1 ),
minSize = ( .5, .5 ),
backgroundColor = ( 1, 1, 1, 1 ),
borderColor = ( 1, 1, 1, 1 ),
titleColor = ( 1, 1, 1, 1 ),
borderSize = 0.04,
titleSize = 0.06,
closeButton = False,
windowParent = aspect2d,
preserve = True,
preserveWhole = True,
):
self.preserve = preserve
self.preserveWhole = preserveWhole
self.windowParent = windowParent
self.windowPos = pos
DirectFrame.__init__( self,
parent = windowParent,
pos = ( self.windowPos[0], 0, self.windowPos[1] ),
frameColor = ( 0, 0, 0, 0 ),
frameTexture = loader.loadTexture( DIRECTORY+'transparent.png' )
)
self.setTransparency(True)
# the title part of the window, drag around to move the window
self.headerHeight = titleSize
h = -self.headerHeight
self.windowHeaderLeft = DirectButton(
parent = self,
frameTexture = DEFAULT_TITLE_GEOM_LEFT,
frameSize = ( -.5, .5, -.5, .5 ),
borderWidth = ( 0, 0 ),
relief = DGG.FLAT,
frameColor = titleColor,
)
self.windowHeaderCenter = DirectButton(
parent = self,
frameTexture = DEFAULT_TITLE_GEOM_CENTER,
frameSize = ( -.5, .5, -.5, .5 ),
borderWidth = ( 0, 0 ),
relief = DGG.FLAT,
frameColor = titleColor,
)
if closeButton:
rightTitleGeom = DEFAULT_TITLE_GEOM_RIGHT_CLOSE
command = self.destroy
else:
rightTitleGeom = DEFAULT_TITLE_GEOM_RIGHT
command = None
self.windowHeaderRight = DirectButton(
parent = self,
frameTexture = rightTitleGeom,
frameSize = ( -.5, .5, -.5, .5 ),
borderWidth = ( 0, 0 ),
relief = DGG.FLAT,
frameColor = titleColor,
command = command
)
self.windowHeaderLeft.setTransparency(True)
self.windowHeaderCenter.setTransparency(True)
self.windowHeaderRight.setTransparency(True)
self.windowHeaderLeft.bind( DGG.B1PRESS, self.startWindowDrag )
self.windowHeaderCenter.bind( DGG.B1PRESS, self.startWindowDrag )
self.windowHeaderRight.bind( DGG.B1PRESS, self.startWindowDrag )
# this is not handled correctly, if a window is dragged which has been
# created before another it will not be released
# check the bugfixed startWindowDrag function
#self.windowHeader.bind(DGG.B1RELEASE,self.stopWindowDrag)
text = TextNode('WindowTitleTextNode')
text.setText(title)
text.setAlign(TextNode.ACenter)
text.setTextColor( 0, 0, 0, 1 )
text.setShadow(0.05, 0.05)
text.setShadowColor( 1, 1, 1, 1 )
self.textNodePath = self.attachNewNode(text)
self.textNodePath.setScale(self.headerHeight*0.8)
# the content part of the window, put stuff beneath
# contentWindow.getCanvas() to put it into it
self.maxVirtualSize = maxSize
self.minVirtualSize = minSize
self.resizeSize = borderSize
self.contentWindow = DirectScrolledFrame(
parent = self,
pos = ( 0, 0, -self.headerHeight ),
canvasSize = ( 0, self.maxVirtualSize[0], 0, self.maxVirtualSize[1] ),
frameColor = ( 0, 0, 0, 0), # defines the background color of the resize-button
relief = DGG.FLAT,
borderWidth = (0, 0),
verticalScroll_frameSize = [0, self.resizeSize, 0, 1],
horizontalScroll_frameSize = [0, 1, 0, self.resizeSize],
# resize the scrollbar according to window size
verticalScroll_resizeThumb = False,
horizontalScroll_resizeThumb = False,
# define the textures for the scrollbars
verticalScroll_frameTexture = VERTICALSCROLL_FRAMETEXTURE,
verticalScroll_incButton_frameTexture = VERTICALSCROLL_INCBUTTON_FRAMETEXTURE,
verticalScroll_decButton_frameTexture = VERTICALSCROLL_DECBUTTON_FRAMETEXTURE,
verticalScroll_thumb_frameTexture = VERTICALSCROLL_TUMB_FRAMETEXTURE,
horizontalScroll_frameTexture = HORIZONTALSCROLL_FRAMETEXTURE,
horizontalScroll_incButton_frameTexture = HORIZONTALSCROLL_INCBUTTON_FRAMETEXTURE,
horizontalScroll_decButton_frameTexture = HORIZONTALSCROLL_DECBUTTON_FRAMETEXTURE,
horizontalScroll_thumb_frameTexture = HORIZONTALSCROLL_TUMB_FRAMETEXTURE,
# make all flat, so the texture is as we want it
verticalScroll_relief = DGG.FLAT,
verticalScroll_thumb_relief = DGG.FLAT,
verticalScroll_decButton_relief = DGG.FLAT,
verticalScroll_incButton_relief = DGG.FLAT,
horizontalScroll_relief = DGG.FLAT,
horizontalScroll_thumb_relief = DGG.FLAT,
horizontalScroll_decButton_relief = DGG.FLAT,
horizontalScroll_incButton_relief = DGG.FLAT,
# colors
verticalScroll_frameColor = borderColor,
verticalScroll_incButton_frameColor = borderColor,
verticalScroll_decButton_frameColor = borderColor,
verticalScroll_thumb_frameColor = borderColor,
horizontalScroll_frameColor = borderColor,
horizontalScroll_incButton_frameColor = borderColor,
horizontalScroll_decButton_frameColor = borderColor,
horizontalScroll_thumb_frameColor = borderColor,
)
self.contentWindow.setTransparency(True)
# background color
self.backgroundColor = DirectFrame(
parent = self.contentWindow.getCanvas(),
frameSize = ( 0, self.maxVirtualSize[0], 0, self.maxVirtualSize[1] ),
frameColor = backgroundColor,
relief = DGG.FLAT,
borderWidth = ( .01, .01),
)
self.backgroundColor.setTransparency(True)
# Add a box
self.box = boxes.VBox(parent = self.getCanvas())
# is needed for some nicer visuals of the resize button (background)
self.windowResizeBackground = DirectButton(
parent = self,
frameSize = ( -.5, .5, -.5, .5 ),
borderWidth = ( 0, 0 ),
scale = ( self.resizeSize, 1, self.resizeSize ),
relief = DGG.FLAT,
frameColor = backgroundColor,
)
# the resize button of the window
self.windowResize = DirectButton(
parent = self,
frameSize = ( -.5, .5, -.5, .5 ),
borderWidth = ( 0, 0 ),
scale = ( self.resizeSize, 1, self.resizeSize ),
relief = DGG.FLAT,
frameTexture = DEFAULT_RESIZE_GEOM,
frameColor = borderColor,
)
self.windowResize.setTransparency(True)
self.windowResize.bind(DGG.B1PRESS,self.startResizeDrag)
self.windowResize.bind(DGG.B1RELEASE,self.stopResizeDrag)
# offset then clicking on the resize button from the mouse to the resizebutton
# position, required to calculate the position / scaling
self.offset = None
self.taskName = "resizeTask-%s" % str(hash(self))
# do sizing of the window (minimum)
#self.resize( Vec3(0,0,0), Vec3(0,0,0) )
# maximum
#self.resize( Vec3(100,0,-100), Vec3(0,0,0) )
self.resize( Vec3(curSize[0], 0, -curSize[1]), Vec3(0,0,0))
def getCanvas(self):
return self.contentWindow.getCanvas()
# dragging functions
def startWindowDrag( self, param ):
self.wrtReparentTo( aspect2dMouseNode )
self.ignoreAll()
self.accept( 'mouse1-up', self.stopWindowDrag )
def stopWindowDrag( self, param=None ):
# this is called 2 times (bug), so make sure it's not already parented to aspect2d
if self.getParent() != self.windowParent:
self.wrtReparentTo( self.windowParent )
if self.preserve:
if self.preserveWhole:
if self.getZ() > 1:
self.setZ(1)
elif self.getZ() < -1 - self.getHeight():
self.setZ(-1 - self.getHeight())
if self.getX() > base.a2dRight - self.getWidth():
self.setX(base.a2dRight - self.getWidth())
elif self.getX() < base.a2dLeft:
self.setX(base.a2dLeft)
else:
if self.getZ() > 1:
self.setZ(1)
elif self.getZ() < -1 + self.headerHeight:
self.setZ(-1 + self.headerHeight)
if self.getX() > base.a2dRight - self.headerHeight:
self.setX(base.a2dRight - self.headerHeight)
elif self.getX() < base.a2dLeft + self.headerHeight - self.getWidth():
self.setX(base.a2dLeft + self.headerHeight - self.getWidth())
#else: #Window moved beyond reach. Destroy window?
# resize functions
def resize( self, mPos, offset ):
mXPos = max( min( mPos.getX(), self.maxVirtualSize[0] ), self.minVirtualSize[0])
mZPos = max( min( mPos.getZ(), -self.minVirtualSize[1] ), -self.maxVirtualSize[1]-self.headerHeight)
self.windowResize.setPos( mXPos-self.resizeSize/2., 0, mZPos+self.resizeSize/2. )
self.windowResizeBackground.setPos( mXPos-self.resizeSize/2., 0, mZPos+self.resizeSize/2. )
self['frameSize'] = (0, mXPos, 0, mZPos)
self.windowHeaderLeft.setPos( self.headerHeight/2., 0, -self.headerHeight/2. )
self.windowHeaderLeft.setScale( self.headerHeight, 1, self.headerHeight )
self.windowHeaderCenter.setPos( mXPos/2., 0, -self.headerHeight/2. )
self.windowHeaderCenter.setScale( mXPos - self.headerHeight*2., 1, self.headerHeight )
self.windowHeaderRight.setPos( mXPos-self.headerHeight/2., 0, -self.headerHeight/2. )
self.windowHeaderRight.setScale( self.headerHeight, 1, self.headerHeight )
self.contentWindow['frameSize'] = ( 0, mXPos, mZPos+self.headerHeight, 0)
self.textNodePath.setPos( mXPos/2., 0, -self.headerHeight/3.*2. )
# show and hide that small background for the window sizer
if mXPos == self.maxVirtualSize[0] and \
mZPos == -self.maxVirtualSize[1]-self.headerHeight:
self.windowResizeBackground.hide()
else:
self.windowResizeBackground.show()
def resizeTask( self, task=None ):
mPos = aspect2dMouseNode.getPos( self )+self.offset
self.resize( mPos, self.offset )
return task.cont
def startResizeDrag( self, param ):
self.offset = self.windowResize.getPos( aspect2dMouseNode )
taskMgr.remove( self.taskName )
taskMgr.add( self.resizeTask, self.taskName )
def stopResizeDrag( self, param ):
taskMgr.remove( self.taskName )
# get the window to the front
self.wrtReparentTo( self.windowParent )
def addHorizontal(self, widgets):
"""
Accepts a list of directgui objects which are added to a horizontal box, which is then added to the vertical stack.
"""
hbox = boxes.HBox()
for widget in widgets:
hbox.pack(widget)
self.box.pack(hbox)
self.updateMaxSize()
def addVertical(self, widgets):
"""
Accepts a list of directgui objects which are added to a vertical box, which is then added to the vertical stack.
May cause funky layout results.
"""
#vbox = boxes.VBox()
for widget in widgets:
self.box.pack(widget)
self.updateMaxSize()
def add(self, widgets):
"""Shortcut function for addVertical"""
self.addVertical(widgets)
def updateMaxSize(self):
"""Updates the max canvas size to include all items packed.
Window is resized to show all contents."""
bottomLeft, topRight = self.box.getTightBounds()
self.maxVirtualSize = (topRight[0], -bottomLeft[2])
self.contentWindow['canvasSize'] = ( 0, self.maxVirtualSize[0], -self.maxVirtualSize[1], 0)
self.backgroundColor['frameSize'] = ( 0, self.maxVirtualSize[0], -self.maxVirtualSize[1], 0 )
#perhaps this should be optional -- automatically resize for new elements
self.reset()
def reset(self):
"""Poorly named function that resizes window to fit all contents"""
self.resize( Vec3(self.maxVirtualSize[0], 0, -self.maxVirtualSize[1]-self.headerHeight), Vec3(0,0,0))
def windowDemo():
WindowSet = aspect2d.attachNewNode("Gui Set 1")
#WindowSet = render2d.attachNewNode("Gui Set 1")
# a first window
window1 = DirectWindow(
title = 'window1',
pos = ( -.8, .8 ),
backgroundColor = ( 0.1, 0.1, 0.1, .6),
borderColor = ( 0.5, 0.5, 0.5, .7),
titleColor = ( 0.5, 0.5, 0.5, .6),
titleSize = 0.1,
borderSize = 0.06,
windowParent = WindowSet,
)
windowContent = DirectButton(text = "Button1", scale=0.5, relief = DGG.FLAT, frameColor = (0,1,0,1),)
windowContent2 = DirectButton(text = "B2", scale=0.5, relief = DGG.FLAT, frameColor = (0,1,0,1),)
window1.addVertical([windowContent, windowContent2])
# a second window
window2 = DirectWindow(
title = 'window2',
pos = ( -.4, .4),
backgroundColor = (1,0,0,0.5),
windowParent = WindowSet,
)
string1 = "Press tab to\ntoggle showing/hiding\nof all windows."
windowContent = DirectLabel(text = string1, frameColor = (0,1,1,1), scale=0.1)
windowContent2 = DirectLabel(text = 'L2', frameColor = (0,1,1,1), scale=0.8)
window2.addHorizontal([windowContent, windowContent2])
# a third window with a close button featuring autoresize
window3 = DirectWindow(
title='window3',
pos = ( 0, 0),
closeButton=True,
windowParent= WindowSet,
)
windowContent = DirectButton(
text = "Press 1 to close me",
relief = DGG.FLAT,
frameColor = (0,1,1,1),
scale = 0.05,
)
window3.add([windowContent])
base.accept('1', window3.destroy)
def toggleShow():
if WindowSet.isHidden():
WindowSet.show()
else:
WindowSet.hide()
base.accept('tab', toggleShow)
if __name__ == "__main__":
# small scale test
windowDemo()
run()