dialog.monkey2 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. Namespace mojox
  2. #rem monkeydoc @hidden
  3. #end
  4. Class DialogTitle Extends Button
  5. Field Dragged:Void( v:Vec2i )
  6. Method New( text:String="" )
  7. Super.New( text )
  8. Style=GetStyle( "DialogTitle" )
  9. End
  10. Private
  11. Field _org:Vec2i
  12. Field _drag:Bool
  13. Field _hover:Bool
  14. Method OnMouseEvent( event:MouseEvent ) Override
  15. Select event.Type
  16. Case EventType.MouseDown
  17. _drag=True
  18. _org=event.Location
  19. Case EventType.MouseUp
  20. _drag=False
  21. Case EventType.MouseEnter
  22. _hover=True
  23. Case EventType.MouseLeave
  24. _hover=False
  25. Case EventType.MouseMove
  26. If _drag Dragged( event.Location-_org )
  27. End
  28. If _drag
  29. StyleState="active"
  30. Else If _hover
  31. StyleState="hover"
  32. Else
  33. StyleState=""
  34. Endif
  35. End
  36. End
  37. #rem monkeydoc The dialog class.
  38. #end
  39. Class Dialog Extends View
  40. #rem monkeydoc Creates a new dialog.
  41. #end
  42. Method New()
  43. Style=GetStyle( "Dialog" )
  44. _title=New DialogTitle
  45. _title.Dragged=Lambda( vec:Vec2i )
  46. Frame+=vec
  47. RequestRender()
  48. End
  49. _content=New DockingView
  50. _content.Style=GetStyle( "DialogContent" )
  51. _actions=New DockingView
  52. _actions.Style=GetStyle( "DialogActions" )
  53. _actions.Layout="float"
  54. _docker=New DockingView
  55. _docker.AddView( _title,"top" )
  56. _docker.ContentView=_content
  57. _docker.AddView( _actions,"bottom" )
  58. AddChildView( _docker )
  59. End
  60. Method New( title:String )
  61. Self.New()
  62. Title=title
  63. End
  64. #rem monkeydoc Dialog title.
  65. #end
  66. Property Title:String()
  67. Return _title.Text
  68. Setter( title:String )
  69. _title.Text=title
  70. End
  71. #rem monkeydoc Dialog content view.
  72. #end
  73. Property ContentView:View()
  74. Return _content.ContentView
  75. Setter( contentView:View )
  76. _content.ContentView=contentView
  77. End
  78. #rem monkeydoc Adds an action to the dialog.
  79. #end
  80. Method AddAction( action:Action )
  81. Local button:=New PushButton( action )
  82. _actions.AddView( button,"left" )
  83. End
  84. Method AddAction:Action( label:String,icon:Image=Null )
  85. Local action:=New Action( label,icon )
  86. AddAction( action )
  87. Return action
  88. End
  89. #rem monkeydoc Binds an action to a key.
  90. #end
  91. Method SetKeyAction( key:Key,action:Action )
  92. _keyActions[key]=action
  93. End
  94. #rem monkeydoc Opens the dialog.
  95. #end
  96. Method Open()
  97. Assert( Not _window,"Dialog is already open" )
  98. _window=App.ActiveWindow
  99. Local size:=MeasureLayoutSize()
  100. Local origin:=(_window.Rect.Size-size)/2
  101. Frame=New Recti( origin,origin+size )
  102. _window.AddChildView( Self )
  103. End
  104. #rem monkeydoc Closes the dialog.
  105. #end
  106. Method Close()
  107. Assert( _window,"Dialog is not open" )
  108. _window.RemoveChildView( Self )
  109. _window=Null
  110. End
  111. #if __TARGET__<>"emscripten"
  112. #rem monkeydoc Creates and runs a modal dialog.
  113. #end
  114. Function Run:Int( title:String,view:View,actions:String[],onEnter:Int=-1,onEscape:Int=-1 )
  115. Local dialog:=New Dialog( title )
  116. dialog.ContentView=view
  117. Local future:=New Future<Int>
  118. For Local i:=0 Until actions.Length
  119. Local action:=dialog.AddAction( actions[i] )
  120. action.Triggered=Lambda()
  121. future.Set( i )
  122. End
  123. If i=onEnter dialog.SetKeyAction( Key.Enter,action )
  124. If i=onEscape dialog.SetKeyAction( Key.Escape,action )
  125. Next
  126. dialog.Open()
  127. App.BeginModal( dialog )
  128. Local result:=future.Get()
  129. App.EndModal()
  130. dialog.Close()
  131. Return result
  132. End
  133. #end
  134. Protected
  135. Method OnMeasure:Vec2i() Override
  136. Return _docker.LayoutSize
  137. End
  138. Method OnLayout() Override
  139. _docker.Frame=Rect
  140. End
  141. Method OnKeyEvent( event:KeyEvent ) Override
  142. Select event.Type
  143. Case EventType.KeyDown
  144. Local action:=_keyActions[event.Key]
  145. If action action.Trigger()
  146. End
  147. event.Eat()
  148. End
  149. Private
  150. Field _title:DialogTitle
  151. Field _content:DockingView
  152. Field _actions:DockingView
  153. Field _docker:DockingView
  154. Field _keyActions:=New Map<Key,Action>
  155. Field _window:Window
  156. End
  157. #rem monkeydoc The TextDialog class.
  158. #end
  159. Class TextDialog Extends Dialog
  160. #rem monkeydoc Creates a new text dialog.
  161. #end
  162. Method New( title:String="",text:String="" )
  163. Super.New( title )
  164. _label=New Label( text )
  165. _label.TextGravity=New Vec2f( .5,.5 )
  166. ContentView=_label
  167. End
  168. #rem monkeydoc Dialog text.
  169. #end
  170. Property Text:String()
  171. Return _label.Text
  172. Setter( text:String )
  173. _label.Text=text
  174. End
  175. #if __TARGET__<>"emscripten"
  176. #rem monkeydoc Creates and runs a modal text dialog.
  177. #end
  178. Function Run:Int( title:String,text:String,actions:String[],onEnter:Int=-1,onEscape:Int=-1 )
  179. Local dialog:=New TextDialog( title,text )
  180. Local result:=New Future<Int>
  181. For Local i:=0 Until actions.Length
  182. Local action:=dialog.AddAction( actions[i] )
  183. action.Triggered=Lambda()
  184. result.Set( i )
  185. End
  186. If i=onEnter dialog.SetKeyAction( Key.Enter,action )
  187. If i=onEscape dialog.SetKeyAction( Key.Escape,action )
  188. Next
  189. dialog.Open()
  190. App.BeginModal( dialog )
  191. Local r:=result.Get()
  192. App.EndModal()
  193. dialog.Close()
  194. Return r
  195. End
  196. #end
  197. Private
  198. Field _label:Label
  199. End
  200. #rem monkeydoc The ProgressDialog class.
  201. #end
  202. Class ProgressDialog Extends Dialog
  203. #rem monkeydoc Creates a new progress dialog.
  204. #end
  205. Method New( title:String="",text:String="" )
  206. Super.New( title )
  207. _label=New Label( text )
  208. _progress=New ProgressBar
  209. Local docker:=New DockingView
  210. docker.AddView( _label,"top" )
  211. docker.AddView( _progress,"top" )
  212. ContentView=docker
  213. End
  214. #rem monkeydoc Dialog text.
  215. #end
  216. Property Text:String()
  217. Return _label.Text
  218. Setter( text:String )
  219. _label.Text=text
  220. End
  221. Private
  222. Field _label:Label
  223. Field _progress:ProgressBar
  224. End