Skip to content

Commit

Permalink
#5757: Add FocusCameraOnSelection command, to put the current selecti…
Browse files Browse the repository at this point in the history
…on on screen
  • Loading branch information
codereader committed Apr 3, 2022
1 parent cfd9204 commit 3faddff
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
10 changes: 7 additions & 3 deletions libs/scenelib.h
Expand Up @@ -332,13 +332,12 @@ inline void assignVisibilityFlagsFromNode(INode& target, const INode& source)
}
}

inline std::pair<Vector3, Vector3> getOriginAndAnglesToLookAtNode(const scene::INode& node)
inline std::pair<Vector3, Vector3> getOriginAndAnglesToLookAtBounds(const AABB& aabb)
{
const AABB& aabb = node.worldAABB();
Vector3 origin(aabb.origin);

// Move the camera a bit off the AABB origin
origin += Vector3(aabb.extents.getLength() * 5, 0, aabb.extents.getLength() * 5);
origin += Vector3(aabb.extents.getLength() * 3, 0, aabb.extents.getLength() * 3);

// Rotate the camera a bit towards the "ground"
Vector3 angles(0, 0, 0);
Expand All @@ -348,4 +347,9 @@ inline std::pair<Vector3, Vector3> getOriginAndAnglesToLookAtNode(const scene::I
return std::make_pair(origin, angles);
}

inline std::pair<Vector3, Vector3> getOriginAndAnglesToLookAtNode(const scene::INode& node)
{
return getOriginAndAnglesToLookAtBounds(node.worldAABB());
}

} // namespace scene
16 changes: 16 additions & 0 deletions radiantcore/map/Map.cpp
Expand Up @@ -540,6 +540,21 @@ void Map::focusViewCmd(const cmd::ArgumentList& args)
focusViews(args[0].getVector3(), args[1].getVector3());
}

void Map::focusCameraOnSelectionCmd(const cmd::ArgumentList& args)
{
if (GlobalSelectionSystem().countSelected() == 0)
{
throw cmd::ExecutionNotPossible(_("Cannot focus, selection is empty"));
}

// Determine the bounds of the current selection
const auto& workZone = GlobalSelectionSystem().getWorkZone();
auto originAndAngles = scene::getOriginAndAnglesToLookAtBounds(workZone.bounds);

// Set the camera and the views to the given point
GlobalCameraManager().focusAllCameras(originAndAngles.first, originAndAngles.second);
}

scene::INodePtr Map::findWorldspawn()
{
scene::INodePtr worldspawn;
Expand Down Expand Up @@ -986,6 +1001,7 @@ void Map::registerCommands()
GlobalCommandSystem().addCommand("SaveSelected", Map::exportSelection);
GlobalCommandSystem().addCommand("ReloadSkins", map::algorithm::reloadSkins);
GlobalCommandSystem().addCommand("FocusViews", std::bind(&Map::focusViewCmd, this, std::placeholders::_1), { cmd::ARGTYPE_VECTOR3, cmd::ARGTYPE_VECTOR3 });
GlobalCommandSystem().addCommand("FocusCameraOnSelection", std::bind(&Map::focusCameraOnSelectionCmd, this, std::placeholders::_1));
GlobalCommandSystem().addCommand("ExportSelectedAsModel", map::algorithm::exportSelectedAsModelCmd,
{ cmd::ARGTYPE_STRING,
cmd::ARGTYPE_STRING,
Expand Down
1 change: 1 addition & 0 deletions radiantcore/map/Map.h
Expand Up @@ -299,6 +299,7 @@ class Map :
*/
void focusViews(const Vector3& point, const Vector3& angles);
void focusViewCmd(const cmd::ArgumentList& args);
void focusCameraOnSelectionCmd(const cmd::ArgumentList& args);

void undoCmd(const cmd::ArgumentList& args);
void redoCmd(const cmd::ArgumentList& args);
Expand Down

0 comments on commit 3faddff

Please sign in to comment.