button.monkey2 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. Namespace mojox
  2. #rem monkeydoc The Button class.
  3. #end
  4. Class Button Extends Label
  5. #rem monkeydoc Invoked when the button is dragged.
  6. #end
  7. Field Dragged:Void( v:Vec2i )
  8. #rem monkeydoc Creates a new button.
  9. #end
  10. Method New( text:String="",icon:Image=Null )
  11. Super.New( text,icon )
  12. Style=GetStyle( "Button" )
  13. TextGravity=New Vec2f( .5,.5 )
  14. Text=text
  15. Icon=icon
  16. End
  17. Method New( action:Action )
  18. Self.New()
  19. Text=action.Text
  20. Icon=action.Icon
  21. Clicked=Lambda()
  22. action.Trigger()
  23. End
  24. action.Modified=Lambda()
  25. Enabled=action.Enabled
  26. Text=action.Text
  27. Icon=action.Icon
  28. End
  29. End
  30. #rem monkeydoc Button selected state.
  31. #end
  32. Property Selected:Bool()
  33. Return _selected
  34. Setter( selected:Bool )
  35. If selected=_selected return
  36. _selected=selected
  37. UpdateStyleState()
  38. End
  39. #rem monkeydoc PushButtonMode flag.
  40. If false (the default), the button will invoke [[Clicked]] each time the left mouse button is pressed.
  41. If true, the button will invoke [[Clicked]] only when the left mouse button is released AND the mouse is still hovering over the button.
  42. #end
  43. Property PushButtonMode:Bool()
  44. Return _pushButtonMode
  45. Setter( mode:Bool )
  46. _pushButtonMode=mode
  47. End
  48. Protected
  49. Method OnMeasure:Vec2i() Override
  50. Return Super.OnMeasure()
  51. End
  52. Method OnValidateStyle() Override
  53. Super.OnValidateStyle()
  54. End
  55. Method OnRender( canvas:Canvas ) Override
  56. Super.OnRender( canvas )
  57. End
  58. Method OnMouseEvent( event:MouseEvent ) Override
  59. Select event.Type
  60. Case EventType.MouseDown,EventType.MouseWheel
  61. Return
  62. Case EventType.MouseClick
  63. _org=event.Location
  64. _active=True
  65. If Not _pushButtonMode Clicked()
  66. Case EventType.MouseDoubleClick
  67. If Not _pushButtonMode DoubleClicked()
  68. Case EventType.MouseRightClick
  69. If Not _pushButtonMode RightClicked()
  70. Case EventType.MouseUp
  71. If _pushButtonMode And _hover Clicked()
  72. _active=False
  73. Case EventType.MouseEnter
  74. _hover=True
  75. Case EventType.MouseLeave
  76. _hover=False
  77. Case EventType.MouseMove
  78. If _active Dragged( event.Location-_org )
  79. End
  80. UpdateStyleState()
  81. event.Eat()
  82. End
  83. Private
  84. Field _pushButtonMode:Bool
  85. Field _selected:Bool
  86. Field _active:Bool
  87. Field _hover:Bool
  88. Field _org:Vec2i
  89. Method UpdateStyleState()
  90. If _selected
  91. StyleState="selected"
  92. Else If _active And _hover
  93. StyleState="active"
  94. Else If _active Or _hover
  95. StyleState="hover"
  96. Else
  97. StyleState=""
  98. Endif
  99. End
  100. End