@ -0,0 +1,28 @@ | |||
/* | |||
* Copyright (C) 2017-2019 Christopher J. Howard | |||
* | |||
* This file is part of Antkeeper Source Code. | |||
* | |||
* Antkeeper Source Code is free software: you can redistribute it and/or modify | |||
* it under the terms of the GNU General Public License as published by | |||
* the Free Software Foundation, either version 3 of the License, or | |||
* (at your option) any later version. | |||
* | |||
* Antkeeper Source Code is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU General Public License | |||
* along with Antkeeper Source Code. If not, see <http://www.gnu.org/licenses/>. | |||
*/ | |||
#ifndef GAME_STATES_HPP | |||
#define GAME_STATES_HPP | |||
#include <functional> | |||
#endif // GAME_STATES_HPP | |||
@ -0,0 +1,126 @@ | |||
/* | |||
* Copyright (C) 2017-2019 Christopher J. Howard | |||
* | |||
* This file is part of Antkeeper Source Code. | |||
* | |||
* Antkeeper Source Code is free software: you can redistribute it and/or modify | |||
* it under the terms of the GNU General Public License as published by | |||
* the Free Software Foundation, either version 3 of the License, or | |||
* (at your option) any later version. | |||
* | |||
* Antkeeper Source Code is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU General Public License | |||
* along with Antkeeper Source Code. If not, see <http://www.gnu.org/licenses/>. | |||
*/ | |||
#include "menu.hpp" | |||
MenuItem::MenuItem(): | |||
activatedCallback(nullptr) | |||
{ | |||
container = new UIContainer(); | |||
container->setAnchor(Anchor::TOP_LEFT); | |||
nameLabel = new UILabel(); | |||
nameLabel->setAnchor(Anchor::TOP_LEFT); | |||
valueLabel = new UILabel(); | |||
valueLabel->setAnchor(Anchor::TOP_RIGHT); | |||
container->addChild(nameLabel); | |||
container->addChild(valueLabel); | |||
} | |||
MenuItem::~MenuItem() | |||
{ | |||
delete container; | |||
delete nameLabel; | |||
delete valueLabel; | |||
} | |||
void MenuItem::setFont(Font* font) | |||
{ | |||
nameLabel->setFont(font); | |||
valueLabel->setFont(font); | |||
} | |||
void MenuItem::setName(const std::string& name) | |||
{ | |||
nameLabel->setText(name); | |||
} | |||
void MenuItem::setValue(const std::string& value) | |||
{ | |||
valueLabel->setText(value); | |||
} | |||
void MenuItem::setActivatedCallback(std::function<void()> callback) | |||
{ | |||
activatedCallback = callback; | |||
} | |||
void MenuItem::activate() | |||
{ | |||
if (activatedCallback) | |||
{ | |||
activatedCallback(); | |||
} | |||
} | |||
Menu::Menu() | |||
{ | |||
container = new UIContainer(); | |||
} | |||
Menu::~Menu() | |||
{ | |||
removeItems(); | |||
delete container; | |||
} | |||
MenuItem* Menu::addItem() | |||
{ | |||
MenuItem* item = new MenuItem(); | |||
container->addChild(item->container); | |||
items.push_back(item); | |||
return item; | |||
} | |||
void Menu::removeItems() | |||
{ | |||
for (MenuItem* item: items) | |||
{ | |||
container->removeChild(item->container); | |||
delete item; | |||
} | |||
items.clear(); | |||
} | |||
void Menu::setFonts(Font* font) | |||
{ | |||
for (MenuItem* item: items) | |||
{ | |||
item->setFont(font); | |||
} | |||
} | |||
void Menu::resize(int w, int h) | |||
{ | |||
container->setDimensions(Vector2(w, h)); | |||
int spacing = h / items.size(); | |||
int offset = 0; | |||
for (MenuItem* item: items) | |||
{ | |||
item->container->setTranslation(Vector2(0, offset)); | |||
item->container->setDimensions(Vector2(w, item->getNameLabel()->getFont()->getMetrics().getHeight())); | |||
offset += spacing; | |||
} | |||
} | |||
@ -0,0 +1,127 @@ | |||
/* | |||
* Copyright (C) 2017-2019 Christopher J. Howard | |||
* | |||
* This file is part of Antkeeper Source Code. | |||
* | |||
* Antkeeper Source Code is free software: you can redistribute it and/or modify | |||
* it under the terms of the GNU General Public License as published by | |||
* the Free Software Foundation, either version 3 of the License, or | |||
* (at your option) any later version. | |||
* | |||
* Antkeeper Source Code is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU General Public License | |||
* along with Antkeeper Source Code. If not, see <http://www.gnu.org/licenses/>. | |||
*/ | |||
#ifndef MENU_HPP | |||
#define MENU_HPP | |||
#include <emergent/emergent.hpp> | |||
using namespace Emergent; | |||
#include "ui/ui.hpp" | |||
#include <functional> | |||
class Menu; | |||
class MenuItem | |||
{ | |||
public: | |||
MenuItem(); | |||
~MenuItem(); | |||
void setFont(Font* font); | |||
void setName(const std::string& name); | |||
void setValue(const std::string& value); | |||
void setActivatedCallback(std::function<void()> callback); | |||
void activate(); | |||
UIContainer* getContainer() const; | |||
UILabel* getNameLabel() const; | |||
UILabel* getValueLabel() const; | |||
private: | |||
friend class Menu; | |||
UIContainer* container; | |||
UILabel* nameLabel; | |||
UILabel* valueLabel; | |||
std::function<void()> activatedCallback; | |||
}; | |||
inline UIContainer* MenuItem::getContainer() const | |||
{ | |||
return container; | |||
} | |||
inline UILabel* MenuItem::getNameLabel() const | |||
{ | |||
return nameLabel; | |||
} | |||
inline UILabel* MenuItem::getValueLabel() const | |||
{ | |||
return valueLabel; | |||
} | |||
class Menu | |||
{ | |||
public: | |||
/// Creates a menu. | |||
Menu(); | |||
/// Destroys a menu. | |||
~Menu(); | |||
/** | |||
* Adds a new item to the menu. | |||
*/ | |||
MenuItem* addItem(); | |||
/** | |||
* Removes an item from the menu. | |||
*/ | |||
void removeItem(MenuItem* item); | |||
/** | |||
* Removes all items from the menu. | |||
*/ | |||
void removeItems(); | |||
/** | |||
* Sets the font for all menu items in the menu. | |||
*/ | |||
void setFonts(Font* font); | |||
const std::vector<MenuItem*>* getItems() const; | |||
UIContainer* getContainer() const; | |||
void resize(int w, int h); | |||
private: | |||
UIContainer* container; | |||
std::vector<MenuItem*> items; | |||
}; | |||
inline const std::vector<MenuItem*>* Menu::getItems() const | |||
{ | |||
return &items; | |||
} | |||
inline UIContainer* Menu::getContainer() const | |||
{ | |||
return container; | |||
} | |||
#endif // MENU_HPP | |||
@ -0,0 +1,29 @@ | |||
/* | |||
* Copyright (C) 2017-2019 Christopher J. Howard | |||
* | |||
* This file is part of Antkeeper Source Code. | |||
* | |||
* Antkeeper Source Code is free software: you can redistribute it and/or modify | |||
* it under the terms of the GNU General Public License as published by | |||
* the Free Software Foundation, either version 3 of the License, or | |||
* (at your option) any later version. | |||
* | |||
* Antkeeper Source Code is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU General Public License | |||
* along with Antkeeper Source Code. If not, see <http://www.gnu.org/licenses/>. | |||
*/ | |||
#include "scheduled-function-event.hpp" | |||
EventBase* ScheduledFunctionEvent::clone() const | |||
{ | |||
ScheduledFunctionEvent* event = new ScheduledFunctionEvent(); | |||
event->caller = caller; | |||
event->function = function; | |||
return event; | |||
} | |||
@ -0,0 +1,45 @@ | |||
/* | |||
* Copyright (C) 2017-2019 Christopher J. Howard | |||
* | |||
* This file is part of Antkeeper Source Code. | |||
* | |||
* Antkeeper Source Code is free software: you can redistribute it and/or modify | |||
* it under the terms of the GNU General Public License as published by | |||
* the Free Software Foundation, either version 3 of the License, or | |||
* (at your option) any later version. | |||
* | |||
* Antkeeper Source Code is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU General Public License | |||
* along with Antkeeper Source Code. If not, see <http://www.gnu.org/licenses/>. | |||
*/ | |||
#include "state-machine.hpp" | |||
StateMachine::StateMachine(): | |||
previousState(nullptr), | |||
currentState(nullptr) | |||
{} | |||
void StateMachine::changeState(const State* nextState) | |||
{ | |||
// Call exit function of current state | |||
if (currentState && currentState->back()) | |||
{ | |||
currentState->back()(); | |||
} | |||
// Change state | |||
previousState = currentState; | |||
currentState = nextState; | |||
// Call enter function of next state | |||
if (currentState && currentState->front()) | |||
{ | |||
currentState->front()(); | |||
} | |||
} | |||
@ -0,0 +1,73 @@ | |||
/* | |||
* Copyright (C) 2017-2019 Christopher J. Howard | |||
* | |||
* This file is part of Antkeeper Source Code. | |||
* | |||
* Antkeeper Source Code is free software: you can redistribute it and/or modify | |||
* it under the terms of the GNU General Public License as published by | |||
* the Free Software Foundation, either version 3 of the License, or | |||
* (at your option) any later version. | |||
* | |||
* Antkeeper Source Code is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU General Public License | |||
* along with Antkeeper Source Code. If not, see <http://www.gnu.org/licenses/>. | |||
*/ | |||
#ifndef STATE_MACHINE_HPP | |||
#define STATE_MACHINE_HPP | |||
#include <array> | |||
#include <functional> | |||
/** | |||
* Extremely lightweight finite-state machine. | |||
*/ | |||
class StateMachine | |||
{ | |||
public: | |||
/** | |||
* A state is a fixed-size array of function pointers with the first and second elements referring to the state's enter and exit functions, respectively. | |||
*/ | |||
typedef std::array<std::function<void()>, 2> State; | |||
/** | |||
* Creates a state machine, setting the initial state to nullptr. | |||
*/ | |||
StateMachine(); | |||
/** | |||
* Changes the current state. | |||
*/ | |||
void changeState(const State* nextState); | |||
/** | |||
* Returns the previous state. | |||
*/ | |||
const State* getPreviousState() const; | |||
/** | |||
* Returns the current state. | |||
*/ | |||
const State* getCurrentState() const; | |||
private: | |||
const State* previousState; | |||
const State* currentState; | |||
}; | |||
inline const StateMachine::State* StateMachine::getPreviousState() const | |||
{ | |||
return previousState; | |||
} | |||
inline const StateMachine::State* StateMachine::getCurrentState() const | |||
{ | |||
return currentState; | |||
} | |||
#endif // STATE_MACHINE_HPP | |||
@ -1,151 +0,0 @@ | |||
/* | |||
* Copyright (C) 2017-2019 Christopher J. Howard | |||
* | |||
* This file is part of Antkeeper Source Code. | |||
* | |||
* Antkeeper Source Code is free software: you can redistribute it and/or modify | |||
* it under the terms of the GNU General Public License as published by | |||
* the Free Software Foundation, either version 3 of the License, or | |||
* (at your option) any later version. | |||
* | |||
* Antkeeper Source Code is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU General Public License | |||
* along with Antkeeper Source Code. If not, see <http://www.gnu.org/licenses/>. | |||
*/ | |||
#include "splash-state.hpp" | |||
#include "game.hpp" | |||
#include "sandbox-state.hpp" | |||
#include "ui/ui.hpp" | |||
SplashState::SplashState(Game* game): | |||
GameState(game) | |||
{} | |||
SplashState::~SplashState() | |||
{} | |||
void SplashState::enter() | |||
{ | |||
AnimationChannel<float>* channel; | |||
// Construct fade-in animation clip | |||
fadeInClip.setInterpolator(lerp<float>); | |||
channel = fadeInClip.addChannel(0); | |||
channel->insertKeyframe(0.0f, 0.0f); | |||
channel->insertKeyframe(0.5f, 0.0f); | |||
channel->insertKeyframe(1.25f, 1.0f); | |||
channel->insertKeyframe(5.25f, 1.0f); | |||
// Construct fade-out animation clip | |||
fadeOutClip.setInterpolator(lerp<float>); | |||
channel = fadeOutClip.addChannel(0); | |||
channel->insertKeyframe(0.0f, 1.0f); | |||
channel->insertKeyframe(0.75f, 0.0f); | |||
channel->insertKeyframe(1.25f, 0.0f); | |||
// Setup animate callback to change splash screen opacity | |||
fadeAnimation.setAnimateCallback | |||
( | |||
[this](std::size_t id, float opacity) | |||
{ | |||
this->game->splashImage->setTintColor(Vector4(1.0f, 1.0f, 1.0f, opacity)); | |||
} | |||
); | |||
// Setup end callback to fade-out after fading in | |||
fadeAnimation.setEndCallback | |||
( | |||
[this]() | |||
{ | |||
// Change clip to fade-out | |||
this->fadeAnimation.setClip(&fadeOutClip); | |||
this->fadeAnimation.setTimeFrame(this->fadeOutClip.getTimeFrame()); | |||
this->fadeAnimation.rewind(); | |||
this->fadeAnimation.play(); | |||
// Setup end callback to change to title state after fading out | |||
this->fadeAnimation.setEndCallback | |||
( | |||
[this]() | |||
{ | |||
//this->game->changeState(this->game->titleState); | |||
this->game->changeState(this->game->sandboxState); | |||
} | |||
); | |||
} | |||
); | |||
// Setup fade animation to fade-in | |||
fadeAnimation.setSpeed(1.0f); | |||
fadeAnimation.setLoop(false); | |||
fadeAnimation.setClip(&fadeInClip); | |||
fadeAnimation.setTimeFrame(fadeInClip.getTimeFrame()); | |||
// Play the fade animation | |||
fadeAnimation.play(); | |||
// Add fade animation to the animator | |||
game->getAnimator()->addAnimation(&fadeAnimation); | |||
// Subscribe this state to input events | |||
game->getEventDispatcher()->subscribe<KeyPressedEvent>(this); | |||
game->getEventDispatcher()->subscribe<MouseButtonPressedEvent>(this); | |||
game->getEventDispatcher()->subscribe<GamepadButtonPressedEvent>(this); | |||
// Make splash screen visible | |||
game->splashBackgroundImage->setVisible(true); | |||
game->splashImage->setVisible(true); | |||
game->splashImage->setTintColor(Vector4(1.0f, 1.0f, 1.0f, 0.0f)); | |||
game->splashBackgroundImage->setTintColor(Vector4(0.0f, 0.0f, 0.0f, 1.0f)); | |||
game->splashImage->resetTweens(); | |||
game->splashBackgroundImage->resetTweens(); | |||
game->uiRootElement->update(); | |||
// Hide mouse | |||
game->mouse->setVisible(false); | |||
} | |||
void SplashState::execute() | |||
{} | |||
void SplashState::exit() | |||
{ | |||
// Remove fade animation from the animator | |||
game->getAnimator()->removeAnimation(&fadeAnimation); | |||
// Unsubscribe this state from input events | |||
game->getEventDispatcher()->unsubscribe<KeyPressedEvent>(this); | |||
game->getEventDispatcher()->unsubscribe<MouseButtonPressedEvent>(this); | |||
game->getEventDispatcher()->unsubscribe<GamepadButtonPressedEvent>(this); | |||
// Make splash screen invisible | |||
game->splashBackgroundImage->setVisible(false); | |||
game->splashImage->setVisible(false); | |||
} | |||
void SplashState::handleEvent(const KeyPressedEvent& event) | |||
{ | |||
skip(); | |||
} | |||
void SplashState::handleEvent(const MouseButtonPressedEvent& event) | |||
{ | |||
skip(); | |||
} | |||
void SplashState::handleEvent(const GamepadButtonPressedEvent& event) | |||
{ | |||
skip(); | |||
} | |||
void SplashState::skip() | |||
{ | |||
game->splashImage->setVisible(false); | |||
game->changeState(game->sandboxState); | |||
} | |||