/filters/source/libs/dupin/display/DistortableSprite.as

https://github.com/wksaopaulo/WkBook
ActionScript | 94 lines | 70 code | 17 blank | 7 comment | 4 complexity | 175dc29ba4171a5245a2ec1a18002370 MD5 | raw file
  1. package dupin.display
  2. {
  3. import flash.display.Sprite;
  4. import flash.geom.Point;
  5. import flash.display.DisplayObject;
  6. import flash.display.BitmapData;
  7. public class DistortableSprite extends Sprite
  8. {
  9. public var container:DisplayObject;
  10. protected var cols:int;
  11. protected var rows:int;
  12. protected var points:Vector.<Number>;
  13. protected var uvs:Vector.<Number>;
  14. protected var indices:Vector.<int>;
  15. public function DistortableSprite(spr:DisplayObject, cols:int, rows:int)
  16. {
  17. container = spr;
  18. this.cols = cols;
  19. this.rows = rows;
  20. points=new Vector.<Number>(rows*cols*2);
  21. uvs=new Vector.<Number>(rows*cols*2);
  22. indices = new Vector.<int>();
  23. /**
  24. * UVs
  25. */
  26. var idx:int=0;
  27. for (var r:int = 0; r < rows; r++)
  28. for (var c:int = 0; c < cols; c++)
  29. {
  30. idx = pointIndex(r+1, c+1);
  31. uvs[idx] = c/(cols-1);
  32. uvs[idx+1] = r/(rows-1);
  33. }
  34. }
  35. public function getPoint(col:int, row:int):void
  36. {
  37. }
  38. public function setPoint(p:Point, col:int, row:int):void
  39. {
  40. var idx:int = pointIndex(row, col);
  41. points[idx] = p.x;
  42. points[idx+1] = p.y;
  43. }
  44. public function pointIndex(row:int, col:int):int
  45. {
  46. return (((row-1)*cols) + (col-1)) * 2;
  47. }
  48. public function triIndex(row:int, col:int):int
  49. {
  50. return (((row-1)*cols) + (col-1));
  51. }
  52. public function draw():void
  53. {
  54. var btmp:BitmapData = new BitmapData(container.width, container.height, true, 0x0);
  55. btmp.draw(container);
  56. //Cleaning indices
  57. indices = new Vector.<int>;
  58. //Looping to create indices
  59. for (var r:int = 1; r < rows; r++)
  60. for (var c:int = 1; c < cols; c++)
  61. {
  62. //1st tri
  63. indices.push(triIndex(r, c));
  64. indices.push(triIndex(r, c+1));
  65. indices.push(triIndex(r+1, c));
  66. //2st tri
  67. indices.push(triIndex(r+1, c));
  68. indices.push(triIndex(r+1, c+1));
  69. indices.push(triIndex(r, c+1));
  70. }
  71. graphics.clear();
  72. graphics.beginBitmapFill(btmp);
  73. graphics.drawTriangles(points, indices, uvs);
  74. graphics.endFill();
  75. }
  76. }
  77. }