Parcourir la source

Startup preferences

Josh Engebretson il y a 10 ans
Parent
commit
e2cff3a978

+ 4 - 0
Data/AtomicEditor/Resources/EditorData/AtomicEditor/editor/ui/mainframe.tb.txt

@@ -32,6 +32,10 @@ TBLayout: distribution: gravity, axis: y
 							@include definitions>menubutton
 							text Help
 							id menu help
+						TBButton
+							@include definitions>menubutton
+							text Developer
+							id menu developer
 						TBLayout: gravity: left right
 							TBWidget
 			TBLayout: distribution: gravity, position: top

+ 27 - 0
Source/Atomic/Graphics/OpenGL/OGLGraphics.cpp

@@ -308,6 +308,33 @@ void Graphics::SetWindowPosition(int x, int y)
     SetWindowPosition(IntVector2(x, y));
 }
 
+void Graphics::SetWindowSize(int width, int height)
+{
+    if (impl_->window_)
+    {
+        SDL_SetWindowSize(impl_->window_, width, height);
+        WindowResized();
+    }
+}
+
+void Graphics::CenterWindow()
+{
+    if (impl_->window_)
+    {
+        SDL_DisplayMode mode;
+        SDL_GetDesktopDisplayMode(0, &mode);
+
+        int width, height;
+        SDL_GetWindowSize(impl_->window_, &width, &height);
+
+        int x = mode.w/2 - width/2;
+        int y = mode.h/2 - height/2;
+
+        SetWindowPosition(x, y);
+
+    }
+}
+
 void Graphics::RaiseWindow()
 {
     if (impl_->window_)

+ 5 - 0
Source/Atomic/Graphics/OpenGL/OGLGraphics.h

@@ -94,6 +94,11 @@ public:
     void SetWindowPosition(const IntVector2& position);
     /// Set window position.
     void SetWindowPosition(int x, int y);
+    /// Set window size.
+    void SetWindowSize(int width, int height);
+    /// Center window.
+    void CenterWindow();
+    /// Bring the window to front with focus
     void RaiseWindow();
     /// Set screen mode. Return true if successful.
     bool SetMode(int width, int height, bool fullscreen, bool borderless, bool resizable, bool vsync, bool tripleBuffer, int multiSample);

+ 12 - 0
Source/AtomicEditor/Source/AEApplication.cpp

@@ -37,6 +37,7 @@
 #include "Import/AEJSONSceneProcess.h"
 
 #include "AEEditor.h"
+#include "AEPreferences.h"
 
 #include "AEApplication.h"
 
@@ -57,6 +58,9 @@ void AEApplication::Start()
     // refactor this
     RegisterEnvironmenttLibrary(context_);
 
+    Engine* engine = GetSubsystem<Engine>();
+    engine->SetAutoExit(false);
+
     // Get default style
     ResourceCache* cache = GetSubsystem<ResourceCache>();
     cache->SetAutoReloadResources(true);
@@ -134,6 +138,14 @@ void AEApplication::Setup()
     engineParameters_["WindowResizable"] = true;
     engineParameters_["FullScreen"] = false;
 
+    AEPreferences::StartupPreferences prefs;
+    if (AEPreferences::ReadStartupPrefs(context_, prefs))
+    {
+        engineParameters_["WindowWidth"] = prefs.windowWidth;
+        engineParameters_["WindowHeight"] = prefs.windowHeight;
+    }
+
+
 #ifdef __APPLE__
     engineParameters_["ResourcePrefixPath"] = "../Resources";
 #endif

+ 15 - 0
Source/AtomicEditor/Source/AEEditor.cpp

@@ -4,6 +4,7 @@
 
 #include "AtomicEditor.h"
 
+#include <Atomic/Engine/Engine.h>
 #include <Atomic/IO/FileSystem.h>
 #include <Atomic/Resource/ResourceCache.h>
 #include <Atomic/Input/Input.h>
@@ -59,6 +60,8 @@ Editor::Editor(Context* context) :
     // Create the Main Editor Frame
     mainframe_ = new MainFrame(context_);
 
+    SubscribeToEvent(E_EXITREQUESTED, HANDLER(Editor, HandleExitRequested));
+
     SubscribeToEvent(E_PLAYERERROR, HANDLER(Editor, HandlePlayerError));
     SubscribeToEvent(E_POSTUPDATE, HANDLER(Editor, HandlePostUpdate));
 
@@ -294,6 +297,18 @@ void Editor::HandlePostUpdate(StringHash eventType, VariantMap& eventData)
 
 }
 
+void Editor::HandleExitRequested(StringHash eventType, VariantMap& eventData)
+{
+    if (aepreferences_.NotNull())
+    {
+        aepreferences_->Write();
+    }
+
+    Engine* engine = GetSubsystem<Engine>();
+    engine->Exit();
+}
+
+
 void RegisterEditorLibrary(Context* context)
 {
 

+ 1 - 0
Source/AtomicEditor/Source/AEEditor.h

@@ -70,6 +70,7 @@ private:
     void HandlePlayStarted(StringHash eventType, VariantMap& eventData);
     void HandlePlayStop(StringHash eventType, VariantMap& eventData);
     void HandlePlayStopped(StringHash eventType, VariantMap& eventData);
+    void HandleExitRequested(StringHash eventType, VariantMap& eventData);
 
     SharedPtr<AEPlayer> player_;
     SharedPtr<Project> project_;

+ 101 - 0
Source/AtomicEditor/Source/AEPreferences.cpp

@@ -12,6 +12,7 @@
 #include <Atomic/IO/FileSystem.h>
 #include <Atomic/IO/Log.h>
 #include <Atomic/IO/File.h>
+#include <Atomic/Graphics/Graphics.h>
 
 #include "AEPreferences.h"
 
@@ -101,6 +102,20 @@ void AEPreferences::Write()
     if (!file)
         return;
 
+    Graphics* graphics = GetSubsystem<Graphics>();
+
+    IntVector2 pos(-1, -1);
+
+    int width = -1;
+    int height = -1;
+
+    if (graphics && !graphics->GetFullscreen())
+    {
+        pos = graphics->GetWindowPosition();
+        width = graphics->GetWidth();
+        height = graphics->GetHeight();
+    }
+
     rapidjson::FileStream s(file);
     rapidjson::PrettyWriter<rapidjson::FileStream> writer(s);
 
@@ -116,6 +131,15 @@ void AEPreferences::Write()
     writer.String("android_sdk_path");
     writer.String(androidSDKPath_.CString());
 
+    writer.String("window_pos_x");
+    writer.Int(pos.x_);
+    writer.String("window_pos_y");
+    writer.Int(pos.y_);
+    writer.String("window_width");
+    writer.Int(width);
+    writer.String("window_height");
+    writer.Int(height);
+
     writer.EndObject();
 
     fclose(file);
@@ -161,4 +185,81 @@ void AEPreferences::RegisterRecentProject(const String& fullpath)
 
 }
 
+bool AEPreferences::ReadStartupPrefs(Context *context, StartupPreferences& prefs)
+{
+
+    FileSystem* fileSystem = context->GetSubsystem<FileSystem>();
+    String filepath = fileSystem->GetAppPreferencesDir("AtomicEditor", "Preferences");
+    filepath += "prefs.json";
+
+    if (!fileSystem->FileExists(filepath))
+        return false;
+
+    SharedPtr<File> file(new File(context, filepath, FILE_READ));
+
+    if (!file->IsOpen())
+        return false;
+
+    String json;
+    file->ReadText(json);
+
+    if (!json.Length())
+        return false;
+
+    rapidjson::Document document;
+
+    if (document.Parse<0>(json.CString()).HasParseError())
+    {
+        return false;
+    }
+
+    bool success = true;
+
+    const Value::Member* imember = document.FindMember("window_pos_x");
+    if (imember && imember->value.IsInt())
+    {
+        prefs.windowPos.x_ = imember->value.GetInt();
+    }
+    else
+    {
+        success = false;
+    }
+
+    imember = document.FindMember("window_pos_y");
+    if (imember && imember->value.IsInt())
+    {
+        prefs.windowPos.y_ = imember->value.GetInt();
+    }
+    else
+    {
+        success = false;
+    }
+
+    imember = document.FindMember("window_width");
+    if (imember && imember->value.IsInt())
+    {
+        prefs.windowWidth = imember->value.GetInt();
+    }
+    else
+    {
+        success = false;
+    }
+
+    imember = document.FindMember("window_height");
+    if (imember && imember->value.IsInt())
+    {
+        prefs.windowHeight = imember->value.GetInt();
+    }
+    else
+    {
+        success = false;
+    }
+
+    if (prefs.windowHeight < 128 || prefs.windowWidth < 128)
+        return false;
+
+    return success;
+
+}
+
 }

+ 9 - 0
Source/AtomicEditor/Source/AEPreferences.h

@@ -18,6 +18,13 @@ class AEPreferences : public Object
 
 public:
 
+    struct StartupPreferences
+    {
+        IntVector2 windowPos;
+        int windowWidth;
+        int windowHeight;
+    };
+
     /// Construct.
     AEPreferences(Context* context);
     /// Destruct.
@@ -32,6 +39,8 @@ public:
     void Read();
     void Write();
 
+    static bool ReadStartupPrefs(Context* context, StartupPreferences& prefs);
+
     void UpdateRecentFiles(bool write = true);
 
 private:

+ 21 - 0
Source/AtomicEditor/Source/UI/UIMainFrame.cpp

@@ -95,6 +95,9 @@ MainFrame::MainFrame(Context* context) :
     menuHelpSource.AddItem(new TBGenericStringItem("-"));
     menuHelpSource.AddItem(new TBGenericStringItem("Atomic Game Engine on GitHub", TBIDC("help_github")));
 
+    menuDeveloperSource.AddItem(new TBGenericStringItem("Set 1920x1080 Resolution", TBIDC("developer_resolution")));
+
+
     TBUI* tbui = GetSubsystem<TBUI>();
     tbui->LoadResourceFile(delegate_, "AtomicEditor/editor/ui/mainframe.tb.txt");
 
@@ -530,6 +533,17 @@ bool MainFrame::OnEvent(const TBWidgetEvent &ev)
 
         }
 
+        if (ev.target->GetID() == TBIDC("developer popup"))
+        {
+            if (ev.ref_id == TBIDC("developer_resolution"))
+            {
+                Graphics* graphics = GetSubsystem<Graphics>();
+                graphics->SetWindowSize(1920, 1080);
+                graphics->CenterWindow();
+            }
+        }
+
+
         if (ev.target->GetID() == TBIDC("menu atomic editor"))
         {
             if (TBMenuWindow *menu = new TBMenuWindow(ev.target, TBIDC("atomic editor popup")))
@@ -592,6 +606,13 @@ bool MainFrame::OnEvent(const TBWidgetEvent &ev)
 
             return true;
         }
+        else if (ev.target->GetID() == TBIDC("menu developer"))
+        {
+            if (TBMenuWindow *menu = new TBMenuWindow(ev.target, TBIDC("developer popup")))
+                menu->Show(&menuDeveloperSource, TBPopupAlignment());
+
+            return true;
+        }
 
 
     }

+ 1 - 0
Source/AtomicEditor/Source/UI/UIMainFrame.h

@@ -41,6 +41,7 @@ class MainFrame : public AEWidget
     TBGenericStringItemSource menuResourcesSource;
     TBGenericStringItemSource menuResourcesCreateSource;
     TBGenericStringItemSource menuHelpSource;
+    TBGenericStringItemSource menuDeveloperSource;
 
 public:
     /// Construct.