Load/save shades in user preferences (close #85)

This commit is contained in:
David Capello 2015-11-25 15:17:34 -03:00
parent 0f8997b3df
commit 8a7a585362
4 changed files with 87 additions and 3 deletions

View File

@ -311,6 +311,7 @@ add_library(app-lib
resource_finder.cpp
scripting/app_scripting.cpp
send_crash.cpp
shade.cpp
shell.cpp
snap_to_grid.cpp
thumbnail_generator.cpp

41
src/app/shade.cpp Normal file
View File

@ -0,0 +1,41 @@
// Aseprite
// Copyright (C) 2001-2015 David Capello
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/shade.h"
#include "base/split_string.h"
namespace app {
Shade shade_from_string(const std::string& str)
{
Shade shade;
std::vector<std::string> parts;
base::split_string(str, parts, " ");
for (const auto& part : parts) {
auto color = app::Color::fromString(part);
if (color.getType() == app::Color::IndexType)
shade.push_back(color);
}
return shade;
}
std::string shade_to_string(const Shade& shade)
{
std::string res;
for (const auto& s : shade) {
res += s.toString();
res += " ";
}
return res;
}
} // namespace app

View File

@ -17,6 +17,9 @@ namespace app {
typedef std::vector<app::Color> Shade;
Shade shade_from_string(const std::string& str);
std::string shade_to_string(const Shade& shade);
} // namespace app
#endif

View File

@ -15,6 +15,7 @@
#include "app/color_utils.h"
#include "app/commands/commands.h"
#include "app/document.h"
#include "app/ini_file.h"
#include "app/modules/gfx.h"
#include "app/modules/gui.h"
#include "app/modules/palettes.h"
@ -712,9 +713,10 @@ class ContextBar::InkShadesField : public HBox {
};
public:
InkShadesField() :
m_button(SkinTheme::instance()->parts.iconArrowDown()->getBitmap(0)),
m_shade(Shade(), ShadeWidget::DragAndDrop) {
InkShadesField()
: m_button(SkinTheme::instance()->parts.iconArrowDown()->getBitmap(0))
, m_shade(Shade(), ShadeWidget::DragAndDrop)
, m_loaded(false) {
SkinTheme* theme = SkinTheme::instance();
m_shade.setBgColor(theme->colors.workspace());
m_button.setBgColor(theme->colors.workspace());
@ -727,6 +729,10 @@ public:
m_button.Click.connect(Bind<void>(&InkShadesField::onShowMenu, this));
}
~InkShadesField() {
saveShades();
}
void reverseShadeColors() {
m_shade.reverseShadeColors();
}
@ -737,6 +743,7 @@ public:
private:
void onShowMenu() {
loadShades();
gfx::Rect bounds = m_button.getBounds();
Menu menu;
@ -790,12 +797,44 @@ private:
}
void onSaveShade() {
loadShades();
m_shades.push_back(m_shade.getShade());
}
void loadShades() {
if (m_loaded)
return;
m_loaded = true;
char buf[32];
int n = get_config_int("shades", "count", 0);
n = MID(0, n, 256);
for (int i=0; i<n; ++i) {
sprintf(buf, "shade%d", i);
Shade shade = shade_from_string(get_config_string("shades", buf, ""));
if (shade.size() >= 2)
m_shades.push_back(shade);
}
}
void saveShades() {
if (!m_loaded)
return;
char buf[32];
int n = int(m_shades.size());
set_config_int("shades", "count", n);
for (int i=0; i<n; ++i) {
sprintf(buf, "shade%d", i);
set_config_string("shades", buf, shade_to_string(m_shades[i]).c_str());
}
}
IconButton m_button;
ShadeWidget m_shade;
std::vector<Shade> m_shades;
bool m_loaded;
};
class ContextBar::InkOpacityField : public IntEntry