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

/flixel/util/FlxSpriteUtil.hx

http://github.com/Beeblerox/HaxeFlixel
Haxe | 645 lines | 328 code | 50 blank | 267 comment | 53 complexity | 36610246fbb21697c2fbeaee16252d1c 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 unexecpted 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 unexecpted 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 (default 0)
  149. * @param verticalSpacing The amount of pixels between each sprite vertically (default 0)
  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. */
  152. public static function space(objects:Array<FlxObject>, startX:Int, startY:Int, horizontalSpacing:Int = 0,
  153. verticalSpacing:Int = 0, spaceFromBounds:Bool = false):Void
  154. {
  155. var prevWidth:Int = 0;
  156. var prevHeight:Int = 0;
  157. for (i in 0...objects.length)
  158. {
  159. var object = objects[i];
  160. if (spaceFromBounds)
  161. {
  162. object.x = startX + prevWidth + (i * horizontalSpacing);
  163. object.y = startY + prevHeight + (i * verticalSpacing);
  164. }
  165. else
  166. {
  167. object.x = startX + (i * horizontalSpacing);
  168. object.y = startY + (i * verticalSpacing);
  169. }
  170. }
  171. }
  172. /**
  173. * This function draws a line on a FlxSprite from position X1,Y1
  174. * to position X2,Y2 with the specified color.
  175. *
  176. * @param sprite The FlxSprite to manipulate
  177. * @param StartX X coordinate of the line's start point.
  178. * @param StartY Y coordinate of the line's start point.
  179. * @param EndX X coordinate of the line's end point.
  180. * @param EndY Y coordinate of the line's end point.
  181. * @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
  182. * @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
  183. * @return The FlxSprite for chaining
  184. */
  185. public static function drawLine(sprite:FlxSprite, StartX:Float, StartY:Float, EndX:Float, EndY:Float,
  186. ?lineStyle:LineStyle, ?drawStyle:DrawStyle):FlxSprite
  187. {
  188. lineStyle = getDefaultLineStyle(lineStyle);
  189. beginDraw(0x0, lineStyle);
  190. flashGfx.moveTo(StartX, StartY);
  191. flashGfx.lineTo(EndX, EndY);
  192. endDraw(sprite, drawStyle);
  193. return sprite;
  194. }
  195. /**
  196. * This function draws a curve on a FlxSprite from position X1,Y1
  197. * to anchor position X2,Y2 using control points X3,Y3 with the specified color.
  198. *
  199. * @param sprite The FlxSprite to manipulate
  200. * @param StartX X coordinate of the curve's start point.
  201. * @param StartY Y coordinate of the curve's start point.
  202. * @param EndX X coordinate of the curve's end/anchor point.
  203. * @param EndY Y coordinate of the curve's end/anchor point.
  204. * @param ControlX X coordinate of the curve's control point.
  205. * @param ControlY Y coordinate of the curve's control point.
  206. * @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.
  207. * @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
  208. * @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
  209. * @return The FlxSprite for chaining
  210. */
  211. public static function drawCurve(sprite:FlxSprite, StartX:Float, StartY:Float, EndX:Float, EndY:Float, ControlX:Float, ControlY:Float,
  212. FillColor:FlxColor = FlxColor.TRANSPARENT, ?lineStyle:LineStyle, ?drawStyle:DrawStyle):FlxSprite
  213. {
  214. lineStyle = getDefaultLineStyle(lineStyle);
  215. beginDraw(FillColor, lineStyle);
  216. flashGfx.moveTo(StartX, StartY);
  217. flashGfx.curveTo(EndX, EndY, ControlX, ControlY);
  218. endDraw(sprite, drawStyle);
  219. return sprite;
  220. }
  221. /**
  222. * This function draws a rectangle on a FlxSprite.
  223. *
  224. * @param sprite The FlxSprite to manipulate
  225. * @param X X coordinate of the rectangle's start point.
  226. * @param Y Y coordinate of the rectangle's start point.
  227. * @param Width Width of the rectangle
  228. * @param Height Height of the rectangle
  229. * @param FillColor The ARGB color to fill this rectangle with. FlxColor.TRANSPARENT (0x0) means no fill.
  230. * @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
  231. * @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
  232. * @return The FlxSprite for chaining
  233. */
  234. public static function drawRect(sprite:FlxSprite, X:Float, Y:Float, Width:Float, Height:Float,
  235. FillColor:FlxColor = FlxColor.WHITE, ?lineStyle:LineStyle, ?drawStyle:DrawStyle):FlxSprite
  236. {
  237. beginDraw(FillColor, lineStyle);
  238. flashGfx.drawRect(X, Y, Width, Height);
  239. endDraw(sprite, drawStyle);
  240. return sprite;
  241. }
  242. /**
  243. * This function draws a rounded rectangle on a FlxSprite.
  244. *
  245. * @param sprite The FlxSprite to manipulate
  246. * @param X X coordinate of the rectangle's start point.
  247. * @param Y Y coordinate of the rectangle's start point.
  248. * @param Width Width of the rectangle
  249. * @param Height Height of the rectangle
  250. * @param EllipseWidth The width of the ellipse used to draw the rounded corners
  251. * @param EllipseHeight The height of the ellipse used to draw the rounded corners
  252. * @param FillColor The ARGB color to fill this rectangle with. FlxColor.TRANSPARENT (0x0) means no fill.
  253. * @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
  254. * @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
  255. * @return The FlxSprite for chaining
  256. */
  257. public static function drawRoundRect(sprite:FlxSprite, X:Float, Y:Float, Width:Float, Height:Float, EllipseWidth:Float,
  258. EllipseHeight:Float, FillColor:FlxColor = FlxColor.WHITE, ?lineStyle:LineStyle, ?drawStyle:DrawStyle):FlxSprite
  259. {
  260. beginDraw(FillColor, lineStyle);
  261. flashGfx.drawRoundRect(X, Y, Width, Height, EllipseWidth, EllipseHeight);
  262. endDraw(sprite, drawStyle);
  263. return sprite;
  264. }
  265. #if flash
  266. /**
  267. * This function draws a rounded rectangle on a FlxSprite. Same as drawRoundRect,
  268. * except it allows you to determine the radius of each corner individually.
  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 TopLeftRadius The radius of the top left corner of the rectangle
  276. * @param TopRightRadius The radius of the top right corner of the rectangle
  277. * @param BottomLeftRadius The radius of the bottom left corner of the rectangle
  278. * @param BottomRightRadius The radius of the bottom right corner of the rectangle
  279. * @param FillColor The ARGB color to fill this rectangle with. FlxColor.TRANSPARENT (0x0) means no fill.
  280. * @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
  281. * @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
  282. * @return The FlxSprite for chaining
  283. */
  284. public static function drawRoundRectComplex(sprite:FlxSprite, X:Float, Y:Float, Width:Float, Height:Float,
  285. TopLeftRadius:Float, TopRightRadius:Float, BottomLeftRadius:Float, BottomRightRadius:Float,
  286. FillColor:FlxColor = FlxColor.WHITE, ?lineStyle:LineStyle, ?drawStyle:DrawStyle):FlxSprite
  287. {
  288. beginDraw(FillColor, lineStyle);
  289. flashGfx.drawRoundRectComplex(X, Y, Width, Height, TopLeftRadius, TopRightRadius, BottomLeftRadius, BottomRightRadius);
  290. endDraw(sprite, drawStyle);
  291. return sprite;
  292. }
  293. #end
  294. /**
  295. * This function draws a circle on a FlxSprite at position X,Y with the specified color.
  296. *
  297. * @param sprite The FlxSprite to manipulate
  298. * @param X X coordinate of the circle's center (automatically centered on the sprite if -1)
  299. * @param Y Y coordinate of the circle's center (automatically centered on the sprite if -1)
  300. * @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)
  301. * @param FillColor The ARGB color to fill this circle with. FlxColor.TRANSPARENT (0x0) means no fill.
  302. * @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
  303. * @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
  304. * @return The FlxSprite for chaining
  305. */
  306. public static function drawCircle(sprite:FlxSprite, X:Float = - 1, Y:Float = - 1, Radius:Float = -1,
  307. FillColor:FlxColor = FlxColor.WHITE, ?lineStyle:LineStyle, ?drawStyle:DrawStyle):FlxSprite
  308. {
  309. if (X == -1 || Y == -1)
  310. {
  311. var midPoint = sprite.getGraphicMidpoint();
  312. if (X == -1)
  313. X = midPoint.x - sprite.x;
  314. if (Y == -1)
  315. Y = midPoint.y - sprite.y;
  316. midPoint.put();
  317. }
  318. if (Radius < 1)
  319. {
  320. var minVal = Math.min(sprite.frameWidth, sprite.frameHeight);
  321. Radius = (minVal / 2);
  322. }
  323. beginDraw(FillColor, lineStyle);
  324. flashGfx.drawCircle(X, Y, Radius);
  325. endDraw(sprite, drawStyle);
  326. return sprite;
  327. }
  328. /**
  329. * This function draws an ellipse on a FlxSprite.
  330. *
  331. * @param sprite The FlxSprite to manipulate
  332. * @param X X coordinate of the ellipse's start point.
  333. * @param Y Y coordinate of the ellipse's start point.
  334. * @param Width Width of the ellipse
  335. * @param Height Height of the ellipse
  336. * @param FillColor The ARGB color to fill this ellipse with. FlxColor.TRANSPARENT (0x0) means no fill.
  337. * @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
  338. * @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
  339. * @return The FlxSprite for chaining
  340. */
  341. public static function drawEllipse(sprite:FlxSprite, X:Float, Y:Float, Width:Float, Height:Float,
  342. FillColor:FlxColor = FlxColor.WHITE, ?lineStyle:LineStyle, ?drawStyle:DrawStyle):FlxSprite
  343. {
  344. beginDraw(FillColor, lineStyle);
  345. flashGfx.drawEllipse(X, Y, Width, Height);
  346. endDraw(sprite, drawStyle);
  347. return sprite;
  348. }
  349. /**
  350. * This function draws a simple, equilateral triangle on a FlxSprite.
  351. *
  352. * @param sprite The FlxSprite to manipulate
  353. * @param X X position of the triangle
  354. * @param Y Y position of the triangle
  355. * @param Height Height of the triangle
  356. * @param FillColor The ARGB color to fill this triangle with. FlxColor.TRANSPARENT (0x0) means no fill.
  357. * @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
  358. * @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
  359. * @return The FlxSprite for chaining
  360. */
  361. public static function drawTriangle(sprite:FlxSprite, X:Float, Y:Float, Height:Float,
  362. FillColor:FlxColor = FlxColor.WHITE, ?lineStyle:LineStyle, ?drawStyle:DrawStyle):FlxSprite
  363. {
  364. beginDraw(FillColor, lineStyle);
  365. flashGfx.moveTo(X + Height / 2, Y);
  366. flashGfx.lineTo(X + Height, Height + Y);
  367. flashGfx.lineTo(X, Height + Y);
  368. flashGfx.lineTo(X + Height / 2, Y);
  369. endDraw(sprite, drawStyle);
  370. return sprite;
  371. }
  372. /**
  373. * This function draws a polygon on a FlxSprite.
  374. *
  375. * @param sprite The FlxSprite to manipulate
  376. * @param Vertices Array of Vertices to use for drawing the polygon
  377. * @param FillColor The ARGB color to fill this polygon with. FlxColor.TRANSPARENT (0x0) means no fill.
  378. * @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
  379. * @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
  380. * @return The FlxSprite for chaining
  381. */
  382. public static function drawPolygon(sprite:FlxSprite, Vertices:Array<FlxPoint>,
  383. FillColor:FlxColor = FlxColor.WHITE, ?lineStyle:LineStyle, ?drawStyle:DrawStyle):FlxSprite
  384. {
  385. beginDraw(FillColor, lineStyle);
  386. var p:FlxPoint = Vertices.shift();
  387. flashGfx.moveTo(p.x, p.y);
  388. for (p in Vertices)
  389. {
  390. flashGfx.lineTo(p.x, p.y);
  391. }
  392. endDraw(sprite, drawStyle);
  393. Vertices.unshift(p);
  394. return sprite;
  395. }
  396. /**
  397. * Helper function that the drawing functions use at the start to set the color and lineStyle.
  398. *
  399. * @param FillColor The ARGB color to use for drawing
  400. * @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
  401. */
  402. @:noUsing
  403. public static inline function beginDraw(FillColor:FlxColor, ?lineStyle:LineStyle):Void
  404. {
  405. flashGfx.clear();
  406. setLineStyle(lineStyle);
  407. if (FillColor != FlxColor.TRANSPARENT)
  408. {
  409. flashGfx.beginFill(FillColor.to24Bit(), FillColor.alphaFloat);
  410. }
  411. }
  412. /**
  413. * Helper function that the drawing functions use at the end.
  414. *
  415. * @param sprite The FlxSprite to draw to
  416. * @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
  417. * @return The FlxSprite for chaining
  418. */
  419. public static inline function endDraw(sprite:FlxSprite, ?drawStyle:DrawStyle):FlxSprite
  420. {
  421. flashGfx.endFill();
  422. updateSpriteGraphic(sprite, drawStyle);
  423. return sprite;
  424. }
  425. /**
  426. * Just a helper function that is called at the end of the draw functions
  427. * to handle a few things related to updating a sprite's graphic.
  428. *
  429. * @param Sprite The FlxSprite to manipulate
  430. * @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
  431. * @return The FlxSprite for chaining
  432. */
  433. public static function updateSpriteGraphic(sprite:FlxSprite, ?drawStyle:DrawStyle):FlxSprite
  434. {
  435. if (drawStyle == null)
  436. {
  437. drawStyle = { smoothing: false };
  438. }
  439. else if (drawStyle.smoothing == null)
  440. {
  441. drawStyle.smoothing = false;
  442. }
  443. sprite.pixels.draw(flashGfxSprite, drawStyle.matrix, drawStyle.colorTransform,
  444. drawStyle.blendMode, drawStyle.clipRect, drawStyle.smoothing);
  445. sprite.dirty = true;
  446. return sprite;
  447. }
  448. /**
  449. * Just a helper function that is called in the draw functions
  450. * to set the lineStyle via Graphics.lineStyle()
  451. *
  452. * @param lineStyle The lineStyle typedef
  453. */
  454. @:noUsing
  455. public static inline function setLineStyle(lineStyle:LineStyle):Void
  456. {
  457. if (lineStyle != null)
  458. {
  459. var color = (lineStyle.color == null) ? FlxColor.BLACK : lineStyle.color;
  460. if (lineStyle.thickness == null)
  461. lineStyle.thickness = 1;
  462. if (lineStyle.pixelHinting == null)
  463. lineStyle.pixelHinting = false;
  464. if (lineStyle.miterLimit == null)
  465. lineStyle.miterLimit = 3;
  466. flashGfx.lineStyle(
  467. lineStyle.thickness,
  468. color.to24Bit(),
  469. color.alphaFloat,
  470. lineStyle.pixelHinting,
  471. lineStyle.scaleMode,
  472. lineStyle.capsStyle,
  473. lineStyle.jointStyle,
  474. lineStyle.miterLimit);
  475. }
  476. }
  477. /**
  478. * Helper function for the default line styles of drawLine() and drawCurve()
  479. *
  480. * @param lineStyle The lineStyle typedef
  481. */
  482. public static inline function getDefaultLineStyle(?lineStyle:LineStyle):LineStyle
  483. {
  484. if (lineStyle == null)
  485. lineStyle = { thickness: 1, color: FlxColor.WHITE };
  486. if (lineStyle.thickness == null)
  487. lineStyle.thickness = 1;
  488. if (lineStyle.color == null)
  489. lineStyle.color = FlxColor.WHITE;
  490. return lineStyle;
  491. }
  492. /**
  493. * Fills this sprite's graphic with a specific color.
  494. *
  495. * @param Sprite The FlxSprite to manipulate
  496. * @param FillColor The color with which to fill the graphic, format 0xAARRGGBB.
  497. * @return The FlxSprite for chaining
  498. */
  499. public static function fill(sprite:FlxSprite, FillColor:FlxColor):FlxSprite
  500. {
  501. sprite.pixels.fillRect(sprite.pixels.rect, FillColor);
  502. if (sprite.pixels != sprite.framePixels)
  503. {
  504. sprite.dirty = true;
  505. }
  506. return sprite;
  507. }
  508. /**
  509. * A simple flicker effect for sprites achieved by toggling visibility.
  510. *
  511. * @param Object The sprite.
  512. * @param Duration How long to flicker for. 0 means "forever".
  513. * @param Interval In what interval to toggle visibility. Set to FlxG.elapsed if <= 0!
  514. * @param EndVisibility Force the visible value when the flicker completes, useful with fast repetitive use.
  515. * @param ForceRestart Force the flicker to restart from beginnig, discarding the flickering effect already in progress if there is one.
  516. * @param CompletionCallback An optional callback that will be triggered when a flickering has finished.
  517. * @param ProgressCallback An optional callback that will be triggered when visibility is toggled.
  518. * @return The FlxFlicker object. FlxFlickers are pooled internally, so beware of storing references.
  519. */
  520. public static inline function flicker(Object:FlxObject, Duration:Float = 1, Interval:Float = 0.04, EndVisibility:Bool = true,
  521. ForceRestart:Bool = true, ?CompletionCallback:FlxFlicker->Void, ?ProgressCallback:FlxFlicker->Void):FlxFlicker
  522. {
  523. return FlxFlicker.flicker(Object, Duration, Interval, EndVisibility, ForceRestart, CompletionCallback, ProgressCallback);
  524. }
  525. /**
  526. * Returns whether an object is flickering or not.
  527. *
  528. * @param Object The object to check against.
  529. */
  530. public static inline function isFlickering(Object:FlxObject):Bool
  531. {
  532. return FlxFlicker.isFlickering(Object);
  533. }
  534. /**
  535. * Stops flickering of the object. Also it will make the object visible.
  536. *
  537. * @param Object The object to stop flickering.
  538. * @return The FlxObject for chaining
  539. */
  540. public static inline function stopFlickering(Object:FlxObject):FlxObject
  541. {
  542. FlxFlicker.stopFlickering(Object);
  543. return Object;
  544. }
  545. /**
  546. * Fade in a sprite, tweening alpha to 1.
  547. *
  548. * @param sprite The object to fade.
  549. * @param Duration How long the fade will take (in seconds).
  550. * @return The FlxSprite for chaining
  551. */
  552. public static inline function fadeIn(sprite:FlxSprite, Duration:Float = 1, ?ResetAlpha:Bool, ?OnComplete:TweenCallback):FlxSprite
  553. {
  554. if (ResetAlpha)
  555. {
  556. sprite.alpha = 0;
  557. }
  558. FlxTween.num(sprite.alpha, 1, Duration, { onComplete: OnComplete }, alphaTween.bind(sprite));
  559. return sprite;
  560. }
  561. /**
  562. * Fade out a sprite, tweening alpha to 0.
  563. *
  564. * @param sprite The object to fade.
  565. * @param Duration How long the fade will take (in seconds).
  566. * @return The FlxSprite for chaining
  567. */
  568. public static inline function fadeOut(sprite:FlxSprite, Duration:Float = 1, ?OnComplete:TweenCallback):FlxSprite
  569. {
  570. FlxTween.num(sprite.alpha, 0, Duration, { onComplete: OnComplete }, alphaTween.bind(sprite));
  571. return sprite;
  572. }
  573. private static function alphaTween(sprite:FlxSprite, f:Float):Void
  574. {
  575. sprite.alpha = f;
  576. }
  577. }
  578. typedef LineStyle =
  579. {
  580. ?thickness:Float,
  581. ?color:FlxColor,
  582. ?pixelHinting:Bool,
  583. ?scaleMode:LineScaleMode,
  584. ?capsStyle:CapsStyle,
  585. ?jointStyle:JointStyle,
  586. ?miterLimit:Float
  587. }
  588. typedef DrawStyle =
  589. {
  590. ?matrix:Matrix,
  591. ?colorTransform:ColorTransform,
  592. ?blendMode:BlendMode,
  593. ?clipRect:Rectangle,
  594. ?smoothing:Bool
  595. }