Skip to content

Commit

Permalink
#5456: Some refactoring to be able to re-use the prefab bounds code d…
Browse files Browse the repository at this point in the history
…uring insertion. Snap the prefab center to grid and translate it to the point the user actually clicked.
  • Loading branch information
codereader committed Jan 22, 2021
1 parent 8232148 commit 408fa40
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
31 changes: 20 additions & 11 deletions libs/scene/PrefabBoundsAccumulator.h
@@ -1,8 +1,8 @@
#pragma once

#include "AABBAccumulateWalker.h"
#include "ilightnode.h"
#include "ispeakernode.h"
#include "iselection.h"

namespace scene
{
Expand All @@ -12,15 +12,15 @@ namespace scene
* scene below a root node. It will ignore light and speaker volumes
* since they are needlessly inflating the bounds.
*/
class PrefabBoundsAggregator final :
public AABBAccumulateWalker
class PrefabBoundsAccumulator final :
public scene::NodeVisitor,
public SelectionSystem::Visitor
{
private:
AABB _bounds;
mutable AABB _bounds;

public:
PrefabBoundsAggregator() :
AABBAccumulateWalker(_bounds) // point the base to the local member
PrefabBoundsAccumulator()
{}

const AABB& getBounds() const
Expand All @@ -29,26 +29,35 @@ class PrefabBoundsAggregator final :
}

bool pre(const INodePtr& node) override
{
_bounds.includeAABB(GetNodeBounds(node));
return false;
}

void visit(const scene::INodePtr& node) const override
{
_bounds.includeAABB(GetNodeBounds(node));
}

static AABB GetNodeBounds(const INodePtr& node)
{
// For lights we'll only sum up the small diamond AABB
auto lightNode = Node_getLightNode(node);

if (lightNode)
{
_bounds.includeAABB(lightNode->getSelectAABB());
return false;
return lightNode->getSelectAABB();
}

// Speakers without radius
auto speakerNode = Node_getSpeakerNode(node);

if (speakerNode)
{
_bounds.includeAABB(speakerNode->getSpeakerAABB());
return false;
return speakerNode->getSpeakerAABB();
}

return AABBAccumulateWalker::pre(node);
return node->worldAABB();
}
};

Expand Down
6 changes: 3 additions & 3 deletions radiant/ui/common/MapPreview.cpp
Expand Up @@ -47,10 +47,10 @@ AABB MapPreview::getSceneBounds()
{
if (!getScene()->root()) return RenderPreview::getSceneBounds();

scene::PrefabBoundsAggregator aggregator;
getScene()->root()->traverseChildren(aggregator);
scene::PrefabBoundsAccumulator accumulator;
getScene()->root()->traverseChildren(accumulator);

return aggregator.getBounds();
return accumulator.getBounds();
}

bool MapPreview::onPreRender()
Expand Down
9 changes: 8 additions & 1 deletion radiantcore/map/Map.cpp
Expand Up @@ -32,6 +32,7 @@

#include "brush/BrushModule.h"
#include "scene/BasicRootNode.h"
#include "scene/PrefabBoundsAccumulator.h"
#include "map/MapFileManager.h"
#include "map/MapPositionManager.h"
#include "map/MapResource.h"
Expand Down Expand Up @@ -653,12 +654,18 @@ void Map::loadPrefabAt(const cmd::ArgumentList& args)
// Now import the prefab (imported items get selected)
import(prefabPath);

// Get the selection bounds, snap its origin to the grid
scene::PrefabBoundsAccumulator accumulator;
GlobalSelectionSystem().foreachSelected(accumulator);

auto prefabCenter = accumulator.getBounds().getOrigin().getSnapped(GlobalGrid().getGridSize());

// Switch texture lock on
bool prevTexLockState = GlobalBrush().textureLockEnabled();
GlobalBrush().setTextureLock(true);

// Translate the selection to the given point
selection::algorithm::translateSelected(targetCoords);
selection::algorithm::translateSelected(targetCoords - prefabCenter);

// Revert to previous state
GlobalBrush().setTextureLock(prevTexLockState);
Expand Down

0 comments on commit 408fa40

Please sign in to comment.