Browse Source

Begin revising state machine architecture and add a menu system with control remapping support

master
C. J. Howard 5 years ago
parent
commit
4328e12e7c
Signed by: cjhoward GPG Key ID: 03E1FABA9C3EC195
15 changed files with 1615 additions and 304 deletions
  1. +28
    -0
      src/game-states.hpp
  2. +1011
    -76
      src/game.cpp
  3. +119
    -16
      src/game.hpp
  4. +126
    -0
      src/menu.cpp
  5. +127
    -0
      src/menu.hpp
  6. +1
    -1
      src/resources/csv-table-loader.cpp
  7. +2
    -2
      src/resources/csv-table.hpp
  8. +29
    -0
      src/scheduled-function-event.cpp
  9. +11
    -25
      src/scheduled-function-event.hpp
  10. +45
    -0
      src/state-machine.cpp
  11. +73
    -0
      src/state-machine.hpp
  12. +6
    -18
      src/states/sandbox-state.cpp
  13. +0
    -151
      src/states/splash-state.cpp
  14. +15
    -7
      src/ui/ui.cpp
  15. +22
    -8
      src/ui/ui.hpp

+ 28
- 0
src/game-states.hpp View File

@ -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

+ 1011
- 76
src/game.cpp
File diff suppressed because it is too large
View File


+ 119
- 16
src/game.hpp View File

@ -23,7 +23,10 @@
#include <emergent/emergent.hpp>
using namespace Emergent;
#include "state-machine.hpp"
#include "entity/entity-id.hpp"
#include "scheduled-function-event.hpp"
#include <array>
#include <map>
#include <string>
#include <vector>
@ -65,11 +68,15 @@ class SteeringSystem;
class LocomotionSystem;
class TerrainSystem;
class ComponentBase;
class Menu;
class MenuItem;
enum class ComponentType;
typedef std::vector<std::vector<std::string>> CSVTable;
class Game:
public Application
public Application,
public StateMachine,
public EventHandler<ScheduledFunctionEvent>
{
public:
/**
@ -84,20 +91,21 @@ public:
virtual ~Game();
/**
* Gets a string in the specified language.
* Gets a string in the current language.
*
* @param languageIndex Index of a language.
* @param name Name of the string.
* @return String in the specified language.
* @return String in the current language.
*/
std::string getString(std::size_t languageIndex, const std::string& name) const;
std::string getString(const std::string& name) const;
/**
* Changes the current language.
*
* @param languageIndex Index of the language to use.
*/
void changeLanguage(std::size_t languageIndex);
void changeLanguage(std::size_t nextLanguageIndex);
void nextLanguage();
/// Returns the number of available languages.
std::size_t getLanguageCount() const;
@ -105,7 +113,16 @@ public:
/// Returns the index of the current language.
std::size_t getLanguageIndex() const;
void openMenu(Menu* menu, int selectedItemIndex);
void closeCurrentMenu();
void selectMenuItem(int index, bool tween);
void selectNextMenuItem();
void selectPreviousMenuItem();
void activateMenuItem();
void activateLastMenuItem();
void toggleFullscreen();
void toggleVSync();
void setUpdateRate(double frequency);
/**
@ -123,6 +140,7 @@ public:
void fadeIn(float duration, const Vector3& color, std::function<void()> callback);
void fadeOut(float duration, const Vector3& color, std::function<void()> callback);
void stopFade();
void selectTool(int toolIndex);
@ -135,6 +153,7 @@ private:
virtual void handleEvent(const WindowResizedEvent& event);
virtual void handleEvent(const GamepadConnectedEvent& event);
virtual void handleEvent(const GamepadDisconnectedEvent& event);
virtual void handleEvent(const ScheduledFunctionEvent& event);
void setupDebugging();
void setupLocalization();
@ -148,11 +167,16 @@ private:
void loadSettings();
void saveSettings();
void loadStrings();
void loadControlProfile();
void saveControlProfile();
void loadFonts();
void loadControlProfile(const std::string& profileName);
void saveControlProfile(const std::string& profileName);
std::array<std::string, 3> getInputMappingStrings(const InputMapping* mapping);
void remapControl(Control* control);
void resetControls();
void resizeUI(int w, int h);
void restringUI();
void restringControlMenuItem(MenuItem* item, const std::string& name);
void resizeRenderTargets();
void setTimeOfDay(float time);
@ -160,9 +184,21 @@ private:
void queueScreenshot();
void screenshot();
// Callback for the input mapper
void mapInput(const InputMapping& mapping);
// State functions
void enterSplashState();
void exitSplashState();
void enterLoadingState();
void exitLoadingState();
void enterTitleState();
void exitTitleState();
void enterPlayState();
void exitPlayState();
void skipSplash();
public:
EntityID createInstance();
@ -182,9 +218,13 @@ public:
public:
// States
// Game states
StateMachine::State splashState;
StateMachine::State loadingState;
StateMachine::State titleState;
StateMachine::State playState;
GameState* currentState;
SplashState* splashState;
SandboxState* sandboxState;
// Paths
@ -219,7 +259,7 @@ public:
ControlSet systemControls;
Control exitControl;
Control toggleFullscreenControl;
Control screenshotControl;
Control takeScreenshotControl;
// Menu control set
ControlSet menuControls;
@ -227,14 +267,14 @@ public:
Control menuDownControl;
Control menuLeftControl;
Control menuRightControl;
Control menuSelectControl;
Control menuActivateControl;
Control menuBackControl;
// Camera control set
ControlSet cameraControls;
Control moveForwardControl;
Control moveBackControl;
Control moveLeftControl;
Control moveBackControl;
Control moveRightControl;
Control zoomInControl;
Control zoomOutControl;
@ -245,7 +285,7 @@ public:
// Tool control set
ControlSet toolControls;
Control openToolMenuControl;
Control changeToolControl;
Control useToolControl;
// Editor control set
@ -267,10 +307,10 @@ public:
float timestep;
// UI
Typeface* labelTypeface;
Font* labelFont;
Typeface* debugTypeface;
Font* debugFont;
Typeface* menuTypeface;
Font* menuFont;
BillboardBatch* uiBatch;
UIBatcher* uiBatcher;
UIContainer* uiRootElement;
@ -333,6 +373,49 @@ public:
Vector4 cameraGridColor;
Vector4 cameraReticleColor;
// Menu selection
UIImage* menuSelectorImage;
Menu* currentMenu;
Menu* previousMenu;
MenuItem* currentMenuItem;
MenuItem* previousMenuItem;
int menuItemIndex;
Vector4 menuItemActiveColor;
Vector4 menuItemInactiveColor;
// Main menu
Menu* mainMenu;
MenuItem* mainMenuContinueItem;
MenuItem* mainMenuNewGameItem;
MenuItem* mainMenuColoniesItem;
MenuItem* mainMenuSettingsItem;
MenuItem* mainMenuQuitItem;
// Settings menu
Menu* settingsMenu;
MenuItem* settingsMenuControlsItem;
MenuItem* settingsMenuFullscreenItem;
MenuItem* settingsMenuVSyncItem;
MenuItem* settingsMenuLanguageItem;
MenuItem* settingsMenuBackItem;
// Controls menu
Menu* controlsMenu;
MenuItem* controlsMenuMoveForwardItem;
MenuItem* controlsMenuMoveLeftItem;
MenuItem* controlsMenuMoveBackItem;
MenuItem* controlsMenuMoveRightItem;
MenuItem* controlsMenuChangeToolItem;
MenuItem* controlsMenuUseToolItem;
MenuItem* controlsMenuAdjustCameraItem;
MenuItem* controlsMenuToggleFullscreenItem;
MenuItem* controlsMenuTakeScreenshotItem;
MenuItem* controlsMenuResetToDefaultItem;
MenuItem* controlsMenuBackItem;
// Rendering
Renderer renderer;
RenderTarget defaultRenderTarget;
@ -364,6 +447,17 @@ public:
// Animation
Animator animator;
Animation<float> antHillZoomAnimation;
AnimationClip<float> antHillZoomClip;
Animation<float> menuFadeAnimation;
AnimationClip<float> menuFadeInClip;
Animation<float> splashFadeInAnimation;
Animation<float> splashFadeOutAnimation;
AnimationClip<float> splashFadeInClip;
AnimationClip<float> splashFadeOutClip;
Animation<float> fadeInAnimation;
Animation<float> fadeOutAnimation;
AnimationClip<float> fadeInClip;
@ -372,6 +466,14 @@ public:
std::function<void()> fadeOutEndCallback;
Animation<float> cameraFlashAnimation;
AnimationClip<float> cameraFlashClip;
Animation<float> menuSelectorSlideAnimation;
AnimationClip<float> menuSelectorSlideClip;
Animation<Vector4> menuItemSelectAnimation;
AnimationClip<Vector4> menuItemSelectClip;
Animation<Vector4> menuItemDeselectAnimation;
AnimationClip<Vector4> menuItemDeselectClip;
// Assets
ResourceManager* resourceManager;
@ -417,6 +519,7 @@ public:
bool vsync;
float fontSizePT;
std::string controlProfileName;
bool toggleFullscreenDisabled;
// Debugging
std::ofstream logFileStream;

+ 126
- 0
src/menu.cpp View File

@ -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;
}
}

+ 127
- 0
src/menu.hpp View File

@ -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

+ 1
- 1
src/resources/csv-table-loader.cpp View File

@ -120,7 +120,7 @@ void ResourceLoader::save(ResourceManager* resourceManager, std::ostre
for (std::size_t j = 0; j < row.size(); ++j)
{
const CSVColumn& column = row[j];
const CSVEntry& column = row[j];
(*os) << column;

+ 2
- 2
src/resources/csv-table.hpp View File

@ -23,8 +23,8 @@
#include <string>
#include <vector>
typedef std::string CSVColumn;
typedef std::vector<CSVColumn> CSVRow;
typedef std::string CSVEntry;
typedef std::vector<CSVEntry> CSVRow;
typedef std::vector<CSVRow> CSVTable;
#endif // CSV_TABLE_HPP

+ 29
- 0
src/scheduled-function-event.cpp View File

@ -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;
}

src/states/splash-state.hpp → src/scheduled-function-event.hpp View File

@ -17,39 +17,25 @@
* along with Antkeeper Source Code. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SPLASH_STATE_HPP
#define SPLASH_STATE_HPP
#include "game-state.hpp"
#ifndef SCHEDULED_FUNCTION_EVENT_HPP
#define SCHEDULED_FUNCTION_EVENT_HPP
#include <emergent/emergent.hpp>
using namespace Emergent;
/**
* Displays the splash screen.
* Event which asks a caller to execute a function when the event is handled.
*
* @ingroup input
*/
class SplashState:
public GameState,
public EventHandler<KeyPressedEvent>,
public EventHandler<MouseButtonPressedEvent>,
public EventHandler<GamepadButtonPressedEvent>
class ScheduledFunctionEvent: public Event<ScheduledFunctionEvent>
{
public:
SplashState(Game* game);
virtual ~SplashState();
virtual void enter();
virtual void execute();
virtual void exit();
virtual EventBase* clone() const;
private:
virtual void handleEvent(const KeyPressedEvent& event);
virtual void handleEvent(const MouseButtonPressedEvent& event);
virtual void handleEvent(const GamepadButtonPressedEvent& event);
void skip();
Animation<float> fadeAnimation;
AnimationClip<float> fadeInClip;
AnimationClip<float> fadeOutClip;
void* caller;
std::function<void()> function;
};
#endif // SPLASH_STATE_HPP
#endif // SCHEDULED_FUNCTION_EVENT_HPP

+ 45
- 0
src/state-machine.cpp View File

@ -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()();
}
}

+ 73
- 0
src/state-machine.hpp View File

@ -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

+ 6
- 18
src/states/sandbox-state.cpp View File

@ -47,7 +47,7 @@ void SandboxState::enter()
game->radialMenuContainer->setVisible(false);
// Show mouse
game->mouse->setVisible(false);
//game->mouse->setVisible(false);
game->fadeIn(1.0f, Vector3(0.0f), nullptr);
@ -56,22 +56,8 @@ void SandboxState::enter()
float elevation = glm::radians(30.0f);
float azimuth = glm::radians(-45.0f);
game->cameraRig = game->orbitCam;
game->orbitCam->setFocalPoint(focalPoint);
game->orbitCam->setTargetFocalPoint(focalPoint);
game->orbitCam->setFocalDistance(focalDistance);
game->orbitCam->setTargetFocalDistance(focalDistance);
game->orbitCam->setElevation(elevation);
game->orbitCam->setTargetElevation(elevation);
game->orbitCam->setAzimuth(azimuth);
game->orbitCam->setTargetAzimuth(azimuth);
game->freeCam->setTranslation(Vector3(-5, 5.0f, -5.0f));
//game->cameraRig = game->freeCam;
//game->mouse->setRelativeMode(true);
toolIndex = 0;
game->selectTool(toolIndex);
//game->selectTool(toolIndex);
//game->currentTool->setActive(false);
game->mouse->warp(game->window, game->w / 2, game->h / 2);
@ -86,7 +72,7 @@ void SandboxState::execute()
game->lightingPass->setTime(game->time);
bool menuClosed = false;
if (game->openToolMenuControl.isActive() && !game->openToolMenuControl.wasActive())
if (game->changeToolControl.isActive() && !game->changeToolControl.wasActive())
{
game->radialMenuContainer->setVisible(true);
game->hudContainer->setVisible(false);
@ -95,7 +81,7 @@ void SandboxState::execute()
selectorVector = Vector2(0.0f);
game->mouse->setRelativeMode(true);
}
else if (!game->openToolMenuControl.isActive() && game->openToolMenuControl.wasActive())
else if (!game->changeToolControl.isActive() && game->changeToolControl.wasActive())
{
game->radialMenuContainer->setVisible(false);
//game->hudContainer->setVisible(true);
@ -186,12 +172,14 @@ void SandboxState::execute()
float logMovementSpeed = lerp(logMinMovementSpeed, logMaxMovementSpeed, 1.0f - zoom);
float movementSpeed = std::exp(logMovementSpeed);
/*
game->orbitCam->setTargetFocalDistance(focalDistance);
game->orbitCam->getCamera()->setPerspective(fov, (float)game->w / (float)game->h, clipNear, clipFar);
game->orbitCam->move(direction * movementSpeed);
game->orbitCam->setFocalPoint(game->orbitCam->getTargetFocalPoint());
//game->orbitCam->setTargetElevation(elevation);
//
*/
if (game->cameraRig == game->freeCam)
{

+ 0
- 151
src/states/splash-state.cpp View File

@ -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);
}

+ 15
- 7
src/ui/ui.cpp View File

@ -39,7 +39,7 @@ UIElement::UIElement():
tintColor(1.0f),
color(tintColor),
visible(true),
active(true),
callbacksEnabled(true),
mouseOver(false),
mouseOverCallback(nullptr),
mouseOutCallback(nullptr),
@ -56,7 +56,8 @@ UIElement::UIElement():
rotationTween(&rotation, lerp<float>),
dimensionsTween(&dimensions, lerp<Vector2>),
positionTween(&position, lerp<Vector2>),
tintColorTween(&tintColor, lerp<Vector4>)
tintColorTween(&tintColor, lerp<Vector4>),
colorTween(&color, lerp<Vector4>)
{}
UIElement::~UIElement()
@ -142,7 +143,7 @@ void UIElement::setMouseReleasedCallback(std::function call
void UIElement::handleEvent(const MouseMovedEvent& event)
{
if (!active)
if (!callbacksEnabled)
{
return;
}
@ -180,7 +181,7 @@ void UIElement::handleEvent(const MouseMovedEvent& event)
void UIElement::handleEvent(const MouseButtonPressedEvent& event)
{
if (!active)
if (!callbacksEnabled)
{
return;
}
@ -201,7 +202,7 @@ void UIElement::handleEvent(const MouseButtonPressedEvent& event)
void UIElement::handleEvent(const MouseButtonReleasedEvent& event)
{
if (!active)
if (!callbacksEnabled)
{
return;
}
@ -228,6 +229,7 @@ void UIElement::interpolate(float dt)
dimensionsTween.interpolate(dt);
positionTween.interpolate(dt);
tintColorTween.interpolate(dt);
colorTween.interpolate(dt);
for (UIElement* child: children)
{
@ -243,6 +245,12 @@ void UIElement::resetTweens()
dimensionsTween.reset();
positionTween.reset();
tintColorTween.reset();
colorTween.reset();
for (UIElement* child: children)
{
child->resetTweens();
}
}
UILabel::UILabel():
@ -401,7 +409,7 @@ void UIBatcher::batchLabel(BillboardBatch* result, const UILabel* label)
const Font* font = label->getFont();
std::size_t index = range->start + range->length;
std::size_t count = 0;
font->puts(result, origin, label->getText(), label->getColor(), index, &count);
font->puts(result, origin, label->getText(), label->getColorTween()->getSubstate(), index, &count);
for (std::size_t i = index; i < index + count; ++i)
{
@ -443,7 +451,7 @@ void UIBatcher::batchImage(BillboardBatch* result, const UIImage* image)
}
billboard->setTextureCoordinates(image->getTextureBounds().getMin(), image->getTextureBounds().getMax());
billboard->setTintColor(image->getTintColorTween()->getSubstate());
billboard->setTintColor(image->getColorTween()->getSubstate());
billboard->resetTweens();
// Increment range length

+ 22
- 8
src/ui/ui.hpp View File

@ -86,7 +86,7 @@ public:
void setVisible(bool visible);
/// Enables or disables callbacks
void setActive(bool active);
void setCallbacksEnabled(bool enabled);
/// Returns the type of this element
virtual UIElement::Type getElementType() const = 0;
@ -148,8 +148,8 @@ public:
/// Returns the visibility of this element
bool isVisible() const;
/// Returns `true` if the element is active (callbacks enabled)
bool isActive() const;
/// Returns `true` if the element callbacks are enabled
bool areCallbacksEnabled() const;
/// Calculates the world-space position and bounds of this element and its children
virtual void update();
@ -185,6 +185,9 @@ public:
Tween<Vector2>* getPositionTween();
const Tween<Vector4>* getTintColorTween() const;
Tween<Vector4>* getTintColorTween();
const Tween<Vector4>* getColorTween() const;
Tween<Vector4>* getColorTween();
protected:
UIMaterial material;
@ -199,7 +202,7 @@ private:
Vector4 tintColor;
Vector4 color;
bool visible;
bool active;
bool callbacksEnabled;
bool mouseOver;
std::function<void()> mouseOverCallback;
std::function<void()> mouseOutCallback;
@ -219,6 +222,7 @@ private:
Tween<Vector2> dimensionsTween;
Tween<Vector2> positionTween;
Tween<Vector4> tintColorTween;
Tween<Vector4> colorTween;
};
inline void UIElement::setAnchor(const Vector2& anchor)
@ -261,9 +265,9 @@ inline void UIElement::setVisible(bool visible)
this->visible = visible;
}
inline void UIElement::setActive(bool active)
inline void UIElement::setCallbacksEnabled(bool enabled)
{
this->active = active;
this->callbacksEnabled = enabled;
}
inline const UIElement* UIElement::getParent() const
@ -361,9 +365,9 @@ inline bool UIElement::isVisible() const
return visible;
}
inline bool UIElement::isActive() const
inline bool UIElement::areCallbacksEnabled() const
{
return active;
return callbacksEnabled;
}
inline const Tween<Vector2>* UIElement::getOriginTween() const
@ -426,6 +430,16 @@ inline Tween* UIElement::getTintColorTween()
return &tintColorTween;
}
inline const Tween<Vector4>* UIElement::getColorTween() const
{
return &colorTween;
}
inline Tween<Vector4>* UIElement::getColorTween()
{
return &colorTween;
}
class UIContainer: public UIElement
{
public:

Loading…
Cancel
Save