2015-02-12 23:16:25 +08:00
|
|
|
// 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.
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2007-09-19 07:57:02 +08:00
|
|
|
#include "config.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#endif
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/app.h"
|
2010-08-25 11:26:37 +08:00
|
|
|
#include "app/color.h"
|
2015-03-21 01:46:48 +08:00
|
|
|
#include "app/color_utils.h"
|
2015-05-09 23:20:58 +08:00
|
|
|
#include "app/commands/commands.h"
|
|
|
|
#include "app/modules/editors.h"
|
2014-04-14 03:26:00 +08:00
|
|
|
#include "app/modules/gui.h"
|
|
|
|
#include "app/modules/palettes.h"
|
2015-05-09 23:20:58 +08:00
|
|
|
#include "app/ui/editor/editor.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/ui/palette_view.h"
|
2015-03-20 06:45:12 +08:00
|
|
|
#include "app/ui/skin/skin_theme.h"
|
|
|
|
#include "app/ui/skin/style.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/ui/status_bar.h"
|
2014-10-21 09:21:31 +08:00
|
|
|
#include "doc/blend.h"
|
|
|
|
#include "doc/image.h"
|
|
|
|
#include "doc/palette.h"
|
2015-03-23 23:25:32 +08:00
|
|
|
#include "doc/remap.h"
|
2015-03-20 19:44:39 +08:00
|
|
|
#include "gfx/color.h"
|
2015-03-20 06:45:12 +08:00
|
|
|
#include "gfx/point.h"
|
2014-04-14 03:26:00 +08:00
|
|
|
#include "ui/graphics.h"
|
2012-06-18 09:49:58 +08:00
|
|
|
#include "ui/manager.h"
|
|
|
|
#include "ui/message.h"
|
2014-04-14 03:26:00 +08:00
|
|
|
#include "ui/paint_event.h"
|
2012-09-27 05:34:52 +08:00
|
|
|
#include "ui/preferred_size_event.h"
|
2012-06-18 09:49:58 +08:00
|
|
|
#include "ui/system.h"
|
|
|
|
#include "ui/theme.h"
|
|
|
|
#include "ui/view.h"
|
|
|
|
#include "ui/widget.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
|
2015-03-23 23:25:32 +08:00
|
|
|
#include <algorithm>
|
2014-06-23 05:53:14 +08:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
namespace app {
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
using namespace ui;
|
2015-03-20 06:45:12 +08:00
|
|
|
using namespace app::skin;
|
2012-06-18 09:02:54 +08:00
|
|
|
|
2013-04-04 09:07:24 +08:00
|
|
|
WidgetType palette_view_type()
|
2007-09-19 07:57:02 +08:00
|
|
|
{
|
2013-04-04 09:07:24 +08:00
|
|
|
static WidgetType type = kGenericWidget;
|
|
|
|
if (type == kGenericWidget)
|
|
|
|
type = register_widget_type();
|
2007-09-19 07:57:02 +08:00
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2015-03-24 01:39:21 +08:00
|
|
|
PaletteView::PaletteView(bool editable, PaletteViewDelegate* delegate, int boxsize)
|
2011-01-28 20:28:54 +08:00
|
|
|
: Widget(palette_view_type())
|
2015-03-21 02:29:44 +08:00
|
|
|
, m_state(State::WAITING)
|
2015-03-20 06:45:12 +08:00
|
|
|
, m_editable(editable)
|
2015-03-23 23:25:32 +08:00
|
|
|
, m_delegate(delegate)
|
2015-03-20 06:45:12 +08:00
|
|
|
, m_columns(16)
|
2015-03-24 01:39:21 +08:00
|
|
|
, m_boxsize(boxsize)
|
2011-06-27 06:07:19 +08:00
|
|
|
, m_currentEntry(-1)
|
|
|
|
, m_rangeAnchor(-1)
|
2015-05-09 23:20:58 +08:00
|
|
|
, m_selectedEntries(Palette::MaxColors)
|
|
|
|
, m_clipboardEntries(Palette::MaxColors)
|
2012-07-18 12:10:43 +08:00
|
|
|
, m_isUpdatingColumns(false)
|
2015-03-21 01:46:48 +08:00
|
|
|
, m_hot(Hit::NONE)
|
2015-05-10 02:57:46 +08:00
|
|
|
, m_copy(false)
|
2015-05-09 23:20:58 +08:00
|
|
|
, m_clipboardEditor(nullptr)
|
2008-03-15 09:54:45 +08:00
|
|
|
{
|
2014-04-14 03:26:00 +08:00
|
|
|
setFocusStop(true);
|
|
|
|
setDoubleBuffered(true);
|
|
|
|
|
2014-11-26 09:33:45 +08:00
|
|
|
this->border_width.l = this->border_width.r = 1 * guiscale();
|
|
|
|
this->border_width.t = this->border_width.b = 1 * guiscale();
|
|
|
|
this->child_spacing = 1 * guiscale();
|
2014-06-02 05:09:19 +08:00
|
|
|
|
2014-06-13 09:29:19 +08:00
|
|
|
m_conn = App::instance()->PaletteChange.connect(&PaletteView::onAppPaletteChange, this);
|
2008-03-15 09:54:45 +08:00
|
|
|
}
|
|
|
|
|
2015-05-09 23:20:58 +08:00
|
|
|
PaletteView::~PaletteView()
|
|
|
|
{
|
|
|
|
setClipboardEditor(nullptr);
|
|
|
|
}
|
|
|
|
|
2011-01-28 20:28:54 +08:00
|
|
|
void PaletteView::setColumns(int columns)
|
2008-03-15 09:54:45 +08:00
|
|
|
{
|
2010-04-11 04:01:56 +08:00
|
|
|
int old_columns = m_columns;
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2012-05-03 10:09:34 +08:00
|
|
|
ASSERT(columns >= 1 && columns <= Palette::MaxColors);
|
2010-04-11 04:01:56 +08:00
|
|
|
m_columns = columns;
|
2008-03-15 09:54:45 +08:00
|
|
|
|
2010-04-11 04:01:56 +08:00
|
|
|
if (m_columns != old_columns) {
|
2011-02-21 05:35:21 +08:00
|
|
|
View* view = View::getView(this);
|
2007-09-19 07:57:02 +08:00
|
|
|
if (view)
|
2011-02-21 05:35:21 +08:00
|
|
|
view->updateView();
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2011-01-22 06:45:04 +08:00
|
|
|
invalidate();
|
2007-09-19 07:57:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-27 06:07:19 +08:00
|
|
|
void PaletteView::clearSelection()
|
|
|
|
{
|
2015-05-10 06:55:33 +08:00
|
|
|
bool invalidate = (m_selectedEntries.picks() > 0);
|
|
|
|
|
2011-06-27 06:07:19 +08:00
|
|
|
std::fill(m_selectedEntries.begin(),
|
2012-01-06 06:45:03 +08:00
|
|
|
m_selectedEntries.end(), false);
|
2015-05-10 06:55:33 +08:00
|
|
|
|
|
|
|
if (invalidate)
|
|
|
|
this->invalidate();
|
2011-06-27 06:07:19 +08:00
|
|
|
}
|
|
|
|
|
2015-05-10 06:55:33 +08:00
|
|
|
void PaletteView::selectColor(int index, bool startRange)
|
2008-03-15 09:54:45 +08:00
|
|
|
{
|
2012-05-03 10:09:34 +08:00
|
|
|
ASSERT(index >= 0 && index < Palette::MaxColors);
|
2008-03-15 09:54:45 +08:00
|
|
|
|
2011-06-27 06:07:19 +08:00
|
|
|
if (m_currentEntry != index || !m_selectedEntries[index]) {
|
|
|
|
m_currentEntry = index;
|
|
|
|
m_rangeAnchor = index;
|
2015-05-10 06:55:33 +08:00
|
|
|
|
|
|
|
if (startRange)
|
|
|
|
m_selectedEntries[index] = true;
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2011-06-27 06:07:19 +08:00
|
|
|
update_scroll(m_currentEntry);
|
2011-01-22 06:45:04 +08:00
|
|
|
invalidate();
|
2007-09-19 07:57:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-27 06:07:19 +08:00
|
|
|
void PaletteView::selectRange(int index1, int index2)
|
2007-09-19 07:57:02 +08:00
|
|
|
{
|
2011-06-27 06:07:19 +08:00
|
|
|
m_rangeAnchor = index1;
|
|
|
|
m_currentEntry = index2;
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2011-06-27 06:07:19 +08:00
|
|
|
std::fill(m_selectedEntries.begin()+std::min(index1, index2),
|
2012-01-06 06:45:03 +08:00
|
|
|
m_selectedEntries.begin()+std::max(index1, index2)+1, true);
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2011-06-27 06:07:19 +08:00
|
|
|
update_scroll(index2);
|
2011-01-22 06:45:04 +08:00
|
|
|
invalidate();
|
2007-09-19 07:57:02 +08:00
|
|
|
}
|
|
|
|
|
2011-06-27 06:07:19 +08:00
|
|
|
int PaletteView::getSelectedEntry() const
|
2007-09-19 07:57:02 +08:00
|
|
|
{
|
2011-06-27 06:07:19 +08:00
|
|
|
return m_currentEntry;
|
2007-09-19 07:57:02 +08:00
|
|
|
}
|
|
|
|
|
2011-06-27 06:07:19 +08:00
|
|
|
bool PaletteView::getSelectedRange(int& index1, int& index2) const
|
2007-09-19 07:57:02 +08:00
|
|
|
{
|
2011-06-27 06:07:19 +08:00
|
|
|
int i, i2, j;
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2011-06-27 06:07:19 +08:00
|
|
|
// Find the first selected entry
|
|
|
|
for (i=0; i<(int)m_selectedEntries.size(); ++i)
|
|
|
|
if (m_selectedEntries[i])
|
2007-09-19 07:57:02 +08:00
|
|
|
break;
|
|
|
|
|
2011-06-27 06:07:19 +08:00
|
|
|
// Find the first unselected entry after i
|
|
|
|
for (i2=i+1; i2<(int)m_selectedEntries.size(); ++i2)
|
|
|
|
if (!m_selectedEntries[i2])
|
2007-09-19 07:57:02 +08:00
|
|
|
break;
|
|
|
|
|
2011-06-27 06:07:19 +08:00
|
|
|
// Find the last selected entry
|
|
|
|
for (j=m_selectedEntries.size()-1; j>=0; --j)
|
|
|
|
if (m_selectedEntries[j])
|
|
|
|
break;
|
2008-03-15 09:54:45 +08:00
|
|
|
|
2011-06-27 06:07:19 +08:00
|
|
|
if (j-i+1 == i2-i) {
|
|
|
|
index1 = i;
|
|
|
|
index2 = j;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
2008-03-15 09:54:45 +08:00
|
|
|
}
|
|
|
|
|
2015-05-09 23:20:58 +08:00
|
|
|
void PaletteView::getSelectedEntries(PalettePicks& entries) const
|
2008-03-15 09:54:45 +08:00
|
|
|
{
|
2011-06-27 06:07:19 +08:00
|
|
|
entries = m_selectedEntries;
|
2007-09-19 07:57:02 +08:00
|
|
|
}
|
|
|
|
|
2015-05-10 06:55:33 +08:00
|
|
|
int PaletteView::getSelectedEntriesCount() const
|
|
|
|
{
|
|
|
|
return m_selectedEntries.picks();
|
|
|
|
}
|
|
|
|
|
2015-03-20 19:44:39 +08:00
|
|
|
app::Color PaletteView::getColorByPosition(const gfx::Point& pos)
|
2011-02-24 06:29:57 +08:00
|
|
|
{
|
2015-03-20 19:44:39 +08:00
|
|
|
gfx::Point relPos = pos - getBounds().getOrigin();
|
2011-02-24 06:29:57 +08:00
|
|
|
Palette* palette = get_current_palette();
|
2015-03-20 19:44:39 +08:00
|
|
|
for (int i=0; i<palette->size(); ++i) {
|
|
|
|
if (getPaletteEntryBounds(i).contains(relPos))
|
|
|
|
return app::Color::fromIndex(i);
|
2011-02-24 06:29:57 +08:00
|
|
|
}
|
2013-01-07 01:45:43 +08:00
|
|
|
return app::Color::fromMask();
|
2011-02-24 06:29:57 +08:00
|
|
|
}
|
|
|
|
|
2015-05-09 01:59:52 +08:00
|
|
|
int PaletteView::getBoxSize() const
|
|
|
|
{
|
|
|
|
return m_boxsize / guiscale();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PaletteView::setBoxSize(int boxsize)
|
|
|
|
{
|
|
|
|
m_boxsize = MID(4*guiscale(), boxsize, 32*guiscale());
|
|
|
|
|
|
|
|
if (m_delegate)
|
2015-05-09 04:00:44 +08:00
|
|
|
m_delegate->onPaletteViewChangeSize(m_boxsize / guiscale());
|
2015-05-09 01:59:52 +08:00
|
|
|
|
|
|
|
View* view = View::getView(this);
|
|
|
|
if (view)
|
|
|
|
view->layout();
|
|
|
|
}
|
|
|
|
|
2015-05-09 23:20:58 +08:00
|
|
|
void PaletteView::copyToClipboard()
|
|
|
|
{
|
|
|
|
if (current_editor) {
|
|
|
|
setClipboardEditor(current_editor);
|
|
|
|
m_clipboardEntries = m_selectedEntries;
|
|
|
|
|
|
|
|
startMarchingAnts();
|
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PaletteView::pasteFromClipboard()
|
|
|
|
{
|
|
|
|
if (m_clipboardEditor && m_clipboardEntries.picks()) {
|
|
|
|
if (m_delegate)
|
|
|
|
m_delegate->onPaletteViewPasteColors(
|
|
|
|
m_clipboardEditor, m_clipboardEntries, m_selectedEntries);
|
|
|
|
|
2015-05-10 06:55:33 +08:00
|
|
|
// We just hide the marching ants, the user can paste multiple
|
|
|
|
// times.
|
2015-05-09 23:20:58 +08:00
|
|
|
stopMarchingAnts();
|
2015-05-10 06:55:33 +08:00
|
|
|
invalidate();
|
2015-05-09 23:20:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-10 06:55:33 +08:00
|
|
|
void PaletteView::discardClipboardSelection()
|
|
|
|
{
|
|
|
|
bool redraw = false;
|
|
|
|
|
|
|
|
if (m_clipboardEditor) {
|
|
|
|
setClipboardEditor(nullptr);
|
|
|
|
redraw = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (areColorsInClipboard()) {
|
|
|
|
stopMarchingAnts();
|
|
|
|
redraw = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (redraw)
|
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
|
2015-05-09 23:20:58 +08:00
|
|
|
bool PaletteView::areColorsInClipboard() const
|
|
|
|
{
|
|
|
|
return isMarchingAntsRunning();
|
|
|
|
}
|
|
|
|
|
2011-04-03 00:14:07 +08:00
|
|
|
bool PaletteView::onProcessMessage(Message* msg)
|
2007-09-19 07:57:02 +08:00
|
|
|
{
|
2013-07-29 08:17:07 +08:00
|
|
|
switch (msg->type()) {
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2015-05-10 06:55:33 +08:00
|
|
|
case kFocusEnterMessage:
|
|
|
|
FocusEnter();
|
2015-05-09 23:20:58 +08:00
|
|
|
break;
|
|
|
|
|
2015-05-10 06:55:33 +08:00
|
|
|
case kKeyDownMessage:
|
2015-05-10 02:57:46 +08:00
|
|
|
case kKeyUpMessage:
|
2015-05-10 06:55:33 +08:00
|
|
|
case kMouseEnterMessage:
|
2015-05-10 02:57:46 +08:00
|
|
|
updateCopyFlag(msg);
|
|
|
|
break;
|
|
|
|
|
2013-04-05 08:53:29 +08:00
|
|
|
case kMouseDownMessage:
|
2015-03-21 02:29:44 +08:00
|
|
|
switch (m_hot.part) {
|
2015-03-23 23:25:32 +08:00
|
|
|
|
2015-03-21 02:29:44 +08:00
|
|
|
case Hit::COLOR:
|
|
|
|
m_state = State::SELECTING_COLOR;
|
|
|
|
break;
|
2015-03-23 23:25:32 +08:00
|
|
|
|
2015-03-21 02:29:44 +08:00
|
|
|
case Hit::OUTLINE:
|
|
|
|
m_state = State::DRAGGING_OUTLINE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-04-25 23:03:25 +08:00
|
|
|
captureMouse();
|
2015-03-21 02:29:44 +08:00
|
|
|
|
2015-03-20 06:45:44 +08:00
|
|
|
// Continue...
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2013-04-05 08:53:29 +08:00
|
|
|
case kMouseMoveMessage: {
|
2013-07-29 08:17:07 +08:00
|
|
|
MouseMessage* mouseMsg = static_cast<MouseMessage*>(msg);
|
2011-02-24 06:29:57 +08:00
|
|
|
|
2015-03-21 02:29:44 +08:00
|
|
|
if (m_state == State::SELECTING_COLOR) {
|
|
|
|
if (m_hot.part == Hit::COLOR) {
|
|
|
|
int idx = m_hot.color;
|
2011-06-27 06:07:19 +08:00
|
|
|
|
2015-03-21 02:29:44 +08:00
|
|
|
StatusBar::instance()->showColor(0, "",
|
|
|
|
app::Color::fromIndex(idx), 255);
|
2011-02-24 06:29:57 +08:00
|
|
|
|
2015-03-23 23:25:32 +08:00
|
|
|
if (hasCapture() && (idx != m_currentEntry || msg->type() == kMouseDownMessage)) {
|
2015-03-21 02:29:44 +08:00
|
|
|
if (!msg->ctrlPressed())
|
|
|
|
clearSelection();
|
2011-02-24 06:29:57 +08:00
|
|
|
|
2015-03-21 02:29:44 +08:00
|
|
|
if (msg->type() == kMouseMoveMessage)
|
|
|
|
selectRange(m_rangeAnchor, idx);
|
|
|
|
else
|
2015-05-10 06:55:33 +08:00
|
|
|
selectColor(idx, true);
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2015-03-21 02:29:44 +08:00
|
|
|
// Emit signal
|
2015-03-23 23:25:32 +08:00
|
|
|
if (m_delegate)
|
|
|
|
m_delegate->onPaletteViewIndexChange(idx, mouseMsg->buttons());
|
2015-03-21 02:29:44 +08:00
|
|
|
}
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
2011-02-24 06:29:57 +08:00
|
|
|
}
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2011-02-24 06:29:57 +08:00
|
|
|
if (hasCapture())
|
2012-01-06 06:45:03 +08:00
|
|
|
return true;
|
2011-02-24 06:29:57 +08:00
|
|
|
|
2007-09-19 07:57:02 +08:00
|
|
|
break;
|
2011-02-24 06:29:57 +08:00
|
|
|
}
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2013-04-05 08:53:29 +08:00
|
|
|
case kMouseUpMessage:
|
2015-03-21 02:29:44 +08:00
|
|
|
if (hasCapture()) {
|
2015-03-21 01:46:48 +08:00
|
|
|
releaseMouse();
|
2015-03-21 02:29:44 +08:00
|
|
|
|
2015-03-23 23:25:32 +08:00
|
|
|
if (m_state == State::DRAGGING_OUTLINE &&
|
|
|
|
m_hot.part == Hit::COLOR) {
|
|
|
|
dropColors(m_hot.color + (m_hot.after ? 1: 0));
|
|
|
|
}
|
|
|
|
|
2015-03-21 02:29:44 +08:00
|
|
|
m_state = State::WAITING;
|
|
|
|
invalidate();
|
|
|
|
}
|
2010-01-31 00:43:13 +08:00
|
|
|
return true;
|
2011-02-20 10:44:17 +08:00
|
|
|
|
2013-04-05 08:53:29 +08:00
|
|
|
case kMouseWheelMessage: {
|
2011-02-21 05:35:21 +08:00
|
|
|
View* view = View::getView(this);
|
2015-03-24 00:28:16 +08:00
|
|
|
if (!view)
|
|
|
|
break;
|
|
|
|
|
|
|
|
gfx::Point delta = static_cast<MouseMessage*>(msg)->wheelDelta();
|
|
|
|
|
|
|
|
if (msg->onlyCtrlPressed()) {
|
|
|
|
int z = delta.x - delta.y;
|
2015-05-09 04:00:44 +08:00
|
|
|
setBoxSize(m_boxsize / guiscale() + z);
|
2015-03-24 00:28:16 +08:00
|
|
|
}
|
|
|
|
else {
|
2012-01-06 06:45:03 +08:00
|
|
|
gfx::Point scroll = view->getViewScroll();
|
2015-03-24 00:28:16 +08:00
|
|
|
scroll += delta * 3 * m_boxsize;
|
2012-01-06 06:45:03 +08:00
|
|
|
view->setViewScroll(scroll);
|
2011-02-20 10:44:17 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-04-05 08:53:29 +08:00
|
|
|
case kMouseLeaveMessage:
|
2012-07-10 00:20:58 +08:00
|
|
|
StatusBar::instance()->clearText();
|
2015-03-21 01:46:48 +08:00
|
|
|
m_hot = Hit(Hit::NONE);
|
|
|
|
invalidate();
|
2011-02-24 06:29:57 +08:00
|
|
|
break;
|
|
|
|
|
2015-03-21 01:46:48 +08:00
|
|
|
case kSetCursorMessage: {
|
|
|
|
MouseMessage* mouseMsg = static_cast<MouseMessage*>(msg);
|
|
|
|
Hit hit = hitTest(mouseMsg->position() - getBounds().getOrigin());
|
|
|
|
if (hit != m_hot) {
|
|
|
|
m_hot = hit;
|
|
|
|
invalidate();
|
|
|
|
}
|
2015-05-10 02:57:46 +08:00
|
|
|
setCursor();
|
2015-03-21 01:46:48 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-09-19 07:57:02 +08:00
|
|
|
}
|
|
|
|
|
2010-08-03 08:29:56 +08:00
|
|
|
return Widget::onProcessMessage(msg);
|
2007-09-19 07:57:02 +08:00
|
|
|
}
|
|
|
|
|
2014-04-14 03:26:00 +08:00
|
|
|
void PaletteView::onPaint(ui::PaintEvent& ev)
|
|
|
|
{
|
2015-03-20 06:45:12 +08:00
|
|
|
SkinTheme* theme = static_cast<SkinTheme*>(getTheme());
|
|
|
|
int outlineWidth = theme->dimensions.paletteOutlineWidth();
|
2014-04-14 03:26:00 +08:00
|
|
|
ui::Graphics* g = ev.getGraphics();
|
|
|
|
gfx::Rect bounds = getClientBounds();
|
|
|
|
Palette* palette = get_current_palette();
|
|
|
|
|
2015-03-21 01:46:48 +08:00
|
|
|
g->fillRect(gfx::rgba(0, 0, 0), bounds);
|
2014-04-14 03:26:00 +08:00
|
|
|
|
2015-03-20 06:45:12 +08:00
|
|
|
// Draw palette entries
|
2015-03-20 19:44:39 +08:00
|
|
|
for (int i=0; i<palette->size(); ++i) {
|
|
|
|
gfx::Rect box = getPaletteEntryBounds(i);
|
|
|
|
gfx::Color color = gfx::rgba(
|
|
|
|
rgba_getr(palette->getEntry(i)),
|
|
|
|
rgba_getg(palette->getEntry(i)),
|
|
|
|
rgba_getb(palette->getEntry(i)));
|
|
|
|
|
|
|
|
g->fillRect(color, box);
|
2015-03-21 01:46:48 +08:00
|
|
|
|
|
|
|
if (m_currentEntry == i)
|
|
|
|
g->fillRect(color_utils::blackandwhite_neg(color),
|
|
|
|
gfx::Rect(box.getCenter(), gfx::Size(1, 1)));
|
2015-03-20 06:45:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw selected entries
|
2015-03-21 01:46:48 +08:00
|
|
|
Style::State state = Style::active();
|
|
|
|
if (m_hot.part == Hit::OUTLINE) state += Style::hover();
|
|
|
|
|
2015-03-20 19:44:39 +08:00
|
|
|
for (int i=0; i<palette->size(); ++i) {
|
|
|
|
if (!m_selectedEntries[i])
|
|
|
|
continue;
|
|
|
|
|
2015-05-09 23:20:58 +08:00
|
|
|
gfx::Rect box, clipR;
|
|
|
|
getEntryBoundsAndClip(i, m_selectedEntries, box, clipR, outlineWidth);
|
2015-03-20 19:44:39 +08:00
|
|
|
|
|
|
|
IntersectClip clip(g, clipR);
|
|
|
|
if (clip) {
|
|
|
|
theme->styles.timelineRangeOutline()->paint(g, box,
|
2015-03-21 01:46:48 +08:00
|
|
|
NULL, state);
|
2014-04-14 03:26:00 +08:00
|
|
|
}
|
|
|
|
}
|
2015-03-21 02:29:44 +08:00
|
|
|
|
2015-05-09 23:20:58 +08:00
|
|
|
// Draw marching ants
|
|
|
|
if (isMarchingAntsRunning() &&
|
|
|
|
m_clipboardEditor &&
|
|
|
|
m_clipboardEditor == current_editor) {
|
|
|
|
for (int i=0; i<palette->size(); ++i) {
|
|
|
|
if (!m_clipboardEntries[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
gfx::Rect box, clipR;
|
|
|
|
getEntryBoundsAndClip(i, m_clipboardEntries, box, clipR, outlineWidth);
|
|
|
|
|
|
|
|
IntersectClip clip(g, clipR);
|
|
|
|
if (clip) {
|
|
|
|
CheckedDrawMode checked(g, getMarchingAntsOffset());
|
|
|
|
g->drawRect(gfx::rgba(0, 0, 0), box);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-21 02:29:44 +08:00
|
|
|
// Draw drop target
|
|
|
|
if (m_state == State::DRAGGING_OUTLINE) {
|
|
|
|
if (m_hot.part == Hit::COLOR) {
|
|
|
|
gfx::Rect box = getPaletteEntryBounds(m_hot.color);
|
|
|
|
if (m_hot.after)
|
|
|
|
box.x += box.w+guiscale();
|
|
|
|
|
|
|
|
box.x -= 3*guiscale();
|
|
|
|
box.y -= 3*guiscale();
|
|
|
|
box.w = 5*guiscale();
|
|
|
|
box.h += 6*guiscale();
|
|
|
|
|
|
|
|
theme->styles.timelineDropFrameDeco()->paint(g,
|
|
|
|
box, NULL, Style::active());
|
|
|
|
}
|
|
|
|
}
|
2014-04-14 03:26:00 +08:00
|
|
|
}
|
|
|
|
|
2013-05-12 04:56:27 +08:00
|
|
|
void PaletteView::onResize(ui::ResizeEvent& ev)
|
|
|
|
{
|
|
|
|
if (!m_isUpdatingColumns) {
|
|
|
|
m_isUpdatingColumns = true;
|
|
|
|
View* view = View::getView(this);
|
|
|
|
if (view) {
|
|
|
|
int columns =
|
|
|
|
(view->getViewportBounds().w-this->child_spacing*2)
|
|
|
|
/ (m_boxsize+this->child_spacing);
|
|
|
|
setColumns(MID(1, columns, Palette::MaxColors));
|
|
|
|
}
|
|
|
|
m_isUpdatingColumns = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget::onResize(ev);
|
|
|
|
}
|
|
|
|
|
2012-09-27 05:34:52 +08:00
|
|
|
void PaletteView::onPreferredSize(ui::PreferredSizeEvent& ev)
|
|
|
|
{
|
|
|
|
gfx::Size sz;
|
|
|
|
request_size(&sz.w, &sz.h);
|
|
|
|
ev.setPreferredSize(sz);
|
|
|
|
}
|
|
|
|
|
2015-05-09 23:20:58 +08:00
|
|
|
void PaletteView::onDrawMarchingAnts()
|
|
|
|
{
|
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PaletteView::onDestroyEditor(Editor* editor)
|
|
|
|
{
|
2015-05-10 06:55:33 +08:00
|
|
|
if (m_clipboardEditor == editor)
|
|
|
|
discardClipboardSelection();
|
2015-05-09 23:20:58 +08:00
|
|
|
}
|
|
|
|
|
2011-01-28 20:28:54 +08:00
|
|
|
void PaletteView::request_size(int* w, int* h)
|
2007-09-19 07:57:02 +08:00
|
|
|
{
|
2012-05-03 10:09:34 +08:00
|
|
|
div_t d = div(Palette::MaxColors, m_columns);
|
2010-04-11 04:01:56 +08:00
|
|
|
int cols = m_columns;
|
2007-09-19 07:57:02 +08:00
|
|
|
int rows = d.quot + ((d.rem)? 1: 0);
|
|
|
|
|
2010-04-11 04:01:56 +08:00
|
|
|
*w = this->border_width.l + this->border_width.r +
|
2011-02-20 09:41:09 +08:00
|
|
|
+ cols*m_boxsize + (cols-1)*this->child_spacing;
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2010-04-11 04:01:56 +08:00
|
|
|
*h = this->border_width.t + this->border_width.b +
|
2011-02-20 09:41:09 +08:00
|
|
|
+ rows*m_boxsize + (rows-1)*this->child_spacing;
|
2007-09-19 07:57:02 +08:00
|
|
|
}
|
|
|
|
|
2011-01-28 20:28:54 +08:00
|
|
|
void PaletteView::update_scroll(int color)
|
2007-09-19 07:57:02 +08:00
|
|
|
{
|
2011-02-21 05:35:21 +08:00
|
|
|
View* view = View::getView(this);
|
|
|
|
if (!view)
|
|
|
|
return;
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2011-02-21 05:35:21 +08:00
|
|
|
gfx::Rect vp = view->getViewportBounds();
|
|
|
|
gfx::Point scroll;
|
|
|
|
int x, y, cols;
|
|
|
|
div_t d;
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2011-02-21 05:35:21 +08:00
|
|
|
scroll = view->getViewScroll();
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2012-05-03 10:09:34 +08:00
|
|
|
d = div(Palette::MaxColors, m_columns);
|
2011-02-21 05:35:21 +08:00
|
|
|
cols = m_columns;
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2011-02-21 05:35:21 +08:00
|
|
|
y = (m_boxsize+this->child_spacing) * (color / cols);
|
|
|
|
x = (m_boxsize+this->child_spacing) * (color % cols);
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2011-02-21 05:35:21 +08:00
|
|
|
if (scroll.x > x)
|
|
|
|
scroll.x = x;
|
|
|
|
else if (scroll.x+vp.w-m_boxsize-2 < x)
|
|
|
|
scroll.x = x-vp.w+m_boxsize+2;
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2011-02-21 05:35:21 +08:00
|
|
|
if (scroll.y > y)
|
|
|
|
scroll.y = y;
|
|
|
|
else if (scroll.y+vp.h-m_boxsize-2 < y)
|
|
|
|
scroll.y = y-vp.h+m_boxsize+2;
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2011-02-21 05:35:21 +08:00
|
|
|
view->setViewScroll(scroll);
|
2007-09-19 07:57:02 +08:00
|
|
|
}
|
2013-08-06 08:20:19 +08:00
|
|
|
|
2014-06-02 05:09:19 +08:00
|
|
|
void PaletteView::onAppPaletteChange()
|
|
|
|
{
|
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
|
2015-05-09 23:20:58 +08:00
|
|
|
gfx::Rect PaletteView::getPaletteEntryBounds(int index) const
|
2015-03-20 19:44:39 +08:00
|
|
|
{
|
|
|
|
gfx::Rect bounds = getClientBounds();
|
|
|
|
int cols = m_columns;
|
|
|
|
int col = index % cols;
|
|
|
|
int row = index / cols;
|
|
|
|
|
|
|
|
return gfx::Rect(
|
|
|
|
bounds.x + this->border_width.l + col*(m_boxsize+this->child_spacing),
|
|
|
|
bounds.y + this->border_width.t + row*(m_boxsize+this->child_spacing),
|
|
|
|
m_boxsize, m_boxsize);
|
|
|
|
}
|
|
|
|
|
2015-03-21 01:46:48 +08:00
|
|
|
PaletteView::Hit PaletteView::hitTest(const gfx::Point& pos)
|
|
|
|
{
|
|
|
|
SkinTheme* theme = static_cast<SkinTheme*>(getTheme());
|
|
|
|
int outlineWidth = theme->dimensions.paletteOutlineWidth();
|
|
|
|
Palette* palette = get_current_palette();
|
|
|
|
|
2015-03-23 23:25:32 +08:00
|
|
|
if (m_state == State::WAITING && m_editable) {
|
2015-03-21 01:46:48 +08:00
|
|
|
// First check if the mouse is inside the selection outline.
|
|
|
|
for (int i=0; i<palette->size(); ++i) {
|
|
|
|
if (!m_selectedEntries[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const int max = Palette::MaxColors;
|
|
|
|
bool top = (i >= m_columns && i-m_columns >= 0 ? m_selectedEntries[i-m_columns]: false);
|
|
|
|
bool bottom = (i < max-m_columns && i+m_columns < max ? m_selectedEntries[i+m_columns]: false);
|
|
|
|
bool left = ((i%m_columns)>0 && i-1 >= 0 ? m_selectedEntries[i-1]: false);
|
|
|
|
bool right = ((i%m_columns)<m_columns-1 && i+1 < max ? m_selectedEntries[i+1]: false);
|
|
|
|
|
|
|
|
gfx::Rect box = getPaletteEntryBounds(i);
|
|
|
|
box.enlarge(outlineWidth);
|
|
|
|
|
|
|
|
if ((!top && gfx::Rect(box.x, box.y, box.w, outlineWidth).contains(pos)) ||
|
2015-03-21 02:29:44 +08:00
|
|
|
(!bottom && gfx::Rect(box.x, box.y+box.h-outlineWidth, box.w, outlineWidth).contains(pos)) ||
|
|
|
|
(!left && gfx::Rect(box.x, box.y, outlineWidth, box.h).contains(pos)) ||
|
|
|
|
(!right && gfx::Rect(box.x+box.w-outlineWidth, box.y, outlineWidth, box.h).contains(pos)))
|
2015-03-21 01:46:48 +08:00
|
|
|
return Hit(Hit::OUTLINE, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we are inside a color.
|
|
|
|
for (int i=0; i<palette->size(); ++i) {
|
|
|
|
gfx::Rect box = getPaletteEntryBounds(i);
|
|
|
|
box.w += child_spacing;
|
|
|
|
box.h += child_spacing;
|
2015-03-21 02:29:44 +08:00
|
|
|
if (box.contains(pos)) {
|
|
|
|
Hit hit(Hit::COLOR, i);
|
|
|
|
hit.after = (pos.x > box.x+box.w/2);
|
|
|
|
return hit;
|
|
|
|
}
|
2015-03-21 01:46:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Hit(Hit::NONE);
|
|
|
|
}
|
|
|
|
|
2015-03-23 23:25:32 +08:00
|
|
|
void PaletteView::dropColors(int beforeIndex)
|
|
|
|
{
|
2015-05-10 02:57:46 +08:00
|
|
|
Palette palette(*get_current_palette());
|
|
|
|
Palette newPalette(palette);
|
|
|
|
Remap remap(palette.size());
|
2015-03-23 23:25:32 +08:00
|
|
|
|
2015-05-10 02:57:46 +08:00
|
|
|
// Copy colors
|
|
|
|
if (m_copy) {
|
|
|
|
int picks = m_selectedEntries.picks();
|
|
|
|
remap = create_remap_to_expand_palette(palette.size(), picks, beforeIndex);
|
|
|
|
|
|
|
|
for (int i=0; i<palette.size(); ++i)
|
|
|
|
newPalette.setEntry(remap[i], palette.getEntry(i));
|
|
|
|
|
|
|
|
for (int i=0, j=0; i<palette.size(); ++i) {
|
|
|
|
if (m_selectedEntries[i])
|
|
|
|
newPalette.setEntry(beforeIndex + (j++), palette.getEntry(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i=0, j=0; i<palette.size(); ++i) {
|
|
|
|
if (m_selectedEntries[i]) {
|
|
|
|
if (m_currentEntry == i) {
|
|
|
|
m_currentEntry = beforeIndex + j;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i=0; i<palette.size(); ++i)
|
|
|
|
m_selectedEntries[i] = (i >= beforeIndex && i < beforeIndex + picks);
|
2015-03-23 23:25:32 +08:00
|
|
|
}
|
2015-05-10 02:57:46 +08:00
|
|
|
// Move colors
|
|
|
|
else {
|
|
|
|
remap = create_remap_to_move_picks(m_selectedEntries, beforeIndex);
|
2015-03-23 23:25:32 +08:00
|
|
|
|
2015-05-10 02:57:46 +08:00
|
|
|
auto oldSelectedCopies = m_selectedEntries;
|
|
|
|
for (int i=0; i<palette.size(); ++i) {
|
|
|
|
newPalette.setEntry(remap[i], palette.getEntry(i));
|
|
|
|
m_selectedEntries[remap[i]] = oldSelectedCopies[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
m_currentEntry = remap[m_currentEntry];
|
|
|
|
}
|
2015-03-23 23:25:32 +08:00
|
|
|
|
|
|
|
if (m_delegate) {
|
2015-05-10 02:57:46 +08:00
|
|
|
m_delegate->onPaletteViewRemapColors(remap, &newPalette);
|
2015-03-23 23:25:32 +08:00
|
|
|
m_delegate->onPaletteViewIndexChange(m_currentEntry, ui::kButtonLeft);
|
|
|
|
}
|
|
|
|
|
2015-05-10 02:57:46 +08:00
|
|
|
set_current_palette(&newPalette, false);
|
2015-03-23 23:25:32 +08:00
|
|
|
getManager()->invalidate();
|
|
|
|
}
|
|
|
|
|
2015-05-09 23:20:58 +08:00
|
|
|
void PaletteView::getEntryBoundsAndClip(int i, const PalettePicks& entries,
|
|
|
|
gfx::Rect& box, gfx::Rect& clip,
|
|
|
|
int outlineWidth) const
|
|
|
|
{
|
|
|
|
box = clip = getPaletteEntryBounds(i);
|
|
|
|
box.enlarge(outlineWidth);
|
|
|
|
|
|
|
|
// Left
|
|
|
|
if (!pickedXY(entries, i, -1, 0)) {
|
|
|
|
clip.x -= outlineWidth;
|
|
|
|
clip.w += outlineWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Top
|
|
|
|
if (!pickedXY(entries, i, 0, -1)) {
|
|
|
|
clip.y -= outlineWidth;
|
|
|
|
clip.h += outlineWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Right
|
|
|
|
if (!pickedXY(entries, i, +1, 0))
|
|
|
|
clip.w += outlineWidth;
|
|
|
|
else {
|
|
|
|
clip.w += guiscale();
|
|
|
|
box.w += outlineWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bottom
|
|
|
|
if (!pickedXY(entries, i, 0, +1))
|
|
|
|
clip.h += outlineWidth;
|
|
|
|
else {
|
|
|
|
clip.h += guiscale();
|
|
|
|
box.h += outlineWidth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PaletteView::pickedXY(const doc::PalettePicks& entries, int i, int dx, int dy) const
|
|
|
|
{
|
|
|
|
int x = (i % m_columns) + dx;
|
|
|
|
int y = (i / m_columns) + dy;
|
|
|
|
|
|
|
|
if (x < 0 || x >= m_columns || y < 0 || y > (Palette::MaxColors-1)/m_columns)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
i = x + y*m_columns;
|
|
|
|
if (i >= 0 && i < entries.size())
|
|
|
|
return entries[i];
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PaletteView::setClipboardEditor(Editor* editor)
|
|
|
|
{
|
|
|
|
if (m_clipboardEditor)
|
|
|
|
m_clipboardEditor->removeObserver(this);
|
|
|
|
|
|
|
|
m_clipboardEditor = editor;
|
|
|
|
|
|
|
|
if (m_clipboardEditor)
|
|
|
|
m_clipboardEditor->addObserver(this);
|
|
|
|
}
|
|
|
|
|
2015-05-10 02:57:46 +08:00
|
|
|
void PaletteView::updateCopyFlag(ui::Message* msg)
|
|
|
|
{
|
|
|
|
bool oldCopy = m_copy;
|
|
|
|
m_copy = (msg->ctrlPressed() || msg->altPressed());
|
|
|
|
if (oldCopy != m_copy)
|
|
|
|
setCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PaletteView::setCursor()
|
|
|
|
{
|
|
|
|
if (m_state == State::DRAGGING_OUTLINE ||
|
|
|
|
m_hot.part == Hit::OUTLINE) {
|
|
|
|
if (m_copy)
|
|
|
|
ui::set_mouse_cursor(kArrowPlusCursor);
|
|
|
|
else
|
|
|
|
ui::set_mouse_cursor(kMoveCursor);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ui::set_mouse_cursor(kArrowCursor);
|
|
|
|
}
|
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
} // namespace app
|