autoheightspacer.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // -----------------------------------------------------------
  2. class AutoHeightSpacer : ScriptedWidgetEventHandler
  3. {
  4. reference bool AlignChilds;
  5. reference int MinHeight;
  6. reference int MaxHeight;
  7. reference int Gap;
  8. reference float Top;
  9. protected Widget m_root;
  10. void Update()
  11. {
  12. float x = 0;
  13. float y = 0;
  14. float width = 0;
  15. float height = 0;
  16. float heightOld = 0;
  17. float top = Top;
  18. float rowRight;
  19. float rowHeight;
  20. float rowWidth;
  21. Widget child = m_root.GetChildren();
  22. //PrintString(m_root.GetName() + ": AutoHeightSpacer::Update()");
  23. if ( !AlignChilds ) top = -100000;
  24. if (child != NULL)
  25. {
  26. // first row init
  27. m_root.GetScreenSize(rowWidth, height);
  28. rowHeight = 0;
  29. rowRight = rowWidth;
  30. while (child)
  31. {
  32. if (child.IsVisible() == false || child.GetName() == "SelectedContainer" || child.GetName() == "Icon")
  33. {
  34. // skip invisible widgets
  35. child = child.GetSibling();
  36. continue;
  37. }
  38. child.GetScreenSize(width, height);
  39. if (AlignChilds)
  40. {
  41. child.SetFlags(WidgetFlags.EXACTPOS, false);
  42. // no space left in this row, move to next one
  43. if (rowRight < width)
  44. {
  45. top += rowHeight;
  46. if ( rowHeight > 0 ) top += Gap;
  47. rowRight = rowWidth;
  48. rowHeight = 0;
  49. }
  50. // increase row height if necessary
  51. if (height > rowHeight) rowHeight = height;
  52. child.SetPos(rowWidth - rowRight, top, false);
  53. rowRight -= width + Gap;
  54. }
  55. else
  56. {
  57. child.GetScreenPos(x, y);
  58. y += height;
  59. if (top < y) top = y;
  60. }
  61. child = child.GetSibling();
  62. }
  63. // add last row height;
  64. top += rowHeight;
  65. if (AlignChilds)
  66. {
  67. height = top;
  68. }
  69. else
  70. {
  71. m_root.GetScreenPos(x, y);
  72. height = top - y;
  73. }
  74. }
  75. m_root.GetSize(width, heightOld);
  76. if (MaxHeight > 0 && height > MaxHeight)
  77. {
  78. height = MaxHeight;
  79. }
  80. if (MinHeight > height)
  81. {
  82. height = MinHeight;
  83. }
  84. if (Math.AbsInt(heightOld - height) > 1)
  85. {
  86. m_root.SetSize(width, height);
  87. }
  88. else if (AlignChilds)
  89. {
  90. m_root.Update();
  91. }
  92. return;
  93. }
  94. void OnWidgetScriptInit(Widget w)
  95. {
  96. m_root = w;
  97. m_root.SetHandler(this);
  98. m_root.SetFlags(WidgetFlags.VEXACTPOS);
  99. Update();
  100. }
  101. override bool OnChildRemove( Widget w, Widget child) {
  102. if (w == m_root)
  103. {
  104. Update();
  105. }
  106. return false;
  107. }
  108. };