1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
#include "../include/ContextMenu.hpp"
#include "../include/ColorScheme.hpp"
#include "../include/ResourceCache.hpp"
#include "../include/Settings.hpp"
#include <SFML/Graphics/Text.hpp>
#include <SFML/Graphics/RectangleShape.hpp>
#include <cmath>
#include <cassert>
namespace dchat
{
const float PADDING_SIDE = 10.0f;
const float MENU_ITEM_FONT_SIZE = 18.0f;
const std::string MENU_FONT_PATH = "fonts/Roboto-Regular.ttf";
ContextMenuItem::ContextMenuItem(const std::string & _text, MenuItemClickCallbackFunc _clickCallbackFunc) :
text(sf::String::fromUtf8(_text.begin(), _text.end()), *ResourceCache::getFont(MENU_FONT_PATH), MENU_ITEM_FONT_SIZE * Settings::getScaling()),
clickCallbackFunc(_clickCallbackFunc)
{
text.setFillColor(ColorScheme::getContextMenuTextColor());
}
ContextMenu::ContextMenu() :
focusedItem(-1),
pressedItem(-1),
visible(true)
{
}
void ContextMenu::addItem(const ContextMenuItem &item)
{
menuItems.push_back(item);
}
void ContextMenu::setPosition(const sf::Vector2f &position)
{
this->position = position;
}
void ContextMenu::setVisible(bool visible)
{
this->visible = visible;
}
ContextMenuItem& ContextMenu::getItemByIndex(usize index)
{
assert(index < menuItems.size());
return menuItems[index];
}
void ContextMenu::processEvent(const sf::Event &event)
{
if(!visible || menuItems.empty()) return;
if(event.type == sf::Event::MouseButtonPressed)
{
if(event.mouseButton.button == sf::Mouse::Button::Left)
{
pressedItem = focusedItem;
if(pressedItem == -1)
setVisible(false);
}
else
{
focusedItem = -1;
pressedItem = -1;
setVisible(false);
}
}
else if(event.type == sf::Event::MouseButtonReleased)
{
if(pressedItem != -1 && pressedItem == focusedItem)
{
assert(pressedItem < menuItems.size());
if(menuItems[pressedItem].clickCallbackFunc)
menuItems[pressedItem].clickCallbackFunc(&menuItems[pressedItem]);
setVisible(false);
}
pressedItem = -1;
}
}
void ContextMenu::draw(sf::RenderWindow &window)
{
if(!visible || menuItems.empty()) return;
const sf::Font *menuItemFont = ResourceCache::getFont(MENU_FONT_PATH);
float boxHeight = menuItemFont->getLineSpacing(MENU_ITEM_FONT_SIZE) * 1.5f;
sf::Vector2f menuSize;
for(ContextMenuItem &menuItem : menuItems)
{
menuItem.text.setCharacterSize(18 * Settings::getScaling());
menuSize.x = std::max(menuSize.x, menuItem.text.getLocalBounds().width);
menuSize.y += boxHeight;
}
menuSize.x += PADDING_SIDE * 2.0f;
menuSize.x = floor(menuSize.x);
menuSize.y = floor(menuSize.y);
sf::Vector2f pos(floor(position.x), floor(position.y));
sf::RectangleShape background(menuSize);
background.setPosition(pos);
background.setFillColor(ColorScheme::getContextMenuBackgroundColor());
window.draw(background);
auto mousePos = sf::Mouse::getPosition(window);
focusedItem = -1;
for(int i = 0; i < menuItems.size(); ++i)
{
ContextMenuItem &menuItem = menuItems[i];
if(mousePos.x >= pos.x && mousePos.x <= pos.x + menuSize.x && mousePos.y >= pos.y && mousePos.y <= pos.y + boxHeight && (pressedItem == -1 || i == pressedItem))
{
sf::RectangleShape selectedItemRect(sf::Vector2f(menuSize.x, floor(boxHeight)));
selectedItemRect.setFillColor(ColorScheme::getContextMenuSelectedItemBackgroundColor());
selectedItemRect.setPosition(floor(pos.x), floor(pos.y));
window.draw(selectedItemRect);
focusedItem = i;
}
menuItem.text.setPosition(floor(pos.x + PADDING_SIDE), floor(pos.y + boxHeight * 0.5f - menuItemFont->getLineSpacing(MENU_ITEM_FONT_SIZE) * 0.5f));
window.draw(menuItem.text);
pos.y += boxHeight;
}
}
}
|