Skip to content

Commit

Permalink
refactor hud and text
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren committed Mar 17, 2024
1 parent b729b34 commit 1e91239
Show file tree
Hide file tree
Showing 15 changed files with 39 additions and 26 deletions.
6 changes: 3 additions & 3 deletions examples/demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ int main() {
auto planeMaterial = plane->material()->as<MeshBasicMaterial>();
scene->add(plane);

HUD hud(canvas);
HUD hud(canvas.size());
FontLoader fontLoader;
const auto font1 = fontLoader.defaultFont();
const auto font2 = *fontLoader.load("data/fonts/helvetiker_regular.typeface.json");
Expand All @@ -129,7 +129,7 @@ int main() {
hud.add(hudText1, HUD::Options());

TextGeometry::Options opts2(font2, 10, 1);
auto hudText2 = Text2D(opts1, "");
auto hudText2 = Text2D(opts2);
hudText2.setColor(Color::red);
hud.add(hudText2, HUD::Options()
.setNormalizedPosition({1, 1})
Expand All @@ -155,7 +155,7 @@ int main() {

box->rotation.y += 0.5f * dt;

hudText2.setText("Delta=" + std::to_string(dt), opts2);
hudText2.setText("Delta=" + std::to_string(dt));
hud.needsUpdate(hudText2);

renderer.clear();
Expand Down
2 changes: 1 addition & 1 deletion examples/geometries/heightmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ int main() {

FontLoader fontLoader;

HUD hud(canvas);
HUD hud(canvas.size());
TextGeometry::Options opts(fontLoader.defaultFont(), 40);
Text2D hudText(opts, "Loading terrain..");
hudText.material()->as<MaterialWithColor>()->color.setHex(Color::black);
Expand Down
2 changes: 1 addition & 1 deletion examples/objects/instancing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ int main() {
canvas.setIOCapture(&capture);
#endif

HUD hud(canvas);
HUD hud(canvas.size());
FontLoader fontLoader;
const auto font = *fontLoader.load("data/fonts/helvetiker_regular.typeface.json");

Expand Down
2 changes: 1 addition & 1 deletion examples/objects/lod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ int main() {
renderer.setSize(size);
});

HUD hud(canvas);
HUD hud(canvas.size());
FontLoader fontLoader;
const auto font = *fontLoader.load("data/fonts/gentilis_bold.typeface.json");

Expand Down
2 changes: 1 addition & 1 deletion examples/objects/sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ int main() {
auto helper = Mesh::create(SphereGeometry::create(0.1));
scene->add(helper);

HUD hud(canvas);
HUD hud(&canvas);
createHudSprites(hud);

canvas.onWindowResize([&](WindowSize size) {
Expand Down
2 changes: 1 addition & 1 deletion examples/projects/Crane3R/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ int main() {
scene->add(light1);
scene->add(light2);

HUD hud(canvas);
HUD hud(canvas.size());
FontLoader fontLoader;
const auto font = *fontLoader.load("data/fonts/helvetiker_regular.typeface.json");

Expand Down
2 changes: 1 addition & 1 deletion examples/projects/MotorControl/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ int main() {
auto motorVisuals = VisualisationObject();
scene.add(motorVisuals);

HUD hud(canvas);
HUD hud(canvas.size());

FontLoader fontLoader;
auto font = fontLoader.defaultFont();
Expand Down
2 changes: 1 addition & 1 deletion examples/projects/Snake/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int main() {
auto camera = OrthographicCamera::create(0, game.gridSize(), 0, game.gridSize());
camera->position.z = 1;

HUD hud(canvas);
HUD hud(canvas.size());
FontLoader fontLoader;
const auto font = fontLoader.defaultFont();

Expand Down
2 changes: 1 addition & 1 deletion examples/projects/Youbot/youbot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ int main() {
auto light2 = AmbientLight::create(0xffffff, 1.f);
scene->add(light2);

HUD hud(canvas);
HUD hud(canvas.size());
FontLoader fontLoader;
const auto font = *fontLoader.load("data/fonts/helvetiker_regular.typeface.json");

Expand Down
2 changes: 1 addition & 1 deletion examples/projects/Youbot/youbot_kine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ int main() {
auto targetHelper = AxesHelper::create(2);
targetHelper->visible = false;

HUD hud(canvas);
HUD hud(canvas.size());
FontLoader fontLoader;
const auto font = *fontLoader.load("data/fonts/helvetiker_regular.typeface.json");

Expand Down
3 changes: 2 additions & 1 deletion include/threepp/objects/HUD.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ namespace threepp {
HorizontalAlignment horizontalAlignment_;
};

explicit HUD(PeripheralsEventSource& eventSource);
explicit HUD(WindowSize size);
explicit HUD(PeripheralsEventSource* eventSource);

void apply(GLRenderer& renderer);

Expand Down
19 changes: 14 additions & 5 deletions include/threepp/objects/Text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ namespace threepp {
class Text2D: public Mesh {

public:
Text2D(const TextGeometry::Options& opts, const std::string& str, const std::shared_ptr<Material>& material = nullptr)
: Mesh(TextGeometry::create(str, opts), material ? material : SpriteMaterial::create()) {}
Text2D(const TextGeometry::Options& opts, const std::string& str = "", const std::shared_ptr<Material>& material = nullptr)
: Mesh(TextGeometry::create(str, opts), material ? material : SpriteMaterial::create()), options(opts) {}

void setColor(const Color& color) {

Expand All @@ -23,22 +23,31 @@ namespace threepp {
}
}

void setText(const std::string& str) {

auto geometry = TextGeometry::create(str, options);
setGeometry(geometry);
}

void setText(const std::string& str, const TextGeometry::Options& opts) {

auto geometry = TextGeometry::create(str, opts);
setGeometry(geometry);
}

static std::shared_ptr<Text2D> create(const TextGeometry::Options& opts, const std::string& str, const std::shared_ptr<Material>& material = nullptr) {
static std::shared_ptr<Text2D> create(const TextGeometry::Options& opts, const std::string& str = "", const std::shared_ptr<Material>& material = nullptr) {

return std::make_shared<Text2D>(opts, str, material);
}

private:
TextGeometry::Options options;
};

class Text3D: public Mesh {

public:
Text3D(const ExtrudeTextGeometry::Options& opts, const std::string& str, const std::shared_ptr<Material>& material = nullptr)
Text3D(const ExtrudeTextGeometry::Options& opts, const std::string& str = "", const std::shared_ptr<Material>& material = nullptr)
: Mesh(ExtrudeTextGeometry::create(str, opts), material ? material : MeshBasicMaterial::create()) {}

void setColor(const Color& color) {
Expand All @@ -54,7 +63,7 @@ namespace threepp {
setGeometry(geometry);
}

static std::shared_ptr<Text3D> create(const ExtrudeTextGeometry::Options& opts, const std::string& str, const std::shared_ptr<Material>& material = nullptr) {
static std::shared_ptr<Text3D> create(const ExtrudeTextGeometry::Options& opts, const std::string& str = "", const std::shared_ptr<Material>& material = nullptr) {

return std::make_shared<Text3D>(opts, str, material);
}
Expand Down
2 changes: 1 addition & 1 deletion include/threepp/scenes/Scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace threepp {

[[nodiscard]] bool isTexture() const;

[[nodiscard]] Color color() const;
[[nodiscard]] Color& color();

[[nodiscard]] std::shared_ptr<Texture> texture() const;

Expand Down
15 changes: 9 additions & 6 deletions src/threepp/objects/HUD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ void HUD::Options::updateElement(Object3D& o, WindowSize windowSize) {

struct HUD::Impl: Scene, MouseListener {

Impl(PeripheralsEventSource* eventSource)
Impl(PeripheralsEventSource* eventSource, const WindowSize& size)
: eventSource_(eventSource),
size_(eventSource->size()),
size_(size),
camera_(0, size_.width, size_.height, 0, 0.1, 10) {

eventSource->addMouseListener(*this);
if (eventSource) eventSource->addMouseListener(*this);

camera_.position.z = 1;
}
Expand Down Expand Up @@ -133,7 +133,7 @@ struct HUD::Impl: Scene, MouseListener {
}

~Impl() override {
eventSource_->removeMouseListener(*this);
if (eventSource_) eventSource_->removeMouseListener(*this);
}

private:
Expand All @@ -148,9 +148,12 @@ struct HUD::Impl: Scene, MouseListener {
std::unordered_map<Object3D*, Options> map_;
};

HUD::HUD(WindowSize size)
: pimpl_(std::make_unique<Impl>(nullptr, size)) {}

HUD::HUD(PeripheralsEventSource& eventSource)
: pimpl_(std::make_unique<Impl>(&eventSource)) {}

HUD::HUD(PeripheralsEventSource* eventSource)
: pimpl_(std::make_unique<Impl>(eventSource, eventSource->size())) {}

void HUD::apply(GLRenderer& renderer) {
pimpl_->apply(renderer);
Expand Down
2 changes: 1 addition & 1 deletion src/threepp/scenes/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ bool Background::isTexture() const {
return texture_ != nullptr;
}

Color Background::color() const {
Color& Background::color() {

return *color_;
}
Expand Down

0 comments on commit 1e91239

Please sign in to comment.