Loading...
Searching...
No Matches
ColorPicker.h
Go to the documentation of this file.
1// Copyright (c) 2025 Dave R. Smith.
2// Malena Framework — Licensed under PolyForm Noncommercial 1.0.0; commercial use requires a paid license. See LICENSE.
3
4#ifndef MALENA_COLORPICKER_H
5#define MALENA_COLORPICKER_H
6
7#pragma once
8
12#include <functional>
13#include <vector>
14
15namespace ml
16{
33 class MALENA_API ColorPicker : public Graphic<sf::RectangleShape>
34 {
35 public:
37
39 void setPalette(const std::vector<sf::Color>& colors);
40 [[nodiscard]] const std::vector<sf::Color>& palette() const { return _palette; }
41
44 void addColor(const sf::Color& c);
46 void removeColor(std::size_t index);
47
49 void setMaxColors(int max);
50
52 void onColorRemoved(std::function<void(const sf::Color&)> cb);
53
56 void onSwatchRightClick(std::function<void(std::size_t, const sf::Vector2f&)> cb);
57
58 void setColumns(int columns);
59 void setSwatchSize(float px);
60 void setSwatchGap(float px);
61
63 void onColorSelected(std::function<void(const sf::Color&)> cb);
64
65 // ── Positionable / size (auto-sized to the grid) ──────────────────────
66 void setPosition(const sf::Vector2f& pos) override;
67 [[nodiscard]] sf::Vector2f getPosition() const override;
68 [[nodiscard]] sf::FloatRect getGlobalBounds() const override;
69
70 void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
71
72 private:
73 sf::FloatRect cellRect(std::size_t index) const;
74 void recomputeSize();
75
76 std::vector<sf::Color> _palette;
77 int _maxColors = 0; // 0 = unlimited
78 std::function<void(const sf::Color&)> _onColorSelected;
79 std::function<void(const sf::Color&)> _onColorRemoved;
80 std::function<void(std::size_t, const sf::Vector2f&)> _onSwatchRightClick;
81
82 sf::Vector2f _pos { 0.f, 0.f };
83 float _swatch = 22.f;
84 float _gap = 4.f;
85 float _pad = 6.f;
86 int _cols = 8;
87
88 bool _prevMouseDown = false;
89 bool _prevRightDown = false;
90 mutable int _hoverIndex = -1;
91 };
92
93} // namespace ml
94
95#endif // MALENA_COLORPICKER_H
void setSwatchGap(float px)
sf::FloatRect getGlobalBounds() const override
Return the axis-aligned bounding box in world space.
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
void removeColor(std::size_t index)
void onColorRemoved(std::function< void(const sf::Color &)> cb)
void setColumns(int columns)
void addColor(const sf::Color &c)
void setSwatchSize(float px)
sf::Vector2f getPosition() const override
Return the current world-space position.
void onColorSelected(std::function< void(const sf::Color &)> cb)
void setMaxColors(int max)
const std::vector< sf::Color > & palette() const
Definition ColorPicker.h:40
void setPosition(const sf::Vector2f &pos) override
Set the world-space position immediately (no animation).
void onSwatchRightClick(std::function< void(std::size_t, const sf::Vector2f &)> cb)
void setPalette(const std::vector< sf::Color > &colors)
#define MALENA_API
Definition Component.h:22
Rect< float > FloatRect
Vector2< float > Vector2f