-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add overlay messages
- Loading branch information
Showing
12 changed files
with
493 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
#include "ScrubAttractState.hpp" | ||
|
||
#include <crogine/core/App.hpp> | ||
|
||
#include <crogine/ecs/components/Transform.hpp> | ||
#include <crogine/ecs/components/Drawable2D.hpp> | ||
#include <crogine/ecs/components/Text.hpp> | ||
#include <crogine/ecs/components/Camera.hpp> | ||
|
||
#include <crogine/ecs/systems/TextSystem.hpp> | ||
#include <crogine/ecs/systems/CameraSystem.hpp> | ||
#include <crogine/ecs/systems/RenderSystem2D.hpp> | ||
|
||
#include <crogine/graphics/Font.hpp> | ||
|
||
ScrubAttractState::ScrubAttractState(cro::StateStack& ss, cro::State::Context ctx) | ||
: cro::State(ss, ctx), | ||
m_uiScene (ctx.appInstance.getMessageBus()) | ||
{ | ||
addSystems(); | ||
buildScene(); | ||
} | ||
|
||
//public | ||
bool ScrubAttractState::handleEvent(const cro::Event& evt) | ||
{ | ||
if (evt.type == SDL_KEYDOWN) | ||
{ | ||
switch (evt.key.keysym.sym) | ||
{ | ||
default: break; | ||
case SDLK_SPACE: | ||
requestStackPop(); | ||
requestStackPush(States::ScratchPad::Scrub); | ||
break; | ||
} | ||
} | ||
else if (evt.type == SDL_CONTROLLERBUTTONDOWN) | ||
{ | ||
switch (evt.cbutton.button) | ||
{ | ||
default: break; | ||
case cro::GameController::ButtonA: | ||
requestStackPop(); | ||
requestStackPush(States::ScratchPad::Scrub); | ||
break; | ||
} | ||
} | ||
|
||
|
||
m_uiScene.forwardEvent(evt); | ||
return false; | ||
} | ||
|
||
void ScrubAttractState::handleMessage(const cro::Message& msg) | ||
{ | ||
m_uiScene.forwardMessage(msg); | ||
} | ||
|
||
bool ScrubAttractState::simulate(float dt) | ||
{ | ||
m_uiScene.simulate(dt); | ||
return false; | ||
} | ||
|
||
void ScrubAttractState::render() | ||
{ | ||
m_uiScene.render(); | ||
} | ||
|
||
//private | ||
void ScrubAttractState::addSystems() | ||
{ | ||
auto& mb = cro::App::getInstance().getMessageBus(); | ||
m_uiScene.addSystem<cro::TextSystem>(mb); | ||
m_uiScene.addSystem<cro::CameraSystem>(mb); | ||
m_uiScene.addSystem<cro::RenderSystem2D>(mb); | ||
} | ||
|
||
void ScrubAttractState::buildScene() | ||
{ | ||
const std::string str = | ||
R"( | ||
Use A/D to scrub or right thumb stick | ||
Use Q to insert and E to remove a ball (or LB and RB on the controller) | ||
Press SPACE or Controller A to add more soap | ||
Press ESCAPE or Start to Pause the game. | ||
Press SPACE or Controller A to begin. | ||
)"; | ||
|
||
//TODO use shared resources | ||
const std::int32_t fontID = 1; | ||
m_resources.fonts.load(fontID, "assets/golf/fonts/IBM_CGA.ttf"); | ||
|
||
const auto& font = m_resources.fonts.get(fontID); | ||
|
||
auto size = glm::vec2(cro::App::getWindow().getSize()); | ||
auto entity = m_uiScene.createEntity(); | ||
entity.addComponent<cro::Transform>().setPosition(size / 2.f); | ||
entity.getComponent<cro::Transform>().move({ 0.f, 60.f }); | ||
entity.addComponent<cro::Drawable2D>(); | ||
entity.addComponent<cro::Text>(font).setString(str); | ||
entity.getComponent<cro::Text>().setCharacterSize(16); | ||
entity.getComponent<cro::Text>().setAlignment(cro::Text::Alignment::Centre); | ||
|
||
auto resize = [&](cro::Camera& cam) | ||
{ | ||
glm::vec2 size(cro::App::getWindow().getSize()); | ||
cam.viewport = { 0.f, 0.f, 1.f, 1.f }; | ||
cam.setOrthographic(0.f, size.x, 0.f, size.y, -0.1f, 10.f); | ||
|
||
//send messge to UI elements to reposition them | ||
//cro::Command cmd; | ||
//cmd.targetFlags = CommandID::UI::UIElement; | ||
//cmd.action = | ||
// [size](cro::Entity e, float) | ||
// { | ||
// const auto& ui = e.getComponent<UIElement>(); | ||
// float x = std::floor(size.x * ui.relativePosition.x); | ||
// float y = std::floor(size.y * ui.relativePosition.y); | ||
// e.getComponent<cro::Transform>().setPosition(glm::vec3(glm::vec2(ui.absolutePosition + glm::vec2(x, y)), ui.depth)); | ||
|
||
// //TODO probably want to rescale downwards too? | ||
// }; | ||
//m_uiScene.getSystem<cro::CommandSystem>()->sendCommand(cmd); | ||
}; | ||
|
||
auto& cam = m_uiScene.getActiveCamera().getComponent<cro::Camera>(); | ||
cam.resizeCallback = resize; | ||
resize(cam); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#pragma once | ||
|
||
#include "../StateIDs.hpp" | ||
|
||
#include <crogine/core/State.hpp> | ||
|
||
#include <crogine/ecs/Scene.hpp> | ||
#include <crogine/graphics/ModelDefinition.hpp> | ||
|
||
class ScrubAttractState final : public cro::State | ||
{ | ||
public: | ||
ScrubAttractState(cro::StateStack&, cro::State::Context); | ||
|
||
cro::StateID getStateID() const override { return States::ScratchPad::ScrubAttract; } | ||
|
||
bool handleEvent(const cro::Event&) override; | ||
void handleMessage(const cro::Message&) override; | ||
bool simulate(float) override; | ||
void render() override; | ||
|
||
private: | ||
|
||
cro::Scene m_uiScene; | ||
cro::ResourceCollection m_resources; | ||
|
||
void addSystems(); | ||
void buildScene(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include "ScrubBackgroundState.hpp" | ||
|
||
ScrubBackgroundState::ScrubBackgroundState(cro::StateStack& ss, cro::State::Context ctx) | ||
:cro::State(ss, ctx) | ||
{ | ||
ctx.mainWindow.loadResources([this]() | ||
{ | ||
//addSystems(); | ||
//loadAssets(); | ||
//createScene(); | ||
//createUI(); | ||
|
||
cacheState(States::ScratchPad::Scrub); | ||
cacheState(States::ScratchPad::ScrubAttract); | ||
cacheState(States::ScratchPad::ScrubPause); | ||
}); | ||
|
||
requestStackPush(States::ScratchPad::ScrubAttract); | ||
} | ||
|
||
//public | ||
bool ScrubBackgroundState::handleEvent(const cro::Event&) | ||
{ | ||
return false; | ||
} | ||
|
||
void ScrubBackgroundState::handleMessage(const cro::Message&) | ||
{ | ||
|
||
} | ||
|
||
bool ScrubBackgroundState::simulate(float) | ||
{ | ||
return false; | ||
} | ||
|
||
void ScrubBackgroundState::render() | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#pragma once | ||
|
||
#include "../StateIDs.hpp" | ||
|
||
#include <crogine/core/State.hpp> | ||
|
||
class ScrubBackgroundState final : public cro::State | ||
{ | ||
public: | ||
ScrubBackgroundState(cro::StateStack&, cro::State::Context); | ||
|
||
cro::StateID getStateID() const override { return States::ScratchPad::ScrubBackground; } | ||
|
||
bool handleEvent(const cro::Event&) override; | ||
void handleMessage(const cro::Message&) override; | ||
bool simulate(float) override; | ||
void render() override; | ||
|
||
private: | ||
|
||
}; |
Oops, something went wrong.