Last active
December 13, 2015 18:09
-
-
Save Groogy/4953503 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Book/Menus/Container.hpp> | |
GUI::Container::Container() | |
: mChildren() | |
, mSelectedChild(-1) | |
{ | |
} | |
void GUI::Container::pack(std::shared_ptr<GUI::Componenet> component) | |
{ | |
mChildren.push_back(component); | |
if (!hasSelection() && component->isSelectable()) | |
select(mChildren.size() - 1); | |
} | |
bool GUI::Container::isSelectable() const | |
{ | |
return false; | |
} | |
void GUI::Container::handleEvent(const sf::Event& event) | |
{ | |
if (hasSelection() && mChildren[mSelectedChild]->isActive()) | |
{ | |
mChildren[mSelectedChild]->handleEvent(event); | |
} | |
else | |
{ | |
if (event.type == sf::Event::KeyReleased) | |
{ | |
if (event.key.code == sf::Keyboard::W || event.key.code == sf::Keyboard::Up) | |
{ | |
selectPrevious(); | |
} | |
else if (event.key.code == sf::Keyboard::S || event.key.code == sf::Keyboard::Down) | |
{ | |
selectNext(); | |
} | |
else if (event.key.code == sf::Keyboard::Enter || event.key.code == sf::Keyboard::Space) | |
{ | |
if(hasSelection()) | |
mChildren[mSelectedChild]->activate(); | |
} | |
} | |
} | |
} | |
void GUI::Container::draw(sf::RenderTarget& target, sf::RenderStates states) const | |
{ | |
states.transform.combine(getTransform()); | |
std::for_each(mChildren.begin(), mChildren.end(), [&target, &states](const std::unique_ptr<GUI::Component>& child) { | |
target.draw(*child, states); | |
}); | |
} | |
bool GUI::Container::hasSelection() const | |
{ | |
return mSelectedChild >= 0; | |
} | |
void GUI::Container::select(unsigned int index) | |
{ | |
if (mChildren[index]->isSelectable()) | |
{ | |
if (hasSelection()) | |
mChildren[mSelectedChild]->deselect(); | |
mChildren[index]->select(); | |
mSelectedChild = index; | |
} | |
} | |
void GUI::Container::selectNext() | |
{ | |
int selectedChild = mSelectedChild; | |
while (hasSelection()) | |
{ | |
selectedChild = selectedChild + 1 >= mChildren.size() ? 0 : selectedChild + 1; | |
if (mChildren[selectedChild]->isSelectable()) | |
break; | |
} | |
if (hasSelection()) | |
select(selectedChild); | |
} | |
void GUI::Container::selectPrevious() | |
{ | |
int selectedChild = mSelectedChild; | |
while (hasSelection()) | |
{ | |
selectedChild = selectedChild - 1 < 0 ? mChildren.size() : selectedChild - 1; | |
if (mChildren[selectedChild]->isSelectable()) | |
break; | |
} | |
if (hasSelection()) | |
select(selectedChild); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment