From 449ae1d9e421aea3fd4046e8e1937144d7dd69b6 Mon Sep 17 00:00:00 2001 From: David Capello Date: Thu, 17 Mar 2016 16:37:31 -0300 Subject: [PATCH] Add new app::ColorSelector to share behavior between ColorTintShadeTone/ColorSpectrum/ColorWheel --- src/app/CMakeLists.txt | 1 + src/app/ui/color_selector.cpp | 41 ++++++++++++++++++++++++++++ src/app/ui/color_selector.h | 41 ++++++++++++++++++++++++++++ src/app/ui/color_spectrum.cpp | 21 -------------- src/app/ui/color_spectrum.h | 20 +++----------- src/app/ui/color_tint_shade_tone.cpp | 23 +--------------- src/app/ui/color_tint_shade_tone.h | 17 ++---------- src/app/ui/color_wheel.cpp | 34 +++++------------------ src/app/ui/color_wheel.h | 19 ++----------- 9 files changed, 99 insertions(+), 118 deletions(-) create mode 100644 src/app/ui/color_selector.cpp create mode 100644 src/app/ui/color_selector.h diff --git a/src/app/CMakeLists.txt b/src/app/CMakeLists.txt index 668951ddc..694df0276 100644 --- a/src/app/CMakeLists.txt +++ b/src/app/CMakeLists.txt @@ -338,6 +338,7 @@ add_library(app-lib ui/color_bar.cpp ui/color_button.cpp ui/color_popup.cpp + ui/color_selector.cpp ui/color_sliders.cpp ui/color_spectrum.cpp ui/color_tint_shade_tone.cpp diff --git a/src/app/ui/color_selector.cpp b/src/app/ui/color_selector.cpp new file mode 100644 index 000000000..f9fae973e --- /dev/null +++ b/src/app/ui/color_selector.cpp @@ -0,0 +1,41 @@ +// Aseprite +// Copyright (C) 2016 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/ui/color_selector.h" + +#include "ui/size_hint_event.h" +#include "ui/theme.h" + +namespace app { + +using namespace ui; + +ColorSelector::ColorSelector() + : Widget(kGenericWidget) + , m_lockColor(false) +{ +} + +void ColorSelector::selectColor(const app::Color& color) +{ + if (m_lockColor) + return; + + m_color = color; + invalidate(); +} + +void ColorSelector::onSizeHint(SizeHintEvent& ev) +{ + ev.setSizeHint(gfx::Size(32*ui::guiscale(), 32*ui::guiscale())); +} + +} // namespace app diff --git a/src/app/ui/color_selector.h b/src/app/ui/color_selector.h new file mode 100644 index 000000000..8e34e0500 --- /dev/null +++ b/src/app/ui/color_selector.h @@ -0,0 +1,41 @@ +// Aseprite +// Copyright (C) 2016 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. + +#ifndef APP_UI_COLOR_SELECTOR_H_INCLUDED +#define APP_UI_COLOR_SELECTOR_H_INCLUDED +#pragma once + +#include "app/color.h" +#include "base/signal.h" +#include "ui/mouse_buttons.h" +#include "ui/widget.h" + +namespace app { + + class ColorSelector : public ui::Widget { + public: + ColorSelector(); + + void selectColor(const app::Color& color); + + // Signals + base::Signal2 ColorChange; + + protected: + void onSizeHint(ui::SizeHintEvent& ev) override; + + app::Color m_color; + + // Internal flag used to lock the modification of m_color. + // E.g. When the user picks a color harmony, we don't want to + // change the main color. + bool m_lockColor; + }; + +} // namespace app + +#endif diff --git a/src/app/ui/color_spectrum.cpp b/src/app/ui/color_spectrum.cpp index fff43610a..f44a722aa 100644 --- a/src/app/ui/color_spectrum.cpp +++ b/src/app/ui/color_spectrum.cpp @@ -29,16 +29,11 @@ using namespace gfx; using namespace ui; ColorSpectrum::ColorSpectrum() - : Widget(kGenericWidget) { setAlign(HORIZONTAL); setBorder(gfx::Border(3*ui::guiscale())); } -ColorSpectrum::~ColorSpectrum() -{ -} - app::Color ColorSpectrum::pickColor(const gfx::Point& pos) const { gfx::Rect rc = childrenBounds(); @@ -70,22 +65,6 @@ app::Color ColorSpectrum::pickColor(const gfx::Point& pos) const MID(0.0, val, 100.0)); } -void ColorSpectrum::selectColor(const app::Color& color) -{ - m_color = color; - invalidate(); -} - -void ColorSpectrum::onSizeHint(SizeHintEvent& ev) -{ - ev.setSizeHint(gfx::Size(32*ui::guiscale(), 32*ui::guiscale())); -} - -void ColorSpectrum::onResize(ui::ResizeEvent& ev) -{ - Widget::onResize(ev); -} - void ColorSpectrum::onPaint(ui::PaintEvent& ev) { ui::Graphics* g = ev.graphics(); diff --git a/src/app/ui/color_spectrum.h b/src/app/ui/color_spectrum.h index cf87f1846..bd8474519 100644 --- a/src/app/ui/color_spectrum.h +++ b/src/app/ui/color_spectrum.h @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2001-2015 David Capello +// Copyright (C) 2001-2016 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 @@ -9,32 +9,20 @@ #define APP_UI_COLOR_SPECTRUM_H_INCLUDED #pragma once -#include "app/color.h" -#include "base/signal.h" -#include "ui/mouse_buttons.h" -#include "ui/widget.h" +#include "app/ui/color_selector.h" +#include "ui/button.h" namespace app { - class ColorSpectrum : public ui::Widget { + class ColorSpectrum : public ColorSelector { public: ColorSpectrum(); - ~ColorSpectrum(); app::Color pickColor(const gfx::Point& pos) const; - void selectColor(const app::Color& color); - - // Signals - base::Signal2 ColorChange; protected: - void onSizeHint(ui::SizeHintEvent& ev) override; - void onResize(ui::ResizeEvent& ev) override; void onPaint(ui::PaintEvent& ev) override; bool onProcessMessage(ui::Message* msg) override; - - private: - app::Color m_color; }; } // namespace app diff --git a/src/app/ui/color_tint_shade_tone.cpp b/src/app/ui/color_tint_shade_tone.cpp index 85e64576d..cfe6c4cf9 100644 --- a/src/app/ui/color_tint_shade_tone.cpp +++ b/src/app/ui/color_tint_shade_tone.cpp @@ -29,16 +29,11 @@ using namespace gfx; using namespace ui; ColorTintShadeTone::ColorTintShadeTone() - : Widget(kGenericWidget) - , m_capturedInHue(false) + : m_capturedInHue(false) { setBorder(gfx::Border(3*ui::guiscale())); } -ColorTintShadeTone::~ColorTintShadeTone() -{ -} - app::Color ColorTintShadeTone::pickColor(const gfx::Point& pos) const { gfx::Rect rc = childrenBounds(); @@ -71,22 +66,6 @@ app::Color ColorTintShadeTone::pickColor(const gfx::Point& pos) const MID(0.0, val, 100.0)); } -void ColorTintShadeTone::selectColor(const app::Color& color) -{ - m_color = color; - invalidate(); -} - -void ColorTintShadeTone::onSizeHint(SizeHintEvent& ev) -{ - ev.setSizeHint(gfx::Size(32*ui::guiscale(), 32*ui::guiscale())); -} - -void ColorTintShadeTone::onResize(ui::ResizeEvent& ev) -{ - Widget::onResize(ev); -} - void ColorTintShadeTone::onPaint(ui::PaintEvent& ev) { ui::Graphics* g = ev.graphics(); diff --git a/src/app/ui/color_tint_shade_tone.h b/src/app/ui/color_tint_shade_tone.h index f555d96e8..5d62da13a 100644 --- a/src/app/ui/color_tint_shade_tone.h +++ b/src/app/ui/color_tint_shade_tone.h @@ -9,26 +9,15 @@ #define APP_UI_COLOR_TINT_SHADE_TONE_H_INCLUDED #pragma once -#include "app/color.h" -#include "base/signal.h" -#include "ui/mouse_buttons.h" -#include "ui/widget.h" +#include "app/ui/color_selector.h" namespace app { - class ColorTintShadeTone : public ui::Widget { + class ColorTintShadeTone : public ColorSelector { public: ColorTintShadeTone(); - ~ColorTintShadeTone(); - - void selectColor(const app::Color& color); - - // Signals - base::Signal2 ColorChange; protected: - void onSizeHint(ui::SizeHintEvent& ev) override; - void onResize(ui::ResizeEvent& ev) override; void onPaint(ui::PaintEvent& ev) override; bool onProcessMessage(ui::Message* msg) override; @@ -37,8 +26,6 @@ namespace app { bool inHueBarArea(const gfx::Point& pos) const; int getHueBarSize() const; - app::Color m_color; - // True when the user pressed the mouse button in the hue slider. // It's used to avoid swapping in both areas (tint/shades/tones // area vs hue slider) when we drag the mouse above this widget. diff --git a/src/app/ui/color_wheel.cpp b/src/app/ui/color_wheel.cpp index a8c5835e6..899183f52 100644 --- a/src/app/ui/color_wheel.cpp +++ b/src/app/ui/color_wheel.cpp @@ -51,13 +51,11 @@ static struct { }; ColorWheel::ColorWheel() - : Widget(kGenericWidget) - , m_discrete(Preferences::instance().colorBar.discreteWheel()) + : m_discrete(Preferences::instance().colorBar.discreteWheel()) , m_colorModel((ColorModel)Preferences::instance().colorBar.wheelModel()) , m_harmony((Harmony)Preferences::instance().colorBar.harmony()) , m_options("", kButtonWidget, kButtonWidget, kCheckWidget) , m_harmonyPicked(false) - , m_lockColor(false) { SkinTheme* theme = SkinTheme::instance(); @@ -74,10 +72,6 @@ ColorWheel::ColorWheel() addChild(&m_options); } -ColorWheel::~ColorWheel() -{ -} - app::Color ColorWheel::pickColor(const gfx::Point& pos) const { m_harmonyPicked = false; @@ -119,7 +113,7 @@ app::Color ColorWheel::pickColor(const gfx::Point& pos) const } // Pick harmonies - if (m_mainColor.getAlpha() > 0) { + if (m_color.getAlpha() > 0) { const gfx::Rect& rc = m_clientBounds; int n = getHarmonies(); int boxsize = MIN(rc.w/10, rc.h/10); @@ -143,15 +137,6 @@ app::Color ColorWheel::pickColor(const gfx::Point& pos) const return app::Color::fromMask(); } -void ColorWheel::selectColor(const app::Color& color) -{ - if (m_lockColor) - return; - - m_mainColor = color; - invalidate(); -} - void ColorWheel::setDiscrete(bool state) { m_discrete = state; @@ -186,21 +171,16 @@ app::Color ColorWheel::getColorInHarmony(int j) const { int i = MID(0, (int)m_harmony, (int)Harmony::LAST); j = MID(0, j, harmonies[i].n-1); - double hue = convertHueAngle(int(m_mainColor.getHue()), -1) + harmonies[i].hues[j]; - double sat = m_mainColor.getSaturation() * harmonies[i].sats[j] / 100.0; + double hue = convertHueAngle(int(m_color.getHue()), -1) + harmonies[i].hues[j]; + double sat = m_color.getSaturation() * harmonies[i].sats[j] / 100.0; return app::Color::fromHsv(std::fmod(hue, 360), MID(0.0, sat, 100.0), - m_mainColor.getValue()); -} - -void ColorWheel::onSizeHint(SizeHintEvent& ev) -{ - ev.setSizeHint(gfx::Size(32*ui::guiscale(), 32*ui::guiscale())); + m_color.getValue()); } void ColorWheel::onResize(ui::ResizeEvent& ev) { - Widget::onResize(ev); + ColorSelector::onResize(ev); gfx::Rect rc = clientChildrenBounds(); int r = MIN(rc.w/2, rc.h/2); @@ -247,7 +227,7 @@ void ColorWheel::onPaint(ui::PaintEvent& ev) } } - if (m_mainColor.getAlpha() > 0) { + if (m_color.getAlpha() > 0) { int n = getHarmonies(); int boxsize = MIN(rc.w/10, rc.h/10); diff --git a/src/app/ui/color_wheel.h b/src/app/ui/color_wheel.h index 01965ab1c..44745edd6 100644 --- a/src/app/ui/color_wheel.h +++ b/src/app/ui/color_wheel.h @@ -9,15 +9,12 @@ #define APP_UI_COLOR_WHEEL_H_INCLUDED #pragma once -#include "app/color.h" -#include "base/signal.h" +#include "app/ui/color_selector.h" #include "ui/button.h" -#include "ui/mouse_buttons.h" -#include "ui/widget.h" namespace app { - class ColorWheel : public ui::Widget { + class ColorWheel : public ColorSelector { public: enum class ColorModel { RGB, @@ -37,10 +34,8 @@ namespace app { }; ColorWheel(); - ~ColorWheel(); app::Color pickColor(const gfx::Point& pos) const; - void selectColor(const app::Color& color); bool isDiscrete() const { return m_discrete; } void setDiscrete(bool state); @@ -48,11 +43,7 @@ namespace app { void setColorModel(ColorModel colorModel); void setHarmony(Harmony harmony); - // Signals - base::Signal2 ColorChange; - private: - void onSizeHint(ui::SizeHintEvent& ev) override; void onResize(ui::ResizeEvent& ev) override; void onPaint(ui::PaintEvent& ev) override; bool onProcessMessage(ui::Message* msg) override; @@ -72,16 +63,10 @@ namespace app { ColorModel m_colorModel; Harmony m_harmony; ui::ButtonBase m_options; - app::Color m_mainColor; // Internal flag used to know if after pickColor() we selected an // harmony. mutable bool m_harmonyPicked; - - // Internal flag used to lock the modification of m_mainColor. - // When the user picks a color harmony, we don't want to change - // the main color. - bool m_lockColor; }; } // namespace app