Button::Button(float x,float y,float width
,float height,Font* font,std::string text
,Color idleColor,Color hoverColor,Color activeColor)
{
this->buttonState=BTN_IDLE;
this->button.setPosition(Vector2f(x,y));
this->button.setSize(Vector2f(width,height));
this->font=font;
this->text.setFont(*this->font);
this->text.setString(text);
this->text.setFillColor(Color::White);
this->text.setCharacterSize(12);
this->text.setPosition(
this->button.getPosition().x +(this->button.getGlobalBounds().width /2.f)-this->text.getGlobalBounds().width /2.f,
this->button.getPosition().y +(this->button.getGlobalBounds().height /2.f)-this->text.getGlobalBounds().width /2.f
);
this->activeColor=activeColor;
this->hoverColor =hoverColor;
this->idleColor=idleColor;
}
Button::~Button()
{
//dtor
}
const bool Button::isPressed() const
{
if(this->buttonState==BTN_ACTIVE)
{
return true;
}
return false;
}
void Button::update(const Vector2f& mousePos)
{
this->buttonState=BTN_IDLE;
if(this->button.getGlobalBounds().contains(mousePos))
{
this->buttonState=BTN_HOVER;
if(Mouse::isButtonPressed(Mouse::Left))
{
this->buttonState = BTN_ACTIVE;
}
}
switch (this->buttonState)
{
case BTN_IDLE:
this->button.setFillColor(this->idleColor);
break;
case BTN_HOVER:
this->button.setFillColor(this->hoverColor);
break;
case BTN_ACTIVE:
this->button.setFillColor(this->activeColor);
break;
default:
this->button.setFillColor(Color::Red);
break;
}
}
void Button::render(RenderTarget* target)
{
target->draw(this->button);
target->draw(this->text);
}
#ifndef BUTTON_H
#define BUTTON_H
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<sstream>
enum button_states{BTN_IDLE=0,BTN_HOVER,BTN_ACTIVE};
using namespace sf;
class Button
{
private:
short unsigned buttonState;
RectangleShape button;
Font* font;
Text text;
Color hoverColor;
Color activeColor;
Color idleColor;
public:
Button(float x,float y,float width
,float height,Font* font,std::string text
,Color idleColor,Color hoverColor,Color activeColor);
virtual ~Button();
const bool isPressed() const;
void update(const Vector2f& mousePos);
void render(RenderTarget* target);
};
#ifndef MAINMENUSTATE_H
#define MAINMENUSTATE_H
#include "GameState.h"
#include "Button.h"
using namespace sf;
class MainMenuState :
public State
{
private:
RectangleShape background;
Font font;
Button* gamestate_btn;
void initFonts();
void initKeybinds();
public:
MainMenuState(RenderWindow* window,std::map<std::string,int>* supportedKeys);
virtual ~MainMenuState();
void endState();
void updateInput(const float& dt);
void update(const float& dt);
void render(RenderTarget* target =NULL);
};
#endif // MAINMENUSTATE_H
#include "MainMenuState.h"
using namespace sf;
void MainMenuState::initKeybinds()
{
std::ifstream ifs("Config/supported_keybinds.ini");
if (ifs.is_open())
{
std::string key = "";
std::string key2 = "";
while (ifs >> key >> key2)
{
this->keybinds[key] = this->supportedKeys->at(key2);
}
}
ifs.close();
}
void MainMenuState::initFonts()
{
if(this->font.loadFromFile("Fonts/Dosis-Light.ttf"))
{
throw("ERROR::MAINMENUSTATE::COULD NOT LOAD FONT");
}
}
MainMenuState::MainMenuState(RenderWindow* window,std::map<std::string,int>* supportedKeys)
: State(window,supportedKeys)
{
this->initFonts();
this->initKeybinds();
this->gamestate_btn =new Button(100,100,150,50,
&this->font,"New Game",
Color(70,70,70,200),Color(150,150,150,200),Color(20,20,20,200));
this->background.setSize(Vector2f(window->getSize().x,window->getSize().y));
this->background.setFillColor(Color::Magenta);
}
MainMenuState::~MainMenuState()
{
delete this->gamestate_btn;
}
void MainMenuState::endState()
{
}
void MainMenuState::update(const float& dt)
{
this->updateMousePositions();
this->updateInput(dt);
this->gamestate_btn->update(this->mousePosView);
}
void MainMenuState::render(RenderTarget* target)
{
if(!target)
target =this->window;
target->draw(this->background);
this->gamestate_btn->render(target);
}
void MainMenuState::updateInput(const float & dt)
{
this->checkForQuit();
}
#include "State.h"
using namespace sf;
State::State(RenderWindow* window, std::map<std::string,int>* supportedKeys)
{
this->window=window;
this->supportedKeys =supportedKeys;
this->quit=false;
}
State::~State()
{
}
const bool & State::getQuit()const
{
return this->quit;
}
void State::checkForQuit()
{
if(Keyboard::isKeyPressed(Keyboard::Key(this->keybinds.at("CLOSE"))))
{
this->quit=true;
}
}
void State::updateMousePositions()
{
this->mousePosScreen =Mouse::getPosition();
this->mousePosWindow =Mouse::getPosition(*this->window);
this->mousePosView = this->window->mapPixelToCoords(Mouse::getPosition(*this->window));
}
#ifndef STATE_H
#define STATE_H
#include "Entity.h"
using namespace sf;
class State
{
protected:
RenderWindow* window;
std::map<std::string,int>* supportedKeys;
std::map<std::string,int> keybinds;
bool quit;
sf::Vector2i mousePosScreen;
sf::Vector2i mousePosWindow;
sf::Vector2f mousePosView;
std::vector<Texture> textures;
virtual void initKeybinds()=0;
public:
State(RenderWindow* window, std::map<std::string,int>* supportedKeys);
virtual ~State();
const bool& getQuit() const;
virtual void checkForQuit();
virtual void endState()=0;
virtual void updateMousePositions();
virtual void updateInput(const float& dt)=0;
virtual void update(const float& dt)=0;
virtual void render(RenderTarget* target=NULL)=0;
};
#endif // STATE_H