| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
- // Please see LICENSE.md in repository root for license information
- // https://github.com/AtomicGameEngine/AtomicGameEngine
- #include <rapidjson/document.h>
- #include "rapidjson/prettywriter.h"
- #include "rapidjson/filestream.h"
- #include <Atomic/Core/Context.h>
- #include <Atomic/IO/FileSystem.h>
- #include <Atomic/IO/Log.h>
- #include <Atomic/IO/File.h>
- #include <Atomic/Graphics/Graphics.h>
- #include "../EditorMode/AEEditorEvents.h"
- #include "AEPreferences.h"
- using namespace rapidjson;
- namespace AtomicEditor
- {
- AEPreferences::AEPreferences(Context* context) :
- Object(context)
- {
- context->RegisterSubsystem(this);
- SubscribeToEvent(E_EDITORSHUTDOWN, HANDLER(AEPreferences, HandleEditorShutdown));
- Read();
- }
- AEPreferences::~AEPreferences()
- {
- }
- String AEPreferences::GetPreferencesFullPath()
- {
- FileSystem* fs = GetSubsystem<FileSystem>();
- String filepath = fs->GetAppPreferencesDir("AtomicEditor", "Preferences");
- filepath += "prefs.json";
- return filepath;
- }
- void AEPreferences::Clear()
- {
- recentProjects_.Clear();
- }
- void AEPreferences::Read()
- {
- rapidjson::Document document;
- String filepath = GetPreferencesFullPath();
- File jsonFile(context_, filepath);
- if (!jsonFile.IsOpen())
- return;
- String json;
- jsonFile.ReadText(json);
- if (!json.Length())
- return;
- if (document.Parse<0>(json.CString()).HasParseError())
- {
- LOGERRORF("Could not parse JSON data from %s", filepath.CString());
- return;
- }
- Clear();
- const Value::Member* recent_files = document.FindMember("recent_files");
- if (recent_files && recent_files->value.IsArray())
- {
- for (Value::ConstValueIterator itr = recent_files->value.Begin(); itr != recent_files->value.End(); itr++)
- {
- if (!(*itr).IsString())
- continue;
- String path(itr->GetString());
- recentProjects_.Push(path.CString());
- }
- }
- const Value::Member* android_sdk_path = document.FindMember("android_sdk_path");
- if (android_sdk_path && android_sdk_path->value.IsString())
- androidSDKPath_ = android_sdk_path->value.GetString();
- const Value::Member* jdk_root_path = document.FindMember("jdk_root_path");
- if (jdk_root_path && jdk_root_path->value.IsString())
- jdkRootPath_ = jdk_root_path->value.GetString();
- const Value::Member* ant_path = document.FindMember("ant_path");
- if (ant_path && ant_path->value.IsString())
- antPath_ = ant_path->value.GetString();
- UpdateRecentFiles(false);
- }
- void AEPreferences::Write()
- {
- String filepath = GetPreferencesFullPath();
- FILE* file = fopen(filepath.CString(), "w");
- 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);
- writer.StartObject();
- // recent files
- writer.String("recent_files");
- writer.StartArray();
- for (unsigned i = 0; i < recentProjects_.Size(); i++)
- writer.String(recentProjects_[i].CString());
- writer.EndArray();
- writer.String("android_sdk_path");
- writer.String(androidSDKPath_.CString());
- writer.String("jdk_root_path");
- writer.String(jdkRootPath_.CString());
- writer.String("ant_path");
- writer.String(antPath_.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);
- }
- void AEPreferences::UpdateRecentFiles(bool write)
- {
- FileSystem* fileSystem = GetSubsystem<FileSystem>();
- Vector<String> recentProjects;
- for (unsigned i = 0; i < recentProjects_.Size(); i++)
- {
- String path = recentProjects_[i];
- if (!fileSystem->FileExists(path))
- continue;
- recentProjects.Push(path);
- if (recentProjects.Size() == 10)
- break;
- }
- recentProjects_ = recentProjects;
- if (write)
- Write();
- }
- void AEPreferences::RegisterRecentProject(const String& fullpath)
- {
- if (recentProjects_.Contains(fullpath))
- {
- recentProjects_.Remove(fullpath);
- }
- recentProjects_.Insert(0, fullpath);
- UpdateRecentFiles();
- }
- void AEPreferences::DeleteRecentProjects()
- {
- if (recentProjects_.Size() <= 0) return;
- recentProjects_.Clear();
- UpdateRecentFiles(true);
- }
- 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;
- }
- void AEPreferences::HandleEditorShutdown(StringHash eventType, VariantMap& eventData)
- {
- context_->RemoveSubsystem(GetType());
- }
- }
|