-
Notifications
You must be signed in to change notification settings - Fork 43
/
arrowdesigner.py
executable file
·359 lines (303 loc) · 11.9 KB
/
arrowdesigner.py
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Draw arrows in GIMP, using the selection as a guide for where to draw.
#
# Copyright 2010,2011 by Akkana Peck, http://www.shallowsky.com/software/
# plus contributions from Robert Brizard.
# You may use and distribute this plug-in under the terms of the GPL.
from gimpfu import *
import gtk, pango
from gobject import timeout_add
# Direction "enums"
DIREC_N, DIREC_NE, DIREC_E, DIREC_SE, DIREC_S, DIREC_SW, DIREC_W, DIREC_NW \
= range(8)
def python_fu_arrow_from_selection(img, layer, arrowangle, arrowsize,
x1, y1, x2, y2, cyc) :
"""
Draw an arrow from (x1, y1) to (x2, y2) with the head at (x2, y2).
The size of the arrowhead (isocele triangle and arrowsize is the length of the isoside)
is controlled by 'arrowsize', and the angle of it by 'arrowangle'.
'cyc' is the wanted integer number of gradient length in the shaft.
"""
# Save the current selection:
savesel = pdb.gimp_selection_save(img)
pdb.gimp_selection_none(img)
aangle = arrowangle * math.pi / 180.
#
# Draw the line first.
# But don't go quite all the way to the end, because that
# would make a rounded tip where the arrow point should be.
#
strokes = [ x1, y1, x2, y2 ]
dy = y2 - y1
dx = x2 - x1
# length of arrowhead in the shaft direction
l_head = arrowsize * math.cos(aangle)
l_arrow = math.sqrt(dx*dx + dy*dy)
# ratio is length_head/length_arrow, if >= 1 no line
ratio = l_head / l_arrow * 0.5
if ratio < 1.0 :
# from similar triangles
strokes[2] -= int(round(ratio*dx))
strokes[3] -= int(round(ratio*dy))
# compute the length of the gradient cycle wanted
if cyc > 0: cycl_grad = int((l_arrow - l_head)/cyc)
elif cyc == 0: cycl_grad = 0
pdb.gimp_paintbrush(layer, 0, 4, strokes, 0, cycl_grad)
#
# Now make the arrowhead
#
theta = math.atan2(y2-y1, x2-x1)
points = [ x2, y2,
int(x2 - arrowsize * math.cos(theta - aangle)),
int(y2 - arrowsize * math.sin(theta - aangle)),
int(x2 - arrowsize * math.cos(theta + aangle)),
int(y2 - arrowsize * math.sin(theta + aangle)) ]
# Only draw the head if the 3 points aren't collinear.
# Otherwise, it can fill the whole arrow layer:
# e.g. try arrow size 1, arrow angle < 30.
if int(l_head) > 1 and points[2:4] != points[4:6] :
# Select the arrowhead shape
pdb.gimp_image_select_polygon(img, CHANNEL_OP_REPLACE, 6, points)
# Fill the arrowhead
pdb.gimp_edit_fill(layer, FILL_FOREGROUND)
# Restore the old selection
pdb.gimp_image_select_item(img, CHANNEL_OP_REPLACE, savesel)
def direc_to_coords(x1, y1, x2, y2, direction) :
if direction == DIREC_N :
return x1, y2, x1, y1
elif direction == DIREC_NE :
return x1, y2, x2, y1
elif direction == DIREC_E :
return x1, y1, x2, y1
elif direction == DIREC_SE :
return x1, y1, x2, y2
elif direction == DIREC_S :
return x1, y1, x1, y2
elif direction == DIREC_SW :
return x2, y1, x1, y2
elif direction == DIREC_W :
return x2, y1, x1, y1
elif direction == DIREC_NW :
return x2, y2, x1, y1
class ArrowWindow(gtk.Window):
def __init__ (self, img, *args):
self.img = img
self.x1, self.y1, self.x2, self.y2 = 0, 0, 0, 0
self.direction = DIREC_N
self.changed = False
self.arrowsize = 30
self.arrowangle = 25
self.num_grad = 0
# Make a new GIMP layer to draw on
self.layer = gimp.Layer(img, "arrow", img.width, img.height,
RGBA_IMAGE, 100, NORMAL_MODE)
img.add_layer(self.layer, 0)
# Create the dialog
win = gtk.Window.__init__(self, *args)
self.set_title("GIMP arrow tool")
# Mac may have a problem with the window disappearing below
# the image window. But on small screens, the window is big
# enough that it can block a lot of the image window.
# Ideally, it would be nice to make sure it's initially
# on top, but then let the user hide it later.
# Or make a checkbox for it in the dialog, but that would
# make the dialog even bigger.
#self.set_keep_above(True) # keep the window on top
# Obey the window manager quit signal:
self.connect("destroy", gtk.main_quit)
# Make the UI
self.set_border_width(10)
vbox = gtk.VBox(spacing=10, homogeneous=False)
self.add(vbox)
label = gtk.Label("Arrow designer by Akkana Peck\nMake a rectangular selection. Will use active colors, brush and gradient.")
# Change color of the label first line R. B.
attr = pango.AttrList()
fg_color = pango.AttrForeground(0, 0, 65535, 0, 30)
size = pango.AttrSize(17000, 0, 14)
bold = pango.AttrWeight(pango.WEIGHT_ULTRABOLD, 0, 14)
attr.insert(fg_color)
attr.insert(size)
attr.insert(bold)
label.set_attributes(attr)
vbox.add(label)
label.show()
table = gtk.Table(rows=3, columns=2, homogeneous=False)
table.set_col_spacings(10)
vbox.add(table)
# Arrow size and sharpness
label = gtk.Label("Arrowhead size (px)")
label.set_alignment(xalign=0.0, yalign=1.0)
table.attach(label, 0, 1, 0, 1, xoptions=gtk.FILL, yoptions=0)
label.show()
adj = gtk.Adjustment(self.arrowsize, 1, 400, 1)
adj.connect("value_changed", self.arrowsize_cb)
scale = gtk.HScale(adj)
scale.set_digits(0)
table.attach(scale, 1, 2, 0, 1)
scale.show()
label = gtk.Label("Arrowhead angle (°)")
label.set_alignment(xalign=0.0, yalign=1.0)
table.attach(label, 0, 1, 1, 2, xoptions=gtk.FILL, yoptions=0)
label.show()
adj = gtk.Adjustment(self.arrowangle, 1, 80, 1)
adj.connect("value_changed", self.arrowangle_cb)
scale = gtk.HScale(adj)
scale.set_digits(0)
table.attach(scale, 1, 2, 1, 2)
scale.show()
label = gtk.Label("Gradient repetitions")
label.set_alignment(xalign=0.0, yalign=1.0)
table.attach(label, 0, 1, 2, 3, xoptions=gtk.FILL, yoptions=0)
label.show()
adj = gtk.Adjustment(self.num_grad, 0, 50, 1)
adj.connect("value_changed", self.num_grad_cb)
scale = gtk.HScale(adj)
scale.set_digits(0)
table.attach(scale, 1, 2, 2, 3)
scale.show()
table.show()
# Selector for arrow direction
hbox = gtk.HBox(spacing=5)
btn = gtk.RadioButton(None, "N")
btn.set_active(True)
btn.connect("toggled", self.direction_cb, DIREC_N)
hbox.add(btn)
btn.show()
btn = gtk.RadioButton(btn, "NE")
btn.connect("toggled", self.direction_cb, DIREC_NE)
hbox.add(btn)
btn.show()
btn = gtk.RadioButton(btn, "E")
btn.connect("toggled", self.direction_cb, DIREC_E)
hbox.add(btn)
btn.show()
btn = gtk.RadioButton(btn, "SE")
btn.connect("toggled", self.direction_cb, DIREC_SE)
hbox.add(btn)
btn.show()
btn = gtk.RadioButton(btn, "S")
btn.connect("toggled", self.direction_cb, DIREC_S)
hbox.add(btn)
btn.show()
btn = gtk.RadioButton(btn, "SW")
btn.connect("toggled", self.direction_cb, DIREC_SW)
hbox.add(btn)
btn.show()
btn = gtk.RadioButton(btn, "W")
btn.connect("toggled", self.direction_cb, DIREC_W)
hbox.add(btn)
btn.show()
btn = gtk.RadioButton(btn, "NW")
btn.connect("toggled", self.direction_cb, DIREC_NW)
hbox.add(btn)
btn.show()
vbox.add(hbox)
hbox.show()
# Make the dialog buttons box
hbox = gtk.HBox(spacing=20)
btn = gtk.Button("Next arrow")
btn.connect("pressed", self.next_arrow)
hbox.add(btn)
btn.show()
btn = gtk.Button("Close")
btn.connect("clicked", self.close_window)
hbox.add(btn)
btn.show()
vbox.add(hbox)
hbox.show()
vbox.show()
self.show()
timeout_add(300, self.update, self)
return win
def close_window(self, widget) :
# Autocrop our new layer before closing.
# autocrop_layer crops the current layer using the layer
# passed in as its crop template -- not clear from the doc.
self.img.active_layer = self.layer
pdb.plug_in_autocrop_layer(self.img, self.layer)
gtk.main_quit()
def direction_cb(self, widget, data=None) :
self.direction = data
self.changed = True
def arrowsize_cb(self, val) :
self.arrowsize = val.value
self.changed = True
def arrowangle_cb(self, val) :
self.arrowangle = val.value
self.changed = True
def num_grad_cb(self, val) :
self.num_grad = val.value
self.changed = True
def arrow(self, x1, y1, x2, y2) :
python_fu_arrow_from_selection(self.img, self.layer,
self.arrowangle, self.arrowsize,
x1, y1, x2, y2, self.num_grad)
def update(self, *args):
exists, x1, y1, x2, y2 = pdb.gimp_selection_bounds(self.img)
timeout_add(500, self.update, self)
if not exists :
return # No selection, no arrow
if (self.x1, self.y1, self.x2, self.y2) == (x1, y1, x2, y2) \
and not self.changed :
return
self.x1, self.y1, self.x2, self.y2 = x1, y1, x2, y2
self.changed = False
# Clear the layer, erasing the old arrow
# pdb.gimp_edit_clear(self.layer)
# self.layer.fill(TRANSPARENT_FILL)
self.layer.fill(FILL_TRANSPARENT)
# Draw the new arrow.
# Order is from, to: arrowhead goes on second X, Y pair.
x1, y1, x2, y2 = direc_to_coords(x1, y1, x2, y2, self.direction)
self.arrow(x1, y1, x2, y2)
pdb.gimp_displays_flush()
def next_arrow(self, data=None):
# pdb.gimp_selection_none(self.img)
# Make a new GIMP layer to draw on
self.layer = gimp.Layer(self.img, "arrow", self.img.width, self.img.height,
RGBA_IMAGE, 100, NORMAL_MODE)
self.img.add_layer(self.layer, 0)
pdb.gimp_displays_flush()
def arrow_designer(image, layer):
image.undo_group_start()
r = ArrowWindow(image)
gtk.main()
# pdb.gimp_selection_none(image)
image.undo_group_end()
def arrow_from_selection(img, layer, angle, size, direction) :
exists, x1, y1, x2, y2 = pdb.gimp_selection_bounds(img)
if not exists :
return
x1, y1, x2, y2 = direc_to_coords(x1, y1, x2, y2, direction)
python_fu_arrow_from_selection(img, layer, angle, size, x1, y1, x2, y2, 0)
register(
"python_fu_arrow_interactive",
"Draw an arrow following the selection (interactive).",
# + "From: " + str(__file__),
"Draw an arrow following the current selection, updating as the selection changes",
"Akkana Peck", "Akkana Peck",
"2010,2011",
"<Image>/Filters/Render/Arrow designer...",
"*",
[
],
[],
arrow_designer)
register(
"python_fu_arrow_from_selection",
"Draw an arrow following the current selection",
"Draw an arrow following the current selection",
"Akkana Peck", "Akkana Peck",
"2010,2011",
"<Image>/Filters/Render/Arrow from selection...",
"*",
[
(PF_SLIDER, "angle", "Arrow angle", 30, (0, 200, 1)),
(PF_SLIDER, "size", "Arrow size", 25, (0, 80, 1)),
(PF_OPTION, "direction", "Direction", 2,
("N", "NE", "E", "SE", "S", "SW", "W", "NW")),
],
[],
arrow_from_selection)
main()