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 10 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
6 changes: 6 additions & 0 deletions src/ui/clicktype.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

enum class ClickType {
None,
Selection
};
38 changes: 38 additions & 0 deletions src/ui/hud/buttonhudget.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#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, std::function<void(ClickType clickType)> callback, float scale, std::string content, FontSize fontSize, bool bounds) :
Hudget(hud),
m_buttonVoxels(new ButtonHudgetVoxels(this, direction, scale, content, fontSize, bounds)),
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::registerCallback(std::function<void(ClickType clickType)> callback) {
Copy link

Choose a reason for hiding this comment

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

setCallback should suffice as name, I think

m_callback = callback;
}

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

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

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

class ButtonHudgetVoxels;
class TextFieldHudgetVoxels;

class ButtonHudget : public Hudget{
Copy link

Choose a reason for hiding this comment

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

Another space after Hudget, please

public:
ButtonHudget(HUD* hud, glm::vec3 direction, std::function<void(ClickType clickType)> callback, float scale = 0.5f, std::string content = "", FontSize fontSize = FontSize::SIZE5x7, bool bounds = true);
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 setContent(std::string content);

void registerCallback(std::function<void(ClickType clickType)> callback);
Copy link
Member

Choose a reason for hiding this comment

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

I think mrzzzrm already mentioned that these should be const references instead of values.


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

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

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


ButtonHudgetVoxels::ButtonHudgetVoxels(ButtonHudget* buttonHudget, glm::vec3 direction, float scale, std::string content, FontSize fontSize, bool bounds) :
TextFieldHudgetVoxels(buttonHudget, direction, scale, content, fontSize),
m_buttonVoxels(new VoxelCluster(scale)),
m_bounds(bounds),
m_hudget(buttonHudget)
Copy link

Choose a reason for hiding this comment

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

indent

{
setContent(content);
}

ButtonHudgetVoxels::~ButtonHudgetVoxels() = default;

void ButtonHudgetVoxels::updateBounds() {
m_buttonVoxels.reset(new VoxelCluster(m_scale));
int width = (int)(m_width/m_scale)*m_content.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 {
m_buttonVoxels->addVoxel(new Voxel(glm::ivec3(i, j, 1), 0x17012D));
}
}
}
}

void ButtonHudgetVoxels::draw() {
if (m_bounds) {
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)));
VoxelRenderer::instance()->draw(*m_buttonVoxels);
}
TextFieldHudgetVoxels::draw();
}

void ButtonHudgetVoxels::setContent(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.

constref ;)

if (m_content.compare(content) == 0) {
return;
}
TextFieldHudgetVoxels::setContent(content);
updateBounds();
}
28 changes: 28 additions & 0 deletions src/ui/hud/buttonhudgetvoxels.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <memory>
#include <string>

#include "textfieldhudgetvoxels.h"

Copy link

Choose a reason for hiding this comment

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

nl++

class VoxelFont;
class ButtonHudget;
class VoxelCluster;

class ButtonHudgetVoxels : public TextFieldHudgetVoxels {
public:
ButtonHudgetVoxels(ButtonHudget* textFieldHudget, glm::vec3 direction, float scale = 0.5f, std::string content = "", FontSize fontSize = FontSize::SIZE5x7, bool bounds = true);
~ButtonHudgetVoxels();

void setContent(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.

I think we should build up our ui in best ui-api-tradition (i.e. Qt). Therefor setText deems me to be the better name

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As it turns out you already made that change to the texthudgets (along some other changes) in mission scripting... It's not like there is much use arguing about that now


void updateBounds();

virtual void draw() override;

protected:
std::unique_ptr<VoxelCluster> m_buttonVoxels;
bool m_bounds;
Hudget* m_hudget;
};

8 changes: 8 additions & 0 deletions src/ui/hud/hud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <algorithm>
#include <cmath>
#include <functional>

#include <glm/glm.hpp>
#include <glm/gtx/quaternion.hpp>
Expand Down Expand Up @@ -35,6 +36,7 @@
#include "display/view.h"
#include "camera/camerahead.h"
#include "textfieldhudget.h"
#include "buttonhudget.h"
#include "physics/physics.h"


Expand All @@ -48,6 +50,7 @@ HUD::HUD(Player* player):
m_scanner(new WorldTreeScanner()),
m_targetName(new TextFieldHudget(this, glm::normalize(glm::vec3(0, -1.1f, -2)), 0.025f, "")),
m_speedLabel(new TextFieldHudget(this, glm::normalize(glm::vec3(1.5f, -1.1f, -2)), 0.020f, "")),
m_menuButton(new ButtonHudget(this, glm::normalize(glm::vec3(-1.5f, 1.1f, -2)), (std::function<void(ClickType clickType)>)std::bind(&HUD::openMenu, this, std::placeholders::_1), 0.01f, "MENU")),
m_target(nullptr),
m_drawHud("vfx.drawhud")
{
Expand All @@ -56,6 +59,7 @@ HUD::HUD(Player* player):
m_hudgets.push_back(m_aimHelper.get());
m_hudgets.push_back(m_targetName.get());
m_hudgets.push_back(m_speedLabel.get());
m_hudgets.push_back(m_menuButton.get());
}

HUD::~HUD() = default;
Expand Down Expand Up @@ -241,6 +245,10 @@ float HUD::fovx() const {
return m_fovx;
}

void HUD::openMenu(ClickType clicktype) {
glow::debug("Not yet implemented");
}
Copy link
Contributor

Choose a reason for hiding this comment

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

when should a callback with clicktype none be fired? Also, when I click I don't get debug output atm


void HUD::setViewer(Viewer& viewer) {
m_viewer = &viewer;
}
13 changes: 6 additions & 7 deletions src/ui/hud/hud.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@

#include "property/property.h"

#include "utils/handle/handle.h"

enum class ClickType {
None,
Selection
};
#include "ui/clicktype.h"

#include "utils/handle/handle.h"

class Player;
class Hudget;
Expand All @@ -27,6 +23,7 @@ class Viewer;
class WorldTreeScanner;
class CrossHair;
class TextFieldHudget;
class ButtonHudget;

class HUD {
public:
Expand Down Expand Up @@ -84,11 +81,13 @@ class HUD {
std::unique_ptr<WorldTreeScanner> m_scanner;
std::unique_ptr<TextFieldHudget> m_speedLabel;
std::unique_ptr<TextFieldHudget> m_targetName;
std::unique_ptr<ButtonHudget> m_menuButton;

std::list<Hudget*> m_hudgets;

std::map<WorldObject*, HUDObjectDelegate*> m_objectDelegates;
void updateScanner(float deltaSec);
void updateFov();
};

void openMenu(ClickType clicktype);
};
2 changes: 1 addition & 1 deletion src/ui/hud/hudget.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <glm/gtx/quaternion.hpp>

#include "geometry/ray.h"
#include "ui/clicktype.h"

enum class ClickType;
class HUD;


Expand Down
18 changes: 10 additions & 8 deletions src/ui/hud/textfieldhudgetvoxels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,28 @@

#include "utils/geometryhelper.h"

TextFieldHudgetVoxels::TextFieldHudgetVoxels(TextFieldHudget* textFieldHudget, glm::vec3 direction, float scale, std::string content, FontSize fontSize) :
m_textFieldHudget(textFieldHudget),
TextFieldHudgetVoxels::TextFieldHudgetVoxels(Hudget* textFieldHudget, glm::vec3 direction, float scale, std::string content, FontSize fontSize) :
m_hudget(textFieldHudget),
Copy link

Choose a reason for hiding this comment

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

indent

m_voxelFont(VoxelFont::instance()),
m_content(content),
m_direction(direction),
m_scale(scale),
m_fontSize(fontSize)
{
m_width = m_voxelFont->letterWidth(fontSize) * m_scale;
m_height = m_voxelFont->letterWidth(fontSize) * m_scale;
m_height = m_voxelFont->letterHeight(fontSize) * m_scale;
m_offset = -1.f * ((m_content.length() - 1) / 2.0f) * m_width;
}

TextFieldHudgetVoxels::~TextFieldHudgetVoxels() = default;

void TextFieldHudgetVoxels::setContent(std::string content) {
m_content = content;
m_offset = -1.f * ((m_content.length() - 1) / 2.0f) * m_width;
}

void TextFieldHudgetVoxels::draw() {
m_voxelFont->drawString(m_content, m_textFieldHudget->worldPosition(m_direction), m_textFieldHudget->worldOrientation(m_direction), m_fontSize, m_scale, FontAlign::CENTER);
m_voxelFont->drawString(m_content, worldPosition(), worldOrientation(), m_fontSize, m_scale, FontAlign::CENTER);
}

const glm::vec3 TextFieldHudgetVoxels::upperLeft() const {
Expand Down Expand Up @@ -67,9 +69,9 @@ float TextFieldHudgetVoxels::scale() {
const glm::vec3 TextFieldHudgetVoxels::offsetToCenter(bool upper, bool left) const {
float horizontalOffset, verticalOffset;
if (left) {
horizontalOffset = (float)m_content.length();
} else {
horizontalOffset = 0;
} else {
horizontalOffset = (float)m_content.length();
}
if (upper) {
verticalOffset = m_height;
Expand All @@ -81,9 +83,9 @@ const glm::vec3 TextFieldHudgetVoxels::offsetToCenter(bool upper, bool left) con
}

glm::vec3 TextFieldHudgetVoxels::worldPosition() const {
return m_textFieldHudget->worldPosition(m_direction);
return m_hudget->worldPosition(m_direction);
}

glm::quat TextFieldHudgetVoxels::worldOrientation() const {
return m_textFieldHudget->worldOrientation(m_direction);
return m_hudget->worldOrientation(m_direction);
}
19 changes: 10 additions & 9 deletions src/ui/hud/textfieldhudgetvoxels.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ class VoxelFont;

class TextFieldHudgetVoxels{
Copy link

Choose a reason for hiding this comment

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

whitespace before {

public:
TextFieldHudgetVoxels(TextFieldHudget* textFieldHudget, glm::vec3 direction, float scale = 0.5f, std::string content = "", FontSize fontSize = FontSize::SIZE5x7);
TextFieldHudgetVoxels(Hudget* textFieldHudget, glm::vec3 direction, float scale = 0.5f, std::string content = "", FontSize fontSize = FontSize::SIZE5x7);
~TextFieldHudgetVoxels();

void setContent(std::string content);
virtual void setContent(std::string content);

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

virtual void draw();

virtual bool isAt(const Ray& ray) const;

Expand All @@ -25,16 +26,16 @@ class TextFieldHudgetVoxels{
float scale();

protected:
const glm::vec3 upperLeft() const;
const glm::vec3 lowerLeft() const;
const glm::vec3 upperRight() const;
const glm::vec3 lowerRight() const;
virtual const glm::vec3 upperLeft() const;
virtual const glm::vec3 lowerLeft() const;
virtual const glm::vec3 upperRight() const;
virtual const glm::vec3 lowerRight() const;

Hudget* m_hudget;
glm::vec3 worldPosition() const;
glm::quat worldOrientation() const;

FontSize m_fontSize;
TextFieldHudget* m_textFieldHudget;
std::string m_content;
VoxelFont* m_voxelFont;
glm::vec3 m_direction;
Expand Down
4 changes: 2 additions & 2 deletions src/ui/voxelfont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ void VoxelFont::drawString(std::string text, glm::vec3 position, glm::quat orien
int VoxelFont::letterWidth(FontSize size) {
switch (size) {
case FontSize::SIZE3x5:
return 3;
case FontSize::SIZE5x7:
return 5;
case FontSize::SIZE5x7:
return 7;
default:
assert(false);
}
Expand Down
Loading