Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .clang-tidy
Copy link
Member

Choose a reason for hiding this comment

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

Please restore this file.

This file was deleted.

2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
# make
#

cmake_minimum_required(VERSION 3.1)
cmake_minimum_required(VERSION 3.5)

## Project name to use as command prefix.

Expand Down
2 changes: 1 addition & 1 deletion src/gui/item_colorchannel_rgba.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,4 @@ ItemColorChannelRGBA::get_color() const
return m_channel;
}

/* EOF */
/* EOF */
Copy link
Member

Choose a reason for hiding this comment

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

Add a new line at the end.

2 changes: 1 addition & 1 deletion src/gui/item_colorchannel_rgba.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ class ItemColorChannelRGBA final : public MenuItem

#endif

/* EOF */
/* EOF */
Copy link
Member

Choose a reason for hiding this comment

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

Add a new line at the end.

209 changes: 209 additions & 0 deletions src/gui/item_colorchannel_saturation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
// SuperTux
// Copyright (C) 2015 Hume2 <[email protected]>
//
// 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 "gui/item_colorchannel_saturation.hpp"

#include <sstream>

#include "math/util.hpp"
#include "supertux/resources.hpp"
#include "video/drawing_context.hpp"

namespace {

std::string sat_value_to_string(float v_raw)
{
float percent = v_raw * 100.0f;
// Round to nearest integer percentage
percent = 0.01f * floorf(percent * 100.0f + 0.5f);
std::ostringstream os;
os << v_raw << " (" << percent << " %" << ")";
return os.str();
}

std::string float_to_string(float v)
{
std::ostringstream os;
os << v;
return os.str();
}

} // namespace

ItemColorChannelSaturation::ItemColorChannelSaturation(Color* color, ColorOKLCh* okl, int id) :
MenuItem(sat_value_to_string(okl->C), id),
m_color(color),
m_okl(okl),
m_sat(&okl->C),
m_sat_prev(okl->C),
m_edit_mode(false),
m_flickw(static_cast<int>(Resources::normal_font->get_text_width("_")))
//m_channel(channel)
{
}

void
ItemColorChannelSaturation::draw(DrawingContext& context, const Vector& pos,
int menu_width, bool active)
{
if (!m_edit_mode && *m_sat != m_sat_prev) {
set_text(sat_value_to_string(*m_sat));
m_sat_prev = *m_sat;
}

MenuItem::draw(context, pos, menu_width, active);
*m_okl = ColorOKLCh(*m_color);
float maxC = m_okl->get_max_chroma();
float fraction = 0.0f;
if (maxC > 0.0f) fraction = std::clamp((*m_sat / 1.0f), 0.0f, 1.0f);
const float lw = float(menu_width - 32) * (fraction);
context.color().draw_filled_rect(Rectf(pos + Vector(16, -4),
pos + Vector(16 + lw, 4)),
Color::WHITE, 0.0f, LAYER_GUI-1);
}

int
ItemColorChannelSaturation::get_width() const
{
return static_cast<int>(Resources::normal_font->get_text_width(get_text()) + 16 + static_cast<float>(m_flickw));
}

void
ItemColorChannelSaturation::enable_edit_mode()
{
if (m_edit_mode)
// Do nothing if it is already enabled
return;
m_edit_mode = true;
set_text(float_to_string(*m_sat));
}


void
ItemColorChannelSaturation::event(const SDL_Event& ev)
{
if (ev.type == SDL_TEXTINPUT) {
std::string txt = ev.text.text;
for (auto& c : txt) {
add_char(c);
}
}
}

void
ItemColorChannelSaturation::add_char(char c)
{
enable_edit_mode();
std::string text = get_text();

if (c == '.' || c == ',')
{
const bool has_comma = (text.find('.') != std::string::npos);
if (!has_comma)
{
if (text.empty()) {
text = "0.";
} else {
text.push_back('.');
}
}
}
else if (isdigit(c))
{
text.push_back(c);
}
else
{
return;
}

float number = std::stof(text);
if (0.0f <= number && number <= 1.0f) {
*m_sat = number;
set_text(text);
}
}

void
ItemColorChannelSaturation::remove_char()
{
enable_edit_mode();
std::string text = get_text();

if (text.empty())
{
*m_sat = 0.0f;
}
else
{
text.pop_back();

if (!text.empty()) {
*m_sat = std::stof(text);
} else {
*m_sat = 0.0f;
}
}

set_text(text);
}

void
ItemColorChannelSaturation::process_action(const MenuAction& action)
{
switch (action)
{
case MenuAction::REMOVE:
remove_char();
break;

case MenuAction::LEFT:
case MenuAction::RIGHT: {
float maxC = m_okl->get_max_chroma();
float delta = (action == MenuAction::LEFT ? -0.01f : +0.01f);

float newSat = *m_sat + delta;
newSat = std::clamp(newSat, 0.0f, maxC);
newSat = roundf(newSat * 100.0f) / 100.0f;
// Snap to reduce jitter at boundaries
if(newSat < 0.005f) newSat = 0.0f;
if(newSat > maxC - 0.005f) newSat = maxC;

*m_sat = newSat;

float oldA = m_color->alpha;
*m_color = Color::from_oklch(*m_okl);
m_color->alpha = oldA; // if you want to preserve alpha
m_edit_mode = false;
break;
}

case MenuAction::UNSELECT:
m_edit_mode = false;
break;

default:
break;
}
}

// Color
// ItemColorChannelSaturation::get_color() const
// {
// return m_channel;
// }

/* EOF */
69 changes: 69 additions & 0 deletions src/gui/item_colorchannel_saturation.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// SuperTux
// Copyright (C) 2015 Hume2 <[email protected]>
//
// 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_ITEM_COLORCHANNEL_SATURATION_HPP
#define HEADER_SUPERTUX_GUI_ITEM_COLORCHANNEL_SATURATION_HPP

#include "gui/menu_item.hpp"

#include "util/colorspace_oklab.hpp"
#include "video/color.hpp"


class ItemColorChannelSaturation final : public MenuItem
{
public:
ItemColorChannelSaturation(Color* color, ColorOKLCh* okl, int id = -1);

/** Draws the menu item. */
virtual void draw(DrawingContext&, const Vector& pos, int menu_width, bool active) override;

/** Returns the minimum width of the menu item. */
virtual int get_width() const override;

/** Processes the menu action. */
virtual void process_action(const MenuAction& action) override;

/** Processes the given event. */
virtual void event(const SDL_Event& ev) override;

//virtual Color get_color() const override;

virtual bool changes_width() const override { return true; }

void change_input(const std::string& input_) { set_text(input_); }

private:
void enable_edit_mode();
void add_char(char c);
void remove_char();

private:
Color* m_color;
ColorOKLCh* m_okl;
float* m_sat;
float m_sat_prev;
bool m_edit_mode;
int m_flickw;

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

#endif

/* EOF */
6 changes: 6 additions & 0 deletions src/gui/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "gui/item_back.hpp"
#include "gui/item_color.hpp"
#include "gui/item_colorchannel_rgba.hpp"
#include "gui/item_colorchannel_saturation.hpp"
#include "gui/item_colordisplay.hpp"
#include "gui/item_color_picker_2d.hpp"
#include "gui/item_controlfield.hpp"
Expand Down Expand Up @@ -267,6 +268,11 @@ Menu::add_color_channel_rgba(float* input, Color channel, int id, bool is_linear
return add_item<ItemColorChannelRGBA>(input, channel, id, is_linear);
}

ItemColorChannelSaturation&
Menu::add_color_channel_saturation(Color* color, ColorOKLCh* okl, int id) {
return add_item<ItemColorChannelSaturation>(color, okl, id);
}

ItemColorPicker2D&
Menu::add_color_picker_2d(Color& color) {
return add_item<ItemColorPicker2D>(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 @@ -30,6 +30,7 @@ class ItemAction;
class ItemBack;
class ItemColor;
class ItemColorChannelRGBA;
class ItemColorChannelSaturation;
class ItemColorPicker2D;
class ItemColorDisplay;
class ItemControlField;
Expand Down Expand Up @@ -102,6 +103,7 @@ class Menu
ItemColorDisplay& add_color_display(Color* color, int id = -1);
ItemColorChannelRGBA& add_color_channel_rgba(float* input, Color channel, int id = -1,
bool is_linear = false);
ItemColorChannelSaturation& add_color_channel_saturation(Color* color, ColorOKLCh* okl, int id);
ItemColorPicker2D& add_color_picker_2d(Color& color);
ItemPaths& add_path_settings(const std::string& text, PathObject& target, const std::string& path_ref);
ItemStringArray& add_string_array(const std::string& text, std::vector<std::string>& items, int id = -1);
Expand Down
12 changes: 11 additions & 1 deletion src/gui/menu_color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@
#include "util/gettext.hpp"

ColorMenu::ColorMenu(Color* color) :
m_color(color)
m_color(color),
m_okl(*color)
{
add_label(_("Mix the colour"));
add_hl();

add_color_picker_2d(*m_color);

add_color_channel_saturation(m_color, &m_okl, MNID_SATURATION);

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);
Expand Down Expand Up @@ -117,6 +121,12 @@ ColorMenu::menu_action(MenuItem& item)
log_warning << "Invalid color format: " << text << ". Supported formats: rgb(r,g,b) and #rrggbb" << std::endl;
}
}
else if (item.get_id() == MNID_SATURATION)
{
float oldA = m_color->alpha;
*m_color = Color::from_oklch(m_okl);
m_color->alpha = oldA;
}
}

/* EOF */
4 changes: 3 additions & 1 deletion src/gui/menu_color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ class ColorMenu final : public Menu
{
MNID_COPY_CLIPBOARD_RGB = 1,
MNID_COPY_CLIPBOARD_HEX,
MNID_PASTE_CLIPBOARD
MNID_PASTE_CLIPBOARD,
MNID_SATURATION
};

private:
Color* m_color;
ColorOKLCh m_okl;

private:
ColorMenu(const ColorMenu&) = delete;
Expand Down
Loading
Loading