worldspawn/plugins/entity/origin.h

168 lines
3.5 KiB
C
Raw Normal View History

2020-11-17 11:16:16 +00:00
/*
Copyright (C) 2001-2006, William Joseph.
All Rights Reserved.
This file is part of GtkRadiant.
GtkRadiant is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
GtkRadiant is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GtkRadiant; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#if !defined( INCLUDED_ORIGIN_H )
#define INCLUDED_ORIGIN_H
#include "ientity.h"
#include "math/matrix.h"
#include "generic/callback.h"
#include "stringio.h"
const Vector3 ORIGINKEY_IDENTITY = Vector3(0, 0, 0);
inline void default_origin(Vector3 &origin)
{
2021-08-04 20:23:18 +00:00
origin = ORIGINKEY_IDENTITY;
2020-11-17 11:16:16 +00:00
}
inline void read_origin(Vector3 &origin, const char *value)
{
2021-08-04 20:23:18 +00:00
if (!string_parse_vector3(value, origin)) {
default_origin(origin);
}
2020-11-17 11:16:16 +00:00
}
inline void write_origin(const Vector3 &origin, Entity *entity, const char *key)
{
2021-08-04 20:23:18 +00:00
char value[64];
sprintf(value, "%f %f %f", origin[0], origin[1], origin[2]);
entity->setKeyValue(key, value);
2020-11-17 11:16:16 +00:00
}
inline Vector3 origin_translated(const Vector3 &origin, const Vector3 &translation)
{
2021-08-04 20:23:18 +00:00
return matrix4_get_translation_vec3(
matrix4_multiplied_by_matrix4(
matrix4_translation_for_vec3(origin),
matrix4_translation_for_vec3(translation)
)
);
2020-11-17 11:16:16 +00:00
}
inline Vector3 origin_snapped(const Vector3 &origin, float snap)
{
2021-08-04 20:23:18 +00:00
return vector3_snapped(origin, snap);
2020-11-17 11:16:16 +00:00
}
class OriginKey {
2021-08-04 20:23:18 +00:00
Callback<void()> m_originChanged;
2020-11-17 11:16:16 +00:00
public:
2021-08-04 20:23:18 +00:00
Vector3 m_origin;
2020-11-17 11:16:16 +00:00
2021-08-04 20:23:18 +00:00
OriginKey(const Callback<void()> &originChanged)
: m_originChanged(originChanged), m_origin(ORIGINKEY_IDENTITY)
{
}
2020-11-17 11:16:16 +00:00
2021-08-04 20:23:18 +00:00
void originChanged(const char *value)
{
read_origin(m_origin, value);
m_originChanged();
}
2020-11-17 11:16:16 +00:00
2021-08-04 20:23:18 +00:00
typedef MemberCaller<OriginKey, void (const char *), &OriginKey::originChanged> OriginChangedCaller;
2020-11-17 11:16:16 +00:00
2021-08-04 20:23:18 +00:00
void write(Entity *entity) const
{
write_origin(m_origin, entity, "origin");
}
2020-11-17 11:16:16 +00:00
};
#include "scenelib.h"
inline BrushDoom3 *Node_getBrushDoom3(scene::Node &node)
{
2021-08-04 20:23:18 +00:00
return NodeTypeCast<BrushDoom3>::cast(node);
2020-11-17 11:16:16 +00:00
}
inline void BrushDoom3_setDoom3GroupOrigin(scene::Node &node, const Vector3 &origin)
{
2021-08-04 20:23:18 +00:00
BrushDoom3 *brush = Node_getBrushDoom3(node);
if (brush != 0) {
brush->setDoom3GroupOrigin(origin);
}
2020-11-17 11:16:16 +00:00
}
class SetDoom3GroupOriginWalker : public scene::Traversable::Walker {
2021-08-04 20:23:18 +00:00
const Vector3 &m_origin;
2020-11-17 11:16:16 +00:00
public:
2021-08-04 20:23:18 +00:00
SetDoom3GroupOriginWalker(const Vector3 &origin) : m_origin(origin)
{
}
bool pre(scene::Node &node) const
{
BrushDoom3_setDoom3GroupOrigin(node, m_origin);
return true;
}
2020-11-17 11:16:16 +00:00
};
class Doom3GroupOrigin : public scene::Traversable::Observer {
2021-08-04 20:23:18 +00:00
scene::Traversable &m_set;
const Vector3 &m_origin;
bool m_enabled;
2020-11-17 11:16:16 +00:00
public:
2021-08-04 20:23:18 +00:00
Doom3GroupOrigin(scene::Traversable &set, const Vector3 &origin) : m_set(set), m_origin(origin), m_enabled(false)
{
}
void enable()
{
m_enabled = true;
originChanged();
}
void disable()
{
m_enabled = false;
}
void originChanged()
{
if (m_enabled) {
m_set.traverse(SetDoom3GroupOriginWalker(m_origin));
}
}
void insert(scene::Node &node)
{
if (m_enabled) {
BrushDoom3_setDoom3GroupOrigin(node, m_origin);
}
}
void erase(scene::Node &node)
{
if (m_enabled) {
BrushDoom3_setDoom3GroupOrigin(node, Vector3(0, 0, 0));
}
}
2020-11-17 11:16:16 +00:00
};
#endif