Copy hex text to native clipboard when copy colors from the palette (fix #4289)

This commit is contained in:
David Capello 2024-01-31 11:45:12 -03:00
parent e47448ca24
commit 40863a3a5e
5 changed files with 85 additions and 13 deletions

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2019-2023 Igara Studio S.A.
// Copyright (C) 2019-2024 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This program is distributed under the terms of
@ -227,7 +227,8 @@ void Clipboard::setData(Image* image,
else
m_data->image.reset(image);
if (set_native_clipboard) {
if (set_native_clipboard &&
use_native_clipboard()) {
// Copy tilemap to the native clipboard
if (isTilemap) {
ASSERT(tileset);
@ -242,8 +243,7 @@ void Clipboard::setData(Image* image,
image->setMaskColor(-1);
}
if (use_native_clipboard())
setNativeBitmap(image, mask, palette);
setNativeBitmap(image, mask, palette);
if (image && !image_source_is_transparent)
image->setMaskColor(oldMask);
@ -456,8 +456,13 @@ void Clipboard::copyPalette(const Palette* palette,
nullptr,
new Palette(*palette),
nullptr,
true, // set native clipboard
false, // Don't touch the native clipboard now
false);
// Here is where we copy the palette as text (hex format)
if (use_native_clipboard())
setNativePalette(palette, picks);
m_data->picks = picks;
}

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2019-2023 Igara Studio S.A.
// Copyright (C) 2019-2024 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This program is distributed under the terms of
@ -114,6 +114,9 @@ namespace app {
doc::Tileset** tileset);
bool getNativeBitmapSize(gfx::Size* size);
bool setNativePalette(const doc::Palette* palette,
const doc::PalettePicks& picks);
struct Data;
std::unique_ptr<Data> m_data;
};

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2020-2022 Igara Studio S.A.
// Copyright (C) 2020-2024 Igara Studio S.A.
// Copyright (C) 2016-2018 David Capello
//
// This program is distributed under the terms of
@ -15,6 +15,7 @@
#include "base/serialization.h"
#include "clip/clip.h"
#include "doc/color_scales.h"
#include "doc/file/hex_file.h"
#include "doc/image.h"
#include "doc/image_impl.h"
#include "doc/image_io.h"
@ -27,6 +28,7 @@
#include "ui/alert.h"
#include <sstream>
#include <string>
#include <vector>
namespace app {
@ -332,4 +334,26 @@ bool Clipboard::getNativeBitmapSize(gfx::Size* size)
return false;
}
bool Clipboard::setNativePalette(const doc::Palette* palette,
const doc::PalettePicks& picks)
{
clip::lock l(native_window_handle());
if (!l.locked())
return false;
l.clear();
// Save the palette in hex format as text
std::stringstream os;
doc::file::save_hex_file(
palette, &picks,
true, // include '#' on each line
false, // don't include a EOL char at the end (so we can copy one color without \n chars)
os);
std::string value = os.str();
l.set_data(clip::text_format(), value.c_str(), value.size());
return true;
}
} // namespace app

View File

@ -1,5 +1,5 @@
// Aseprite Document Library
// Copyright (c) 2022 Igara Studio S.A.
// Copyright (c) 2022-2024 Igara Studio S.A.
// Copyright (c) 2016-2018 David Capello
//
// This file is released under the terms of the MIT license.
@ -9,10 +9,13 @@
#include "config.h"
#endif
#include "doc/file/hex_file.h"
#include "base/fstream_path.h"
#include "base/hex.h"
#include "base/trim_string.h"
#include "doc/palette.h"
#include "doc/palette_picks.h"
#include <cctype>
#include <fstream>
@ -71,17 +74,47 @@ std::unique_ptr<Palette> load_hex_file(const char *filename)
bool save_hex_file(const Palette *pal, const char *filename)
{
std::ofstream f(FSTREAM_PATH(filename));
if (f.bad()) return false;
if (f.bad())
return false;
save_hex_file(pal, nullptr,
false, // don't include '#' per line
true, // include a EOL char at the end
f);
return true;
}
void save_hex_file(const Palette* pal,
const PalettePicks* picks,
const bool include_hash_char,
const bool final_eol,
std::ostream& f)
{
bool first = true;
f << std::hex << std::setfill('0');
for (int i=0; i<pal->size(); ++i) {
if (picks && !(*picks)[i])
continue;
if (!final_eol) {
if (first)
first = false;
else
f << "\n";
}
uint32_t col = pal->getEntry(i);
if (include_hash_char)
f << '#';
f << std::setw(2) << ((int)rgba_getr(col))
<< std::setw(2) << ((int)rgba_getg(col))
<< std::setw(2) << ((int)rgba_getb(col)) << "\n";
}
<< std::setw(2) << ((int)rgba_getb(col));
return true;
if (final_eol)
f << "\n";
}
}
} // namespace file

View File

@ -1,5 +1,5 @@
// Aseprite Document Library
// Copyright (c) 2022 Igara Studio S.A.
// Copyright (c) 2022-2024 Igara Studio S.A.
// Copyright (c) 2016 David Capello
//
// This file is released under the terms of the MIT license.
@ -9,16 +9,23 @@
#define DOC_FILE_HEX_FILE_H_INCLUDED
#pragma once
#include <iosfwd>
#include <memory>
namespace doc {
class Palette;
class PalettePicks;
namespace file {
std::unique_ptr<Palette> load_hex_file(const char* filename);
bool save_hex_file(const Palette* pal, const char* filename);
void save_hex_file(const Palette* palette,
const PalettePicks* picks,
const bool include_hash_char,
const bool final_eol,
std::ostream& f);
} // namespace file
} // namespace doc