pthom / imgui_bundle

Your fast track to powerful GUIs: Dear ImGui Bundle, an extensive toolkit for Python and C++ with immediate mode efficiency.
https://pthom.github.io/imgui_bundle/
MIT License
703 stars 73 forks source link

multiple independent Color Editor 3 #236

Closed maxmbed closed 4 months ago

maxmbed commented 4 months ago

Hi,

When using multiple Color Edit 3 widget, it displays good in window but editing any color will apply the change to all other color editors at the same time. While I wanted to have independent color editors instead.

I am new to imgui_bundle so pretty sure I am missing some basic principle here. Below is the used code. Vastly copied from imgui_hello_world.py with few adaptations.

How one can have multiple self independent Color Editor 3 using imgui_bundle ?

from imgui_bundle import imgui, immapp
from imgui_bundle import ImVec4

def help_marker(desc: str) -> None:
    imgui.text_disabled("(?)")
    if imgui.begin_item_tooltip():
        imgui.push_text_wrap_pos(imgui.get_font_size() * 35.0)
        imgui.text_unformatted(desc)
        imgui.pop_text_wrap_pos()
        imgui.end_tooltip()

def widget_show_color_editor():
    static_list = [list, list]

    for i, static in enumerate(static_list):
        static = widget_show_color_editor
        #if imgui.tree_node("Color/Picker Widgets"):
        if not hasattr(static, "color"):
            static.color = ImVec4(114.0 / 255.0, 144.0 / 255.0, 154.0 / 255.0, 200.0 / 255.0)
        if not hasattr(static, "color3"):
            static.color3 = [114.0 / 255.0, 144.0 / 255.0, 154.0 / 255.0]
        if not hasattr(static, "alpha_preview"):
            static.alpha_preview = True
        if not hasattr(static, "alpha_half_preview"):
            static.alpha_half_preview = False
        if not hasattr(static, "drag_and_drop"):
            static.drag_and_drop = True
        if not hasattr(static, "options_menu"):
            static.options_menu = True
        if not hasattr(static, "hdr"):
            static.hdr = False

        static_list[i] = static

    imgui.ColorEditFlags_.hdr.value
    misc_flags = (imgui.ColorEditFlags_.hdr.value if static.hdr else 0) | (0 if static.drag_and_drop else imgui.ColorEditFlags_.no_drag_drop.value) | (
         imgui.ColorEditFlags_.alpha_preview_half.value if static.alpha_half_preview else (imgui.ColorEditFlags_.alpha_preview.value if static.alpha_preview else 0)) | (
                      0 if static.options_menu else imgui.ColorEditFlags_.no_options.value)

    imgui.separator_text("Inline color editor")
    imgui.text("Color widget:")
    imgui.same_line()
    help_marker(
        "Click on the color square to open a color picker.\n"
        "CTRL+click on individual component to input value.\n")

    for i, static in enumerate(static_list):
        name = "MyColor##{}".format(i)
        _, static.color3 = imgui.color_edit3(name, static.color3, misc_flags)

def gui():
    widget_show_color_editor()

if __name__ == "__main__":
    immapp.run(
        gui_function=gui,  # The Gui function to run
        window_title="Hello!",  # the window title
        window_size_auto=True,  # Auto size the application window given its widgets
        # Uncomment the next line to restore window position and size from previous run
        # window_restore_previous_geometry==True
    )
pthom commented 4 months ago

Hello, some comments and answer:

    for i, static in enumerate(static_list):
        #                      Note: "static" below emulates C/C++ static variables. 
        #                            In a production program, it would be better to pass an instance of a state class 
    for i, static in enumerate(static_list):
        #                    You are using a different id for each color, which is good
        #                    Note: this could be rewritten as:
        #                    name = f"MyColor##{i}"
        name = "MyColor##{}".format(i)

        #                    As with all imgui widget, imgui.color_edit3 returns two values:
        #                              (modified, new_value)
        #                    Here, you store the result of both widgets in the same static.color3 variable
        #                    ==> By construction this variable can only store one value
        _, static.color3 = imgui.color_edit3(name, static.color3, misc_flags)