Panda3D

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(const RenderState *state,
00081                                PreparedGraphicsObjects *prepared_objects,
00082                                Thread *current_thread);
00083 
00084 public:
00085   virtual void xform(const LMatrix4f &mat);
00086   bool activate_region(const LMatrix4f &transform, int sort,
00087                        const ClipPlaneAttrib *cpa,
00088                        const ScissorAttrib *sa);
00089   INLINE PGMouseWatcherRegion *get_region() const;
00090 
00091   virtual void enter_region(const MouseWatcherParameter &param);
00092   virtual void exit_region(const MouseWatcherParameter &param);
00093   virtual void within_region(const MouseWatcherParameter &param);
00094   virtual void without_region(const MouseWatcherParameter &param);
00095   virtual void focus_in();
00096   virtual void focus_out();
00097   virtual void press(const MouseWatcherParameter &param, bool background);
00098   virtual void release(const MouseWatcherParameter &param, bool background);
00099   virtual void keystroke(const MouseWatcherParameter &param, bool background);
00100   virtual void candidate(const MouseWatcherParameter &param, bool background);
00101   virtual void move(const MouseWatcherParameter &param);
00102 
00103   static void background_press(const MouseWatcherParameter &param);
00104   static void background_release(const MouseWatcherParameter &param);
00105   static void background_keystroke(const MouseWatcherParameter &param);
00106   static void background_candidate(const MouseWatcherParameter &param);
00107 
00108   INLINE void set_notify(PGItemNotify *notify);
00109   INLINE bool has_notify() const;
00110   INLINE PGItemNotify *get_notify() const;
00111 
00112 PUBLISHED:
00113   INLINE void set_frame(float left, float right, float bottom, float top);
00114   INLINE void set_frame(const LVecBase4f &frame);
00115   INLINE const LVecBase4f &get_frame() const;
00116   INLINE bool has_frame() const;
00117   INLINE void clear_frame();
00118 
00119   INLINE void set_state(int state);
00120   INLINE int get_state() const;
00121 
00122   virtual void set_active(bool active);
00123   INLINE bool get_active() const;
00124 
00125   virtual void set_focus(bool focus);
00126   INLINE bool get_focus() const;
00127 
00128   void set_background_focus(bool focus);
00129   INLINE bool get_background_focus() const;
00130 
00131   INLINE void set_suppress_flags(int suppress_flags);
00132   INLINE int get_suppress_flags() const;
00133 
00134   int get_num_state_defs() const;
00135   void clear_state_def(int state);
00136   bool has_state_def(int state) const;
00137   NodePath &get_state_def(int state);
00138   MAKE_SEQ(get_state_defs, get_num_state_defs, get_state_def);
00139   NodePath instance_to_state_def(int state, const NodePath &path);
00140 
00141   PGFrameStyle get_frame_style(int state);
00142   void set_frame_style(int state, const PGFrameStyle &style);
00143 
00144   INLINE const string &get_id() const;
00145   INLINE void set_id(const string &id);
00146 
00147   INLINE static string get_enter_prefix();
00148   INLINE static string get_exit_prefix();
00149   INLINE static string get_within_prefix();
00150   INLINE static string get_without_prefix();
00151   INLINE static string get_focus_in_prefix();
00152   INLINE static string get_focus_out_prefix();
00153   INLINE static string get_press_prefix();
00154   INLINE static string get_repeat_prefix();
00155   INLINE static string get_release_prefix();
00156   INLINE static string get_keystroke_prefix();
00157 
00158   INLINE string get_enter_event() const;
00159   INLINE string get_exit_event() const;
00160   INLINE string get_within_event() const;
00161   INLINE string get_without_event() const;
00162   INLINE string get_focus_in_event() const;
00163   INLINE string get_focus_out_event() const;
00164   INLINE string get_press_event(const ButtonHandle &button) const;
00165   INLINE string get_repeat_event(const ButtonHandle &button) const;
00166   INLINE string get_release_event(const ButtonHandle &button) const;
00167   INLINE string get_keystroke_event() const;
00168 
00169   INLINE LMatrix4f get_frame_inv_xform() const;
00170 
00171 #ifdef HAVE_AUDIO
00172   void set_sound(const string &event, AudioSound *sound);
00173   void clear_sound(const string &event);
00174   AudioSound *get_sound(const string &event) const;
00175   bool has_sound(const string &event) const;
00176 #endif
00177 
00178   static TextNode *get_text_node();
00179   INLINE static void set_text_node(TextNode *node);
00180 
00181   INLINE static PGItem *get_focus_item();
00182 
00183 protected:
00184   void play_sound(const string &event);
00185 
00186   void reduce_region(LVecBase4f &clip, PGItem *obscurer) const;
00187   void reduce_region(LVecBase4f &frame, float px, float py) const;
00188   LVecBase4f get_relative_frame(PGItem *item) const;
00189   LPoint3f mouse_to_local(const LPoint2f &mouse_point) const;
00190 
00191   virtual void frame_changed();
00192 
00193 private:
00194   void slot_state_def(int state);
00195   void update_frame(int state);
00196   void mark_frames_stale();
00197 
00198   INLINE static float compute_area(const LVecBase4f &frame);
00199   INLINE static void compare_largest(const LVecBase4f *&largest, 
00200                                      float &largest_area, 
00201                                      const LVecBase4f *new_frame);
00202 
00203   bool clip_frame(pvector<LPoint2f> &source_points, const Planef &plane) const;
00204 
00205 protected:
00206   LightReMutex _lock;
00207 
00208 private:
00209   PGItemNotify *_notify;
00210 
00211   bool _has_frame;
00212   LVecBase4f _frame;
00213   int _state;
00214   enum Flags {
00215     F_active             = 0x01,
00216     F_focus              = 0x02,
00217     F_background_focus   = 0x04,
00218   };
00219   int _flags;
00220 
00221   PT(PGMouseWatcherRegion) _region;
00222 
00223   LMatrix4f _frame_inv_xform;
00224 
00225   class StateDef {
00226   public:
00227     INLINE StateDef();
00228     NodePath _root;
00229     PGFrameStyle _frame_style;
00230     NodePath _frame;
00231     bool _frame_stale;
00232   };
00233   typedef pvector<StateDef> StateDefs;
00234   StateDefs _state_defs;
00235 
00236 #ifdef HAVE_AUDIO
00237   typedef pmap<string, PT(AudioSound) > Sounds;
00238   Sounds _sounds;
00239 #endif
00240 
00241   static PT(TextNode) _text_node;
00242   static PGItem *_focus_item;
00243 
00244   typedef pset<PGItem *> BackgroundFocus;
00245   static BackgroundFocus _background_focus;
00246 
00247 public:
00248   static TypeHandle get_class_type() {
00249     return _type_handle;
00250   }
00251   static void init_type() {
00252     PandaNode::init_type();
00253     register_type(_type_handle, "PGItem",
00254                   PandaNode::get_class_type());
00255   }
00256   virtual TypeHandle get_type() const {
00257     return get_class_type();
00258   }
00259   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00260 
00261 private:
00262   static TypeHandle _type_handle;
00263 };
00264 
00265 #include "pgItem.I"
00266 
00267 #endif
 All Classes Functions Variables Enumerations