Skip to content

Commit

Permalink
Menu command New Scene on Editor
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Oct 4, 2024
1 parent 6f7aef5 commit 863bc06
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 16 deletions.
71 changes: 60 additions & 11 deletions Projects/Editor/Source/Editor/CEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,8 @@ namespace Skylicht

GUI::CMenuItem* file = m_menuBar->addItem(L"File");
submenu = file->getMenu();
submenu->addItem(L"New scene");
submenu->addSeparator();
submenu->addItem(L"Save", GUI::ESystemIcon::Save, L"Ctrl + S");
submenu->addItem(L"Save As");
submenu->OnOpen = BIND_LISTENER(&CEditor::OnOpenMenuFile, this);
Expand Down Expand Up @@ -1043,6 +1045,11 @@ namespace Skylicht
}
}

void CEditor::OnMenuNewScene(GUI::CBase* item)
{
CSceneController::getInstance()->newScene();
}

void CEditor::OnMenuSave(GUI::CBase* item)
{
bool saveScene = true;
Expand All @@ -1068,6 +1075,50 @@ namespace Skylicht
}
}

void CEditor::OnMenuSaveAs(GUI::CBase* item)
{
bool saveScene = true;
bool saveCanvas = false;

CSelectObject* selectObject = CSelection::getInstance()->getLastSelected();
if (selectObject != NULL)
{
if (selectObject->getType() == CSelectObject::GUIElement)
{
saveCanvas = true;
saveScene = false;
}
}

std::string assetFolder = CAssetManager::getInstance()->getAssetFolder();

if (saveScene)
{
GUI::COpenSaveDialog* dialog = new GUI::COpenSaveDialog(m_canvas,
GUI::COpenSaveDialog::SaveAs,
assetFolder.c_str(),
assetFolder.c_str(),
"scene;*");
dialog->OnSave = [&](std::string path)
{
CSceneController::getInstance()->save(path.c_str());
};
}
else if (saveCanvas)
{
GUI::COpenSaveDialog* dialog = new GUI::COpenSaveDialog(m_canvas,
GUI::COpenSaveDialog::Save,
assetFolder.c_str(),
assetFolder.c_str(),
"gui;*"
);
dialog->OnSave = [&](std::string path)
{
CGUIDesignController::getInstance()->save(path.c_str());
};
}
}

void CEditor::onSaveScene()
{
std::string assetFolder = CAssetManager::getInstance()->getAssetFolder();
Expand Down Expand Up @@ -1110,17 +1161,17 @@ namespace Skylicht
"gui;*"
);

dialog->OnSave = [&, controller = guiDesignController](std::string path)
dialog->OnSave = [&](std::string path)
{
controller->save(path.c_str());
CGUIDesignController::getInstance()->save(path.c_str());
};
}
else
{
guiDesignController->save(fileName.c_str());
}
}

void CEditor::OnCommandLogo(GUI::CBase* item)
{
GUI::CMenuItem* menuItem = dynamic_cast<GUI::CMenuItem*>(item);
Expand Down Expand Up @@ -1152,19 +1203,17 @@ namespace Skylicht
GUI::CMenuItem* menuItem = dynamic_cast<GUI::CMenuItem*>(item);
const std::wstring& label = menuItem->getLabel();

if (label == L"Save")
if (label == L"New scene")
{
OnMenuNewScene(item);
}
else if (label == L"Save")
{
OnMenuSave(item);
}
else if (label == L"Save As")
{
std::string assetFolder = CAssetManager::getInstance()->getAssetFolder();

GUI::COpenSaveDialog* dialog = new GUI::COpenSaveDialog(m_canvas, GUI::COpenSaveDialog::SaveAs, assetFolder.c_str(), assetFolder.c_str(), "scene;*");
dialog->OnSave = [&, controller = CSceneController::getInstance()](std::string path)
{
controller->save(path.c_str());
};
OnMenuSaveAs(item);
}
}

Expand Down
8 changes: 6 additions & 2 deletions Projects/Editor/Source/Editor/CEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,12 @@ namespace Skylicht

void OnMenuQuit(GUI::CBase* item);

void OnMenuNewScene(GUI::CBase* item);

void OnMenuSave(GUI::CBase* item);

void OnMenuSaveAs(GUI::CBase* item);

void OnCommandLogo(GUI::CBase* item);

void OnOpenMenuFile(GUI::CBase* item);
Expand All @@ -177,9 +181,9 @@ namespace Skylicht
public:

void onSaveScene();

void onSaveGUICanvas();

void showProjectSetting();

void showGoogleMap();
Expand Down
2 changes: 1 addition & 1 deletion Projects/Editor/Source/Editor/Space/Scene/CSpaceScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ namespace Skylicht
root->setDock(EGUIDock::NoDock);
root->setRect(core::rectf(0.0f, 0.0f, 1920.0f, 1080.0f));

// camera
// camera
camObj = zone->createEmptyObject();
camObj->setName(L"GUICamera");
camObj->setEditorObject(true);
Expand Down
87 changes: 85 additions & 2 deletions Projects/Editor/Source/Editor/SpaceController/CSceneController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,78 @@ namespace Skylicht
}
}

void CSceneController::doLoadScene(const std::string& path)
void CSceneController::newScene()
{
m_scenePath = path;
bool modify = true;
if (modify)
{
GUI::CMessageBox* msgBox = new GUI::CMessageBox(m_canvas, GUI::CMessageBox::YesNoCancel);
msgBox->setMessage(L"Do you want to save current scene?", m_scene->getName());
msgBox->OnYes = [&](GUI::CBase* button) {
if (m_scenePath.empty() == true)
{
std::string assetFolder = CAssetManager::getInstance()->getAssetFolder();
GUI::COpenSaveDialog* dialog = new GUI::COpenSaveDialog(m_canvas, GUI::COpenSaveDialog::Save, assetFolder.c_str(), assetFolder.c_str(), "scene;*");
dialog->OnSave = [&](std::string path)
{
save(path.c_str());
};
}
else
{
save(m_scenePath.c_str());
doNewScene();
}
};
msgBox->OnNo = [&](GUI::CBase* button) {
doNewScene();
};
}
else
{
doNewScene();
}
}

void CSceneController::doNewScene()
{
m_scenePath.clear();

// clear current scene gui
CSelection::getInstance()->clear();
CSelecting::getInstance()->end();

CPropertyController::getInstance()->setProperty(NULL);

CHandles::getInstance()->end();
CHandles::getInstance()->setNullRenderer();

m_gizmos = NULL;

if (m_history)
{
delete m_history;
m_history = NULL;
}

if (m_spaceHierarchy != NULL)
{
m_spaceHierarchy->deleteHierarchyNode();
m_hierachyNode = NULL;
}

deleteScene();

if (m_spaceScene == NULL)
{
return;
}

m_spaceScene->initDefaultScene();
}

void CSceneController::doLoadScene(const std::string& path)
{
// clear current scene gui
CSelection::getInstance()->clear();
CSelecting::getInstance()->end();
Expand All @@ -182,6 +250,14 @@ namespace Skylicht
m_hierachyNode = NULL;
}

if (m_spaceScene == NULL)
{
deleteScene();
return;
}

m_scenePath = path;

// create new scene
m_scene = m_spaceScene->initNullScene();

Expand Down Expand Up @@ -946,6 +1022,13 @@ namespace Skylicht
propertyController->setProperty(NULL);
}
}
else
{
// remove last observer
CSelectObject* selectedObject = selection->getLastSelected();
if (selectedObject != NULL)
selectedObject->removeAllObserver();
}
}

void CSceneController::onObjectChange(CGameObject* object)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ namespace Skylicht

virtual void loadFile(const std::string& path);

void newScene();

void doNewScene();

void doLoadScene(const std::string& path);

void doFinishLoadScene();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace Skylicht
{
for (auto it : m_groups)
{
it.first->InstancingGroup = NULL;
delete it.second;
}
m_groups.clear();
Expand Down

0 comments on commit 863bc06

Please sign in to comment.