gridview.monkey2 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. Namespace mojox
  2. Class GridView Extends View
  3. Method New( width:Int,height:Int )
  4. _gridw=width
  5. _gridh=height
  6. Style=GetStyle( "GridView" )
  7. End
  8. Property GridWidth:Int()
  9. Return _gridw
  10. Setter( gridw:Int )
  11. If gridw=_gridw Return
  12. _gridw=gridw
  13. RequestRender()
  14. End
  15. Property GridHeight:Int()
  16. Return _gridh
  17. Setter( gridh:Int )
  18. If gridh=_gridh Return
  19. _gridh=gridh
  20. RequestRender()
  21. End
  22. Method Clear()
  23. For Local cell:=Eachin _cells
  24. RemoveChildView( cell.view )
  25. Next
  26. _cells.Clear()
  27. RequestRender()
  28. End
  29. Method AddView( view:View,x:Int,y:Int,width:Int=1,height:Int=1 )
  30. _cells.Add( New Cell( view,x,y,width,height ) )
  31. AddChildView( view )
  32. RequestRender()
  33. End
  34. Method ReplaceView( view:View,with:View )
  35. For Local cell:=Eachin _cells
  36. If cell.view<>view Continue
  37. RemoveChildView( view )
  38. AddChildView( with )
  39. cell.view=with
  40. RequestRender()
  41. Return
  42. Next
  43. End
  44. Method RemoveView( view:View )
  45. For Local cell:=Eachin _cells
  46. If cell.view<>view Continue
  47. RemoveChildView( view )
  48. _cells.Remove( cell )
  49. RequestRender()
  50. Return
  51. Next
  52. End
  53. Protected
  54. Method OnMeasure:Vec2i() Override
  55. Local maxw:=0,maxh:=0
  56. For Local cell:=Eachin _cells
  57. Local size:=cell.view.LayoutSize
  58. maxw=Max( maxw,size.x/cell.w )
  59. maxh=Max( maxh,size.y/cell.h )
  60. Next
  61. Return New Vec2i( maxw * _gridw,maxh * _gridh )
  62. End
  63. Method OnLayout() Override
  64. For Local cell:=Eachin _cells
  65. Local x0:=cell.x * Width / _gridw
  66. Local x1:=(cell.x+cell.w) * Width / _gridw
  67. Local y0:=cell.y * Height / _gridh
  68. Local y1:=(cell.y+cell.h) * Height / _gridh
  69. cell.view.Frame=New Recti( x0,y0,x1,y1 )
  70. Next
  71. End
  72. Private
  73. Struct Cell
  74. Field view:View
  75. Field x:Int
  76. Field y:Int
  77. Field w:Int
  78. Field h:Int
  79. Method New( view:View,x:Int,y:Int,w:Int,h:Int )
  80. Self.view=view
  81. Self.x=x
  82. Self.y=y
  83. Self.w=w
  84. Self.h=h
  85. End
  86. End
  87. Field _gridw:Int
  88. Field _gridh:Int
  89. Field _cells:=New Stack<Cell>
  90. End