Panda3D
Loading...
Searching...
No Matches
sliderTable.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 sliderTable.cxx
10 * @author drose
11 * @date 2005-03-28
12 */
13
14#include "sliderTable.h"
15#include "bamReader.h"
16#include "bamWriter.h"
17#include "vertexTransform.h"
18
19SparseArray SliderTable::_empty_array;
20TypeHandle SliderTable::_type_handle;
21
22/**
23 *
24 */
25SliderTable::
26SliderTable() :
27 _is_registered(false)
28{
29}
30
31/**
32 *
33 */
34SliderTable::
35SliderTable(const SliderTable &copy) :
36 _is_registered(false),
37 _sliders(copy._sliders),
38 _sliders_by_name(copy._sliders_by_name)
39{
40}
41
42/**
43 *
44 */
45void SliderTable::
46operator = (const SliderTable &copy) {
47 nassertv(!_is_registered);
48 _sliders = copy._sliders;
49 _sliders_by_name = copy._sliders_by_name;
50}
51
52/**
53 *
54 */
55SliderTable::
56~SliderTable() {
57 if (_is_registered) {
58 do_unregister();
59 }
60}
61
62/**
63 * Replaces the nth slider. Only valid for unregistered tables.
64 */
66set_slider(size_t n, const VertexSlider *slider) {
67 nassertv(!_is_registered);
68 nassertv(n < _sliders.size());
69
70 if (_sliders[n]._slider->get_name() != slider->get_name()) {
71 _sliders_by_name[_sliders[n]._slider->get_name()].clear_bit(n);
72 _sliders_by_name[slider->get_name()].set_bit(n);
73 }
74
75 _sliders[n]._slider = slider;
76}
77
78/**
79 * Replaces the rows affected by the nth slider. Only valid for unregistered
80 * tables.
81 */
83set_slider_rows(size_t n, const SparseArray &rows) {
84 // We don't actually enforce the registration requirement, since gee, it
85 // doesn't actually matter here; and the GeomVertexData needs to be able to
86 // change the SparseArrays in the bam reader. nassertv(!_is_registered);
87 nassertv(n < _sliders.size());
88
89 _sliders[n]._rows = rows;
90}
91
92/**
93 * Removes the nth slider. Only valid for unregistered tables.
94 */
96remove_slider(size_t n) {
97 nassertv(!_is_registered);
98 nassertv(n < _sliders.size());
99
100 _sliders_by_name[_sliders[n]._slider->get_name()].clear_bit(n);
101 _sliders.erase(_sliders.begin() + n);
102}
103
104/**
105 * Adds a new slider to the table, and returns the index number of the new
106 * slider. Only valid for unregistered tables.
107 */
109add_slider(const VertexSlider *slider, const SparseArray &rows) {
110 nassertr(!_is_registered, 0);
111
112 size_t new_index = _sliders.size();
113
114 SliderDef slider_def;
115 slider_def._slider = slider;
116 slider_def._rows = rows;
117 _sliders.push_back(slider_def);
118 _sliders_by_name[slider->get_name()].set_bit(new_index);
119
120 return new_index;
121}
122
123/**
124 *
125 */
126void SliderTable::
127write(std::ostream &out) const {
128 for (size_t i = 0; i < _sliders.size(); ++i) {
129 out << i << ". " << *_sliders[i]._slider << " "
130 << _sliders[i]._rows << "\n";
131 }
132}
133
134/**
135 * Called internally when the table is registered.
136 */
137void SliderTable::
138do_register() {
139 nassertv(!_is_registered);
140
141 Sliders::iterator si;
142 for (si = _sliders.begin(); si != _sliders.end(); ++si) {
143 const VertexSlider *slider = (*si)._slider;
144 bool inserted = ((VertexSlider *)slider)->_tables.insert(this).second;
145 nassertv(inserted);
146 }
147 _is_registered = true;
148}
149
150/**
151 * Called internally when the table is unregistered (i.e. right before
152 * destruction).
153 */
154void SliderTable::
155do_unregister() {
156 nassertv(_is_registered);
157
158 Sliders::iterator si;
159 for (si = _sliders.begin(); si != _sliders.end(); ++si) {
160 const VertexSlider *slider = (*si)._slider;
161 ((VertexSlider *)slider)->_tables.erase(this);
162 }
163 _is_registered = false;
164}
165
166/**
167 * Tells the BamReader how to create objects of type SliderTable.
168 */
171 BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
172}
173
174/**
175 * Writes the contents of this object to the datagram for shipping out to a
176 * Bam file.
177 */
179write_datagram(BamWriter *manager, Datagram &dg) {
181
182 dg.add_uint16(_sliders.size());
183 Sliders::const_iterator si;
184 for (si = _sliders.begin(); si != _sliders.end(); ++si) {
185 manager->write_pointer(dg, (*si)._slider->get_name());
186 manager->write_pointer(dg, (*si)._slider);
187 (*si)._rows.write_datagram(manager, dg);
188 }
189
190 manager->write_cdata(dg, _cycler);
191}
192
193/**
194 * Receives an array of pointers, one for each time manager->read_pointer()
195 * was called in fillin(). Returns the number of pointers processed.
196 */
198complete_pointers(TypedWritable **p_list, BamReader *manager) {
199 int pi = TypedWritableReferenceCount::complete_pointers(p_list, manager);
200
201 for (size_t n = 0; n < _sliders.size(); ++n) {
202 CPT(InternalName) name = DCAST(InternalName, p_list[pi++]);
203 PT(VertexSlider) slider = DCAST(VertexSlider, p_list[pi++]);
204
205 _sliders[n]._slider = slider;
206 _sliders_by_name[name].set_bit(n);
207 }
208
209 return pi;
210}
211
212/**
213 * This function is called by the BamReader's factory when a new object of
214 * type SliderTable is encountered in the Bam file. It should create the
215 * SliderTable and extract its information from the file.
216 */
217TypedWritable *SliderTable::
218make_from_bam(const FactoryParams &params) {
219 SliderTable *object = new SliderTable;
220 DatagramIterator scan;
221 BamReader *manager;
222
223 parse_params(params, scan, manager);
224 object->fillin(scan, manager);
225
226 return object;
227}
228
229/**
230 * This internal function is called by make_from_bam to read in all of the
231 * relevant data from the BamFile for the new SliderTable.
232 */
233void SliderTable::
234fillin(DatagramIterator &scan, BamReader *manager) {
235 TypedWritable::fillin(scan, manager);
236
237 size_t num_sliders = scan.get_uint16();
238 _sliders.reserve(num_sliders);
239 for (size_t i = 0; i < num_sliders; ++i) {
240 manager->read_pointer(scan);
241 manager->read_pointer(scan);
242 _sliders.push_back(SliderDef());
243 if (manager->get_file_minor_ver() >= 7) {
244 _sliders[i]._rows.read_datagram(scan, manager);
245 } else {
246 // In this case, for bam files prior to 6.7, we must define the
247 // SparseArray with the full number of vertices. This is done in
248 // GeomVertexData::complete_pointers().
249 }
250 }
251
252 manager->read_cdata(scan, _cycler);
253}
254
255/**
256 *
257 */
258CycleData *SliderTable::CData::
259make_copy() const {
260 return new CData(*this);
261}
262
263/**
264 * Writes the contents of this object to the datagram for shipping out to a
265 * Bam file.
266 */
267void SliderTable::CData::
268write_datagram(BamWriter *manager, Datagram &dg) const {
269}
270
271/**
272 * This internal function is called by make_from_bam to read in all of the
273 * relevant data from the BamFile for the new SliderTable.
274 */
275void SliderTable::CData::
276fillin(DatagramIterator &scan, BamReader *manager) {
277 Thread *current_thread = Thread::get_current_thread();
278 _modified = VertexTransform::get_next_modified(current_thread);
279}
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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
bool read_pointer(DatagramIterator &scan)
The interface for reading a pointer to another object from a Bam file.
int get_file_minor_ver() const
Returns the minor version number of the Bam file currently being read.
Definition bamReader.I:83
void read_cdata(DatagramIterator &scan, PipelineCyclerBase &cycler)
Reads in the indicated CycleData object.
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
void write_cdata(Datagram &packet, const PipelineCyclerBase &cycler)
Writes out the indicated CycleData object.
void write_pointer(Datagram &packet, const TypedWritable *dest)
The interface for writing a pointer to another object to a Bam file.
A single page of data maintained by a PipelineCycler.
Definition cycleData.h:50
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
Encodes a string name in a hash table, mapping it to a pointer.
Stores the total set of VertexSliders that the vertices in a particular GeomVertexData object might d...
Definition sliderTable.h:37
static void register_with_read_factory()
Tells the BamReader how to create objects of type SliderTable.
void set_slider_rows(size_t n, const SparseArray &rows)
Replaces the rows affected by the nth slider.
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 set_slider(size_t n, const VertexSlider *slider)
Replaces the nth slider.
virtual int complete_pointers(TypedWritable **plist, BamReader *manager)
Receives an array of pointers, one for each time manager->read_pointer() was called in fillin().
size_t add_slider(const VertexSlider *slider, const SparseArray &rows)
Adds a new slider to the table, and returns the index number of the new slider.
void remove_slider(size_t n)
Removes the nth slider.
This class records a set of integers, where each integer is either present or not present in the set.
Definition sparseArray.h:43
A thread; that is, a lightweight process.
Definition thread.h:46
get_current_thread
Returns a pointer to the currently-executing Thread object.
Definition thread.h:109
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.
virtual int complete_pointers(TypedWritable **p_list, BamReader *manager)
Receives an array of pointers, one for each time manager->read_pointer() was called in fillin().
This is an abstract base class that retains some slider value, which is a linear value that typically...
get_name
Returns the name of this particular slider.
static UpdateSeq get_next_modified(Thread *current_thread)
Returns a monotonically increasing sequence.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.