diff --git a/click.wav b/click.wav new file mode 100644 index 0000000..d883789 Binary files /dev/null and b/click.wav differ diff --git a/select.wav b/select.wav new file mode 100644 index 0000000..6ff6a31 Binary files /dev/null and b/select.wav differ diff --git a/src/game_menu_impl.cpp b/src/game_menu_impl.cpp index 3b1617a..10dc3b2 100644 --- a/src/game_menu_impl.cpp +++ b/src/game_menu_impl.cpp @@ -1,144 +1,132 @@ #include "game_menu_impl.h" #include "SFML/Graphics/Color.hpp" #include "SFML/Window/Event.hpp" - +#include "SFML/Audio.hpp" #include #include game_menu::MENU *create_menu_context(sf::RenderWindow &wnd, game_menu::MenuConfig &config) { - return reinterpret_cast(new game_menu::Menu(wnd, config)); + return reinterpret_cast(new game_menu::Menu(wnd, config)); } void menu_destroy_context(game_menu::MENU *menu) { - delete reinterpret_cast(menu); + delete reinterpret_cast(menu); } void menu_handle_event(game_menu::MENU *menu, sf::Event &event) { - reinterpret_cast(menu)->handleEvent(event); + reinterpret_cast(menu)->handleEvent(event); } void menu_render(game_menu::MENU *menu) { - reinterpret_cast(menu)->render(); + reinterpret_cast(menu)->render(); } 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; - } else if (event.key.code == sf::Keyboard::Return) { - _items[_currently_selected_item].data.action(_window); + 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; + _click_sound.play(); + } else if (event.key.code == sf::Keyboard::Down) { + _currently_selected_item = (_currently_selected_item + 1) % max_items; + _click_sound.play(); + } else if (event.key.code == sf::Keyboard::Return) { + _items[_currently_selected_item].data.action(_window); + _select_sound.play(); + } } - } } void Menu::render() { - setMenu(); - drawMenu(); + setMenu(); + drawMenu(); } void 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; - text.setString(str); - text.setFont(*font); - text.setFillColor(textColor); - text.setCharacterSize(size); - sf::FloatRect textRect = text.getLocalBounds(); - text.setOrigin(textRect.width / 2.0f, 0); - if (x - textRect.width / 2.0f < 0) { - x = textRect.width / 2 + _style.PaddingTitle.left; - } - if (x + textRect.width / 2.0f > _window.getSize().x) { - x = _window.getSize().x - textRect.width / 2 + _style.PaddingTitle.left; - } - text.setPosition(sf::Vector2f(x, y)); - _window.draw(text); -} // writeText(...) + sf::Color textColor(color); + sf::Text text; + text.setString(str); + text.setFont(*font); + text.setFillColor(textColor); + text.setCharacterSize(size); + sf::FloatRect textRect = text.getLocalBounds(); + text.setOrigin(textRect.width / 2.0f, 0); + if (x - textRect.width / 2.0f < 0) { + x = textRect.width / 2 + _style.PaddingTitle.left; + } + if (x + textRect.width / 2.0f > _window.getSize().x) { + x = _window.getSize().x - textRect.width / 2 + _style.PaddingTitle.left; + } + text.setPosition(sf::Vector2f(x, y)); + _window.draw(text); +} void Menu::setMenu() { - - // std::cout << "screen size:" << window.getSize().x << " " << - // window.getSize().y - // << std::endl; - - /* Setting title of menu */ - { - /* Small scope just to be able to freely use the variable names */ float offset_coefficient = 0.5; - switch (_style.TitleAlign) { case Align::Center: - offset_coefficient = 0.5; - break; + offset_coefficient = 0.5; + break; case Align::Left: - offset_coefficient = 0.25; - break; + offset_coefficient = 0.25; + break; case Align::Right: - offset_coefficient = 0.75; - break; + offset_coefficient = 0.75; + break; } float x = (float)_window.getSize().x * offset_coefficient , y = _style.PaddingTitle.top; _title_location.x = (x + _style.PaddingTitle.left); _title_location.y = y; - } - - float menu_screen_height = - _title_location.y + _style.PaddingItems.top; - float block_height = - (float)_style.ItemFontSize * _style.MenuItemScaleFactor; - - float offset_coefficient = 0.5; - - switch (_style.ItemAlign) { - case Align::Center: - offset_coefficient = 0.5; - break; - case Align::Left: - offset_coefficient = 0.25; - break; - case Align::Right: - offset_coefficient = 0.75; - break; - } - - float x = (float)_window.getSize().x * offset_coefficient + - _style.PaddingItems.left; - float y = menu_screen_height + - block_height + _style.PaddingItems.top; - /* Calculating Menu item locations */ - for (int8_t i = 0; i < _items.size(); ++i) { - coordinates crd; - crd.x = x; - crd.y = y; - _items[i].location = crd; - y += block_height; - } - -} // setMenu() -void Menu::drawMenu() { - writeText(_title, _style.ItemFont, _style.TitleFontSize, _title_location.x, - _title_location.y, _style.colorScheme.titleColor); - game_menu::Color color(_style.colorScheme.itemColor); - for (int i = 0; i < _items.size(); ++i) { - if (i == _currently_selected_item) { - color = _style.colorScheme.selectedColor; - } else { - color = _style.colorScheme.itemColor; + float menu_screen_height = _title_location.y + _style.PaddingItems.top; + float block_height = (float)_style.ItemFontSize * _style.MenuItemScaleFactor; + + float offset_coefficient_items = 0.5; + switch (_style.ItemAlign) { + case Align::Center: + offset_coefficient_items = 0.5; + break; + case Align::Left: + offset_coefficient_items = 0.25; + break; + case Align::Right: + offset_coefficient_items = 0.75; + break; } - writeText(_items[i].data.name, _style.ItemFont, _style.ItemFontSize, - _items[i].location.x, _items[i].location.y, color); - } -} // drawMenu() + float x_items = (float)_window.getSize().x * offset_coefficient_items + + _style.PaddingItems.left; + float y_items = menu_screen_height + + block_height + _style.PaddingItems.top; + + for (int8_t i = 0; i < _items.size(); ++i) { + coordinates crd; + crd.x = x_items; + crd.y = y_items; + _items[i].location = crd; + y_items += block_height; + } +} + +void Menu::drawMenu() { + writeText(_title, _style.ItemFont, _style.TitleFontSize, _title_location.x, + _title_location.y, _style.colorScheme.titleColor); + game_menu::Color color(_style.colorScheme.itemColor); + for (int i = 0; i < _items.size(); ++i) { + if (i == _currently_selected_item) { + color = _style.colorScheme.selectedColor; + } else { + color = _style.colorScheme.itemColor; + } + writeText(_items[i].data.name, _style.ItemFont, _style.ItemFontSize, + _items[i].location.x, _items[i].location.y, color); + } +} -} // namespace game_menu \ No newline at end of file +} // namespace game_menu diff --git a/src/game_menu_impl.h b/src/game_menu_impl.h index 83278db..e179727 100644 --- a/src/game_menu_impl.h +++ b/src/game_menu_impl.h @@ -4,50 +4,56 @@ #include "SFML/Graphics/RenderTarget.hpp" #include "SFML/Graphics/RenderWindow.hpp" #include "game_menu/game_menu.h" - #include "SFML/Graphics.hpp" - +#include "SFML/Audio.hpp" #include #include #include namespace game_menu { + struct coordinates { - float x = 0; - float y = 0; + float x = 0; + float y = 0; }; struct Item { - MenuItem data; - coordinates location; + MenuItem data; + coordinates location; }; class Menu { public: - Menu(sf::RenderTarget &window, MenuConfig config) - : _window(window), _style(config.style), _title(config.title) { - for (auto &menu_item : config.items) { - _items.push_back({menu_item, {0, 0}}); - std::cout << " Processing item " << menu_item.name << std::endl; + Menu(sf::RenderTarget &window, MenuConfig config) + : _window(window), _style(config.style), _title(config.title) { + _click_sound.setBuffer(config.click_sound_effect); + _select_sound.setBuffer(config.select_sound_effect); + + for (auto &menu_item : config.items) { + _items.push_back({menu_item, {0, 0}}); + std::cout << " Processing item " << menu_item.name << std::endl; + } + std::cout << "Size of items : " << _items.size() << std::endl; } - std::cout << "Size of items : " << _items.size() << std::endl; - } - void handleEvent(sf::Event &event); - void render(); + void handleEvent(sf::Event &event); + void render(); private: - void setMenu(); - void drawMenu(); - void writeText(std::string str, sf::Font* font, unsigned int size, float x, - float y, const Color color); - sf::RenderTarget &_window; - Style _style; - std::string _title; - std::vector _items; - coordinates _title_location; - int _currently_selected_item = 0; + void setMenu(); + void drawMenu(); + void writeText(std::string str, sf::Font *font, unsigned int size, float x, + float y, const Color color); + + sf::RenderTarget &_window; + Style _style; + std::string _title; + std::vector _items; + coordinates _title_location; + int _currently_selected_item = 0; + + sf::Sound _click_sound; + sf::Sound _select_sound; }; -} // namespace game_menu -#endif // GMENU_IMPL_H \ No newline at end of file +} // namespace game_menu \ No newline at end of file