Skip to content

Commit

Permalink
🧹 Terrain editor final touches
Browse files Browse the repository at this point in the history
TerrainEditor.h - fixed comments, variables moved for clarity, renamed `node` to static_object_node
TerrainEditor.cpp - fixed direct manipulations with `node` (caused crash when pressing middle mouse button in editor)
TerrainObjectManager.cpp - `destroyObject()` - handle preloaded actors correctly
  • Loading branch information
ohlidalp committed Jan 6, 2025
1 parent e32b809 commit 81c550c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
14 changes: 7 additions & 7 deletions source/main/terrain/TerrainEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void TerrainEditor::UpdateInputEvents(float dt)
Vector3 direction = terrain_editor_mouse_ray.getDirection();
for (int i = 0; i < (int)object_list.size(); i++)
{
Real ray_object_distance = direction.crossProduct(object_list[i]->node->getPosition() - origin).length();
Real ray_object_distance = direction.crossProduct(object_list[i]->getPosition() - origin).length();
if (ray_object_distance < min_dist)
{
min_dist = ray_object_distance;
Expand All @@ -77,7 +77,7 @@ void TerrainEditor::UpdateInputEvents(float dt)
float min_dist = std::numeric_limits<float>::max();
for (int i = 0; i < (int)object_list.size(); i++)
{
float dist = ref_pos.squaredDistance(object_list[i]->node->getPosition());
float dist = ref_pos.squaredDistance(object_list[i]->getPosition());
if (dist < min_dist)
{
this->SetSelectedObjectByID(i);
Expand Down Expand Up @@ -406,9 +406,9 @@ void TerrainEditorObjectRefreshActorVisual(TerrainEditorObjectPtr obj)
void TerrainEditorObject::setPosition(Ogre::Vector3 const& pos)
{
position = pos;
if (node)
if (static_object_node)
{
node->setPosition(pos);
static_object_node->setPosition(pos);
}
else if (special_object_type != TObjSpecialObject::NONE)
{
Expand All @@ -419,10 +419,10 @@ void TerrainEditorObject::setPosition(Ogre::Vector3 const& pos)
void TerrainEditorObject::setRotation(Ogre::Vector3 const& rot)
{
rotation = rot;
if (node)
if (static_object_node)
{
node->setOrientation(Quaternion(Degree(rot.x), Vector3::UNIT_X) * Quaternion(Degree(rot.y), Vector3::UNIT_Y) * Quaternion(Degree(rot.z), Vector3::UNIT_Z));
node->pitch(Degree(-90));
static_object_node->setOrientation(Quaternion(Degree(rot.x), Vector3::UNIT_X) * Quaternion(Degree(rot.y), Vector3::UNIT_Y) * Quaternion(Degree(rot.z), Vector3::UNIT_Z));
static_object_node->pitch(Degree(-90));
}
else if (special_object_type != TObjSpecialObject::NONE)
{
Expand Down
11 changes: 6 additions & 5 deletions source/main/terrain/TerrainEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ class TerrainEditorObject : public RefCountingObject<TerrainEditorObject>
Ogre::Vector3 rotation = Ogre::Vector3::ZERO;
Ogre::Vector3 initial_position = Ogre::Vector3::ZERO;
Ogre::Vector3 initial_rotation = Ogre::Vector3::ZERO;
Ogre::SceneNode* node = nullptr;
bool enable_collisions = true;
int script_handler = -1;
int tobj_cache_id = -1;
std::string tobj_comments;
// ~ only for static objects:
Ogre::SceneNode* static_object_node = nullptr;
std::vector<int> static_collision_boxes;
std::vector<int> static_collision_tris;
bool enable_collisions = true;
int script_handler = -1;
// ~ only for preloaded actors:
TObjSpecialObject special_object_type = TObjSpecialObject::NONE;
ActorInstanceID_t actor_instance_id = ACTORINSTANCEID_INVALID;
Expand All @@ -62,8 +63,8 @@ class TerrainEditorObject : public RefCountingObject<TerrainEditorObject>
std::string const& getName();
std::string const& getInstanceName();
std::string const& getType();
TObjSpecialObject getSpecialObjectType();
// ~ only for preloaded actors:
TObjSpecialObject getSpecialObjectType();
void setSpecialObjectType(TObjSpecialObject type);
ActorInstanceID_t getActorInstanceId();
void setActorInstanceId(ActorInstanceID_t instance_id);
Expand All @@ -74,7 +75,7 @@ class TerrainEditorObject : public RefCountingObject<TerrainEditorObject>
/// * Select object by middle mouse button or Enter key (closest to avatar)
/// * Rotate/move selected object with keys
/// * Select/edit also preloaded actors - this resets the actor (added 2024 by Petr Ohlidal)
/// Upon exit, file 'editor_out.cfg' is written to ROR_HOME/config (see RGN_CONFIG)
/// Upon exit, the original *.tobj files are updated in place (only if terrain is unzipped in directory)
class TerrainEditor
{
public:
Expand Down
41 changes: 30 additions & 11 deletions source/main/terrain/TerrainObjectManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -510,21 +510,40 @@ void TerrainObjectManager::destroyObject(const String& instancename)
return;
}

for (int tri : m_editor_objects[id]->static_collision_tris)
TerrainEditorObjectPtr object = m_editor_objects[id];

if (object->getSpecialObjectType() != TObjSpecialObject::NONE)
{
terrainManager->GetCollisions()->removeCollisionTri(tri);
// Preloaded actor: despawn it.
ROR_ASSERT(!object->static_object_node);
ROR_ASSERT(!object->static_collision_tris.size());
ROR_ASSERT(!object->static_collision_boxes.size());
ActorPtr actor = App::GetGameContext()->GetActorManager()->GetActorById(object->actor_instance_id);
if (actor)
{
App::GetGameContext()->PushMessage(Message(MSG_SIM_DELETE_ACTOR_REQUESTED, new ActorPtr(actor)));
}
}
for (int box : m_editor_objects[id]->static_collision_boxes)
else
{
terrainManager->GetCollisions()->removeCollisionBox(box);
}
// Static object: Destroy the scene node and everything attached to it.
ROR_ASSERT(object->static_object_node);
for (Ogre::MovableObject* mova : object->static_object_node->getAttachedObjects())
{
App::GetGfxScene()->GetSceneManager()->destroyMovableObject(mova);
}
App::GetGfxScene()->GetSceneManager()->destroySceneNode(object->static_object_node);

// Destroy the scene node and everything attached to it.
for (Ogre::MovableObject* mova : m_editor_objects[id]->node->getAttachedObjects())
{
App::GetGfxScene()->GetSceneManager()->destroyMovableObject(mova);
// Undo static collisions
for (int tri : object->static_collision_tris)
{
terrainManager->GetCollisions()->removeCollisionTri(tri);
}
for (int box : object->static_collision_boxes)
{
terrainManager->GetCollisions()->removeCollisionBox(box);
}
}
App::GetGfxScene()->GetSceneManager()->destroySceneNode(m_editor_objects[id]->node);

// Release the object from editor, if active.
if (id == App::GetGameContext()->GetTerrain()->GetTerrainEditor()->GetSelectedObjectID())
Expand Down Expand Up @@ -647,7 +666,7 @@ bool TerrainObjectManager::LoadTerrainObject(const Ogre::String& name, const Ogr
object->rotation = rot;
object->initial_position = pos;
object->initial_rotation = rot;
object->node = tenode;
object->static_object_node = tenode;
object->enable_collisions = enable_collisions;
object->script_handler = scripthandler;
object->tobj_cache_id = m_tobj_cache_active_id;
Expand Down

0 comments on commit 81c550c

Please sign in to comment.