Author Topic: [Haxe NME] FlxGridSprite  (Read 2747 times)

pWEN

  • Cicadomorph
  • Member
  • **
  • Posts: 85
  • Karma: +0/-0
    • View Profile
    • Portfolio
[Haxe NME] FlxGridSprite
« on: Thu, Sep 27, 2012 »
A while ago, user ChainedLupine created a FlxGridSprite class that added 9-slice scaling to Flixel. Finding such a thing useful, I tried adapting it to fit the Flixel port for Haxe NME. I'm still learning the ins and outs of those three systems, but thanks to the help of TestSubject06 and Asmageddon on the #flixel IRC channel, I seem to have a working port. I could use some feedback though!

Code: [Select]
package com.gapingwolf.gwslib.extensions;

import nme.display.Bitmap;
import nme.display.BitmapData;
import nme.geom.ColorTransform;

import flash.display.Shape;
//import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Rectangle;
import flash.geom.Point;

import org.flixel.FlxG;
import org.flixel.FlxObject;
import org.flixel.FlxSprite;
import org.flixel.FlxRect;

/**
 * Creates a 9-grid style sprite that stretches.
 * @author David "ChainedLupine" Grace (modified for Haxe NME's Flixel port by Owen "pWEN" McNamara. Special thanks to TestSubject06 and Asmageddon on the #flixel IRC channel for helping me!)
 */
class FlxGridSprite extends FlxObject
{
private var _sourceImage:BitmapData;

private var _grid:FlxRect;

private var _scaledImage:BitmapData;

private var _flashRect:Rectangle;
private var _flashPoint:Point;

private var _smoothing:Bool;
public var smoothing(getSmoothing, setSmoothing):Bool;

private var _color:Int = 0xFFFFFF;
public var color(getColor, setColor):Int;

private var _alpha:Float = 1.0;
public var alpha(getAlpha, setAlpha):Float;

private var _ct:ColorTransform;

/**
* Creates the grid sprite using the 9-slice grid given
*
* @param X Left coord to draw the sprite
* @param Y Top coord to draw the sprite
* @param Width Total width of the sprite
* @param Height Total height of the sprite
* @param BitmapClass The image class to use
* @param Grid A FlxRect that contains the grid demarks
* @param Smoothing Smooth when we scale the individual pieces?
*/
public function new(X:Int, Y:Int, Width:Int, Height:Int, BitmapClass:Class<Bitmap>, Grid:FlxRect, Smoothing:Bool = false)
{
super(Math.floor(X), Math.floor(Y), Math.floor(Width), Math.floor(Height));

//FlxG.log("FlxGridSprite x: "+x+", y: "+y+", width: "+width+", height: "+height);

_sourceImage = FlxG.addBitmap(BitmapClass);

_grid = Grid;

_flashRect = new Rectangle (0, 0, width, height);

_smoothing = Smoothing;

buildScaledImage();
}//

/*****************************************************************
*
* GET/SET FUNCTIONS
*
*****************************************************************/

//public function get smoothing():Bool
public function getSmoothing():Bool
{
return _smoothing;
}//

//public function set smoothing(v:Bool):Void
public function setSmoothing(v:Bool):Bool
{
_smoothing = v;
buildScaledImage();
return _smoothing;
}//

public function getColor():Int
{
return _color;
}//

public function setColor(v:Int):Int
{
_color = v;
buildScaledImage();
return _color;
}//

public function getAlpha():Float
{
return _alpha;
}//

public function setAlpha(v:Float):Float
{
if(v > 1)
{
v = 1;
}

if(v < 0)
{
v = 0;
}

if(v == _alpha)
{
return _alpha;
}

_alpha = v;
buildScaledImage();
return _alpha;
}//

/*****************************************************************
*
* FUNCTIONS
*
*****************************************************************/

/**
* Called whenever we need to rebuild the scaled bitmap that is rendered.
*
* Do this after you modify the width/height of this sprite.
*/
public function buildScaledImage():Void
{
if (_scaledImage != null)
{
_scaledImage.dispose();
}

_scaledImage = new BitmapData(Math.floor(width), Math.floor(height), true, 0x0);//00000);
//FlxG.log("_scaledImage width: "+_scaledImage.width+", height: "+_scaledImage.height);

var rows:Array<Int> = [
0,
Math.floor(_grid.top),
Math.floor(_grid.bottom),
Math.floor(_sourceImage.height)
];
var cols:Array<Int> = [
0,
Math.floor(_grid.left),
Math.floor(_grid.right),
Math.floor(_sourceImage.width)
];

var dRows:Array<Int> = [
0,
Math.floor(_grid.top),
Math.floor(height - (_sourceImage.height - _grid.bottom)),
Math.floor(height)
];
var dCols:Array<Int> = [
0,
Math.floor(_grid.left),
Math.floor(width - (_sourceImage.width - _grid.right)),
Math.floor(width)
];

var origin:Rectangle;
var draw:Rectangle;
var mat:Matrix = new Matrix();
var ct:ColorTransform;

//FlxG.log("_color: "+_color+", alpha: "+alpha);
if (_color > 0 || alpha != 1.0)
{
ct = new ColorTransform((_color >> 16 & 0xFF) / 255,
(_color >> 8 & 0xff) / 255,
(_color & 0xff) / 255,
_alpha);
//FlxG.log("ColorTransform - R: "+((_color >> 16 & 0xFF) / 255)+", G: "+((_color >> 8 & 0xff) / 255)+", B: "+((_color & 0xff) / 255)+", A: "+_alpha);
}
else
{
ct = null;
//FlxG.log("ColorTransform - null");
}

//for (var cx:Int = 0; cx < 3; cx++)
for (cx in 0...3)
{
//for (var cy:Int = 0; cy < 3; cy++)
for (cy in 0...3)
{
origin = new Rectangle(cols[cx], rows[cy], cols[cx + 1] - cols[cx], rows[cy + 1] - rows[cy]);
draw = new Rectangle(dCols[cx], dRows[cy], dCols[cx + 1] - dCols[cx], dRows[cy + 1] - dRows[cy]);
//FlxG.log("cx: "+cx+", cy: "+cy);
//FlxG.log("    draw x: "+draw.x+", y: "+draw.y+", width: "+draw.width+", height: "+draw.height);
mat.identity();
mat.a = draw.width / origin.width;
mat.d = draw.height / origin.height;
mat.tx = draw.x - origin.x * mat.a;
mat.ty = draw.y - origin.y * mat.d;
_scaledImage.draw(_sourceImage, mat, ct, null, draw, _smoothing);
//FlxG.log("    _scaledImage: "+_scaledImage);
//FlxG.log("    _sourceImage: "+_sourceImage);
}
}

_flashRect = new Rectangle (0, 0, width, height);

this.draw();
}//

//override public function render():Void
//public function render():Void
override public function draw():Void
{
//FlxG.log("draw() - _point");
//FlxG.log("x: "+_point.x+", y: "+_point.y);
_flashPoint = new Point(_point.x, _point.y);
FlxG.camera.buffer.copyPixels(_scaledImage, _flashRect, _flashPoint, null, null, true);
//FlxG.log("draw() - done");
}//

override public function destroy():Void
{
super.destroy();
_scaledImage.dispose();
}//
}

TestSubject06

  • Member
  • **
  • Posts: 69
  • Karma: +0/-0
    • View Profile
Re: [Haxe NME] FlxGridSprite
« Reply #1 on: Thu, Sep 27, 2012 »
One of these days it would be nice to figure out why FlxGroup wasn't calling draw() on it.
Stuff.

pWEN

  • Cicadomorph
  • Member
  • **
  • Posts: 85
  • Karma: +0/-0
    • View Profile
    • Portfolio
Re: [Haxe NME] FlxGridSprite
« Reply #2 on: Thu, Sep 27, 2012 »
One of these days it would be nice to figure out why FlxGroup wasn't calling draw() on it.

Yeah! I'd be welcome to look into it more, but I'm not sure I'd recognize where the problem is.

zaphod

  • Member
  • **
  • Posts: 25
  • Karma: +0/-0
    • View Profile
Re: [Haxe NME] FlxGridSprite
« Reply #3 on: Thu, Sep 27, 2012 »
@pWEN: I'll try to see into your problem, since this class seems interesting to me

pWEN

  • Cicadomorph
  • Member
  • **
  • Posts: 85
  • Karma: +0/-0
    • View Profile
    • Portfolio
Re: [Haxe NME] FlxGridSprite
« Reply #4 on: Fri, Sep 28, 2012 »
@pWEN: I'll try to see into your problem, since this class seems interesting to me

Much appreciated. :)