Skip to content

Commit 1224b38

Browse files
committed
#158 Implement save GUI to file
1 parent 9e385fe commit 1224b38

File tree

6 files changed

+132
-34
lines changed

6 files changed

+132
-34
lines changed

Projects/Editor/Source/Editor/CEditor.cpp

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,22 +1016,69 @@ namespace Skylicht
10161016

10171017
void CEditor::OnMenuSave(GUI::CBase* item)
10181018
{
1019-
CSceneController* sceneController = CSceneController::getInstance();
1020-
const std::string& fileName = sceneController->getScenePath();
1019+
bool saveScene = true;
1020+
bool saveCanvas = false;
10211021

1022-
if (fileName.empty())
1022+
CSelectObject* selectObject = CSelection::getInstance()->getLastSelected();
1023+
if (selectObject != NULL)
10231024
{
1024-
std::string assetFolder = CAssetManager::getInstance()->getAssetFolder();
1025+
if (selectObject->getType() == CSelectObject::GUIElement)
1026+
{
1027+
saveCanvas = true;
1028+
saveScene = false;
1029+
}
1030+
}
1031+
1032+
std::string assetFolder = CAssetManager::getInstance()->getAssetFolder();
1033+
1034+
if (saveScene)
1035+
{
1036+
CSceneController* sceneController = CSceneController::getInstance();
1037+
const std::string& fileName = sceneController->getScenePath();
10251038

1026-
GUI::COpenSaveDialog* dialog = new GUI::COpenSaveDialog(m_canvas, GUI::COpenSaveDialog::Save, assetFolder.c_str(), assetFolder.c_str(), "scene;*");
1027-
dialog->OnSave = [&, controller = sceneController](std::string path)
1039+
if (fileName.empty())
10281040
{
1029-
controller->save(path.c_str());
1030-
};
1041+
GUI::COpenSaveDialog* dialog = new GUI::COpenSaveDialog(m_canvas,
1042+
GUI::COpenSaveDialog::Save,
1043+
assetFolder.c_str(),
1044+
assetFolder.c_str(),
1045+
"scene;*"
1046+
);
1047+
1048+
dialog->OnSave = [&, controller = sceneController](std::string path)
1049+
{
1050+
controller->save(path.c_str());
1051+
};
1052+
}
1053+
else
1054+
{
1055+
sceneController->save(fileName.c_str());
1056+
}
10311057
}
1032-
else
1058+
else if (saveCanvas)
10331059
{
1034-
sceneController->save(fileName.c_str());
1060+
CGUIDesignController* guiDesignController = CGUIDesignController::getInstance();
1061+
1062+
const std::string& fileName = guiDesignController->getSaveGUIPath();
1063+
1064+
if (fileName.empty())
1065+
{
1066+
GUI::COpenSaveDialog* dialog = new GUI::COpenSaveDialog(m_canvas,
1067+
GUI::COpenSaveDialog::Save,
1068+
assetFolder.c_str(),
1069+
assetFolder.c_str(),
1070+
"gui;*"
1071+
);
1072+
1073+
dialog->OnSave = [&, controller = guiDesignController](std::string path)
1074+
{
1075+
controller->save(path.c_str());
1076+
};
1077+
}
1078+
else
1079+
{
1080+
guiDesignController->save(fileName.c_str());
1081+
}
10351082
}
10361083
}
10371084

Projects/Editor/Source/Editor/SpaceController/CGUIDesignController.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ This file is part of the "Skylicht Engine".
3131
#include "CSceneController.h"
3232
#include "Selection/CSelection.h"
3333
#include "Graphics2D/CCanvas.h"
34+
#include "Graphics2D/CGUIExporter.h"
35+
#include "Graphics2D/CGUIImporter.h"
3436

3537
namespace Skylicht
3638
{
@@ -361,6 +363,15 @@ namespace Skylicht
361363
}
362364
}
363365

366+
void CGUIDesignController::save(const char* path)
367+
{
368+
// Save the canvas infomation to file
369+
if (CGUIExporter::save(path, m_guiCanvas))
370+
{
371+
m_canvasPath = path;
372+
}
373+
}
374+
364375
void CGUIDesignController::loadFile(const std::string& path)
365376
{
366377
CEditor* editor = CEditor::getInstance();

Projects/Editor/Source/Editor/SpaceController/CGUIDesignController.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ namespace Skylicht
5757

5858
CContextMenuGUIElement* m_contextMenu;
5959

60+
std::string m_canvasPath;
61+
6062
public:
6163
CGUIDesignController();
6264

@@ -84,6 +86,13 @@ namespace Skylicht
8486

8587
void initContextMenu(GUI::CCanvas* canvas);
8688

89+
inline const std::string& getSaveGUIPath()
90+
{
91+
return m_canvasPath;
92+
}
93+
94+
void save(const char* path);
95+
8796
virtual void loadFile(const std::string& path);
8897

8998
void rebuildGUIHierachy();

Projects/Skylicht/Engine/Source/Graphics2D/CGUIExporter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ namespace Skylicht
5959
for (CGUIElement* element : childs)
6060
{
6161
CObjectSerializable* object = element->createSerializable();
62+
parents->addProperty(object);
63+
parents->autoRelease(object);
6264

6365
if (element->getChilds().size() > 0)
6466
{

Projects/Skylicht/Engine/Source/Graphics2D/GUI/CGUIText.cpp

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ namespace Skylicht
4242
m_updateTextRender(true),
4343
m_font(NULL),
4444
m_customfont(font),
45-
m_fontData(NULL)
45+
m_fontData(NULL),
46+
m_lastWidth(0.0f),
47+
m_lastHeight(0.0f)
4648
{
4749

4850
}
@@ -59,7 +61,9 @@ namespace Skylicht
5961
m_updateTextRender(true),
6062
m_font(NULL),
6163
m_customfont(font),
62-
m_fontData(NULL)
64+
m_fontData(NULL),
65+
m_lastWidth(0.0f),
66+
m_lastHeight(0.0f)
6367
{
6468
init();
6569
}
@@ -402,6 +406,15 @@ namespace Skylicht
402406
m_font = font;
403407
}
404408

409+
const core::rectf& rect = getRect();
410+
411+
if (m_lastWidth != rect.getWidth() || m_lastHeight != rect.getHeight())
412+
{
413+
m_lastWidth = rect.getWidth();
414+
m_lastHeight = rect.getHeight();
415+
m_updateTextRender = true;
416+
}
417+
405418
if (m_updateTextRender == true)
406419
{
407420
// encode string to list modules & format
@@ -411,7 +424,7 @@ namespace Skylicht
411424
if (m_multiLine == true)
412425
{
413426
// split to multi line
414-
splitText(m_arrayCharRender, m_arrayCharFormat, (int)getRect().getWidth());
427+
splitText(m_arrayCharRender, m_arrayCharFormat, (int)m_lastWidth);
415428
}
416429
else
417430
{
@@ -451,9 +464,9 @@ namespace Skylicht
451464

452465
// calc text algin vertial
453466
if (TextVertical == EGUIVerticalAlign::Middle)
454-
y = ((int)getRect().getHeight() - textHeight - m_textOffsetY) / 2;
467+
y = ((int)m_lastHeight - textHeight - m_textOffsetY) / 2;
455468
else if (TextVertical == EGUIVerticalAlign::Bottom)
456-
y = (int)getRect().getHeight() - textHeight;
469+
y = (int)m_lastHeight - textHeight;
457470

458471
if (m_centerRotate == true)
459472
y = -textHeight / 2;
@@ -684,29 +697,33 @@ namespace Skylicht
684697
CObjectSerializable* CGUIText::createSerializable()
685698
{
686699
CObjectSerializable* object = CGUIElement::createSerializable();
687-
object->autoRelease(new CBoolProperty(object, "Multiline", m_multiLine));
688-
object->autoRelease(new CIntProperty(object, "Char padding", m_charPadding));
689-
object->autoRelease(new CIntProperty(object, "Char space padding", m_charSpacePadding));
690-
object->autoRelease(new CIntProperty(object, "Line padding", m_linePadding));
700+
object->autoRelease(new CBoolProperty(object, "multiline", m_multiLine));
701+
object->autoRelease(new CIntProperty(object, "charPadding", m_charPadding));
702+
object->autoRelease(new CIntProperty(object, "charSpacePadding", m_charSpacePadding));
703+
object->autoRelease(new CIntProperty(object, "linePadding", m_linePadding));
691704

692-
CEnumProperty<EGUIVerticalAlign>* verticalAlign = new CEnumProperty<EGUIVerticalAlign>(object, "Text Verticle", TextVertical);
705+
CEnumProperty<EGUIVerticalAlign>* verticalAlign = new CEnumProperty<EGUIVerticalAlign>(object, "textVerticle", TextVertical);
693706
verticalAlign->addEnumString("Top", EGUIVerticalAlign::Top);
694707
verticalAlign->addEnumString("Middle", EGUIVerticalAlign::Middle);
695708
verticalAlign->addEnumString("Bottom", EGUIVerticalAlign::Bottom);
696709
object->autoRelease(verticalAlign);
697710

698-
CEnumProperty<EGUIHorizontalAlign>* horizontalAlign = new CEnumProperty<EGUIHorizontalAlign>(object, "Text Horizontal", TextHorizontal);
711+
CEnumProperty<EGUIHorizontalAlign>* horizontalAlign = new CEnumProperty<EGUIHorizontalAlign>(object, "textHorizontal", TextHorizontal);
699712
horizontalAlign->addEnumString("Left", EGUIHorizontalAlign::Left);
700713
horizontalAlign->addEnumString("Center", EGUIHorizontalAlign::Center);
701714
horizontalAlign->addEnumString("Right", EGUIHorizontalAlign::Right);
702715
object->autoRelease(horizontalAlign);
703716

704-
object->autoRelease(new CFilePathProperty(object, "Font", m_fontSource.c_str(), "font"));
705-
CStringProperty* fontGUID = new CStringProperty(object, "FontGUID", m_fontGUID.c_str());
717+
object->autoRelease(new CFilePathProperty(object, "font", m_fontSource.c_str(), "font"));
718+
719+
if (m_fontData)
720+
m_fontGUID = m_fontData->getGUID();
721+
722+
CStringProperty* fontGUID = new CStringProperty(object, "font.guid", m_fontGUID.c_str());
706723
fontGUID->setHidden(true);
707724
object->autoRelease(fontGUID);
708725

709-
object->autoRelease(new CStringProperty(object, "Text", m_text.c_str()));
726+
object->autoRelease(new CStringProperty(object, "text", m_text.c_str()));
710727

711728
return object;
712729
}
@@ -715,22 +732,32 @@ namespace Skylicht
715732
{
716733
CGUIElement::loadSerializable(object);
717734

718-
m_multiLine = object->get<bool>("Multiline", true);
719-
m_charPadding = object->get<int>("Char padding", 0);
720-
m_charSpacePadding = object->get<int>("Char space padding", 0);
721-
m_linePadding = object->get<int>("Line padding", 0);
735+
m_multiLine = object->get<bool>("multiline", true);
736+
m_charPadding = object->get<int>("charPadding", 0);
737+
m_charSpacePadding = object->get<int>("charSpacePadding", 0);
738+
m_linePadding = object->get<int>("linePadding", 0);
739+
740+
TextVertical = object->get<EGUIVerticalAlign>("textVerticle", EGUIVerticalAlign::Top);
741+
TextHorizontal = object->get<EGUIHorizontalAlign>("textHorizontal", EGUIHorizontalAlign::Left);
722742

723-
TextVertical = object->get<EGUIVerticalAlign>("Text Verticle", EGUIVerticalAlign::Top);
724-
TextHorizontal = object->get<EGUIHorizontalAlign>("Text Horizontal", EGUIHorizontalAlign::Left);
743+
m_fontSource = object->get<std::string>("font", std::string(""));
744+
m_fontGUID = object->get<std::string>("font.guid", std::string(""));
725745

726-
m_fontSource = object->get<std::string>("Font", std::string(""));
727-
m_fontGUID = object->get<std::string>("Font", std::string(""));
746+
// read font by id first
747+
m_fontData = CFontManager::getInstance()->getFontById(m_fontGUID.c_str());
748+
if (!m_fontData)
749+
{
750+
m_fontData = CFontManager::getInstance()->loadFontSource(m_fontSource.c_str());
751+
}
728752

729-
m_fontData = CFontManager::getInstance()->loadFontSource(m_fontSource.c_str());
753+
// init font
730754
if (m_fontData)
755+
{
731756
m_fontData->initFont();
757+
m_fontGUID = m_fontData->getGUID();
758+
}
732759

733-
std::string value = object->get<std::string>("Text", std::string(""));
760+
std::string value = object->get<std::string>("text", std::string(""));
734761
setText(value.c_str());
735762

736763
m_updateTextRender = true;

Projects/Skylicht/Engine/Source/Graphics2D/GUI/CGUIText.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ namespace Skylicht
7474

7575
CFontSource* m_fontData;
7676

77+
float m_lastWidth;
78+
float m_lastHeight;
7779
protected:
7880
CGUIText(CCanvas* canvas, CGUIElement* parent, IFont* font);
7981
CGUIText(CCanvas* canvas, CGUIElement* parent, const core::rectf& rect, IFont* font);

0 commit comments

Comments
 (0)