PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/flixel/util/FlxSpriteUtil.hx

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