Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Button hudget #546

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
ef177fe
wip
gersseba Mar 5, 2014
508130f
Buttons have borders
gersseba Mar 6, 2014
17c6736
wip
gersseba Mar 8, 2014
ccaa5c0
Merge branch 'master' of https://github.com/voxelinc/voxellancer into…
gersseba Mar 16, 2014
6abe92d
update glow
gersseba Mar 16, 2014
de7421f
wip
gersseba Mar 17, 2014
28dd753
Merge branch 'master' of https://github.com/voxelinc/voxellancer into…
gersseba Mar 30, 2014
c483e2b
added assertion and constuctor with callback
gersseba Mar 30, 2014
30cbecb
fix const HUD issue
gersseba Mar 30, 2014
4cb1f33
fix reference from bind
gersseba Mar 30, 2014
2025c05
coding convention
gersseba Mar 31, 2014
b10a8e4
const ref & colors
gersseba Mar 31, 2014
ff88081
Merge branch 'master' of https://github.com/voxelinc/voxellancer into…
gersseba Mar 31, 2014
f9cc792
adapting to changes in hud from mission script pr
gersseba Mar 31, 2014
0ffe45d
more changes to fit recent pr
gersseba Mar 31, 2014
a8d7c0f
wip
gersseba Apr 21, 2014
d9bbe2b
Merge branch 'master' of https://github.com/voxelinc/voxellancer into…
gersseba Apr 21, 2014
e99667f
wip
gersseba Apr 22, 2014
617ff47
Button style
gersseba Apr 24, 2014
db528bd
Merge branch 'intro' of https://github.com/voxelinc/voxellancer into …
gersseba Apr 24, 2014
96328d3
something is not right yet
gersseba May 4, 2014
82f0e35
Merge branch 'master' of https://github.com/voxelinc/voxellancer into…
gersseba May 4, 2014
63b5c80
reset button working
gersseba May 4, 2014
e37bf54
Merge branch 'master' of github.com:voxelinc/voxellancer into button_…
May 18, 2014
2f8f3ba
fixed crash
gersseba May 31, 2014
50527ce
Merge branch 'master' of https://github.com/voxelinc/voxellancer into…
gersseba May 31, 2014
85435b5
Merge branch 'master' of https://github.com/voxelinc/voxellancer into…
gersseba Aug 1, 2014
ec7c65c
fix for problem with master merge
gersseba Aug 1, 2014
287ec9d
removed old code, changed to match code docs
gersseba Aug 11, 2014
3ed3521
button w/out callback possible
gersseba Aug 13, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/display/rendering/starfield.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ namespace glow {
class Buffer;
};


/**
* Renders a starfield around the camera.
* Old camera positions/orientations are stored in order to stretch the
Expand Down
19 changes: 19 additions & 0 deletions src/gamestate/gameplay/gameplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "camera/camerahead.h"
#include "camera/cameradolly.h"
#include "ui/hud/hud.h"
#include "ui/hud/buttonhudget.h"
#include "display/viewer.h"

#include "display/rendering/texturerenderer.h"
Expand All @@ -43,8 +44,10 @@ GamePlay::GamePlay(Game* game) :
m_freecamInput(new GamePlayFreecamInput(*this)),
m_freecamActive(false),
m_scene(new GamePlayScene(*this)),
m_scenario(new ScriptedScenario(this, "")),
m_soundManager(new SoundManager())
{
World::instance()->player().hud().resetButton()->setCallback((std::function<void(ClickType clickType)>)std::bind(&GamePlay::resetButtonCallback, this, std::placeholders::_1));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line seems to be the same as setResetCallback(). Please use this method then.

updateView();
setInitialSubState(m_runningState);

Expand Down Expand Up @@ -140,6 +143,8 @@ void GamePlay::loadScenario(int i) {
}

m_scenario->load();
setResetCallback();

}

void GamePlay::update(float deltaSec) {
Expand Down Expand Up @@ -170,3 +175,17 @@ void GamePlay::updateView() {
World::instance()->player().hud().setView(&m_game->viewer().view());
}

void GamePlay::resetButtonCallback(ClickType clickType){
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems unlogical to me to have the code reseting the scenario inside some callback of a click.

This should read (I don't care how you make it do that :P ) like:

m_scenario->reset(); // or World::instance()->reset(), dunno
setResetCallback();

you know what I mean?

TextureRenderer loadRenderer("data/textures/loading.dds");
loadRenderer.display("Loading Scenario...");

m_soundManager->stopAll();
m_scenario->clear();
updateView();
m_scenario->load();
setResetCallback();
}

void GamePlay::setResetCallback() {
World::instance()->player().hud().resetButton()->setCallback((std::function<void(ClickType clickType)>)std::bind(&GamePlay::resetButtonCallback, this, std::placeholders::_1));
}
5 changes: 5 additions & 0 deletions src/gamestate/gameplay/gameplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <memory>

#include "gamestate/gamestate.h"
#include "ui/clicktype.h"


class BaseScenario;
Expand Down Expand Up @@ -64,5 +65,9 @@ class GamePlay: public GameState {
std::unique_ptr<GamePlayFreecamInput> m_freecamInput;

bool m_freecamActive;

void resetButtonCallback(ClickType clickType);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation and stuff

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

o.O it doesn't look like that in my ide


void setResetCallback();
};

26 changes: 16 additions & 10 deletions src/gamestate/gameplay/input/gameplaynormalinput.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#include "gameplaynormalinput.h"

#ifdef WIN32
#include <windows.h>
#endif
#include <GL/glew.h>
#include <GLFW/glfw3.h>

Expand Down Expand Up @@ -103,7 +100,8 @@ GamePlayNormalInput::GamePlayNormalInput(GamePlay& gamePlay) :
m_lastfocus = glfwGetWindowAttrib(glfwGetCurrentContext(), GLFW_FOCUSED);

retrieveInputValues();
m_currentTimePressed = 0;
m_currentTimePressedRight = 0;
m_currentTimePressedLeft = 0;
}

void GamePlayNormalInput::onResizeEvent(const unsigned int width, const unsigned int height) {
Expand Down Expand Up @@ -143,11 +141,16 @@ void GamePlayNormalInput::onKeyEvent(int key, int scancode, int action, int mods

void GamePlayNormalInput::onMouseButtonEvent(int button, int action, int mods) {
if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_RELEASE) {
if (m_currentTimePressed > 0 && m_currentTimePressed < m_maxClickTime) {
if (m_currentTimePressedRight > 0 && m_currentTimePressedRight < m_maxClickTime) {
World::instance()->player().hud().onClick(ClickType::Selection);
} else {
}
m_currentTimePressed = 0;
m_currentTimePressedRight = 0;
}
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE) {
if (m_currentTimePressedLeft > 0 && m_currentTimePressedLeft < m_maxClickTime) {
World::instance()->player().hud().onClick(ClickType::Selection);
}
m_currentTimePressedLeft = 0;
}
}

Expand Down Expand Up @@ -239,11 +242,14 @@ void GamePlayNormalInput::processMouseUpdate(float deltaSec) {
m_fireUpdate = true;
}

if (glfwGetMouseButton(glfwGetCurrentContext(), GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS){
m_currentTimePressed += deltaSec;
if (glfwGetMouseButton(glfwGetCurrentContext(), GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS) {
m_currentTimePressedRight += deltaSec;
}
if (glfwGetMouseButton(glfwGetCurrentContext(), GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS) {
m_currentTimePressedLeft += deltaSec;
}

if (m_mouseControl || glfwGetMouseButton(glfwGetCurrentContext(), GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS && m_maxClickTime < m_currentTimePressed) {
if (m_mouseControl || glfwGetMouseButton(glfwGetCurrentContext(), GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS && m_maxClickTime < m_currentTimePressedRight) {
glm::vec3 rot;
x = ContextProvider::instance()->resolution().width() / 2 - (int)floor(x);
y = ContextProvider::instance()->resolution().height() / 2 - (int)floor(y);
Expand Down
3 changes: 2 additions & 1 deletion src/gamestate/gameplay/input/gameplaynormalinput.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ class GamePlayNormalInput : public GamePlayInput {
void addActionsToVector();
void retrieveInputValues();

float m_currentTimePressed;
float m_currentTimePressedLeft;
float m_currentTimePressedRight;

Property<float> m_deadzoneMouse;
Property<float> m_deadzoneGamepad;
Expand Down
2 changes: 2 additions & 0 deletions src/input/inputconfigurator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@ bool InputConfigurator::isConfiguring() {

void InputConfigurator::startConfiguration(InputClass inputClass) {
if (inputClass == InputClass::Primary) {
World::instance()->player().hud().showMissionInfo("", "Starting configuration for primary input device (keyboard), Please follow the instructions");
glow::info("Starting configuration for primary input device (keyboard), Please follow the instructions");
} else {
World::instance()->player().hud().showMissionInfo("", "Starting configuration for secondary input device (gamepad/Joystick), Please follow the instructions");
glow::info("Starting configuration for secondary input device (gamepad/Joystick), Please follow the instructions");
m_idleValues.resize(m_secondaryInputValues->axisCnt);
for(int a = 0; a < m_secondaryInputValues->axisCnt; a++) {
Expand Down
3 changes: 0 additions & 3 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

#include "factions/factionmatrix.h"

#include "gamestate/game.h"
#include "gamestate/gameplay/gameplay.h"

#include "ui/hud/hud.h"
#include "ui/hud/hudget.h"
#include "ui/hud/aimhelperhudget.h"
Expand Down
4 changes: 2 additions & 2 deletions src/scenarios/basescenario.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class BaseScenario {
public:
BaseScenario(GamePlay* gamePlay);

void load();
void clear();
virtual void load();
virtual void clear();
void reset();


Expand Down
14 changes: 12 additions & 2 deletions src/scenarios/scriptedscenario.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@

ScriptedScenario::ScriptedScenario(GamePlay* gamePlay, const std::string& path):
BaseScenario(gamePlay),
m_script(new GamePlayScript(World::instance()->scriptEngine()))
m_script(new GamePlayScript(World::instance()->scriptEngine())),
m_path(path)
{
m_script->load(path);
}

ScriptedScenario::~ScriptedScenario() = default;
Expand All @@ -54,3 +54,13 @@ void ScriptedScenario::populateWorld() {
m_world->scriptEngine().addScript(m_script);
}

void ScriptedScenario::load() {
m_script.reset(new GamePlayScript(World::instance()->scriptEngine()));
m_script->load(m_path);
BaseScenario::load();
}

void ScriptedScenario::clear() {
m_script.reset();
BaseScenario::clear();
}
4 changes: 4 additions & 0 deletions src/scenarios/scriptedscenario.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ class ScriptedScenario: public BaseScenario {
ScriptedScenario(GamePlay* gamePlay, const std::string& path);
virtual ~ScriptedScenario();

virtual void load() override;
virtual void clear() override;

protected:
std::shared_ptr<GamePlayScript> m_script;

virtual void populateWorld() override;

std::string m_path;
};

7 changes: 7 additions & 0 deletions src/ui/clicktype.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

enum class ClickType {
None,
Selection,
Fire
};
50 changes: 50 additions & 0 deletions src/ui/hud/buttonhudget.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "buttonhudget.h"

#include "hud.h"
#include "buttonhudgetvoxels.h"

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nl++

ButtonHudget::ButtonHudget(HUD* hud, glm::vec3 direction, const std::function<void(ClickType clickType)>& callback, TextOrientation textOrientation, float scale, std::string content, FontSize fontSize, ButtonStyle buttonStyle) :
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const ref to glm::vec3

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...and to everything else you're passing by value that's not a primitive datatype ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

except for enums right? Compiler will only pass on an int anyway I believe.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enums are primitive datatypes, correct ;)

Hudget(hud),
m_buttonVoxels(new ButtonHudgetVoxels(this, direction, textOrientation, scale, content, fontSize, buttonStyle)),
m_callback(callback)
{
m_buttonVoxels->updateBounds();
}

ButtonHudget::~ButtonHudget() = default;

void ButtonHudget::update(float deltaSec) {
}

void ButtonHudget::draw() {
m_buttonVoxels->draw();
}

bool ButtonHudget::isAt(const Ray& ray) const {
return m_buttonVoxels->isAt(ray);
}

void ButtonHudget::onClick(ClickType clickType) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather like

if(m_callback)
m_callback(clickType)

mmh?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we allow setting nullptrs as callbacks? if you want no action on click you should rather use a dummy callback

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In any case it should be possible to not set a callback at all - I know of no GUI-API that requires you to react in some way if some button is pressed.

assert(m_callback);
m_callback(clickType);
}

void ButtonHudget::setCallback(const std::function<void(ClickType clickType)>& callback) {
m_callback = callback;
}

void ButtonHudget::setText(const std::string& content) {
m_buttonVoxels->setText(content);
}

void ButtonHudget::setTextOrientation(TextOrientation textOrientation) {
m_buttonVoxels->setTextOrientation(textOrientation);
}

ButtonStyle ButtonHudget::buttonStyle() {
return m_buttonVoxels->buttonStyle();
}

void ButtonHudget::setButtonStyle(ButtonStyle buttonStyle) {
m_buttonVoxels->setButtonStyle(buttonStyle);
}
40 changes: 40 additions & 0 deletions src/ui/hud/buttonhudget.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include <memory>
#include <string>
#include <functional>

#include "hudget.h"
#include "ui/voxelfontconstants.h"


class ButtonHudgetVoxels;
class TextFieldHudgetVoxels;

class ButtonHudget : public Hudget {
public:
ButtonHudget(HUD* hud, glm::vec3 direction, const std::function<void(ClickType clickType)>& callback, TextOrientation textOrientation = TextOrientation::BACKWARDS, float scale = 0.5f, std::string content = "", FontSize fontSize = FontSize::SIZE5x7, ButtonStyle borderStyle = ButtonStyle::BORDERED);
virtual ~ButtonHudget();

virtual void update(float deltaSec) override;
virtual void draw() override;

virtual bool isAt(const Ray& ray) const override;

virtual void onClick(ClickType clicktype) override;

virtual void setText(const std::string& content);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason why this is virtual? if none, please remove


void setCallback(const std::function<void(ClickType clickType)>& callback);

void setTextOrientation(TextOrientation textOrientation);

ButtonStyle buttonStyle();
void setButtonStyle(ButtonStyle buttonStyle);

protected:
std::function<void(ClickType clickType)> m_callback;
std::string m_content;
std::unique_ptr<ButtonHudgetVoxels> m_buttonVoxels;
};

72 changes: 72 additions & 0 deletions src/ui/hud/buttonhudgetvoxels.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include "buttonhudgetvoxels.h"

#include "buttonhudget.h"
#include "voxel/voxelcluster.h"
#include "voxel/voxel.h"
#include "voxel/voxelrenderer.h"
#include "hud.h"


ButtonHudgetVoxels::ButtonHudgetVoxels(ButtonHudget* buttonHudget, glm::vec3 direction, TextOrientation textOrientation, float scale, std::string content, FontSize fontSize, ButtonStyle buttonStyle) :
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, please use constrefs

TextFieldHudgetVoxels(buttonHudget, direction, textOrientation, scale, content, fontSize),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indent

m_buttonVoxels(new VoxelCluster(scale)),
m_buttonStyle(buttonStyle),
m_hudget(buttonHudget)
{
setText(content);
}

ButtonHudgetVoxels::~ButtonHudgetVoxels() = default;

void ButtonHudgetVoxels::updateBounds() {
if (m_buttonStyle == ButtonStyle::PLAIN) {
return;
}
m_buttonVoxels.reset(new VoxelCluster(m_scale));
int width = (int)(m_width/m_scale)*m_text.size()+1;
int height = (int)(m_height/m_scale)*2;
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if (i == 0 || j == 0 || i == width - 1 || j == height - 1) {
m_buttonVoxels->addVoxel(new Voxel(glm::ivec3(i, j, 0), 0x0FF00F));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume these are border and content colors? please make them constants

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or even better: configurable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will declare them at the beginning of the method (as it is done in all other hudgets). I created a #554 which addresses the issue of a configurable color theme.

} else if (m_buttonStyle == ButtonStyle::BORDERED_FILLED) {
m_buttonVoxels->addVoxel(new Voxel(glm::ivec3(i, j, 1), 0x17012D));
}
}
}
}

void ButtonHudgetVoxels::draw() {
if (m_buttonStyle == ButtonStyle::BORDERED || m_buttonStyle == ButtonStyle::BORDERED_FILLED) {
switch (textOrientation()) {
case TextOrientation::BACKWARDS:
m_buttonVoxels->transform().setPosition(lowerRight());
m_buttonVoxels->transform().setOrientation(m_hudget->hud()->orientation());
m_buttonVoxels->transform().rotate(glm::angleAxis(glm::pi<float>(), glm::vec3(0, 1, 0)));
break;
case TextOrientation::SPHERE_STRAIGHT:
m_buttonVoxels->transform().setPosition(lowerRight());
m_buttonVoxels->transform().setOrientation(m_hudget->worldOrientation(TextFieldHudgetVoxels::m_direction));
m_buttonVoxels->transform().rotate(glm::angleAxis(glm::pi<float>(), glm::vec3(0, 1, 0)));
break;
}
VoxelRenderer::instance()->draw(*m_buttonVoxels);
}
TextFieldHudgetVoxels::draw();
}

void ButtonHudgetVoxels::setText(const std::string& text) {
if (m_text.compare(text) == 0) {
return;
}
TextFieldHudgetVoxels::setText(text);
updateBounds();
}

ButtonStyle ButtonHudgetVoxels::buttonStyle() {
return m_buttonStyle;
}

void ButtonHudgetVoxels::setButtonStyle(ButtonStyle buttonStyle) {
m_buttonStyle = buttonStyle;
}
Loading