Skip to content

Commit d1d4408

Browse files
committed
string-only tooltip
1 parent 43661fd commit d1d4408

File tree

5 files changed

+34
-5
lines changed

5 files changed

+34
-5
lines changed

sources/include/cage-engine/guiBuilder.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,21 @@ namespace cage
4646
BuilderItem update(Delegate<void(Entity *)> u);
4747

4848
BuilderItem tooltip(const GuiTooltipComponent &t);
49+
BuilderItem tooltip(const String &txt, uint64 delay = GuiTooltipComponent().delay)
50+
{
51+
(*this)->value<GuiTooltipComponent>() = GuiTooltipComponent{ .tooltip = privat::guiTooltipText(entity(), txt), .delay = delay };
52+
return *this;
53+
}
4954
template<StringLiteral Text>
5055
BuilderItem tooltip(uint64 delay = GuiTooltipComponent().delay)
5156
{
52-
(*this)->template value<GuiTooltipComponent>().tooltip = detail::guiTooltipText<Text>();
53-
(*this)->template value<GuiTooltipComponent>().delay = delay;
57+
(*this)->template value<GuiTooltipComponent>() = GuiTooltipComponent{ .tooltip = detail::guiTooltipText<Text>(), .delay = delay };
5458
return *this;
5559
}
5660
template<uint32 TextId>
5761
BuilderItem tooltip(uint64 delay = GuiTooltipComponent().delay)
5862
{
59-
(*this)->template value<GuiTooltipComponent>().tooltip = detail::guiTooltipText<TextId>();
60-
(*this)->template value<GuiTooltipComponent>().delay = delay;
63+
(*this)->template value<GuiTooltipComponent>() = GuiTooltipComponent{ .tooltip = detail::guiTooltipText<TextId>(), .delay = delay };
6164
return *this;
6265
}
6366

sources/include/cage-engine/guiComponents.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ namespace cage
298298
namespace privat
299299
{
300300
CAGE_ENGINE_API GuiTooltipComponent::TooltipCallback guiTooltipText(const GuiTextComponent *txt);
301+
CAGE_ENGINE_API GuiTooltipComponent::TooltipCallback guiTooltipText(Entity *e, const String &txt);
301302
}
302303

303304
namespace detail

sources/libengine/gui/gui.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace cage
2020
#define GCHL_GENERATE(T) entityMgr->defineComponent(CAGE_JOIN(Gui, CAGE_JOIN(T, Component))());
2121
CAGE_EVAL_SMALL(CAGE_EXPAND_ARGS(GCHL_GENERATE, GCHL_GUI_COMMON_COMPONENTS, GCHL_GUI_WIDGET_COMPONENTS, GCHL_GUI_LAYOUT_COMPONENTS));
2222
#undef GCHL_GENERATE
23+
entityMgr->defineComponent(GuiTooltipStringComponent());
2324

2425
skins.reserve(config.skinsCount);
2526
for (uint32 i = 0; i < config.skinsCount; i++)

sources/libengine/gui/private.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,15 @@ namespace cage
274274
bool removing = false;
275275
};
276276

277+
// this component is added to the root entity of shown tooltip to track its deletion
277278
struct GuiTooltipMarkerComponent
278-
{}; // this component is added to the root entity of shown tooltip to track its deletion
279+
{};
280+
281+
// this component is used to hold data for text-only tooltips
282+
struct GuiTooltipStringComponent
283+
{
284+
String data;
285+
};
279286

280287
class GuiImpl : public GuiManager
281288
{

sources/libengine/gui/tooltips.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,15 @@ namespace cage
267267
e->value<GuiLabelComponent>();
268268
e->value<GuiTextComponent>() = *txt;
269269
}
270+
271+
void guiTooltipStringImpl(const GuiTooltipConfig &cfg)
272+
{
273+
cfg.tooltip->value<GuiPanelComponent>();
274+
Entity *e = cfg.tooltip->manager()->createUnique();
275+
e->value<GuiParentComponent>().parent = cfg.tooltip->id();
276+
e->value<GuiLabelComponent>();
277+
e->value<GuiTextComponent>().value = cfg.invoker->value<GuiTooltipStringComponent>().data;
278+
}
270279
}
271280

272281
namespace privat
@@ -277,5 +286,13 @@ namespace cage
277286
tt.bind<const GuiTextComponent *, guiTooltipTextImpl>(txt);
278287
return tt;
279288
}
289+
290+
GuiTooltipComponent::TooltipCallback guiTooltipText(Entity *e, const String &txt)
291+
{
292+
e->value<GuiTooltipStringComponent>().data = txt;
293+
GuiTooltipComponent::TooltipCallback tt;
294+
tt.bind<guiTooltipStringImpl>();
295+
return tt;
296+
}
280297
}
281298
}

0 commit comments

Comments
 (0)