Skip to content

Added Mouse Integration #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 46 additions & 10 deletions src/game_menu_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,46 @@ namespace game_menu {
void Menu::handleEvent(sf::Event &event) {
auto max_items = _items.size();
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Up) {
_currently_selected_item =
(_currently_selected_item + max_items - 1) % max_items;
} else if (event.key.code == sf::Keyboard::Down) {
_currently_selected_item = (_currently_selected_item + 1) % max_items;
if (event.key.code == sf::Keyboard::Up ||
event.key.code == sf::Keyboard::Down) {
changeCurrSelectedItem(event.key.code == sf::Keyboard::Up);
} else if (event.key.code == sf::Keyboard::Return) {
_items[_currently_selected_item].data.action(_window);
performCurrSelectedItemAction();
}
}
}
else if (event.type == sf::Event::MouseMoved) {
sf::Vector2f mousePos(event.mouseMove.x, event.mouseMove.y);
for (int i = 0; i < _items.size(); ++i) {
if (_items[i].textObj.getGlobalBounds().contains(mousePos)) {
_currently_selected_item = i;
break;
}
}
}
else if (event.type == sf::Event::MouseButtonPressed) {
if (event.mouseButton.button == sf::Mouse::Left) {
sf::Vector2f mousePos(event.mouseButton.x, event.mouseButton.y);
for (int i = 0; i < _items.size(); ++i) {
if (_items[i].textObj.getGlobalBounds().contains(mousePos)) {
_items[i].data.action(_window);
break;
}
}
}
}
else if (event.type == sf::Event::MouseWheelScrolled) {
if (event.mouseWheelScroll.wheel == sf::Mouse::VerticalWheel) {
changeCurrSelectedItem(event.mouseWheelScroll.delta > 0.0);
}
}
} // handleEvent(...)

void Menu::render() {
setMenu();
drawMenu();
}
} // render(...)

void Menu::writeText(std::string str, sf::Font *font, unsigned int size,
sf::Text Menu::writeText(std::string str, sf::Font *font, unsigned int size,
float x, float y, const Color color) {
sf::Color textColor(color);
sf::Text text;
Expand All @@ -61,6 +84,7 @@ void Menu::writeText(std::string str, sf::Font *font, unsigned int size,
}
text.setPosition(sf::Vector2f(x, y));
_window.draw(text);
return text;
} // writeText(...)

void Menu::setMenu() {
Expand Down Expand Up @@ -135,10 +159,22 @@ void Menu::drawMenu() {
} else {
color = _style.colorScheme.itemColor;
}
writeText(_items[i].data.name, _style.ItemFont, _style.ItemFontSize,
_items[i].textObj = writeText(_items[i].data.name, _style.ItemFont, _style.ItemFontSize,
_items[i].location.x, _items[i].location.y, color);
}

} // drawMenu()

void Menu::changeCurrSelectedItem(const bool moveUp) {
const auto maxItems = _items.size();
_currently_selected_item = moveUp
? (_currently_selected_item + maxItems - 1) % maxItems
: (_currently_selected_item + 1) % maxItems;
} // changeCurrSelectedItem()

void Menu::performCurrSelectedItemAction()
{
_items[_currently_selected_item].data.action(_window);
} // performCurrSelectedItemAction()

} // namespace game_menu
5 changes: 4 additions & 1 deletion src/game_menu_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct coordinates {
struct Item {
MenuItem data;
coordinates location;
sf::Text textObj;
};

class Menu {
Expand All @@ -39,8 +40,10 @@ class Menu {
private:
void setMenu();
void drawMenu();
void writeText(std::string str, sf::Font* font, unsigned int size, float x,
sf::Text writeText(std::string str, sf::Font* font, unsigned int size, float x,
float y, const Color color);
void changeCurrSelectedItem(const bool moveUp);
void performCurrSelectedItemAction();
sf::RenderTarget &_window;
Style _style;
std::string _title;
Expand Down