Panda3D
 All Classes Functions Variables Enumerations
buttonEventList.cxx
1 // Filename: buttonEventList.cxx
2 // Created by: drose (12Mar02)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "buttonEventList.h"
16 #include "modifierButtons.h"
17 #include "indent.h"
18 
19 TypeHandle ButtonEventList::_type_handle;
20 
21 ////////////////////////////////////////////////////////////////////
22 // Function: ButtonEventList::add_events
23 // Access: Public
24 // Description: Appends the events from the other list onto the end
25 // of this one.
26 ////////////////////////////////////////////////////////////////////
28 add_events(const ButtonEventList &other) {
29  _events.reserve(_events.size() + other._events.size());
30  Events::const_iterator ei;
31  for (ei = other._events.begin(); ei != other._events.end(); ++ei) {
32  _events.push_back(*ei);
33  }
34 }
35 
36 ////////////////////////////////////////////////////////////////////
37 // Function: ButtonEventList::update_mods
38 // Access: Public
39 // Description: Updates the indicated ModifierButtons object with all
40 // of the button up/down transitions indicated in the
41 // list.
42 ////////////////////////////////////////////////////////////////////
45  Events::const_iterator ei;
46  for (ei = _events.begin(); ei != _events.end(); ++ei) {
47  (*ei).update_mods(mods);
48  }
49 }
50 
51 ////////////////////////////////////////////////////////////////////
52 // Function: ButtonEventList::output
53 // Access: Public, Virtual
54 // Description:
55 ////////////////////////////////////////////////////////////////////
56 void ButtonEventList::
57 output(ostream &out) const {
58  if (_events.empty()) {
59  out << "(no buttons)";
60  } else {
61  Events::const_iterator ei;
62  ei = _events.begin();
63  out << "(" << (*ei);
64  ++ei;
65  while (ei != _events.end()) {
66  out << " " << (*ei);
67  ++ei;
68  }
69  out << ")";
70  }
71 }
72 
73 ////////////////////////////////////////////////////////////////////
74 // Function: ButtonEventList::write
75 // Access: Public
76 // Description:
77 ////////////////////////////////////////////////////////////////////
78 void ButtonEventList::
79 write(ostream &out, int indent_level) const {
80  indent(out, indent_level) << _events.size() << " events:\n";
81  Events::const_iterator ei;
82  for (ei = _events.begin(); ei != _events.end(); ++ei) {
83  indent(out, indent_level + 2) << (*ei) << "\n";
84  }
85 }
86 
87 ////////////////////////////////////////////////////////////////////
88 // Function: ButtonEventList::register_with_read_factory
89 // Access: Public, Static
90 // Description: Tells the BamReader how to create objects of type
91 // Lens.
92 ////////////////////////////////////////////////////////////////////
95  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
96 }
97 
98 ////////////////////////////////////////////////////////////////////
99 // Function: ButtonEventList::write_datagram
100 // Access: Public, Virtual
101 // Description: Writes the contents of this object to the datagram
102 // for shipping out to a Bam file.
103 ////////////////////////////////////////////////////////////////////
106  TypedWritable::write_datagram(manager, dg);
107 
108  dg.add_uint16(_events.size());
109  Events::const_iterator ei;
110  for (ei = _events.begin(); ei != _events.end(); ++ei) {
111  (*ei).write_datagram(dg);
112  }
113 }
114 
115 ////////////////////////////////////////////////////////////////////
116 // Function: ButtonEventList::make_from_bam
117 // Access: Protected, Static
118 // Description: This function is called by the BamReader's factory
119 // when a new object of type Lens is encountered
120 // in the Bam file. It should create the Lens
121 // and extract its information from the file.
122 ////////////////////////////////////////////////////////////////////
123 TypedWritable *ButtonEventList::
124 make_from_bam(const FactoryParams &params) {
125  ButtonEventList *list = new ButtonEventList;
126  DatagramIterator scan;
127  BamReader *manager;
128 
129  parse_params(params, scan, manager);
130  list->fillin(scan, manager);
131 
132  return list;
133 }
134 
135 ////////////////////////////////////////////////////////////////////
136 // Function: ButtonEventList::fillin
137 // Access: Public
138 // Description: This internal function is called by make_from_bam to
139 // read in all of the relevant data from the BamFile for
140 // the new ButtonEventList.
141 //
142 // This function is normally protected, but it is
143 // declared public in this case so that MouseRecorder
144 // may call it to read a ButtonEventList from the middle
145 // of a datagram.
146 ////////////////////////////////////////////////////////////////////
149  TypedWritable::fillin(scan, manager);
150 
151  int num_events = scan.get_uint16();
152  _events.clear();
153  _events.reserve(num_events);
154  for (int i = 0; i < num_events; i++) {
155  ButtonEvent event;
156  event.read_datagram(scan);
157  _events.push_back(event);
158  }
159 }
This class monitors the state of a number of individual buttons and tracks whether each button is kno...
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:122
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:37
static void register_with_read_factory()
Tells the BamReader how to create objects of type Lens.
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:73
void fillin(DatagramIterator &scan, BamReader *manager)
This internal function is called by make_from_bam to read in all of the relevant data from the BamFil...
Records a button event of some kind.
Definition: buttonEvent.h:53
Records a set of button events that happened recently.
virtual void fillin(DatagramIterator &scan, BamReader *manager)
This internal function is intended to be called by each class&#39;s make_from_bam() method to read in all...
PN_uint16 get_uint16()
Extracts an unsigned 16-bit integer.
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
void update_mods(ModifierButtons &mods) const
Updates the indicated ModifierButtons object with all of the button up/down transitions indicated in ...
void add_events(const ButtonEventList &other)
Appends the events from the other list onto the end of this one.
void read_datagram(DatagramIterator &scan)
Restores the event from the datagram.
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
void register_factory(TypeHandle handle, CreateFunc *func)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:90
void add_uint16(PN_uint16 value)
Adds an unsigned 16-bit integer to the datagram.
Definition: datagram.I:181
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:213
A class to retrieve the individual data elements previously stored in a Datagram. ...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43