This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
extensions:sxsimpleclippedimage [2011/07/08 08:28] – Changed the sourcecode blocks to be downloadable mnem | extensions:sxsimpleclippedimage [2015/09/14 11:14] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ~~NOTOC~~ | ||
+ | ====== SXSimpleClippedImage ====== | ||
+ | |||
+ | ---- dataentry extension ---- | ||
+ | type : extension | ||
+ | author_mail | ||
+ | description | ||
+ | lastupdate_dt : 2011-07-06 | ||
+ | compatible | ||
+ | tags : clip, image, clipped, mask | ||
+ | homepage_url | ||
+ | download_url | ||
+ | ---- | ||
+ | |||
+ | ===== Usage ===== | ||
+ | |||
+ | To use, just create it instead of an SPImage and then set the area of the image you want to clip: | ||
+ | |||
+ | <code objc> | ||
+ | waterfall = [SXSimpleClippedImage imageWithTexture: | ||
+ | // Or you can use imageWithContentsOfFile: | ||
+ | |||
+ | [waterfall setClipX:0 Y:0 Width:16 Height:16]; | ||
+ | |||
+ | [container addChild: | ||
+ | </ | ||
+ | |||
+ | Each point of the clipping rectangle is a property, so you can easily tween them for different effects. For example, a simple vanish/ | ||
+ | |||
+ | <code objc> | ||
+ | - (void)playTween | ||
+ | { | ||
+ | SPTween *tween = [SPTween tweenWithTarget: | ||
+ | |||
+ | [tween animateProperty: | ||
+ | [tween animateProperty: | ||
+ | |||
+ | [tween animateProperty: | ||
+ | [tween animateProperty: | ||
+ | |||
+ | [tween animateProperty: | ||
+ | [tween animateProperty: | ||
+ | |||
+ | tween.loop = SPLoopTypeReverse; | ||
+ | |||
+ | [self.stage.juggler addObject: | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Changelog ===== | ||
+ | |||
+ | * // | ||
+ | |||
+ | ===== Source Code ===== | ||
+ | |||
+ | <code objc SXSimpleClippedImage.h> | ||
+ | /** | ||
+ | * Licensed under the MIT license: | ||
+ | | ||
+ | | ||
+ | | ||
+ | * (c) Copyright 2011 David Wagner. | ||
+ | | ||
+ | * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
+ | * of this software and associated documentation files (the " | ||
+ | * in the Software without restriction, | ||
+ | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
+ | * copies of the Software, and to permit persons to whom the Software is | ||
+ | * furnished to do so, subject to the following conditions: | ||
+ | | ||
+ | * The above copyright notice and this permission notice shall be included in | ||
+ | * all copies or substantial portions of the Software. | ||
+ | | ||
+ | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
+ | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
+ | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
+ | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
+ | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
+ | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
+ | * THE SOFTWARE. | ||
+ | */ | ||
+ | |||
+ | #import " | ||
+ | |||
+ | /** -------------------------------------------------------------------------- | ||
+ | An SXSimpleClippedImage displays an image, clipped by the clip dimensions | ||
+ | | ||
+ | |||
+ | Any part of the image within the clipping rectangle' | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | Or, for the more mathematically minded: | ||
+ | |||
+ | - 0 ≤ clipX ≤ originalWidth | ||
+ | - 0 ≤ clipY ≤ originalHeight | ||
+ | - 0 ≤ clipWidth ≤ (originalWidth - clipX) | ||
+ | - 0 ≤ clipHeight ≤ (originalHeight - clipY) | ||
+ | |||
+ | In order to not mess with tweens, you can set values other than those | ||
+ | | ||
+ | | ||
+ | to worry about them never finishing because they can't set values outside | ||
+ | that range. | ||
+ | --------------------------------------------------------------------------- */ | ||
+ | |||
+ | @interface SXSimpleClippedImage : SPImage | ||
+ | { | ||
+ | @private | ||
+ | float originalWidth; | ||
+ | float originalHeight; | ||
+ | |||
+ | float clipX; | ||
+ | float clipY; | ||
+ | float clipWidth; | ||
+ | float clipHeight; | ||
+ | |||
+ | SPPoint *topLeft; | ||
+ | SPPoint *topRight; | ||
+ | SPPoint *bottomLeft; | ||
+ | SPPoint *bottomRight; | ||
+ | } | ||
+ | |||
+ | /// -------------------- | ||
+ | /// @name Initialization | ||
+ | /// -------------------- | ||
+ | |||
+ | /// Initialize with a texture. _Designated Initializer_. | ||
+ | - (id)initWithTexture: | ||
+ | |||
+ | /// Initialize | ||
+ | - (id)initWithContentsOfFile: | ||
+ | |||
+ | /// Set the clip rectangle | ||
+ | - (void)setClipX: | ||
+ | |||
+ | /// Factory method | ||
+ | + (SXSimpleClippedImage*)imageWithTexture: | ||
+ | |||
+ | /// Factory method | ||
+ | + (SXSimpleClippedImage*)imageWithContentsOfFile: | ||
+ | |||
+ | /// ---------------- | ||
+ | /// @name Properties | ||
+ | /// ---------------- | ||
+ | |||
+ | /// The original width of the image | ||
+ | @property (nonatomic, readonly) float originalWidth; | ||
+ | |||
+ | /// The original height of the image | ||
+ | @property (nonatomic, readonly) float originalHeight; | ||
+ | |||
+ | /// The clipping rectangle' | ||
+ | @property (nonatomic, assign) float clipX; | ||
+ | |||
+ | /// The clipping rectangle' | ||
+ | @property (nonatomic, assign) float clipY; | ||
+ | |||
+ | /// The width of the clipping rectangle | ||
+ | @property (nonatomic, assign) float clipWidth; | ||
+ | |||
+ | // The height of the clipping rectangle | ||
+ | @property (nonatomic, assign) float clipHeight; | ||
+ | |||
+ | @end | ||
+ | </ | ||
+ | |||
+ | <code objc SXSimpleClippedImage.m> | ||
+ | /** | ||
+ | * Licensed under the MIT license: | ||
+ | | ||
+ | | ||
+ | | ||
+ | * (c) Copyright 2011 David Wagner. | ||
+ | | ||
+ | * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
+ | * of this software and associated documentation files (the " | ||
+ | * in the Software without restriction, | ||
+ | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
+ | * copies of the Software, and to permit persons to whom the Software is | ||
+ | * furnished to do so, subject to the following conditions: | ||
+ | | ||
+ | * The above copyright notice and this permission notice shall be included in | ||
+ | * all copies or substantial portions of the Software. | ||
+ | | ||
+ | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
+ | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
+ | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
+ | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
+ | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
+ | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
+ | * THE SOFTWARE. | ||
+ | */ | ||
+ | |||
+ | #import " | ||
+ | |||
+ | #define CLAMP(n, min, max) ((n) < (min) ? (min) : (n) > (max) ? (max) : (n)) | ||
+ | |||
+ | @interface SXSimpleClippedImage () | ||
+ | |||
+ | - (void)updateTextureCoordinates; | ||
+ | - (void)storeInitialValues; | ||
+ | |||
+ | @end | ||
+ | |||
+ | @implementation SXSimpleClippedImage | ||
+ | |||
+ | @synthesize clipX; | ||
+ | @synthesize clipY; | ||
+ | @synthesize clipWidth; | ||
+ | @synthesize clipHeight; | ||
+ | @synthesize originalWidth; | ||
+ | @synthesize originalHeight; | ||
+ | |||
+ | + (SXSimpleClippedImage *)imageWithTexture: | ||
+ | { | ||
+ | return [[[SXSimpleClippedImage alloc] initWithTexture: | ||
+ | } | ||
+ | |||
+ | + (SXSimpleClippedImage*)imageWithContentsOfFile: | ||
+ | { | ||
+ | return [[[SXSimpleClippedImage alloc] initWithContentsOfFile: | ||
+ | } | ||
+ | |||
+ | - (void)storeInitialValues | ||
+ | { | ||
+ | originalWidth = self.width; | ||
+ | originalHeight = self.height; | ||
+ | |||
+ | topLeft = [[SPPoint alloc] initWithX: 0.0 y:0.0]; | ||
+ | topRight = [[SPPoint alloc] initWithX: 1.0 y:0.0]; | ||
+ | bottomLeft = [[SPPoint alloc] initWithX: 0.0 y:1.0]; | ||
+ | bottomRight = [[SPPoint alloc] initWithX: 1.0 y:1.0]; | ||
+ | |||
+ | clipX = 0; | ||
+ | clipY = 0; | ||
+ | clipWidth = originalWidth; | ||
+ | clipHeight = originalHeight; | ||
+ | } | ||
+ | |||
+ | - (id)initWithTexture: | ||
+ | { | ||
+ | self = [super initWithTexture: | ||
+ | if (self) | ||
+ | { | ||
+ | [self storeInitialValues]; | ||
+ | } | ||
+ | | ||
+ | return self; | ||
+ | } | ||
+ | |||
+ | - (id)initWithContentsOfFile: | ||
+ | { | ||
+ | self = [super initWithContentsOfFile: | ||
+ | if (self) | ||
+ | { | ||
+ | [self storeInitialValues]; | ||
+ | } | ||
+ | | ||
+ | return self; | ||
+ | } | ||
+ | |||
+ | - (void)dealloc | ||
+ | { | ||
+ | [topLeft release]; | ||
+ | [topRight release]; | ||
+ | [bottomLeft release]; | ||
+ | [bottomRight release]; | ||
+ | |||
+ | [super dealloc]; | ||
+ | } | ||
+ | |||
+ | - (void)setClipX: | ||
+ | { | ||
+ | clipX = x; | ||
+ | [self updateTextureCoordinates]; | ||
+ | } | ||
+ | |||
+ | - (void)setClipY: | ||
+ | { | ||
+ | clipY = y; | ||
+ | [self updateTextureCoordinates]; | ||
+ | } | ||
+ | |||
+ | - (void)setClipWidth: | ||
+ | { | ||
+ | clipWidth = width; | ||
+ | [self updateTextureCoordinates]; | ||
+ | } | ||
+ | |||
+ | - (void)setClipHeight: | ||
+ | { | ||
+ | clipHeight = height; | ||
+ | [self updateTextureCoordinates]; | ||
+ | } | ||
+ | |||
+ | - (void)setClipX: | ||
+ | { | ||
+ | clipX = x; | ||
+ | clipY = y; | ||
+ | clipWidth = width; | ||
+ | clipHeight = height; | ||
+ | |||
+ | [self updateTextureCoordinates]; | ||
+ | } | ||
+ | |||
+ | - (void)updateTextureCoordinates | ||
+ | { | ||
+ | float cX = CLAMP(clipX, | ||
+ | float cY = CLAMP(clipY, | ||
+ | float cWidth = CLAMP(clipWidth, | ||
+ | float cHeight = CLAMP(clipHeight, | ||
+ | |||
+ | // Normalise the values for use as texture coordinates | ||
+ | cX /= originalWidth; | ||
+ | cY /= originalHeight; | ||
+ | cWidth /= originalWidth; | ||
+ | cHeight /= originalHeight; | ||
+ | |||
+ | // Setup the cached point values | ||
+ | topLeft.x = cX; | ||
+ | topLeft.y = cY; | ||
+ | |||
+ | topRight.x = cX + cWidth; | ||
+ | topRight.y = cY; | ||
+ | |||
+ | bottomLeft.x = cX; | ||
+ | bottomLeft.y = cY + cHeight; | ||
+ | |||
+ | bottomRight.x = cX + cWidth; | ||
+ | bottomRight.y = cY + cHeight; | ||
+ | |||
+ | // Copy them to the texture vertices | ||
+ | [self setTexCoords: | ||
+ | [self setTexCoords: | ||
+ | [self setTexCoords: | ||
+ | [self setTexCoords: | ||
+ | |||
+ | // Update our scale so we are the correct width and height | ||
+ | self.scaleX = cWidth; | ||
+ | self.scaleY = cHeight; | ||
+ | } | ||
+ | |||
+ | @end | ||
+ | </ | ||
+ | |||
+ | ===== Discussion ===== | ||
+ | |||
+ | //No comments so far. Feel free to edit this part of the page.// |