diff --git a/src/app/app.cpp b/src/app/app.cpp index 87a93fb5a..db964981d 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -450,6 +450,14 @@ App::~App() delete m_legacy; delete m_modules; + + // Save preferences only if we are running in GUI mode. when we + // run in batch mode we might want to reset some preferences so + // the scripts have a reproducible behavior. Those reset + // preferences must not be saved. + if (isGui()) + m_coreModules->m_preferences.save(); + delete m_coreModules; #ifdef ENABLE_UI @@ -639,18 +647,14 @@ void app_rebuild_documents_tabs() PixelFormat app_get_current_pixel_format() { -#ifdef ENABLE_UI - Context* context = UIContext::instance(); - ASSERT(context != NULL); + Context* ctx = App::instance()->context(); + ASSERT(ctx); - Doc* document = context->activeDocument(); - if (document != NULL) - return document->sprite()->pixelFormat(); + Doc* doc = ctx->activeDocument(); + if (doc) + return doc->sprite()->pixelFormat(); else return IMAGE_RGB; -#else // ENABLE_UI - return IMAGE_RGB; -#endif } void app_default_statusbar_message() diff --git a/src/app/ini_file.cpp b/src/app/ini_file.cpp index 3acacefa5..ed78e8dfd 100644 --- a/src/app/ini_file.cpp +++ b/src/app/ini_file.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2018 Igara Studio S.A. +// Copyright (C) 2018-2019 Igara Studio S.A. // Copyright (C) 2001-2016 David Capello // // This program is distributed under the terms of @@ -274,6 +274,11 @@ void del_config_value(const char* section, const char* name) g_configs.back()->deleteValue(section, name); } +void del_config_section(const char* section) +{ + g_configs.back()->deleteSection(section); +} + base::paths enum_config_keys(const char* section) { base::paths keys; diff --git a/src/app/ini_file.h b/src/app/ini_file.h index 8c595cea2..db879e123 100644 --- a/src/app/ini_file.h +++ b/src/app/ini_file.h @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2018 Igara Studio S.A. +// Copyright (C) 2018-2019 Igara Studio S.A. // Copyright (C) 2001-2016 David Capello // // This program is distributed under the terms of @@ -58,6 +58,7 @@ namespace app { void set_config_color(const char* section, const char* name, const app::Color& value); void del_config_value(const char* section, const char* name); + void del_config_section(const char* section); base::paths enum_config_keys(const char* section); diff --git a/src/app/pref/preferences.cpp b/src/app/pref/preferences.cpp index 2c9b1d9d7..965fdb60b 100644 --- a/src/app/pref/preferences.cpp +++ b/src/app/pref/preferences.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2018 Igara Studio S.A. +// Copyright (C) 2018-2019 Igara Studio S.A. // Copyright (C) 2001-2018 David Capello // // This program is distributed under the terms of @@ -57,7 +57,9 @@ Preferences::Preferences() Preferences::~Preferences() { - save(); + // Don't save preferences, this must be done by the client of the + // Preferences instance (the App class). + //save(); for (auto& pair : m_tools) delete pair.second; @@ -148,6 +150,16 @@ DocumentPreferences& Preferences::document(const Doc* doc) } } +void Preferences::resetToolPreferences(tools::Tool* tool) +{ + auto it = m_tools.find(tool->getId()); + if (it != m_tools.end()) + m_tools.erase(it); + + std::string section = std::string("tool.") + tool->getId(); + del_config_section(section.c_str()); +} + void Preferences::removeDocument(Doc* doc) { ASSERT(doc); diff --git a/src/app/pref/preferences.h b/src/app/pref/preferences.h index f9b0a8d54..b432ff45e 100644 --- a/src/app/pref/preferences.h +++ b/src/app/pref/preferences.h @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2018 Igara Studio S.A. +// Copyright (C) 2018-2019 Igara Studio S.A. // Copyright (C) 2001-2018 David Capello // // This program is distributed under the terms of @@ -64,6 +64,11 @@ namespace app { ToolPreferences& tool(tools::Tool* tool); DocumentPreferences& document(const Doc* doc); + // Used to reset the tool preferences in scripting mode when the + // UI is not available (so scripts have a common default + // preferences and a reproducible behavior for automation). + void resetToolPreferences(tools::Tool* tool); + // Remove one document explicitly (this can be used if the // document used in Preferences::document() function wasn't member // of UIContext. diff --git a/src/app/ui/editor/tool_loop_impl.cpp b/src/app/ui/editor/tool_loop_impl.cpp index 6ce0908cd..cb8169b2f 100644 --- a/src/app/ui/editor/tool_loop_impl.cpp +++ b/src/app/ui/editor/tool_loop_impl.cpp @@ -337,7 +337,6 @@ public: #endif } - }; ////////////////////////////////////////////////////////////////////// @@ -534,6 +533,8 @@ public: #ifdef ENABLE_UI if (redraw) update_screen_for_document(m_document); +#else + (void)redraw; // To avoid warning about unused variable #endif } @@ -700,6 +701,12 @@ tools::ToolLoop* create_tool_loop_for_script( const tools::ToolLoop::Button toolLoopButton = tools::ToolLoop::Left; tools::Controller* controller = tool->getController(toolLoopButton); + // If we don't have the UI available, we reset the tools + // preferences, so scripts that are executed in batch mode have a + // reproducible behavior. + if (!context->isUIAvailable()) + Preferences::instance().resetToolPreferences(tool); + return new ToolLoopImpl( nullptr, site, context, tool, ink, controller, brush, diff --git a/src/cfg/LICENSE.txt b/src/cfg/LICENSE.txt index aea0f6e8f..7585bab69 100644 --- a/src/cfg/LICENSE.txt +++ b/src/cfg/LICENSE.txt @@ -1,4 +1,5 @@ -Copyright (c) 2014 David Capello +Copyright (C) 2019 Igara Studio S.A. +Copyright (C) 2014-2017 David Capello Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/src/cfg/README.md b/src/cfg/README.md index 8a1d9c014..02a0876c1 100644 --- a/src/cfg/README.md +++ b/src/cfg/README.md @@ -1,4 +1,3 @@ # Aseprite Config Library -*Copyright (C) 2014 David Capello* > Distributed under [MIT license](LICENSE.txt) diff --git a/src/cfg/cfg.cpp b/src/cfg/cfg.cpp index f8cd2aef6..c8fe17288 100644 --- a/src/cfg/cfg.cpp +++ b/src/cfg/cfg.cpp @@ -1,5 +1,6 @@ // Aseprite Config Library -// Copyright (c) 2014-2017 David Capello +// Copyright (C) 2019 Igara Studio S.A. +// Copyright (C) 2014-2017 David Capello // // This file is released under the terms of the MIT license. // Read LICENSE.txt for more information. @@ -83,6 +84,10 @@ public: m_ini.Delete(section, name, true); } + void deleteSection(const char* section) { + m_ini.Delete(section, nullptr, true); + } + void load(const std::string& filename) { m_filename = filename; @@ -181,6 +186,11 @@ void CfgFile::deleteValue(const char* section, const char* name) m_impl->deleteValue(section, name); } +void CfgFile::deleteSection(const char* section) +{ + m_impl->deleteSection(section); +} + void CfgFile::load(const std::string& filename) { m_impl->load(filename); diff --git a/src/cfg/cfg.h b/src/cfg/cfg.h index be5cd8d80..fef98afd0 100644 --- a/src/cfg/cfg.h +++ b/src/cfg/cfg.h @@ -1,5 +1,6 @@ // Aseprite Config Library -// Copyright (c) 2014-2016 David Capello +// Copyright (C) 2019 Igara Studio S.A. +// Copyright (C) 2014-2016 David Capello // // This file is released under the terms of the MIT license. // Read LICENSE.txt for more information. @@ -34,6 +35,7 @@ namespace cfg { void setDoubleValue(const char* section, const char* name, double value); void deleteValue(const char* section, const char* name); + void deleteSection(const char* section); void load(const std::string& filename); void save();