forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectScrollBar.py
More file actions
199 lines (165 loc) · 7.69 KB
/
DirectScrollBar.py
File metadata and controls
199 lines (165 loc) · 7.69 KB
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
"""Defines the DirectScrollBar class.
See the :ref:`directscrollbar` page in the programming manual for a more
in-depth explanation and an example of how to use this class.
"""
__all__ = ['DirectScrollBar']
from panda3d.core import *
from . import DirectGuiGlobals as DGG
from .DirectFrame import *
from .DirectButton import *
"""
import DirectScrollBar
d = DirectScrollBar(borderWidth=(0, 0))
"""
class DirectScrollBar(DirectFrame):
"""
DirectScrollBar -- a widget which represents a scroll bar the user can
use for paging through a large document or panel.
"""
def __init__(self, parent = None, **kw):
optiondefs = (
# Define type of DirectGuiWidget
('pgFunc', PGSliderBar, None),
('state', DGG.NORMAL, None),
('frameColor', (0.6, 0.6, 0.6, 1), None),
('range', (0, 1), self.setRange),
('value', 0, self.__setValue),
('scrollSize', 0.01, self.setScrollSize),
('pageSize', 0.1, self.setPageSize),
('orientation', DGG.HORIZONTAL, self.setOrientation),
('manageButtons', 1, self.setManageButtons),
('resizeThumb', 1, self.setResizeThumb),
# Function to be called repeatedly as the bar is scrolled
('command', None, None),
('extraArgs', [], None),
)
if kw.get('orientation') in (DGG.VERTICAL, DGG.VERTICAL_INVERTED):
# These are the default options for a vertical layout.
optiondefs += (
('frameSize', (-0.04, 0.04, -0.5, 0.5), None),
)
else:
# These are the default options for a horizontal layout.
optiondefs += (
('frameSize', (-0.5, 0.5, -0.04, 0.04), None),
)
# Merge keyword options with default options
self.defineoptions(kw, optiondefs)
# Initialize superclasses
DirectFrame.__init__(self, parent)
self.thumb = self.createcomponent(
"thumb", (), None,
DirectButton, (self,),
borderWidth = self['borderWidth'])
self.incButton = self.createcomponent(
"incButton", (), None,
DirectButton, (self,),
borderWidth = self['borderWidth'])
self.decButton = self.createcomponent(
"decButton", (), None,
DirectButton, (self,),
borderWidth = self['borderWidth'])
if self.decButton['frameSize'] == None and \
self.decButton.bounds == [0.0, 0.0, 0.0, 0.0]:
f = self['frameSize']
if self['orientation'] == DGG.HORIZONTAL:
self.decButton['frameSize'] = (f[0]*0.05, f[1]*0.05, f[2], f[3])
else:
self.decButton['frameSize'] = (f[0], f[1], f[2]*0.05, f[3]*0.05)
if self.incButton['frameSize'] == None and \
self.incButton.bounds == [0.0, 0.0, 0.0, 0.0]:
f = self['frameSize']
if self['orientation'] == DGG.HORIZONTAL:
self.incButton['frameSize'] = (f[0]*0.05, f[1]*0.05, f[2], f[3])
else:
self.incButton['frameSize'] = (f[0], f[1], f[2]*0.05, f[3]*0.05)
self._lastOrientation = self['orientation']
self.guiItem.setThumbButton(self.thumb.guiItem)
self.guiItem.setLeftButton(self.decButton.guiItem)
self.guiItem.setRightButton(self.incButton.guiItem)
# Bind command function
self.bind(DGG.ADJUST, self.commandFunc)
# Call option initialization functions
self.initialiseoptions(DirectScrollBar)
def setRange(self):
# Try to preserve the value across a setRange call.
v = self['value']
r = self['range']
self.guiItem.setRange(r[0], r[1])
self['value'] = v
def __setValue(self):
# This is the internal function that is called when
# self['value'] is directly assigned.
self.guiItem.setValue(self['value'])
def setValue(self, value):
# This is the public function that is meant to be called by a
# user that doesn't like to use (or doesn't understand) the
# preferred interface of self['value'].
self['value'] = value
def getValue(self):
return self.guiItem.getValue()
def getRatio(self):
return self.guiItem.getRatio()
def setScrollSize(self):
self.guiItem.setScrollSize(self['scrollSize'])
def setPageSize(self):
self.guiItem.setPageSize(self['pageSize'])
def scrollStep(self, stepCount):
"""Scrolls the indicated number of steps forward. If
stepCount is negative, scrolls backward."""
self['value'] = self.guiItem.getValue() + self.guiItem.getScrollSize() * stepCount
def scrollPage(self, pageCount):
"""Scrolls the indicated number of pages forward. If
pageCount is negative, scrolls backward."""
self['value'] = self.guiItem.getValue() + self.guiItem.getPageSize() * pageCount
def setOrientation(self):
if self['orientation'] == DGG.HORIZONTAL:
if self._lastOrientation in (DGG.VERTICAL, DGG.VERTICAL_INVERTED):
fpre = self['frameSize']
# swap frameSize width and height to keep custom frameSizes
self['frameSize'] = (fpre[2], fpre[3], fpre[0], fpre[1])
f = self.decButton['frameSize']
self.decButton['frameSize'] = (f[2], f[3], f[0], f[1])
f = self.incButton['frameSize']
self.incButton['frameSize'] = (f[2], f[3], f[0], f[1])
self.guiItem.setAxis(Vec3(1, 0, 0))
elif self['orientation'] == DGG.VERTICAL:
if self._lastOrientation == DGG.HORIZONTAL:
fpre = self['frameSize']
# swap frameSize width and height to keep custom frameSizes
self['frameSize'] = (fpre[2], fpre[3], fpre[0], fpre[1])
f = self.decButton['frameSize']
self.decButton['frameSize'] = (f[2], f[3], f[0], f[1])
f = self.incButton['frameSize']
self.incButton['frameSize'] = (f[2], f[3], f[0], f[1])
self.guiItem.setAxis(Vec3(0, 0, -1))
elif self['orientation'] == DGG.VERTICAL_INVERTED:
if self._lastOrientation == DGG.HORIZONTAL:
fpre = self['frameSize']
# swap frameSize width and height to keep custom frameSizes
self['frameSize'] = (fpre[2], fpre[3], fpre[0], fpre[1])
f = self.decButton['frameSize']
self.decButton['frameSize'] = (f[2], f[3], f[0], f[1])
f = self.incButton['frameSize']
self.incButton['frameSize'] = (f[2], f[3], f[0], f[1])
self.guiItem.setAxis(Vec3(0, 0, 1))
else:
raise ValueError('Invalid value for orientation: %s' % (self['orientation']))
self._lastOrientation = self['orientation']
def setManageButtons(self):
self.guiItem.setManagePieces(self['manageButtons'])
def setResizeThumb(self):
self.guiItem.setResizeThumb(self['resizeThumb'])
def destroy(self):
self.thumb.destroy()
del self.thumb
self.incButton.destroy()
del self.incButton
self.decButton.destroy()
del self.decButton
DirectFrame.destroy(self)
def commandFunc(self):
# Store the updated value in self['value']
self._optionInfo['value'][DGG._OPT_VALUE] = self.guiItem.getValue()
if self['command']:
self['command'](*self['extraArgs'])