forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectWaitBar.py
More file actions
127 lines (108 loc) · 4.68 KB
/
DirectWaitBar.py
File metadata and controls
127 lines (108 loc) · 4.68 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
"""Contains the DirectWaitBar class, a progress bar widget.
See the :ref:`directwaitbar` page in the programming manual for a more
in-depth explanation and an example of how to use this class.
"""
__all__ = ['DirectWaitBar']
from panda3d.core import *
from . import DirectGuiGlobals as DGG
from .DirectFrame import *
"""
import DirectWaitBar
d = DirectWaitBar(borderWidth=(0, 0))
"""
class DirectWaitBar(DirectFrame):
""" DirectWaitBar - A DirectWidget that shows progress completed
towards a task. """
def __init__(self, parent = None, **kw):
# Inherits from DirectFrame
# A Direct Frame can have:
# - A background texture (pass in path to image, or Texture Card)
# - A midground geometry item (pass in geometry)
# - A foreground text Node (pass in text string or Onscreen Text)
optiondefs = (
# Define type of DirectGuiWidget
('pgFunc', PGWaitBar, None),
('frameSize', (-1, 1, -0.08, 0.08), None),
('borderWidth', (0, 0), None),
('range', 100, self.setRange),
('value', 0, self.setValue),
('barBorderWidth', (0, 0), self.setBarBorderWidth),
('barColor', (1, 0, 0, 1), self.setBarColor),
('barTexture', None, self.setBarTexture),
('barRelief', DGG.FLAT, self.setBarRelief),
('sortOrder', DGG.NO_FADE_SORT_INDEX, None),
)
if 'text' in kw:
textoptiondefs = (
('text_pos', (0, -0.025), None),
('text_scale', 0.1, None)
)
else:
textoptiondefs = ()
# Merge keyword options with default options
self.defineoptions(kw, optiondefs + textoptiondefs)
# Initialize superclasses
DirectFrame.__init__(self, parent)
self.barStyle = PGFrameStyle()
# Call option initialization functions
self.initialiseoptions(DirectWaitBar)
self.updateBarStyle()
def destroy(self):
self.barStyle = None
DirectFrame.destroy(self)
def setRange(self):
"""Updates the bar range which you can set using bar['range'].
This is the value at which the WaitBar indicates 100%."""
self.guiItem.setRange(self['range'])
def setValue(self):
"""Updates the bar value which you can set using bar['value'].
The value should range between 0 and bar['range']."""
self.guiItem.setValue(self['value'])
def getPercent(self):
"""Returns the percentage complete."""
return self.guiItem.getPercent()
def updateBarStyle(self):
if not self.fInit:
self.guiItem.setBarStyle(self.barStyle)
def setBarRelief(self):
"""Updates the bar relief, which you can set using bar['barRelief']."""
self.barStyle.setType(self['barRelief'])
self.updateBarStyle()
def setBarBorderWidth(self):
"""Updates the bar's border width, which you can set using bar['barBorderWidth']."""
self.barStyle.setWidth(*self['barBorderWidth'])
self.updateBarStyle()
def setBarColor(self):
"""Updates the bar color, which you can set using bar['barColor']."""
color = self['barColor']
self.barStyle.setColor(color[0], color[1], color[2], color[3])
self.updateBarStyle()
def setBarTexture(self):
"""Updates the bar texture, which you can set using bar['barTexture']."""
# this must be a single texture (or a string).
texture = self['barTexture']
if isinstance(texture, str):
texture = loader.loadTexture(texture)
if texture:
self.barStyle.setTexture(texture)
else:
self.barStyle.clearTexture()
self.updateBarStyle()
def update(self, value):
"""Updates the bar with the given value and renders a frame."""
self['value'] = value
# Render a frame out-of-sync with the igLoop to update the
# window right now. This allows the wait bar to be updated
# even though we are not normally rendering frames.
base.graphicsEngine.renderFrame()
def finish(self, N = 10):
"""Fill the bar in N frames. This call is blocking."""
remaining = self['range'] - self['value']
if remaining:
step = max(1, int(remaining / N))
count = self['value']
while count != self['range']:
count += step
if count > self['range']:
count = self['range']
self.update(count)