Aligning groups (No Reference point=No problem)

Here’s a small utility function that can easily align display-groups in Graphics 2.0.

This is a work in progress but it’s come in handy a few times for me. There’s not a lot of assertions made, so be sure you pass valid arguments.

Use, copy, modify as you wish :)!

Syntax:

alignGroup(group [, alignment, x, y [, options] ])

group

Group to be aligned

alignment

String. One of these values:

“TL” (top-left)

“TC” (top center)

“TR” (top right)

“CL” (center left)

“C” (center)

“CR” (center right)

“BL” (bottom left)

“BC” (bottom center)

“BR” (bottom right)

x, y

position of object

options

Table. Normally omitted. 

Set to {move=false} to calculate new position without moving group (useful for transitions)

Returns:

New x, y, anchor-x and anchor-y position of object if options set to {move=false}

local alignGroup = function(g, pos, x, y, options) pos = pos or "TL"; x = x or 0; y = y or 0; options = options or {move=true}; g.anchorChildren = true; local newAnchorX, newAnchorY; if (pos == "TL") then newAnchorX = 0; newAnchorY = 0; elseif (pos == "TC") then newAnchorX = 0.5; newAnchorY = 0; elseif (pos == "TR") then newAnchorX = 1; newAnchorY = 0; elseif (pos == "CL") then newAnchorX = 0; newAnchorY = 0.5; elseif (pos == "C") then newAnchorX = 0.5; newAnchorY = 0.5; elseif (pos == "CR") then newAnchorX = 1; newAnchorY = 0.5; elseif (pos == "BL") then newAnchorX = 0; newAnchorY = 1; elseif (pos == "BC") then newAnchorX = 0.5; newAnchorY = 1; elseif (pos == "BR") then newAnchorX = 1; newAnchorY = 1; else print("WARNING: alignGroup: Invalid alignment "..pos); end -- return coordinates and anchor (can be used for transitions etc) if (not options.move) then return x, y, newAnchorX, newAnchorY; else -- move immediately g.anchorX, g.anchorY = newAnchorX, newAnchorY; g.x, g.y = x, y; end end 

Here you can see output of the sample project that creates 9 identical groups and aligns them:

Sample Project (includes one transition)