forked from NaiJi/project-kyoku
Implement test main menu state
parent
686107b215
commit
325d49270d
@ -1,48 +0,0 @@
|
||||
#include "button.h"
|
||||
|
||||
Button::Button(const std::string &text)
|
||||
{
|
||||
_button_text.setString(text);
|
||||
_button_text.setFillColor(sf::Color::Black);
|
||||
_button_content.setFillColor(sf::Color::White);
|
||||
}
|
||||
|
||||
void Button::input(const sf::Event& event)
|
||||
{
|
||||
switch (event.type)
|
||||
{
|
||||
case sf::Event::MouseButtonPressed:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Button::update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Button::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Button::setRect(const sf::IntRect& rect)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Button::setPosition(const sf::Vector2f& position)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Button::setText(const std::string& text)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Button::setCallback(std::function<void(void)> callback)
|
||||
{
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,41 @@
|
||||
#include "mainmenu.h"
|
||||
#include "widgets/button.h"
|
||||
#include "widgets/group.h"
|
||||
|
||||
MainMenu::MainMenu(sf::RenderWindow& game_window) :
|
||||
_buttons(std::make_shared<Group>()),
|
||||
_game_window(game_window)
|
||||
{
|
||||
std::shared_ptr<Button> button_start = std::make_shared<Button>("Start");
|
||||
button_start->setRect(sf::FloatRect(140, 140, 500, 100));
|
||||
button_start->setCallback([&]()
|
||||
{
|
||||
_game_window.close();
|
||||
});
|
||||
|
||||
std::shared_ptr<Button> button_exit = std::make_shared<Button>("Exit");
|
||||
button_exit->setRect(sf::FloatRect(140, 140, 400, 100));
|
||||
button_exit->setPosition({240, 340});
|
||||
button_exit->setCallback([&]()
|
||||
{
|
||||
_game_window.close();
|
||||
});
|
||||
|
||||
_buttons->addChild(button_start);
|
||||
_buttons->addChild(button_exit);
|
||||
}
|
||||
|
||||
void MainMenu::input(const sf::Event& event)
|
||||
{
|
||||
_buttons->input(event);
|
||||
}
|
||||
|
||||
void MainMenu::update()
|
||||
{
|
||||
_buttons->update();
|
||||
}
|
||||
|
||||
void MainMenu::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
||||
{
|
||||
target.draw(*_buttons, states);
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include "gui/state.h"
|
||||
#include <SFML/Graphics/RenderWindow.hpp>
|
||||
|
||||
class Group;
|
||||
|
||||
class MainMenu : public GUIState
|
||||
{
|
||||
public:
|
||||
MainMenu(sf::RenderWindow& game_window);
|
||||
virtual void input(const sf::Event& event) override;
|
||||
virtual void update() override;
|
||||
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<Group> _buttons;
|
||||
sf::RenderWindow& _game_window;
|
||||
};
|
||||
|
@ -1,13 +0,0 @@
|
||||
#include "widget.h"
|
||||
|
||||
Widget::Widget(const std::shared_ptr<Widget>& parent) :
|
||||
_parent(parent)
|
||||
{
|
||||
if (_parent)
|
||||
_parent->addChild(shared_from_this());
|
||||
}
|
||||
|
||||
void Widget::addChild(const std::shared_ptr<Widget> &child)
|
||||
{
|
||||
_children.emplace_back(child);
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
#include "button.h"
|
||||
|
||||
Button::Button(const std::string &text) :
|
||||
_pressed(false)
|
||||
{
|
||||
setText(text);
|
||||
_button_text.setFillColor(sf::Color::Black);
|
||||
_button_content.setFillColor(sf::Color::White);
|
||||
}
|
||||
|
||||
void Button::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 Button::update()
|
||||
{
|
||||
Widget::update();
|
||||
}
|
||||
|
||||
void Button::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
||||
{
|
||||
target.draw(_button_content, states);
|
||||
target.draw(_button_text, states);
|
||||
|
||||
Widget::draw(target, states);
|
||||
}
|
||||
|
||||
void Button::setRect(const sf::FloatRect& rect)
|
||||
{
|
||||
_button_content.setPosition(rect.left, rect.top);
|
||||
_button_content.setSize({rect.width - rect.left, rect.height - rect.top});
|
||||
}
|
||||
|
||||
void Button::setPosition(const sf::Vector2f &position)
|
||||
{
|
||||
_button_content.setPosition(position);
|
||||
}
|
||||
|
||||
bool Button::isUnderMouse(int mouse_x, int mouse_y) const
|
||||
{
|
||||
return _button_content.getGlobalBounds().contains(mouse_x, mouse_y);
|
||||
}
|
||||
|
||||
void Button::setText(const std::string& text)
|
||||
{
|
||||
_button_text.setString(text);
|
||||
}
|
||||
|
||||
void Button::setCallback(std::function<void(void)> callback)
|
||||
{
|
||||
_on_click_callback = callback;
|
||||
}
|
||||
|
@ -0,0 +1,32 @@
|
||||
#include "group.h"
|
||||
|
||||
void Group::input(const sf::Event& event)
|
||||
{
|
||||
Widget::input(event);
|
||||
}
|
||||
|
||||
void Group::update()
|
||||
{
|
||||
Widget::update();
|
||||
}
|
||||
|
||||
void Group::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
||||
{
|
||||
Widget::draw(target, states);
|
||||
}
|
||||
|
||||
void Group::setRect(const sf::FloatRect& rect)
|
||||
{
|
||||
_rect = rect;
|
||||
}
|
||||
|
||||
void Group::setPosition(const sf::Vector2f& position)
|
||||
{
|
||||
_rect.top = position.y;
|
||||
_rect.left = position.x;
|
||||
}
|
||||
|
||||
bool Group::isUnderMouse(int mouse_x, int mouse_y) const
|
||||
{
|
||||
return _rect.contains(mouse_x, mouse_y);
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include "widget.h"
|
||||
|
||||
class Group : 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::FloatRect _rect;
|
||||
};
|
||||
|
@ -0,0 +1,25 @@
|
||||
#include "widget.h"
|
||||
|
||||
void Widget::input(const sf::Event &event)
|
||||
{
|
||||
for (auto& child : _children)
|
||||
child->input(event);
|
||||
}
|
||||
|
||||
void Widget::update()
|
||||
{
|
||||
for (auto& child : _children)
|
||||
child->update();
|
||||
}
|
||||
|
||||
void Widget::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
||||
{
|
||||
for (auto& child : _children)
|
||||
child->draw(target, states);
|
||||
}
|
||||
|
||||
void Widget::addChild(const std::shared_ptr<Widget> &child)
|
||||
{
|
||||
child->_parent = shared_from_this();
|
||||
_children.emplace_back(child);
|
||||
}
|
Loading…
Reference in New Issue