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