Skip to content

Instantly share code, notes, and snippets.

@savage69kr
Created July 6, 2012 15:51
Show Gist options
  • Save savage69kr/3061015 to your computer and use it in GitHub Desktop.
Save savage69kr/3061015 to your computer and use it in GitHub Desktop.
Add the possibility to flip fonts in impact
// Add the possibility to flip fonts in impact
// This adds two parameters to the draw method of ig.Font
// Use it like this:
// this.font.draw( 'It Works!', x, y, ig.Font.ALIGN.CENTER, true, true );
// The last two values are the new parameters, the first flips horizontally and the second vertically
// Use like you want to, just don't blame me for anything.
ig.module( 'plugins.flipFont' )
.requires(
'impact.font'
)
.defines(function() {
ig.Font.inject({
draw: function( text, x, y, align, flipX, flipY ) {
if ( typeof(text) != 'string' ) {
text = text.toString();
}
if ( align == ig.Font.ALIGN.RIGHT || align == ig.Font.ALIGN.CENTER ) {
var width = 0;
for ( var i = 0; i < text.length; i++ ) {
var c = text.charCodeAt(i);
width += this.widthMap[c - this.firstChar] + 1;
}
x -= align == ig.Font.ALIGN.CENTER ? width/2 : width;
}
if ( flipX ) {
for ( var i = text.length; i--; ) {
var c = text.charCodeAt(i);
x += this._drawChar( c - this.firstChar, x, y, flipX, flipY );
}
}
else {
for ( var i = 0; i < text.length; i++ ) {
var c = text.charCodeAt(i);
x += this._drawChar( c - this.firstChar, x, y );
}
}
ig.Image.drawCount += text.length;
},
_drawChar: function( c, targetX, targetY, flipX, flipY ) {
if ( !this.loaded || c < 0 || c >= this.indices.length ) { return 0; }
var scale = ig.system.scale,
charX = this.indices[c] * scale,
charY = 0,
charWidth = this.widthMap[c] * scale,
charHeight = (this.height-2) * scale,
scaleX = flipX ? -1 : 1,
scaleY = flipY ? -1 : 1;
if ( flipX || flipY ) {
ig.system.context.save();
ig.system.context.translate(
ig.system.getDrawPos( targetX ) + ( flipX ? charWidth : 0 ),
ig.system.getDrawPos( targetY ) + ( flipY ? charHeight - ( 2 * ig.system.scale ) : 0 )
);
ig.system.context.scale( scaleX, scaleY );
ig.system.context.drawImage(
this.data,
charX, charY,
charWidth, charHeight,
0, 0,
charWidth, charHeight
);
ig.system.context.restore();
}
else {
ig.system.context.drawImage(
this.data,
charX, charY,
charWidth, charHeight,
ig.system.getDrawPos( targetX ),
ig.system.getDrawPos( targetY ),
charWidth, charHeight
);
}
return this.widthMap[c] + 1;
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment