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
41 changes: 41 additions & 0 deletions src/gui/color_clipboard.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SuperTux
// Copyright (C) 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
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// 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 "color_clipboard.hpp"

ColorClipboard::ColorClipboard()
{
m_color = std::make_unique<Color>(Color::WHITE);
}

ColorClipboard&
ColorClipboard::instance()
{
static ColorClipboard instance;
return instance;
}

void
ColorClipboard::set_color(const Color& color)
{
m_color = std::make_unique<Color>(color);
}

const
Color* ColorClipboard::get_color() const
{
return m_color.get();
}
45 changes: 45 additions & 0 deletions src/gui/color_clipboard.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SuperTux
// Copyright (C) 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
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#ifndef HEADER_SUPERTUX_GUI_COLOR_CLIPBOARD_HPP
#define HEADER_SUPERTUX_GUI_COLOR_CLIPBOARD_HPP

#include <memory>

#include "video/color.hpp"

class ColorClipboard
bruhmoent marked this conversation as resolved.
Show resolved Hide resolved
{
public:
ColorClipboard();
~ColorClipboard() = default;

static ColorClipboard& instance();

void set_color(const Color& color);
const Color* get_color() const;

private:
std::unique_ptr<Color> m_color;

private:
ColorClipboard(const ColorClipboard&) = delete;
ColorClipboard& operator=(const ColorClipboard&) = delete;
};

#endif

/* EOF */
25 changes: 24 additions & 1 deletion src/gui/menu_color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#include "gui/menu_color.hpp"
#include "menu_item.hpp"
#include "item_action.hpp"

#include "util/gettext.hpp"

ColorMenu::ColorMenu(Color* color_) :
color(color_)
color(color_),
clipboard(ColorClipboard::instance())
{
add_label(_("Mix the colour"));
add_hl();
Expand All @@ -31,13 +34,33 @@ ColorMenu::ColorMenu(Color* color_) :
add_color_channel_rgba(&(color->alpha), Color::BLACK, -1, true);
add_color_display(color);

add_hl();
add_item(std::make_unique<MenuItem>(_("Copy"), 1));
add_item(std::make_unique<MenuItem>(
bruhmoent marked this conversation as resolved.
Show resolved Hide resolved
_("Paste"),
2,
clipboard.get_color() ? std::make_optional(*clipboard.get_color()) : std::nullopt));

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

void
ColorMenu::menu_action(MenuItem& item)
{
if (item.get_id() == 1)
{
clipboard.set_color(*color);
MenuItem& menu_paste_item = get_item_by_id(2);
if (&menu_paste_item)
bruhmoent marked this conversation as resolved.
Show resolved Hide resolved
menu_paste_item.set_text_color(*color);
}
else if (item.get_id() == 2)
{
const Color* clipboard_color = clipboard.get_color();
if (clipboard_color)
*color = *clipboard_color;
}
}

/* EOF */
2 changes: 2 additions & 0 deletions src/gui/menu_color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define HEADER_SUPERTUX_GUI_MENU_COLOR_HPP

#include "gui/menu.hpp"
#include "color_clipboard.hpp"

class ColorMenu final : public Menu
{
Expand All @@ -28,6 +29,7 @@ class ColorMenu final : public Menu

private:
Color* color;
ColorClipboard& clipboard;

private:
ColorMenu(const ColorMenu&) = delete;
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);
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
Loading