DearPyGui icon indicating copy to clipboard operation
DearPyGui copied to clipboard

Align method for node attributes

Open ghost opened this issue 2 years ago • 1 comments

The group method works fantastically for aligning items horizontally, but I cannot align node attributes. Additionally, the attribute's position is relative to the node editor, not the node itself. Is it possible to implement some group method for node attributes?

ghost avatar Jun 25 '23 01:06 ghost

Need write your own align method maybe. Here is an example of align left,right,bottom for selected node:

def save_update_node_position(node_tag_pos_list=""):
    global current_nde_tag,current_dict_name,current_pj_in_dict_key
    dict_one_pj = open_pj_mange_json(json_path=current_dict_name)
    print (current_dict_name,current_pj_in_dict_key,"===")
    print(node_tag_pos_list,"sss")
    for one_node_pair in node_tag_pos_list:
        one_node_tag=one_node_pair[0]
        one_new_pos=one_node_pair[1]
        node_index=int(dpg.get_item_label(one_node_tag).split(":")[1])
        dict_one_pj[current_pj_in_dict_key]["all_node_save_as_a_list_of_dict"][node_index]["pos_of_node"] = one_new_pos
    save_new_dict_as_json(new_dict=dict_one_pj, json_path=current_dict_name)
    return
def align_selected_node(sender,appdata,userdata):
    global current_nde_tag
    label_align=dpg.get_item_label(sender)
    node_info = {}
    dpg.hide_item(popup_tag_nde)
    selected_nodes = dpg.get_selected_nodes(current_nde_tag)
    nodes_to_update = []
    if len(selected_nodes)>=2:
        for node_tag in selected_nodes:
            pos = dpg.get_item_pos(node_tag)
            size = dpg.get_item_rect_size(node_tag)
            node_info[node_tag] = {
                "pos": pos,
                "size": size,
                "left": pos[0],
                "right": pos[0] + size[0],
                "top": pos[1],
                "bottom": pos[1] + size[1]
            }
        if label_align==l_align:
            reference = min(node_info.values(), key=lambda x: x["left"])
            reference_value = reference["left"]
            for node_tag, info in node_info.items():
                new_x = reference_value
                new_pos=[new_x, info["pos"][1]]
                dpg.set_item_pos(node_tag, new_pos)
                nodes_to_update.append([node_tag,new_pos])
        elif label_align==r_align:
            reference = max(node_info.values(), key=lambda x: x["right"])
            reference_value = reference["right"]
            for node_tag, info in node_info.items():
                new_x = reference_value - info["size"][0]
                new_pos=[new_x, info["pos"][1]]
                dpg.set_item_pos(node_tag, new_pos)
                nodes_to_update.append([node_tag,new_pos])
        elif label_align==u_align:
            reference = min(node_info.values(), key=lambda x: x["top"])
            reference_value = reference["top"]
            for node_tag, info in node_info.items():
                new_y = reference_value
                new_pos=[info["pos"][0], new_y]
                dpg.set_item_pos(node_tag, new_pos)
                nodes_to_update.append([node_tag,new_pos])
        elif label_align==b_align:
            reference = max(node_info.values(), key=lambda x: x["bottom"])
            reference_value = reference["bottom"]
            for node_tag, info in node_info.items():
                new_y = reference_value - info["size"][1]
                new_pos=[info["pos"][0], new_y]
                dpg.set_item_pos(node_tag, new_pos)
                nodes_to_update.append([node_tag,new_pos])
        save_update_node_position(node_tag_pos_list=nodes_to_update)
    return
def popup_for_nde(popup_tag=""):
    global current_dict_name,current_pj_in_dict_key,current_pj_pbr_tag
    # window_popup=dpg.generate_uuid()
    with dpg.window(tag=popup_tag,width=resolution_x*0.22,height=resolution_y*0.14,no_move=True, no_close=True, no_resize=True, show=False,no_collapse=True, label="Select Item"):   
        dpg.add_button(label="add_one_node", width=resolution_x*0.2,callback=add_node_one_part_of_pj)
        dpg.add_button(label=l_align, width=resolution_x*0.2,callback=align_selected_node)
        dpg.add_button(label=r_align, width=resolution_x*0.2,callback=align_selected_node)
        dpg.add_button(label=u_align, width=resolution_x*0.2,callback=align_selected_node)
        dpg.add_button(label=b_align, width=resolution_x*0.2,callback=align_selected_node)

utmcontent avatar Apr 02 '25 07:04 utmcontent