spacerbase.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // -----------------------------------------------------------
  2. class SpacerBase : ScriptedWidgetEventHandler
  3. {
  4. protected Widget m_root;
  5. protected int m_count;
  6. // -----------------------------------------------------------
  7. void OnWidgetScriptInit(Widget w)
  8. {
  9. m_root = w;
  10. m_count = 0;
  11. Widget child = m_root.GetChildren();
  12. while (child)
  13. {
  14. m_count++;
  15. child.SetFlags(WidgetFlags.EXACTPOS | WidgetFlags.EXACTSIZE, false);
  16. child = child.GetSibling();
  17. }
  18. m_root.SetHandler(this);
  19. }
  20. // -----------------------------------------------------------
  21. override bool OnUpdate(Widget w)
  22. {
  23. if (w == m_root) UpdateLayout();
  24. return false;
  25. }
  26. // -----------------------------------------------------------
  27. override bool OnChildAdd( Widget w, Widget child)
  28. {
  29. m_count++;
  30. child.SetFlags(WidgetFlags.EXACTPOS | WidgetFlags.EXACTSIZE, false);
  31. return false;
  32. }
  33. // -----------------------------------------------------------
  34. override bool OnChildRemove( Widget w, Widget child)
  35. {
  36. m_count--;
  37. return false;
  38. }
  39. // -----------------------------------------------------------
  40. protected int GetChildIndex(Widget w)
  41. {
  42. Widget child = m_root.GetChildren();
  43. int index = 0;
  44. while (child)
  45. {
  46. if (child == w) return index;
  47. index++;
  48. child = child.GetSibling();
  49. }
  50. return INDEX_NOT_FOUND;
  51. }
  52. // -----------------------------------------------------------
  53. void UpdateLayout()
  54. {
  55. if (m_count == 0) return;
  56. float width;
  57. float height;
  58. m_root.GetScreenSize(width, height);
  59. Widget child = m_root.GetChildren();
  60. int index = 0;
  61. while (child)
  62. {
  63. UpdateChild(child, width, height, index);
  64. index++;
  65. child = child.GetSibling();
  66. }
  67. }
  68. protected void UpdateChild(Widget child, float w, float h, int index) {}
  69. };