ProjectUserPrefs.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "ProjectEvents.h"
  23. #include "ProjectUserPrefs.h"
  24. #include <Atomic/IO/File.h>
  25. #include <Atomic/Resource/JSONFile.h>
  26. namespace ToolCore
  27. {
  28. ProjectUserPrefs::ProjectUserPrefs(Context* context) : Object(context),
  29. snapTranslationX_(1.0f),
  30. snapTranslationY_(1.0f),
  31. snapTranslationZ_(1.0f),
  32. snapRotation_(15.0f),
  33. snapScale_(0.1f)
  34. {
  35. #ifdef ATOMIC_PLATFORM_OSX
  36. defaultPlatform_ = PLATFORMID_MAC;
  37. #else
  38. defaultPlatform_ = PLATFORMID_WINDOWS;
  39. #endif
  40. }
  41. ProjectUserPrefs::~ProjectUserPrefs()
  42. {
  43. }
  44. float ProjectUserPrefs::GetSnapTranslationX() const
  45. {
  46. return snapTranslationX_;
  47. }
  48. float ProjectUserPrefs::GetSnapTranslationY() const
  49. {
  50. return snapTranslationY_;
  51. }
  52. float ProjectUserPrefs::GetSnapTranslationZ() const
  53. {
  54. return snapTranslationZ_;
  55. }
  56. float ProjectUserPrefs::GetSnapRotation() const
  57. {
  58. return snapRotation_;
  59. }
  60. float ProjectUserPrefs::GetSnapScale() const
  61. {
  62. return snapScale_;
  63. }
  64. void ProjectUserPrefs::SetSnapTranslationX(float value)
  65. {
  66. snapTranslationX_ = value;
  67. }
  68. void ProjectUserPrefs::SetSnapTranslationY(float value)
  69. {
  70. snapTranslationY_ = value;
  71. }
  72. void ProjectUserPrefs::SetSnapTranslationZ(float value)
  73. {
  74. snapTranslationZ_ = value;
  75. }
  76. void ProjectUserPrefs::SetSnapRotation(float value)
  77. {
  78. snapRotation_ = value;
  79. }
  80. void ProjectUserPrefs::SetSnapScale(float value)
  81. {
  82. snapScale_ = value;
  83. }
  84. bool ProjectUserPrefs::Load(const String& path)
  85. {
  86. SharedPtr<File> file(new File(context_, path));
  87. if (!file->IsOpen())
  88. return false;
  89. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  90. bool result = jsonFile->Load(*file);
  91. file->Close();
  92. if (!result)
  93. return false;
  94. JSONValue& root = jsonFile->GetRoot();
  95. if (!root.IsObject())
  96. return false;
  97. lastBuildPath_ = root.Get("lastBuildPath").GetString();
  98. if (root.Contains("snapTransX"))
  99. SetSnapTranslationX(root.Get("snapTransX").GetFloat());
  100. if (root.Contains("snapTransY"))
  101. SetSnapTranslationY(root.Get("snapTransY").GetFloat());
  102. if (root.Contains("snapTransZ"))
  103. SetSnapTranslationZ(root.Get("snapTransZ").GetFloat());
  104. if (root.Contains("snapRotation"))
  105. SetSnapRotation(root.Get("snapRotation").GetFloat());
  106. if (root.Contains("snapScale"))
  107. SetSnapScale(root.Get("snapScale").GetFloat());
  108. return true;
  109. }
  110. void ProjectUserPrefs::Save(const String& path)
  111. {
  112. SharedPtr<JSONFile> jsonFile(new JSONFile(context_));
  113. // Load existing prefs file if it exists and just update editor settings. The
  114. // file may contain other settings we are not aware of so we shouldn't overwrite those
  115. SharedPtr<File> oldFile(new File(context_, path));
  116. if (oldFile->IsOpen())
  117. {
  118. bool result = jsonFile->Load(*oldFile);
  119. oldFile->Close();
  120. }
  121. JSONValue& root = jsonFile->GetRoot();
  122. SharedPtr<File> file(new File(context_, path, FILE_WRITE));
  123. root.Set("lastBuildPath", lastBuildPath_);
  124. // Snap settings
  125. root.Set("snapTransX", snapTranslationX_);
  126. root.Set("snapTransY", snapTranslationY_);
  127. root.Set("snapTransZ", snapTranslationZ_);
  128. root.Set("snapRotation", snapRotation_);
  129. root.Set("snapScale", snapScale_);
  130. jsonFile->Save(*file, String(" "));
  131. file->Close();
  132. VariantMap evData;
  133. evData[ProjectUserPrefSaved::P_PREFS] = this;
  134. SendEvent(E_PROJECTUSERPREFSAVED);
  135. }
  136. }