From 9e7fa49f168c4ae25d23e4858a895216e997d205 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 19 Feb 2012 21:29:24 +0000 Subject: Added a seperate class to hold entity properties. --- src/Makefile.am | 5 +- src/editorwindow.cc | 16 ++-- src/entityproperties.cc | 42 +++++++++ src/entityproperties.h | 68 +++++++++++++++ src/entitywidget.cc | 63 +------------- src/entitywidget.h | 97 ++------------------- src/inistream.h | 10 +-- src/mapwidget.cc | 23 ++--- src/mapwidget.h | 4 +- src/properties.cc | 40 +++++++++ src/properties.h | 158 +++++++++++++++++++++++++++++++++ src/sidebar.cc | 32 +++---- src/sidebar.h | 18 +++- src/vector3f.cc | 157 +++++++++++++++++++++++++++++++++ src/vector3f.h | 227 ++++++++++++++++++++++++++++++++++++++++++++++++ src/zoneproperties.cc | 0 src/zoneproperties.h | 0 17 files changed, 766 insertions(+), 194 deletions(-) create mode 100644 src/entityproperties.cc create mode 100644 src/entityproperties.h create mode 100644 src/properties.cc create mode 100644 src/properties.h create mode 100644 src/vector3f.cc create mode 100644 src/vector3f.h create mode 100644 src/zoneproperties.cc create mode 100644 src/zoneproperties.h diff --git a/src/Makefile.am b/src/Makefile.am index aa2355c..08b3859 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -5,11 +5,14 @@ bin_PROGRAMS = editor editor_SOURCES = \ editor.cc \ editorwindow.cc \ + entityproperties.cc \ entitywidget.cc \ inistream.cc \ mainwindow.cc \ mapwidget.cc \ - sidebar.cc + properties.cc \ + sidebar.cc \ + vector3f.cc # You have one .h file, it's called editor.h. Therefore, here I list # its mocced name, moc_editor.cpp. diff --git a/src/editorwindow.cc b/src/editorwindow.cc index 1ced0c0..f01548d 100644 --- a/src/editorwindow.cc +++ b/src/editorwindow.cc @@ -24,7 +24,7 @@ EditorWindow::EditorWindow(QWidget *parent) : QWidget(parent) editorwindow_mapwidget = new MapWidget(this); editorwindow_sidebar = new SideBar(this); - connect(editorwindow_mapwidget, SIGNAL(selected(EntityWidget *)), editorwindow_sidebar, SLOT(setEntity(EntityWidget *))); + connect(editorwindow_mapwidget, SIGNAL(propertiesChanged(EntityProperties *)), editorwindow_sidebar, SLOT(setProperties(EntityProperties *))); connect(editorwindow_sidebar, SIGNAL(entityChanged()), editorwindow_mapwidget, SLOT(resizeChildren())); } @@ -108,7 +108,7 @@ bool EditorWindow::loadFile(const QString &filename) entity = editorwindow_mapwidget->addEntity(); } if (entity && in_subsection) { - entity->add_subsection(ini.section()); + entity->properties()->add_subsection(ini.section()); } } else if(ini.got_key()) { @@ -117,24 +117,24 @@ bool EditorWindow::loadFile(const QString &filename) // read entity properties into the MapWidget instance if (ini.got_key_vector3f("location" , x, y, z)) { - entity->set_location(x, y, z); + entity->properties()->set_location(x, y, z); //qDebug() << "got location " << x << " " << y << " " << z; } else if (ini.got_key_string("label", str)) { - entity->set_label(str); + entity->properties()->set_label(str); } else if (ini.got_key_string("name", str)) { - entity->set_name(str); + entity->properties()->set_name(str); } else if (ini.got_key_float("radius", f)) { - entity->set_radius(f); + entity->properties()->set_radius(f); } else if (ini.got_key()) { - entity->add_property(ini.key(), ini.value()); + entity->properties()->add_value(ini.key(), ini.value()); } } else if (entity && in_subsection) { - entity->add_subsection_property(ini.key(), ini.value()); + entity->properties()->add_subsection_value(ini.key(), ini.value()); } else if (ini.in_section("zone")) { diff --git a/src/entityproperties.cc b/src/entityproperties.cc new file mode 100644 index 0000000..371746f --- /dev/null +++ b/src/entityproperties.cc @@ -0,0 +1,42 @@ +/* + entityproperties.cc + This file is part of the Project::OSiRiON world editor + and is distributed under the terms and conditions of + the GNU General Public License version 2 +*/ + +#include "entityproperties.h" + +namespace editor +{ + +EntityProperties::EntityProperties() +{ +} + +EntityProperties::~EntityProperties() +{ +} + +void EntityProperties::add_subsection(const QString &name) +{ + if (properties_subsections.size()) { + properties_subsections += '\n'; + } + + properties_subsections += '['; + properties_subsections += name; + properties_subsections += ']'; + properties_subsections += '\n'; + +} + +void EntityProperties::add_subsection_value(const QString &key, const QString &value) +{ + properties_subsections += key; + properties_subsections += '='; + properties_subsections += value; + properties_subsections += '\n'; +} + +} // namespace editor diff --git a/src/entityproperties.h b/src/entityproperties.h new file mode 100644 index 0000000..9ea38b5 --- /dev/null +++ b/src/entityproperties.h @@ -0,0 +1,68 @@ +/* + entityproperties.h + This file is part of the Project::OSiRiON world editor + and is distributed under the terms and conditions of + the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_EDITOR_ENTITYPROPERTIES__ +#define __INCLUDED_EDITOR_ENTITYPROPERTIES__ + +#include "properties.h" + +namespace editor +{ + +/** + * @brief contains the game properties for an entity + * */ +class EntityProperties : public Properties +{ +public: + EntityProperties(); + virtual ~EntityProperties(); + + /* ---- inspectors ---- */ + + /** + * @brief returns the object radius + * */ + inline const float radius() const { + return properties_radius; + } + + /** + * @brief returns the subsections string of this object + * */ + inline const QString & subsections() const { + return properties_subsections; + } + + /* ---- mutators ---- */ + + /** + * @brief add a header to the subsection string of this object + * */ + void add_subsection(const QString &name); + + /** + * @brief add a value key pair to the subsection string of this object + * */ + void add_subsection_value(const QString &key, const QString &value); + + /** + * @brief set the object radius + * */ + inline void set_radius(const float radius) { + properties_radius = radius; + } + +private: + float properties_radius; + + QString properties_subsections; +}; + +} // namespace editor + +#endif // __INCLUDED_EDITOR_ENTITYPROPERTIES__ diff --git a/src/entitywidget.cc b/src/entitywidget.cc index 50056c0..c903ea8 100644 --- a/src/entitywidget.cc +++ b/src/entitywidget.cc @@ -18,8 +18,6 @@ EntityWidget::EntityWidget(QWidget *parent) : QWidget(parent) { is_selected = false; is_dragging = false; - - entity_radius = 0; } void EntityWidget::set_selected(const bool selected) @@ -28,63 +26,6 @@ void EntityWidget::set_selected(const bool selected) update(); } -void EntityWidget::set_label(const QString &label) -{ - entity_label = label; -} - -void EntityWidget::set_name(const QString &name) -{ - entity_name = name; -} - -void EntityWidget::set_radius(const float radius) -{ - entity_radius = radius; - -} - -void EntityWidget::set_location(const float x, const float y, const float z) -{ - entity_location[0] = x; - entity_location[1] = y; - entity_location[2] = z; -} - -void EntityWidget::set_properties(const QString &properties) -{ - entity_properties = properties; -} - -void EntityWidget::add_property(const QString &key, const QString &value) -{ - entity_properties += key; - entity_properties += '='; - entity_properties += value; - entity_properties += '\n'; -} - -void EntityWidget::add_subsection(const QString &name) -{ - if (entity_subsections.size()) { - entity_subsections += '\n'; - } - - entity_subsections += '['; - entity_subsections += name; - entity_subsections += ']'; - entity_subsections += '\n'; - -} - -void EntityWidget::add_subsection_property(const QString &key, const QString &value) -{ - entity_subsections += key; - entity_subsections += '='; - entity_subsections += value; - entity_subsections += '\n'; -} - void EntityWidget::paintEvent(QPaintEvent *event) { QPen pen(Qt::black, 1, Qt::SolidLine); @@ -104,7 +45,8 @@ void EntityWidget::mousePressEvent(QMouseEvent *event) if (event->button() == Qt::LeftButton) { //qDebug() << "clicked entity " << name(); event->accept(); - is_dragging = true; + if (is_selected) + is_dragging = true; emit clicked(this); } else { event->ignore(); @@ -122,6 +64,7 @@ void EntityWidget::mouseReleaseEvent(QMouseEvent *event) void EntityWidget::mouseMoveEvent(QMouseEvent *event) { if (is_dragging) { + // TODO add delay emit dragged(this, event->pos().x(), event->pos().y()); } } diff --git a/src/entitywidget.h b/src/entitywidget.h index 4eaaf49..1bdfb93 100644 --- a/src/entitywidget.h +++ b/src/entitywidget.h @@ -8,6 +8,8 @@ #ifndef __INCLUDED_EDITOR_ENTITYWIDGET__ #define __INCLUDED_EDITOR_ENTITYWIDGET__ +#include "entityproperties.h" + #include #include #include @@ -26,46 +28,12 @@ public: EntityWidget(QWidget *parent = 0); /** - * @brief returns the entity label - * */ - inline const QString &label() const { - return entity_label; - } - - /** - * @brief returns the entity name - * */ - inline const QString &name() const { - return entity_name; - } - - /** - * @brief returns the entity radius + * @brief returns the entity propertie * */ - inline const float radius() const { - return entity_radius; + inline EntityProperties *properties() { + return &entitywidget_entityproperties; } - /** - * @brief returns the x, y, or z coordinate of the entity location - * */ - inline const float location(int index) const { - return entity_location[index]; - } - - /** - * @brief returns the properties string - * */ - inline const QString &properties() const { - return entity_properties; - } - - /** - * @brief returns the subsections string - * */ - inline const QString &subsections() const { - return entity_subsections; - } signals: /** * @brief this signal is emitted if the entity is clicked with the left mouse button @@ -78,47 +46,7 @@ signals: void dragged(EntityWidget *entity, int x, int y); public slots: - - /** - * @brief set the entity name - * */ - void set_label(const QString &label); - /** - * @brief set the entity label - * */ - void set_name(const QString &name); - - /** - * @brief set the entity radius - * */ - void set_radius(const float radius); - - /** - * @brief set the entity location - * */ - void set_location(const float x, const float y, const float z); - - /** - * @brief set the entity properties string - * */ - void set_properties(const QString &properties); - - /** - * @brief add a property - * */ - void add_property(const QString &key, const QString &value); - - /** - * @brief add a subsection - * */ - void add_subsection(const QString &name); - - /** - * @brief add a subsection property - * */ - void add_subsection_property(const QString &key, const QString &value); - /** * @brief set the selected state * */ @@ -146,20 +74,11 @@ protected: virtual void mouseMoveEvent(QMouseEvent *event); private: - bool is_selected; - bool is_dragging; - - QString entity_label; - QString entity_name; - QString entity_type; - - QString entity_properties; - QString entity_subsections; + EntityProperties entitywidget_entityproperties; - float entity_location[3]; - float entity_radius; + bool is_selected; + bool is_dragging; - QColor entity_color; }; } diff --git a/src/inistream.h b/src/inistream.h index a9e2817..08a6ef4 100644 --- a/src/inistream.h +++ b/src/inistream.h @@ -15,8 +15,8 @@ namespace editor { -/// a class to read ini streams /** + * @brief a class to read an ini file on a text stream * The IniStream class is able to decode the structure of a windows-like * .ini file from a text stream. **/ @@ -57,21 +57,21 @@ public: bool in_section(const char *sectionlabel) const; /// true if the last read statement was a certain section header - bool got_section(const char * sectionlabel) const; + bool got_section(const char *sectionlabel) const; /// true if the last read statement was a key=value pair bool got_key() const; - bool got_key(const char * keylabel); + bool got_key(const char *keylabel); /// check if the last read key=value pair matches keylabel and store the string value - bool got_key_string(const char * keylabel, QString & valuestring); + bool got_key_string(const char *keylabel, QString &valuestring); /// check if the last read key=value pair matches keylabel and store the value in x y and z bool got_key_vector3f(const char *keylabel, float &x, float &y, float &z); /// check if the last read key=value pair matches keylabel and store the float value - bool got_key_float(const char * keylabel, float & f); + bool got_key_float(const char *keylabel, float &f); /* /// check if the last read key=value pair matches keylabel and store the value in valuestring, converted to label diff --git a/src/mapwidget.cc b/src/mapwidget.cc index 78c413d..d9ec735 100644 --- a/src/mapwidget.cc +++ b/src/mapwidget.cc @@ -101,12 +101,12 @@ void MapWidget::resizeChildren() for (int i = 0; i < mapwidget_enties.size(); ++i) { EntityWidget * entity = mapwidget_enties.at(i); - int radius = (int) (entity->radius() * scale); + int radius = (int) (entity->properties()->radius() * scale); if (radius < 6) radius = 6; - int x = width() / 2 - (int) (entity->location(1) * scale) - radius; - int y = height() / 2 - (int)(entity->location(0) * scale) - radius; + int x = width() / 2 - (int) (entity->properties()->location()[1] * scale) - radius; + int y = height() / 2 - (int)(entity->properties()->location()[0] * scale) - radius; entity->setGeometry(center_x + x, center_y + y, radius * 2 , radius * 2); //qDebug() << "Moving entity to " << x << " " << y; @@ -120,22 +120,23 @@ void MapWidget::deselect() EntityWidget *entitywidget = mapwidget_enties.at(i); entitywidget->set_selected(false); } - emit selected(0); + emit propertiesChanged(0); } void MapWidget::dragEntity(EntityWidget *entity, int x, int y) { const float scale = (float) (mapwidget_zoom * 256) / (float) width(); - int radius = (int) (entity->radius() / scale); + int radius = (int) (entity->properties()->radius() / scale); if (radius < 6) radius = 6; - entity->set_location( - entity->location(0) - (int) ((float) (y - radius) * scale), - entity->location(1) - (int) ((float) (x - radius) * scale), - entity->location(2) + entity->properties()->set_location( + entity->properties()->location()[0] - (int) ((float) (y - radius) * scale), + entity->properties()->location()[1] - (int) ((float) (x - radius) * scale), + entity->properties()->location()[2] ); - emit selected(entity); + + emit propertiesChanged(entity->properties()); resizeChildren(); } void MapWidget::select(EntityWidget *entity) @@ -148,7 +149,7 @@ void MapWidget::select(EntityWidget *entity) entity->set_selected(true); //qDebug() << "selected entity " << entity->name(); - emit selected(entity); + emit propertiesChanged(entity->properties()); } else { diff --git a/src/mapwidget.h b/src/mapwidget.h index 531eb4d..0afcaff 100644 --- a/src/mapwidget.h +++ b/src/mapwidget.h @@ -8,6 +8,8 @@ #ifndef __INCLUDED_EDITOR_MAPWIDGET__ #define __INCLUDED_EDITOR_MAPWIDGET__ +#include "entityproperties.h" + #include #include @@ -72,7 +74,7 @@ signals: /** * @brief the selected() signal is emitted if an entity on the map is selected * */ - void selected(EntityWidget *entity); + void propertiesChanged(EntityProperties *properties); public slots: /** diff --git a/src/properties.cc b/src/properties.cc new file mode 100644 index 0000000..f533c15 --- /dev/null +++ b/src/properties.cc @@ -0,0 +1,40 @@ +/* + properties.cc + This file is part of the Project::OSiRiON world editor + and is distributed under the terms and conditions of + the GNU General Public License version 2 +*/ + +#include "properties.h" + +namespace editor +{ + +Properties::Properties() +{ +} + +Properties::~Properties() +{ +} + +void Properties::add_value(const QString &key, const QString &value) +{ + properties_values += key; + properties_values += '='; + properties_values += value; + properties_values += '\n'; +} + +void Properties::set_info(const QString &text) +{ + properties_info = text; +} + +void Properties::add_info(const QString &text) +{ + properties_info += text; + properties_info += '\n'; +} + +} // namespace editor diff --git a/src/properties.h b/src/properties.h new file mode 100644 index 0000000..472d4b4 --- /dev/null +++ b/src/properties.h @@ -0,0 +1,158 @@ +/* + properties.h + This file is part of the Project::OSiRiON world editor + and is distributed under the terms and conditions of + the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_EDITOR_PROPERTIES__ +#define __INCLUDED_EDITOR_PROPERTIES__ + +#include "vector3f.h" + +#include +#include + +namespace editor +{ + +/** + * @brief abstract base class to hold properties for game objects + * */ +class Properties +{ +public: + Properties(); + + virtual ~Properties(); + + /* ---- inspectors ---- */ + + /** + * @brief returns the label of this object + * */ + inline const QString &label() const { + return properties_label; + } + + /** + * @brief returns the name of this object + * */ + inline const QString &name() const { + return properties_name; + } + + /** + * @brief returns the location of this object + * */ + inline const Vector3f &location() const { + return properties_location; + } + + /** + * @brief returns the color of this object + * */ + inline const QColor &color() const { + return properties_color; + } + + + /** + * @brief returns the info string of this object + * */ + inline const QString & info() const { + return properties_info; + } + + /** + * @brief returns the values string of this object + * */ + inline const QString & values() const { + return properties_values; + } + + /* ---- mutators ---- */ + + /** + * @brief set the info string of this object + * */ + void set_info(const QString &text); + + /** + * @brief add a line of text to the info string of this object + * */ + void add_info(const QString &text); + + /** + * @brief add a value key pair to the values string + * */ + void add_value(const QString &key, const QString &value); + + + /** + * @brief set the object label + * */ + inline void set_label(const QString &label) { + properties_label = label; + } + + /** + * @brief set the object name + * */ + inline void set_name(const QString &name) { + properties_name = name; + } + + /** + * @brief set the object location + * */ + inline void set_location(const Vector3f &location) { + properties_location = location; + } + + /** + * @brief set the object location + * */ + inline void set_location(const float x, const float y, const float z) { + properties_location.assign(x, y, z); + } + + /** + * @brief set the object color + * */ + inline void set_color(const QColor &color) { + properties_color = color; + } + + /** + * @brief set the object color + * */ + inline void set_color(const float r, const float g, const float b) { + float cr = r; + float cg = g; + float cb = b; + + // Qt RGB colors are in the 0-255 range + if ((r <= 1) && (g <= 1) && (b <= 1)) { + cr *= 255; + cg *= 255; + cg *= 255; + } + + properties_color.setRgb(r, g, b); + } + +private: + QString properties_label; + QString properties_name; + + Vector3f properties_location; + QColor properties_color; + + QString properties_info; + QString properties_values; +}; + +} // namespace editor + +#endif // __INCLUDED_EDITOR_PROPERTIES__ \ No newline at end of file diff --git a/src/sidebar.cc b/src/sidebar.cc index 498a457..c541f92 100644 --- a/src/sidebar.cc +++ b/src/sidebar.cc @@ -76,7 +76,7 @@ SideBar::SideBar(QWidget *parent) : QWidget(parent) setLayout(box_global); - setEntity(0); + setProperties(0); } void SideBar::updateEntityLocationX(const QString &value) @@ -85,7 +85,7 @@ void SideBar::updateEntityLocationX(const QString &value) bool ok; float x = value.toFloat(&ok); if (ok) { - last_selected->set_location(x, last_selected->location(1), last_selected->location(2)); + last_selected->set_location(x, last_selected->location()[1], last_selected->location()[2]); emit entityChanged(); } } @@ -97,7 +97,7 @@ void SideBar::updateEntityLocationY(const QString &value) bool ok; float y = value.toFloat(&ok); if (ok) { - last_selected->set_location(last_selected->location(0), y, last_selected->location(2)); + last_selected->set_location(last_selected->location()[0], y, last_selected->location()[2]); emit entityChanged(); } } @@ -109,19 +109,17 @@ void SideBar::updateEntityLocationZ(const QString &value) bool ok; float z = value.toFloat(&ok); if (ok) { - last_selected->set_location(last_selected->location(0), last_selected->location(1), z); + last_selected->set_location(last_selected->location()[0], last_selected->location()[1], z); emit entityChanged(); } } } -void SideBar::setEntity(EntityWidget *entity) +void SideBar::setProperties(EntityProperties *properties) { - QString value; + last_selected = properties; - last_selected = entity; - - if (!entity) { + if (!properties) { edit_entitylabel->setEnabled(false); edit_entitylabel->clear(); @@ -143,29 +141,31 @@ void SideBar::setEntity(EntityWidget *entity) text_subsections->setEnabled(false); text_subsections->clear(); } else { + QString value; + edit_entitylabel->setEnabled(true); - edit_entitylabel->setText(entity->label()); + edit_entitylabel->setText(properties->label()); edit_entityname->setEnabled(true); - edit_entityname->setText(entity->name()); + edit_entityname->setText(properties->name()); edit_entitylocation_x->setEnabled(true); - value.setNum(entity->location(0)); + value.setNum(properties->location()[0]); edit_entitylocation_x->setText(value); edit_entitylocation_y->setEnabled(true); - value.setNum(entity->location(1)); + value.setNum(properties->location()[1]); edit_entitylocation_y->setText(value); edit_entitylocation_z->setEnabled(true); - value.setNum(entity->location(2)); + value.setNum(properties->location()[2]); edit_entitylocation_z->setText(value); text_entityproperties->setEnabled(true); - text_entityproperties->setText(entity->properties()); + text_entityproperties->setText(properties->values()); text_subsections->setEnabled(true); - text_subsections->setText(entity->subsections()); + text_subsections->setText(properties->subsections()); } } diff --git a/src/sidebar.h b/src/sidebar.h index 5a20c37..05a79f0 100644 --- a/src/sidebar.h +++ b/src/sidebar.h @@ -8,6 +8,8 @@ #ifndef __INCLUDED_EDITOR_SIDEBAR__ #define __INCLUDED_EDITOR_SIDEBAR__ +#include "entityproperties.h" + #include class QLabel; @@ -31,14 +33,24 @@ class SideBar : public QWidget public: SideBar(QWidget *parent = 0); -public slots: - void setEntity(EntityWidget *entity); +public slots: + + /** + * @brief set the sidebar properties + * */ + void setProperties(EntityProperties *properties); + /** + * @brief set the zone name + * */ void setZoneName(const QString &name); private slots: + void updateEntityLocationX(const QString &value); + void updateEntityLocationY(const QString &value); + void updateEntityLocationZ(const QString &value); signals: @@ -55,7 +67,7 @@ private: QTextEdit *text_entityproperties; QTextEdit *text_subsections; - EntityWidget *last_selected; + EntityProperties *last_selected; }; } diff --git a/src/vector3f.cc b/src/vector3f.cc new file mode 100644 index 0000000..6d42742 --- /dev/null +++ b/src/vector3f.cc @@ -0,0 +1,157 @@ +/* + vector3f.cc + This file is part of the Project::OSiRiON world editor + and is distributed under the terms and conditions of + the GNU General Public License version 2 +*/ + +#include +#include "vector3f.h" + +namespace editor +{ + +Vector3f::Vector3f() +{ + clear(); +} + +Vector3f::Vector3f(const Vector3f &other) +{ + assign(other); +} + +Vector3f::Vector3f(const float x, const float y, const float z) +{ + assign(x, y, z); +} + +Vector3f::~Vector3f() +{ +} + +void Vector3f::clear() +{ + coord[0] = 0; + coord[1] = 0; + coord[2] = 0; +} + +void Vector3f::assign(const float x, const float y, const float z) +{ + coord[0] = x; + coord[1] = y; + coord[2] = z; +} + +void Vector3f::assign(const Vector3f & other) +{ + coord[0] = other.coord[0]; + coord[1] = other.coord[1]; + coord[2] = other.coord[2]; +} + +Vector3f & Vector3f::operator=(const Vector3f & other) +{ + assign(other); + return (*this); +} + +Vector3f & Vector3f::operator*=(const float scalar) +{ + for (int i = 0; i < 3; i++) + coord[i] *= scalar; + return (*this); +} + +Vector3f & Vector3f::operator/=(const float scalar) +{ + for (int i = 0; i < 3; i++) + coord[i] /= scalar; + return (*this); +} + +Vector3f &Vector3f::operator-=(const Vector3f & other) +{ + for (int i = 0; i < 3; i++) + coord[i] -= other[i]; + return (*this); +} + +Vector3f &Vector3f::operator+=(const Vector3f &other) +{ + for (int i = 0; i < 3; i++) + coord[i] += other[i]; + return (*this); +} + +bool Vector3f::operator==(const Vector3f& other) const +{ + for (int i = 0; i < 3; i++) + if (coord[i] != other.coord[i]) + return (false); + return (true); +} + +float Vector3f::lengthsquared() const +{ + float r = 0; + for (int i = 0; i < 3; i++) + r += coord[i] * coord[i]; + return ((float) r); +} + +float Vector3f::length() const +{ + float r = 0; + for (int i = 0; i < 3; i++) + r += coord[i] * coord[i]; + + return (sqrtf(r)); +} + +void Vector3f::normalize() +{ + (*this) /= this->length(); +} + +Vector3f operator*(float scalar, const Vector3f &vector) +{ + return vector * scalar; +} + +const Vector3f crossproduct(const Vector3f & first, const Vector3f &second) +{ + float vx = first[1] * second[2] - first[2] * second[1]; + float vy = first[2] * second[0] - first[0] * second[2]; + float vz = first[0] * second[1] - first[1] * second[0]; + + return(Vector3f(vx, vy, vz)); +} + +float dotproduct(const Vector3f& first, const Vector3f& second) +{ + float r = 0; + for (int i = 0; i < 3; i++) + r += first[i] * second[i]; + return (r); +} + +float distance(const Vector3f& first, const Vector3f& second) +{ + float r = 0; + for (int i = 0; i < 3; i++) + r += (first[i] - second[i]) * (first[i] - second[i]); + + return (sqrtf(r)); +} + +float distancesquared(const Vector3f& first, const Vector3f& second) +{ + float r = 0; + for (int i = 0; i < 3; i++) + r += (first[i] - second[i]) * (first[i] - second[i]); + return (r); +} + +} // namespace math diff --git a/src/vector3f.h b/src/vector3f.h new file mode 100644 index 0000000..211bb4f --- /dev/null +++ b/src/vector3f.h @@ -0,0 +1,227 @@ +/* + vector3f.h + This file is part of the Project::OSiRiON world editor + and is distributed under the terms and conditions of + the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_EDITOR_VECTOR3F__ +#define __INCLUDED_EDITOR_VECTOR3F__ + +#include + +namespace editor +{ + +/** + * @brief a point or vector in 3D space + * An instance of this class represents a point in 3D-space or a 3D-vector + * and forms the basic building block for all spatial calculations. + */ +class Vector3f +{ + +public: + /** + * @brief default constructor, assigns (0,0,0) + * */ + Vector3f(); + + /** + * @brief copy constructor + * Create a new Vector3f that is a copy from an other + * @param other the vector to copy values from + */ + Vector3f(const Vector3f &other); + + /** + * @brief create a Vector3f with given coordinates + * @param x the x-coordinate of the location + * @param y the y-coordinate of the location + * @param z the z-coordinate of the location + */ + Vector3f(const float x, const float y, const float z); + + /// destructor + ~Vector3f(); + + /* -- Assignment operators -- */ + /// assign (0, 0, 0) + void clear(); + + /// assignment + void assign(const float x, const float y, const float z); + + /// assignment + void assign(const Vector3f & other); + + /// assignment operator + Vector3f& operator=(const Vector3f &other); + + /// scalar multiplication assignment + /** @param scalar multiplication factor + */ + Vector3f& operator*=(const float scalar); + + /// scalar division assignment + /** @param scalar divider + */ + Vector3f& operator/=(const float scalar); + + /// vector sum assignment + Vector3f& operator+=(const Vector3f &other); + + /// vector difference assignment + Vector3f& operator-=(const Vector3f &other); + + /* -- Mathematical operators -- */ + + /// scalar multiplication + inline Vector3f operator*(const float scalar) const { + Vector3f v(coord[0] * scalar, coord[1] * scalar, coord[2] * scalar); + return v; + } + + /// scalar division + Vector3f operator/(const float scalar) const { + Vector3f v(coord[0] / scalar, coord[1] / scalar, coord[2] / scalar); + return v; + } + + /// vector sum + inline Vector3f operator+(const Vector3f &other) const { + Vector3f v(coord[0] + other.coord[0], coord[1] + other.coord[1], coord[2] + other.coord[2]); + return v; + } + + /// vector difference + inline Vector3f operator-(const Vector3f &other) const { + Vector3f v(coord[0] - other.coord[0], coord[1] - other.coord[1], coord[2] - other.coord[2]); + return v; + } + + /// comparison operator + bool operator==(const Vector3f &other) const; + + /// assign a value to an element of this vector + /*! range is not checked + * @param index the index of the element to assign to ( 0 <= index < 3 ) + */ + inline float& operator[](const int index) { + assert ((0 <= index) && (index < 3)); + return coord[index]; + } + + /// returns the value of an element of this vector + /*! range is not checked + * @param index the index of the element to return ( 0 <= index < 3 ) + */ + inline float operator[](const int index) const { + assert ((0 <= index) && (index < 3)); + return coord[index]; + } + + /// x coordinate + inline float x() const { + return coord[0]; + } + + /// y coordinate + inline float y() const { + return coord[1]; + } + + /// z coordinate + inline float z() const { + return coord[2]; + } + + /// mutable reference to the x coordinate + inline float & get_x() { + return coord[0]; + } + + /// mutable reference to the y coordinate + inline float & get_y() { + return coord[1]; + } + + /// mutable reference to the z coordinate + inline float & get_z() { + return coord[2]; + } + + /// a pointer to the internal data + inline float *ptr() const { + return (float *) coord; + } + + /// cartesian length + float length() const; + + /// cartesian length squared + float lengthsquared() const; + + /// divide this Vector by its length + /// @see normalized() + /// vector must not be (0, 0, 0) + void normalize(); + + /* static functions */ + + /// Returns the unity vector on the X-axis + static inline Vector3f Xaxis() { + return Vector3f(1.0f, 0.0f, 0.0f); + } + + /// Returns the unity vector on the Y-axis + static inline Vector3f Yaxis() { + return Vector3f(0.0f, 1.0f, 0.0f); + } + + /// Returns the unity vector on the Z-axis + static inline Vector3f Zaxis() { + return Vector3f(0.0f, 0.0f, 1.0f); + } + + /// cartesian lenght + static inline float length(const Vector3f & vector) { + return vector.length(); + } + + /// Return a vector divided by it's length + /// @see normalize() + /// WARNING: vector must not be (0, 0, 0) + static inline Vector3f normalized(const Vector3f & vector) { + return (vector / vector.length()); + } + +private: + float coord[3]; +}; + +/// scalar * Vector3f operators +Vector3f operator*(float scalar, const Vector3f & vector); + +/// vector cross product +const Vector3f crossproduct(const Vector3f & first, const Vector3f & second); + +/// vector dot product +float dotproduct(const Vector3f& first, const Vector3f & second); + +/// distance between two vectors +float distance(const Vector3f& first, const Vector3f& second); + +/// distance between two vectors squared +float distancesquared(const Vector3f& first, const Vector3f& second); + +/// calculate the normal of a plane defined by three vertices +inline const Vector3f normal(const Vector3f & center, const Vector3f & u, const Vector3f & v) { + Vector3f r = crossproduct(u - center, v - center); + r.normalize(); + return r; +} + +} // namespace math + +#endif // __INCLUDED_EDITOR_VECTOR3F__ diff --git a/src/zoneproperties.cc b/src/zoneproperties.cc new file mode 100644 index 0000000..e69de29 diff --git a/src/zoneproperties.h b/src/zoneproperties.h new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3