PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/flixel/util/FlxSpriteUtil.hx

https://github.com/dehydrogenaza/flixel
Haxe | 676 lines | 362 code | 53 blank | 261 comment | 62 complexity | b374412473f322d23ba7cd902be6f273 MD5 | raw file
  1. package flixel.util;
  2. import flash.display.BitmapData;
  3. import flash.display.BitmapDataChannel;
  4. import flash.display.BlendMode;
  5. import flash.display.CapsStyle;
  6. import flash.display.Graphics;
  7. import flash.display.JointStyle;
  8. import flash.display.LineScaleMode;
  9. import flash.display.Sprite;
  10. import flash.geom.ColorTransform;
  11. import flash.geom.Matrix;
  12. import flash.geom.Point;
  13. import flash.geom.Rectangle;
  14. import flixel.effects.FlxFlicker;
  15. import flixel.FlxG;
  16. import flixel.FlxObject;
  17. import flixel.FlxSprite;
  18. import flixel.math.FlxMath;
  19. import flixel.math.FlxPoint;
  20. import flixel.system.FlxAssets;
  21. import flixel.tweens.FlxTween;
  22. // TODO: pad(): Pad the sprite out with empty pixels left/right/above/below it
  23. // TODO: rotateClockwise(): Takes the bitmapData from the given source FlxSprite and rotates it 90 degrees clockwise
  24. /**
  25. * Some handy functions for FlxSprite (FlxObject) manipulation, mostly drawing-related.
  26. * Note that stage quality impacts the results of the draw() functions -
  27. * use FlxG.stage.quality = flash.display.StageQuality.BEST; for best results.
  28. */
  29. class FlxSpriteUtil
  30. {
  31. /**
  32. * Useful helper objects for doing Flash-specific rendering.
  33. * Primarily used for "debug visuals" like drawing bounding boxes directly to the screen buffer.
  34. */
  35. public static var flashGfxSprite(default, null):Sprite = new Sprite();
  36. public static var flashGfx(default, null):Graphics = flashGfxSprite.graphics;
  37. /**
  38. * Takes two source images (typically from Embedded bitmaps) and puts the resulting image into the output FlxSprite.
  39. * Note: It assumes the source and mask are the same size. Different sizes may result in undesired results.
  40. * It works by copying the source image (your picture) into the output sprite. Then it removes all areas of it that do not
  41. * have an alpha color value in the mask image. So if you draw a big black circle in your mask with a transparent edge, you'll
  42. * get a circular image appear. Look at the mask PNG files in the assets/pics folder for examples.
  43. *
  44. * @param output The FlxSprite you wish the resulting image to be placed in (will adjust width/height of image)
  45. * @param source The source image. Typically the one with the image / picture / texture in it.
  46. * @param mask The mask to apply. Remember the non-alpha zero areas are the parts that will display.
  47. * @return The FlxSprite for chaining
  48. */
  49. public static function alphaMask(output:FlxSprite, source:Dynamic, mask:Dynamic):FlxSprite
  50. {
  51. var data:BitmapData = null;
  52. if (Std.is(source, String))
  53. {
  54. data = FlxAssets.getBitmapData(source);
  55. }
  56. else if (Std.is(source, Class))
  57. {
  58. data = Type.createInstance(source, []).bitmapData;
  59. }
  60. else if (Std.is(source, BitmapData))
  61. {
  62. data = cast(source, BitmapData).clone();
  63. }
  64. else
  65. {
  66. return null;
  67. }
  68. var maskData:BitmapData = null;
  69. if (Std.is(mask, String))
  70. {
  71. maskData = FlxAssets.getBitmapData(mask);
  72. }
  73. else if (Std.is(mask, Class))
  74. {
  75. maskData = Type.createInstance(mask, []).bitmapData;
  76. }
  77. else if (Std.is(mask, BitmapData))
  78. {
  79. maskData = mask;
  80. }
  81. else
  82. {
  83. return null;
  84. }
  85. data.copyChannel(maskData, new Rectangle(0, 0, data.width, data.height), new Point(), BitmapDataChannel.ALPHA, BitmapDataChannel.ALPHA);
  86. output.pixels = data;
  87. return output;
  88. }
  89. /**
  90. * Takes the image data from two FlxSprites and puts the resulting image into the output FlxSprite.
  91. * Note: It assumes the source and mask are the same size. Different sizes may result in undesired results.
  92. * It works by copying the source image (your picture) into the output sprite. Then it removes all areas of it that do not
  93. * have an alpha color value in the mask image. So if you draw a big black circle in your mask with a transparent edge, you'll
  94. * get a circular image appear.
  95. *
  96. * @param sprite The source FlxSprite. Typically the one with the image / picture / texture in it.
  97. * @param mask The FlxSprite containing the mask to apply. Remember the non-alpha zero areas are the parts that will display.
  98. * @param output The FlxSprite you wish the resulting image to be placed in (will adjust width/height of image)
  99. * @return The output FlxSprite for chaining
  100. */
  101. public static function alphaMaskFlxSprite(sprite:FlxSprite, mask:FlxSprite, output:FlxSprite):FlxSprite
  102. {
  103. sprite.drawFrame();
  104. var data:BitmapData = sprite.pixels.clone();
  105. data.copyChannel(mask.pixels, new Rectangle(0, 0, sprite.width, sprite.height), new Point(), BitmapDataChannel.ALPHA, BitmapDataChannel.ALPHA);
  106. output.pixels = data;
  107. return output;
  108. }
  109. /**
  110. * Checks the x/y coordinates of the FlxSprite and keeps them within the
  111. * area of 0, 0, FlxG.width, FlxG.height (i.e. wraps it around the screen)
  112. *
  113. * @param sprite The FlxSprite to keep within the screen
  114. * @param Left Whether to activate screen wrapping on the left side of the screen
  115. * @param Right Whether to activate screen wrapping on the right side of the screen
  116. * @param Top Whether to activate screen wrapping on the top of the screen
  117. * @param Bottom Whether to activate screen wrapping on the bottom of the screen
  118. * @return The FlxSprite for chaining
  119. */
  120. public static function screenWrap(sprite:FlxSprite, Left:Bool = true, Right:Bool = true, Top:Bool = true, Bottom:Bool = true):FlxSprite
  121. {
  122. if (Left && ((sprite.x + sprite.frameWidth) <= 0))
  123. {
  124. sprite.x = FlxG.width;
  125. }
  126. else if (Right && (sprite.x >= FlxG.width))
  127. {
  128. sprite.x = 0;
  129. }
  130. if (Top && ((sprite.y + sprite.frameHeight) <= 0))
  131. {
  132. sprite.y = FlxG.height;
  133. }
  134. else if (Bottom && (sprite.y >= FlxG.height))
  135. {
  136. sprite.y = 0;
  137. }
  138. return sprite;
  139. }
  140. /**
  141. * Makes sure a FlxSprite doesn't leave the specified area - most common use case is to call this every frame in update().
  142. * If you call this without specifying an area, the game area (FlxG.width / height as max) will be used. Takes the graphic size into account.
  143. *
  144. * @param sprite The FlxSprite to bound to an area
  145. * @param MinX The minimum x position allowed
  146. * @param MaxX The maximum x position allowed
  147. * @param MinY The minimum y position allowed
  148. * @param MaxY The minimum y position allowed
  149. * @return The FlxSprite for chaining
  150. */
  151. public static function bound(sprite:FlxSprite, MinX:Float = 0, MaxX:Float = 0, MinY:Float = 0, MaxY:Float = 0):FlxSprite
  152. {
  153. if (MaxX <= 0)
  154. {
  155. MaxX = FlxG.width;
  156. }
  157. if (MaxY <= 0)
  158. {
  159. MaxY = FlxG.height;
  160. }
  161. MaxX -= sprite.frameWidth;
  162. MaxY -= sprite.frameHeight;
  163. sprite.x = FlxMath.bound(sprite.x, MinX, MaxX);
  164. sprite.y = FlxMath.bound(sprite.y, MinY, MaxY);
  165. return sprite;
  166. }
  167. /**
  168. * Aligns a set of FlxObjects so there is equal spacing between them
  169. *
  170. * @param objects An Array of FlxObjects
  171. * @param startX The base X coordinate to start the spacing from
  172. * @param startY The base Y coordinate to start the spacing from
  173. * @param horizontalSpacing The amount of pixels between each sprite horizontally (default 0)
  174. * @param verticalSpacing The amount of pixels between each sprite vertically (default 0)
  175. * @param spaceFromBounds If set to true the h/v spacing values will be added to the width/height of the sprite, if false it will ignore this
  176. */
  177. public static function space(objects:Array<FlxObject>, startX:Int, startY:Int, horizontalSpacing:Int = 0,
  178. verticalSpacing:Int = 0, spaceFromBounds:Bool = false):Void
  179. {
  180. var prevWidth:Int = 0;
  181. var prevHeight:Int = 0;
  182. for (i in 0...(objects.length))
  183. {
  184. var object = objects[i];
  185. if (spaceFromBounds)
  186. {
  187. object.x = startX + prevWidth + (i * horizontalSpacing);
  188. object.y = startY + prevHeight + (i * verticalSpacing);
  189. }
  190. else
  191. {
  192. object.x = startX + (i * horizontalSpacing);
  193. object.y = startY + (i * verticalSpacing);
  194. }
  195. }
  196. }
  197. /**
  198. * Centers the given FlxObject on the screen, either by the x axis, y axis, or both
  199. *
  200. * @param object The FlxSprite to center
  201. * @param Horizontally Boolean true if you want it centered horizontally
  202. * @param Vertically Boolean true if you want it centered vertically
  203. * @return The FlxObject for chaining
  204. */
  205. public static function screenCenter(object:FlxObject, xAxis:Bool = true, yAxis:Bool = true):FlxObject
  206. {
  207. if (xAxis)
  208. {
  209. object.x = (FlxG.width / 2) - (object.width / 2);
  210. }
  211. if (yAxis)
  212. {
  213. object.y = (FlxG.height / 2) - (object.height / 2);
  214. }
  215. return object;
  216. }
  217. /**
  218. * This function draws a line on a FlxSprite from position X1,Y1
  219. * to position X2,Y2 with the specified color.
  220. *
  221. * @param sprite The FlxSprite to manipulate
  222. * @param StartX X coordinate of the line's start point.
  223. * @param StartY Y coordinate of the line's start point.
  224. * @param EndX X coordinate of the line's end point.
  225. * @param EndY Y coordinate of the line's end point.
  226. * @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
  227. * @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
  228. * @return The FlxSprite for chaining
  229. */
  230. public static function drawLine(sprite:FlxSprite, StartX:Float, StartY:Float, EndX:Float, EndY:Float,
  231. ?lineStyle:LineStyle, ?drawStyle:DrawStyle):FlxSprite
  232. {
  233. if (lineStyle == null)
  234. lineStyle = { thickness: 1, color: FlxColor.WHITE };
  235. if (lineStyle.thickness == null)
  236. lineStyle.thickness = 1;
  237. if (lineStyle.color == null)
  238. lineStyle.color = FlxColor.WHITE;
  239. beginDraw(0, lineStyle);
  240. flashGfx.moveTo(StartX, StartY);
  241. flashGfx.lineTo(EndX, EndY);
  242. endDraw(sprite, drawStyle);
  243. return sprite;
  244. }
  245. /**
  246. * This function draws a rectangle on a FlxSprite.
  247. *
  248. * @param sprite The FlxSprite to manipulate
  249. * @param X X coordinate of the rectangle's start point.
  250. * @param Y Y coordinate of the rectangle's start point.
  251. * @param Width Width of the rectangle
  252. * @param Height Height of the rectangle
  253. * @param Color The rectangle's color.
  254. * @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
  255. * @param fillStyle A FillStyle typedef containing the params of Graphics.fillStyle()
  256. * @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
  257. * @return The FlxSprite for chaining
  258. */
  259. public static function drawRect(sprite:FlxSprite, X:Float, Y:Float, Width:Float, Height:Float, Color:FlxColor,
  260. ?lineStyle:LineStyle, ?fillStyle:FillStyle, ?drawStyle:DrawStyle):FlxSprite
  261. {
  262. beginDraw(Color, lineStyle, fillStyle);
  263. flashGfx.drawRect(X, Y, Width, Height);
  264. endDraw(sprite, drawStyle);
  265. return sprite;
  266. }
  267. /**
  268. * This function draws a rounded rectangle on a FlxSprite.
  269. *
  270. * @param sprite The FlxSprite to manipulate
  271. * @param X X coordinate of the rectangle's start point.
  272. * @param Y Y coordinate of the rectangle's start point.
  273. * @param Width Width of the rectangle
  274. * @param Height Height of the rectangle
  275. * @param EllipseWidth The width of the ellipse used to draw the rounded corners
  276. * @param EllipseHeight The height of the ellipse used to draw the rounded corners
  277. * @param Color The rectangle's color.
  278. * @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
  279. * @param fillStyle A FillStyle typedef containing the params of Graphics.fillStyle()
  280. * @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
  281. * @return The FlxSprite for chaining
  282. */
  283. public static function drawRoundRect(sprite:FlxSprite, X:Float, Y:Float, Width:Float, Height:Float, EllipseWidth:Float,
  284. EllipseHeight:Float, Color:FlxColor, ?lineStyle:LineStyle, ?fillStyle:FillStyle, ?drawStyle:DrawStyle):FlxSprite
  285. {
  286. beginDraw(Color, lineStyle, fillStyle);
  287. flashGfx.drawRoundRect(X, Y, Width, Height, EllipseWidth, EllipseHeight);
  288. endDraw(sprite, drawStyle);
  289. return sprite;
  290. }
  291. #if flash
  292. /**
  293. * This function draws a rounded rectangle on a FlxSprite. Same as drawRoundRect,
  294. * except it allows you to determine the radius of each corner individually.
  295. *
  296. * @param sprite The FlxSprite to manipulate
  297. * @param X X coordinate of the rectangle's start point.
  298. * @param Y Y coordinate of the rectangle's start point.
  299. * @param Width Width of the rectangle
  300. * @param Height Height of the rectangle
  301. * @param TopLeftRadius The radius of the top left corner of the rectangle
  302. * @param TopRightRadius The radius of the top right corner of the rectangle
  303. * @param BottomLeftRadius The radius of the bottom left corner of the rectangle
  304. * @param BottomRightRadius The radius of the bottom right corner of the rectangle
  305. * @param Color The rectangle's color.
  306. * @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
  307. * @param fillStyle A FillStyle typedef containing the params of Graphics.fillStyle()
  308. * @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
  309. * @return The FlxSprite for chaining
  310. */
  311. public static function drawRoundRectComplex(sprite:FlxSprite, X:Float, Y:Float, Width:Float, Height:Float,
  312. TopLeftRadius:Float, TopRightRadius:Float, BottomLeftRadius:Float, BottomRightRadius:Float, Color:FlxColor,
  313. ?lineStyle:LineStyle, ?fillStyle:FillStyle, ?drawStyle:DrawStyle):FlxSprite
  314. {
  315. beginDraw(Color, lineStyle, fillStyle);
  316. flashGfx.drawRoundRectComplex(X, Y, Width, Height, TopLeftRadius, TopRightRadius, BottomLeftRadius, BottomRightRadius);
  317. endDraw(sprite, drawStyle);
  318. return sprite;
  319. }
  320. #end
  321. /**
  322. * This function draws a circle on a FlxSprite at position X,Y with the specified color.
  323. *
  324. * @param sprite The FlxSprite to manipulate
  325. * @param X X coordinate of the circle's center (automatically centered on the sprite if -1)
  326. * @param Y Y coordinate of the circle's center (automatically centered on the sprite if -1)
  327. * @param Radius Radius of the circle (makes sure the circle fully fits on the sprite's graphic if < 1, assuming and and y are centered)
  328. * @param Color Color of the circle
  329. * @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
  330. * @param fillStyle A FillStyle typedef containing the params of Graphics.fillStyle()
  331. * @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
  332. * @return The FlxSprite for chaining
  333. */
  334. public static function drawCircle(sprite:FlxSprite, X:Float = - 1, Y:Float = - 1, Radius:Float = -1,
  335. Color:FlxColor = FlxColor.WHITE, ?lineStyle:LineStyle, ?fillStyle:FillStyle, ?drawStyle:DrawStyle):FlxSprite
  336. {
  337. if ((X == -1) || (Y == -1))
  338. {
  339. var midPoint = sprite.getGraphicMidpoint();
  340. if (X == -1)
  341. X = midPoint.x;
  342. if (Y == -1)
  343. Y = midPoint.y;
  344. midPoint.put();
  345. }
  346. if (Radius < 1)
  347. {
  348. var minVal = Math.min(sprite.frameWidth, sprite.frameHeight);
  349. Radius = (minVal / 2);
  350. }
  351. beginDraw(Color, lineStyle, fillStyle);
  352. flashGfx.drawCircle(X, Y, Radius);
  353. endDraw(sprite, drawStyle);
  354. return sprite;
  355. }
  356. /**
  357. * This function draws an ellipse on a FlxSprite.
  358. *
  359. * @param sprite The FlxSprite to manipulate
  360. * @param X X coordinate of the ellipse's start point.
  361. * @param Y Y coordinate of the ellipse's start point.
  362. * @param Width Width of the ellipse
  363. * @param Height Height of the ellipse
  364. * @param Color The ellipse's color.
  365. * @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
  366. * @param fillStyle A FillStyle typedef containing the params of Graphics.fillStyle()
  367. * @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
  368. * @return The FlxSprite for chaining
  369. */
  370. public static function drawEllipse(sprite:FlxSprite, X:Float, Y:Float, Width:Float, Height:Float, Color:FlxColor,
  371. ?lineStyle:LineStyle, ?fillStyle:FillStyle, ?drawStyle:DrawStyle):FlxSprite
  372. {
  373. beginDraw(Color, lineStyle, fillStyle);
  374. flashGfx.drawEllipse(X, Y, Width, Height);
  375. endDraw(sprite, drawStyle);
  376. return sprite;
  377. }
  378. /**
  379. * This function draws a simple, equilateral triangle on a FlxSprite.
  380. *
  381. * @param sprite The FlxSprite to manipulate
  382. * @param X X position of the triangle
  383. * @param Y Y position of the triangle
  384. * @param Height Height of the triangle
  385. * @param Color Color of the triangle
  386. * @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
  387. * @param fillStyle A FillStyle typedef containing the params of Graphics.fillStyle()
  388. * @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
  389. * @return The FlxSprite for chaining
  390. */
  391. public static function drawTriangle(sprite:FlxSprite, X:Float, Y:Float, Height:Float, Color:FlxColor,
  392. ?lineStyle:LineStyle, ?fillStyle:FillStyle, ?drawStyle:DrawStyle):FlxSprite
  393. {
  394. beginDraw(Color, lineStyle, fillStyle);
  395. flashGfx.moveTo(X + Height / 2, Y);
  396. flashGfx.lineTo(X + Height, Height + Y);
  397. flashGfx.lineTo(X, Height + Y);
  398. flashGfx.lineTo(X + Height / 2, Y);
  399. endDraw(sprite, drawStyle);
  400. return sprite;
  401. }
  402. /**
  403. * This function draws a polygon on a FlxSprite.
  404. *
  405. * @param sprite The FlxSprite to manipulate
  406. * @param Vertices Array of Vertices to use for drawing the polygon
  407. * @param Color Color of the polygon
  408. * @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
  409. * @param fillStyle A FillStyle typedef containing the params of Graphics.fillStyle();
  410. * @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
  411. * @return The FlxSprite for chaining
  412. */
  413. public static function drawPolygon(sprite:FlxSprite, Vertices:Array<FlxPoint>, Color:FlxColor, ?lineStyle:LineStyle,
  414. ?fillStyle:FillStyle, ?drawStyle:DrawStyle):FlxSprite
  415. {
  416. beginDraw(Color, lineStyle, fillStyle);
  417. var p:FlxPoint = Vertices.shift();
  418. flashGfx.moveTo(p.x, p.y);
  419. for (p in Vertices)
  420. {
  421. flashGfx.lineTo(p.x, p.y);
  422. }
  423. endDraw(sprite, drawStyle);
  424. return sprite;
  425. }
  426. /**
  427. * Helper function that the drawing functions use at the start to set the color and lineStyle.
  428. *
  429. * @param Color The color to use for drawing
  430. * @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
  431. * @param fillStyle A FillStyle typedef containing the params of Graphics.fillStyle()
  432. */
  433. @:noUsing
  434. public static inline function beginDraw(Color:FlxColor, ?lineStyle:LineStyle, ?fillStyle:FillStyle):Void
  435. {
  436. flashGfx.clear();
  437. setLineStyle(lineStyle);
  438. if ((fillStyle != null) && (fillStyle.hasFill))
  439. {
  440. //use the fillStyle for color information
  441. Color = fillStyle.color;
  442. }
  443. flashGfx.beginFill(Color.to24Bit(), Color.alphaFloat);
  444. }
  445. /**
  446. * Helper function that the drawing functions use at the end.
  447. *
  448. * @param sprite The FlxSprite to draw to
  449. * @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
  450. * @return The FlxSprite for chaining
  451. */
  452. public static inline function endDraw(sprite:FlxSprite, ?drawStyle:DrawStyle):FlxSprite
  453. {
  454. flashGfx.endFill();
  455. updateSpriteGraphic(sprite, drawStyle);
  456. return sprite;
  457. }
  458. /**
  459. * Just a helper function that is called at the end of the draw functions
  460. * to handle a few things related to updating a sprite's graphic.
  461. *
  462. * @param Sprite The FlxSprite to manipulate
  463. * @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
  464. * @return The FlxSprite for chaining
  465. */
  466. public static function updateSpriteGraphic(sprite:FlxSprite, ?drawStyle:DrawStyle):FlxSprite
  467. {
  468. if (drawStyle == null)
  469. {
  470. drawStyle = { smoothing: false };
  471. }
  472. else if (drawStyle.smoothing == null)
  473. {
  474. drawStyle.smoothing = false;
  475. }
  476. sprite.pixels.draw(flashGfxSprite, drawStyle.matrix, drawStyle.colorTransform,
  477. drawStyle.blendMode, drawStyle.clipRect, drawStyle.smoothing);
  478. sprite.dirty = true;
  479. sprite.resetFrameBitmapDatas();
  480. return sprite;
  481. }
  482. /**
  483. * Just a helper function that is called in the draw functions
  484. * to set the lineStyle via Graphics.lineStyle()
  485. *
  486. * @param lineStyle The lineStyle typedef
  487. */
  488. @:noUsing
  489. public static inline function setLineStyle(lineStyle:LineStyle):Void
  490. {
  491. if (lineStyle != null)
  492. {
  493. var color:FlxColor;
  494. if (lineStyle.color == null)
  495. {
  496. color = FlxColor.BLACK;
  497. }
  498. else
  499. {
  500. color = lineStyle.color;
  501. }
  502. if (lineStyle.pixelHinting == null) { lineStyle.pixelHinting = false; }
  503. if (lineStyle.miterLimit == null) { lineStyle.miterLimit = 3; }
  504. flashGfx.lineStyle(
  505. lineStyle.thickness,
  506. color.to24Bit(),
  507. color.alphaFloat,
  508. lineStyle.pixelHinting,
  509. lineStyle.scaleMode,
  510. lineStyle.capsStyle,
  511. lineStyle.jointStyle,
  512. lineStyle.miterLimit);
  513. }
  514. }
  515. /**
  516. * Fills this sprite's graphic with a specific color.
  517. *
  518. * @param Sprite The FlxSprite to manipulate
  519. * @param Color The color with which to fill the graphic, format 0xAARRGGBB.
  520. * @return The FlxSprite for chaining
  521. */
  522. public static function fill(sprite:FlxSprite, Color:FlxColor):FlxSprite
  523. {
  524. sprite.pixels.fillRect(sprite.pixels.rect, Color);
  525. if (sprite.pixels != sprite.framePixels)
  526. {
  527. sprite.dirty = true;
  528. }
  529. sprite.resetFrameBitmapDatas();
  530. return sprite;
  531. }
  532. /**
  533. * A simple flicker effect for sprites achieved by toggling visibility.
  534. *
  535. * @param Object The sprite.
  536. * @param Duration How long to flicker for. 0 means "forever".
  537. * @param Interval In what interval to toggle visibility. Set to FlxG.elapsed if <= 0!
  538. * @param EndVisibility Force the visible value when the flicker completes, useful with fast repetitive use.
  539. * @param ForceRestart Force the flicker to restart from beginnig, discarding the flickering effect already in progress if there is one.
  540. * @param CompletionCallback An optional callback that will be triggered when a flickering has finished.
  541. * @param ProgressCallback An optional callback that will be triggered when visibility is toggled.
  542. * @return The FlxObject for chaining
  543. */
  544. public static inline function flicker(Object:FlxObject, Duration:Float = 1, Interval:Float = 0.04, EndVisibility:Bool = true,
  545. ForceRestart:Bool = true, ?CompletionCallback:FlxFlicker->Void, ?ProgressCallback:FlxFlicker->Void):FlxObject
  546. {
  547. FlxFlicker.flicker(Object, Duration, Interval, EndVisibility, ForceRestart, CompletionCallback, ProgressCallback);
  548. return Object;
  549. }
  550. /**
  551. * Returns whether an object is flickering or not.
  552. *
  553. * @param Object The object to check against.
  554. */
  555. public static inline function isFlickering(Object:FlxObject):Bool
  556. {
  557. return FlxFlicker.isFlickering(Object);
  558. }
  559. /**
  560. * Stops flickering of the object. Also it will make the object visible.
  561. *
  562. * @param Object The object to stop flickering.
  563. * @return The FlxObject for chaining
  564. */
  565. public static inline function stopFlickering(Object:FlxObject):FlxObject
  566. {
  567. FlxFlicker.stopFlickering(Object);
  568. return Object;
  569. }
  570. /**
  571. * Fade in a sprite, tweening alpha to 1.
  572. *
  573. * @param sprite The object to fade.
  574. * @param Duration How long the fade will take (in seconds).
  575. * @return The FlxSprite for chaining
  576. */
  577. public static inline function fadeIn(sprite:FlxSprite, Duration:Float = 1, ?ResetAlpha:Bool, ?OnComplete:CompleteCallback):FlxSprite
  578. {
  579. if (ResetAlpha)
  580. {
  581. sprite.alpha = 0;
  582. }
  583. FlxTween.num(sprite.alpha, 1, Duration, { complete: OnComplete }, alphaTween.bind(sprite));
  584. return sprite;
  585. }
  586. /**
  587. * Fade out a sprite, tweening alpha to 0.
  588. *
  589. * @param sprite The object to fade.
  590. * @param Duration How long the fade will take (in seconds).
  591. * @return The FlxSprite for chaining
  592. */
  593. public static inline function fadeOut(sprite:FlxSprite, Duration:Float = 1, ?OnComplete:CompleteCallback):FlxSprite
  594. {
  595. FlxTween.num(sprite.alpha, 0, Duration, { complete: OnComplete }, alphaTween.bind(sprite));
  596. return sprite;
  597. }
  598. private static function alphaTween(sprite:FlxSprite, f:Float):Void
  599. {
  600. sprite.alpha = f;
  601. }
  602. }
  603. typedef LineStyle = {
  604. ?thickness:Float,
  605. ?color:FlxColor,
  606. ?pixelHinting:Bool,
  607. ?scaleMode:LineScaleMode,
  608. ?capsStyle:CapsStyle,
  609. ?jointStyle:JointStyle,
  610. ?miterLimit:Float
  611. }
  612. typedef FillStyle = {
  613. ?hasFill:Bool,
  614. ?color:FlxColor,
  615. ?alpha:Float
  616. }
  617. typedef DrawStyle = {
  618. ?matrix:Matrix,
  619. ?colorTransform:ColorTransform,
  620. ?blendMode:BlendMode,
  621. ?clipRect:Rectangle,
  622. ?smoothing:Bool
  623. }