sizetochild.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. class SizeToChild extends ScriptedWidgetEventHandler
  2. {
  3. reference string m_ChildName;
  4. reference float m_HorizontalOffset;
  5. reference float m_VerticalOffset;
  6. reference bool m_ResizeHorizontal;
  7. reference bool m_ResizeVertical;
  8. protected Widget m_Root;
  9. protected Widget m_Child;
  10. protected static bool m_IgnoredBool;
  11. void OnWidgetScriptInit(Widget w)
  12. {
  13. m_Root = w;
  14. m_Child = m_Root.FindAnyWidget( m_ChildName );
  15. if ( m_Child )
  16. {
  17. ResizeParentToChild();
  18. }
  19. }
  20. bool ResizeParentToChild()
  21. {
  22. return ResizeParentToChild( m_IgnoredBool, -1, true );
  23. }
  24. bool ResizeParentToChild( out bool changed_size, int limit = -1, bool immedUpdate = true )
  25. {
  26. float x, y, o_x, o_y, new_x, new_y;
  27. if ( m_Child )
  28. {
  29. m_Child.Update();
  30. m_Child.GetScreenSize( x, y );
  31. m_Root.GetScreenSize( new_x, new_y );
  32. m_Root.GetSize( o_x, o_y );
  33. bool changed = false;
  34. bool hit_limit = false;
  35. if ( m_ResizeHorizontal && ( x > new_x + 0.01 || x < new_x - 0.01 ) )
  36. {
  37. new_x = x + m_HorizontalOffset;
  38. changed = true;
  39. }
  40. else
  41. new_x = o_x;
  42. if ( m_ResizeVertical && ( y > new_y + 0.01 || y < new_y - 0.01 ) )
  43. {
  44. new_y = y + m_VerticalOffset;
  45. changed = true;
  46. }
  47. else
  48. new_y = o_y;
  49. if ( limit > 0 && new_y > limit )
  50. {
  51. new_y = limit;
  52. hit_limit = true;
  53. }
  54. if ( changed )
  55. {
  56. m_Root.SetSize( new_x, new_y, immedUpdate );
  57. }
  58. changed_size = changed;
  59. return hit_limit;
  60. }
  61. else
  62. {
  63. m_Child = m_Root.FindAnyWidget( m_ChildName );
  64. if ( !m_Child )
  65. {
  66. Print( "Error in size to child, " + m_Root.GetName() + " has no child named " + m_ChildName );
  67. }
  68. }
  69. return false;
  70. }
  71. }
  72. class SizeToParent extends ScriptedWidgetEventHandler
  73. {
  74. reference bool m_ResizeHorizontal;
  75. reference bool m_ResizeVertical;
  76. protected Widget m_Root;
  77. protected Widget m_Parent;
  78. void OnWidgetScriptInit(Widget w)
  79. {
  80. m_Root = w;
  81. if ( m_ResizeHorizontal )
  82. m_Root.ClearFlags( WidgetFlags.HEXACTSIZE );
  83. if ( m_ResizeVertical )
  84. m_Root.ClearFlags( WidgetFlags.VEXACTSIZE );
  85. m_Parent = m_Root.GetParent();
  86. Refresh();
  87. }
  88. void Refresh()
  89. {
  90. float x, y, o_x, o_y, new_x, new_y;
  91. m_Parent.Update();
  92. m_Parent.GetScreenSize( x, y );
  93. m_Root.GetScreenSize( new_x, new_y );
  94. m_Root.GetSize( o_x, o_y );
  95. bool changed = false;
  96. if ( m_ResizeHorizontal && x != new_x )
  97. {
  98. new_x = x;
  99. changed = true;
  100. }
  101. else
  102. new_x = o_x;
  103. if ( m_ResizeVertical && y != new_y )
  104. {
  105. new_y = y;
  106. changed = true;
  107. }
  108. else
  109. new_y = o_y;
  110. if ( changed )
  111. m_Root.SetSize( new_x, new_y );
  112. }
  113. }