From 6cab7b208c9e8c3efa2f39667bb031e79a3ffcb8 Mon Sep 17 00:00:00 2001 From: David Capello Date: Mon, 2 Dec 2019 19:13:27 -0300 Subject: [PATCH] Fix using default preferences on new documents (fix #2198) Fixed regression introduced in 002abc92452d89dabe601af0bfedf81e3e2ab0fd. Bug reports: * https://github.com/aseprite/aseprite/issues/2198 * https://steamcommunity.com/app/431730/discussions/2/1657817111845334892/ * https://community.aseprite.org/t/grid-size-for-new-documents-not-applied/4305 Added new ASEPRITE_USER_FOLDER environment variable to test preferences from a clean folder. --- src/app/pref/option.h | 5 +++++ src/app/pref/preferences.cpp | 24 ++++++++++++++++------- src/app/resource_finder.cpp | 37 +++++++++++++++++++++++++----------- src/app/resource_finder.h | 5 +++++ src/doc/sprite.cpp | 13 ++++++++++++- src/doc/sprite.h | 2 ++ 6 files changed, 67 insertions(+), 19 deletions(-) diff --git a/src/app/pref/option.h b/src/app/pref/option.h index 939355463..eafa0dd79 100644 --- a/src/app/pref/option.h +++ b/src/app/pref/option.h @@ -86,8 +86,13 @@ namespace app { // Changes the default value and the current one. void setValueAndDefault(const T& value) { + bool wasDirty = isDirty(); + setDefaultValue(value); setValue(value); + + if (!wasDirty) + cleanDirtyFlag(); } const T& defaultValue() const { return m_default; } diff --git a/src/app/pref/preferences.cpp b/src/app/pref/preferences.cpp index 5da017447..bcc520d41 100644 --- a/src/app/pref/preferences.cpp +++ b/src/app/pref/preferences.cpp @@ -50,6 +50,16 @@ Preferences::Preferences() load(); + // Create a connection with the default document preferences grid + // bounds to sync the default grid bounds for new sprites in the + // "doc" layer. + auto& defPref = document(nullptr); + defPref.grid.bounds.AfterChange.connect( + [](const gfx::Rect& newValue){ + doc::Sprite::SetDefaultGridBounds(newValue); + }); + doc::Sprite::SetDefaultGridBounds(defPref.grid.bounds()); + // Hide the menu bar depending on: // 1. this is the first run of the program // 2. the native menu bar is available @@ -135,21 +145,21 @@ DocumentPreferences& Preferences::document(const Doc* doc) DocumentPreferences* docPref = new DocumentPreferences(""); m_docs[doc] = docPref; - // If there is not a .ini file with the "doc" preferences to be - // loaded, we will setup the default preferences for this file. - // (This must be done just one time, when the .ini file with the - // specific settings for "doc" doesn't exist.) - if (doc && !base::is_file(docConfigFileName(doc))) { + // Setup the preferences of this document with the default ones + // (these preferences will be overwritten in the next statement + // loading the preferences from the .ini file of this doc) + if (doc) { // The default preferences for this document are the current // defaults for (document=nullptr). DocumentPreferences& defPref = this->document(nullptr); *docPref = defPref; // Default values for symmetry - docPref->symmetry.xAxis.setDefaultValue(doc->sprite()->width()/2); - docPref->symmetry.yAxis.setDefaultValue(doc->sprite()->height()/2); + docPref->symmetry.xAxis.setValueAndDefault(doc->sprite()->width()/2); + docPref->symmetry.yAxis.setValueAndDefault(doc->sprite()->height()/2); } + // Load specific settings of this document serializeDocPref(doc, docPref, false); return *docPref; diff --git a/src/app/resource_finder.cpp b/src/app/resource_finder.cpp index 4ba8cf567..4c20eb929 100644 --- a/src/app/resource_finder.cpp +++ b/src/app/resource_finder.cpp @@ -1,4 +1,5 @@ // Aseprite +// Copyright (C) 2019 Igara Studio S.A. // Copyright (C) 2001-2016 David Capello // // This program is distributed under the terms of @@ -152,7 +153,11 @@ void ResourceFinder::includeUserDir(const char* filename) { #ifdef _WIN32 - if (App::instance()->isPortable()) { + // $ASEPRITE_USER_FOLDER/filename + if (const wchar_t* env = _wgetenv(L"ASEPRITE_USER_FOLDER")) { + addPath(base::join_path(base::to_utf8(env), filename)); + } + else if (App::instance()->isPortable()) { // $BINDIR/filename includeBinDir(filename); } @@ -161,20 +166,30 @@ void ResourceFinder::includeUserDir(const char* filename) includeHomeDir(filename); } -#elif __APPLE__ +#else // Unix-like - // $HOME/Library/Application Support/Aseprite/filename - addPath( - base::join_path( - base::join_path(base::get_lib_app_support_path(), PACKAGE), - filename).c_str()); + // $ASEPRITE_USER_FOLDER/filename + if (const char* env = std::getenv("ASEPRITE_USER_FOLDER")) { + addPath(base::join_path(env, filename)); + } + else { + #ifdef __APPLE__ -#else + // $HOME/Library/Application Support/Aseprite/filename + addPath( + base::join_path( + base::join_path(base::get_lib_app_support_path(), PACKAGE), + filename).c_str()); - // $HOME/.config/aseprite/filename - includeHomeDir((std::string(".config/aseprite/") + filename).c_str()); + #else // !__APPLE__ -#endif + // $HOME/.config/aseprite/filename + includeHomeDir((std::string(".config/aseprite/") + filename).c_str()); + + #endif + } + +#endif // end Unix-like } void ResourceFinder::includeDesktopDir(const char* filename) diff --git a/src/app/resource_finder.h b/src/app/resource_finder.h index 7816508e5..25e0bf16a 100644 --- a/src/app/resource_finder.h +++ b/src/app/resource_finder.h @@ -1,4 +1,5 @@ // Aseprite +// Copyright (C) 2019 Igara Studio S.A. // Copyright (C) 2001-2018 David Capello // // This program is distributed under the terms of @@ -42,6 +43,10 @@ namespace app { // Tries to add the given filename in these locations: // For Windows: + // - If ASEPRITE_USER_FOLDER environment variable is defined, it + // should point the folder where the "user dir" is (it's useful + // for testing purposes to test with an empty preferences + // folder) // - If the app is running in portable mode, the filename // will be in the same location as the .exe file. // - If the app is installed, the filename will be inside diff --git a/src/doc/sprite.cpp b/src/doc/sprite.cpp index 45cde4a50..5fa9139c3 100644 --- a/src/doc/sprite.cpp +++ b/src/doc/sprite.cpp @@ -34,8 +34,19 @@ namespace doc { ////////////////////////////////////////////////////////////////////// // Constructors/Destructor +static gfx::Rect g_defaultGridBounds(0, 0, 16, 16); + // static -gfx::Rect Sprite::DefaultGridBounds() { return gfx::Rect(0, 0, 16, 16); } +gfx::Rect Sprite::DefaultGridBounds() +{ + return g_defaultGridBounds; +} + +// static +void Sprite::SetDefaultGridBounds(const gfx::Rect& defGridBounds) +{ + g_defaultGridBounds = defGridBounds; +} Sprite::Sprite(const ImageSpec& spec, int ncolors) diff --git a/src/doc/sprite.h b/src/doc/sprite.h index 296b3b7d1..1e6915676 100644 --- a/src/doc/sprite.h +++ b/src/doc/sprite.h @@ -101,6 +101,8 @@ namespace doc { void setTransparentColor(color_t color); static gfx::Rect DefaultGridBounds(); + static void SetDefaultGridBounds(const gfx::Rect& defGridBounds); + const gfx::Rect& gridBounds() const { return m_gridBounds; } void setGridBounds(const gfx::Rect& rc) { m_gridBounds = rc; }