-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalign-nodes.py
More file actions
185 lines (160 loc) · 6.17 KB
/
align-nodes.py
File metadata and controls
185 lines (160 loc) · 6.17 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from tkinter import *
from textwrap import fill
# from krita import *
#-------------------------------------------------------------------
class dimensions():
def __init__(self, width, height):
self.width = width
self.height = height
#-------------------------------------------------------------------
class point():
def __init__(self, a, b):
self.x = a
self.y = b
def translateXY(self, other):
return point(self.x + other.x, self.y + other.y)
def translateX(self, other):
return point(self.x + other.x, self.y)
def translateY(self, other):
return point(self.x, self.y + other.x)
#-------------------------------------------------------------------
class alignmentPoints():
def __init__(self, origin, dimensions):
self.resetOrigin = origin
self.center = point(dimensions.width/2+origin.x, dimensions.height/2+origin.y)
self.topLeft = origin
self.topRight = point(origin.x+dimensions.width, origin.y)
self.topCenter = point(self.center.x, origin.y)
self.bottomLeft = point(origin.x, origin.y+dimensions.height)
self.bottomRight = point(origin.x+dimensions.width, origin.y+dimensions.height)
self.bottomCenter = point(self.center.x, origin.y+dimensions.height)
self.top = self.topLeft.y
self.bottom = self.bottomLeft.y
self.left = self.topLeft.x
self.right = self.topRight.x
#-------------------------------------------------------------------
class knode():
def __init__(self, name, origin, dimensions, color):
self.name = name
self.dimensions = dimensions
self.alignmentPoints = alignmentPoints(origin, dimensions)
self.color = color
##-------------------------------------------------------------------
def distrubuteHorizontally():
print("need to implement")
def distrubuteVertically():
print("need to implement")
def alignTopsToTops():
for layer in layers:
if layer != anchorLayer:
layer.alignmentPoints.top = anchorLayer.alignmentPoints.top
def alignBottomsToTops():
for layer in layers:
if layer != anchorLayer:
layer.alignmentPoints.top = anchorLayer.alignmentPoints.top - layer.dimensions.height
def alignBottomsToBottoms():
for layer in layers:
if layer != anchorLayer:
layer.alignmentPoints.top = anchorLayer.alignmentPoints.bottom - layer.dimensions.height
def alignTopsToBottoms():
for layer in layers:
if layer != anchorLayer:
layer.alignmentPoints.top = anchorLayer.alignmentPoints.bottom
def alignLeftsToLefts():
for layer in layers:
if layer != anchorLayer:
layer.alignmentPoints.left = anchorLayer.alignmentPoints.left
def alignLeftsToRights():
for layer in layers:
if layer != anchorLayer:
layer.alignmentPoints.left = anchorLayer.alignmentPoints.left - layer.dimensions.width
def alignRightsToRights():
for layer in layers:
if layer != anchorLayer:
layer.alignmentPoints.left = anchorLayer.alignmentPoints.right - layer.dimensions.width
def alignRightsToLefts():
for layer in layers:
if layer != anchorLayer:
layer.alignmentPoints.left = anchorLayer.alignmentPoints.right
def alignCenters():
for layer in layers:
if layer != anchorLayer:
layer.alignmentPoints.top = anchorLayer.alignmentPoints.center.y - layer.dimensions.height/2
layer.alignmentPoints.left = anchorLayer.alignmentPoints.center.x - layer.dimensions.width/2
def alignVerticalCenters():
for layer in layers:
if layer != anchorLayer:
layer.alignmentPoints.top = anchorLayer.alignmentPoints.center.y - layer.dimensions.height/2
def alignHorizontalCenters():
for layer in layers:
if layer != anchorLayer:
layer.alignmentPoints.left = anchorLayer.alignmentPoints.center.x - layer.dimensions.width/2
def alignReset():
for layer in layers:
layer.alignmentPoints.top = layer.alignmentPoints.resetOrigin.y
layer.alignmentPoints.left = layer.alignmentPoints.resetOrigin.x
##-------------------------------------------------------------------
## y
## t
##
## d f vgc j h
##
## b
## n
##-------------------------------------------------------------------
def keypress(event):
if event.char == "t":
alignTopsToTops()
elif event.char == "y":
alignBottomsToTops()
elif event.char == "b":
alignBottomsToBottoms()
elif event.char == "n":
alignTopsToBottoms()
elif event.char == "f":
alignLeftsToLefts()
elif event.char == "d":
alignLeftsToRights()
elif event.char == "h":
alignRightsToRights()
elif event.char == "j":
alignRightsToLefts()
elif event.char == "g":
alignCenters()
elif event.char == "v":
alignVerticalCenters()
elif event.char == "c":
alignHorizontalCenters()
elif event.char == "r":
alignReset()
for layer in layers:
if layer != anchorLayer:
index = layers.index(layer)
myCanvas.moveto(layerShapes[index], layer.alignmentPoints.left, layer.alignmentPoints.top)
#-------------------------------------------------------------------
# doc = Krita.instance().activeDocument()
doc = knode("document", point(100,100), dimensions(800, 800), "white")
# node = doc.activeNode()
anchorLayer = knode("anchorLayer", point(250,250), dimensions(300, 300), "red")
layer = knode("layer", point(450,50), dimensions(200, 100), "green")
layer3 = knode("layer3", point(50,650), dimensions(400, 50), "blue")
layerShapes = []
layers = [anchorLayer, layer, layer3]
anchorLayer = layers[0]
# init tk
root = Tk()
windowPosition = f'+{doc.alignmentPoints.topLeft.x}+{doc.alignmentPoints.topLeft.y}'
root.geometry(windowPosition)
root.bind("<Key>", keypress)
# create canvas
myCanvas = Canvas(root, bg="white", height=doc.dimensions.height, width=doc.dimensions.width)
# draw arcs
for layer in layers:
print("%s center point: %s, %s" %(layer.name, layer.alignmentPoints.center.x, layer.alignmentPoints.center.y) )
layerShape = myCanvas.create_rectangle(layer.alignmentPoints.topLeft.x, layer.alignmentPoints.topLeft.y, layer.alignmentPoints.bottomRight.x, layer.alignmentPoints.bottomRight.y, fill=f'{layer.color}')
layerShapes.append(layerShape)
# add to window and show
myCanvas.pack()
root.mainloop()