Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/pgui/pgButton.cxx

Go to the documentation of this file.
00001 // Filename: pgButton.cxx
00002 // Created by:  drose (13Mar02)
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 "pgButton.h"
00020 #include "pgMouseWatcherParameter.h"
00021 
00022 #include "throw_event.h"
00023 #include "mouseButton.h"
00024 #include "mouseWatcherParameter.h"
00025 #include "colorAttrib.h"
00026 #include "transformState.h"
00027 
00028 TypeHandle PGButton::_type_handle;
00029 
00030 ////////////////////////////////////////////////////////////////////
00031 //     Function: PGButton::Constructor
00032 //       Access: Published
00033 //  Description: 
00034 ////////////////////////////////////////////////////////////////////
00035 PGButton::
00036 PGButton(const string &name) : PGItem(name)
00037 {
00038   _button_down = false;
00039   _click_buttons.insert(MouseButton::one());
00040 
00041   set_active(true);
00042 }
00043 
00044 ////////////////////////////////////////////////////////////////////
00045 //     Function: PGButton::Destructor
00046 //       Access: Public, Virtual
00047 //  Description: 
00048 ////////////////////////////////////////////////////////////////////
00049 PGButton::
00050 ~PGButton() {
00051 }
00052 
00053 ////////////////////////////////////////////////////////////////////
00054 //     Function: PGButton::Copy Constructor
00055 //       Access: Public
00056 //  Description: 
00057 ////////////////////////////////////////////////////////////////////
00058 PGButton::
00059 PGButton(const PGButton &copy) :
00060   PGItem(copy)
00061 {
00062   _button_down = false;
00063 }
00064 
00065 ////////////////////////////////////////////////////////////////////
00066 //     Function: PGButton::make_copy
00067 //       Access: Public, Virtual
00068 //  Description: Returns a newly-allocated Node that is a shallow copy
00069 //               of this one.  It will be a different Node pointer,
00070 //               but its internal data may or may not be shared with
00071 //               that of the original Node.
00072 ////////////////////////////////////////////////////////////////////
00073 PandaNode *PGButton::
00074 make_copy() const {
00075   return new PGButton(*this);
00076 }
00077 
00078 ////////////////////////////////////////////////////////////////////
00079 //     Function: PGButton::enter
00080 //       Access: Public, Virtual
00081 //  Description: This is a callback hook function, called whenever the
00082 //               mouse enters the region.
00083 ////////////////////////////////////////////////////////////////////
00084 void PGButton::
00085 enter(const MouseWatcherParameter &param) {
00086   if (get_active()) {
00087     set_state(_button_down ? S_depressed : S_rollover);
00088   }
00089   PGItem::enter(param);
00090 }
00091 
00092 ////////////////////////////////////////////////////////////////////
00093 //     Function: PGButton::exit
00094 //       Access: Public, Virtual
00095 //  Description: This is a callback hook function, called whenever the
00096 //               mouse exits the region.
00097 ////////////////////////////////////////////////////////////////////
00098 void PGButton::
00099 exit(const MouseWatcherParameter &param) {
00100   if (get_active()) {
00101     set_state(S_ready);
00102   }
00103   PGItem::exit(param);
00104 }
00105 
00106 ////////////////////////////////////////////////////////////////////
00107 //     Function: PGButton::press
00108 //       Access: Public, Virtual
00109 //  Description: This is a callback hook function, called whenever a
00110 //               mouse or keyboard button is depressed while the mouse
00111 //               is within the region.
00112 ////////////////////////////////////////////////////////////////////
00113 void PGButton::
00114 press(const MouseWatcherParameter &param, bool background) {
00115   if (has_click_button(param.get_button())) {
00116     if (get_active()) {
00117       _button_down = true;
00118       set_state(S_depressed);
00119     }
00120   }
00121   PGItem::press(param, background);
00122 }
00123 
00124 ////////////////////////////////////////////////////////////////////
00125 //     Function: PGButton::release
00126 //       Access: Public, Virtual
00127 //  Description: This is a callback hook function, called whenever a
00128 //               mouse or keyboard button previously depressed with
00129 //               press() is released.
00130 ////////////////////////////////////////////////////////////////////
00131 void PGButton::
00132 release(const MouseWatcherParameter &param, bool background) {
00133   if (has_click_button(param.get_button())) {
00134     _button_down = false;
00135     if (get_active()) {
00136       if (param.is_outside()) {
00137         set_state(S_ready);
00138       } else {
00139         set_state(S_rollover);
00140         click(param);
00141       }
00142     }
00143   }
00144   PGItem::release(param, background);
00145 }
00146 
00147 ////////////////////////////////////////////////////////////////////
00148 //     Function: PGButton::click
00149 //       Access: Public, Virtual
00150 //  Description: This is a callback hook function, called whenever the
00151 //               button is clicked down-and-up by the user normally.
00152 ////////////////////////////////////////////////////////////////////
00153 void PGButton::
00154 click(const MouseWatcherParameter &param) {
00155   PGMouseWatcherParameter *ep = new PGMouseWatcherParameter(param);
00156   string event = get_click_event(param.get_button());
00157   play_sound(event);
00158   throw_event(event, EventParameter(ep));
00159 }
00160 
00161 ////////////////////////////////////////////////////////////////////
00162 //     Function: PGButton::setup
00163 //       Access: Published
00164 //  Description: Sets up the button as a default text button using the
00165 //               indicated label string.  The TextNode defined by
00166 //               PGItem::get_text_node() will be used to create the
00167 //               label geometry.  This automatically sets up the frame
00168 //               according to the size of the text.
00169 ////////////////////////////////////////////////////////////////////
00170 void PGButton::
00171 setup(const string &label) {
00172   clear_state_def(S_ready);
00173   clear_state_def(S_depressed);
00174   clear_state_def(S_rollover);
00175   clear_state_def(S_inactive);
00176 
00177   TextNode *text_node = get_text_node();
00178   text_node->set_text(label);
00179   PT(PandaNode) geom = text_node->generate();
00180 
00181   LVecBase4f frame = text_node->get_card_actual();
00182   set_frame(frame[0] - 0.4f, frame[1] + 0.4f, frame[2] - 0.15f, frame[3] + 0.15f);
00183 
00184   PT(PandaNode) ready = new PandaNode("ready");
00185   PT(PandaNode) depressed = new PandaNode("depressed");
00186   PT(PandaNode) rollover = new PandaNode("rollover");
00187   PT(PandaNode) inactive = new PandaNode("inactive");
00188 
00189   get_state_def(S_ready).attach_new_node(ready);
00190   get_state_def(S_depressed).attach_new_node(depressed);
00191   get_state_def(S_rollover).attach_new_node(rollover);
00192   get_state_def(S_inactive).attach_new_node(inactive);
00193 
00194   ready->add_child(geom);
00195   depressed->add_child(geom);
00196   rollover->add_child(geom);
00197   inactive->add_child(geom);
00198 
00199   PGFrameStyle style;
00200   style.set_color(0.8f, 0.8f, 0.8f, 1.0f);
00201   style.set_width(0.1f, 0.1f);
00202 
00203   style.set_type(PGFrameStyle::T_bevel_out);
00204   set_frame_style(S_ready, style);
00205 
00206   style.set_color(0.9f, 0.9f, 0.9f, 1.0f);
00207   set_frame_style(S_rollover, style);
00208 
00209   inactive->set_attrib(ColorAttrib::make_flat(Colorf(0.8f, 0.8f, 0.8f, 1.0f)));
00210   style.set_color(0.6f, 0.6f, 0.6f, 1.0f);
00211   set_frame_style(S_inactive, style);
00212 
00213   style.set_type(PGFrameStyle::T_bevel_in);
00214   style.set_color(0.8f, 0.8f, 0.8f, 1.0f);
00215   set_frame_style(S_depressed, style);
00216   depressed->set_transform(TransformState::make_pos(LVector3f(0.05f, 0.0f, -0.05f)));
00217 }
00218 
00219 ////////////////////////////////////////////////////////////////////
00220 //     Function: PGButton::setup
00221 //       Access: Published
00222 //  Description: Sets up the button using the indicated NodePath as
00223 //               arbitrary geometry.
00224 ////////////////////////////////////////////////////////////////////
00225 void PGButton::
00226 setup(const NodePath &ready, const NodePath &depressed, 
00227       const NodePath &rollover, const NodePath &inactive) {
00228   clear_state_def(S_ready);
00229   clear_state_def(S_depressed);
00230   clear_state_def(S_rollover);
00231   clear_state_def(S_inactive);
00232 
00233   instance_to_state_def(S_ready, ready);
00234   instance_to_state_def(S_depressed, depressed);
00235   instance_to_state_def(S_rollover, rollover);
00236   instance_to_state_def(S_inactive, inactive);
00237 }
00238 
00239 ////////////////////////////////////////////////////////////////////
00240 //     Function: PGButton::set_active
00241 //       Access: Published, Virtual
00242 //  Description: Toggles the active/inactive state of the button.  In
00243 //               the case of a PGButton, this also changes its visual
00244 //               appearance.
00245 ////////////////////////////////////////////////////////////////////
00246 void PGButton:: 
00247 set_active(bool active) {
00248   if (active != get_active()) {
00249     PGItem::set_active(active);
00250     set_state(active ? S_ready : S_inactive);
00251   }
00252 }
00253 
00254 ////////////////////////////////////////////////////////////////////
00255 //     Function: PGButton::add_click_button
00256 //       Access: Published
00257 //  Description: Adds the indicated button to the set of buttons that
00258 //               can effectively "click" the PGButton.  Normally, this
00259 //               is just MouseButton::one().  Returns true if the
00260 //               button was added, or false if it was already there.
00261 ////////////////////////////////////////////////////////////////////
00262 bool PGButton::
00263 add_click_button(const ButtonHandle &button) {
00264   return _click_buttons.insert(button).second;
00265 }
00266 
00267 ////////////////////////////////////////////////////////////////////
00268 //     Function: PGButton::remove_click_button
00269 //       Access: Published
00270 //  Description: Removes the indicated button from the set of buttons
00271 //               that can effectively "click" the PGButton.  Normally,
00272 //               this is just MouseButton::one().  Returns true if the
00273 //               button was removed, or false if it was not in the
00274 //               set.
00275 ////////////////////////////////////////////////////////////////////
00276 bool PGButton::
00277 remove_click_button(const ButtonHandle &button) {
00278   return (_click_buttons.erase(button) != 0);
00279 }
00280 
00281 ////////////////////////////////////////////////////////////////////
00282 //     Function: PGButton::has_click_button
00283 //       Access: Published
00284 //  Description: Returns true if the indicated button is on the set of
00285 //               buttons that can effectively "click" the PGButton.
00286 //               Normally, this is just MouseButton::one().
00287 ////////////////////////////////////////////////////////////////////
00288 bool PGButton::
00289 has_click_button(const ButtonHandle &button) {
00290   return (_click_buttons.count(button) != 0);
00291 }

Generated on Fri May 2 00:42:35 2003 for Panda by doxygen1.3