Skip to content

Commit

Permalink
text label to use wording
Browse files Browse the repository at this point in the history
  • Loading branch information
igagis committed Oct 4, 2024
1 parent 9ccf4c2 commit 03dccfa
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/ruis/util/localization.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ class wording
return !this->vocabulary;
}

std::string_view id() const
const std::string& id() const
{
ASSERT(!this->empty())
return this->iter->first;
}

std::u32string_view string() const
const std::u32string& string() const
{
ASSERT(!this->empty())
return this->iter->second;
Expand Down
29 changes: 25 additions & 4 deletions src/ruis/widget/base/text_string_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,19 @@ text_string_widget::text_string_widget(
) :
widget(std::move(context), {}, {}),
text_widget(this->context, std::move(text_widget_params)),
text(std::move(text))
text_string(std::move(text))
{
this->recompute_bounding_box();
}

text_string_widget::text_string_widget(
utki::shared_ref<ruis::context> context,
text_widget::parameters text_widget_params,
wording localized_text
) :
widget(std::move(context), {}, {}),
text_widget(this->context, std::move(text_widget_params)),
text_string(std::move(localized_text))
{
this->recompute_bounding_box();
}
Expand All @@ -45,7 +57,7 @@ text_string_widget::text_string_widget(const utki::shared_ref<ruis::context>& c,
}

if (p.value == "text") {
this->text = utki::to_utf32(get_property_value(p).string);
this->text_string = utki::to_utf32(get_property_value(p).string);
this->recompute_bounding_box();
}
}
Expand Down Expand Up @@ -77,12 +89,21 @@ void text_string_widget::on_text_change()

void text_string_widget::set_text(std::u32string text)
{
this->text = std::move(text);
this->text_string = std::move(text);
this->invalidate_layout();
this->on_text_change();
}

const std::u32string& text_string_widget::get_text_string() const noexcept
{
if (std::holds_alternative<std::u32string>(this->text_string)) {
return std::get<std::u32string>(this->text_string);
}
ASSERT(std::holds_alternative<wording>(this->text_string));
return std::get<wording>(this->text_string).string();
}

std::u32string text_string_widget::get_text() const
{
return this->text;
return this->get_text_string();
}
15 changes: 10 additions & 5 deletions src/ruis/widget/base/text_string_widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

#pragma once

#include "../../util/localization.hpp"

#include "text_widget.hpp"

namespace ruis {
Expand All @@ -29,7 +31,7 @@ class text_string_widget : public text_widget
{
mutable ruis::rect bb{};

std::u32string text;
std::variant<std::u32string, wording> text_string;

protected:
vector2 measure(const ruis::vector2& quotum) const noexcept override;
Expand All @@ -42,6 +44,12 @@ class text_string_widget : public text_widget
std::u32string text
);

text_string_widget(
utki::shared_ref<ruis::context> context,
text_widget::parameters text_widget_params,
wording localized_text
);

const ruis::rect& get_bounding_box() const
{
return this->bb;
Expand All @@ -56,10 +64,7 @@ class text_string_widget : public text_widget

std::u32string get_text() const override;

const std::u32string& get_text_string() const
{
return this->text;
}
const std::u32string& get_text_string() const noexcept;

void on_font_change() override
{
Expand Down
3 changes: 1 addition & 2 deletions src/ruis/widget/group/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,7 @@ utki::shared_ref<container> make_caption(utki::shared_ref<context> c)
.widget_params = {
.id = "ruis_title"s
}
},
{}
}
)
}
)
Expand Down
3 changes: 1 addition & 2 deletions src/ruis/widget/input/text_input_line.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ vector2 text_input_line::measure(const ruis::vector2& quotum) const noexcept
vector2 ret;

if (quotum.x() < 0) {
ret.x() =
this->get_bounding_box().d.x() + cursor_width * this->context.get().units.dots_per_fp();
ret.x() = this->get_bounding_box().d.x() + cursor_width * this->context.get().units.dots_per_fp();
} else {
ret.x() = quotum.x();
}
Expand Down
21 changes: 21 additions & 0 deletions src/ruis/widget/label/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,27 @@ text::text(
)
{}

text::text(
utki::shared_ref<ruis::context> context, //
all_parameters params,
wording localized_text
) :
widget(
std::move(context), //
std::move(params.layout_params),
std::move(params.widget_params)
),
text_string_widget(
this->context, //
std::move(params.text_params),
std::move(localized_text)
),
color_widget(
this->context, //
std::move(params.color_params)
)
{}

void text::render(const ruis::matrix4& matrix) const
{
ruis::matrix4 matr(matrix);
Expand Down
25 changes: 19 additions & 6 deletions src/ruis/widget/label/text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#pragma once

#include "../../res/font.hpp"
#include "../../util/localization.hpp"
#include "../base/text_string_widget.hpp"
#include "../widget.hpp"

Expand All @@ -37,8 +36,6 @@ class text :
public text_string_widget, //
public color_widget
{
wording localized_wording;

public:
text(const utki::shared_ref<ruis::context>& c, const tml::forest& desc);

Expand All @@ -58,7 +55,7 @@ class text :
text(
utki::shared_ref<ruis::context> context, //
all_parameters params,
wording localized_wording
wording localized_text
);

public:
Expand All @@ -77,12 +74,28 @@ namespace make {
inline utki::shared_ref<ruis::text> text(
utki::shared_ref<ruis::context> context,
text::all_parameters params,
std::u32string text
std::u32string text = {}
)
{
return utki::make_shared<ruis::text>(std::move(context), std::move(params), std::move(text));
return utki::make_shared<ruis::text>(
std::move(context), //
std::move(params),
std::move(text)
);
}

inline utki::shared_ref<ruis::text> text(
utki::shared_ref<ruis::context> context,
text::all_parameters params,
wording localized_text
)
{
return utki::make_shared<ruis::text>(
std::move(context), //
std::move(params),
std::move(localized_text)
);
}
} // namespace make

} // namespace ruis
3 changes: 3 additions & 0 deletions tests/app2/res/localization/en.tml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ vocabulary{
some_checkbox{
str{"some checkbox"}
}
language{
str{"Language:"}
}
}
3 changes: 3 additions & 0 deletions tests/app2/res/localization/fi.tml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ vocabulary{
some_checkbox{
str{"jokin valintaruutu"}
}
language{
str{"Kieli (Language):"}
}
}
3 changes: 3 additions & 0 deletions tests/app2/res/localization/ru.tml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ vocabulary{
some_checkbox{
str{"какая-то галочка"}
}
language{
str{"Язык (Language):"}
}
}
6 changes: 3 additions & 3 deletions tests/app2/src/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ utki::shared_ref<ruis::window> make_sliders_window(utki::shared_ref<ruis::contex
{
m::text(c,
{},
U"Hello world!"s
c.get().localization.get("hello_world"sv)
)
}
),
Expand Down Expand Up @@ -116,7 +116,7 @@ utki::shared_ref<ruis::window> make_sliders_window(utki::shared_ref<ruis::contex
),
m::text(c,
{},
U"some checkbox"s
c.get().localization.get("some_checkbox"sv)
)
}
)
Expand Down Expand Up @@ -273,7 +273,7 @@ utki::shared_ref<ruis::window> make_selection_box_window(utki::shared_ref<ruis::
.align = {ruis::align::front, ruis::align::center}
}
},
U"Language:"s
c.get().localization.get("language"sv)
),
m::selection_box(c,
{
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/src/localization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <ruis/util/localization.hpp>

using namespace std::string_literals;
using namespace std::string_view_literals;

namespace{
Expand Down Expand Up @@ -37,8 +38,8 @@ const tst::set set("localization", [](tst::suite& suite){
tst::check(!wording_1.empty(), SL);
tst::check(!wording_2.empty(), SL);

tst::check_eq(wording_1.id(), "str_1"sv, SL);
tst::check_eq(wording_2.id(), "str_2"sv, SL);
tst::check_eq(wording_1.id(), "str_1"s, SL);
tst::check_eq(wording_2.id(), "str_2"s, SL);
tst::check(wording_1.string() == U"hello"sv, SL);
tst::check(wording_2.string() == U"world!"sv, SL);
});
Expand Down

0 comments on commit 03dccfa

Please sign in to comment.