Skip to content

Commit

Permalink
Generalize text_styled to allow font/color anywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
aardappel committed Nov 6, 2023
1 parent dd363f3 commit e6d3daa
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
48 changes: 37 additions & 11 deletions dev/src/imbind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ enum Nesting {
N_CHILD,
N_VGROUP,
N_TOOLTIP,
N_FONT,
N_COLOR,
N_DRAG_DROP_SOURCE,
N_DRAG_DROP_TARGET,
};
Expand Down Expand Up @@ -188,6 +190,12 @@ void NPop(VM &vm, Nesting n) {
case N_TOOLTIP:
ImGui::EndTooltip();
break;
case N_FONT:
ImGui::PopFont();
break;
case N_COLOR:
ImGui::PopStyleColor();
break;
case N_DRAG_DROP_SOURCE:
ImGui::EndDragDropSource();
break;
Expand Down Expand Up @@ -945,27 +953,45 @@ nfr("text", "label", "S", "",
return NilVal();
});

nfr("text_styled", "label,font_idx,color", "SIF}:4", "",
nfr("text_wrapped", "label", "S", "",
"",
[](StackPtr &, VM &vm, Value &text) {
IsInit(vm);
auto &s = *text.sval();
ImGui::TextWrapped("%s", s.strvnt().c_str());
return NilVal();
});

nfr("font_start", "font_idx", "I", "",
"(use im.font instead)",
[](StackPtr &sp, VM &vm) {
IsInit(vm);
auto c = PopVec<float4>(sp);
int i = std::max(0, std::min(ImGui::GetIO().Fonts->Fonts.size() - 1, Pop(sp).intval()));
ImGui::PushFont(ImGui::GetIO().Fonts->Fonts[i]);
NPush(N_FONT);
});

nfr("font_end", "", "", "",
"",
[](StackPtr &, VM &vm) {
IsInit(vm);
NPop(vm, N_FONT);
});

nfr("color_start", "color", "F}:4", "",
"(use im.tooltip_multi instead)",
[](StackPtr &sp, VM &vm) {
IsInit(vm);
auto c = PopVec<float4>(sp);
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(c.x, c.y, c.z, c.w));
auto &s = *Pop(sp).sval();
Text(s.strv());
ImGui::PopStyleColor();
ImGui::PopFont();
NPush(N_COLOR);
});

nfr("text_wrapped", "label", "S", "",
nfr("color_end", "", "", "",
"",
[](StackPtr &, VM &vm, Value &text) {
[](StackPtr &, VM &vm) {
IsInit(vm);
auto &s = *text.sval();
ImGui::TextWrapped("%s", s.strvnt().c_str());
return NilVal();
NPop(vm, N_COLOR);
});

nfr("tooltip", "label", "S", "",
Expand Down
16 changes: 16 additions & 0 deletions modules/imgui.lobster
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,16 @@ def width(width, body):
body()
width_end()

def font(font_idx, body):
font_start(font_idx)
body()
font_end()

def color(col, body):
color_start(col)
body()
color_end()

def child(title, size, body):
child_start(title, size, 0)
body()
Expand Down Expand Up @@ -319,3 +329,9 @@ def show_stack_traces():
treenode("{i}"):
text(st)
while stack_traces.length: stack_traces.pop()

// Convenience.

def text_font(font_idx, msg):
font(font_idx):
text(msg)

0 comments on commit e6d3daa

Please sign in to comment.