forked from Aloshi/EmulationStation
-
Notifications
You must be signed in to change notification settings - Fork 351
Expand file tree
/
Copy pathMenuComponent.cpp
More file actions
133 lines (107 loc) · 3.91 KB
/
MenuComponent.cpp
File metadata and controls
133 lines (107 loc) · 3.91 KB
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
129
130
131
132
133
#include "components/MenuComponent.h"
#include "components/ButtonComponent.h"
#define BUTTON_GRID_VERT_PADDING 32
#define BUTTON_GRID_HORIZ_PADDING 10
#define TITLE_HEIGHT (mTitle->getFont()->getLetterHeight() + TITLE_VERT_PADDING)
MenuComponent::MenuComponent(Window* window, const char* title, const std::shared_ptr<Font>& titleFont, ListLoopType loop_type) : GuiComponent(window),
mBackground(window), mGrid(window, Vector2i(1, 3))
{
addChild(&mBackground);
addChild(&mGrid);
mBackground.setImagePath(":/frame.png");
// set up title
mTitle = std::make_shared<TextComponent>(mWindow);
mTitle->setHorizontalAlignment(ALIGN_CENTER);
mTitle->setColor(0x555555FF);
setTitle(title, titleFont);
mGrid.setEntry(mTitle, Vector2i(0, 0), false);
// set up list which will never change (externally, anyway)
mList = std::make_shared<ComponentList>(mWindow, loop_type);
mGrid.setEntry(mList, Vector2i(0, 1), true);
updateGrid();
updateSize();
mGrid.resetCursor();
}
void MenuComponent::setTitle(const char* title, const std::shared_ptr<Font>& font)
{
mTitle->setText(Utils::String::toUpper(title));
mTitle->setFont(font);
}
float MenuComponent::getButtonGridHeight() const
{
return (mButtonGrid ? mButtonGrid->getSize().y() : Font::get(FONT_SIZE_MEDIUM)->getHeight() + BUTTON_GRID_VERT_PADDING);
}
void MenuComponent::updateSize()
{
const float maxHeight = Renderer::getScreenHeight() * 0.75f;
float height = TITLE_HEIGHT + mList->getTotalRowHeight() + getButtonGridHeight() + 2;
if(height > maxHeight)
{
height = TITLE_HEIGHT + getButtonGridHeight();
int i = 0;
while(i < mList->size())
{
float rowHeight = mList->getRowHeight(i);
if(height + rowHeight < maxHeight)
height += rowHeight;
else
break;
i++;
}
}
float width = (float)Math::min((int)Renderer::getScreenHeight(), (int)(Renderer::getScreenWidth() * 0.90f));
setSize(width, height);
}
void MenuComponent::onSizeChanged()
{
mBackground.fitTo(mSize, Vector3f::Zero(), Vector2f(-32, -32));
// update grid row/col sizes
mGrid.setRowHeightPerc(0, TITLE_HEIGHT / mSize.y());
mGrid.setRowHeightPerc(2, getButtonGridHeight() / mSize.y());
mGrid.setSize(mSize);
}
void MenuComponent::addButton(const std::string& name, const std::string& helpText, const std::function<void()>& callback)
{
mButtons.push_back(std::make_shared<ButtonComponent>(mWindow, Utils::String::toUpper(name), helpText, callback));
updateGrid();
updateSize();
}
void MenuComponent::updateGrid()
{
if(mButtonGrid)
mGrid.removeEntry(mButtonGrid);
mButtonGrid.reset();
if(mButtons.size())
{
mButtonGrid = makeButtonGrid(mWindow, mButtons);
mGrid.setEntry(mButtonGrid, Vector2i(0, 2), true, false);
}
}
std::vector<HelpPrompt> MenuComponent::getHelpPrompts()
{
return mGrid.getHelpPrompts();
}
std::shared_ptr<ComponentGrid> makeButtonGrid(Window* window, const std::vector< std::shared_ptr<ButtonComponent> >& buttons)
{
std::shared_ptr<ComponentGrid> buttonGrid = std::make_shared<ComponentGrid>(window, Vector2i((int)buttons.size(), 2));
float buttonGridWidth = (float)BUTTON_GRID_HORIZ_PADDING * buttons.size(); // initialize to padding
for(int i = 0; i < (int)buttons.size(); i++)
{
buttonGrid->setEntry(buttons.at(i), Vector2i(i, 0), true, false);
buttonGridWidth += buttons.at(i)->getSize().x();
}
for(unsigned int i = 0; i < buttons.size(); i++)
{
buttonGrid->setColWidthPerc(i, (buttons.at(i)->getSize().x() + BUTTON_GRID_HORIZ_PADDING) / buttonGridWidth);
}
buttonGrid->setSize(buttonGridWidth, buttons.at(0)->getSize().y() + BUTTON_GRID_VERT_PADDING + 2);
buttonGrid->setRowHeightPerc(1, 2 / buttonGrid->getSize().y()); // spacer row to deal with dropshadow to make buttons look centered
return buttonGrid;
}
std::shared_ptr<ImageComponent> makeArrow(Window* window)
{
auto bracket = std::make_shared<ImageComponent>(window);
bracket->setImage(":/arrow.svg");
bracket->setResize(0, Math::round(Font::get(FONT_SIZE_MEDIUM)->getLetterHeight()));
return bracket;
}