PandaFramework should respect per-window keypresses

This commit is contained in:
David Rose 2003-02-14 08:13:17 +00:00
parent 1749f817c2
commit 74a704440e
11 changed files with 155 additions and 27 deletions

View File

@ -267,6 +267,17 @@ remove_all_windows() {
terminate_threads(); terminate_threads();
} }
////////////////////////////////////////////////////////////////////
// Function: GraphicsEngine::is_empty
// Access: Published
// Description: Returns true if there are no windows managed by the
// engine, false if there is at least one.
////////////////////////////////////////////////////////////////////
bool GraphicsEngine::
is_empty() const {
return _windows.empty();
}
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Function: GraphicsEngine::render_frame // Function: GraphicsEngine::render_frame
// Access: Published // Access: Published

View File

@ -64,6 +64,7 @@ PUBLISHED:
const string &threading_model); const string &threading_model);
bool remove_window(GraphicsWindow *window); bool remove_window(GraphicsWindow *window);
void remove_all_windows(); void remove_all_windows();
bool is_empty() const;
void render_frame(); void render_frame();
void sync_frame(); void sync_frame();

View File

@ -95,13 +95,13 @@ get_coordinate_system() const {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Function: EggData::set_egg_filename // Function: EggData::set_egg_filename
// Access: Public // Access: Public
// Description: Sets the directory in which the egg file is // Description: Sets the filename--especially the directory part--in
// considered to reside. This is also implicitly set by // which the egg file is considered to reside. This is
// read(). // also implicitly set by read().
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
INLINE void EggData:: INLINE void EggData::
set_egg_filename(const Filename &directory) { set_egg_filename(const Filename &egg_filename) {
_egg_filename = directory; _egg_filename = egg_filename;
} }
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////

View File

@ -69,7 +69,7 @@ public:
void set_coordinate_system(CoordinateSystem coordsys); void set_coordinate_system(CoordinateSystem coordsys);
INLINE CoordinateSystem get_coordinate_system() const; INLINE CoordinateSystem get_coordinate_system() const;
INLINE void set_egg_filename(const Filename &directory); INLINE void set_egg_filename(const Filename &egg_filenamea);
INLINE const Filename &get_egg_filename() const; INLINE const Filename &get_egg_filename() const;
INLINE void recompute_vertex_normals(double threshold); INLINE void recompute_vertex_normals(double threshold);

View File

@ -37,9 +37,15 @@ EventParameter() {
// Description: Defines an EventParameter that stores a pointer to // Description: Defines an EventParameter that stores a pointer to
// any kind of TypedReferenceCount object. This is the // any kind of TypedReferenceCount object. This is the
// most general constructor. // most general constructor.
//
// This accepts a const pointer, even though it stores
// (and eventually returns) a non-const pointer. This
// is just the simplest way to allow both const and
// non-const pointers to be stored, but it does lose the
// constness. Be careful.
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
INLINE EventParameter:: INLINE EventParameter::
EventParameter(const TypedReferenceCount *ptr) : _ptr(ptr) { } EventParameter(const TypedReferenceCount *ptr) : _ptr((TypedReferenceCount *)ptr) { }
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
@ -195,7 +201,7 @@ get_string_value() const {
// contains. This is the only way to retrieve the value // contains. This is the only way to retrieve the value
// when it is not one of the above predefined types. // when it is not one of the above predefined types.
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
INLINE const TypedReferenceCount *EventParameter:: INLINE TypedReferenceCount *EventParameter::
get_ptr() const { get_ptr() const {
return _ptr; return _ptr;
} }

View File

@ -60,12 +60,12 @@ PUBLISHED:
INLINE bool is_string() const; INLINE bool is_string() const;
INLINE string get_string_value() const; INLINE string get_string_value() const;
INLINE const TypedReferenceCount *get_ptr() const; INLINE TypedReferenceCount *get_ptr() const;
void output(ostream &out) const; void output(ostream &out) const;
private: private:
CPT(TypedReferenceCount) _ptr; PT(TypedReferenceCount) _ptr;
}; };
INLINE ostream &operator << (ostream &out, const EventParameter &param); INLINE ostream &operator << (ostream &out, const EventParameter &param);

View File

@ -205,6 +205,26 @@ open_window(const WindowProperties &props, GraphicsPipe *pipe) {
return wf; return wf;
} }
////////////////////////////////////////////////////////////////////
// Function: PandaFramework::find_window
// Access: Public
// Description: Returns the index of the WindowFramework object that
// references the indicated GraphicsWindow pointer, or
// -1 if none do.
////////////////////////////////////////////////////////////////////
int PandaFramework::
find_window(const GraphicsWindow *win) const {
int n;
for (n = 0; n < (int)_windows.size(); n++) {
if (_windows[n]->get_graphics_window() == win) {
return n;
}
}
return -1;
}
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Function: PandaFramework::close_window // Function: PandaFramework::close_window
// Access: Public // Access: Public
@ -587,13 +607,28 @@ do_enable_default_keys() {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Function: PandaFramework::event_esc // Function: PandaFramework::event_esc
// Access: Protected, Static // Access: Protected, Static
// Description: Default handler for ESC or q key: exit the // Description: Default handler for ESC or q key: close the current
// application. // window (and exit the application if that was the last
// window).
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
void PandaFramework:: void PandaFramework::
event_esc(CPT_Event, void *data) { event_esc(CPT_Event event, void *data) {
PandaFramework *self = (PandaFramework *)data; if (event->get_num_parameters() == 1) {
self->_exit_flag = true; EventParameter param = event->get_parameter(0);
GraphicsWindow *win;
DCAST_INTO_V(win, param.get_ptr());
PandaFramework *self = (PandaFramework *)data;
int n = self->find_window(win);
if (n >= 0) {
self->close_window(n);
}
// If we closed the last window, shut down.
if (self->_windows.empty()) {
self->_exit_flag = true;
}
}
} }
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
@ -661,18 +696,23 @@ event_l(CPT_Event, void *data) {
// the scene, or over the highlighted part of the scene. // the scene, or over the highlighted part of the scene.
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
void PandaFramework:: void PandaFramework::
event_c(CPT_Event, void *data) { event_c(CPT_Event event, void *data) {
PandaFramework *self = (PandaFramework *)data; if (event->get_num_parameters() == 1) {
EventParameter param = event->get_parameter(0);
const GraphicsWindow *win;
DCAST_INTO_V(win, param.get_ptr());
NodePath node = self->get_highlight(); PandaFramework *self = (PandaFramework *)data;
if (node.is_empty()) {
node = self->get_models();
}
Windows::iterator wi; NodePath node = self->get_highlight();
for (wi = self->_windows.begin(); wi != self->_windows.end(); ++wi) { if (node.is_empty()) {
WindowFramework *wf = (*wi); node = self->get_models();
wf->center_trackball(node); }
int n = self->find_window(win);
if (n >= 0) {
self->_windows[n]->center_trackball(node);
}
} }
} }

View File

@ -60,6 +60,7 @@ public:
INLINE int get_num_windows() const; INLINE int get_num_windows() const;
INLINE WindowFramework *get_window(int n) const; INLINE WindowFramework *get_window(int n) const;
int find_window(const GraphicsWindow *win) const;
void close_window(int n); void close_window(int n);
void close_all_windows(); void close_all_windows();
bool all_windows_closed() const; bool all_windows_closed() const;

View File

@ -198,6 +198,7 @@ enable_keyboard() {
NodePath mouse = get_mouse(); NodePath mouse = get_mouse();
PT(ButtonThrower) bt = new ButtonThrower("kb-events"); PT(ButtonThrower) bt = new ButtonThrower("kb-events");
bt->add_parameter(EventParameter(_window));
ModifierButtons mods; ModifierButtons mods;
mods.add_button(KeyboardButton::shift()); mods.add_button(KeyboardButton::shift());
mods.add_button(KeyboardButton::control()); mods.add_button(KeyboardButton::control());

View File

@ -22,6 +22,7 @@
#include "buttonEventList.h" #include "buttonEventList.h"
#include "dataNodeTransmit.h" #include "dataNodeTransmit.h"
#include "throw_event.h" #include "throw_event.h"
#include "event.h"
#include "indent.h" #include "indent.h"
#include "dcast.h" #include "dcast.h"
@ -89,6 +90,44 @@ get_prefix() const {
return _prefix; return _prefix;
} }
////////////////////////////////////////////////////////////////////
// Function: ButtonThrower::add_parameter
// Access: Public
// Description: Adds the indicated parameter to the list of
// parameters that will be passed with each event
// generated by this ButtonThrower.
////////////////////////////////////////////////////////////////////
void ButtonThrower::
add_parameter(const EventParameter &obj) {
_parameters.push_back(obj);
}
////////////////////////////////////////////////////////////////////
// Function: ButtonThrower::get_num_parameters
// Access: Public
// Description: Returns the number of parameters that have been added
// to the list of parameters to be passed with each
// event generated by this ButtonThrower.
////////////////////////////////////////////////////////////////////
int ButtonThrower::
get_num_parameters() const {
return _parameters.size();
}
////////////////////////////////////////////////////////////////////
// Function: ButtonThrower::get_parameter
// Access: Public
// Description: Returns the nth parameter that has been added to the
// list of parameters passed with each event generated
// by this ButtonThrower.
////////////////////////////////////////////////////////////////////
EventParameter ButtonThrower::
get_parameter(int n) const {
nassertr(n >= 0 && n < (int)_parameters.size(), EventParameter(0));
return _parameters[n];
}
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Function: ButtonThrower::get_modifier_buttons // Function: ButtonThrower::get_modifier_buttons
// Access: Published // Access: Published
@ -324,6 +363,24 @@ write(ostream &out, int indent_level) const {
} }
} }
////////////////////////////////////////////////////////////////////
// Function: ButtonThrower::do_throw_event
// Access: Private
// Description: Generates an event of the indicated name, adding on
// all of the user-requested parameters.
////////////////////////////////////////////////////////////////////
void ButtonThrower::
do_throw_event(const string &event_name) {
Event *event = new Event(_prefix + event_name);
ParameterList::const_iterator pi;
for (pi = _parameters.begin(); pi != _parameters.end(); ++pi) {
event->add_parameter(*pi);
}
throw_event(event);
}
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Function: ButtonThrower::do_transmit_data // Function: ButtonThrower::do_transmit_data
// Access: Protected, Virtual // Access: Protected, Virtual
@ -362,7 +419,7 @@ do_transmit_data(const DataNodeTransmit &input, DataNodeTransmit &output) {
if (!_throw_buttons_active || has_throw_button(_mods, be._button)) { if (!_throw_buttons_active || has_throw_button(_mods, be._button)) {
// Process this button. // Process this button.
throw_event(_prefix + event_name); do_throw_event(event_name);
} else { } else {
// Don't process this button; instead, pass it down to future // Don't process this button; instead, pass it down to future
@ -385,7 +442,7 @@ do_transmit_data(const DataNodeTransmit &input, DataNodeTransmit &output) {
// definition for the button at all, regardless of the state // definition for the button at all, regardless of the state
// of the modifier keys. // of the modifier keys.
if (!_throw_buttons_active || has_throw_button(be._button)) { if (!_throw_buttons_active || has_throw_button(be._button)) {
throw_event(_prefix + event_name + "-up"); do_throw_event(event_name + "-up");
} }
if (_throw_buttons_active) { if (_throw_buttons_active) {
// Now pass the event on to future generations. We always // Now pass the event on to future generations. We always

View File

@ -26,6 +26,7 @@
#include "buttonEventList.h" #include "buttonEventList.h"
#include "pvector.h" #include "pvector.h"
#include "pmap.h" #include "pmap.h"
#include "eventParameter.h"
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Class : ButtonThrower // Class : ButtonThrower
@ -48,6 +49,10 @@ PUBLISHED:
bool has_prefix() const; bool has_prefix() const;
string get_prefix() const; string get_prefix() const;
void add_parameter(const EventParameter &obj);
int get_num_parameters() const;
EventParameter get_parameter(int n) const;
const ModifierButtons &get_modifier_buttons() const; const ModifierButtons &get_modifier_buttons() const;
void set_modifier_buttons(const ModifierButtons &mods); void set_modifier_buttons(const ModifierButtons &mods);
@ -63,10 +68,16 @@ PUBLISHED:
public: public:
virtual void write(ostream &out, int indent_level = 0) const; virtual void write(ostream &out, int indent_level = 0) const;
private:
void do_throw_event(const string &event_name);
private: private:
string _prefix; string _prefix;
ModifierButtons _mods; ModifierButtons _mods;
typedef pvector<EventParameter> ParameterList;
ParameterList _parameters;
typedef pvector<ModifierButtons> ThrowButtonDef; typedef pvector<ModifierButtons> ThrowButtonDef;
typedef pmap<ButtonHandle, ThrowButtonDef> ThrowButtons; typedef pmap<ButtonHandle, ThrowButtonDef> ThrowButtons;
ThrowButtons _throw_buttons; ThrowButtons _throw_buttons;