Skip to content

Commit e70f89b

Browse files
committed
xrEditor now can save position of it's panels
1 parent 10573b1 commit e70f89b

File tree

6 files changed

+260
-16
lines changed

6 files changed

+260
-16
lines changed
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
#include "pch.hpp"
2+
#include "DockPanelSerializer.h"
3+
4+
#define COMPANY_NAME "GSC Game World"
5+
#define PRODUCT_NAME "OpenXRay"
6+
7+
using Microsoft::Win32::Registry;
8+
using Microsoft::Win32::RegistryKey;
9+
using Microsoft::Win32::RegistryValueKind;
10+
11+
namespace XRay::Editor::Controls
12+
{
13+
template <typename T>
14+
inline static T RegistryValue(RegistryKey^ key, System::String^ valueId, const T& defaultValue)
15+
{
16+
auto names = key->GetValueNames();
17+
if (names->IndexOf(names, valueId) >= 0)
18+
return ((T)key->GetValue(valueId));
19+
20+
return defaultValue;
21+
}
22+
23+
static RegistryKey^ BaseRegistryKey()
24+
{
25+
auto software = Registry::CurrentUser->OpenSubKey("Software", true);
26+
VERIFY(software);
27+
28+
auto company = software->OpenSubKey(COMPANY_NAME, true);
29+
if (!company)
30+
company = software->CreateSubKey(COMPANY_NAME);
31+
VERIFY(company);
32+
software->Close();
33+
34+
auto product = company->OpenSubKey(PRODUCT_NAME, true);
35+
if (!product)
36+
product = company->CreateSubKey(PRODUCT_NAME);
37+
38+
VERIFY(product);
39+
company->Close();
40+
41+
return product;
42+
}
43+
44+
void Serializer::SerializeDockPanelRoot(Form^ root, DockPanel^ panel, String^ windowName)
45+
{
46+
using System::IO::MemoryStream;
47+
48+
auto product = BaseRegistryKey();
49+
R_ASSERT(product);
50+
51+
auto windows = product->CreateSubKey("Windows");
52+
53+
auto dock_panel = windows->CreateSubKey(windowName);
54+
auto stream = gcnew MemoryStream();
55+
56+
panel->SaveAsXml(stream, System::Text::Encoding::Unicode, true);
57+
stream->Seek(0, System::IO::SeekOrigin::Begin);
58+
dock_panel->SetValue("Panels", stream->ToArray());
59+
delete stream;
60+
61+
dock_panel->SetValue("WindowState", 2);
62+
switch (root->WindowState)
63+
{
64+
case FormWindowState::Maximized:
65+
dock_panel->SetValue("WindowState", 1);
66+
67+
case FormWindowState::Minimized:
68+
{
69+
auto position = dock_panel->CreateSubKey("Position");
70+
position->SetValue("Left", root->RestoreBounds.X);
71+
position->SetValue("Top", root->RestoreBounds.Y);
72+
position->SetValue("Width", root->RestoreBounds.Width);
73+
position->SetValue("Height", root->RestoreBounds.Height);
74+
position->Close();
75+
76+
break;
77+
}
78+
79+
default:
80+
{
81+
dock_panel->SetValue("WindowState", 2);
82+
{
83+
auto position = dock_panel->CreateSubKey("Position");
84+
position->SetValue("Left", root->Location.X);
85+
position->SetValue("Top", root->Location.Y);
86+
position->SetValue("Width", root->Size.Width);
87+
position->SetValue("Height", root->Size.Height);
88+
position->Close();
89+
}
90+
break;
91+
}
92+
}
93+
94+
dock_panel->Close();
95+
96+
windows->Close();
97+
product->Close();
98+
}
99+
100+
bool Serializer::DeserializeDockPanelRoot(Form^ root, DockPanel^ panel, String^ windowName, DeserializeDockContent^ getPanelForSettingCallback)
101+
{
102+
bool isLoadSuccess = false;
103+
root->SuspendLayout();
104+
105+
root->Width = 800;
106+
root->Height = 600;
107+
108+
auto product = BaseRegistryKey();
109+
R_ASSERT(product);
110+
111+
auto windows = product->OpenSubKey("Windows");
112+
if (windows)
113+
{
114+
auto dockPanel = windows->OpenSubKey(windowName);
115+
if (dockPanel)
116+
{
117+
auto temp = dockPanel->GetValue("Panels");
118+
119+
if (temp)
120+
{
121+
auto object = safe_cast<System::Array^>(dockPanel->GetValue("Panels"));
122+
123+
windows->Close();
124+
delete windows;
125+
126+
product->Close();
127+
delete product;
128+
129+
auto stream = gcnew System::IO::MemoryStream();
130+
stream->Write(safe_cast<array<unsigned char, 1>^>(object), 0, object->Length);
131+
stream->Seek(0, System::IO::SeekOrigin::Begin);
132+
panel->LoadFromXml(stream, getPanelForSettingCallback);
133+
root->ResumeLayout();
134+
isLoadSuccess = true;
135+
}
136+
137+
auto position = dockPanel->OpenSubKey("Position");
138+
if (position)
139+
{
140+
root->Left = (int)RegistryValue(position, "Left", root->Left);
141+
root->Top = (int)RegistryValue(position, "Top", root->Top);
142+
root->Width = (int)RegistryValue(position, "Width", root->Width);
143+
root->Height = (int)RegistryValue(position, "Height", root->Height);
144+
145+
root->Location = Point(root->Left, root->Top);
146+
147+
position->Close();
148+
}
149+
150+
switch ((int)RegistryValue(dockPanel, "WindowState", 2))
151+
{
152+
case 1:
153+
{
154+
root->WindowState = FormWindowState::Maximized;
155+
break;
156+
}
157+
158+
case 2:
159+
{
160+
root->WindowState = FormWindowState::Normal;
161+
break;
162+
}
163+
164+
default: NODEFAULT;
165+
}
166+
167+
dockPanel->Close();
168+
}
169+
windows->Close();
170+
delete windows;
171+
}
172+
173+
product->Close();
174+
delete product;
175+
176+
root->ResumeLayout();
177+
178+
return isLoadSuccess;
179+
}
180+
181+
} // namespace XRay::Editor::Controls::Serializer
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
3+
namespace XRay
4+
{
5+
namespace Editor
6+
{
7+
namespace Controls
8+
{
9+
using namespace System;
10+
using namespace System::Windows::Forms;
11+
using namespace System::Drawing;
12+
using namespace WeifenLuo::WinFormsUI::Docking;
13+
14+
public ref class Serializer
15+
{
16+
public:
17+
static void SerializeDockPanelRoot(Form^ root, DockPanel^ panel, String^ windowName);
18+
static bool DeserializeDockPanelRoot(Form^ root, DockPanel^ panel, String^ windowName, DeserializeDockContent^ getPanelForSettingCallback);
19+
};
20+
} // namespace XRay::Editor::Controls::Serializer
21+
}
22+
}

src/editors/xrECore/Props/WindowIDE.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "WindowIDE.h"
33
#include "WindowView.h"
44
#include "WindowLog.h"
5+
#include "DockPanelSerializer.h"
56

67
namespace XRay::ECore::Props
78
{
@@ -10,10 +11,33 @@ void WindowIDE::Initialize()
1011
windowView = gcnew WindowView();
1112
windowLog = gcnew WindowLog();
1213

13-
windowView->Show(editorDock, WeifenLuo::WinFormsUI::Docking::DockState::Document);
14-
windowLog->Show(editorDock, WeifenLuo::WinFormsUI::Docking::DockState::DockBottomAutoHide);
14+
if (!Editor::Controls::Serializer::DeserializeDockPanelRoot(
15+
this, editorDock, this->Text,
16+
gcnew WeifenLuo::WinFormsUI::Docking::DeserializeDockContent(this, &WindowIDE::reloadContent)))
17+
{
18+
windowView->Show(editorDock, WeifenLuo::WinFormsUI::Docking::DockState::Document);
19+
windowLog->Show(editorDock, WeifenLuo::WinFormsUI::Docking::DockState::DockBottomAutoHide);
20+
}
1521

1622
auto cb = LogCallback(ELogCallback, windowLog->Handle.ToPointer());
1723
SetLogCB(cb);
1824
}
25+
26+
System::Void WindowIDE::WindowIDE_FormClosing(System::Object^ sender, System::Windows::Forms::FormClosingEventArgs^ e)
27+
{
28+
Editor::Controls::Serializer::SerializeDockPanelRoot(this, editorDock, this->Text);
29+
}
30+
31+
WeifenLuo::WinFormsUI::Docking::IDockContent^ WindowIDE::reloadContent(System::String^ persistString)
32+
{
33+
WeifenLuo::WinFormsUI::Docking::IDockContent^ result = nullptr;
34+
35+
if (persistString == windowView->GetType()->ToString())
36+
result = windowView;
37+
38+
if (persistString == windowLog->GetType()->ToString())
39+
result = windowLog;
40+
41+
return result;
42+
}
1943
} // namespace XRay::ECore::Props

src/editors/xrECore/Props/WindowIDE.h

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,27 @@ using namespace System::Drawing;
2929
public ref class WindowIDE : public System::Windows::Forms::Form
3030
{
3131
public:
32-
WindowIDE(void)
33-
{
34-
InitializeComponent();
32+
WindowIDE(void)
33+
{
34+
InitializeComponent();
3535
Initialize();
36-
}
36+
}
3737

3838
protected:
39-
~WindowIDE()
40-
{
41-
if (components)
42-
{
43-
delete components;
44-
}
45-
}
39+
~WindowIDE()
40+
{
41+
if (components)
42+
{
43+
delete components;
44+
}
45+
}
4646

4747
private:
4848
void Initialize();
49-
49+
WeifenLuo::WinFormsUI::Docking::IDockContent^ reloadContent(System::String^ persistString);
50+
51+
private: System::Void WindowIDE_FormClosing(System::Object^ sender, System::Windows::Forms::FormClosingEventArgs^ e);
52+
5053
private: WeifenLuo::WinFormsUI::Docking::DockPanel^ editorDock;
5154
private: WeifenLuo::WinFormsUI::Docking::VS2015LightTheme^ editorTheme;
5255
private: WindowView^ windowView;
@@ -56,8 +59,9 @@ private: WindowLog^ windowLog;
5659
private: System::ComponentModel::Container^ components;
5760

5861
#pragma region Windows Form Designer generated code
59-
void InitializeComponent(void)
60-
{
62+
void InitializeComponent(void)
63+
{
64+
System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(WindowIDE::typeid));
6165
this->editorTheme = (gcnew WeifenLuo::WinFormsUI::Docking::VS2015LightTheme());
6266
this->editorDock = (gcnew WeifenLuo::WinFormsUI::Docking::DockPanel());
6367
this->SuspendLayout();
@@ -76,8 +80,10 @@ private: System::ComponentModel::Container^ components;
7680
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
7781
this->ClientSize = System::Drawing::Size(284, 264);
7882
this->Controls->Add(this->editorDock);
83+
this->Icon = (cli::safe_cast<System::Drawing::Icon^>(resources->GetObject(L"$this.Icon")));
7984
this->Name = L"WindowIDE";
8085
this->Text = L"OpenXRay Editor";
86+
this->FormClosing += gcnew System::Windows::Forms::FormClosingEventHandler(this, &WindowIDE::WindowIDE_FormClosing);
8187
this->ResumeLayout(false);
8288

8389
}

src/editors/xrECore/xrECore.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
<ClCompile Include="Core\UI_MainCommand.cpp">
8585
<ExcludedFromBuild>true</ExcludedFromBuild>
8686
</ClCompile>
87+
<ClCompile Include="Props\DockPanelSerializer.cpp" />
8788
<ClCompile Include="Props\WindowIDE.cpp" />
8889
<ClCompile Include="Props\WindowLog.cpp" />
8990
<ClCompile Include="pch.cpp">
@@ -105,6 +106,7 @@
105106
<ClInclude Include="Core\ELog.h" />
106107
<ClInclude Include="Core\Token.h" />
107108
<ClInclude Include="Core\UI_MainCommand.h" />
109+
<ClInclude Include="Props\DockPanelSerializer.h" />
108110
<ClInclude Include="Props\WindowIDE.h">
109111
<FileType>CppForm</FileType>
110112
</ClInclude>

src/editors/xrECore/xrECore.vcxproj.filters

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
<Filter Include="Props &amp; Windows\WindowIDE">
4444
<UniqueIdentifier>{417ebcd4-5c96-4d26-b3aa-f634ec4eb59d}</UniqueIdentifier>
4545
</Filter>
46+
<Filter Include="Props &amp; Windows\DockPanelSerializer">
47+
<UniqueIdentifier>{7f8665bb-1582-4101-a123-3e0a52973049}</UniqueIdentifier>
48+
</Filter>
4649
</ItemGroup>
4750
<ItemGroup>
4851
<ClCompile Include="pch.cpp" />
@@ -91,6 +94,9 @@
9194
<ClCompile Include="Props\WindowLog.cpp">
9295
<Filter>Props &amp; Windows\WindowLog</Filter>
9396
</ClCompile>
97+
<ClCompile Include="Props\DockPanelSerializer.cpp">
98+
<Filter>Props &amp; Windows\DockPanelSerializer</Filter>
99+
</ClCompile>
94100
</ItemGroup>
95101
<ItemGroup>
96102
<ClInclude Include="pch.hpp" />
@@ -142,6 +148,9 @@
142148
<ClInclude Include="Props\WindowLog.h">
143149
<Filter>Props &amp; Windows\WindowLog</Filter>
144150
</ClInclude>
151+
<ClInclude Include="Props\DockPanelSerializer.h">
152+
<Filter>Props &amp; Windows\DockPanelSerializer</Filter>
153+
</ClInclude>
145154
</ItemGroup>
146155
<ItemGroup>
147156
<EmbeddedResource Include="Props\ItemList.resx">

0 commit comments

Comments
 (0)