label.monkey2 3.6 KB

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