1 ----------------------------------------------------------------------
3 ----------------------------------------------------------------------
6 about = [[ Some features making it easier to work with graphs. ]]
8 local deactivateGraphMode = false
9 local moveInvisibleObjects = false
11 function toggleGraphMode ()
12 if deactivateGraphMode then
13 deactivateGraphMode = false
15 deactivateGraphMode = true
19 function toggleMoveInvisible ()
20 if moveInvisibleObjects then
21 moveInvisibleObjects = false
23 moveInvisibleObjects = true
28 local currMarkId = nil
30 --------------------------------------------------------------------------------
31 -- add an edit action for marks ------------------------------------------------
33 -- saving the old function
34 function _G.MODEL:graph_backup_actinon_edit () end
35 _G.MODEL.graph_backup_action_edit = _G.MODEL.action_edit
37 -- adding support for objects of type reference
38 function _G.MODEL:action_edit()
39 if deactivateGraphMode then
40 self:graph_backup_action_edit()
44 local prim = p:primarySelection()
46 self:graph_backup_action_edit()
50 if obj:type() == "reference" then
51 action_edit_reference (self, prim, obj)
53 self:graph_backup_action_edit()
58 -- starting to edit a mark
59 function action_edit_reference(model, prim, obj)
62 local p = model:page()
64 local pos = obj:matrix() * obj:position()
67 -- creating a circle at the position of the mark
68 local ellipse = {type="ellipse"}
69 ellipse[1] = ipe.Matrix({10, 0, 0, 10, pos.x, pos.y})
71 local circ = ipe.Path(model.attributes, {ellipse})
72 p:insert(nil, circ, 0, p:layerOf(prim))
74 -- edit the circle instead of the mark itself
75 model:action_edit_path(#p, circ)
80 --------------------------------------------------------------------------------
81 -- pressing a key while editing the cycle --------------------------------------
83 -- saving old function
84 function _G.EDITTOOL:graph_backup_key(code, modifiers, text) end
85 _G.EDITTOOL.graph_backup_key = _G.EDITTOOL.key
88 function _G.EDITTOOL:key(code, modifiers, text)
89 self:graph_backup_key(code, modifiers, text)
90 if deactivateGraphMode then return end
92 -- react if and only if we are currently editing a mark and key ESC
93 -- or SPACE is pressed
94 if text ~= "\027" and code ~= 0x20 then return end
95 if not editing then return end
99 -- finding new and old position
100 local p = self.model:page()
102 local mark = p[currMarkId]
103 local oldPos = mark:matrix() * mark:position()
104 local newPos = circ:shape()[1][1]:translation()
106 -- remove the intermediate step of moving the cycle from the undo
107 -- stack and remove the cycle itself
108 local undoSt = self.model.undo
112 -- new action for the undo stack moving the mark and all endpoints
113 -- ending at the mark
114 local t = { label = "edit reference",
115 pno = self.model.pno,
116 vno = self.model.vno,
117 selection = self.model:selection(),
118 original = self.model:page():clone(),
120 undo = _G.revertOriginal,}
121 t.redo = function (t, doc)
122 p:transform(currMarkId, ipe.Translation(newPos-oldPos))
123 moveEndpoints(oldPos, newPos, p, self.model)
125 self.model:register(t)
128 -- function moving all endpoints and intermediate points in polylines
129 -- to newPos, if the squared distance to oldPos is at most sqEps
131 function moveEndpoints(oldPos, newPos, p, model)
133 for i, obj, sel, layer in p:objects() do
134 -- do nothing if the object is invisible and invisible objects
135 -- should not be moved
136 if not p:visible(model.vno, layer) and
137 not moveInvisibleObjects then
140 -- do nothing if it is not a path
141 if obj:type() ~= "path" then
144 local shape = obj:shape()
145 for _, subPath in ipairs(shape) do
146 if (subPath["type"] == "curve") then
147 for _,seg in ipairs(subPath) do
148 if (seg["type"] == "segment") then
149 for j, point in ipairs(seg) do
150 -- print(j, point, oldPos)
151 if (obj:matrix() * point - oldPos):sqLen() < sqEps then
152 seg[j] = obj:matrix():inverse() * newPos
153 -- print("test", seg[j])
156 elseif (seg["type"] == "spline") then
157 if (obj:matrix() * seg[1] - oldPos):sqLen() < sqEps then
158 seg[1] = obj:matrix():inverse() * newPos
160 if (obj:matrix() * seg[#seg] - oldPos):sqLen() < sqEps then
161 seg[#seg] = obj:matrix():inverse() * newPos
173 --------------------------------------------------------------------------------
174 -- working with groups ---------------------------------------------------------
176 local function regroup(elem)
178 for i, obj in ipairs(elem) do
179 if obj[1] ~= nil then
180 groupElem[#groupElem + 1] = regroup(obj)
182 groupElem[#groupElem + 1] = obj
185 return ipe.Group(groupElem)
188 local function ungroup(group)
189 local elem = group:elements()
191 for i, obj in ipairs(elem) do
192 if (obj:type() == "group") then
193 local subElem, subPlainElem = ungroup(obj)
195 for _, subObj in ipairs(subPlainElem) do
196 table.insert(plainElem, subObj)
199 table.insert(plainElem, obj)
202 return elem, plainElem
205 --------------------------------------------------------------------------------
206 -- shorten paths ---------------------------------------------------------------
208 function shortenObj(obj, lenSource, lenTarget)
209 if obj:type() == "path" then
210 local shape = obj:shape()
211 for _, subPath in ipairs(shape) do
212 local first = subPath[1]
213 local last = subPath[#subPath]
215 local p1 = obj:matrix() * first[1]
216 local p2 = obj:matrix() * first[2]
217 local pDelta = p2 - p1
218 local pNorm = pDelta:normalized()
219 local newP1 = p1 + pNorm*lenSource
221 local q1 = obj:matrix() * last[#last]
222 local q2 = obj:matrix() * last[#last - 1]
223 local qDelta = q2 - q1
224 local qNorm = qDelta:normalized()
225 local newQ1 = q1 + qNorm*lenTarget
227 first[1] = obj:matrix():inverse() * newP1
228 last[#last] = obj:matrix():inverse() * newQ1
234 function getString(model, string)
235 if ipeui.getString ~= nil then
236 return ipeui.getString(model.ui, "Enter length")
238 return model:getString("Enter length")
242 function shorten(model, num)
246 -- local str = ipeui.getString(model.ui, "Enter length")
247 -- local str = model:getString("Enter length")
248 local str = getString(model, "Enter length")
249 if not str or str:match("^%s*$)") then return end
250 if num == 1 then -- shorten target
251 lenTarget = tonumber(str)
252 elseif num == 2 then -- shorten source
253 lenSource = tonumber(str)
254 elseif num == 3 then -- shorten both
255 lenTarget = tonumber(str)
256 lenSource = tonumber(str)
259 -- start to edit the edges
260 local t = { label = "shorten edges",
263 selection = model:selection(),
264 original = model:page():clone(),
266 undo = _G.revertOriginal,}
267 t.redo = function (t, doc)
269 for _, i in ipairs(t.selection) do
273 for i, obj, sel, layer in p:objects() do
274 if sel and obj:type() == "group" then
275 local elem, plainElem = ungroup(obj)
276 for _,subobj in pairs(plainElem) do
277 shortenObj(subobj, lenSource, lenTarget)
279 p:replace(i, regroup(elem))
281 shortenObj(obj, lenSource, lenTarget)
290 { label = "toggle graph mode", run=toggleGraphMode },
291 { label = "toggle move invisible", run=toggleMoveInvisible },
292 { label = "shorten target", run=shorten },
293 { label = "shorten source", run=shorten },
294 { label = "shorten both", run=shorten },
297 ----------------------------------------------------------------------