From ec79dc1b9aebfed3fae40dd93c6102b8342bd449 Mon Sep 17 00:00:00 2001 From: Dunfan Lu Date: Wed, 4 Jan 2023 03:35:52 +0000 Subject: [PATCH] [GUI] Support colored texts (#7036) Issue: #3013 ### Brief Summary Implementing colored texts: ```py window = ti.ui.Window('Window', (768, 768), vsync=True) canvas = window.get_canvas() gui = window.get_gui() while window.running: with gui.sub_window("Yo", 0.05, 0.05, 0.9, 0.2) as w: w.text("haha", (1.0,0.2,0.3)) window.show() ``` Screenshot 2023-01-03 at 20 07 11 Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- python/taichi/ui/imgui.py | 7 +++++-- taichi/python/export_ggui.cpp | 4 ++++ taichi/ui/backends/vulkan/gui.cpp | 28 +++++++++++++++++++++------- taichi/ui/backends/vulkan/gui.h | 15 ++++++++------- taichi/ui/common/gui_base.h | 16 +++++++++------- 5 files changed, 47 insertions(+), 23 deletions(-) diff --git a/python/taichi/ui/imgui.py b/python/taichi/ui/imgui.py index 0c67cf0d067c1..4200e974a397c 100644 --- a/python/taichi/ui/imgui.py +++ b/python/taichi/ui/imgui.py @@ -58,10 +58,13 @@ def end(self): """ self.gui.end() - def text(self, text): + def text(self, text, color=None): """Declares a line of text. """ - self.gui.text(text) + if color is None: + self.gui.text(text) + else: + self.gui.text_colored(text, color) def checkbox(self, text, old_value): """Declares a checkbox, and returns whether or not it has been checked. diff --git a/taichi/python/export_ggui.cpp b/taichi/python/export_ggui.cpp index 31ce2b284ade3..ba3750ec655f6 100644 --- a/taichi/python/export_ggui.cpp +++ b/taichi/python/export_ggui.cpp @@ -63,6 +63,9 @@ struct PyGui { void text(std::string text) { gui->text(text); } + void text_colored(std::string text, py::tuple color) { + gui->text(text, tuple_to_vec3(color)); + } bool checkbox(std::string name, bool old_value) { return gui->checkbox(name, old_value); } @@ -521,6 +524,7 @@ void export_ggui(py::module &m) { .def("begin", &PyGui::begin) .def("end", &PyGui::end) .def("text", &PyGui::text) + .def("text_colored", &PyGui::text_colored) .def("checkbox", &PyGui::checkbox) .def("slider_int", &PyGui::slider_int) .def("slider_float", &PyGui::slider_float) diff --git a/taichi/ui/backends/vulkan/gui.cpp b/taichi/ui/backends/vulkan/gui.cpp index 9187f93787d85..b71bc690c7892 100644 --- a/taichi/ui/backends/vulkan/gui.cpp +++ b/taichi/ui/backends/vulkan/gui.cpp @@ -131,7 +131,11 @@ float Gui::abs_y(float y) { return y * app_context_->config.height; } -void Gui::begin(std::string name, float x, float y, float width, float height) { +void Gui::begin(const std::string &name, + float x, + float y, + float width, + float height) { if (!initialized()) { return; } @@ -146,27 +150,37 @@ void Gui::end() { } ImGui::End(); } -void Gui::text(std::string text) { +void Gui::text(const std::string &text) { if (!initialized()) { return; } ImGui::Text("%s", text.c_str()); } -bool Gui::checkbox(std::string name, bool old_value) { +void Gui::text(const std::string &text, glm::vec3 color) { + if (!initialized()) { + return; + } + ImGui::TextColored(ImVec4(color[0], color[1], color[2], 1.0f), "%s", + text.c_str()); +} +bool Gui::checkbox(const std::string &name, bool old_value) { if (!initialized()) { return old_value; } ImGui::Checkbox(name.c_str(), &old_value); return old_value; } -int Gui::slider_int(std::string name, int old_value, int minimum, int maximum) { +int Gui::slider_int(const std::string &name, + int old_value, + int minimum, + int maximum) { if (!initialized()) { return old_value; } ImGui::SliderInt(name.c_str(), &old_value, minimum, maximum); return old_value; } -float Gui::slider_float(std::string name, +float Gui::slider_float(const std::string &name, float old_value, float minimum, float maximum) { @@ -176,14 +190,14 @@ float Gui::slider_float(std::string name, ImGui::SliderFloat(name.c_str(), &old_value, minimum, maximum); return old_value; } -glm::vec3 Gui::color_edit_3(std::string name, glm::vec3 old_value) { +glm::vec3 Gui::color_edit_3(const std::string &name, glm::vec3 old_value) { if (!initialized()) { return old_value; } ImGui::ColorEdit3(name.c_str(), (float *)&old_value); return old_value; } -bool Gui::button(std::string text) { +bool Gui::button(const std::string &text) { if (!initialized()) { return false; } diff --git a/taichi/ui/backends/vulkan/gui.h b/taichi/ui/backends/vulkan/gui.h index 68ebb79ba0912..b9782cdb4fde7 100644 --- a/taichi/ui/backends/vulkan/gui.h +++ b/taichi/ui/backends/vulkan/gui.h @@ -30,25 +30,26 @@ class TI_DLL_EXPORT Gui final : public GuiBase { void init_render_resources(VkRenderPass render_pass); void cleanup_render_resources(); - void begin(std::string name, + void begin(const std::string &name, float x, float y, float width, float height) override; void end() override; - void text(std::string text) override; - bool checkbox(std::string name, bool old_value) override; - int slider_int(std::string name, + void text(const std::string &text) override; + void text(const std::string &text, glm::vec3 color) override; + bool checkbox(const std::string &name, bool old_value) override; + int slider_int(const std::string &name, int old_value, int minimum, int maximum) override; - float slider_float(std::string name, + float slider_float(const std::string &name, float old_value, float minimum, float maximum) override; // TODO: consider renaming this? - glm::vec3 color_edit_3(std::string name, glm::vec3 old_value) override; - bool button(std::string text) override; + glm::vec3 color_edit_3(const std::string &name, glm::vec3 old_value) override; + bool button(const std::string &text) override; void draw(taichi::lang::CommandList *cmd_list); diff --git a/taichi/ui/common/gui_base.h b/taichi/ui/common/gui_base.h index 127621789c366..7ba94cd5ad4d8 100644 --- a/taichi/ui/common/gui_base.h +++ b/taichi/ui/common/gui_base.h @@ -6,24 +6,26 @@ namespace taichi::ui { class GuiBase { public: - virtual void begin(std::string name, + virtual void begin(const std::string &name, float x, float y, float width, float height) = 0; virtual void end() = 0; - virtual void text(std::string text) = 0; - virtual bool checkbox(std::string name, bool old_value) = 0; - virtual int slider_int(std::string name, + virtual void text(const std::string &text) = 0; + virtual void text(const std::string &text, glm::vec3 color) = 0; + virtual bool checkbox(const std::string &name, bool old_value) = 0; + virtual int slider_int(const std::string &name, int old_value, int minimum, int maximum) = 0; - virtual float slider_float(std::string name, + virtual float slider_float(const std::string &name, float old_value, float minimum, 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 glm::vec3 color_edit_3(const std::string &name, + glm::vec3 old_value) = 0; + virtual bool button(const std::string &text) = 0; virtual ~GuiBase() = default; };