Panda3D
 All Classes Functions Variables Enumerations
pgItem.h
00001 // Filename: pgItem.h
00002 // Created by:  drose (13Mar02)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #ifndef PGITEM_H
00016 #define PGITEM_H
00017 
00018 #include "pandabase.h"
00019 
00020 #include "pgMouseWatcherRegion.h"
00021 #include "pgFrameStyle.h"
00022 #include "pgItemNotify.h"
00023 
00024 #include "pandaNode.h"
00025 #include "nodePath.h"
00026 #include "luse.h"
00027 #include "pointerTo.h"
00028 #include "textNode.h"
00029 #include "plane.h"
00030 #include "pmap.h"
00031 #include "lightReMutex.h"
00032 #include "lightReMutexHolder.h"
00033 
00034 #ifdef HAVE_AUDIO
00035 #include "audioSound.h"
00036 #endif
00037 
00038 class PGTop;
00039 class MouseWatcherParameter;
00040 class AudioSound;
00041 class ClipPlaneAttrib;
00042 class ScissorAttrib;
00043 
00044 ////////////////////////////////////////////////////////////////////
00045 //       Class : PGItem
00046 // Description : This is the base class for all the various kinds of
00047 //               gui widget objects.
00048 //
00049 //               It is a Node which corresponds to a rectangular
00050 //               region on the screen, and it may have any number of
00051 //               "state" subgraphs, one of which is rendered at any
00052 //               given time according to its current state.
00053 //
00054 //               The PGItem node must be parented to the scene graph
00055 //               somewhere beneath a PGTop node in order for this
00056 //               behavior to work.
00057 ////////////////////////////////////////////////////////////////////
00058 class EXPCL_PANDA_PGUI PGItem : public PandaNode {
00059 PUBLISHED:
00060   PGItem(const string &name);
00061   virtual ~PGItem();
00062 
00063   INLINE void set_name(const string &name);
00064 
00065 protected:
00066   PGItem(const PGItem &copy);
00067 
00068   virtual PandaNode *make_copy() const;
00069   virtual void transform_changed();
00070   virtual void draw_mask_changed();
00071 
00072   virtual bool cull_callback(CullTraverser *trav, CullTraverserData &data);
00073   virtual bool is_renderable() const;
00074 
00075   virtual void compute_internal_bounds(CPT(BoundingVolume) &internal_bounds,
00076                                        int &internal_vertices,
00077                                        int pipeline_stage,
00078                                        Thread *current_thread) const;
00079 
00080   virtual void r_prepare_scene(GraphicsStateGuardianBase *gsg,
00081                                const RenderState *node_state,
00082                                GeomTransformer &transformer,
00083                                Thread *current_thread);
00084 
00085 public:
00086   virtual void xform(const LMatrix4 &mat);
00087   bool activate_region(const LMatrix4 &transform, int sort,
00088                        const ClipPlaneAttrib *cpa,
00089                        const ScissorAttrib *sa);
00090   INLINE PGMouseWatcherRegion *get_region() const;
00091 
00092   virtual void enter_region(const MouseWatcherParameter &param);
00093   virtual void exit_region(const MouseWatcherParameter &param);
00094   virtual void within_region(const MouseWatcherParameter &param);
00095   virtual void without_region(const MouseWatcherParameter &param);
00096   virtual void focus_in();
00097   virtual void focus_out();
00098   virtual void press(const MouseWatcherParameter &param, bool background);
00099   virtual void release(const MouseWatcherParameter &param, bool background);
00100   virtual void keystroke(const MouseWatcherParameter &param, bool background);
00101   virtual void candidate(const MouseWatcherParameter &param, bool background);
00102   virtual void move(const MouseWatcherParameter &param);
00103 
00104   static void background_press(const MouseWatcherParameter &param);
00105   static void background_release(const MouseWatcherParameter &param);
00106   static void background_keystroke(const MouseWatcherParameter &param);
00107   static void background_candidate(const MouseWatcherParameter &param);
00108 
00109   INLINE void set_notify(PGItemNotify *notify);
00110   INLINE bool has_notify() const;
00111   INLINE PGItemNotify *get_notify() const;
00112 
00113 PUBLISHED:
00114   INLINE void set_frame(PN_stdfloat left, PN_stdfloat right, PN_stdfloat bottom, PN_stdfloat top);
00115   INLINE void set_frame(const LVecBase4 &frame);
00116   INLINE const LVecBase4 &get_frame() const;
00117   INLINE bool has_frame() const;
00118   INLINE void clear_frame();
00119 
00120   INLINE void set_state(int state);
00121   INLINE int get_state() const;
00122 
00123   virtual void set_active(bool active);
00124   INLINE bool get_active() const;
00125 
00126   virtual void set_focus(bool focus);
00127   INLINE bool get_focus() const;
00128 
00129   void set_background_focus(bool focus);
00130   INLINE bool get_background_focus() const;
00131 
00132   INLINE void set_suppress_flags(int suppress_flags);
00133   INLINE int get_suppress_flags() const;
00134 
00135   int get_num_state_defs() const;
00136   void clear_state_def(int state);
00137   bool has_state_def(int state) const;
00138   NodePath &get_state_def(int state);
00139   MAKE_SEQ(get_state_defs, get_num_state_defs, get_state_def);
00140   NodePath instance_to_state_def(int state, const NodePath &path);
00141 
00142   PGFrameStyle get_frame_style(int state);
00143   void set_frame_style(int state, const PGFrameStyle &style);
00144 
00145   INLINE const string &get_id() const;
00146   INLINE void set_id(const string &id);
00147 
00148   INLINE static string get_enter_prefix();
00149   INLINE static string get_exit_prefix();
00150   INLINE static string get_within_prefix();
00151   INLINE static string get_without_prefix();
00152   INLINE static string get_focus_in_prefix();
00153   INLINE static string get_focus_out_prefix();
00154   INLINE static string get_press_prefix();
00155   INLINE static string get_repeat_prefix();
00156   INLINE static string get_release_prefix();
00157   INLINE static string get_keystroke_prefix();
00158 
00159   INLINE string get_enter_event() const;
00160   INLINE string get_exit_event() const;
00161   INLINE string get_within_event() const;
00162   INLINE string get_without_event() const;
00163   INLINE string get_focus_in_event() const;
00164   INLINE string get_focus_out_event() const;
00165   INLINE string get_press_event(const ButtonHandle &button) const;
00166   INLINE string get_repeat_event(const ButtonHandle &button) const;
00167   INLINE string get_release_event(const ButtonHandle &button) const;
00168   INLINE string get_keystroke_event() const;
00169 
00170   INLINE LMatrix4 get_frame_inv_xform() const;
00171 
00172 #ifdef HAVE_AUDIO
00173   void set_sound(const string &event, AudioSound *sound);
00174   void clear_sound(const string &event);
00175   AudioSound *get_sound(const string &event) const;
00176   bool has_sound(const string &event) const;
00177 #endif
00178 
00179   static TextNode *get_text_node();
00180   INLINE static void set_text_node(TextNode *node);
00181 
00182   INLINE static PGItem *get_focus_item();
00183 
00184 protected:
00185   void play_sound(const string &event);
00186 
00187   void reduce_region(LVecBase4 &clip, PGItem *obscurer) const;
00188   void reduce_region(LVecBase4 &frame, PN_stdfloat px, PN_stdfloat py) const;
00189   LVecBase4 get_relative_frame(PGItem *item) const;
00190   LPoint3 mouse_to_local(const LPoint2 &mouse_point) const;
00191 
00192   virtual void frame_changed();
00193 
00194 private:
00195   void slot_state_def(int state);
00196   void update_frame(int state);
00197   void mark_frames_stale();
00198 
00199   INLINE static PN_stdfloat compute_area(const LVecBase4 &frame);
00200   INLINE static void compare_largest(const LVecBase4 *&largest, 
00201                                      PN_stdfloat &largest_area, 
00202                                      const LVecBase4 *new_frame);
00203 
00204   typedef pvector<LVecBase2> ClipPoints;
00205   bool clip_frame(ClipPoints &source_points, const LPlane &plane) const;
00206 
00207 protected:
00208   LightReMutex _lock;
00209 
00210 private:
00211   PGItemNotify *_notify;
00212 
00213   bool _has_frame;
00214   LVecBase4 _frame;
00215   int _state;
00216   enum Flags {
00217     F_active             = 0x01,
00218     F_focus              = 0x02,
00219     F_background_focus   = 0x04,
00220   };
00221   int _flags;
00222 
00223   PT(PGMouseWatcherRegion) _region;
00224 
00225   LMatrix4 _frame_inv_xform;
00226 
00227   class StateDef {
00228   public:
00229     INLINE StateDef();
00230     NodePath _root;
00231     PGFrameStyle _frame_style;
00232     NodePath _frame;
00233     bool _frame_stale;
00234   };
00235   typedef pvector<StateDef> StateDefs;
00236   StateDefs _state_defs;
00237 
00238 #ifdef HAVE_AUDIO
00239   typedef pmap<string, PT(AudioSound) > Sounds;
00240   Sounds _sounds;
00241 #endif
00242 
00243   static PT(TextNode) _text_node;
00244   static PGItem *_focus_item;
00245 
00246   typedef pset<PGItem *> BackgroundFocus;
00247   static BackgroundFocus _background_focus;
00248 
00249 public:
00250   static TypeHandle get_class_type() {
00251     return _type_handle;
00252   }
00253   static void init_type() {
00254     PandaNode::init_type();
00255     register_type(_type_handle, "PGItem",
00256                   PandaNode::get_class_type());
00257   }
00258   virtual TypeHandle get_type() const {
00259     return get_class_type();
00260   }
00261   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00262 
00263 private:
00264   static TypeHandle _type_handle;
00265 };
00266 
00267 #include "pgItem.I"
00268 
00269 #endif
 All Classes Functions Variables Enumerations