From: Thomas Bläsius Date: Tue, 17 Dec 2013 09:41:07 +0000 (+0100) Subject: decorator in a usable state X-Git-Url: https://i11git.iti.kit.edu/anon-gitweb/?p=Misc%2Fipe.git;a=commitdiff_plain;h=0a034064373daa9ed6081c760772b2c2143aa647 decorator in a usable state --- diff --git a/ipelets/decorator/decorator.lua b/ipelets/decorator/decorator.lua new file mode 100644 index 0000000..8797c8b --- /dev/null +++ b/ipelets/decorator/decorator.lua @@ -0,0 +1,97 @@ + +-- return a table of names associated with decorator symbols +function decorator_names(model) + local sheets = model.doc:sheets() + local symbols = sheets:allNames("symbol") + local res = {} + for _, name in pairs(symbols) do + if name:find("deco/") == 1 then + res[#res + 1] = name + end + end + return res +end + +-- Decorate something given by its bounding box with a given deco +-- object, which needs to be a path. +function decorate(model, bbox, deco) + if (deco:type() ~= "path") then + model.ui:explain("The decoration needs to be a path.") + return + end + + local shape = deco:shape() + for _,path in pairs(shape) do + for _,subpath in ipairs(path) do + -- move all points + for i,point in ipairs(subpath) do + subpath[i] = translation(bbox, point) * point + end + + -- for acs, the center must be translated separately + if (subpath["type"] == "arc") then + local arc = subpath["arc"] + local arc_pos = arc:matrix():translation() + subpath["arc"] = translation(bbox, arc_pos) * arc + end + end + end + -- update model + deco:setShape(shape) + model:creation("create", deco) +end + +-- The translation matrix that should be applied to a given point when +-- doing the decoration. +function translation(bbox, point) + local dx = 0 + local dy = 0 + if (point.x > 0) then + dx = dx + bbox:width() + end + if (point.y > 0) then + dy = dy + bbox:height() + end + dx = dx + bbox:left() + dy = dy + bbox:bottom() + return ipe.Translation(dx, dy) +end + +function mainWindow(model) + if model.ui.win == nil then + return model.ui + else + return model.ui:win() + end +end + +function run_decorator(model) + -- get bbox of primary selection + local p = model:page() + local prim = p:primarySelection() + if not prim then + model.ui:explain("An object must be selected.") + return + end + local bbox = p:bbox(prim) + + -- create decorator object + local dialog = ipeui.Dialog(mainWindow(model), "Select a decorator.") + local decorators = decorator_names(model) + dialog:add("deco", "combo", decorators, 1, 1, 1, 2) + dialog:add("ok", "button", { label="&Ok", action="accept" }, 2, 2) + dialog:add("cancel", "button", { label="&Cancel", action="reject" }, 2, 1) + local r = dialog:execute() + if not r then return end + local deco_name = decorators[dialog:get("deco")] + local symbol = model.doc:sheets():find("symbol", deco_name) + local deco = symbol:clone() + + -- run the decoration + decorate(model, bbox, deco) +end + +label = "Decorator" +methods = { + { label = "Decorate", run=run_decorator}, +} diff --git a/ipelets/decorator/test.ipe b/ipelets/decorator/test.ipe new file mode 100644 index 0000000..29da8f1 --- /dev/null +++ b/ipelets/decorator/test.ipe @@ -0,0 +1,318 @@ + + + + +\usepackage[english]{babel} +\usepackage{blindtext} + + + +0 0 m +-1 0.333 l +-1 -0.333 l +h + + + + +0 0 m +-1 0.333 l +-1 -0.333 l +h + + + + +0.6 0 0 0.6 0 0 e +0.4 0 0 0.4 0 0 e + + + + +0.6 0 0 0.6 0 0 e + + + + + +0.5 0 0 0.5 0 0 e + + +0.6 0 0 0.6 0 0 e +0.4 0 0 0.4 0 0 e + + + + + +-0.6 -0.6 m +0.6 -0.6 l +0.6 0.6 l +-0.6 0.6 l +h +-0.4 -0.4 m +0.4 -0.4 l +0.4 0.4 l +-0.4 0.4 l +h + + + + +-0.6 -0.6 m +0.6 -0.6 l +0.6 0.6 l +-0.6 0.6 l +h + + + + + +-0.5 -0.5 m +0.5 -0.5 l +0.5 0.5 l +-0.5 0.5 l +h + + +-0.6 -0.6 m +0.6 -0.6 l +0.6 0.6 l +-0.6 0.6 l +h +-0.4 -0.4 m +0.4 -0.4 l +0.4 0.4 l +-0.4 0.4 l +h + + + + + + +-0.43 -0.57 m +0.57 0.43 l +0.43 0.57 l +-0.57 -0.43 l +h + + +-0.43 0.57 m +0.57 -0.43 l +0.43 -0.57 l +-0.57 0.43 l +h + + + + + +0 0 m +-1 0.333 l +-1 -0.333 l +h + + + + +0 0 m +-1 0.333 l +-0.8 0 l +-1 -0.333 l +h + + + + +0 0 m +-1 0.333 l +-0.8 0 l +-1 -0.333 l +h + + + + +-1 0.333 m +0 0 l +-1 -0.333 l + + + + +0 0 m +-1 0.333 l +-1 -0.333 l +h +-1 0 m +-2 0.333 l +-2 -0.333 l +h + + + + +0 0 m +-1 0.333 l +-1 -0.333 l +h +-1 0 m +-2 0.333 l +-2 -0.333 l +h + + + + + +-4 4 m +-4 0 l +0 0 l +0 4 l +h + + +-4 0 m +-4 4 l +0 4 l + + + + + +5 1 m +4 0 0 4 1 1 1 5 a +-5 5 l +-5 -1 l +4 0 0 4 -1 -1 -1 -5 a +5 -5 l +h + + + + +-5 5 m +-5 -5 l +5 -5 l +5 5 l +h + + + + +5 1 m +4 0 0 4 1 1 1 5 a +-1 5 l +4 0 0 4 -1 1 -5 1 a +-5 -1 l +4 0 0 4 -1 -1 -1 -5 a +1 -5 l +4 0 0 4 1 -1 5 -1 a +h + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +469 369 m +4 0 0 4 465 369 465 373 a +159 373 l +4 0 0 4 159 369 155 369 a +155 262.503 l +4 0 0 4 159 262.503 159 258.503 a +465 258.503 l +4 0 0 4 465 262.503 469 262.503 a +h + + +139 501 m +139 386.503 l +453 386.503 l +453 501 l +h + + +469 625 m +4 0 0 4 465 625 465 629 a +155 629 l +155 518.503 l +4 0 0 4 159 518.503 159 514.503 a +469 514.503 l +h + +\blindtext +\blindtext +\blindtext + +