viewlayout.monkey2 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #rem
  2. A slightly more complicated window example.
  3. The example implements a resizable window with virtual resolution support via the "letterbox" and "stretch" layout modes, and shows
  4. some simple keyboard/mouse event handling.
  5. #end
  6. Namespace test
  7. #Import "<std>"
  8. #Import "<mojo>"
  9. Using std..
  10. Using mojo..
  11. Class MyWindow Extends Window
  12. Field virtualRes:=New Vec2i( 320,240 )
  13. Method New( title:String,width:Int,height:Int,flags:WindowFlags=WindowFlags.Resizable )
  14. 'Call super class constructor
  15. '
  16. Super.New( title,width,height,flags )
  17. 'Set initial layout (this is the default for Windows).
  18. '
  19. Layout="fill"
  20. 'Window clear color - for "letterbox" and "float" layouts, this is effectively the border color.
  21. '
  22. ClearColor=Color.Black
  23. 'Set minimum view size
  24. '
  25. MinSize=New Vec2i( 200,140 )
  26. 'Set view background color.
  27. '
  28. Style.BackgroundColor=Color.DarkGrey
  29. End
  30. Method OnRender( canvas:Canvas ) Override
  31. 'This is necessary for 'continuous' rendering.
  32. '
  33. 'Without it, OnRender will only be called when necessary, eg: when window is resized.
  34. '
  35. App.RequestRender()
  36. 'Gets mouse location in 'view' coordinates.
  37. '
  38. Local mouse:=Mouse.Location
  39. 'Render!
  40. '
  41. Local h:=canvas.Font.Height
  42. canvas.DrawText( "Size="+Rect.Size.ToString(),0,0 )
  43. canvas.DrawText( "Mouse="+mouse.ToString(),0,h )
  44. canvas.DrawText( "Layout=~q"+Layout+"~q ('L' to cycle)",0,h*2 )
  45. If Layout="float"
  46. canvas.DrawText( "Resolution="+virtualRes.ToString()+" ('R' to cycle)",0,h*3 )
  47. canvas.DrawText( "Gravity="+Gravity.ToString()+" ('G' to cycle)",0,h*4 )
  48. Else If Layout="letterbox"
  49. canvas.DrawText( "Resolution="+virtualRes.ToString()+" ('R' to cycle)",0,h*3 )
  50. canvas.DrawText( "Gravity="+Gravity.ToString()+" ('G' to cycle)",0,h*4 )
  51. Else If Layout="stretch"
  52. canvas.DrawText( "Resolution="+virtualRes.ToString()+" ('R' to cycle)",0,h*3 )
  53. Endif
  54. canvas.DrawText( "Hello World!",Width/2,Height/2,.5,.5 )
  55. End
  56. 'Measured out view.
  57. '
  58. 'This is used by the "float", "letterbox" and "stretch" layouts.
  59. '
  60. Method OnMeasure:Vec2i() Override
  61. Return virtualRes
  62. End
  63. 'Process a KeyEvent.
  64. '
  65. 'Needed because there's no App.KeyHit yet!
  66. '
  67. Method OnKeyEvent( event:KeyEvent ) Override
  68. Select event.Type
  69. Case EventType.KeyDown
  70. Select event.Key
  71. Case Key.L
  72. CycleLayout()
  73. Case Key.G
  74. CycleGravity()
  75. Case Key.R
  76. CycleVirtualRes()
  77. End
  78. End
  79. End
  80. 'Process a MouseEvent.
  81. '
  82. 'Note: event.Location property is in 'view space' coordinates.
  83. '
  84. Method OnMouseEvent( event:MouseEvent ) Override
  85. End
  86. Method OnWindowEvent( event:WindowEvent ) Override
  87. Select event.Type
  88. Case EventType.WindowClose
  89. Print "Window close"
  90. App.Terminate()
  91. Case EventType.WindowMoved
  92. Print "Window moved to:"+Frame.Origin.ToString()
  93. Case EventType.WindowResized
  94. Print "Window resized to:"+Frame.Size.ToString()
  95. App.RequestRender() 'note: we do this to trigger a render when window is being 'modally resized'.
  96. End
  97. End
  98. Method CycleLayout()
  99. Select Layout
  100. Case "fill"
  101. Layout="letterbox"
  102. Case "letterbox"
  103. Layout="stretch"
  104. Case "stretch"
  105. Layout="float"
  106. Case "float"
  107. Layout="fill"
  108. End
  109. virtualRes=New Vec2i( 320,240 )
  110. Gravity=New Vec2f( .5,.5 )
  111. End
  112. Method CycleGravity()
  113. Local gravity:=Gravity
  114. gravity.x+=.5
  115. If gravity.x>1
  116. gravity.x=0
  117. gravity.y+=.5
  118. If gravity.y>1 gravity.y=0
  119. Endif
  120. Gravity=gravity
  121. End
  122. Method CycleVirtualRes()
  123. Select virtualRes.x
  124. Case 320
  125. virtualRes=New Vec2i( 640,480 ) '4:3
  126. Case 640
  127. virtualRes=New Vec2i( 1024,768 ) '4:3
  128. Case 1024
  129. virtualRes=New Vec2i( 1280,720 ) '16:9
  130. Case 1280
  131. virtualRes=New Vec2i( 1920,1080 ) '16:9
  132. Case 1920
  133. virtualRes=New Vec2i( 320,240 ) '4:3
  134. End
  135. End
  136. End
  137. Function Main()
  138. New AppInstance
  139. New MyWindow( "Simple Window Demo!",640,512 )
  140. App.Run()
  141. End