Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color Clipboard #3100

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
10 changes: 6 additions & 4 deletions src/gui/item_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@

#include "gui/item_action.hpp"

ItemAction::ItemAction(const std::string& text, int id, std::function<void()> callback) :
MenuItem(text, id),
ItemAction::ItemAction(const std::string& text, int id, std::function<void()> callback, const Color& text_color) :
MenuItem(text, id, text_color),
m_callback(std::move(callback))
{
}

void
ItemAction::process_action(const MenuAction& action)
{
if (action == MenuAction::HIT) {
if (m_callback) {
if (action == MenuAction::HIT)
{
if (m_callback)
{
m_callback();
}
}
bruhmoent marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
2 changes: 1 addition & 1 deletion src/gui/item_action.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
class ItemAction final : public MenuItem
{
public:
ItemAction(const std::string& text, int id = -1, std::function<void()> callback = {});
ItemAction(const std::string& text, int id = -1, std::function<void()> callback = {}, const Color& text_color = Color(1.f, 1.f, 1.f));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we have a Color::WHITE constant somewhere?


virtual void process_action(const MenuAction& action) override;

Expand Down
12 changes: 12 additions & 0 deletions src/gui/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,18 @@ Menu::add_entry(const std::string& text, const std::function<void()>& callback)
return add_item<ItemAction>(text, -1, callback);
}

ItemAction&
Menu::add_entry(int id, const std::string& text, const Color& text_color)
{
return add_item<ItemAction>(text, id, [](){}, text_color);
}

ItemAction&
Menu::add_entry(const std::string& text, const std::function<void()>& callback, const Color& text_color)
{
return add_item<ItemAction>(text, -1, callback, text_color);
}

ItemInactive&
Menu::add_inactive(const std::string& text, bool default_color)
{
Expand Down
2 changes: 2 additions & 0 deletions src/gui/menu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ class Menu
ItemHorizontalLine& add_hl();
ItemLabel& add_label(const std::string& text);
ItemAction& add_entry(int id, const std::string& text);
ItemAction& add_entry(int id, const std::string& text, const Color& text_color);
ItemAction& add_entry(const std::string& text, const std::function<void()>& callback);
ItemAction& add_entry(const std::string& text, const std::function<void()>& callback, const Color& text_color);
ItemToggle& add_toggle(int id, const std::string& text, bool* toggled, bool center_text = false);
ItemToggle& add_toggle(int id, const std::string& text,
const std::function<bool()>& get_func,
Expand Down
95 changes: 95 additions & 0 deletions src/gui/menu_color.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SuperTux
// Copyright (C) 2015 Hume2 <[email protected]>
// 2024 bruhmoent
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
Expand All @@ -14,7 +15,12 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#include <regex>
#include <sstream>

#include "gui/menu_color.hpp"
#include "menu_item.hpp"
#include "../util/log.hpp"
bruhmoent marked this conversation as resolved.
Show resolved Hide resolved

#include "util/gettext.hpp"

Expand All @@ -31,13 +37,102 @@ ColorMenu::ColorMenu(Color* color_) :
add_color_channel_rgba(&(color->alpha), Color::BLACK, -1, true);
add_color_display(color);

add_hl();
add_entry(MNID_COPY, _("Copy"));
if (Color::s_clipboard_color != nullptr)
add_entry(MNID_PASTE, _("Paste"), *Color::s_clipboard_color);
else
add_entry(MNID_PASTE, _("Paste"), Color(1.f, 1.f, 1.f));

add_hl();
add_entry(MNID_PASTE_CLIPBOARD, _("Paste clipboard"));
bruhmoent marked this conversation as resolved.
Show resolved Hide resolved

add_hl();
add_back(_("OK"));
}

void
ColorMenu::menu_action(MenuItem& item)
{
if (item.get_id() == MNID_COPY)
{
if (color)
{
Color::s_clipboard_color = std::make_unique<Color>(*color);
MenuItem& menu_paste_item = get_item_by_id(MNID_PASTE);
menu_paste_item.set_text_color(*color);
}
}
else if (item.get_id() == MNID_PASTE)
{
if (Color::s_clipboard_color)
*color = *Color::s_clipboard_color;
}
else if (item.get_id() == MNID_PASTE_CLIPBOARD)
{
if (!SDL_HasClipboardText())
return;

const char* clipboard_text = SDL_GetClipboardText();
if (!clipboard_text)
return;

const std::string text(clipboard_text);
SDL_free(const_cast<char*>(clipboard_text));

Color new_color;
bool is_valid_format = false;

// rgb(r,g,b)
const std::regex rgb_format(R"(^\s*rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)\s*$)");
std::smatch rgb_matches;

if (std::regex_match(text, rgb_matches, rgb_format))
{
const int r = std::stoi(rgb_matches[1].str());
const int g = std::stoi(rgb_matches[2].str());
const int b = std::stoi(rgb_matches[3].str());

if (r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255)
bruhmoent marked this conversation as resolved.
Show resolved Hide resolved
{
new_color = Color(static_cast<float>(r) / 255.0f, static_cast<float>(g) / 255.0f, static_cast<float>(b) / 255.0f, 1.0f);
is_valid_format = true;
}
}
else
{
// #rrggbb
const std::regex hex_format(R"(^\s*#([A-Fa-f0-9]{6})\s*$)");
std::smatch hex_matches;

if (std::regex_match(text, hex_matches, hex_format))
{
const std::string hex_value = hex_matches[1].str();
unsigned int hex_color;
std::stringstream ss;
ss << std::hex << hex_value;
ss >> hex_color;

const float r = ((hex_color >> 16) & 0xFF) / 255.0f;
const float g = ((hex_color >> 8) & 0xFF) / 255.0f;
const float b = (hex_color & 0xFF) / 255.0f;

new_color = Color(r, g, b, 1.0f);
is_valid_format = true;
}
}

if (is_valid_format)
{
*color = new_color;

Color::s_clipboard_color = std::make_unique<Color>(new_color);
MenuItem& menu_paste_item = get_item_by_id(MNID_PASTE);
menu_paste_item.set_text_color(new_color);
}
else
log_warning << "Invalid color format: " << text << ". Supported formats: rgb(r,g,b) and #rrggbb" << std::endl;
}
bruhmoent marked this conversation as resolved.
Show resolved Hide resolved
}

/* EOF */
8 changes: 8 additions & 0 deletions src/gui/menu_color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ class ColorMenu final : public Menu
private:
ColorMenu(const ColorMenu&) = delete;
ColorMenu& operator=(const ColorMenu&) = delete;

private:
enum MenuIDs
{
MNID_COPY = 1,
MNID_PASTE,
MNID_PASTE_CLIPBOARD
};
};

#endif
Expand Down
25 changes: 16 additions & 9 deletions src/gui/menu_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,17 @@
#include "supertux/resources.hpp"
#include "video/drawing_context.hpp"

//static const float FLICK_CURSOR_TIME = 0.5f;

MenuItem::MenuItem(const std::string& text, int id) :
MenuItem::MenuItem(const std::string& text, int id, const std::optional<Color>& text_color) :
m_id(id),
m_text(text),
m_help(),
m_font(Resources::normal_font)
m_font(Resources::normal_font),
m_text_color(text_color)
{
}

MenuItem::~MenuItem() {

MenuItem::~MenuItem()
{
}

void
Expand All @@ -59,12 +58,20 @@ MenuItem::draw(DrawingContext& context, const Vector& pos, int menu_width, bool
}

Color
MenuItem::get_color() const {
return ColorScheme::Menu::default_color;
MenuItem::get_color() const
{
return m_text_color.value_or(ColorScheme::Menu::default_color);
}

void
MenuItem::set_text_color(const Color& color)
{
m_text_color = color;
}

int
MenuItem::get_width() const {
MenuItem::get_width() const
{
return static_cast<int>(m_font->get_text_width(m_text)) + 16;
}

Expand Down
5 changes: 4 additions & 1 deletion src/gui/menu_item.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
#define HEADER_SUPERTUX_GUI_MENU_ITEM_HPP

#include "gui/menu.hpp"
#include <optional>

class MenuItem
{
public:
MenuItem(const std::string& text, int id = -1);
explicit MenuItem(const std::string& text, int id = -1, const std::optional<Color>& text_color = std::nullopt);
virtual ~MenuItem();

int get_id() const { return m_id; }
Expand Down Expand Up @@ -65,6 +66,7 @@ class MenuItem
virtual void event(const SDL_Event& ev) { }

virtual Color get_color() const;
virtual void set_text_color(const Color& color);

/** Returns true when the MenuManager shouldn't do anything else. */
virtual bool no_other_action() const {
Expand All @@ -86,6 +88,7 @@ class MenuItem
std::string m_text;
std::string m_help;
FontPtr m_font;
std::optional<Color> m_text_color;
bruhmoent marked this conversation as resolved.
Show resolved Hide resolved

private:
MenuItem(const MenuItem&) = delete;
Expand Down
2 changes: 2 additions & 0 deletions src/video/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const Color Color::MAGENTA(1.0, 0.0, 1.0);
const Color Color::YELLOW(1.0, 1.0, 0.0);
const Color Color::WHITE(1.0, 1.0, 1.0);

std::unique_ptr<Color> Color::s_clipboard_color = nullptr;
bruhmoent marked this conversation as resolved.
Show resolved Hide resolved

Color::Color() :
red(0),
green(0),
Expand Down
3 changes: 3 additions & 0 deletions src/video/color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <string>
#include <vector>
#include <math.h>
#include <memory>

#include <SDL_image.h>

Expand Down Expand Up @@ -65,6 +66,8 @@ class Color final
static const Color YELLOW;
static const Color WHITE;

static std::unique_ptr<Color> s_clipboard_color;

public:
static Color from_rgb888(uint8_t r, uint8_t g, uint8_t b)
{
Expand Down
Loading