Last active
December 21, 2015 09:49
-
-
Save kchodorow/6288227 to your computer and use it in GitHub Desktop.
This is a SpriteScale9 class for use with LimeJS. It can be used to create resizable shapes with rounded corners, e.g., speech bubbles and progress bars.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (C) 2013 Kristina Chodorow | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// 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. | |
// This class allows you to create resizable images such as speech bubbles, | |
// progress bars, and other such "fixed edge" shapes. | |
// | |
// SpriteScale9 is a child class of lime.Sprite, so most methods that work on | |
// lime.Sprite should be usable with kchodorow.lime.SpriteScale9. This class is | |
// designed to be used with a lime.SpriteSheet, not "bare" images. | |
// | |
// Usage: | |
// | |
// require('kchodorow.lime.SpriteScale9'); | |
// | |
// var spriteSheet = new lime.SpriteSheet('someSpriteSheet.png', | |
// lime.ASSETS.someSpriteSheet.json, lime.parser.JSON); | |
// var sprite = new kchodorow.lime.SpriteScale9() | |
// .setFill(spriteSheet.getFrame('bubble.png')).scale9(300,200); | |
// scene.appendChild(sprite); | |
// | |
goog.provide('kchodorow.lime.SpriteScale9'); | |
goog.require('lime.Node'); | |
goog.require('lime.Sprite'); | |
kchodorow.lime.SpriteScale9 = function() { | |
goog.base(this); | |
}; | |
goog.inherits(kchodorow.lime.SpriteScale9, lime.Sprite); | |
kchodorow.lime.SpriteScale9.prototype.id = 'spritescale9'; | |
// Takes a lime.fill.Frame and splits it into 9 pieces: two cuts horizontally | |
// and two cuts vertically. | |
kchodorow.lime.SpriteScale9.prototype.setFill = function(fill) { | |
var rect = fill.rect_; | |
var width = this.width_ = rect.width/3; | |
var height = this.height_ = rect.height/3; | |
for (var x = 0; x < 3; x++) { | |
for (var y = 0; y < 3; y++) { | |
var frame = new lime.fill.Frame(fill.image_, rect.left+width*x, | |
rect.top+height*y, width+1, height+1); | |
var sprite = new lime.Sprite().setFill(frame) | |
.setPosition(width*x, height*y).setAnchorPoint(0, 0); | |
this.appendChild(sprite); | |
} | |
} | |
return this; | |
}; | |
// Human-readable name for each quadent. | |
var kLeftTopChild = 0, | |
kLeftMiddleChild = 1, | |
kLeftBottomChild = 2, | |
kMiddleTopChild = 3, | |
kMiddleMiddleChild = 4, | |
kMiddleBottomChild = 5, | |
kRightTopChild = 6, | |
kRightMiddleChild = 7, | |
kRightBottomChild = 8; | |
// This method scales the shape to a given height and width. | |
// @param width Number, must be larger than the original width | |
// @param height Number, must be larger than the original height | |
// @returns this SpriteScale9 | |
kchodorow.lime.SpriteScale9.prototype.scale9 = function(width, height) { | |
var stretch_x = width - this.width_*2; | |
var stretch_y = height - this.height_*2; | |
// Fix sizes | |
this.children_[kMiddleTopChild].setSize(stretch_x, this.height_); | |
this.children_[kLeftMiddleChild].setSize(this.width_, stretch_y); | |
this.children_[kMiddleMiddleChild].setSize(stretch_x+1, stretch_y+1); | |
this.children_[kRightMiddleChild].setSize(this.width_, stretch_y); | |
this.children_[kMiddleBottomChild].setSize(stretch_x, this.height_); | |
// Fix positions | |
this.children_[kLeftBottomChild].setPosition(0, stretch_y+this.height_-2); | |
this.children_[kMiddleBottomChild].setPosition(this.width_, | |
stretch_y+this.height_-1); | |
this.children_[kMiddleMiddleChild].setPosition(this.width_-1, this.height_-1); | |
this.children_[kRightTopChild].setPosition(stretch_x+this.width_-2, 0); | |
this.children_[kRightMiddleChild].setPosition(stretch_x+this.width_-1, | |
this.height_-1); | |
this.children_[kRightBottomChild].setPosition(stretch_x+this.width_-2, | |
stretch_y+this.height_-2); | |
return this; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment