forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectFrame.py
More file actions
155 lines (127 loc) · 4.96 KB
/
DirectFrame.py
File metadata and controls
155 lines (127 loc) · 4.96 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
"""A DirectFrame is a basic DirectGUI component that acts as the base
class for various other components, and can also serve as a basic
container to hold other DirectGUI components.
A DirectFrame 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 OnscreenText)
Each of these has 1 or more states. The same object can be used for
all states or each state can have a different text/geom/image (for
radio button and check button indicators, for example).
See the :ref:`directframe` page in the programming manual for a more in-depth
explanation and an example of how to use this class.
"""
__all__ = ['DirectFrame']
from panda3d.core import *
from . import DirectGuiGlobals as DGG
from .DirectGuiBase import *
from .OnscreenImage import OnscreenImage
from .OnscreenGeom import OnscreenGeom
from .OnscreenText import OnscreenText
class DirectFrame(DirectGuiWidget):
DefDynGroups = ('text', 'geom', 'image')
def __init__(self, parent=None, **kw):
# Inherits from DirectGuiWidget
optiondefs = (
# Define type of DirectGuiWidget
('pgFunc', PGItem, None),
('numStates', 1, None),
('state', self.inactiveInitState, None),
# Frame can have:
# A background texture
('image', None, self.setImage),
# A midground geometry item
('geom', None, self.setGeom),
# A foreground text node
('text', None, self.setText),
# Change default value of text mayChange flag from 0
# (OnscreenText.py) to 1
('textMayChange', 1, None),
)
# Merge keyword options with default options
self.defineoptions(kw, optiondefs,
dynamicGroups=DirectFrame.DefDynGroups)
# Initialize superclasses
DirectGuiWidget.__init__(self, parent)
# Call option initialization functions
self.initialiseoptions(DirectFrame)
def __reinitComponent(self, name, component_class, states, **kwargs):
"""Recreates the given component using the given keyword args."""
assert name in ("geom", "image", "text")
# constants should be local to or default arguments of constructors
for c in range(self['numStates']):
component_name = name + str(c)
try:
state = states[c]
except IndexError:
state = states[-1]
if self.hascomponent(component_name):
if state is None:
self.destroycomponent(component_name)
else:
self[component_name + "_" + name] = state
else:
if state is None:
return
kwargs[name] = state
self.createcomponent(
component_name,
(),
name,
component_class,
(),
parent=self.stateNodePath[c],
**kwargs
)
def clearText(self):
self['text'] = None
self.setText()
def setText(self, text=None):
if text is not None:
self["text"] = text
text = self["text"]
if text is None or isinstance(text, str):
text_list = (text,) * self['numStates']
else:
text_list = text
self.__reinitComponent("text", OnscreenText, text_list,
scale=1,
mayChange=self['textMayChange'],
sort=DGG.TEXT_SORT_INDEX)
def clearGeom(self):
self['geom'] = None
self.setGeom()
def setGeom(self, geom=None):
if geom is not None:
self["geom"] = geom
geom = self["geom"]
if geom is None or \
isinstance(geom, NodePath) or \
isinstance(geom, str):
geom_list = (geom,) * self['numStates']
else:
geom_list = geom
self.__reinitComponent("geom", OnscreenGeom, geom_list,
scale=1,
sort=DGG.GEOM_SORT_INDEX)
def clearImage(self):
self['image'] = None
self.setImage()
def setImage(self, image=None):
if image is not None:
self["image"] = image
image = self["image"]
if image is None or \
isinstance(image, NodePath) or \
isinstance(image, Texture) or \
isinstance(image, str) or \
isinstance(image, Filename) or \
(len(image) == 2 and \
isinstance(image[0], str) and \
isinstance(image[1], str)):
image_list = (image,) * self['numStates']
else:
image_list = image
self.__reinitComponent("image", OnscreenImage, image_list,
scale=1,
sort=DGG.IMAGE_SORT_INDEX)