label.monkey2 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. Namespace mojox
  2. #rem monkeydoc The Lable class.
  3. #end
  4. Class Label Extends View
  5. #rem monkeydoc Invoked when the label is clicked.
  6. #end
  7. Field Clicked:Void()
  8. #rem monkeydoc Invoked when the label is right clicked.
  9. #end
  10. Field RightClicked:Void()
  11. #rem monkeydoc Invoked when the label is double clicked.
  12. #end
  13. Field DoubleClicked:Void()
  14. #rem monkeydoc Creates a new label.
  15. #end
  16. Method New( text:String="",icon:Image=Null )
  17. Style=GetStyle( "Label" )
  18. Layout="fill-x"
  19. Gravity=New Vec2f( 0,.5 )
  20. TextGravity=New Vec2f( 0,.5 )
  21. Text=text
  22. Icon=icon
  23. End
  24. Method New( action:Action )
  25. Self.New()
  26. Text=action.Text
  27. Icon=action.Icon
  28. Clicked=Lambda()
  29. action.Trigger()
  30. End
  31. action.Modified=Lambda()
  32. Enabled=action.Enabled
  33. Text=action.Text
  34. Icon=action.Icon
  35. End
  36. End
  37. #rem monkeydoc Label text.
  38. #end
  39. Property Text:String()
  40. Return _text
  41. Setter( text:String )
  42. If text=_text Return
  43. _text=text
  44. RequestRender()
  45. End
  46. #rem monkeydoc Label text gravity.
  47. #end
  48. Property TextGravity:Vec2f()
  49. Return _textGravity
  50. Setter( textGravity:Vec2f )
  51. If textGravity=_textGravity Return
  52. _textGravity=textGravity
  53. RequestRender()
  54. End
  55. #rem monkeydoc Label icon.
  56. #end
  57. Property Icon:Image()
  58. Return _icon
  59. Setter( icon:Image )
  60. If icon=_icon Return
  61. _icon=icon
  62. RequestRender()
  63. End
  64. #rem monkeydoc Adds a view to the right of the label.
  65. #end
  66. Method AddView( view:View )
  67. AddChildView( view )
  68. _views.Push( view )
  69. End
  70. #rem monkeydoc Removes a view from the label.
  71. #end
  72. Method RemoveView( view:View )
  73. RemoveChildView( view )
  74. _views.Remove( view )
  75. End
  76. Protected
  77. Method OnMeasure:Vec2i() Override
  78. _iconSize=New Vec2i( 0,0 )
  79. _textSize=New Vec2i( 0,0 )
  80. _viewsSize=New Vec2i( 0,0 )
  81. Local w:=0,h:=0
  82. If _icon
  83. _iconSize=New Vec2i( _icon.Width,_icon.Height )
  84. w=_iconSize.x
  85. h=_iconSize.y
  86. Endif
  87. If _text
  88. _textSize=RenderStyle.MeasureText( _text )
  89. w+=_textSize.x
  90. h=Max( h,_textSize.y )
  91. Endif
  92. For Local view:=Eachin _views
  93. _viewsSize.x+=view.LayoutSize.x
  94. _viewsSize.y=Max( _viewsSize.y,view.LayoutSize.y )
  95. Next
  96. w+=_viewsSize.x
  97. h=Max( h,_viewsSize.y )
  98. Return New Vec2i( w,h )
  99. End
  100. Method OnLayout() Override
  101. Local iy:=(Height-_iconSize.y)/2
  102. _iconRect=New Recti( 0,iy,_iconSize.x,iy+_iconSize.y )
  103. Local tx:=_iconSize.x,ty:=0
  104. Local tw:=_textSize.x,th:=Height
  105. _textRect=New Recti( tx,ty,tx+tw,ty+th )
  106. Local x1:=Width
  107. For Local i:=_views.Length-1 To 0 Step -1
  108. Local view:=_views[i]
  109. Local x0:=i ? Max( x1-view.LayoutSize.x,_textRect.Right ) Else _textRect.Right
  110. view.Frame=New Recti( x0,0,x1,Height )
  111. x1=x0
  112. Next
  113. #rem
  114. For Local view:=Eachin _views.Backwards()
  115. Local x0:=Max( x1-view.LayoutSize.x,_textRect.Right )
  116. view.Frame=New Recti( x0,0,x1,Height )
  117. x1=x0
  118. Next
  119. _textRect.Right=x1
  120. #end
  121. Return
  122. Local x0:=_textRect.Right
  123. For Local view:=Eachin _views
  124. Local x1:=Min( x0+view.LayoutSize.x,Width )
  125. view.Frame=New Recti( x0,0,x1,Height )
  126. x0=x1
  127. Next
  128. End
  129. Method OnRender( canvas:Canvas ) Override
  130. If _icon
  131. RenderStyle.DrawIcon( canvas,_icon,_iconRect.X,_iconRect.Y )
  132. Endif
  133. If _text
  134. RenderStyle.DrawText( canvas,_text,_textRect,_textGravity )
  135. Endif
  136. End
  137. Method OnMouseEvent( event:MouseEvent ) Override
  138. Select event.Type
  139. Case EventType.MouseDown,EventType.MouseWheel
  140. Return
  141. Case EventType.MouseClick
  142. Clicked()
  143. Case EventType.MouseRightClick
  144. RightClicked()
  145. Case EventType.MouseDoubleClick
  146. DoubleClicked()
  147. End
  148. event.Eat()
  149. End
  150. Private
  151. Field _text:String
  152. Field _textGravity:Vec2f=New Vec2f( 0,.5 )
  153. Field _icon:Image
  154. Field _views:=New Stack<View>
  155. Field _iconSize:Vec2i
  156. Field _iconRect:Recti
  157. Field _textSize:Vec2i
  158. Field _textRect:Recti
  159. Field _viewsSize:Vec2i
  160. End