forked from NaiJi/project-kyoku
Implement main menubar for the Editor
This commit is contained in:
parent
7e97510f31
commit
7e2f038a8b
|
@ -1,36 +1,27 @@
|
|||
#include "editor.h"
|
||||
#include "widgets/button.h"
|
||||
#include "widgets/group.h"
|
||||
#include "widgets/menubar.h"
|
||||
#include "tools/bpmcalculator.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
Editor::Editor(sf::RenderWindow& game_window, Callbacks&& callbacks, std::unique_ptr<Music>&& music) :
|
||||
_buttons(std::make_shared<Group>()),
|
||||
_menu_bar(std::make_shared<MenuBar>()),
|
||||
_game_window(game_window),
|
||||
_music(std::move(music)),
|
||||
_bpm_calculator(std::make_unique<BPMCalculator>(_music))
|
||||
{
|
||||
(void)callbacks;
|
||||
const float window_width = game_window.getSize().x;
|
||||
const float window_height = game_window.getSize().y;
|
||||
//const float window_height = game_window.getSize().y;
|
||||
|
||||
_music->openFromFile("Uta-test.flac");
|
||||
_music->setVolume(20);
|
||||
|
||||
std::shared_ptr<Button> button_start = std::make_shared<Button>("Start");
|
||||
button_start->setRect(sf::FloatRect(window_width / 3., window_height / 7. * 2, window_width / 3., window_height / 7.));
|
||||
button_start->setCallback([&]()
|
||||
{
|
||||
_bpm_calculator->startListening(0);
|
||||
});
|
||||
|
||||
_buttons->addChild(button_start);
|
||||
_menu_bar->setRect(sf::FloatRect(0, 0, window_width, 27));
|
||||
}
|
||||
|
||||
void Editor::input(const sf::Event& event)
|
||||
{
|
||||
_buttons->input(event);
|
||||
_menu_bar->input(event);
|
||||
|
||||
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Space)
|
||||
{
|
||||
|
@ -41,21 +32,21 @@ void Editor::input(const sf::Event& event)
|
|||
|
||||
void Editor::update()
|
||||
{
|
||||
_buttons->update();
|
||||
_menu_bar->update();
|
||||
}
|
||||
|
||||
void Editor::draw() const
|
||||
{
|
||||
_game_window.draw(*_buttons);
|
||||
_game_window.draw(*_menu_bar);
|
||||
}
|
||||
|
||||
void Editor::enter()
|
||||
{
|
||||
_buttons->setVisibility();
|
||||
_menu_bar->setVisibility();
|
||||
}
|
||||
|
||||
void Editor::leave()
|
||||
{
|
||||
_buttons->setVisibility(false);
|
||||
_menu_bar->setVisibility(false);
|
||||
}
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
#include <SFML/Graphics/RenderWindow.hpp>
|
||||
|
||||
class Group;
|
||||
class MenuBar;
|
||||
class BPMCalculator;
|
||||
|
||||
class Editor : public GUIState
|
||||
|
@ -26,7 +26,7 @@ public:
|
|||
virtual void leave() override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<Group> _buttons;
|
||||
std::shared_ptr<MenuBar> _menu_bar;
|
||||
sf::RenderWindow& _game_window;
|
||||
|
||||
std::shared_ptr<Music> _music;
|
||||
|
|
|
@ -0,0 +1,66 @@
|
|||
#include "bpmcalculator.h"
|
||||
|
||||
BPMCalculatorWindow::BPMCalculatorWindow() :
|
||||
_pressed(false)
|
||||
{
|
||||
_button_text.setFillColor(sf::Color::Black);
|
||||
_button_content.setFillColor(sf::Color::White);
|
||||
}
|
||||
|
||||
void BPMCalculatorWindow::input(const sf::Event& event)
|
||||
{
|
||||
switch (event.type)
|
||||
{
|
||||
default:
|
||||
break;
|
||||
|
||||
case sf::Event::MouseButtonPressed:
|
||||
if (isUnderMouse(event.mouseButton.x, event.mouseButton.y))
|
||||
{
|
||||
_pressed = true;
|
||||
_button_content.setFillColor(sf::Color(155, 155, 155));
|
||||
}
|
||||
break;
|
||||
|
||||
case sf::Event::MouseButtonReleased:
|
||||
if (_pressed)
|
||||
{
|
||||
_button_content.setFillColor(sf::Color::White);
|
||||
_pressed = false;
|
||||
if (isUnderMouse(event.mouseButton.x, event.mouseButton.y))
|
||||
_on_click_callback();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
Widget::input(event);
|
||||
}
|
||||
|
||||
void BPMCalculatorWindow::update()
|
||||
{
|
||||
Widget::update();
|
||||
}
|
||||
|
||||
void BPMCalculatorWindow::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
||||
{
|
||||
target.draw(_button_content, states);
|
||||
target.draw(_button_text, states);
|
||||
|
||||
Widget::draw(target, states);
|
||||
}
|
||||
|
||||
void BPMCalculatorWindow::setRect(const sf::FloatRect& rect)
|
||||
{
|
||||
_button_content.setPosition(rect.left, rect.top);
|
||||
_button_content.setSize({rect.width, rect.height});
|
||||
}
|
||||
|
||||
void BPMCalculatorWindow::setPosition(const sf::Vector2f &position)
|
||||
{
|
||||
_button_content.setPosition(position);
|
||||
}
|
||||
|
||||
bool BPMCalculatorWindow::isUnderMouse(int mouse_x, int mouse_y) const
|
||||
{
|
||||
return _button_content.getGlobalBounds().contains(mouse_x, mouse_y);
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
#pragma once
|
||||
|
||||
#include "widget.h"
|
||||
#include <functional>
|
||||
#include <SFML/Graphics/RectangleShape.hpp>
|
||||
#include <SFML/Graphics/Text.hpp>
|
||||
|
||||
class BPMCalculatorWindow : public Widget
|
||||
{
|
||||
public:
|
||||
BPMCalculatorWindow();
|
||||
|
||||
virtual void input(const sf::Event& event) override;
|
||||
virtual void update() override;
|
||||
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
|
||||
virtual void setRect(const sf::FloatRect& rect) override;
|
||||
virtual void setPosition(const sf::Vector2f& position) override;
|
||||
virtual bool isUnderMouse(int mouse_x, int mouse_y) const override;
|
||||
|
||||
private:
|
||||
sf::RectangleShape _button_content;
|
||||
sf::Text _button_text;
|
||||
bool _pressed;
|
||||
|
||||
std::function<void(void)> _on_click_callback;
|
||||
};
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
#include "menubar.h"
|
||||
#include "iostream"
|
||||
MenuBar::MenuBar()
|
||||
{
|
||||
}
|
||||
|
||||
void MenuBar::input(const sf::Event &event)
|
||||
{
|
||||
Widget::input(event);
|
||||
|
||||
for (const auto& bar_button : _bar_buttons)
|
||||
bar_button->input(event);
|
||||
}
|
||||
|
||||
void MenuBar::update()
|
||||
{
|
||||
Widget::update();
|
||||
}
|
||||
|
||||
void MenuBar::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
||||
{
|
||||
Widget::draw(target, states);
|
||||
|
||||
target.draw(_bar_rect, states);
|
||||
|
||||
for (const auto& bar_button : _bar_buttons)
|
||||
target.draw(*bar_button, states);
|
||||
}
|
||||
|
||||
void MenuBar::setRect(const sf::FloatRect& rect)
|
||||
{
|
||||
_bar_rect.setPosition(rect.left, rect.top);
|
||||
_bar_rect.setSize({rect.width, rect.height});
|
||||
|
||||
int x = 0;
|
||||
int button_width = 100;
|
||||
for (const auto& bar_button : _bar_buttons)
|
||||
{
|
||||
bar_button->setRect(sf::FloatRect(x, 0, button_width, _bar_rect.getSize().y));
|
||||
x += button_width;
|
||||
}
|
||||
}
|
||||
|
||||
void MenuBar::setPosition(const sf::Vector2f& position)
|
||||
{
|
||||
_bar_rect.setPosition(position);
|
||||
}
|
||||
|
||||
bool MenuBar::isUnderMouse(int mouse_x, int mouse_y) const
|
||||
{
|
||||
return _bar_rect.getGlobalBounds().contains(mouse_x, mouse_y);
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
#pragma once
|
||||
|
||||
#include "widget.h"
|
||||
#include "menubutton.h"
|
||||
#include "menudrop.h"
|
||||
|
||||
#include <SFML/Graphics/RectangleShape.hpp>
|
||||
|
||||
class MenuBar : public Widget
|
||||
{
|
||||
public:
|
||||
explicit MenuBar();
|
||||
|
||||
virtual void input(const sf::Event& event) override;
|
||||
virtual void update() override;
|
||||
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
|
||||
virtual void setRect(const sf::FloatRect& rect) override;
|
||||
virtual void setPosition(const sf::Vector2f& position) override;
|
||||
virtual bool isUnderMouse(int mouse_x, int mouse_y) const override;
|
||||
|
||||
bool isActive() const;
|
||||
void addSubMenu(std::unique_ptr<MenuDrop>&& submenu);
|
||||
|
||||
private:
|
||||
sf::RectangleShape _bar_rect;
|
||||
std::vector<std::unique_ptr<MenuButton>> _bar_buttons;
|
||||
};
|
|
@ -0,0 +1,22 @@
|
|||
#pragma once
|
||||
|
||||
#include "widget.h"
|
||||
|
||||
#include <SFML/Graphics/RectangleShape.hpp>
|
||||
|
||||
class MenuButton : public Widget
|
||||
{
|
||||
public:
|
||||
explicit MenuButton(std::string&& text);
|
||||
|
||||
virtual void input(const sf::Event& event) = 0;
|
||||
virtual void update() = 0;
|
||||
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const = 0;
|
||||
virtual void setRect(const sf::FloatRect& rect) = 0;
|
||||
virtual void setPosition(const sf::Vector2f& position) = 0;
|
||||
virtual bool isUnderMouse(int mouse_x, int mouse_y) const = 0;
|
||||
|
||||
virtual bool isActive() const = 0;
|
||||
virtual void setActive(bool is_active) = 0;
|
||||
virtual void setHovered(bool is_hovered) = 0;
|
||||
};
|
|
@ -0,0 +1,44 @@
|
|||
#include "menudrop.h"
|
||||
|
||||
void MenuDrop::input(const sf::Event &event)
|
||||
{
|
||||
for (auto& child : _children)
|
||||
child->input(event);
|
||||
}
|
||||
|
||||
void MenuDrop::update()
|
||||
{
|
||||
for (auto& child : _children)
|
||||
child->update();
|
||||
}
|
||||
|
||||
void MenuDrop::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
||||
{
|
||||
if (!_is_visible)
|
||||
return;
|
||||
|
||||
for (auto& child : _children)
|
||||
child->draw(target, states);
|
||||
}
|
||||
|
||||
void MenuDrop::setRect(const sf::FloatRect& rect)
|
||||
{
|
||||
(void)rect;
|
||||
/*_bar_rect.setPosition(rect.left, rect.top);
|
||||
_bar_rect.setSize({rect.width, rect.height});
|
||||
|
||||
_bar_button_rect.setSize(sf::Vector2f(100, _bar_rect.getSize().y));*/
|
||||
}
|
||||
|
||||
void MenuDrop::setPosition(const sf::Vector2f& position)
|
||||
{
|
||||
(void)position;
|
||||
//_bar_rect.setPosition(position);
|
||||
}
|
||||
|
||||
bool MenuDrop::isUnderMouse(int mouse_x, int mouse_y) const
|
||||
{
|
||||
(void)mouse_x;
|
||||
(void)mouse_y;
|
||||
return false;//_bar_rect.getGlobalBounds().contains(mouse_x, mouse_y);
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
#pragma once
|
||||
|
||||
#include "widget.h"
|
||||
|
||||
#include <SFML/Graphics/RectangleShape.hpp>
|
||||
|
||||
class MenuDrop : public Widget
|
||||
{
|
||||
public:
|
||||
virtual void input(const sf::Event& event) override;
|
||||
virtual void update() override;
|
||||
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
|
||||
virtual void setRect(const sf::FloatRect& rect) override;
|
||||
virtual void setPosition(const sf::Vector2f& position) override;
|
||||
virtual bool isUnderMouse(int mouse_x, int mouse_y) const override;
|
||||
|
||||
private:
|
||||
sf::RectangleShape _content_rect;
|
||||
sf::RectangleShape _bar_button_rect;
|
||||
|
||||
sf::RectangleShape _drop_rect;
|
||||
sf::RectangleShape _drop_button_rect;
|
||||
};
|
Loading…
Reference in New Issue