PageRenderTime 70ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 0ms

/flixel/util/FlxSpriteUtil.hx

https://github.com/Deathstroke7/flixel
Haxe | 692 lines | 367 code | 52 blank | 273 comment | 61 complexity | d4243d1fb2c65d6b0060a85452b2a048 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. lineStyle = getDefaultLineStyle(lineStyle);
  234. beginDraw(0x0, lineStyle);
  235. flashGfx.moveTo(StartX, StartY);
  236. flashGfx.lineTo(EndX, EndY);
  237. endDraw(sprite, drawStyle);
  238. return sprite;
  239. }
  240. /**
  241. * This function draws a curve on a FlxSprite from position X1,Y1
  242. * to anchor position X2,Y2 using control points X3,Y3 with the specified color.
  243. *
  244. * @param sprite The FlxSprite to manipulate
  245. * @param StartX X coordinate of the curve's start point.
  246. * @param StartY Y coordinate of the curve's start point.
  247. * @param EndX X coordinate of the curve's end/anchor point.
  248. * @param EndY Y coordinate of the curve's end/anchor point.
  249. * @param ControlX X coordinate of the curve's control point.
  250. * @param ControlY Y coordinate of the curve's control point.
  251. * @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.
  252. * @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
  253. * @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
  254. * @return The FlxSprite for chaining
  255. */
  256. public static function drawCurve(sprite:FlxSprite, StartX:Float, StartY:Float, EndX:Float, EndY:Float, ControlX:Float, ControlY:Float,
  257. FillColor:FlxColor = FlxColor.TRANSPARENT, ?lineStyle:LineStyle, ?drawStyle:DrawStyle):FlxSprite
  258. {
  259. lineStyle = getDefaultLineStyle(lineStyle);
  260. beginDraw(FillColor, lineStyle);
  261. flashGfx.moveTo(StartX, StartY);
  262. flashGfx.curveTo(EndX, EndY, ControlX, ControlY);
  263. endDraw(sprite, drawStyle);
  264. return sprite;
  265. }
  266. /**
  267. * This function draws a rectangle on a FlxSprite.
  268. *
  269. * @param sprite The FlxSprite to manipulate
  270. * @param X X coordinate of the rectangle's start point.
  271. * @param Y Y coordinate of the rectangle's start point.
  272. * @param Width Width of the rectangle
  273. * @param Height Height of the rectangle
  274. * @param FillColor The ARGB color to fill this rectangle with. FlxColor.TRANSPARENT (0x0) means no fill.
  275. * @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
  276. * @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
  277. * @return The FlxSprite for chaining
  278. */
  279. public static function drawRect(sprite:FlxSprite, X:Float, Y:Float, Width:Float, Height:Float,
  280. FillColor:FlxColor = FlxColor.WHITE, ?lineStyle:LineStyle, ?drawStyle:DrawStyle):FlxSprite
  281. {
  282. beginDraw(FillColor, lineStyle);
  283. flashGfx.drawRect(X, Y, Width, Height);
  284. endDraw(sprite, drawStyle);
  285. return sprite;
  286. }
  287. /**
  288. * This function draws a rounded rectangle on a FlxSprite.
  289. *
  290. * @param sprite The FlxSprite to manipulate
  291. * @param X X coordinate of the rectangle's start point.
  292. * @param Y Y coordinate of the rectangle's start point.
  293. * @param Width Width of the rectangle
  294. * @param Height Height of the rectangle
  295. * @param EllipseWidth The width of the ellipse used to draw the rounded corners
  296. * @param EllipseHeight The height of the ellipse used to draw the rounded corners
  297. * @param FillColor The ARGB color to fill this rectangle with. FlxColor.TRANSPARENT (0x0) means no fill.
  298. * @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
  299. * @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
  300. * @return The FlxSprite for chaining
  301. */
  302. public static function drawRoundRect(sprite:FlxSprite, X:Float, Y:Float, Width:Float, Height:Float, EllipseWidth:Float,
  303. EllipseHeight:Float, FillColor:FlxColor = FlxColor.WHITE, ?lineStyle:LineStyle, ?drawStyle:DrawStyle):FlxSprite
  304. {
  305. beginDraw(FillColor, lineStyle);
  306. flashGfx.drawRoundRect(X, Y, Width, Height, EllipseWidth, EllipseHeight);
  307. endDraw(sprite, drawStyle);
  308. return sprite;
  309. }
  310. #if flash
  311. /**
  312. * This function draws a rounded rectangle on a FlxSprite. Same as drawRoundRect,
  313. * except it allows you to determine the radius of each corner individually.
  314. *
  315. * @param sprite The FlxSprite to manipulate
  316. * @param X X coordinate of the rectangle's start point.
  317. * @param Y Y coordinate of the rectangle's start point.
  318. * @param Width Width of the rectangle
  319. * @param Height Height of the rectangle
  320. * @param TopLeftRadius The radius of the top left corner of the rectangle
  321. * @param TopRightRadius The radius of the top right corner of the rectangle
  322. * @param BottomLeftRadius The radius of the bottom left corner of the rectangle
  323. * @param BottomRightRadius The radius of the bottom right corner of the rectangle
  324. * @param FillColor The ARGB color to fill this rectangle with. FlxColor.TRANSPARENT (0x0) means no fill.
  325. * @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
  326. * @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
  327. * @return The FlxSprite for chaining
  328. */
  329. public static function drawRoundRectComplex(sprite:FlxSprite, X:Float, Y:Float, Width:Float, Height:Float,
  330. TopLeftRadius:Float, TopRightRadius:Float, BottomLeftRadius:Float, BottomRightRadius:Float,
  331. FillColor:FlxColor = FlxColor.WHITE, ?lineStyle:LineStyle, ?drawStyle:DrawStyle):FlxSprite
  332. {
  333. beginDraw(FillColor, lineStyle);
  334. flashGfx.drawRoundRectComplex(X, Y, Width, Height, TopLeftRadius, TopRightRadius, BottomLeftRadius, BottomRightRadius);
  335. endDraw(sprite, drawStyle);
  336. return sprite;
  337. }
  338. #end
  339. /**
  340. * This function draws a circle on a FlxSprite at position X,Y with the specified color.
  341. *
  342. * @param sprite The FlxSprite to manipulate
  343. * @param X X coordinate of the circle's center (automatically centered on the sprite if -1)
  344. * @param Y Y coordinate of the circle's center (automatically centered on the sprite if -1)
  345. * @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)
  346. * @param FillColor The ARGB color to fill this circle with. FlxColor.TRANSPARENT (0x0) means no fill.
  347. * @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
  348. * @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
  349. * @return The FlxSprite for chaining
  350. */
  351. public static function drawCircle(sprite:FlxSprite, X:Float = - 1, Y:Float = - 1, Radius:Float = -1,
  352. FillColor:FlxColor = FlxColor.WHITE, ?lineStyle:LineStyle, ?drawStyle:DrawStyle):FlxSprite
  353. {
  354. if ((X == -1) || (Y == -1))
  355. {
  356. var midPoint = sprite.getGraphicMidpoint();
  357. if (X == -1)
  358. X = midPoint.x;
  359. if (Y == -1)
  360. Y = midPoint.y;
  361. midPoint.put();
  362. }
  363. if (Radius < 1)
  364. {
  365. var minVal = Math.min(sprite.frameWidth, sprite.frameHeight);
  366. Radius = (minVal / 2);
  367. }
  368. beginDraw(FillColor, lineStyle);
  369. flashGfx.drawCircle(X, Y, Radius);
  370. endDraw(sprite, drawStyle);
  371. return sprite;
  372. }
  373. /**
  374. * This function draws an ellipse on a FlxSprite.
  375. *
  376. * @param sprite The FlxSprite to manipulate
  377. * @param X X coordinate of the ellipse's start point.
  378. * @param Y Y coordinate of the ellipse's start point.
  379. * @param Width Width of the ellipse
  380. * @param Height Height of the ellipse
  381. * @param FillColor The ARGB color to fill this ellipse with. FlxColor.TRANSPARENT (0x0) means no fill.
  382. * @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
  383. * @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
  384. * @return The FlxSprite for chaining
  385. */
  386. public static function drawEllipse(sprite:FlxSprite, X:Float, Y:Float, Width:Float, Height:Float,
  387. FillColor:FlxColor = FlxColor.WHITE, ?lineStyle:LineStyle, ?drawStyle:DrawStyle):FlxSprite
  388. {
  389. beginDraw(FillColor, lineStyle);
  390. flashGfx.drawEllipse(X, Y, Width, Height);
  391. endDraw(sprite, drawStyle);
  392. return sprite;
  393. }
  394. /**
  395. * This function draws a simple, equilateral triangle on a FlxSprite.
  396. *
  397. * @param sprite The FlxSprite to manipulate
  398. * @param X X position of the triangle
  399. * @param Y Y position of the triangle
  400. * @param Height Height of the triangle
  401. * @param FillColor The ARGB color to fill this triangle with. FlxColor.TRANSPARENT (0x0) means no fill.
  402. * @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
  403. * @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
  404. * @return The FlxSprite for chaining
  405. */
  406. public static function drawTriangle(sprite:FlxSprite, X:Float, Y:Float, Height:Float,
  407. FillColor:FlxColor = FlxColor.WHITE, ?lineStyle:LineStyle, ?drawStyle:DrawStyle):FlxSprite
  408. {
  409. beginDraw(FillColor, lineStyle);
  410. flashGfx.moveTo(X + Height / 2, Y);
  411. flashGfx.lineTo(X + Height, Height + Y);
  412. flashGfx.lineTo(X, Height + Y);
  413. flashGfx.lineTo(X + Height / 2, Y);
  414. endDraw(sprite, drawStyle);
  415. return sprite;
  416. }
  417. /**
  418. * This function draws a polygon on a FlxSprite.
  419. *
  420. * @param sprite The FlxSprite to manipulate
  421. * @param Vertices Array of Vertices to use for drawing the polygon
  422. * @param FillColor The ARGB color to fill this polygon with. FlxColor.TRANSPARENT (0x0) means no fill.
  423. * @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
  424. * @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
  425. * @return The FlxSprite for chaining
  426. */
  427. public static function drawPolygon(sprite:FlxSprite, Vertices:Array<FlxPoint>,
  428. FillColor:FlxColor = FlxColor.WHITE, ?lineStyle:LineStyle, ?drawStyle:DrawStyle):FlxSprite
  429. {
  430. beginDraw(FillColor, lineStyle);
  431. var p:FlxPoint = Vertices.shift();
  432. flashGfx.moveTo(p.x, p.y);
  433. for (p in Vertices)
  434. {
  435. flashGfx.lineTo(p.x, p.y);
  436. }
  437. endDraw(sprite, drawStyle);
  438. return sprite;
  439. }
  440. /**
  441. * Helper function that the drawing functions use at the start to set the color and lineStyle.
  442. *
  443. * @param FillColor The ARGB color to use for drawing
  444. * @param lineStyle A LineStyle typedef containing the params of Graphics.lineStyle()
  445. */
  446. @:noUsing
  447. public static inline function beginDraw(FillColor:FlxColor, ?lineStyle:LineStyle):Void
  448. {
  449. flashGfx.clear();
  450. setLineStyle(lineStyle);
  451. if (FillColor != FlxColor.TRANSPARENT)
  452. {
  453. flashGfx.beginFill(FillColor.to24Bit(), FillColor.alphaFloat);
  454. }
  455. }
  456. /**
  457. * Helper function that the drawing functions use at the end.
  458. *
  459. * @param sprite The FlxSprite to draw to
  460. * @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
  461. * @return The FlxSprite for chaining
  462. */
  463. public static inline function endDraw(sprite:FlxSprite, ?drawStyle:DrawStyle):FlxSprite
  464. {
  465. flashGfx.endFill();
  466. updateSpriteGraphic(sprite, drawStyle);
  467. return sprite;
  468. }
  469. /**
  470. * Just a helper function that is called at the end of the draw functions
  471. * to handle a few things related to updating a sprite's graphic.
  472. *
  473. * @param Sprite The FlxSprite to manipulate
  474. * @param drawStyle A DrawStyle typdef containing the params of BitmapData.draw()
  475. * @return The FlxSprite for chaining
  476. */
  477. public static function updateSpriteGraphic(sprite:FlxSprite, ?drawStyle:DrawStyle):FlxSprite
  478. {
  479. if (drawStyle == null)
  480. {
  481. drawStyle = { smoothing: false };
  482. }
  483. else if (drawStyle.smoothing == null)
  484. {
  485. drawStyle.smoothing = false;
  486. }
  487. sprite.pixels.draw(flashGfxSprite, drawStyle.matrix, drawStyle.colorTransform,
  488. drawStyle.blendMode, drawStyle.clipRect, drawStyle.smoothing);
  489. sprite.dirty = true;
  490. sprite.resetFrameBitmapDatas();
  491. return sprite;
  492. }
  493. /**
  494. * Just a helper function that is called in the draw functions
  495. * to set the lineStyle via Graphics.lineStyle()
  496. *
  497. * @param lineStyle The lineStyle typedef
  498. */
  499. @:noUsing
  500. public static inline function setLineStyle(lineStyle:LineStyle):Void
  501. {
  502. if (lineStyle != null)
  503. {
  504. var color = (lineStyle.color == null) ? FlxColor.BLACK : lineStyle.color;
  505. if (lineStyle.thickness == null)
  506. lineStyle.thickness = 1;
  507. if (lineStyle.pixelHinting == null)
  508. lineStyle.pixelHinting = false;
  509. if (lineStyle.miterLimit == null)
  510. lineStyle.miterLimit = 3;
  511. flashGfx.lineStyle(
  512. lineStyle.thickness,
  513. color.to24Bit(),
  514. color.alphaFloat,
  515. lineStyle.pixelHinting,
  516. lineStyle.scaleMode,
  517. lineStyle.capsStyle,
  518. lineStyle.jointStyle,
  519. lineStyle.miterLimit);
  520. }
  521. }
  522. /**
  523. * Helper function for the default line styles of drawLine() and drawCurve()
  524. *
  525. * @param lineStyle The lineStyle typedef
  526. */
  527. public static inline function getDefaultLineStyle(?lineStyle:LineStyle):LineStyle
  528. {
  529. if (lineStyle == null)
  530. lineStyle = { thickness: 1, color: FlxColor.WHITE };
  531. if (lineStyle.thickness == null)
  532. lineStyle.thickness = 1;
  533. if (lineStyle.color == null)
  534. lineStyle.color = FlxColor.WHITE;
  535. return lineStyle;
  536. }
  537. /**
  538. * Fills this sprite's graphic with a specific color.
  539. *
  540. * @param Sprite The FlxSprite to manipulate
  541. * @param FillColor The color with which to fill the graphic, format 0xAARRGGBB.
  542. * @return The FlxSprite for chaining
  543. */
  544. public static function fill(sprite:FlxSprite, FillColor:FlxColor):FlxSprite
  545. {
  546. sprite.pixels.fillRect(sprite.pixels.rect, FillColor);
  547. if (sprite.pixels != sprite.framePixels)
  548. {
  549. sprite.dirty = true;
  550. }
  551. sprite.resetFrameBitmapDatas();
  552. return sprite;
  553. }
  554. /**
  555. * A simple flicker effect for sprites achieved by toggling visibility.
  556. *
  557. * @param Object The sprite.
  558. * @param Duration How long to flicker for. 0 means "forever".
  559. * @param Interval In what interval to toggle visibility. Set to FlxG.elapsed if <= 0!
  560. * @param EndVisibility Force the visible value when the flicker completes, useful with fast repetitive use.
  561. * @param ForceRestart Force the flicker to restart from beginnig, discarding the flickering effect already in progress if there is one.
  562. * @param CompletionCallback An optional callback that will be triggered when a flickering has finished.
  563. * @param ProgressCallback An optional callback that will be triggered when visibility is toggled.
  564. * @return The FlxObject for chaining
  565. */
  566. public static inline function flicker(Object:FlxObject, Duration:Float = 1, Interval:Float = 0.04, EndVisibility:Bool = true,
  567. ForceRestart:Bool = true, ?CompletionCallback:FlxFlicker->Void, ?ProgressCallback:FlxFlicker->Void):FlxObject
  568. {
  569. FlxFlicker.flicker(Object, Duration, Interval, EndVisibility, ForceRestart, CompletionCallback, ProgressCallback);
  570. return Object;
  571. }
  572. /**
  573. * Returns whether an object is flickering or not.
  574. *
  575. * @param Object The object to check against.
  576. */
  577. public static inline function isFlickering(Object:FlxObject):Bool
  578. {
  579. return FlxFlicker.isFlickering(Object);
  580. }
  581. /**
  582. * Stops flickering of the object. Also it will make the object visible.
  583. *
  584. * @param Object The object to stop flickering.
  585. * @return The FlxObject for chaining
  586. */
  587. public static inline function stopFlickering(Object:FlxObject):FlxObject
  588. {
  589. FlxFlicker.stopFlickering(Object);
  590. return Object;
  591. }
  592. /**
  593. * Fade in a sprite, tweening alpha to 1.
  594. *
  595. * @param sprite The object to fade.
  596. * @param Duration How long the fade will take (in seconds).
  597. * @return The FlxSprite for chaining
  598. */
  599. public static inline function fadeIn(sprite:FlxSprite, Duration:Float = 1, ?ResetAlpha:Bool, ?OnComplete:CompleteCallback):FlxSprite
  600. {
  601. if (ResetAlpha)
  602. {
  603. sprite.alpha = 0;
  604. }
  605. FlxTween.num(sprite.alpha, 1, Duration, { complete: OnComplete }, alphaTween.bind(sprite));
  606. return sprite;
  607. }
  608. /**
  609. * Fade out a sprite, tweening alpha to 0.
  610. *
  611. * @param sprite The object to fade.
  612. * @param Duration How long the fade will take (in seconds).
  613. * @return The FlxSprite for chaining
  614. */
  615. public static inline function fadeOut(sprite:FlxSprite, Duration:Float = 1, ?OnComplete:CompleteCallback):FlxSprite
  616. {
  617. FlxTween.num(sprite.alpha, 0, Duration, { complete: OnComplete }, alphaTween.bind(sprite));
  618. return sprite;
  619. }
  620. private static function alphaTween(sprite:FlxSprite, f:Float):Void
  621. {
  622. sprite.alpha = f;
  623. }
  624. }
  625. typedef LineStyle = {
  626. ?thickness:Float,
  627. ?color:FlxColor,
  628. ?pixelHinting:Bool,
  629. ?scaleMode:LineScaleMode,
  630. ?capsStyle:CapsStyle,
  631. ?jointStyle:JointStyle,
  632. ?miterLimit:Float
  633. }
  634. typedef DrawStyle = {
  635. ?matrix:Matrix,
  636. ?colorTransform:ColorTransform,
  637. ?blendMode:BlendMode,
  638. ?clipRect:Rectangle,
  639. ?smoothing:Bool
  640. }