From efe31b02d9f8d24894fb79b8a3f7c2c804d6af8e Mon Sep 17 00:00:00 2001 From: David Capello Date: Thu, 8 Sep 2016 15:34:09 -0300 Subject: [PATCH] Save fg/bg color selector dialog positions between sessions (fix #1249) --- src/app/ui/color_bar.cpp | 2 ++ src/app/ui/color_button.cpp | 60 ++++++++++++++++++++++++++------- src/app/ui/color_button.h | 3 ++ src/app/ui/popup_window_pin.cpp | 19 +++++++---- src/app/ui/popup_window_pin.h | 4 ++- src/gfx/LICENSE.txt | 2 +- src/gfx/rect_io.h | 21 ++++++++++-- src/gfx/region_tests.cpp | 17 ++-------- 8 files changed, 90 insertions(+), 38 deletions(-) diff --git a/src/app/ui/color_bar.cpp b/src/app/ui/color_bar.cpp index 5f8af59b0..8be2fad15 100644 --- a/src/app/ui/color_bar.cpp +++ b/src/app/ui/color_bar.cpp @@ -202,6 +202,8 @@ ColorBar::ColorBar(int align) addChild(fgBox); addChild(bgBox); + m_fgColor.setId("fg_color"); + m_bgColor.setId("bg_color"); m_fgColor.setExpansive(true); m_bgColor.setExpansive(true); diff --git a/src/app/ui/color_button.cpp b/src/app/ui/color_button.cpp index f0d0f369a..75a72764a 100644 --- a/src/app/ui/color_button.cpp +++ b/src/app/ui/color_button.cpp @@ -25,6 +25,7 @@ #include "doc/layer.h" #include "doc/site.h" #include "doc/sprite.h" +#include "gfx/rect_io.h" #include "ui/size_hint_event.h" #include "ui/ui.h" @@ -105,6 +106,11 @@ bool ColorButton::onProcessMessage(Message* msg) { switch (msg->type()) { + case kOpenMessage: + if (!m_windowDefaultBounds.isEmpty()) + openSelectorDialog(); + break; + case kCloseMessage: if (m_window && m_window->isVisible()) m_window->closeWindow(NULL); @@ -236,34 +242,64 @@ void ColorButton::onClick(Event& ev) } } +void ColorButton::onLoadLayout(ui::LoadLayoutEvent& ev) +{ + if (m_canPinSelector) { + bool pinned = false; + ev.stream() >> pinned; + if (ev.stream() && pinned) + ev.stream() >> m_windowDefaultBounds; + } +} + +void ColorButton::onSaveLayout(ui::SaveLayoutEvent& ev) +{ + if (m_canPinSelector && m_window && m_window->isPinned()) + ev.stream() << 1 << ' ' << m_window->bounds(); + else + ev.stream() << 0; +} + void ColorButton::openSelectorDialog() { - int x, y; + bool pinned = (!m_windowDefaultBounds.isEmpty()); if (m_window == NULL) { m_window = new ColorPopup(m_canPinSelector); m_window->ColorChange.connect(&ColorButton::onWindowColorChange, this); } + if (pinned) + m_window->setPinned(true); + m_window->setColor(m_color, ColorPopup::ChangeType); m_window->openWindow(); - x = MID(0, bounds().x, ui::display_w()-m_window->bounds().w); - if (bounds().y2() <= ui::display_h()-m_window->bounds().h) - y = MAX(0, bounds().y2()); - else - y = MAX(0, bounds().y-m_window->bounds().h); - - m_window->positionWindow(x, y); + gfx::Rect winBounds = m_windowDefaultBounds; + if (!pinned) { + winBounds = m_window->bounds(); + winBounds.x = MID(0, bounds().x, ui::display_w()-winBounds.w); + if (bounds().y2() <= ui::display_h()-winBounds.h) + winBounds.y = MAX(0, bounds().y2()); + else + winBounds.y = MAX(0, bounds().y-winBounds.h); + } + winBounds.x = MID(0, winBounds.x, ui::display_w()-winBounds.w); + winBounds.y = MID(0, winBounds.y, ui::display_h()-winBounds.h); + m_window->setBounds(winBounds); m_window->manager()->dispatchMessages(); m_window->layout(); // Setup the hot-region - gfx::Rect rc = bounds().createUnion(m_window->bounds()); - rc.enlarge(8); - gfx::Region rgn(rc); - static_cast(m_window)->setHotRegion(rgn); + if (!pinned) { + gfx::Rect rc = bounds().createUnion(m_window->bounds()); + rc.enlarge(8); + gfx::Region rgn(rc); + static_cast(m_window)->setHotRegion(rgn); + } + + m_windowDefaultBounds = gfx::Rect(); } void ColorButton::closeSelectorDialog() diff --git a/src/app/ui/color_button.h b/src/app/ui/color_button.h index 4ef3de2e1..46c990d5f 100644 --- a/src/app/ui/color_button.h +++ b/src/app/ui/color_button.h @@ -45,6 +45,8 @@ namespace app { void onSizeHint(ui::SizeHintEvent& ev) override; void onPaint(ui::PaintEvent& ev) override; void onClick(ui::Event& ev) override; + void onLoadLayout(ui::LoadLayoutEvent& ev) override; + void onSaveLayout(ui::SaveLayoutEvent& ev) override; private: void openSelectorDialog(); @@ -55,6 +57,7 @@ namespace app { app::Color m_color; PixelFormat m_pixelFormat; ColorPopup* m_window; + gfx::Rect m_windowDefaultBounds; bool m_dependOnLayer; bool m_canPinSelector; }; diff --git a/src/app/ui/popup_window_pin.cpp b/src/app/ui/popup_window_pin.cpp index 7e4f1a035..7e4ae729b 100644 --- a/src/app/ui/popup_window_pin.cpp +++ b/src/app/ui/popup_window_pin.cpp @@ -45,6 +45,14 @@ bool PopupWindowPin::showPin(bool state) m_pin.setVisible(state); } +void PopupWindowPin::setPinned(bool pinned) +{ + m_pin.setSelected(pinned); + + Event ev(this); + onPinClick(ev); +} + void PopupWindowPin::onPinClick(Event& ev) { if (m_pin.isSelected()) { @@ -62,14 +70,11 @@ bool PopupWindowPin::onProcessMessage(Message* msg) { switch (msg->type()) { - case kOpenMessage: - m_pin.setSelected(false); - makeFixed(); - break; - - case kCloseMessage: - m_pin.setSelected(false); + case kOpenMessage: { + if (!isPinned()) + makeFixed(); break; + } } diff --git a/src/app/ui/popup_window_pin.h b/src/app/ui/popup_window_pin.h index 004be7371..2a5e2339d 100644 --- a/src/app/ui/popup_window_pin.h +++ b/src/app/ui/popup_window_pin.h @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2001-2015 David Capello +// Copyright (C) 2001-2016 David Capello // // This program is distributed under the terms of // the End-User License Agreement for Aseprite. @@ -18,6 +18,8 @@ namespace app { PopupWindowPin(const std::string& text, ClickBehavior clickBehavior); bool showPin(bool state); + bool isPinned() const { return m_pin.isSelected(); } + void setPinned(bool pinned); protected: virtual bool onProcessMessage(ui::Message* msg) override; diff --git a/src/gfx/LICENSE.txt b/src/gfx/LICENSE.txt index 220ac0d42..0d2d81fd4 100644 --- a/src/gfx/LICENSE.txt +++ b/src/gfx/LICENSE.txt @@ -1,4 +1,4 @@ -Copyright (c) 2001-2014 David Capello +Copyright (c) 2001-2016 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/gfx/rect_io.h b/src/gfx/rect_io.h index c455c3b2a..69be02bf7 100644 --- a/src/gfx/rect_io.h +++ b/src/gfx/rect_io.h @@ -1,5 +1,5 @@ // Aseprite Gfx Library -// Copyright (C) 2001-2014 David Capello +// Copyright (C) 2001-2016 David Capello // // This file is released under the terms of the MIT license. // Read LICENSE.txt for more information. @@ -13,8 +13,7 @@ namespace gfx { - inline std::ostream& operator<<(std::ostream& os, const Rect& rect) - { + inline std::ostream& operator<<(std::ostream& os, const Rect& rect) { return os << "(" << rect.x << ", " << rect.y << ", " @@ -22,6 +21,22 @@ namespace gfx { << rect.h << ")"; } + inline std::istream& operator>>(std::istream& in, Rect& rect) { + while (in && in.get() != '(') + ; + + if (!in) + return in; + + char chr; + in >> rect.x >> chr + >> rect.y >> chr + >> rect.w >> chr + >> rect.h >> chr; + + return in; + } + } #endif diff --git a/src/gfx/region_tests.cpp b/src/gfx/region_tests.cpp index 920b555eb..99d4ea4a4 100644 --- a/src/gfx/region_tests.cpp +++ b/src/gfx/region_tests.cpp @@ -1,5 +1,5 @@ // Aseprite Gfx Library -// Copyright (C) 2001-2013 David Capello +// Copyright (C) 2001-2016 David Capello // // This file is released under the terms of the MIT license. // Read LICENSE.txt for more information. @@ -10,24 +10,13 @@ #include -#include "gfx/region.h" #include "gfx/point.h" +#include "gfx/rect_io.h" +#include "gfx/region.h" using namespace std; using namespace gfx; -namespace gfx { - - ostream& operator<<(ostream& os, const Rect& rect) { - return os << "(" - << rect.x << ", " - << rect.y << ", " - << rect.w << ", " - << rect.h << ")"; - } - -} - ostream& operator<<(ostream& os, const Region& rgn) { os << "{";