calculator.monkey2 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. Namespace myapp
  2. #Import "<std>"
  3. #Import "<mojo>"
  4. #Import "<mojox>"
  5. Using std..
  6. Using mojo..
  7. Using mojox..
  8. 'Simple gridview layout, since mojox doesn't have one yet!
  9. '
  10. Class GridView Extends View
  11. Method New( columns:Int=2 )
  12. _cols=columns
  13. Layout="fill"
  14. End
  15. Property Columns:Int()
  16. Return _cols
  17. Setter( cols:Int )
  18. _cols=cols
  19. End
  20. Method AddView( view:View )
  21. AddChildView( view )
  22. _views.Push( view )
  23. End
  24. Protected
  25. Method OnMeasure:Vec2i() Override
  26. Local rows:=(_views.Length-1)/_cols+1
  27. Local w:=0,h:=0
  28. For Local view:=Eachin _views
  29. w=Max( w,view.LayoutSize.x )
  30. h=Max( h,view.LayoutSize.y )
  31. Next
  32. Return New Vec2i( w * _cols,h * rows )
  33. End
  34. Method OnLayout() Override
  35. Local rows:=(_views.Length-1)/_cols+1
  36. Local i:=0,j:=0
  37. For Local view:=Eachin _views
  38. Local x0:=(i * Width)/_cols
  39. Local y0:=(j * Height)/rows
  40. Local x1:=((i+1) * Width)/_cols
  41. Local y1:=((j+1) * Height)/rows
  42. view.Frame=New Recti( x0,y0,x1,y1 )
  43. i=(i+1) Mod _cols
  44. If Not i j+=1
  45. Next
  46. End
  47. Private
  48. Field _cols:Int=2
  49. Field _views:=New Stack<View>
  50. End
  51. Class CalcButton Extends PushButton
  52. Method New( text:String )
  53. Super.New( text )
  54. TextGravity=New Vec2f( .5,.5 )
  55. MinSize=New Vec2i( 32,0 )
  56. End
  57. End
  58. Class CalcView Extends DockingView
  59. Field _result:Label
  60. Field _buttons:GridView
  61. Field _acc:Long
  62. Field _lastOp:Long( lhs:Long,rhs:Long )
  63. Field _nullOp:Long( lhs:Long,rhs:Long )
  64. Field _clr:Bool
  65. Method AddButton( text:String,clicked:Void() )
  66. Local button:=New CalcButton( text )
  67. button.Clicked=clicked
  68. _buttons.AddView( button )
  69. End
  70. Method AddButton( text:String,op:Long( x:Long,y:Long ) )
  71. Local button:=New CalcButton( text )
  72. button.Clicked=Lambda()
  73. _acc=_lastOp( _acc,Long( _result.Text ) )
  74. _result.Text=String( _acc )
  75. _clr=True
  76. _lastOp=op
  77. End
  78. _buttons.AddView( button )
  79. End
  80. Method New()
  81. _result=New Label( "0" )
  82. _result.Style=App.Theme.GetStyle( "PushButton" )
  83. _result.TextGravity=New Vec2f( 1,.5 )
  84. AddView( _result,"top" )
  85. _buttons=New GridView( 3 )
  86. AddView( _buttons,"top" )
  87. For Local i:=0 Until 10
  88. AddButton( String( i ),Lambda()
  89. If _clr
  90. _clr=False
  91. _result.Text=""
  92. Else if _result.Text="0"
  93. _result.Text=""
  94. Endif
  95. _result.Text+=String( i )
  96. End )
  97. Next
  98. AddButton( "+",Lambda:Long( lhs:Long,rhs:Long )
  99. Return lhs + rhs
  100. End )
  101. AddButton( "-",Lambda:Long( lhs:Long,rhs:Long )
  102. Return lhs - rhs
  103. End )
  104. AddButton( "*",Lambda:Long( lhs:Long,rhs:Long )
  105. Return lhs * rhs
  106. End )
  107. AddButton( "/",Lambda:Long( lhs:Long,rhs:Long )
  108. Return lhs - rhs
  109. End )
  110. AddButton( "=",Lambda()
  111. _acc=_lastOp( _acc,Long( _result.Text ) )
  112. _result.Text=String( _acc )
  113. _lastOp=_nullOp
  114. _clr=True
  115. End )
  116. AddButton( "+/-",Lambda()
  117. If Not Long( _result.Text ) Return
  118. If _result.Text.StartsWith( "-" )
  119. _result.Text=_result.Text.Slice( 1 )
  120. Else
  121. _result.Text="-"+_result.Text
  122. Endif
  123. End )
  124. AddButton( "CE",Lambda()
  125. _result.Text="0"
  126. End )
  127. AddButton( "C",Lambda()
  128. _result.Text="0"
  129. _lastOp=_nullOp
  130. _acc=0
  131. End )
  132. _nullOp=Lambda:Long( lhs:Long,rhs:Long )
  133. Return rhs
  134. End
  135. _result.Text="0"
  136. _lastOp=_nullOp
  137. _acc=0
  138. End
  139. End
  140. Class MyWindow Extends Window
  141. Field _calc:CalcView
  142. Field _dialog:Dialog
  143. Method New()
  144. _calc=New CalcView
  145. _dialog=New Dialog( "Calculator" )
  146. _dialog.ContentView=_calc
  147. App.Idle+=_dialog.Open
  148. End
  149. End
  150. Function Main()
  151. New AppInstance
  152. New MyWindow
  153. App.Run()
  154. End