Skip to content

Commit 09a39a1

Browse files
committed
Implement the color clipboard
1 parent 126e60e commit 09a39a1

File tree

6 files changed

+132
-11
lines changed

6 files changed

+132
-11
lines changed

src/gui/color_clipboard.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// SuperTux
2+
// Copyright (C) 2024 bruhmoent
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
#include "color_clipboard.hpp"
18+
19+
ColorClipboard::ColorClipboard()
20+
{
21+
m_color = std::make_unique<Color>(Color::WHITE);
22+
}
23+
24+
ColorClipboard&
25+
ColorClipboard::instance()
26+
{
27+
static ColorClipboard instance;
28+
return instance;
29+
}
30+
31+
void
32+
ColorClipboard::set_color(const Color& color)
33+
{
34+
m_color = std::make_unique<Color>(color);
35+
}
36+
37+
const
38+
Color* ColorClipboard::get_color() const
39+
{
40+
return m_color.get();
41+
}

src/gui/color_clipboard.hpp

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// SuperTux
2+
// Copyright (C) 2024 bruhmoent
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
#ifndef HEADER_SUPERTUX_GUI_COLOR_CLIPBOARD_HPP
18+
#define HEADER_SUPERTUX_GUI_COLOR_CLIPBOARD_HPP
19+
20+
#include <memory>
21+
22+
#include "video/color.hpp"
23+
24+
class ColorClipboard
25+
{
26+
public:
27+
ColorClipboard();
28+
~ColorClipboard() = default;
29+
30+
static ColorClipboard& instance();
31+
32+
void set_color(const Color& color);
33+
const Color* get_color() const;
34+
35+
private:
36+
std::unique_ptr<Color> m_color;
37+
38+
private:
39+
ColorClipboard(const ColorClipboard&) = delete;
40+
ColorClipboard& operator=(const ColorClipboard&) = delete;
41+
};
42+
43+
#endif
44+
45+
/* EOF */

src/gui/menu_color.cpp

+24-1
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

1717
#include "gui/menu_color.hpp"
18+
#include "menu_item.hpp"
19+
#include "item_action.hpp"
1820

1921
#include "util/gettext.hpp"
2022

2123
ColorMenu::ColorMenu(Color* color_) :
22-
color(color_)
24+
color(color_),
25+
clipboard(ColorClipboard::instance())
2326
{
2427
add_label(_("Mix the colour"));
2528
add_hl();
@@ -31,13 +34,33 @@ ColorMenu::ColorMenu(Color* color_) :
3134
add_color_channel_rgba(&(color->alpha), Color::BLACK, -1, true);
3235
add_color_display(color);
3336

37+
add_hl();
38+
add_item(std::make_unique<MenuItem>(_("Copy"), 1));
39+
add_item(std::make_unique<MenuItem>(
40+
_("Paste"),
41+
2,
42+
clipboard.get_color() ? std::make_optional(*clipboard.get_color()) : std::nullopt));
43+
3444
add_hl();
3545
add_back(_("OK"));
3646
}
3747

3848
void
3949
ColorMenu::menu_action(MenuItem& item)
4050
{
51+
if (item.get_id() == 1)
52+
{
53+
clipboard.set_color(*color);
54+
MenuItem& menu_paste_item = get_item_by_id(2);
55+
if (&menu_paste_item)
56+
menu_paste_item.set_text_color(*color);
57+
}
58+
else if (item.get_id() == 2)
59+
{
60+
const Color* clipboard_color = clipboard.get_color();
61+
if (clipboard_color)
62+
*color = *clipboard_color;
63+
}
4164
}
4265

4366
/* EOF */

src/gui/menu_color.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define HEADER_SUPERTUX_GUI_MENU_COLOR_HPP
1919

2020
#include "gui/menu.hpp"
21+
#include "color_clipboard.hpp"
2122

2223
class ColorMenu final : public Menu
2324
{
@@ -28,6 +29,7 @@ class ColorMenu final : public Menu
2829

2930
private:
3031
Color* color;
32+
ColorClipboard& clipboard;
3133

3234
private:
3335
ColorMenu(const ColorMenu&) = delete;

src/gui/menu_item.cpp

+16-9
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,17 @@
2323
#include "supertux/resources.hpp"
2424
#include "video/drawing_context.hpp"
2525

26-
//static const float FLICK_CURSOR_TIME = 0.5f;
27-
28-
MenuItem::MenuItem(const std::string& text, int id) :
26+
MenuItem::MenuItem(const std::string& text, int id, const std::optional<Color>& text_color) :
2927
m_id(id),
3028
m_text(text),
3129
m_help(),
32-
m_font(Resources::normal_font)
30+
m_font(Resources::normal_font),
31+
m_text_color(text_color)
3332
{
3433
}
3534

36-
MenuItem::~MenuItem() {
37-
35+
MenuItem::~MenuItem()
36+
{
3837
}
3938

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

6160
Color
62-
MenuItem::get_color() const {
63-
return ColorScheme::Menu::default_color;
61+
MenuItem::get_color() const
62+
{
63+
return m_text_color.value_or(ColorScheme::Menu::default_color);
64+
}
65+
66+
void
67+
MenuItem::set_text_color(const Color& color)
68+
{
69+
m_text_color = color;
6470
}
6571

6672
int
67-
MenuItem::get_width() const {
73+
MenuItem::get_width() const
74+
{
6875
return static_cast<int>(m_font->get_text_width(m_text)) + 16;
6976
}
7077

src/gui/menu_item.hpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919
#define HEADER_SUPERTUX_GUI_MENU_ITEM_HPP
2020

2121
#include "gui/menu.hpp"
22+
#include <optional>
2223

2324
class MenuItem
2425
{
2526
public:
26-
MenuItem(const std::string& text, int id = -1);
27+
MenuItem(const std::string& text, int id = -1, const std::optional<Color>& text_color = std::nullopt);
2728
virtual ~MenuItem();
2829

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

6768
virtual Color get_color() const;
69+
virtual void set_text_color(const Color& color);
6870

6971
/** Returns true when the MenuManager shouldn't do anything else. */
7072
virtual bool no_other_action() const {
@@ -86,6 +88,7 @@ class MenuItem
8688
std::string m_text;
8789
std::string m_help;
8890
FontPtr m_font;
91+
std::optional<Color> m_text_color;
8992

9093
private:
9194
MenuItem(const MenuItem&) = delete;

0 commit comments

Comments
 (0)