diff --git a/panda/src/pgui/Sources.pp b/panda/src/pgui/Sources.pp index 64fe4e4df1..0135dce723 100644 --- a/panda/src/pgui/Sources.pp +++ b/panda/src/pgui/Sources.pp @@ -12,6 +12,7 @@ #define SOURCES \ config_pgui.h \ pgButton.I pgButton.h \ + pgButtonEvent.I pgButtonEvent.h \ pgFrameStyle.I pgFrameStyle.h \ pgItem.I pgItem.h \ pgMouseWatcherRegion.I pgMouseWatcherRegion.h \ @@ -21,6 +22,7 @@ #define SOURCES $[SOURCES] \ config_pgui.cxx \ pgButton.cxx \ + pgButtonEvent.cxx \ pgFrameStyle.cxx \ pgItem.cxx \ pgMouseWatcherRegion.cxx \ @@ -28,6 +30,7 @@ #define INSTALL_HEADERS \ pgButton.I pgButton.h \ + pgButtonEvent.I pgButtonEvent.h \ pgFrameStyle.I pgFrameStyle.h \ pgItem.I pgItem.h \ pgMouseWatcherRegion.I pgMouseWatcherRegion.h \ diff --git a/panda/src/pgui/config_pgui.cxx b/panda/src/pgui/config_pgui.cxx index da8f83fd08..a18b37f148 100644 --- a/panda/src/pgui/config_pgui.cxx +++ b/panda/src/pgui/config_pgui.cxx @@ -18,6 +18,7 @@ #include "config_pgui.h" #include "pgButton.h" +#include "pgButtonEvent.h" #include "pgItem.h" #include "pgMouseWatcherRegion.h" #include "pgTop.h" @@ -29,6 +30,7 @@ NotifyCategoryDef(pgui, ""); ConfigureFn(config_pgui) { PGButton::init_type(); + PGButtonEvent::init_type(); PGItem::init_type(); PGMouseWatcherRegion::init_type(); PGTop::init_type(); diff --git a/panda/src/pgui/pgButton.cxx b/panda/src/pgui/pgButton.cxx index 0e1dbc211c..c46fb9eba1 100644 --- a/panda/src/pgui/pgButton.cxx +++ b/panda/src/pgui/pgButton.cxx @@ -20,6 +20,8 @@ #include "throw_event.h" #include "renderRelation.h" #include "colorTransition.h" +#include "transformTransition.h" +#include "mouseButton.h" TypeHandle PGButton::_type_handle; @@ -114,12 +116,16 @@ exit() { // is within the region. //////////////////////////////////////////////////////////////////// void PGButton:: -button_down(ButtonHandle button) { - if (get_active()) { - _button_down = true; - set_state(S_depressed); +button_down(ButtonHandle button, float x, float y) { + if (button == MouseButton::one() || + button == MouseButton::two() || + button == MouseButton::three()) { + if (get_active()) { + _button_down = true; + set_state(S_depressed); + } } - PGItem::button_down(button); + PGItem::button_down(button, x, y); } //////////////////////////////////////////////////////////////////// @@ -133,17 +139,21 @@ button_down(ButtonHandle button) { // outside the region. //////////////////////////////////////////////////////////////////// void PGButton:: -button_up(ButtonHandle button, bool is_within) { - _button_down = false; - if (get_active()) { - if (is_within) { - set_state(S_rollover); - click(); - } else { - set_state(S_ready); +button_up(ButtonHandle button, float x, float y, bool is_within) { + if (button == MouseButton::one() || + button == MouseButton::two() || + button == MouseButton::three()) { + _button_down = false; + if (get_active()) { + if (is_within) { + set_state(S_rollover); + click(); + } else { + set_state(S_ready); + } } } - PGItem::button_up(button, is_within); + PGItem::button_up(button, x, y, is_within); } //////////////////////////////////////////////////////////////////// @@ -168,6 +178,11 @@ click() { //////////////////////////////////////////////////////////////////// void PGButton:: setup(const string &label) { + clear_state_def(S_ready); + clear_state_def(S_depressed); + clear_state_def(S_rollover); + clear_state_def(S_inactive); + TextNode *text_node = get_text_node(); text_node->set_text(label); PT_Node geom = text_node->generate(); @@ -176,7 +191,7 @@ setup(const string &label) { set_frame(frame[0] - 0.4, frame[1] + 0.4, frame[2] - 0.15, frame[3] + 0.15); new RenderRelation(get_state_def(S_ready), geom); - new RenderRelation(get_state_def(S_depressed), geom); + NodeRelation *down = new RenderRelation(get_state_def(S_depressed), geom); new RenderRelation(get_state_def(S_rollover), geom); NodeRelation *inact = new RenderRelation(get_state_def(S_inactive), geom); @@ -197,8 +212,10 @@ setup(const string &label) { style.set_type(PGFrameStyle::T_bevel_in); style.set_color(0.8, 0.8, 0.8, 1.0); - style.set_width(0.05, 0.05); set_frame_style(S_depressed, style); + LMatrix4f translate = LMatrix4f::translate_mat(0.05, 0.0, -0.05); + TransformTransition *ttrans = new TransformTransition(translate); + down->set_transition(ttrans); } //////////////////////////////////////////////////////////////////// @@ -210,6 +227,11 @@ setup(const string &label) { void PGButton:: setup(const ArcChain &ready, const ArcChain &depressed, const ArcChain &rollover, const ArcChain &inactive) { + clear_state_def(S_ready); + clear_state_def(S_depressed); + clear_state_def(S_rollover); + clear_state_def(S_inactive); + instance_to_state_def(S_ready, ready); instance_to_state_def(S_depressed, depressed); instance_to_state_def(S_rollover, rollover); diff --git a/panda/src/pgui/pgButton.h b/panda/src/pgui/pgButton.h index d7bb3bf8f1..6ba6cc3a61 100644 --- a/panda/src/pgui/pgButton.h +++ b/panda/src/pgui/pgButton.h @@ -44,8 +44,8 @@ public: virtual void enter(); virtual void exit(); - virtual void button_down(ButtonHandle button); - virtual void button_up(ButtonHandle button, bool is_within); + virtual void button_down(ButtonHandle button, float x, float y); + virtual void button_up(ButtonHandle button, float x, float y, bool is_within); virtual void click(); diff --git a/panda/src/pgui/pgButtonEvent.I b/panda/src/pgui/pgButtonEvent.I new file mode 100644 index 0000000000..9aea5636f8 --- /dev/null +++ b/panda/src/pgui/pgButtonEvent.I @@ -0,0 +1,94 @@ +// Filename: pgButtonEvent.I +// Created by: drose (05Jul01) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://www.panda3d.org/license.txt . +// +// To contact the maintainers of this program write to +// panda3d@yahoogroups.com . +// +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +// Function: PGButtonEvent::Constructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE PGButtonEvent:: +PGButtonEvent() { +} + +//////////////////////////////////////////////////////////////////// +// Function: PGButtonEvent::Constructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE PGButtonEvent:: +PGButtonEvent(ButtonHandle button, float mouse_x, float mouse_y) : + _button(button), + _mouse_x(mouse_x), + _mouse_y(mouse_y) +{ +} + +//////////////////////////////////////////////////////////////////// +// Function: PGButtonEvent::get_button +// Access: Published +// Description: Returns the ButtonHandle of the button involved with +// this event. +//////////////////////////////////////////////////////////////////// +INLINE ButtonHandle PGButtonEvent:: +get_button() const { + return _button; +} + +//////////////////////////////////////////////////////////////////// +// Function: PGButtonEvent::get_button_name +// Access: Published +// Description: Returns the name of the button involved with this +// event. This is just a shorthand way to get to the +// name, since get_button().get_name() will return the +// same thing. +//////////////////////////////////////////////////////////////////// +INLINE string PGButtonEvent:: +get_button_name() const { + return _button.get_name(); +} + +//////////////////////////////////////////////////////////////////// +// Function: PGButtonEvent::get_mouse_x +// Access: Published +// Description: Returns the X position of the mouse within the window +// at the time the button event occurred. This is in +// the normalized range [-1 .. 1]. +//////////////////////////////////////////////////////////////////// +INLINE float PGButtonEvent:: +get_mouse_x() const { + return _mouse_x; +} + +//////////////////////////////////////////////////////////////////// +// Function: PGButtonEvent::get_mouse_y +// Access: Published +// Description: Returns the Y position of the mouse within the window +// at the time the button event occurred. This is in +// the normalized range [-1 .. 1]. +//////////////////////////////////////////////////////////////////// +INLINE float PGButtonEvent:: +get_mouse_y() const { + return _mouse_y; +} + +INLINE ostream & +operator << (ostream &out, const PGButtonEvent &event) { + event.output(out); + return out; +} diff --git a/panda/src/pgui/pgButtonEvent.cxx b/panda/src/pgui/pgButtonEvent.cxx new file mode 100644 index 0000000000..ceef395de1 --- /dev/null +++ b/panda/src/pgui/pgButtonEvent.cxx @@ -0,0 +1,40 @@ +// Filename: pgButtonEvent.cxx +// Created by: drose (05Jul01) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://www.panda3d.org/license.txt . +// +// To contact the maintainers of this program write to +// panda3d@yahoogroups.com . +// +//////////////////////////////////////////////////////////////////// + +#include "pgButtonEvent.h" + +TypeHandle PGButtonEvent::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: PGButtonEvent::Destructor +// Access: Public, Virtual +// Description: +//////////////////////////////////////////////////////////////////// +PGButtonEvent:: +~PGButtonEvent() { +} + +//////////////////////////////////////////////////////////////////// +// Function: PGButtonEvent::output +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void PGButtonEvent:: +output(ostream &out) const { + out << _button << " at (" << _mouse_x << ", " << _mouse_y << ")"; +} diff --git a/panda/src/pgui/pgButtonEvent.h b/panda/src/pgui/pgButtonEvent.h new file mode 100644 index 0000000000..f480c1ddad --- /dev/null +++ b/panda/src/pgui/pgButtonEvent.h @@ -0,0 +1,76 @@ +// Filename: pgButtonEvent.h +// Created by: drose (05Jul01) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://www.panda3d.org/license.txt . +// +// To contact the maintainers of this program write to +// panda3d@yahoogroups.com . +// +//////////////////////////////////////////////////////////////////// + +#ifndef PGBUTTONEVENT_H +#define PGBUTTONEVENT_H + +#include "pandabase.h" + +#include "typedReferenceCount.h" +#include "buttonHandle.h" + +//////////////////////////////////////////////////////////////////// +// Class : PGButtonEvent +// Description : This is sent along as a parameter to a button_down or +// button_up event for an item to indicate which button +// was involved, and what the current mouse position +// was. +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDA PGButtonEvent : public TypedReferenceCount { +public: + INLINE PGButtonEvent(); + INLINE PGButtonEvent(ButtonHandle button, float mouse_x, float mouse_y); + virtual ~PGButtonEvent(); + +PUBLISHED: + INLINE ButtonHandle get_button() const; + INLINE string get_button_name() const; + + INLINE float get_mouse_x() const; + INLINE float get_mouse_y() const; + + void output(ostream &out) const; + +public: + ButtonHandle _button; + float _mouse_x; + float _mouse_y; + +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + TypedReferenceCount::init_type(); + register_type(_type_handle, "PGButtonEvent", + TypedReferenceCount::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() {init_type(); return get_class_type();} + +private: + static TypeHandle _type_handle; +}; + +INLINE ostream &operator << (ostream &out, const PGButtonEvent &event); + +#include "pgButtonEvent.I" + +#endif diff --git a/panda/src/pgui/pgItem.I b/panda/src/pgui/pgItem.I index 6dc7812834..7c17a60c3a 100644 --- a/panda/src/pgui/pgItem.I +++ b/panda/src/pgui/pgItem.I @@ -193,8 +193,8 @@ get_exit_event() const { // depressed while the mouse is within the frame. // // This event will be thrown with one parameter, which -// will be the name of the button that generated the -// event. +// will be the PGButtonEvent for the button that +// generated the event. //////////////////////////////////////////////////////////////////// INLINE string PGItem:: get_button_down_event() const { @@ -210,10 +210,11 @@ get_button_down_event() const { // released. // // This event will be thrown with two parameters, the -// first of which will be the name of the button that -// generated the event, and the second of which will be -// a boolean flag indicating true if the mouse was -// within the frame while the button was released. +// first of which will be the PGButtonEvent for the +// button that generated the event, and the second of +// which will be a boolean flag indicating true if the +// mouse was within the frame while the button was +// released. //////////////////////////////////////////////////////////////////// INLINE string PGItem:: get_button_up_event() const { diff --git a/panda/src/pgui/pgItem.cxx b/panda/src/pgui/pgItem.cxx index e5243f3cf7..e600e27a55 100644 --- a/panda/src/pgui/pgItem.cxx +++ b/panda/src/pgui/pgItem.cxx @@ -17,6 +17,7 @@ //////////////////////////////////////////////////////////////////// #include "pgItem.h" +#include "pgButtonEvent.h" #include "namedNode.h" #include "throw_event.h" @@ -142,8 +143,10 @@ exit() { // is within the region. //////////////////////////////////////////////////////////////////// void PGItem:: -button_down(ButtonHandle button) { - throw_event(get_button_down_event(), button.get_name()); +button_down(ButtonHandle button, float x, float y) { + PGButtonEvent *be = new PGButtonEvent(button, x, y); + throw_event(get_button_down_event(), + EventParameter(be)); } //////////////////////////////////////////////////////////////////// @@ -157,9 +160,10 @@ button_down(ButtonHandle button) { // outside the region. //////////////////////////////////////////////////////////////////// void PGItem:: -button_up(ButtonHandle button, bool is_within) { +button_up(ButtonHandle button, float x, float y, bool is_within) { + PGButtonEvent *be = new PGButtonEvent(button, x, y); throw_event(get_button_up_event(), - EventParameter(button.get_name()), + EventParameter(be), EventParameter(is_within)); } diff --git a/panda/src/pgui/pgItem.h b/panda/src/pgui/pgItem.h index 9c112d2a45..dd35c0cea8 100644 --- a/panda/src/pgui/pgItem.h +++ b/panda/src/pgui/pgItem.h @@ -64,8 +64,8 @@ public: virtual void enter(); virtual void exit(); - virtual void button_down(ButtonHandle button); - virtual void button_up(ButtonHandle button, bool is_within); + virtual void button_down(ButtonHandle button, float x, float y); + virtual void button_up(ButtonHandle button, float x, float y, bool is_within); PUBLISHED: INLINE void set_frame(float left, float right, float bottom, float top); diff --git a/panda/src/pgui/pgMouseWatcherRegion.cxx b/panda/src/pgui/pgMouseWatcherRegion.cxx index 6648246102..300d6c56df 100644 --- a/panda/src/pgui/pgMouseWatcherRegion.cxx +++ b/panda/src/pgui/pgMouseWatcherRegion.cxx @@ -82,9 +82,9 @@ exit() { // is within the region. //////////////////////////////////////////////////////////////////// void PGMouseWatcherRegion:: -button_down(ButtonHandle button) { +button_down(ButtonHandle button, float x, float y) { if (_item != (PGItem *)NULL) { - _item->button_down(button); + _item->button_down(button, x, y); } } @@ -99,8 +99,8 @@ button_down(ButtonHandle button) { // outside the region. //////////////////////////////////////////////////////////////////// void PGMouseWatcherRegion:: -button_up(ButtonHandle button, bool is_within) { +button_up(ButtonHandle button, float x, float y, bool is_within) { if (_item != (PGItem *)NULL) { - _item->button_up(button, is_within); + _item->button_up(button, x, y, is_within); } } diff --git a/panda/src/pgui/pgMouseWatcherRegion.h b/panda/src/pgui/pgMouseWatcherRegion.h index 1c71563066..3b546fed3a 100644 --- a/panda/src/pgui/pgMouseWatcherRegion.h +++ b/panda/src/pgui/pgMouseWatcherRegion.h @@ -39,8 +39,8 @@ public: virtual void enter(); virtual void exit(); - virtual void button_down(ButtonHandle button); - virtual void button_up(ButtonHandle button, bool is_within); + virtual void button_down(ButtonHandle button, float x, float y); + virtual void button_up(ButtonHandle button, float x, float y, bool is_within); private: PGItem *_item; diff --git a/panda/src/putil/buttonHandle.I b/panda/src/putil/buttonHandle.I index a59e75ba38..d5b1b6986e 100644 --- a/panda/src/putil/buttonHandle.I +++ b/panda/src/putil/buttonHandle.I @@ -92,6 +92,22 @@ get_ascii_equivalent() const { return has_ascii_equivalent() ? (char)_index : '\0'; } +//////////////////////////////////////////////////////////////////// +// Function: ButtonHandle::get_index +// Access: Public +// Description: Returns the integer index associated with this +// ButtonHandle. Each different ButtonHandle will have a +// different index. However, you probably shouldn't be +// using this method; you should just treat the +// ButtonHandles as opaque classes. This is provided +// for the convenience of non-C++ scripting languages to +// build a hashtable of ButtonHandles. +//////////////////////////////////////////////////////////////////// +INLINE int ButtonHandle:: +get_index() const { + return _index; +} + //////////////////////////////////////////////////////////////////// // Function: ButtonHandle::none // Access: Public, Static diff --git a/panda/src/putil/buttonHandle.h b/panda/src/putil/buttonHandle.h index c45f7c35a6..77bc5597bd 100644 --- a/panda/src/putil/buttonHandle.h +++ b/panda/src/putil/buttonHandle.h @@ -43,6 +43,7 @@ PUBLISHED: INLINE bool has_ascii_equivalent() const; INLINE char get_ascii_equivalent() const; + INLINE int get_index() const; INLINE static ButtonHandle none(); private: diff --git a/panda/src/tform/mouseWatcher.cxx b/panda/src/tform/mouseWatcher.cxx index 1636a59519..42280e1018 100644 --- a/panda/src/tform/mouseWatcher.cxx +++ b/panda/src/tform/mouseWatcher.cxx @@ -359,7 +359,7 @@ transmit_data(NodeAttributes &data) { // both of the button-up events properly. if (_button_down_region != (MouseWatcherRegion *)NULL) { bool is_within = (_current_region == _button_down_region); - _button_down_region->button_up(be._button, is_within); + _button_down_region->button_up(be._button, get_mouse_x(), get_mouse_y(), is_within); throw_event_pattern(_button_up_pattern, _button_down_region, be._button.get_name()); } @@ -373,7 +373,7 @@ transmit_data(NodeAttributes &data) { } _button_down = true; if (_button_down_region != (MouseWatcherRegion *)NULL) { - _button_down_region->button_down(be._button); + _button_down_region->button_down(be._button, get_mouse_x(), get_mouse_y()); throw_event_pattern(_button_down_pattern, _button_down_region, be._button.get_name()); } diff --git a/panda/src/tform/mouseWatcherRegion.cxx b/panda/src/tform/mouseWatcherRegion.cxx index 2823620106..242b4c5948 100644 --- a/panda/src/tform/mouseWatcherRegion.cxx +++ b/panda/src/tform/mouseWatcherRegion.cxx @@ -71,7 +71,7 @@ exit() { // is within the region. //////////////////////////////////////////////////////////////////// void MouseWatcherRegion:: -button_down(ButtonHandle) { +button_down(ButtonHandle, float, float) { } //////////////////////////////////////////////////////////////////// @@ -85,5 +85,5 @@ button_down(ButtonHandle) { // outside the region. //////////////////////////////////////////////////////////////////// void MouseWatcherRegion:: -button_up(ButtonHandle, bool) { +button_up(ButtonHandle, float, float, bool) { } diff --git a/panda/src/tform/mouseWatcherRegion.h b/panda/src/tform/mouseWatcherRegion.h index 72c7a963c0..6e3f745f87 100644 --- a/panda/src/tform/mouseWatcherRegion.h +++ b/panda/src/tform/mouseWatcherRegion.h @@ -59,8 +59,8 @@ public: virtual void enter(); virtual void exit(); - virtual void button_down(ButtonHandle button); - virtual void button_up(ButtonHandle button, bool is_within); + virtual void button_down(ButtonHandle button, float x, float y); + virtual void button_up(ButtonHandle button, float x, float y, bool is_within); private: LVecBase4f _frame;