-
-
Notifications
You must be signed in to change notification settings - Fork 492
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
bruhmoent
wants to merge
13
commits into
SuperTux:master
Choose a base branch
from
bruhmoent:colorclipboard
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Color Clipboard #3100
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
09a39a1
Implement the color clipboard
bruhmoent 2cbed93
Fix mistakes
bruhmoent 929378c
Simplify
bruhmoent 7f93aad
Implement pasting color values from the SDL's clipboard
bruhmoent dfb9cb4
Fix C-style casting + code style
bruhmoent aa65232
Proper type conversion
bruhmoent 86455b4
Only operate on `SDL clipboard`
bruhmoent 22bd2ae
Remove unnecessary `s_clipboard_color` variable + move function imple…
bruhmoent 0ce785c
Fix includes
bruhmoent 2adda50
General improvements
bruhmoent bdb99e1
Code style
bruhmoent e2cbb86
Prevent `const_cast`
Vankata453 4e4b439
Merge conditions
bruhmoent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -14,30 +15,108 @@ | |
// 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 <sstream> | ||
|
||
#include "gui/menu_color.hpp" | ||
#include "gui/menu_item.hpp" | ||
#include "util/log.hpp" | ||
|
||
#include "util/gettext.hpp" | ||
|
||
ColorMenu::ColorMenu(Color* color_) : | ||
color(color_) | ||
ColorMenu::ColorMenu(Color* color) : | ||
m_color(color) | ||
{ | ||
add_label(_("Mix the colour")); | ||
add_hl(); | ||
|
||
add_color_picker_2d(*color); | ||
add_color_channel_rgba(&(color->red), Color::RED); | ||
add_color_channel_rgba(&(color->green), Color::GREEN); | ||
add_color_channel_rgba(&(color->blue), Color::BLUE); | ||
add_color_channel_rgba(&(color->alpha), Color::BLACK, -1, true); | ||
add_color_display(color); | ||
add_color_picker_2d(*m_color); | ||
add_color_channel_rgba(&(m_color->red), Color::RED); | ||
add_color_channel_rgba(&(m_color->green), Color::GREEN); | ||
add_color_channel_rgba(&(m_color->blue), Color::BLUE); | ||
add_color_channel_rgba(&(m_color->alpha), Color::BLACK, -1, true); | ||
add_color_display(m_color); | ||
|
||
add_hl(); | ||
add_entry(MNID_COPY_CLIPBOARD_RGB, _("Copy to clipboard (rgb)")); | ||
add_entry(MNID_COPY_CLIPBOARD_HEX, _("Copy to clipboard (hex)")); | ||
if (SDL_HasClipboardText()) | ||
{ | ||
char* clipboard_text = SDL_GetClipboardText(); | ||
std::optional<Color> clipboard_color; | ||
|
||
if (clipboard_text) | ||
{ | ||
const std::string text(clipboard_text); | ||
SDL_free(clipboard_text); | ||
|
||
clipboard_color = Color::deserialize_from_rgb(text); | ||
if (!clipboard_color) | ||
clipboard_color = Color::deserialize_from_hex(text); | ||
} | ||
|
||
add_entry(MNID_PASTE_CLIPBOARD, _("Paste from clipboard"), clipboard_color.value_or(Color(1.f, 1.f, 1.f))); | ||
} | ||
else | ||
add_entry(MNID_PASTE_CLIPBOARD, _("Paste from clipboard"), Color(1.f, 1.f, 1.f)); | ||
tobbi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
add_hl(); | ||
add_back(_("OK")); | ||
} | ||
|
||
void | ||
ColorMenu::copy_to_clipboard(const std::string& color_str) | ||
{ | ||
if (SDL_SetClipboardText(color_str.c_str()) != 0) | ||
log_warning << "Failed to set SDL clipboard text: " << SDL_GetError() << std::endl; | ||
|
||
MenuItem& menu_paste_item = get_item_by_id(MNID_PASTE_CLIPBOARD); | ||
menu_paste_item.set_text_color(*m_color); | ||
} | ||
|
||
void | ||
ColorMenu::menu_action(MenuItem& item) | ||
{ | ||
if (item.get_id() == MNID_COPY_CLIPBOARD_RGB) | ||
{ | ||
if (m_color) | ||
{ | ||
const std::string clipboard_text(Color::serialize_to_rgb(*m_color)); | ||
copy_to_clipboard(clipboard_text); | ||
} | ||
} | ||
else if (item.get_id() == MNID_COPY_CLIPBOARD_HEX) | ||
{ | ||
if (m_color) | ||
{ | ||
const std::string clipboard_text(Color::serialize_to_hex(*m_color)); | ||
copy_to_clipboard(clipboard_text); | ||
} | ||
} | ||
else if (item.get_id() == MNID_PASTE_CLIPBOARD) | ||
{ | ||
if (SDL_HasClipboardText()) | ||
{ | ||
char* clipboard_text = SDL_GetClipboardText(); | ||
if (!clipboard_text) | ||
return; | ||
|
||
const std::string text(clipboard_text); | ||
SDL_free(clipboard_text); | ||
|
||
std::optional<Color> clipboard_color = Color::deserialize_from_rgb(text); | ||
if (!clipboard_color) | ||
clipboard_color = Color::deserialize_from_hex(text); | ||
|
||
if (clipboard_color) | ||
{ | ||
*m_color = *clipboard_color; | ||
MenuItem& menu_paste_item = get_item_by_id(MNID_PASTE_CLIPBOARD); | ||
menu_paste_item.set_text_color(*clipboard_color); | ||
} | ||
else | ||
log_warning << "Invalid color format: " << text << ". Supported formats: rgb(r,g,b) and #rrggbb" << std::endl; | ||
} | ||
} | ||
} | ||
|
||
/* EOF */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?