Panda3D
pgWaitBar.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file pgWaitBar.cxx
10  * @author drose
11  * @date 2002-03-14
12  */
13 
14 #include "pgWaitBar.h"
16 
17 #include "throw_event.h"
18 
19 TypeHandle PGWaitBar::_type_handle;
20 
21 /**
22  *
23  */
24 PGWaitBar::
25 PGWaitBar(const std::string &name) : PGItem(name)
26 {
27  set_cull_callback();
28 
29  _range = 100.0;
30  _value = 0.0;
31  _bar_state = -1;
32 }
33 
34 /**
35  *
36  */
37 PGWaitBar::
38 ~PGWaitBar() {
39 }
40 
41 /**
42  *
43  */
44 PGWaitBar::
45 PGWaitBar(const PGWaitBar &copy) :
46  PGItem(copy),
47  _range(copy._range),
48  _value(copy._value)
49 {
50  _bar_state = -1;
51 }
52 
53 /**
54  * Returns a newly-allocated Node that is a shallow copy of this one. It will
55  * be a different Node pointer, but its internal data may or may not be shared
56  * with that of the original Node.
57  */
59 make_copy() const {
60  LightReMutexHolder holder(_lock);
61  return new PGWaitBar(*this);
62 }
63 
64 /**
65  * This function will be called during the cull traversal to perform any
66  * additional operations that should be performed at cull time. This may
67  * include additional manipulation of render state or additional
68  * visible/invisible decisions, or any other arbitrary operation.
69  *
70  * Note that this function will *not* be called unless set_cull_callback() is
71  * called in the constructor of the derived class. It is necessary to call
72  * set_cull_callback() to indicated that we require cull_callback() to be
73  * called.
74  *
75  * By the time this function is called, the node has already passed the
76  * bounding-volume test for the viewing frustum, and the node's transform and
77  * state have already been applied to the indicated CullTraverserData object.
78  *
79  * The return value is true if this node should be visible, or false if it
80  * should be culled.
81  */
82 bool PGWaitBar::
84  LightReMutexHolder holder(_lock);
85  update();
86  return PGItem::cull_callback(trav, data);
87 }
88 
89 /**
90  * Creates a PGWaitBar with the indicated dimensions, with the indicated
91  * maximum range.
92  */
93 void PGWaitBar::
94 setup(PN_stdfloat width, PN_stdfloat height, PN_stdfloat range) {
95  LightReMutexHolder holder(_lock);
96  set_state(0);
97  clear_state_def(0);
98 
99  set_frame(-0.5f * width, 0.5f * width, -0.5f * height, 0.5f * height);
100 
101  PN_stdfloat bevel = 0.05f;
102 
103  PGFrameStyle style;
104  style.set_width(bevel, bevel);
105 
106  style.set_color(0.6f, 0.6f, 0.6f, 1.0f);
107  style.set_type(PGFrameStyle::T_bevel_in);
108  set_frame_style(0, style);
109 
110  style.set_color(0.8f, 0.8f, 0.8f, 1.0f);
111  style.set_type(PGFrameStyle::T_bevel_out);
112  set_bar_style(style);
113 }
114 
115 /**
116  * Computes the appropriate size of the bar frame according to the percentage
117  * completed.
118  */
119 void PGWaitBar::
120 update() {
121  LightReMutexHolder holder(_lock);
122  int state = get_state();
123 
124  // If the bar was last drawn in this state and is still current, we don't
125  // have to draw it again.
126  if (_bar_state == state) {
127  return;
128  }
129 
130  // Remove the old bar geometry, if any.
131  _bar.remove_node();
132 
133  // Now create new bar geometry.
134  if ((_value != 0.0f) && (_range != 0.0f)) {
135  NodePath &root = get_state_def(state);
136  nassertv(!root.is_empty());
137 
138  PGFrameStyle style = get_frame_style(state);
139  const LVecBase4 &frame = get_frame();
140  const LVecBase2 &width = style.get_width();
141 
142  // Put the bar within the item's frame's border.
143  LVecBase4 bar_frame(frame[0] + width[0],
144  frame[1] - width[0],
145  frame[2] + width[1],
146  frame[3] - width[1]);
147 
148  // And scale the bar according to our value.
149  PN_stdfloat frac = _value / _range;
150  frac = std::max(std::min(frac, (PN_stdfloat)1.0), (PN_stdfloat)0.0);
151  bar_frame[1] = bar_frame[0] + frac * (bar_frame[1] - bar_frame[0]);
152 
153  _bar = _bar_style.generate_into(root, bar_frame, 1);
154  }
155 
156  // Indicate that the bar is current for this state.
157  _bar_state = state;
158 }
A basic node of the scene graph or data graph.
Definition: pandaNode.h:64
void set_bar_style(const PGFrameStyle &style)
Sets the kind of frame that is drawn on top of the WaitBar to represent the amount completed.
Definition: pgWaitBar.I:67
void set_width(PN_stdfloat x, PN_stdfloat y)
Sets the width parameter, which has meaning only for certain frame types.
Definition: pgFrameStyle.I:139
PGFrameStyle get_frame_style(int state)
Returns the kind of frame that will be drawn behind the item when it is in the indicated state.
Definition: pgItem.cxx:979
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual bool cull_callback(CullTraverser *trav, CullTraverserData &data)
This function will be called during the cull traversal to perform any additional operations that shou...
Definition: pgWaitBar.cxx:83
This is the base class for all the various kinds of gui widget objects.
Definition: pgItem.h:53
int get_state() const
Returns the "state" of this particular PGItem.
Definition: pgItem.I:150
bool is_empty() const
Returns true if the NodePath contains no nodes.
Definition: nodePath.I:188
void set_type(Type type)
Sets the basic type of frame.
Definition: pgFrameStyle.I:64
void set_frame(PN_stdfloat left, PN_stdfloat right, PN_stdfloat bottom, PN_stdfloat top)
Sets the bounding rectangle of the item, in local coordinates.
Definition: pgItem.I:81
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual PandaNode * make_copy() const
Returns a newly-allocated Node that is a shallow copy of this one.
Definition: pgWaitBar.cxx:59
This collects together the pieces of data that are accumulated for each node while walking the scene ...
void setup(PN_stdfloat width, PN_stdfloat height, PN_stdfloat range)
Creates a PGWaitBar with the indicated dimensions, with the indicated maximum range.
Definition: pgWaitBar.cxx:94
void set_frame_style(int state, const PGFrameStyle &style)
Changes the kind of frame that will be drawn behind the item when it is in the indicated state.
Definition: pgItem.cxx:992
void set_color(PN_stdfloat r, PN_stdfloat g, PN_stdfloat b, PN_stdfloat a)
Sets the dominant color of the frame.
Definition: pgFrameStyle.I:80
NodePath generate_into(const NodePath &parent, const LVecBase4 &frame, int sort=0)
Generates geometry representing a frame of the indicated size, and parents it to the indicated node,...
This is a particular kind of PGItem that draws a little bar that fills from left to right to indicate...
Definition: pgWaitBar.h:26
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Similar to MutexHolder, but for a light reentrant mutex.
void set_state(int state)
Sets the "state" of this particular PGItem.
Definition: pgItem.I:141
void remove_node(Thread *current_thread=Thread::get_current_thread())
Disconnects the referenced node from the scene graph.
Definition: nodePath.cxx:591
void clear_state_def(int state)
Resets the NodePath assigned to the indicated state to its initial default, with only a frame represe...
Definition: pgItem.cxx:944
const LVecBase4 & get_frame() const
Returns the bounding rectangle of the item.
Definition: pgItem.I:106
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
get_state_def
Returns the Node that is the root of the subgraph that will be drawn when the PGItem is in the indica...
Definition: pgItem.h:135
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:161
This object performs a depth-first traversal of the scene graph, with optional view-frustum culling,...
Definition: cullTraverser.h:45