~nch/glue

ref: 50333171e5b5973cdef9c6e8d045750a3a0936e6 glue/glue.py -rw-r--r-- 3.5 KiB
50333171 — nc WIP this seems(?) to be working 1 year, 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import tkinter as tk
import tkinter.dnd as dnd # type: ignore
from dataclasses import dataclass
from typing import Dict, Tuple
import table

def _cursor_pos(canvas, event):
    # where the corner of the canvas is relative to the screen:
    x_org = canvas.winfo_rootx()
    y_org = canvas.winfo_rooty()
    # XXX UTTER INSANITY -- x/y_root are relative to the *MONITOR* x/y mouse positions!
    #     That's why we're getting the offset here:
    return event.x_root - x_org, event.y_root - y_org

class DraggedNode:
    def __init__(self, orig_node):
        x1, y1, x2, y2 = orig_node.canvas.bbox(orig_node.id)
        self.orig_node = orig_node
        self.id = orig_node.canvas.create_rectangle(x1, y1, x2, y2)

    def move(self, x, y):
        x1, y1, _1, _2 = self.orig_node.canvas.bbox(self.id)
        self.orig_node.canvas.move(self.id, x-x1, y-y1)

    def dnd_end(self, target, event):
        self.orig_node.canvas.delete(self.id)
        self.orig_node.move(*_cursor_pos(self.orig_node.canvas, event))

class DraggedEdge:
    def __init__(self, start_node, x, y):
        self.ox, self.oy = x, y
        self.start_node = start_node
        self.id = start_node.canvas.create_line(x, y, x, y)

    def move(self, x, y):
        self.start_node.canvas.coords(self.id, self.ox, self.oy, x, y)

    def dnd_end(self, target, event):
        self.start_node.canvas.delete(self.id)
        if isinstance(target, Node): # FIXME
            self.start_node.add_child(target)
            print('add child', target)

class Node:
    def __init__(self, canvas, x=10, y=10):
        self.frame = tk.Frame(canvas, borderwidth=2, bg='#aaa', width=100, height=100, pady=20, padx=10)
        self.id = canvas.create_window(x, y, window=self.frame, anchor='nw')
        self.canvas = canvas

        # FIXME I really don't like that it's possible to add a child without adding a node if this array is appended to instead of calling add_child().
        #       Figure out a better way of enforcing this.
        self.children = []
        self.frame.bind('<Button-1>', lambda ev: dnd.dnd_start(DraggedNode(self), ev))
        self.frame.bind('<Button-3>', lambda ev: dnd.dnd_start(DraggedEdge(self, *_cursor_pos(self.canvas, ev)), ev))

    def add_child(self, child):
        self.children.append(child)
        # TODO self.canvas.create_line()

    def move(self, x, y):
        self.canvas.coords(self.id, x, y)

    def dnd_accept(self, source, event):
        return self

class NodeCanvas:
    def __init__(self, root):
        self.canvas = tk.Canvas(root, width=800, height=600, background='#444')
        self.canvas.pack(fill='both', expand=1)
        self.canvas.dnd_accept = self.dnd_accept

    def dnd_accept(self, source, event):
        pass
        #return self

    def dnd_motion(self, source, event):
        x, y = _cursor_pos(self.canvas, event)
        source.move(x, y)

    ### unused ###
    def dnd_enter(self, source, event):
        pass

    ### unused ###
    def dnd_leave(self, source, event):
        pass

    ### unused ###
    def dnd_commit(self, source, event):
        pass

import numpy as np # type: ignore
# TODO: make initialization notation easier...
nodes = table.Table({'id': np.array([], dtype='int'), 'gui_handle': np.array([], dtype='object')})
edges = table.Table({'n1_id': np.array([], dtype='int'), 'n2_id': np.array([], dtype='int')})

root = tk.Tk()
root.geometry("+1+1")
canvas = NodeCanvas(root)
n = Node(canvas.canvas).frame
tk.Label(n, text='test').pack()
n2 = Node(canvas.canvas).frame
tk.Label(n2, text='test2').pack()
root.mainloop()