label.monkey2 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. _textRect.Right=x1
  114. #rem
  115. For Local view:=Eachin _views.Backwards()
  116. Local x0:=Max( x1-view.LayoutSize.x,_textRect.Right )
  117. view.Frame=New Recti( x0,0,x1,Height )
  118. x1=x0
  119. Next
  120. _textRect.Right=x1
  121. #end
  122. Return
  123. Local x0:=_textRect.Right
  124. For Local view:=Eachin _views
  125. Local x1:=Min( x0+view.LayoutSize.x,Width )
  126. view.Frame=New Recti( x0,0,x1,Height )
  127. x0=x1
  128. Next
  129. End
  130. Method OnRender( canvas:Canvas ) Override
  131. If _icon
  132. RenderStyle.DrawIcon( canvas,_icon,_iconRect.X,_iconRect.Y )
  133. Endif
  134. If _text
  135. RenderStyle.DrawText( canvas,_text,_textRect,_textGravity )
  136. Endif
  137. End
  138. Method OnMouseEvent( event:MouseEvent ) Override
  139. Select event.Type
  140. Case EventType.MouseDown,EventType.MouseWheel
  141. Return
  142. Case EventType.MouseClick
  143. Clicked()
  144. Case EventType.MouseRightClick
  145. RightClicked()
  146. Case EventType.MouseDoubleClick
  147. DoubleClicked()
  148. End
  149. event.Eat()
  150. End
  151. Private
  152. Field _text:String
  153. Field _textGravity:Vec2f=New Vec2f( 0,.5 )
  154. Field _icon:Image
  155. Field _views:=New Stack<View>
  156. Field _iconSize:Vec2i
  157. Field _iconRect:Recti
  158. Field _textSize:Vec2i
  159. Field _textRect:Recti
  160. Field _viewsSize:Vec2i
  161. End