Loading...
Searching...
No Matches
ContextMenu.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_CONTEXTMENU_H
5#define MALENA_CONTEXTMENU_H
6
7#pragma once
8
12#include <functional>
13#include <string>
14#include <vector>
15
16namespace ml
17{
34 class MALENA_API ContextMenu : public Graphic<sf::RectangleShape>
35 {
36 public:
38
39 void addItem(const std::string& label, std::function<void()> action);
40 void clearItems();
41
42 void showAt(const sf::Vector2f& pos);
43 void hide();
44 [[nodiscard]] bool isOpen() const { return _open; }
45
47 void onClose(std::function<void()> cb);
48
49 void setPosition(const sf::Vector2f& pos) override;
50 [[nodiscard]] sf::Vector2f getPosition() const override;
51 [[nodiscard]] sf::FloatRect getGlobalBounds() const override;
52
53 void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
54
55 private:
56 struct Item { std::string label; std::function<void()> action; };
57
58 [[nodiscard]] sf::FloatRect itemRect(std::size_t i) const;
59 void recomputeSize();
60
61 std::vector<Item> _items;
62 bool _open = false;
63 bool _prevMouseDown = false;
64 sf::Vector2f _pos { 0.f, 0.f };
65 float _itemH = 26.f;
66 float _pad = 4.f;
67 float _width = 140.f;
68 unsigned int _charSize = 14;
69 mutable int _hover = -1;
70 std::function<void()> _onClose;
71 };
72
73} // namespace ml
74
75#endif // MALENA_CONTEXTMENU_H
void addItem(const std::string &label, std::function< void()> action)
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
sf::FloatRect getGlobalBounds() const override
Return the axis-aligned bounding box in world space.
void onClose(std::function< void()> cb)
void showAt(const sf::Vector2f &pos)
void setPosition(const sf::Vector2f &pos) override
Set the world-space position immediately (no animation).
sf::Vector2f getPosition() const override
Return the current world-space position.
bool isOpen() const
Definition ContextMenu.h:44
#define MALENA_API
Definition Component.h:22
Rect< float > FloatRect
Vector2< float > Vector2f