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

[GUI] Support text_colored in GGUI #3078

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
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
8 changes: 6 additions & 2 deletions examples/ggui_examples/mass_spring_game_ggui.py
Original file line number Diff line number Diff line change
@@ -26,6 +26,9 @@
rest_length = ti.field(dtype=ti.f32,
shape=(max_num_particles, max_num_particles))

# gray color
gray = (128, 128, 128, 255)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the other GGUI APIs use floats between 0 and 1 to represent color. The text_colored API should do the same for consistency.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a chance that IMGUI automatically guesses the color range used (see the existing color_edit_3 API) so perhaps nothing needs to be done here. But please investigate and confirm this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I will double check it.



@ti.kernel
def substep():
@@ -159,10 +162,11 @@ def main():
canvas.circles(x, per_vertex_color=per_vertex_color, radius=0.02)

window.GUI.begin("mass spring", 0.05, 0.05, 0.9, 0.2)
window.GUI.text(
window.GUI.text_colored(
gray,
"Left click: add mass point (with shift to fix); Right click: attract"
)
window.GUI.text("C: clear all; Space: pause")
window.GUI.text_colored(gray, "C: clear all; Space: pause")
spring_Y[None] = window.GUI.slider_float("Spring Young's modulus",
spring_Y[None], 100, 10000)
drag_damping[None] = window.GUI.slider_float("Drag damping",
12 changes: 12 additions & 0 deletions python/taichi/ui/gui.py
Original file line number Diff line number Diff line change
@@ -49,6 +49,9 @@ def end(self):

def text(self, text):
"""Declares a line of text.

Args:
text (str): a line of text to be shown.
"""
self.gui.text(text)

@@ -88,3 +91,12 @@ def button(self, text):
text (str): a line of text to be shown next to the button
"""
return self.gui.button(text)

def text_colored(self, color, text):
"""Declares a line of colored text.

Args:
color (Tuple[float]): the color of the text, this should be a tuple of floats in [0,1] that indicates RGB values.
text (str): a line of text to be shown.
"""
return self.gui.text_colored(color, text)
12 changes: 11 additions & 1 deletion taichi/python/export_ggui.cpp
Original file line number Diff line number Diff line change
@@ -29,6 +29,11 @@ glm::vec3 tuple_to_vec3(pybind11::tuple t) {
return glm::vec3(t[0].cast<float>(), t[1].cast<float>(), t[2].cast<float>());
}

glm::vec4 tuple_to_vec4(py::tuple t) {
return glm::vec4(t[0].cast<float>(), t[1].cast<float>(), t[2].cast<float>(),
t[3].cast<float>());
}

pybind11::tuple vec3_to_tuple(glm::vec3 v) {
return pybind11::make_tuple(v.x, v.y, v.z);
}
@@ -61,6 +66,10 @@ struct PyGui {
bool button(std::string name) {
return gui->button(name);
}
void text_colored(py::tuple color, std::string text) {
glm::vec4 glm_color = tuple_to_vec4(color);
gui->text_colored(glm_color, text);
}
};

struct PyCamera {
@@ -341,7 +350,8 @@ void export_ggui(py::module &m) {
.def("checkbox", &PyGui::checkbox)
.def("slider_float", &PyGui::slider_float)
.def("color_edit_3", &PyGui::color_edit_3)
.def("button", &PyGui::button);
.def("button", &PyGui::button)
.def("text_colored", &PyGui::text_colored);

py::class_<PyScene>(m, "PyScene")
.def(py::init<>())
7 changes: 7 additions & 0 deletions taichi/ui/backends/vulkan/gui.cpp
Original file line number Diff line number Diff line change
@@ -173,6 +173,13 @@ bool Gui::button(std::string text) {
}
return ImGui::Button(text.c_str());
}
void Gui::text_colored(glm::vec4 color, std::string text) {
if (!initialized()) {
return;
}
ImGui::TextColored(ImVec4(color[0], color[1], color[2], color[3]),
text.c_str());
}

void Gui::draw(taichi::lang::CommandList *cmd_list) {
// Rendering
2 changes: 2 additions & 0 deletions taichi/ui/backends/vulkan/gui.h
Original file line number Diff line number Diff line change
@@ -42,6 +42,8 @@ class Gui final : public GuiBase {
glm::vec3 old_value) override;
virtual bool button(std::string text) override;

virtual void text_colored(glm::vec4 color, std::string text) override;

void draw(taichi::lang::CommandList *cmd_list);

void prepare_for_next_frame();
1 change: 1 addition & 0 deletions taichi/ui/common/gui_base.h
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ class GuiBase {
float maximum) = 0;
virtual glm::vec3 color_edit_3(std::string name, glm::vec3 old_value) = 0;
virtual bool button(std::string text) = 0;
virtual void text_colored(glm::vec4 color, std::string text) = 0;
virtual ~GuiBase() = default;
};