Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Extension (category) to align every SPDisplayObject and arrange children in a SPDisplayObjectContainer
// Version: 1.2
// Date: 2011-07-15
// Changes: solved bug in pivotX/Y calculation; added alignment for object positioning outside the screen;
// new calculation functions for aligning, useful for setting X and Y as tween value
// Version: 1.1
// Date: 2011-06-09
// Changes: pivotX/Y now recognized in alignment; selectors for aligning pivotX and pivotY; changed code style
//
// Version: 1.0
// Date: 2011-06-04
// Usage:
//
// vAlign... / hAlign... expects values from 0..1 (0 - 100%)
// i.e. 0.5 centers the object
//
// offset: moves the aligned container with negative or positive values
//
// baseWidth / baseHeight: Is used instead of the width / height of the parent
// the calculated values are rounded by default
#define ROUND_CALCULATED_RESULTS
#import "SPDisplayobject.h"
@interface SPDisplayObject (Alignable)
- (float)calculateHAlign:(float)align baseWidth:(float)baseWidth offsetPx:(float)offset;
- (float)calculateHAlign:(float)align offsetPx:(float)offset;
- (float)calculateVAlign:(float)align baseHeight:(float)baseWidth offsetPx:(float)offset;
- (float)calculateVAlign:(float)align offsetPx:(float)offset;
- (void)hAlign:(float)align baseWidth:(float)baseWidth offsetPx:(float)offset;
- (void)hAlign:(float)align offsetPx:(float)offset;
- (void)hAlign:(float)align;
- (void)vAlign:(float)align baseHeight:(float)baseWidth offsetPx:(float)offset;
- (void)vAlign:(float)align offsetPx:(float)offset;
- (void)vAlign:(float)align;
- (void)hAlign:(float)hAlign vAlign:(float)vAlign;
- (void)alignOutsideLeft;
- (void)alignOutsideTop;
- (void)alignOutsideBottom;
- (void)alignOutsideRight;
- (void)alignPivotX:(float)align;
- (void)alignPivotY:(float)align;
- (void)alignPivotXY:(float)align;
@end
#import "SPDisplayObject+Alignable.h"
@implementation SPDisplayObject (Alignable)
- (float)calculateHAlign:(float)align baseWidth:(float)baseWidth offsetPx:(float)offset
{
float value = (baseWidth - self.width) * align + self.pivotX + offset;
#ifdef ROUND_CALCULATED_RESULTS
value = round(value);
#endif
return value;
}
- (float)calculateHAlign:(float)align offsetPx:(float)offset
{
return [self calculateHAlign:align baseWidth:self.parent.width offsetPx:offset];
}
// ---------------------------------------------------------------------------------------
- (float)calculateVAlign:(float)align baseHeight:(float)baseWidth offsetPx:(float)offset
{
float value = (baseWidth - self.height) * align + self.pivotY + offset;
#ifdef ROUND_CALCULATED_RESULTS
value = round(value);
#endif
return value;
}
- (float)calculateVAlign:(float)align offsetPx:(float)offset
{
return [self calculateVAlign:align baseHeight:self.parent.height offsetPx:offset];
}
// ---------------------------------------------------------------------------------------
- (void)hAlign:(float)align baseWidth:(float)baseWidth offsetPx:(float)offset
{
self.x = [self calculateHAlign:align baseWidth:baseWidth offsetPx:offset];
}
- (void)hAlign:(float)align offsetPx:(float)offset
{
[self hAlign:align baseWidth:self.parent.width offsetPx:offset];
}
- (void)hAlign:(float)align
{
[self hAlign:align baseWidth:self.parent.width offsetPx:0];
}
// ---------------------------------------------------------------------------------------
- (void)vAlign:(float)align baseHeight:(float)baseHeight offsetPx:(float)offset
{
self.y = [self calculateVAlign:align baseHeight:baseHeight offsetPx:offset];
}
- (void)vAlign:(float)align offsetPx:(float)offset
{
[self vAlign:align baseHeight:self.parent.height offsetPx:offset];
}
- (void)vAlign:(float)align
{
[self vAlign:align baseHeight:self.parent.height offsetPx:0];
}
// ---------------------------------------------------------------------------------------
- (void)hAlign:(float)hAlign vAlign:(float)vAlign
{
[self hAlign:hAlign];
[self vAlign:vAlign];
}
// ---------------------------------------------------------------------------------------
- (void)alignOutsideLeft
{
[self hAlign:0 baseWidth:[SPStage mainStage].width offsetPx:-self.width];
}
- (void)alignOutsideTop
{
[self vAlign:0 baseHeight:[SPStage mainStage].height offsetPx:-self.height];
}
- (void)alignOutsideBottom
{
[self vAlign:1.0f baseHeight:[SPStage mainStage].height offsetPx:self.height];
}
- (void)alignOutsideRight
{
[self hAlign:1.0f baseWidth:[SPStage mainStage].width offsetPx:self.width];
}
// ---------------------------------------------------------------------------------------
- (void)alignPivotX:(float)align
{
self.pivotX = self.width * align;
#ifdef ROUND_CALCULATED_RESULTS
self.pivotX = round(self.pivotX);
#endif
}
- (void)alignPivotY:(float)align
{
self.pivotY = self.height * align;
#ifdef ROUND_CALCULATED_RESULTS
self.pivotY = round(self.pivotY);
#endif
}
- (void)alignPivotXY:(float)align;
{
[self alignPivotX:align];
[self alignPivotY:align];
}
@end
// Version: 1.1
// Date: 2011-06-09
// Changes: changed code style
//
// Version: 1.0
// Date: 2011-06-04
// Usage:
//
// vAlign... / hAlign... expects values from 0..1 (0 - 100%)
// i.e. 0.5 centers the object
//
// offset: moves the aligned container with negative or positive values
//
// gap: set a space between all repositioned children of an SPDisplayObjectContainer
#import "SPDisplayObjectContainer.h"
@interface SPDisplayObjectContainer (Alignable)
- (void)hArrangeChildrenWithGap:(float)gap;
- (void)vArrangeChildrenWithGap:(float)gap;
// Aligning and arranging of children in one step in the same direction
- (void)hAlignChildren:(float)align offsetPx:(float)offset gap:(float)gap;
- (void)vAlignChildren:(float)align offsetPx:(float)offset gap:(float)gap;
@end
#import "SPDisplayObjectContainer+Alignable.h"
#import "SPDisplayObject+Alignable.h"
@implementation SPDisplayObjectContainer (Alignable)
// ---------------------------------------------------------------------------------------
//
- (void)hArrangeChildrenWithGap:(float)gap;
{
if (self.numChildren < 2)
return;
SPDisplayObject *prevChild;
for (int i = 1; i < self.numChildren; i++)
{
prevChild = [self childAtIndex:i-1];
SPDisplayObject *child = [self childAtIndex:i];
child.x = prevChild.x + prevChild.width + gap;
}
}
// ---------------------------------------------------------------------------------------
//
- (void)vArrangeChildrenWithGap:(float)gap
{
if (self.numChildren < 2)
return;
SPDisplayObject *prevChild;
for (int i = 1; i < self.numChildren; i++)
{
prevChild = [self childAtIndex:i-1];
SPDisplayObject *child = [self childAtIndex:i];
child.y = prevChild.y + prevChild.height + gap;
}
}
// This:
// [childBoxesGroup hAlignChildren:0 offsetPx:10 gap:10];
// ... is a shortcut for:
// [childBoxesGroup hArrangeChildrenWithGap:10];
// [childBoxesGroup hAlign:0 offsetPx:10];
// ---------------------------------------------------------------------------------------
//
- (void)hAlignChildren:(float)align offsetPx:(float)offset gap:(float)gap
{
float widthParent = self.parent.width;
[self hArrangeChildrenWithGap:gap];
[self hAlign:align baseWidth:widthParent offsetPx:offset];
}
// This:
// [childBoxesGroup vAlignChildren:0 offsetPx:10 gap:10];
// ... is a shortcut for:
// [childBoxesGroup vArrangeChildrenWithGap:10];
// [childBoxesGroup vAlign:0 offsetPx:10];
// ---------------------------------------------------------------------------------------
//
- (void)vAlignChildren:(float)align offsetPx:(float)offset gap:(float)gap
{
float heightParent = self.parent.height;
[self vArrangeChildrenWithGap:gap];
[self vAlign:align baseHeight:heightParent offsetPx:offset];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.