Panda3D
 All Classes Functions Variables Enumerations
pgWaitBar.cxx
1 // Filename: pgWaitBar.cxx
2 // Created by: drose (14Mar02)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "pgWaitBar.h"
16 #include "pgMouseWatcherParameter.h"
17 
18 #include "throw_event.h"
19 
20 TypeHandle PGWaitBar::_type_handle;
21 
22 ////////////////////////////////////////////////////////////////////
23 // Function: PGWaitBar::Constructor
24 // Access: Published
25 // Description:
26 ////////////////////////////////////////////////////////////////////
27 PGWaitBar::
28 PGWaitBar(const string &name) : PGItem(name)
29 {
30  set_cull_callback();
31 
32  _range = 100.0;
33  _value = 0.0;
34  _bar_state = -1;
35 }
36 
37 ////////////////////////////////////////////////////////////////////
38 // Function: PGWaitBar::Destructor
39 // Access: Public, Virtual
40 // Description:
41 ////////////////////////////////////////////////////////////////////
42 PGWaitBar::
43 ~PGWaitBar() {
44 }
45 
46 ////////////////////////////////////////////////////////////////////
47 // Function: PGWaitBar::Copy Constructor
48 // Access: Protected
49 // Description:
50 ////////////////////////////////////////////////////////////////////
51 PGWaitBar::
52 PGWaitBar(const PGWaitBar &copy) :
53  PGItem(copy),
54  _range(copy._range),
55  _value(copy._value)
56 {
57  _bar_state = -1;
58 }
59 
60 ////////////////////////////////////////////////////////////////////
61 // Function: PGWaitBar::make_copy
62 // Access: Public, Virtual
63 // Description: Returns a newly-allocated Node that is a shallow copy
64 // of this one. It will be a different Node pointer,
65 // but its internal data may or may not be shared with
66 // that of the original Node.
67 ////////////////////////////////////////////////////////////////////
69 make_copy() const {
70  LightReMutexHolder holder(_lock);
71  return new PGWaitBar(*this);
72 }
73 
74 ////////////////////////////////////////////////////////////////////
75 // Function: PGWaitBar::cull_callback
76 // Access: Protected, Virtual
77 // Description: This function will be called during the cull
78 // traversal to perform any additional operations that
79 // should be performed at cull time. This may include
80 // additional manipulation of render state or additional
81 // visible/invisible decisions, or any other arbitrary
82 // operation.
83 //
84 // Note that this function will *not* be called unless
85 // set_cull_callback() is called in the constructor of
86 // the derived class. It is necessary to call
87 // set_cull_callback() to indicated that we require
88 // cull_callback() to be called.
89 //
90 // By the time this function is called, the node has
91 // already passed the bounding-volume test for the
92 // viewing frustum, and the node's transform and state
93 // have already been applied to the indicated
94 // CullTraverserData object.
95 //
96 // The return value is true if this node should be
97 // visible, or false if it should be culled.
98 ////////////////////////////////////////////////////////////////////
99 bool PGWaitBar::
101  LightReMutexHolder holder(_lock);
102  update();
103  return PGItem::cull_callback(trav, data);
104 }
105 
106 ////////////////////////////////////////////////////////////////////
107 // Function: PGWaitBar::setup
108 // Access: Published
109 // Description: Creates a PGWaitBar with the indicated dimensions,
110 // with the indicated maximum range.
111 ////////////////////////////////////////////////////////////////////
112 void PGWaitBar::
113 setup(PN_stdfloat width, PN_stdfloat height, PN_stdfloat range) {
114  LightReMutexHolder holder(_lock);
115  set_state(0);
116  clear_state_def(0);
117 
118  set_frame(-0.5f * width, 0.5f * width, -0.5f * height, 0.5f * height);
119 
120  PN_stdfloat bevel = 0.05f;
121 
122  PGFrameStyle style;
123  style.set_width(bevel, bevel);
124 
125  style.set_color(0.6f, 0.6f, 0.6f, 1.0f);
126  style.set_type(PGFrameStyle::T_bevel_in);
127  set_frame_style(0, style);
128 
129  style.set_color(0.8f, 0.8f, 0.8f, 1.0f);
130  style.set_type(PGFrameStyle::T_bevel_out);
131  set_bar_style(style);
132 }
133 
134 ////////////////////////////////////////////////////////////////////
135 // Function: PGWaitBar::update
136 // Access: Private
137 // Description: Computes the appropriate size of the bar frame
138 // according to the percentage completed.
139 ////////////////////////////////////////////////////////////////////
140 void PGWaitBar::
141 update() {
142  LightReMutexHolder holder(_lock);
143  int state = get_state();
144 
145  // If the bar was last drawn in this state and is still current, we
146  // don't have to draw it again.
147  if (_bar_state == state) {
148  return;
149  }
150 
151  // Remove the old bar geometry, if any.
152  _bar.remove_node();
153 
154  // Now create new bar geometry.
155  if ((_value != 0.0f) && (_range != 0.0f)) {
156  NodePath &root = get_state_def(state);
157  nassertv(!root.is_empty());
158 
159  PGFrameStyle style = get_frame_style(state);
160  const LVecBase4 &frame = get_frame();
161  const LVecBase2 &width = style.get_width();
162 
163  // Put the bar within the item's frame's border.
164  LVecBase4 bar_frame(frame[0] + width[0],
165  frame[1] - width[0],
166  frame[2] + width[1],
167  frame[3] - width[1]);
168 
169  // And scale the bar according to our value.
170  PN_stdfloat frac = _value / _range;
171  frac = max(min(frac, (PN_stdfloat)1.0), (PN_stdfloat)0.0);
172  bar_frame[1] = bar_frame[0] + frac * (bar_frame[1] - bar_frame[0]);
173 
174  _bar = _bar_style.generate_into(root, bar_frame, 1);
175  }
176 
177  // Indicate that the bar is current for this state.
178  _bar_state = state;
179 }
const LVecBase4 & get_frame() const
Returns the bounding rectangle of the item.
Definition: pgItem.I:131
A basic node of the scene graph or data graph.
Definition: pandaNode.h:72
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:81
void set_width(PN_stdfloat x, PN_stdfloat y)
Sets the width parameter, which has meaning only for certain frame types.
Definition: pgFrameStyle.I:172
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:1087
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:100
This is the base class for all the various kinds of gui widget objects.
Definition: pgItem.h:58
NodePath & get_state_def(int state)
Returns the Node that is the root of the subgraph that will be drawn when the PGItem is in the indica...
Definition: pgItem.cxx:1043
void set_type(Type type)
Sets the basic type of frame.
Definition: pgFrameStyle.I:76
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:100
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:113
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:1102
int get_state() const
Returns the "state" of this particular PGItem.
Definition: pgItem.I:187
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:96
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:29
This is the base class for all two-component vectors and points.
Definition: lvecBase2.h:105
Similar to MutexHolder, but for a light reentrant mutex.
This is the base class for all three-component vectors and points.
Definition: lvecBase4.h:111
void set_state(int state)
Sets the "state" of this particular PGItem.
Definition: pgItem.I:175
void remove_node(Thread *current_thread=Thread::get_current_thread())
Disconnects the referenced node from the scene graph.
Definition: nodePath.cxx:757
bool is_empty() const
Returns true if the NodePath contains no nodes.
Definition: nodePath.I:236
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:1021
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
virtual PandaNode * make_copy() const
Returns a newly-allocated Node that is a shallow copy of this one.
Definition: pgWaitBar.cxx:69
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:165
This object performs a depth-first traversal of the scene graph, with optional view-frustum culling...
Definition: cullTraverser.h:48