Flow.hx 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
  1. package h2d;
  2. /**
  3. `Flow` content alignment rules.
  4. **/
  5. enum FlowAlign {
  6. /**
  7. Aligns children to the top edge of the `Flow`.
  8. Only applicable to the `Flow.verticalAlign`.
  9. **/
  10. Top;
  11. /**
  12. Aligns children to the left edge of the `Flow`.
  13. Only applicable to the `Flow.horizontalAlign`.
  14. **/
  15. Left;
  16. /**
  17. Aligns children to the right edge of the `Flow`.
  18. Only applicable to the `Flow.horizontalAlign`.
  19. **/
  20. Right;
  21. /**
  22. Aligns children to the center of the `Flow`.
  23. **/
  24. Middle;
  25. /**
  26. Aligns children to the bottom edge of the `Flow`.
  27. Only applicable to the `Flow.verticalAlign`.
  28. **/
  29. Bottom;
  30. }
  31. /**
  32. The `Flow.layout` type.
  33. **/
  34. enum FlowLayout {
  35. /**
  36. Children are aligned horizontally from left to right (or right to left when `Flow.reverse` is enabled).
  37. If `Flow.multiline` is enabled - children can overflow to the next row if there is not enough space available within the Flow size constraints.
  38. `Flow.lineHeight` can be used to set a fixed row height when `Flow.overflow` is set to `Limit` or `Hidden`.
  39. Objects with height that exceed the limitation will be aligned according to `Flow.verticalAlign` value vertically with `null` being treated as `Bottom`.
  40. **/
  41. Horizontal;
  42. /**
  43. Children are aligned vertically from top to bottom (or bottom to top when `Flow.reverse` is enabled).
  44. If `Flow.multiline` is enabled - children can overflow to the next column if there is not enough space available within the Flow size constraints.
  45. `Flow.colWidth` can be used to set a fixed column width when `Flow.overflow` is set to `Limit` or `Hidden`.
  46. Objects with height that exceed the limitation will be aligned according to `Flow.horizontalAlign` value horizontally with `null` being treated as `Left`.
  47. **/
  48. Vertical;
  49. /**
  50. Children are aligned independently (`Flow.reverse` has no effect).
  51. **/
  52. Stack;
  53. }
  54. /**
  55. The `Flow.overflow` rules.
  56. **/
  57. enum FlowOverflow {
  58. /**
  59. Children larger than `Flow.maxWidth` / `Flow.maxHeight` will expand the flow size.
  60. **/
  61. Expand;
  62. /**
  63. Limits the bounds reported by the flow using `Flow.maxWidth` or `Flow.maxHeight`, if set.
  64. Children larger than max size will draw outside of the Flow bounds or overflow if `Flow.multiline` is enabled.
  65. **/
  66. Limit;
  67. /**
  68. Limits the bounds reported by the flow using `Flow.maxWidth` or `Flow.maxHeight`, if set.
  69. Compared to `Limit` - Flow will mask out the content that is outside of Flow bounds.
  70. **/
  71. Hidden;
  72. /**
  73. Similar to `Hidden` but allows to scroll using `Flow.scroll*` properties for control.
  74. **/
  75. Scroll;
  76. }
  77. /**
  78. An individual `Flow` element properties.
  79. Can be obtained after adding the element to the Flow and calling `Flow.getProperties`.
  80. Contains configuration unique of each Flow element.
  81. **/
  82. @:allow(h2d.Flow)
  83. class FlowProperties {
  84. var elt : Object;
  85. /**
  86. An extra padding to the left of the flow element.
  87. **/
  88. public var paddingLeft = 0;
  89. /**
  90. An extra padding to the top of the flow element.
  91. **/
  92. public var paddingTop = 0;
  93. /**
  94. An extra padding to the right of the flow element.
  95. **/
  96. public var paddingRight = 0;
  97. /**
  98. An extra padding to the bottom of the flow element.
  99. **/
  100. public var paddingBottom = 0;
  101. /**
  102. When enabled, element won't be automatically positioned during `Flow.reflow` and
  103. instead treated as an absolute element relative to the Flow.
  104. **/
  105. public var isAbsolute(default,set) = false;
  106. /**
  107. The `Flow.horizontalAlign` override.
  108. If `FlowProperties.isAbsolute` is enabled - aligns the element within the Flow boundaries.
  109. Otherwise affects the element alignment within the Flow. Does not affect the alignment if `Flow.layout` is `Horizontal`.
  110. **/
  111. public var horizontalAlign : Null<FlowAlign>;
  112. /**
  113. The `Flow.verticalAlign` override.
  114. If `FlowProperties.isAbsolute` is enabled - aligns the element within the Flow boundaries.
  115. Otherwise affects the element alignment within the Flow. Does not affect the alignment if `Flow.layout` is `Vertical`.
  116. **/
  117. public var verticalAlign : Null<FlowAlign>;
  118. /**
  119. A visual offset of the element along the X axis.
  120. Offset does not affect the occupied space by the element, and can lead to overlapping with other elements.
  121. **/
  122. public var offsetX = 0;
  123. /**
  124. A visual offset of the element along the Y axis.
  125. Offset does not affect the occupied space by the element, and can lead to overlapping with other elements.
  126. **/
  127. public var offsetY = 0;
  128. /**
  129. The minimum occupied width of the element within the flow.
  130. **/
  131. public var minWidth : Null<Int>;
  132. /**
  133. The minimum occupied height of the element within the flow.
  134. **/
  135. public var minHeight : Null<Int>;
  136. /**
  137. The calculated element width since last element reflow.
  138. **/
  139. public var calculatedWidth(default,null) : Int = 0;
  140. /**
  141. The calculated element height since last element reflow.
  142. **/
  143. public var calculatedHeight(default,null) : Int = 0;
  144. /**
  145. Whether this element is the last on its current row/column, and the next flow element being on the next row/column after overflow.
  146. **/
  147. public var isBreak(default,null) : Bool;
  148. /**
  149. Forces this element to break the line and flow onto the next row/column.
  150. `Flow.multiline` is not required to be enabled.
  151. **/
  152. public var lineBreak = false;
  153. /**
  154. Will constraint the element size through `Object.constraintSize` if the Flow have a set maximum size.
  155. @see `Flow.maxWidth`
  156. @see `Flow.maxHeight`
  157. **/
  158. public var constraint = true;
  159. /**
  160. When set, element will use the maximum size of non-autoSize elements as size constraint instead of current constraint on the parent flow.
  161. **/
  162. public var autoSize(never, set) : Null<Float>;
  163. public var autoSizeWidth : Null<Float>;
  164. public var autoSizeHeight : Null<Float>;
  165. @:dox(hide)
  166. public function new(elt) {
  167. this.elt = elt;
  168. }
  169. /**
  170. Shortcut to set both `FlowProperties.verticalAlign` and `FlowProperties.horizontalAlign`.
  171. **/
  172. public inline function align(vertical, horizontal) {
  173. this.verticalAlign = vertical;
  174. this.horizontalAlign = horizontal;
  175. }
  176. function set_isAbsolute(a) {
  177. if( a ) {
  178. @:privateAccess elt.constraintSize( -1, -1); // remove constraint
  179. isBreak = false;
  180. }
  181. return isAbsolute = a;
  182. }
  183. function set_autoSize(s) {
  184. autoSizeWidth = s;
  185. autoSizeHeight = s;
  186. return s;
  187. }
  188. }
  189. /**
  190. An automatic layout system.
  191. **/
  192. class Flow extends Object {
  193. var tmpBounds = new h2d.col.Bounds();
  194. /**
  195. If some sub element gets resized, you need to set reflow to true in order to force
  196. the reflow of elements. You can also directly call `Flow.reflow` which will immediately
  197. update all elements positions.
  198. If a reflow is needed, `Flow.reflow` will be called before rendering the flow.
  199. Each change in one of the flow properties or addition/removal of elements will set needReflow to true.
  200. **/
  201. public var needReflow(default, set) : Bool = true;
  202. /**
  203. Horizontal alignment of elements inside the flow.
  204. See `FlowAlign` for more details.
  205. **/
  206. public var horizontalAlign(default, set) : Null<FlowAlign>;
  207. /**
  208. Vertical alignment of elements inside the flow.
  209. See `FlowAlign` for more details.
  210. **/
  211. public var verticalAlign(default,set) : Null<FlowAlign>;
  212. /**
  213. Ensures that Flow is at least the specified outer width at all times when not null.
  214. **/
  215. public var minWidth(default, set) : Null<Int>;
  216. /**
  217. Ensures that Flow is at least the specified outer height at all times when not null.
  218. **/
  219. public var minHeight(default, set) : Null<Int>;
  220. /**
  221. Attempts to limit the Flow outer width to the specified width.
  222. Used as a baseline for overflow when `Flow.multiline` is enabled and `Flow.layout` is `Horizontal`.
  223. **/
  224. public var maxWidth(default, set) : Null<Int>;
  225. /**
  226. Attempts to limit the Flow outer height to the specified height.
  227. Used as a baseline for overflow when `Flow.multiline` is enabled and `Flow.layout` is `Vertical`.
  228. **/
  229. public var maxHeight(default, set) : Null<Int>;
  230. /**
  231. Sets the minimum row height when `Flow.layout` is `Horizontal`.
  232. **/
  233. public var lineHeight(default, set) : Null<Int>;
  234. /**
  235. Sets the minimum colum width when `Flow.layout` is `Vertical`.
  236. **/
  237. public var colWidth(default, set) : Null<Int>;
  238. /**
  239. Enabling overflow will treat maxWidth/maxHeight and lineHeight/colWidth constraints as absolute : bigger elements will overflow instead of expanding the limit.
  240. See respective `FlowOverflow` values for more details.
  241. **/
  242. public var overflow(default, set) : FlowOverflow = Expand;
  243. /**
  244. Will set all padding values at the same time.
  245. Note that padding is applied inside the flow boundaries and included in the size constraint, shrinking available space for Flow children.
  246. @see `Flow.paddingLeft`
  247. @see `Flow.paddingRight`
  248. @see `Flow.paddingTop`
  249. @see `Flow.paddingBottom`
  250. @see `Flow.paddingHorizontal`
  251. @see `Flow.paddingVertical`
  252. **/
  253. public var padding(never, set) : Int;
  254. /**
  255. Will set `Flow.paddingLeft` and `Flow.paddingRight` to the given value.
  256. Note that padding is applied inside the flow boundaries and included in the size constraint, shrinking available space for Flow children.
  257. **/
  258. public var paddingHorizontal(never, set) : Int;
  259. /**
  260. Will set `Flow.paddingTop` and `Flow.paddingBottom` to the given value.
  261. Note that padding is applied inside the flow boundaries and included in the size constraint, shrinking available space for Flow children.
  262. **/
  263. public var paddingVertical(never, set) : Int;
  264. /**
  265. Sets the extra padding along the left edge of the Flow.
  266. Note that padding is applied inside the flow boundaries and included in the size constraint, shrinking available space for Flow children.
  267. **/
  268. public var paddingLeft(default, set) : Int = 0;
  269. /**
  270. Sets the extra padding along the right edge of the Flow.
  271. Note that padding is applied inside the flow boundaries and included in the size constraint, shrinking available space for Flow children.
  272. **/
  273. public var paddingRight(default, set) : Int = 0;
  274. /**
  275. Sets the extra padding along the top edge of the Flow.
  276. Note that padding is applied inside the flow boundaries and included in the size constraint, shrinking available space for Flow children.
  277. **/
  278. public var paddingTop(default, set) : Int = 0;
  279. /**
  280. Sets the extra padding along the bottom edge of the Flow.
  281. Note that padding is applied inside the flow boundaries and included in the size constraint, shrinking available space for Flow children.
  282. **/
  283. public var paddingBottom(default, set) : Int = 0;
  284. /**
  285. The horizontal separation spacing between two flowed elements.
  286. **/
  287. public var horizontalSpacing(default, set) : Int = 0;
  288. /**
  289. The vertical separation spacing between two flowed elements.
  290. **/
  291. public var verticalSpacing(default, set) : Int = 0;
  292. /**
  293. Adds an `h2d.Interactive` to the Flow that is accessible through `Flow.interactive` field.
  294. This Interactive is automatically resized to cover the whole Flow area.
  295. Flow is added as a bottom-most (after the `Flow.backgroundTile`) child as to not impede flow elements with Interactives.
  296. **/
  297. public var enableInteractive(default, set) : Bool;
  298. /**
  299. @see `Flow.enableInteractive`.
  300. **/
  301. public var interactive(default, null) : h2d.Interactive;
  302. /**
  303. Setting a background tile will create an `h2d.ScaleGrid` background which uses the `Flow.borderWidth`/`Flow.borderHeigh` values for its borders.
  304. It will automatically resize when the reflow is done to cover the whole Flow area.
  305. **/
  306. public var backgroundTile(default, set) : h2d.Tile;
  307. /**
  308. Set the border width of the `Flow.backgroundTile`'s left and right borders.
  309. Does not affect padding by default, which can be enabled with `-D flow_border` compilation flag.
  310. If border padding is enabled, `Flow.outerWidth` will be affected accordingly even if background tile is not set
  311. and will follow the same constraint limitation as padding.
  312. @see `Flow.paddingLeft`
  313. @see `Flow.paddingRight`
  314. @see `Flow.paddingHorizontal`
  315. @see `h2d.ScaleGrid.borderWidth`
  316. **/
  317. public var borderWidth(never, set) : Int;
  318. /**
  319. Left border width of the `Flow.backgroundTile`.
  320. Does not affect padding by default, which can be enabled with `-D flow_border` compilation flag.
  321. If border padding is enabled, `Flow.outerHeight` will be affected accordingly even if background tile is not set
  322. and will follow the same constraint limitation as padding.
  323. @see `Flow.paddingLeft`
  324. @see `h2d.ScaleGrid.borderLeft`
  325. **/
  326. public var borderLeft(default, set) : Int = 0;
  327. /**
  328. Right border width of the `Flow.backgroundTile`.
  329. Does not affect padding by default, which can be enabled with `-D flow_border` compilation flag.
  330. If border padding is enabled, `Flow.outerHeight` will be affected accordingly even if background tile is not set
  331. and will follow the same constraint limitation as padding.
  332. @see `Flow.paddingRight`
  333. @see `h2d.ScaleGrid.borderRight`
  334. **/
  335. public var borderRight(default, set) : Int = 0;
  336. /**
  337. Set the border height of the `Flow.backgroundTile`'s top and bottom borders.
  338. Does not affect padding by default, which can be enabled with `-D flow_border` compilation flag.
  339. If border padding is enabled, `Flow.outerHeight` will be affected accordingly even if background tile is not set
  340. and will follow the same constraint limitation as padding.
  341. @see `Flow.paddingTop`
  342. @see `Flow.paddingBottom`
  343. @see `Flow.paddingVertical`
  344. @see `h2d.ScaleGrid.borderHeight`
  345. **/
  346. public var borderHeight(never, set) : Int;
  347. /**
  348. Top border width of the `Flow.backgroundTile`.
  349. Does not affect padding by default, which can be enabled with `-D flow_border` compilation flag.
  350. If border padding is enabled, `Flow.outerHeight` will be affected accordingly even if background tile is not set
  351. and will follow the same constraint limitation as padding.
  352. @see `Flow.paddingTop`
  353. @see `h2d.ScaleGrid.borderTop`
  354. **/
  355. public var borderTop(default, set) : Int = 0;
  356. /**
  357. Bottom border width of the `Flow.backgroundTile`.
  358. Does not affect padding by default, which can be enabled with `-D flow_border` compilation flag.
  359. If border padding is enabled, `Flow.outerHeight` will be affected accordingly even if background tile is not set
  360. and will follow the same constraint limitation as padding.
  361. @see `Flow.paddingBottom`
  362. @see `h2d.ScaleGrid.borderBottom`
  363. **/
  364. public var borderBottom(default, set) : Int = 0;
  365. /**
  366. Calculate the client width, which is the inner size of the flow without the borders and padding.
  367. @see `Flow.padding`
  368. **/
  369. public var innerWidth(get, never) : Int;
  370. /**
  371. Calculate the client height, which is the inner size of the flow without the borders and padding.
  372. @see `Flow.padding`
  373. **/
  374. public var innerHeight(get, never) : Int;
  375. /**
  376. Flow total width. Compared to `Flow.innerWidth`, it also includes paddings and, if enabled, borders (see `Flow.borderWidth`).
  377. @see `Flow.padding`
  378. **/
  379. public var outerWidth(get, never) : Int;
  380. /**
  381. Flow total height Compared to `Flow.innerHeight`, it also includes paddings and, if enabled, borders (see `Flow.borderHeight`).
  382. @see `Flow.padding`
  383. **/
  384. public var outerHeight(get, never) : Int;
  385. /**
  386. The Flow item layout rules.
  387. See `FlowLayout` for specific details on each mode.
  388. **/
  389. public var layout(default, set) : FlowLayout = Horizontal;
  390. @:deprecated("isVertical is replaced by layout=Vertical")
  391. @:dox(hide)
  392. public var isVertical(get, set) : Bool;
  393. /**
  394. When isInline is set to false, the flow size will be reported based on its bounds instead of its calculated size.
  395. @see `Object.getSize`
  396. **/
  397. public var isInline = true;
  398. /**
  399. When set to true, the Flow will display a debug overlay.
  400. * Red box around the flow
  401. * Green box for the client space.
  402. * Blue boxes for each element.
  403. **/
  404. public var debug(default, set) : Bool;
  405. /**
  406. When set to true, uses specified lineHeight/colWidth instead of maxWidth/maxHeight for alignment.
  407. **/
  408. public var multiline(default,set) : Bool = false;
  409. /**
  410. When set to true, children are aligned in reverse order.
  411. Note that it does not affect render ordering, and may cause overlap of elements due to them positioned in reverse order.
  412. **/
  413. public var reverse(default,set) : Bool = false;
  414. /**
  415. When set to true, if a width constraint is present and `minWidth` is null - Flow will expand to fill all the available horizontal space
  416. **/
  417. public var fillWidth(default,set) : Bool = false;
  418. /**
  419. When set to true, if a height constraint is present and `minHeight` is null - Flow will expand to fill all the available vertical space
  420. **/
  421. public var fillHeight(default,set) : Bool = false;
  422. /**
  423. The scroll bar component created when `overflow` is set to `Scroll`
  424. **/
  425. public var scrollBar(default, null) : h2d.Flow;
  426. /**
  427. The scroll bar cursor component created when `overflow` is set to `Scroll`
  428. **/
  429. public var scrollBarCursor(default, null) : h2d.Flow;
  430. /**
  431. The amount of scrolling that is done when using mouse wheel (in pixels).
  432. **/
  433. public var scrollWheelSpeed : Float = 30.;
  434. /**
  435. The current scrolling position for the flow content (in pixels). Only applies when overflow is Scroll or Hidden.
  436. **/
  437. public var scrollPosY(default, set) : Float = 0.;
  438. var background : h2d.ScaleGrid;
  439. var debugGraphics : h2d.Graphics;
  440. var properties : Array<FlowProperties> = [];
  441. var calculatedWidth : Float = 0.;
  442. var calculatedHeight : Float = 0.;
  443. var contentWidth : Float = 0.;
  444. var contentHeight : Float = 0.;
  445. var constraintWidth : Float = -1;
  446. var constraintHeight : Float = -1;
  447. var realMaxWidth : Float = -1;
  448. var realMaxHeight : Float = -1;
  449. var realMinWidth : Int = -1;
  450. var realMinHeight : Int = -1;
  451. var isConstraint : Bool;
  452. /**
  453. Create a new Flow instance.
  454. @param parent An optional parent `h2d.Object` instance to which Flow adds itself if set.
  455. **/
  456. public function new(?parent) {
  457. super(parent);
  458. }
  459. /**
  460. Get the per-element properties. Returns null if the element is not currently part of the Flow.
  461. Requesting the properties will cause a reflow regardless if properties values were changed or not.
  462. **/
  463. public function getProperties( e : h2d.Object ) {
  464. needReflow = true; // properties might be changed
  465. return properties[getChildIndex(e)];
  466. }
  467. inline function flowCeil( f : Float ) {
  468. return hxd.Math.ceil(f - hxd.Math.EPSILON);
  469. }
  470. inline function flowFloor( f : Float ) {
  471. return hxd.Math.floor(f + hxd.Math.EPSILON);
  472. }
  473. function set_layout(v) {
  474. if(layout == v)
  475. return v;
  476. needReflow = true;
  477. return layout = v == null ? Horizontal : v;
  478. }
  479. function get_isVertical() {
  480. return layout == Vertical;
  481. }
  482. function set_isVertical(v) {
  483. layout = v ? Vertical : Horizontal;
  484. return v;
  485. }
  486. function set_horizontalAlign(v) {
  487. if( horizontalAlign == v )
  488. return v;
  489. needReflow = true;
  490. return horizontalAlign = v;
  491. }
  492. function set_debug(v) {
  493. if( debug == v )
  494. return v;
  495. needReflow = true;
  496. if( v ) {
  497. debugGraphics = new h2d.Graphics(this);
  498. getProperties(debugGraphics).isAbsolute = true;
  499. } else {
  500. debugGraphics.remove();
  501. debugGraphics = null;
  502. }
  503. return debug = v;
  504. }
  505. function set_verticalAlign(v) {
  506. if( verticalAlign == v )
  507. return v;
  508. needReflow = true;
  509. return verticalAlign = v;
  510. }
  511. function makeScrollBar(): h2d.Flow {
  512. var bar = new h2d.Flow();
  513. bar.backgroundTile = h2d.Tile.fromColor(0);
  514. bar.alpha = 0.5;
  515. return bar;
  516. }
  517. function makeScrollBarCursor(): h2d.Flow {
  518. var cursor = new h2d.Flow();
  519. cursor.minWidth = 10;
  520. cursor.minHeight = 20;
  521. cursor.backgroundTile = h2d.Tile.fromColor(-1);
  522. return cursor;
  523. }
  524. function set_overflow(v) {
  525. if( overflow == v )
  526. return v;
  527. needReflow = true;
  528. if( v == Scroll ) {
  529. enableInteractive = true;
  530. if( scrollBar == null ) {
  531. scrollBar = makeScrollBar();
  532. addChild(scrollBar);
  533. scrollBar.verticalAlign = Top;
  534. scrollBar.enableInteractive = true;
  535. function setCursor( e : hxd.Event) {
  536. var cursorY = e.relY - scrollBarCursor.minHeight * 0.5;
  537. if( cursorY < 0 ) cursorY = 0;
  538. scrollPosY = (cursorY / (scrollBar.minHeight - scrollBarCursor.minHeight)) * (contentHeight - calculatedHeight);
  539. }
  540. var pushed = false;
  541. scrollBar.interactive.cursor = Button;
  542. scrollBar.interactive.onPush = function(e:hxd.Event) {
  543. var scene = getScene();
  544. if( scene == null ) return;
  545. scrollBar.interactive.startCapture(function(e) {
  546. switch( e.kind ) {
  547. case ERelease, EReleaseOutside:
  548. scene.stopCapture();
  549. case EPush, EMove:
  550. setCursor(e);
  551. default:
  552. }
  553. e.propagate = false;
  554. });
  555. setCursor(e);
  556. };
  557. var p = getProperties(scrollBar);
  558. p.isAbsolute = true;
  559. p.horizontalAlign = Right;
  560. p.verticalAlign = Top;
  561. scrollBarCursor = makeScrollBarCursor();
  562. scrollBar.addChild(scrollBarCursor);
  563. }
  564. } else {
  565. if( scrollBar != null ) {
  566. scrollBar.remove();
  567. scrollBar = null;
  568. scrollBarCursor = null;
  569. }
  570. }
  571. return overflow = v;
  572. }
  573. function set_multiline(v) {
  574. if( multiline == v )
  575. return v;
  576. needReflow = true;
  577. return multiline = v;
  578. }
  579. function set_reverse(v) {
  580. if( reverse == v )
  581. return v;
  582. needReflow = true;
  583. return reverse = v;
  584. }
  585. function set_needReflow(v) {
  586. if( needReflow == v )
  587. return v;
  588. if( v )
  589. onContentChanged();
  590. return needReflow = v;
  591. }
  592. function set_lineHeight(v) {
  593. if( lineHeight == v )
  594. return v;
  595. needReflow = true;
  596. return lineHeight = v;
  597. }
  598. function set_colWidth(v) {
  599. if( colWidth == v )
  600. return v;
  601. needReflow = true;
  602. return colWidth = v;
  603. }
  604. function set_padding(v) {
  605. paddingLeft = v;
  606. paddingTop = v;
  607. paddingRight = v;
  608. paddingBottom = v;
  609. return v;
  610. }
  611. function set_scrollPosY(v:Float) {
  612. if( needReflow ) reflow();
  613. if( v < 0 ) v = 0;
  614. if( v > contentHeight - calculatedHeight ) v = contentHeight - calculatedHeight;
  615. if( scrollPosY == v )
  616. return v;
  617. var delta = Std.int(v) - Std.int(scrollPosY);
  618. var i = 0;
  619. for( c in children ) {
  620. var p = properties[i++];
  621. if( p.isAbsolute ) continue;
  622. c.y -= delta;
  623. }
  624. scrollPosY = v;
  625. updateScrollCursor();
  626. return v;
  627. }
  628. function updateScrollCursor() {
  629. if( scrollBarCursor == null ) return;
  630. var prev = needReflow;
  631. var p = scrollBar.getProperties(scrollBarCursor);
  632. p.paddingTop = Std.int( scrollPosY * (calculatedHeight - scrollBarCursor.minHeight) / (contentHeight - calculatedHeight) );
  633. needReflow = prev;
  634. }
  635. inline function set_paddingHorizontal(v) {
  636. paddingLeft = v;
  637. paddingRight = v;
  638. return v;
  639. }
  640. inline function set_paddingVertical(v) {
  641. paddingTop = v;
  642. paddingBottom = v;
  643. return v;
  644. }
  645. function get_outerWidth() {
  646. if( needReflow ) reflow();
  647. return flowCeil(calculatedWidth);
  648. }
  649. function get_outerHeight() {
  650. if( needReflow ) reflow();
  651. return flowCeil(calculatedHeight);
  652. }
  653. function get_innerWidth() {
  654. if( needReflow ) reflow();
  655. return flowCeil(calculatedWidth) - (paddingLeft + paddingRight #if flow_border + (borderLeft + borderRight) #end);
  656. }
  657. function get_innerHeight() {
  658. if( needReflow ) reflow();
  659. return flowCeil(calculatedHeight) - (paddingTop + paddingBottom #if flow_border + (borderTop + borderBottom) #end);
  660. }
  661. function set_paddingLeft(v) {
  662. if( paddingLeft == v ) return v;
  663. needReflow = true;
  664. return paddingLeft = v;
  665. }
  666. function set_paddingRight(v) {
  667. if( paddingRight == v ) return v;
  668. needReflow = true;
  669. return paddingRight = v;
  670. }
  671. function set_paddingTop(v) {
  672. if( paddingTop == v ) return v;
  673. needReflow = true;
  674. return paddingTop = v;
  675. }
  676. function set_paddingBottom(v) {
  677. if( paddingBottom == v ) return v;
  678. needReflow = true;
  679. return paddingBottom = v;
  680. }
  681. function set_fillWidth(v) {
  682. if( fillWidth == v )
  683. return v;
  684. needReflow = true;
  685. return fillWidth = v;
  686. }
  687. function set_fillHeight(v) {
  688. if( fillHeight == v )
  689. return v;
  690. needReflow = true;
  691. return fillHeight = v;
  692. }
  693. override function constraintSize( width, height ) {
  694. constraintWidth = width;
  695. constraintHeight = height;
  696. isConstraint = true;
  697. updateConstraint();
  698. }
  699. override function onHierarchyMoved(parentChanged:Bool) {
  700. super.onHierarchyMoved(parentChanged);
  701. isConstraint = false;
  702. constraintWidth = -1;
  703. constraintHeight = -1;
  704. updateConstraint();
  705. }
  706. override function contentChanged( s : Object ) {
  707. while( s.parent != this )
  708. s = s.parent;
  709. var p = getProperties(s);
  710. if( p != null && p.isAbsolute )
  711. return;
  712. needReflow = true;
  713. onContentChanged();
  714. }
  715. /**
  716. Adds some spacing by either increasing the padding of the latest
  717. non-absolute element or the padding of the flow if there are no elements in it.
  718. The padding affected depends on the `Flow.layout` mode.
  719. It's impossible to add spacing with a `Stack` Flow layout.
  720. **/
  721. public function addSpacing( v : Int ) {
  722. var last = properties.length - 1;
  723. while( last >= 0 && properties[last].isAbsolute )
  724. last--;
  725. switch (layout) {
  726. case Horizontal:
  727. if( last >= 0 )
  728. properties[last].paddingRight += v;
  729. else
  730. paddingLeft += v;
  731. case Vertical:
  732. if( last >= 0 )
  733. properties[last].paddingBottom += v;
  734. else
  735. paddingTop += v;
  736. case Stack:
  737. }
  738. }
  739. override function getBoundsRec( relativeTo, out, forSize ) {
  740. if( needReflow ) reflow();
  741. if( forSize ) {
  742. if( !isInline )
  743. super.getBoundsRec(relativeTo, out, true);
  744. if( calculatedWidth != 0 ) {
  745. if( posChanged ) {
  746. calcAbsPos();
  747. for( c in children )
  748. c.posChanged = true;
  749. posChanged = false;
  750. }
  751. addBounds(relativeTo, out, 0, 0, calculatedWidth, calculatedHeight);
  752. }
  753. } else
  754. super.getBoundsRec(relativeTo, out, forSize);
  755. }
  756. override function setParentContainer(c) {
  757. parentContainer = c;
  758. // break propagation
  759. }
  760. override function addChildAt( s, pos ) {
  761. if( background != null ) pos++;
  762. if( interactive != null ) pos++;
  763. if( scrollBar != null && pos == children.length ) pos--;
  764. var fp = getProperties(s);
  765. super.addChildAt(s, pos);
  766. if( fp == null ) fp = new FlowProperties(s) else properties.remove(fp);
  767. properties.insert(pos, fp);
  768. needReflow = true;
  769. s.setParentContainer(this);
  770. }
  771. override public function removeChild(s:Object) {
  772. var index = getChildIndex(s);
  773. super.removeChild(s);
  774. if( index >= 0 ) {
  775. needReflow = true;
  776. properties.splice(index, 1);
  777. s.constraintSize( -1, -1); // remove constraint
  778. }
  779. if( s != null ) {
  780. if( s == background )
  781. backgroundTile = null;
  782. if( s == interactive )
  783. enableInteractive = false;
  784. }
  785. }
  786. override function removeChildren() {
  787. var k = 0;
  788. while( numChildren > k ) {
  789. var c = getChildAt(k);
  790. if( c == background
  791. || c == interactive
  792. || c == debugGraphics
  793. || c == scrollBar ) k++; else removeChild(c);
  794. }
  795. }
  796. override function sync(ctx:RenderContext) {
  797. if( !isConstraint && (fillWidth || fillHeight) ) {
  798. var scene = ctx.scene;
  799. var cw = fillWidth ? scene.width : -1;
  800. var ch = fillHeight ? scene.height : -1;
  801. if( cw != constraintWidth || ch != constraintHeight ) needReflow = true;
  802. }
  803. if( needReflow ) reflow();
  804. super.sync(ctx);
  805. }
  806. override function drawRec(ctx:RenderContext) {
  807. if( overflow == Hidden || overflow == Scroll ) {
  808. if( posChanged ) {
  809. calcAbsPos();
  810. for ( c in children )
  811. c.posChanged = true;
  812. posChanged = false;
  813. }
  814. Mask.maskWith(ctx, this, flowCeil(calculatedWidth), flowCeil(calculatedHeight), 0, 0);
  815. super.drawRec(ctx);
  816. Mask.unmask(ctx);
  817. } else {
  818. super.drawRec(ctx);
  819. }
  820. }
  821. function set_maxWidth(w) {
  822. if( maxWidth == w )
  823. return w;
  824. maxWidth = w;
  825. updateConstraint();
  826. return w;
  827. }
  828. function set_maxHeight(h) {
  829. if( maxHeight == h )
  830. return h;
  831. maxHeight = h;
  832. updateConstraint();
  833. return h;
  834. }
  835. function updateConstraint() {
  836. var oldW = realMaxWidth, oldH = realMaxHeight;
  837. realMaxWidth = if( maxWidth == null ) constraintWidth else if( constraintWidth < 0 ) maxWidth else hxd.Math.min(maxWidth, constraintWidth);
  838. realMaxHeight = if( maxHeight == null ) constraintHeight else if( constraintHeight < 0 ) maxHeight else hxd.Math.min(maxHeight, constraintHeight);
  839. if( minWidth != null && realMaxWidth < minWidth && realMaxWidth >= 0 )
  840. realMaxWidth = minWidth;
  841. if( minHeight != null && realMaxHeight < minHeight && realMaxWidth >= 0 )
  842. realMaxHeight = minHeight;
  843. if( realMaxWidth != oldW || realMaxHeight != oldH )
  844. needReflow = true;
  845. var oldW = realMinWidth, oldH = realMinHeight;
  846. realMinWidth = if(fillWidth) hxd.Math.imax(flowCeil(constraintWidth), minWidth != null ? minWidth : -1) else if( minWidth != null ) minWidth else -1;
  847. realMinHeight = if(fillHeight) hxd.Math.imax(flowCeil(constraintHeight), minHeight != null ? minHeight : -1) else if( minHeight != null ) minHeight else -1;
  848. if(realMinWidth != oldW || realMinHeight != oldH)
  849. needReflow = true;
  850. }
  851. function set_minWidth(w) {
  852. if( minWidth == w )
  853. return w;
  854. needReflow = true;
  855. minWidth = w;
  856. updateConstraint();
  857. return w;
  858. }
  859. function set_minHeight(h) {
  860. if( minHeight == h )
  861. return h;
  862. needReflow = true;
  863. minHeight = h;
  864. updateConstraint();
  865. return h;
  866. }
  867. function set_horizontalSpacing(s) {
  868. if( horizontalSpacing == s )
  869. return s;
  870. needReflow = true;
  871. return horizontalSpacing = s;
  872. }
  873. function set_verticalSpacing(s) {
  874. if( verticalSpacing == s )
  875. return s;
  876. needReflow = true;
  877. return verticalSpacing = s;
  878. }
  879. function set_enableInteractive(b) {
  880. if( enableInteractive == b )
  881. return b;
  882. if( b ) {
  883. if( interactive == null ) {
  884. var interactive = new h2d.Interactive(0, 0);
  885. addChildAt(interactive,0);
  886. this.interactive = interactive;
  887. interactive.cursor = Default;
  888. getProperties(interactive).isAbsolute = true;
  889. if( !needReflow ) {
  890. interactive.width = calculatedWidth;
  891. interactive.height = calculatedHeight;
  892. }
  893. interactive.onWheel = onMouseWheel;
  894. }
  895. } else {
  896. if( interactive != null ) {
  897. interactive.remove();
  898. interactive = null;
  899. }
  900. }
  901. return enableInteractive = b;
  902. }
  903. function onMouseWheel( e : hxd.Event ) {
  904. if( overflow == Scroll ) {
  905. scrollPosY += e.wheelDelta * scrollWheelSpeed;
  906. e.propagate = false;
  907. }
  908. }
  909. function set_backgroundTile(t) {
  910. if( backgroundTile == t )
  911. return t;
  912. if( t != null ) {
  913. if( background == null ) {
  914. var background = new h2d.ScaleGrid(t, borderLeft, borderTop, borderRight, borderBottom);
  915. addChildAt(background, 0);
  916. getProperties(background).isAbsolute = true;
  917. this.background = background;
  918. if( !needReflow ) {
  919. background.width = flowCeil(calculatedWidth);
  920. background.height = flowCeil(calculatedHeight);
  921. }
  922. }
  923. background.tile = t;
  924. } else {
  925. if( background != null ) {
  926. background.remove();
  927. background = null;
  928. }
  929. }
  930. return backgroundTile = t;
  931. }
  932. function set_borderWidth(v) {
  933. if(borderLeft == v)
  934. return v;
  935. return borderLeft = borderRight = v;
  936. }
  937. function set_borderLeft(v) {
  938. if( background != null ) background.borderLeft = v;
  939. #if flow_border needReflow = true; #end
  940. return borderLeft = v;
  941. }
  942. function set_borderRight(v) {
  943. if( borderRight == v )
  944. return v;
  945. if( background != null ) background.borderRight = v;
  946. #if flow_border needReflow = true; #end
  947. return borderRight = v;
  948. }
  949. function set_borderHeight(v) {
  950. if(borderTop == v)
  951. return v;
  952. return borderTop = borderBottom = v;
  953. }
  954. function set_borderTop(v) {
  955. if( borderTop == v )
  956. return v;
  957. if( background != null ) background.borderTop = v;
  958. #if flow_border needReflow = true; #end
  959. return borderTop = v;
  960. }
  961. function set_borderBottom(v) {
  962. if( borderBottom == v )
  963. return v;
  964. if( background != null ) background.borderBottom = v;
  965. #if flow_border needReflow = true; #end
  966. return borderBottom = v;
  967. }
  968. /**
  969. Call to force all flowed elements position to be updated.
  970. See `Flow.needReflow` for more information.
  971. **/
  972. public function reflow() {
  973. onBeforeReflow();
  974. syncPos();
  975. if( !isConstraint && (fillWidth || fillHeight) ) {
  976. var scene = getScene();
  977. var cw = fillWidth ? scene.width : -1;
  978. var ch = fillHeight ? scene.height : -1;
  979. if( cw != constraintWidth || ch != constraintHeight ) {
  980. constraintSize(cw, ch);
  981. isConstraint = false;
  982. }
  983. }
  984. var borderTop = #if flow_border borderTop #else 0 #end;
  985. var borderBottom = #if flow_border borderBottom #else 0 #end;
  986. var borderLeft = #if flow_border borderLeft #else 0 #end;
  987. var borderRight = #if flow_border borderRight #else 0 #end;
  988. var tmpBounds = tmpBounds;
  989. if( tmpBounds == null ) throw "Recursive reflow";
  990. this.tmpBounds = null;
  991. inline function getSize(c:h2d.Object) {
  992. var b = tmpBounds;
  993. b.empty();
  994. c.getBoundsRec(this, b, true);
  995. if( b.isEmpty() ) b.addPos(0,0) else b.offset(-c.x, -c.y);
  996. return b;
  997. }
  998. var isConstraintWidth = realMaxWidth >= 0;
  999. var isConstraintHeight = realMaxHeight >= 0;
  1000. // outer size
  1001. var maxTotWidth = realMaxWidth < 0 ? 100000000 : flowFloor(realMaxWidth);
  1002. var maxTotHeight = realMaxHeight < 0 ? 100000000 : flowFloor(realMaxHeight);
  1003. // inner size
  1004. var maxInWidth = maxTotWidth - (paddingLeft + paddingRight + (borderLeft + borderRight));
  1005. var maxInHeight = maxTotHeight - (paddingTop + paddingBottom + (borderTop + borderBottom));
  1006. if( debug )
  1007. debugGraphics.clear();
  1008. inline function childAt(i: Int) {
  1009. return children[ reverse ? children.length - i - 1 : i ];
  1010. }
  1011. inline function propAt(i: Int) {
  1012. return properties[ reverse ? children.length - i - 1 : i ];
  1013. }
  1014. inline function forChildren(func : Int->FlowProperties->h2d.Object->Void) {
  1015. for( i in 0...children.length ) {
  1016. var p = propAt(i);
  1017. if( p.isAbsolute && p.horizontalAlign == null && p.verticalAlign == null ) continue;
  1018. var c = childAt(i);
  1019. if( !c.visible ) continue;
  1020. func(i, p, c);
  1021. }
  1022. }
  1023. var cw, ch;
  1024. switch(layout) {
  1025. case Horizontal:
  1026. var halign = horizontalAlign == null ? Left : horizontalAlign;
  1027. var valign = verticalAlign == null ? Bottom : verticalAlign;
  1028. var startX = paddingLeft + borderLeft;
  1029. var x = startX;
  1030. var y = paddingTop + borderTop;
  1031. cw = x;
  1032. var maxLineHeight = 0;
  1033. var minLineHeight = this.lineHeight != null ? lineHeight : (this.realMinHeight >= 0 && !multiline) ? (this.realMinHeight - (paddingTop + paddingBottom + borderTop + borderBottom)) : 0;
  1034. var lastIndex = 0;
  1035. inline function alignLine( maxIndex ) {
  1036. if( maxLineHeight < minLineHeight )
  1037. maxLineHeight = minLineHeight;
  1038. else if( overflow != Expand && minLineHeight != 0 )
  1039. maxLineHeight = minLineHeight;
  1040. var absHeight = maxLineHeight > maxInHeight && overflow != Expand ? maxInHeight : maxLineHeight;
  1041. for( i in lastIndex...maxIndex ) {
  1042. var p = propAt(i);
  1043. if( p.isAbsolute && p.verticalAlign == null ) continue;
  1044. var c = childAt(i);
  1045. if( !c.visible ) continue;
  1046. var a = p.verticalAlign != null ? p.verticalAlign : valign;
  1047. c.y = y + p.offsetY + p.paddingTop;
  1048. var height = p.isAbsolute ? absHeight : maxLineHeight;
  1049. switch( a ) {
  1050. case Bottom:
  1051. c.y += height - Std.int(p.calculatedHeight);
  1052. case Middle:
  1053. c.y += Std.int((height - p.calculatedHeight) * 0.5);
  1054. default:
  1055. }
  1056. }
  1057. lastIndex = maxIndex;
  1058. }
  1059. inline function remSize(from: Int) {
  1060. var size = 0;
  1061. for( j in from...children.length ) {
  1062. var p = propAt(j);
  1063. if( p.isAbsolute || !childAt(j).visible ) continue;
  1064. if( p.isBreak ) break;
  1065. size += horizontalSpacing + p.calculatedWidth;
  1066. }
  1067. return size;
  1068. }
  1069. var autoWidth = maxInWidth;
  1070. var autoSum = 0.0;
  1071. inline function calcSize(p : FlowProperties, c : h2d.Object) {
  1072. var pw = p.paddingLeft + p.paddingRight;
  1073. var ph = p.paddingTop + p.paddingBottom;
  1074. if( !p.isAbsolute )
  1075. c.constraintSize(
  1076. isConstraintWidth && p.constraint ? ((p.autoSizeWidth != null ? flowFloor(autoWidth * p.autoSizeWidth / autoSum) : maxInWidth) - pw) / Math.abs(c.scaleX) : -1,
  1077. isConstraintHeight && p.constraint ? ((p.autoSizeHeight != null ? hxd.Math.imax(maxLineHeight, minLineHeight) * p.autoSizeHeight : maxInHeight) - ph) / Math.abs(c.scaleY) : -1
  1078. );
  1079. var b = getSize(c);
  1080. p.calculatedWidth = flowCeil(b.xMax) + pw;
  1081. p.calculatedHeight = flowCeil(b.yMax) + ph;
  1082. if( p.minWidth != null && p.calculatedWidth < p.minWidth ) p.calculatedWidth = p.minWidth;
  1083. if( p.minHeight != null && p.calculatedHeight < p.minHeight ) p.calculatedHeight = p.minHeight;
  1084. }
  1085. var count = 0;
  1086. forChildren(function(i, p, c) {
  1087. if(count > 0 && !p.isAbsolute) autoWidth -= horizontalSpacing;
  1088. if(p.autoSizeWidth == null) {
  1089. calcSize(p, c);
  1090. if(!p.isAbsolute) {
  1091. if( p.calculatedHeight > maxLineHeight ) maxLineHeight = p.calculatedHeight;
  1092. autoWidth -= p.calculatedWidth;
  1093. }
  1094. }
  1095. else
  1096. autoSum += p.autoSizeWidth;
  1097. count++;
  1098. });
  1099. forChildren(function(i, p, c) {
  1100. if(p.autoSizeWidth != null || p.autoSizeHeight != null)
  1101. calcSize(p, c);
  1102. if(!p.isAbsolute) {
  1103. var br = false;
  1104. if( ((multiline && x - startX + p.calculatedWidth > maxInWidth) || p.lineBreak) && x - startX > 0 ) {
  1105. br = true;
  1106. alignLine(i);
  1107. y += maxLineHeight + verticalSpacing;
  1108. maxLineHeight = 0;
  1109. x = startX;
  1110. }
  1111. p.isBreak = br;
  1112. x += p.calculatedWidth;
  1113. if( x > cw ) cw = x;
  1114. x += horizontalSpacing;
  1115. if( p.calculatedHeight > maxLineHeight ) maxLineHeight = p.calculatedHeight;
  1116. }
  1117. });
  1118. alignLine(children.length);
  1119. cw += paddingRight + borderRight;
  1120. ch = y + maxLineHeight + paddingBottom + borderBottom;
  1121. // horizontal align
  1122. if( realMinWidth >= 0 && cw < realMinWidth ) cw = realMinWidth;
  1123. var endX = cw - (paddingRight + borderRight);
  1124. var xmin = startX, xmax = endX;
  1125. var midSpace = 0, curAlign = null;
  1126. for( i in 0...children.length ) {
  1127. var p = propAt(i);
  1128. var c = childAt(i);
  1129. if( !c.visible ) continue;
  1130. if( p.isAbsolute ) {
  1131. switch( p.horizontalAlign ) {
  1132. case null:
  1133. case Right:
  1134. c.x = endX - p.calculatedWidth + p.offsetX;
  1135. case Left:
  1136. c.x = startX + p.offsetX;
  1137. case Middle:
  1138. c.x = startX + Std.int((endX - startX - p.calculatedWidth) * 0.5) + p.offsetX + startX;
  1139. default:
  1140. }
  1141. continue;
  1142. }
  1143. if( p.isBreak ) {
  1144. xmin = startX;
  1145. xmax = endX;
  1146. midSpace = 0;
  1147. }
  1148. var px;
  1149. var align = p.horizontalAlign == null ? halign : p.horizontalAlign;
  1150. if( curAlign != align ) {
  1151. curAlign = align;
  1152. midSpace = 0;
  1153. }
  1154. switch( align ) {
  1155. case Right:
  1156. if( midSpace == 0 ) {
  1157. var remSize = p.calculatedWidth + remSize(i + 1);
  1158. midSpace = (xmax - xmin) - remSize;
  1159. xmin += midSpace;
  1160. }
  1161. px = xmin;
  1162. xmin += p.calculatedWidth + horizontalSpacing;
  1163. case Middle:
  1164. if( midSpace == 0 ) {
  1165. var remSize = p.calculatedWidth + remSize(i + 1);
  1166. midSpace = Std.int(((xmax - xmin) - remSize) * 0.5);
  1167. xmin += midSpace;
  1168. }
  1169. px = xmin;
  1170. xmin += p.calculatedWidth + horizontalSpacing;
  1171. default:
  1172. px = xmin;
  1173. xmin += p.calculatedWidth + horizontalSpacing;
  1174. }
  1175. c.x = px + p.offsetX + p.paddingLeft;
  1176. if( p.isAbsolute ) xmin = px;
  1177. }
  1178. case Vertical:
  1179. var halign = horizontalAlign == null ? Left : horizontalAlign;
  1180. var valign = verticalAlign == null ? Top : verticalAlign;
  1181. var startY = paddingTop + borderTop;
  1182. var y = startY;
  1183. var x = paddingLeft + borderLeft;
  1184. ch = y;
  1185. var maxColWidth = 0;
  1186. var minColWidth = this.colWidth != null ? colWidth : (this.realMinWidth >= 0 && !multiline) ? (this.realMinWidth - (paddingLeft + paddingRight + borderLeft + borderRight)) : 0;
  1187. var lastIndex = 0;
  1188. inline function alignLine( maxIndex ) {
  1189. if( maxColWidth < minColWidth )
  1190. maxColWidth = minColWidth;
  1191. else if( overflow != Expand && minColWidth != 0 )
  1192. maxColWidth = minColWidth;
  1193. var absWidth = maxColWidth > maxInWidth && overflow != Expand ? maxInWidth : maxColWidth;
  1194. for( i in lastIndex...maxIndex ) {
  1195. var p = propAt(i);
  1196. if( p.isAbsolute && p.horizontalAlign == null ) continue;
  1197. var c = childAt(i);
  1198. if( !c.visible ) continue;
  1199. var a = p.horizontalAlign != null ? p.horizontalAlign : halign;
  1200. c.x = x + p.offsetX + p.paddingLeft;
  1201. var width = p.isAbsolute ? absWidth : maxColWidth;
  1202. switch( a ) {
  1203. case Right:
  1204. c.x += width - p.calculatedWidth;
  1205. case Middle:
  1206. c.x += Std.int((width - p.calculatedWidth) * 0.5);
  1207. default:
  1208. }
  1209. }
  1210. lastIndex = maxIndex;
  1211. }
  1212. inline function remSize(from: Int) {
  1213. var size = 0;
  1214. for( j in from...children.length ) {
  1215. var p = propAt(j);
  1216. if( p.isAbsolute || !childAt(j).visible ) continue;
  1217. if( p.isBreak ) break;
  1218. size += verticalSpacing + p.calculatedHeight;
  1219. }
  1220. return size;
  1221. }
  1222. var autoHeight = maxInHeight;
  1223. var autoSum = 0.0;
  1224. inline function calcSize(p : FlowProperties, c : h2d.Object) {
  1225. var pw = p.paddingLeft + p.paddingRight;
  1226. var ph = p.paddingTop + p.paddingBottom;
  1227. if( !p.isAbsolute )
  1228. c.constraintSize(
  1229. isConstraintWidth && p.constraint ? ((p.autoSizeWidth != null ? hxd.Math.imax(maxColWidth, minColWidth) * p.autoSizeWidth : maxInWidth) - pw) / Math.abs(c.scaleX) : -1,
  1230. isConstraintHeight && p.constraint ? ((p.autoSizeHeight != null ? flowFloor(autoHeight * p.autoSizeHeight / autoSum) : maxInHeight) - ph) / Math.abs(c.scaleY) : -1
  1231. );
  1232. var b = getSize(c);
  1233. p.calculatedWidth = flowCeil(b.xMax) + pw;
  1234. p.calculatedHeight = flowCeil(b.yMax) + ph;
  1235. if( p.minWidth != null && p.calculatedWidth < p.minWidth ) p.calculatedWidth = p.minWidth;
  1236. if( p.minHeight != null && p.calculatedHeight < p.minHeight ) p.calculatedHeight = p.minHeight;
  1237. }
  1238. var count = 0;
  1239. forChildren(function(i, p, c) {
  1240. if(count > 0 && !p.isAbsolute) autoHeight -= verticalSpacing;
  1241. if(p.autoSizeHeight == null) {
  1242. calcSize(p, c);
  1243. if(!p.isAbsolute) {
  1244. if( p.calculatedWidth > maxColWidth ) maxColWidth = p.calculatedWidth;
  1245. autoHeight -= p.calculatedHeight;
  1246. }
  1247. }
  1248. else
  1249. autoSum += p.autoSizeHeight;
  1250. count++;
  1251. });
  1252. forChildren(function(i, p, c) {
  1253. if(p.autoSizeWidth != null || p.autoSizeHeight != null)
  1254. calcSize(p, c);
  1255. if(!p.isAbsolute) {
  1256. var br = false;
  1257. if( ((multiline && y - startY + p.calculatedHeight > maxInHeight) || p.lineBreak) && y - startY > 0 ) {
  1258. br = true;
  1259. alignLine(i);
  1260. x += maxColWidth + horizontalSpacing;
  1261. maxColWidth = 0;
  1262. y = startY;
  1263. }
  1264. p.isBreak = br;
  1265. c.y = y + p.offsetY + p.paddingTop;
  1266. y += p.calculatedHeight;
  1267. if( y > ch ) ch = y;
  1268. y += verticalSpacing;
  1269. if( p.calculatedWidth > maxColWidth ) maxColWidth = p.calculatedWidth;
  1270. }
  1271. });
  1272. alignLine(children.length);
  1273. ch += paddingBottom + borderBottom;
  1274. cw = x + maxColWidth + paddingRight + borderRight;
  1275. // vertical align
  1276. if( realMinHeight >= 0 && ch < realMinHeight ) ch = realMinHeight;
  1277. var endY : Int = ch - (paddingBottom + borderBottom);
  1278. var ymin = startY, ymax = endY;
  1279. var midSpace = 0, curAlign = null;
  1280. for( i in 0...children.length ) {
  1281. var p = propAt(i);
  1282. var c = childAt(i);
  1283. if( !c.visible )
  1284. continue;
  1285. if( p.isAbsolute ) {
  1286. switch( p.verticalAlign ) {
  1287. case null:
  1288. case Bottom:
  1289. c.y = endY - p.calculatedHeight + p.offsetY;
  1290. case Top:
  1291. c.y = startY + p.offsetY;
  1292. case Middle:
  1293. c.y = startY + Std.int((endY - startY - p.calculatedHeight) * 0.5) + p.offsetY + startY;
  1294. default:
  1295. }
  1296. continue;
  1297. }
  1298. if( p.isBreak ) {
  1299. ymin = startY;
  1300. ymax = endY;
  1301. midSpace = 0;
  1302. }
  1303. var py;
  1304. var align = p.verticalAlign == null ? valign : p.verticalAlign;
  1305. if( curAlign != align ) {
  1306. curAlign = align;
  1307. midSpace = 0;
  1308. }
  1309. switch( align ) {
  1310. case Bottom:
  1311. if( midSpace == 0 ) {
  1312. var remSize = p.calculatedHeight + remSize(i + 1);
  1313. midSpace = (ymax - ymin) - remSize;
  1314. ymin += midSpace;
  1315. }
  1316. py = ymin;
  1317. ymin += p.calculatedHeight + verticalSpacing;
  1318. case Middle:
  1319. if( midSpace == 0 ) {
  1320. var remSize = p.calculatedHeight + remSize(i + 1);
  1321. midSpace = Std.int(((ymax - ymin) - remSize) * 0.5);
  1322. ymin += midSpace;
  1323. }
  1324. py = ymin;
  1325. ymin += p.calculatedHeight + verticalSpacing;
  1326. default:
  1327. py = ymin;
  1328. ymin += p.calculatedHeight + verticalSpacing;
  1329. }
  1330. c.y = py + p.offsetY + p.paddingTop;
  1331. }
  1332. case Stack:
  1333. var halign = horizontalAlign == null ? Left : horizontalAlign;
  1334. var valign = verticalAlign == null ? Top : verticalAlign;
  1335. var maxChildW = 0;
  1336. var maxChildH = 0;
  1337. for( i in 0...children.length ) {
  1338. var c = childAt(i);
  1339. if( !c.visible ) continue;
  1340. var p = propAt(i);
  1341. var isAbs = p.isAbsolute;
  1342. if( isAbs && p.verticalAlign == null && p.horizontalAlign == null ) continue;
  1343. var pw = p.paddingLeft + p.paddingRight;
  1344. var ph = p.paddingTop + p.paddingBottom;
  1345. if( !isAbs )
  1346. c.constraintSize(
  1347. isConstraintWidth && p.constraint ? (maxInWidth - pw) / Math.abs(c.scaleX) : -1,
  1348. isConstraintHeight && p.constraint ? (maxInHeight - ph) / Math.abs(c.scaleY) : -1
  1349. );
  1350. var b = getSize(c);
  1351. p.calculatedWidth = flowCeil(b.xMax) + pw;
  1352. p.calculatedHeight = flowCeil(b.yMax) + ph;
  1353. if( p.minWidth != null && p.calculatedWidth < p.minWidth ) p.calculatedWidth = p.minWidth;
  1354. if( p.minHeight != null && p.calculatedHeight < p.minHeight ) p.calculatedHeight = p.minHeight;
  1355. if( isAbs ) continue;
  1356. if( p.calculatedWidth > maxChildW ) maxChildW = p.calculatedWidth;
  1357. if( p.calculatedHeight > maxChildH ) maxChildH = p.calculatedHeight;
  1358. }
  1359. var xmin = paddingLeft + borderLeft;
  1360. var ymin = paddingTop + borderTop;
  1361. var xmax = if(realMaxWidth > 0 && overflow != Expand) flowFloor(realMaxWidth - (paddingRight + borderRight))
  1362. else hxd.Math.imax(xmin + maxChildW, realMinWidth - (paddingRight + borderRight));
  1363. var ymax = if(realMaxWidth > 0 && overflow != Expand) flowFloor(realMaxHeight - (paddingBottom + borderBottom))
  1364. else hxd.Math.imax(ymin + maxChildH, realMinHeight - (paddingBottom + borderBottom));
  1365. cw = xmax + paddingRight + borderRight;
  1366. ch = ymax + paddingBottom + borderBottom;
  1367. for( i in 0...children.length ) {
  1368. var c = childAt(i);
  1369. if( !c.visible ) continue;
  1370. var p = propAt(i);
  1371. var isAbs = p.isAbsolute;
  1372. if( isAbs && p.verticalAlign == null && p.horizontalAlign == null ) continue;
  1373. var valign = p.verticalAlign == null ? valign : p.verticalAlign;
  1374. var halign = p.horizontalAlign == null ? halign : p.horizontalAlign;
  1375. var px = switch( halign ) {
  1376. case Right:
  1377. xmax - p.calculatedWidth;
  1378. case Middle:
  1379. xmin + Std.int(((xmax - xmin) - p.calculatedWidth) * 0.5);
  1380. default:
  1381. xmin;
  1382. }
  1383. var py = switch( valign ) {
  1384. case Bottom:
  1385. ymax - p.calculatedHeight;
  1386. case Middle:
  1387. ymin + Std.int(((ymax - ymin) - p.calculatedHeight) * 0.5);
  1388. default:
  1389. ymin;
  1390. }
  1391. if( !isAbs || p.horizontalAlign != null )
  1392. c.x = px + p.offsetX + p.paddingLeft;
  1393. if( !isAbs || p.verticalAlign != null )
  1394. c.y = py + p.offsetY + p.paddingTop;
  1395. }
  1396. }
  1397. if( scrollPosY != 0 ) {
  1398. var i = 0;
  1399. var sy = Std.int(scrollPosY);
  1400. for( c in children ) {
  1401. var p = properties[i++];
  1402. if( p.isAbsolute ) continue;
  1403. c.y -= sy;
  1404. }
  1405. }
  1406. if( realMinWidth >= 0 && cw < realMinWidth ) cw = realMinWidth;
  1407. if( realMinHeight >= 0 && ch < realMinHeight ) ch = realMinHeight;
  1408. contentWidth = cw;
  1409. contentHeight = ch;
  1410. if( overflow != Expand ) {
  1411. if( isConstraintWidth && cw > maxTotWidth ) cw = maxTotWidth;
  1412. if( isConstraintHeight && ch > maxTotHeight ) ch = maxTotHeight;
  1413. }
  1414. if( interactive != null ) {
  1415. interactive.width = cw;
  1416. interactive.height = ch;
  1417. }
  1418. if( background != null ) {
  1419. background.width = flowCeil(cw);
  1420. background.height = flowCeil(ch);
  1421. }
  1422. calculatedWidth = cw;
  1423. calculatedHeight = ch;
  1424. if( scrollBar != null ) {
  1425. if( contentHeight <= calculatedHeight )
  1426. scrollBar.visible = false;
  1427. else {
  1428. scrollBar.visible = true;
  1429. scrollBar.minHeight = flowCeil(calculatedHeight);
  1430. scrollBarCursor.minHeight = hxd.Math.imax(1, Std.int(calculatedHeight * (1 - (contentHeight - calculatedHeight)/contentHeight)));
  1431. updateScrollCursor();
  1432. }
  1433. }
  1434. needReflow = false;
  1435. if( overflow == Scroll || overflow == Hidden )
  1436. posChanged = true;
  1437. if( debug ) {
  1438. if( debugGraphics != children[children.length - 1] ) {
  1439. addChild(debugGraphics); // always on-top
  1440. needReflow = false;
  1441. }
  1442. if( paddingLeft != 0 || paddingRight != 0 || paddingTop != 0 || paddingBottom != 0 ) {
  1443. debugGraphics.lineStyle(1, 0x00FF00);
  1444. debugGraphics.drawRect(paddingLeft, paddingTop, innerWidth, innerHeight);
  1445. }
  1446. debugGraphics.lineStyle(1, 0x0080FF);
  1447. for( i in 0...children.length ) {
  1448. var p = propAt(i);
  1449. var c = childAt(i);
  1450. if( p.isAbsolute || !c.visible ) continue;
  1451. debugGraphics.drawRect(c.x - p.offsetX - p.paddingLeft, c.y - p.offsetY - p.paddingTop, p.calculatedWidth, p.calculatedHeight);
  1452. }
  1453. debugGraphics.lineStyle(1, 0xFF0000);
  1454. debugGraphics.drawRect(0, 0, cw, ch);
  1455. }
  1456. this.tmpBounds = tmpBounds;
  1457. onAfterReflow();
  1458. }
  1459. /**
  1460. Sent at the start of the `Flow.reflow`.
  1461. **/
  1462. public dynamic function onBeforeReflow() {
  1463. }
  1464. /**
  1465. Sent after the `Flow.reflow` was finished.
  1466. **/
  1467. public dynamic function onAfterReflow() {
  1468. }
  1469. }