Skip to content

Commit

Permalink
Added RunInEditor flag to processors that allows processors to either…
Browse files Browse the repository at this point in the history
… run while the editor is running, and/or only when the game is playing.
  • Loading branch information
fLindahl committed Nov 9, 2024
1 parent 46a89c4 commit b60ad12
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 29 deletions.
1 change: 1 addition & 0 deletions code/addons/audiofeature/managers/audiomanager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ AudioManager::Create()
.Func(UpdateSpatialAudio)
.On("OnFrame")
.Order(53)
.RunInEditor()
.Build();

Game::ManagerAPI api;
Expand Down
22 changes: 12 additions & 10 deletions code/addons/graphicsfeature/managers/cameramanager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,20 @@ CameraManager::InitUpdateCameraProcessor()
// Setup processor that handles both worldtransform and camera (heirarchy)
Game::ProcessorBuilder(world, "CameraManager.UpdateCameraWorldTransform")
.Func(
[](Game::World*, Camera const& camera, Game::Position const& parentPosition, Game::Orientation const& parentOrientation)
{
if (IsViewHandleValid(camera.viewHandle))
[](Game::World*, Camera const& camera, Game::Position const& parentPosition, Game::Orientation const& parentOrientation)
{
Graphics::GraphicsEntityId gid = Singleton->viewHandleMap[Ids::Index(camera.viewHandle)].gid;
Camera& settings = Singleton->viewHandleMap[Ids::Index(camera.viewHandle)].currentSettings;
UpdateCameraSettings(gid, settings, camera);
Math::mat4 parentTransform = Math::translation(parentPosition) * Math::rotationquat(parentOrientation);
Graphics::CameraContext::SetView(gid, settings.localTransform * parentTransform);
if (IsViewHandleValid(camera.viewHandle))
{
Graphics::GraphicsEntityId gid = Singleton->viewHandleMap[Ids::Index(camera.viewHandle)].gid;
Camera& settings = Singleton->viewHandleMap[Ids::Index(camera.viewHandle)].currentSettings;
UpdateCameraSettings(gid, settings, camera);
Math::mat4 parentTransform = Math::translation(parentPosition) * Math::rotationquat(parentOrientation);
Graphics::CameraContext::SetView(gid, settings.localTransform * parentTransform);
}
}
}
).Build();
)
.RunInEditor()
.Build();
}

//------------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions code/addons/physicsfeature/managers/physicsmanager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ PhysicsManager::InitPollTransformProcessor()
Game::ProcessorBuilder(world, "PhysicsManager.PassKinematicTransforms"_atm)
.Excluding<Game::Static>()
.On("OnFrame")
.RunInEditor()
.Func(&PassKinematicTransforms)
.Build();
}
Expand Down
2 changes: 2 additions & 0 deletions code/application/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ nebula_begin_module(application)
componentinspection.cc
world.h
world.cc
editorstate.h
editorstate.cc
)
fips_dir(game/messaging)
fips_files(
Expand Down
28 changes: 28 additions & 0 deletions code/application/game/editorstate.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//------------------------------------------------------------------------------
// @file editorstate.cc
// @copyright (C) 2024 Individual contributors, see AUTHORS file
//------------------------------------------------------------------------------
#include "foundation/stdneb.h"
#include "editorstate.h"
namespace Game
{

__ImplementSingleton(Game::EditorState);

//------------------------------------------------------------------------------
/**
*/
EditorState::EditorState()
{
__ConstructSingleton;
}

//------------------------------------------------------------------------------
/**
*/
EditorState::~EditorState()
{
__DestructSingleton;
}

} // namespace Game
34 changes: 34 additions & 0 deletions code/application/game/editorstate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once
//------------------------------------------------------------------------------
/**
Game::EditorState
A state singleton that keeps track of the state of the level editor.
@note Though the editor is an external library, we still need to keep
track of the editors state from the game layer in some way, which
is why this exists.
@copyright
(C) 2024 Individual contributors, see AUTHORS file
*/
//------------------------------------------------------------------------------
#include "core/singleton.h"

namespace Game
{

class EditorState
{
__DeclareSingleton(EditorState)
public:
EditorState();
~EditorState();

/// is true if the editor is running
bool isRunning = false;
/// is true if the editor is currently playing/simulating the game (play-in-editor)
bool isPlaying = false;
};

} // namespace Game
15 changes: 15 additions & 0 deletions code/application/game/frameevent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "world.h"
#include "memdb/database.h"
#include "jobs2/jobs2.h"
#include "editorstate.h"

namespace Game
{
Expand Down Expand Up @@ -270,8 +271,15 @@ FrameEvent::Batch::ExecuteAsync(World* world)
for (IndexT i = 0; i < this->processors.Size(); i++)
{
Processor* processor = this->processors[i];

#ifdef WITH_NEBULA_EDITOR
Game::EditorState* editor = Game::EditorState::Instance();
if (editor && editor->isRunning && !editor->isPlaying && !processor->runInEditor)
continue;
#endif
datasets[i] = world->Query(processor->filter, processor->cache);
numJobs += datasets[i].numViews;

}

if (numJobs == 0)
Expand Down Expand Up @@ -309,6 +317,13 @@ FrameEvent::Batch::ExecuteSequential(World* world)
for (SizeT i = 0; i < this->processors.Size(); i++)
{
Processor* processor = this->processors[i];

#ifdef WITH_NEBULA_EDITOR
Game::EditorState* editor = Game::EditorState::Instance();
if (editor && editor->isRunning && !editor->isPlaying && !processor->runInEditor)
continue;
#endif

Dataset data = world->Query(processor->filter, processor->cache);
for (int v = 0; v < data.numViews; v++)
{
Expand Down
17 changes: 17 additions & 0 deletions code/application/game/processor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ ProcessorBuilder::Order(int order)
return *this;
}

//------------------------------------------------------------------------------
/**
*/
ProcessorBuilder&
ProcessorBuilder::RunInEditor()
{
#ifdef WITH_NEBULA_EDITOR
this->runInEditor = true;
#endif
return *this;
}

//------------------------------------------------------------------------------
/**
*/
Expand All @@ -89,12 +101,17 @@ ProcessorBuilder::Build()
processor->order = this->order;
processor->filter = this->filterBuilder.Build();

#ifdef WITH_NEBULA_EDITOR
processor->runInEditor = this->runInEditor;
#endif

if (this->onlyModified)
processor->callback = this->funcModified;
else
processor->callback = this->func;

FrameEvent* frameEvent = world->GetFramePipeline().GetFrameEvent(this->onEvent);
n_assert(frameEvent != nullptr);
frameEvent->AddProcessor(processor);
return processor;
}
Expand Down
10 changes: 10 additions & 0 deletions code/application/game/processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class Processor
Util::Array<MemDb::TableId> cache;
/// set to false if the cache is invalid
bool cacheValid = false;
#ifdef WITH_NEBULA_EDITOR
/// set to true if you want this processor to always run in editor
bool runInEditor = false;
#endif

private:
friend ProcessorBuilder;
Expand Down Expand Up @@ -159,6 +163,9 @@ class ProcessorBuilder
/// Set the sorting order for the processor
ProcessorBuilder& Order(int order);

/// Processor should always run, even in editor; when the game is paused.
ProcessorBuilder& RunInEditor();

/// Build the processor and attach it to the world
Processor* Build();

Expand All @@ -172,6 +179,9 @@ class ProcessorBuilder
bool async = false;
bool onlyModified = false;
int order = 100;
#ifdef WITH_NEBULA_EDITOR
bool runInEditor = false;
#endif
};

//------------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions code/foundation/core/cvar.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
`ui_` - UI specific
`cl_` - General client/application
`sv_` - Server only (networking)
`ed_` - Editor
@copyright
(C) 2021 Individual contributors, see AUTHORS file
Expand Down
25 changes: 11 additions & 14 deletions toolkit/editor/editor/editor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
#include "tools/pathconverter.h"
#include "io/assignregistry.h"

#include "game/editorstate.h"

namespace Editor
{

//------------------------------------------------------------------------------
/**
*/
EditorState state;
State state;

//------------------------------------------------------------------------------
/**
Expand Down Expand Up @@ -54,6 +56,9 @@ Create()
// Create a command manager with a 20MB buffer
Edit::CommandManager::Create(20_MB);
CreatePathConverter({});

Game::EditorState::Singleton = new Game::EditorState();
Game::EditorState::Instance()->isRunning = true;
}

//------------------------------------------------------------------------------
Expand All @@ -63,7 +68,7 @@ void
Destroy()
{
Edit::CommandManager::Discard();
// empty
delete Game::EditorState::Singleton;
}

//------------------------------------------------------------------------------
Expand All @@ -72,6 +77,7 @@ Destroy()
void
PlayGame()
{
Game::EditorState::Instance()->isPlaying = true;
Game::TimeManager::SetGlobalTimeFactor(1.0f);
}

Expand All @@ -81,27 +87,18 @@ PlayGame()
void
PauseGame()
{
Game::EditorState::Instance()->isPlaying = false;
Game::TimeManager::SetGlobalTimeFactor(0.0f);
}

//------------------------------------------------------------------------------
/**
*/
void
SetTimeScale(float timeScale)
{
if (state.isPlayingGame)
{
Game::TimeManager::SetGlobalTimeFactor(timeScale);
}
}

//------------------------------------------------------------------------------
/**
*/
void
StopGame()
{
Game::EditorState::Instance()->isPlaying = false;

Game::World* gameWorld = Game::GetWorld(WORLD_DEFAULT);
Game::GameServer::Instance()->CleanupWorld(gameWorld);
Game::GameServer::Instance()->SetupEmptyWorld(gameWorld);
Expand Down
6 changes: 2 additions & 4 deletions toolkit/editor/editor/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ struct Editable

};

struct EditorState
struct State
{
/// is set to true if the game is currently playing
bool isPlayingGame = false;
/// contains the world state for the editor
Game::World* editorWorld;
/// maps from editor entity index to editable
Expand All @@ -61,6 +59,6 @@ void PauseGame();
void StopGame();

/// global editor state
extern EditorState state;
extern State state;

} // namespace Editor
21 changes: 20 additions & 1 deletion toolkit/editor/editorfeature/editorfeatureunit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "basegamefeature/components/orientation.h"
#include "basegamefeature/components/scale.h"
#include "graphicsfeature/components/graphicsfeature.h"
#include "lighting/lightcontext.h"
#include "models/modelcontext.h"

namespace EditorFeature
Expand Down Expand Up @@ -67,12 +68,30 @@ EditorFeatureUnit::OnActivate()

Game::World* world = Game::GetWorld(WORLD_DEFAULT);
Game::ProcessorBuilder(world, "EditorGameManager.UpdateModelTransforms"_atm)
.On("OnEndFrame")
.OnlyModified()
.RunInEditor()
.Func(
[](Game::World* world, Game::Position const& pos, Game::Orientation const& orient, Game::Scale const& scale, GraphicsFeature::Model const& model)
{
Math::mat4 worldTransform = Math::trs(pos, orient, scale);
Models::ModelContext::SetTransform(model.graphicsEntityId, worldTransform);
if (Models::ModelContext::IsEntityRegistered(model.graphicsEntityId))
Models::ModelContext::SetTransform(model.graphicsEntityId, worldTransform);
}
)
.Build();

Game::ProcessorBuilder(world, "EditorGameManager.UpdatePointLightPositions"_atm)
.On("OnEndFrame")
.OnlyModified()
.RunInEditor()
.Func(
[](Game::World* world,
Game::Position const& pos,
GraphicsFeature::PointLight const& light)
{
if (Lighting::LightContext::IsEntityRegistered(light.graphicsEntityId))
Lighting::LightContext::SetPosition(light.graphicsEntityId, pos);
}
)
.Build();
Expand Down

0 comments on commit b60ad12

Please sign in to comment.