-
Notifications
You must be signed in to change notification settings - Fork 10
Added menu sound effects for select and click actions #20
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,144 +1,132 @@ | ||
#include "game_menu_impl.h" | ||
#include "SFML/Graphics/Color.hpp" | ||
#include "SFML/Window/Event.hpp" | ||
|
||
#include "SFML/Audio.hpp" | ||
#include <iostream> | ||
#include <string> | ||
|
||
game_menu::MENU *create_menu_context(sf::RenderWindow &wnd, | ||
game_menu::MenuConfig &config) { | ||
return reinterpret_cast<game_menu::MENU *>(new game_menu::Menu(wnd, config)); | ||
return reinterpret_cast<game_menu::MENU *>(new game_menu::Menu(wnd, config)); | ||
} | ||
|
||
void menu_destroy_context(game_menu::MENU *menu) { | ||
delete reinterpret_cast<game_menu::Menu *>(menu); | ||
delete reinterpret_cast<game_menu::Menu *>(menu); | ||
} | ||
|
||
void menu_handle_event(game_menu::MENU *menu, sf::Event &event) { | ||
reinterpret_cast<game_menu::Menu *>(menu)->handleEvent(event); | ||
reinterpret_cast<game_menu::Menu *>(menu)->handleEvent(event); | ||
} | ||
|
||
void menu_render(game_menu::MENU *menu) { | ||
reinterpret_cast<game_menu::Menu *>(menu)->render(); | ||
reinterpret_cast<game_menu::Menu *>(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 | ||
} // namespace game_menu |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <string> | ||
#include <vector> | ||
#include <iostream> | ||
|
||
namespace game_menu { | ||
|
||
struct coordinates { | ||
float x = 0; | ||
float y = 0; | ||
float x = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lot of formatting changes - please don't do the auto format over the doc - I have the tab width set as 2 instead of 4 which is probably the cause. |
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you miss including the public header in your commit? |
||
_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<Item> _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<Item> _items; | ||
coordinates _title_location; | ||
int _currently_selected_item = 0; | ||
|
||
sf::Sound _click_sound; | ||
sf::Sound _select_sound; | ||
}; | ||
} // namespace game_menu | ||
|
||
#endif // GMENU_IMPL_H | ||
} // namespace game_menu |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put this sample audio next to the examples - Also add a readme with attribution and source of these assets.