00001 // Filename: pgWaitBar.cxx 00002 // Created by: drose (14Mar02) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved 00008 // 00009 // All use of this software is subject to the terms of the Panda 3d 00010 // Software license. You should have received a copy of this license 00011 // along with this source code; you will also find a current copy of 00012 // the license at http://www.panda3d.org/license.txt . 00013 // 00014 // To contact the maintainers of this program write to 00015 // panda3d@yahoogroups.com . 00016 // 00017 //////////////////////////////////////////////////////////////////// 00018 00019 #include "pgWaitBar.h" 00020 #include "pgMouseWatcherParameter.h" 00021 00022 #include "throw_event.h" 00023 00024 TypeHandle PGWaitBar::_type_handle; 00025 00026 //////////////////////////////////////////////////////////////////// 00027 // Function: PGWaitBar::Constructor 00028 // Access: Published 00029 // Description: 00030 //////////////////////////////////////////////////////////////////// 00031 PGWaitBar:: 00032 PGWaitBar(const string &name) : PGItem(name) 00033 { 00034 _range = 100.0; 00035 _value = 0.0; 00036 _bar_state = -1; 00037 } 00038 00039 //////////////////////////////////////////////////////////////////// 00040 // Function: PGWaitBar::Destructor 00041 // Access: Public, Virtual 00042 // Description: 00043 //////////////////////////////////////////////////////////////////// 00044 PGWaitBar:: 00045 ~PGWaitBar() { 00046 } 00047 00048 //////////////////////////////////////////////////////////////////// 00049 // Function: PGWaitBar::Copy Constructor 00050 // Access: Protected 00051 // Description: 00052 //////////////////////////////////////////////////////////////////// 00053 PGWaitBar:: 00054 PGWaitBar(const PGWaitBar ©) : 00055 PGItem(copy), 00056 _range(copy._range), 00057 _value(copy._value) 00058 { 00059 _bar_state = -1; 00060 } 00061 00062 //////////////////////////////////////////////////////////////////// 00063 // Function: PGWaitBar::make_copy 00064 // Access: Public, Virtual 00065 // Description: Returns a newly-allocated Node that is a shallow copy 00066 // of this one. It will be a different Node pointer, 00067 // but its internal data may or may not be shared with 00068 // that of the original Node. 00069 //////////////////////////////////////////////////////////////////// 00070 PandaNode *PGWaitBar:: 00071 make_copy() const { 00072 return new PGWaitBar(*this); 00073 } 00074 00075 //////////////////////////////////////////////////////////////////// 00076 // Function: PGWaitBar::has_cull_callback 00077 // Access: Protected, Virtual 00078 // Description: Should be overridden by derived classes to return 00079 // true if cull_callback() has been defined. Otherwise, 00080 // returns false to indicate cull_callback() does not 00081 // need to be called for this node during the cull 00082 // traversal. 00083 //////////////////////////////////////////////////////////////////// 00084 bool PGWaitBar:: 00085 has_cull_callback() const { 00086 return true; 00087 } 00088 00089 //////////////////////////////////////////////////////////////////// 00090 // Function: PGWaitBar::cull_callback 00091 // Access: Protected, Virtual 00092 // Description: If has_cull_callback() returns true, this function 00093 // will be called during the cull traversal to perform 00094 // any additional operations that should be performed at 00095 // cull time. This may include additional manipulation 00096 // of render state or additional visible/invisible 00097 // decisions, or any other arbitrary operation. 00098 // 00099 // By the time this function is called, the node has 00100 // already passed the bounding-volume test for the 00101 // viewing frustum, and the node's transform and state 00102 // have already been applied to the indicated 00103 // CullTraverserData object. 00104 // 00105 // The return value is true if this node should be 00106 // visible, or false if it should be culled. 00107 //////////////////////////////////////////////////////////////////// 00108 bool PGWaitBar:: 00109 cull_callback(CullTraverser *trav, CullTraverserData &data) { 00110 update(); 00111 return PGItem::cull_callback(trav, data); 00112 } 00113 00114 //////////////////////////////////////////////////////////////////// 00115 // Function: PGWaitBar::setup 00116 // Access: Public 00117 // Description: Creates a PGWaitBar with the indicated dimensions, 00118 // with the indicated maximum range. 00119 //////////////////////////////////////////////////////////////////// 00120 void PGWaitBar:: 00121 setup(float width, float height, float range) { 00122 set_state(0); 00123 clear_state_def(0); 00124 00125 set_frame(-0.5f * width, 0.5f * width, -0.5f * height, 0.5f * height); 00126 00127 PGFrameStyle style; 00128 style.set_width(0.05f, 0.05f); 00129 00130 style.set_color(0.6f, 0.6f, 0.6f, 1.0f); 00131 style.set_type(PGFrameStyle::T_bevel_in); 00132 set_frame_style(0, style); 00133 00134 style.set_color(0.8f, 0.8f, 0.8f, 1.0f); 00135 style.set_type(PGFrameStyle::T_bevel_out); 00136 set_bar_style(style); 00137 } 00138 00139 //////////////////////////////////////////////////////////////////// 00140 // Function: PGWaitBar::update 00141 // Access: Private 00142 // Description: Computes the appropriate size of the bar frame 00143 // according to the percentage completed. 00144 //////////////////////////////////////////////////////////////////// 00145 void PGWaitBar:: 00146 update() { 00147 int state = get_state(); 00148 00149 // If the bar was last drawn in this state and is still current, we 00150 // don't have to draw it again. 00151 if (_bar_state == state) { 00152 return; 00153 } 00154 00155 // Remove the old bar geometry, if any. 00156 _bar.remove_node(); 00157 00158 // Now create new bar geometry. 00159 if ((_value != 0.0f) && (_range != 0.0f)) { 00160 NodePath &root = get_state_def(state); 00161 nassertv(!root.is_empty()); 00162 00163 PGFrameStyle style = get_frame_style(state); 00164 const LVecBase4f &frame = get_frame(); 00165 const LVecBase2f &width = style.get_width(); 00166 00167 // Put the bar within the item's frame's border. 00168 LVecBase4f bar_frame(frame[0] + width[0], 00169 frame[1] - width[0], 00170 frame[2] + width[1], 00171 frame[3] - width[1]); 00172 00173 // And scale the bar according to our value. 00174 float frac = _value / _range; 00175 frac = max(min(frac, 1.0f), 0.0f); 00176 bar_frame[1] = bar_frame[0] + frac * (bar_frame[1] - bar_frame[0]); 00177 00178 _bar = _bar_style.generate_into(root, bar_frame); 00179 } 00180 00181 // Indicate that the bar is current for this state. 00182 _bar_state = state; 00183 }