Skip to content

Commit

Permalink
[GUI] Support colored texts (#7036)
Browse files Browse the repository at this point in the history
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()
```
<img width="767" alt="Screenshot 2023-01-03 at 20 07 11"
src="https://user-images.githubusercontent.com/16066115/210433302-e58fbc41-5331-4904-ae10-65f60e0b3446.png">

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
AmesingFlank and pre-commit-ci[bot] authored Jan 4, 2023
1 parent c012eb3 commit ec79dc1
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 23 deletions.
7 changes: 5 additions & 2 deletions python/taichi/ui/imgui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions taichi/python/export_ggui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)
Expand Down
28 changes: 21 additions & 7 deletions taichi/ui/backends/vulkan/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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) {
Expand All @@ -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;
}
Expand Down
15 changes: 8 additions & 7 deletions taichi/ui/backends/vulkan/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
16 changes: 9 additions & 7 deletions taichi/ui/common/gui_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down

0 comments on commit ec79dc1

Please sign in to comment.