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 15 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, const 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::setCallback(const std::function<void(ClickType clickType)>& callback) {
m_callback = callback;
}

void ButtonHudget::setText(const std::string& content) {
m_buttonVoxels->setText(content);
}
35 changes: 35 additions & 0 deletions src/ui/hud/buttonhudget.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#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, 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 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);

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)
{
setText(content);
}

ButtonHudgetVoxels::~ButtonHudgetVoxels() = default;

void ButtonHudgetVoxels::updateBounds() {
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 {
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::setText(const std::string& text) {
if (m_text.compare(text) == 0) {
return;
}
TextFieldHudgetVoxels::setText(text);
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();

virtual void setText(const std::string& text);

void updateBounds();

virtual void draw() override;

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

11 changes: 11 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 <string>

#include <glm/glm.hpp>
Expand Down Expand Up @@ -45,6 +46,7 @@
#include "crosshair.h"
#include "aimhelperhudget.h"
#include "textfieldhudget.h"
#include "buttonhudget.h"



Expand All @@ -54,6 +56,9 @@ HUD::HUD(Player* player):
m_crossHair(new CrossHair(this)),
m_aimHelper(new AimHelperHudget(this)),
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_elements(new HUDElements(*this)),
m_target(nullptr),
m_drawHud("vfx.drawhud"),
Expand All @@ -63,6 +68,9 @@ HUD::HUD(Player* player):

m_elements->addHudget(m_aimHelper);
m_elements->addHudget(m_crossHair);
m_elements->addHudget(m_targetName);
m_elements->addHudget(m_speedLabel);
m_elements->addHudget(m_menuButton);
}

HUD::~HUD() = default;
Expand Down Expand Up @@ -258,4 +266,7 @@ void HUD::setView(const View* view) {
m_view = view;
}

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


15 changes: 8 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"

#include "ui/clicktype.h"

enum class ClickType {
None,
Selection
};
#include "utils/handle/handle.h"

class Player;
class Hudget;
Expand All @@ -30,6 +26,7 @@ class Viewer;
class WorldTreeScanner;
class CrossHair;
class TextFieldHudget;
class ButtonHudget;
class View;

class HUD {
Expand Down Expand Up @@ -87,6 +84,9 @@ class HUD {
float m_fovy;
float m_fovx;

TextFieldHudget* m_speedLabel;
TextFieldHudget* m_targetName;
ButtonHudget* m_menuButton;
CrossHair* m_crossHair;
AimHelperHudget* m_aimHelper;

Expand All @@ -98,5 +98,6 @@ class HUD {

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
28 changes: 18 additions & 10 deletions src/ui/hud/textfieldhudgetvoxels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@

#include "utils/geometryhelper.h"


TextFieldHudgetVoxels::TextFieldHudgetVoxels(TextFieldHudget* textFieldHudget, const glm::vec3& direction, float scale, const std::string& text, FontSize fontSize) :
m_textFieldHudget(textFieldHudget),
TextFieldHudgetVoxels::TextFieldHudgetVoxels(Hudget* textFieldHudget, const glm::vec3& direction, float scale, const std::string& text, FontSize fontSize) :
m_hudget(textFieldHudget),
m_voxelFont(VoxelFont::instance()),
m_text(text),
m_direction(direction),
Expand All @@ -32,7 +31,7 @@ void TextFieldHudgetVoxels::setText(const std::string& text) {
}

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

const glm::vec3 TextFieldHudgetVoxels::upperLeft() const {
Expand Down Expand Up @@ -68,17 +67,26 @@ float TextFieldHudgetVoxels::scale() {
}

const glm::vec3 TextFieldHudgetVoxels::offsetToCenter(bool upper, bool left) const {
float horizontalOffset = left ? static_cast<float>(m_text.length()) : 0;
float verticalOffset = upper ? m_height : -m_height;

return glm::vec3(m_offset + m_width * horizontalOffset - m_width / 2, verticalOffset, 0);
float horizontalOffset, verticalOffset;
if (left) {
horizontalOffset = 0;
} else {
horizontalOffset = (float)m_text.length();
}
if (upper) {
verticalOffset = m_height;
} else {
verticalOffset = -m_height;
}

return glm::vec3(m_offset + m_width*horizontalOffset - m_width / 2, verticalOffset, 0);
}

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

26 changes: 13 additions & 13 deletions src/ui/hud/textfieldhudgetvoxels.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,43 @@
#include "hudget.h"
#include "ui/voxelfontconstants.h"

class TextFieldHudget;
class Hudget;
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, const glm::vec3& direction, float scale = 0.5f, const std::string& text = "", FontSize fontSize = FontSize::SIZE5x7);
TextFieldHudgetVoxels(Hudget* textFieldHudget, const glm::vec3& direction, float scale = 0.5f, const std::string& text = "", FontSize fontSize = FontSize::SIZE5x7);

void setText(const std::string& text);
virtual void setText(const std::string& text);

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

virtual bool isAt(const Ray& ray) const;

float width();
float height();
float scale();


protected:
FontSize m_fontSize;
TextFieldHudget* m_textFieldHudget;
std::string m_text;
VoxelFont* m_voxelFont;

Hudget* m_hudget;

glm::vec3 m_direction;
glm::vec3 worldPosition() const;
glm::quat worldOrientation() const;

FontSize m_fontSize;
VoxelFont* m_voxelFont;

float m_width, m_height, m_scale;
float m_offset;


const glm::vec3 offsetToCenter(bool upper, bool left) const;

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

glm::vec3 worldPosition() const;
glm::quat worldOrientation() const;
};

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