dialog.monkey2 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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,contentView:View=Null )
  61. Self.New()
  62. Title=title
  63. If contentView ContentView=contentView
  64. End
  65. #rem monkeydoc Dialog title.
  66. #end
  67. Property Title:String()
  68. Return _title.Text
  69. Setter( title:String )
  70. _title.Text=title
  71. End
  72. #rem monkeydoc Dialog content view.
  73. #end
  74. Property ContentView:View()
  75. Return _content.ContentView
  76. Setter( contentView:View )
  77. _content.ContentView=contentView
  78. End
  79. #rem monkeydoc Adds an action to the dialog.
  80. #end
  81. Method AddAction( action:Action )
  82. Local button:=New PushButton( action )
  83. _actions.AddView( button,"left" )
  84. End
  85. Method AddAction:Action( label:String,icon:Image=Null )
  86. Local action:=New Action( label,icon )
  87. AddAction( action )
  88. Return action
  89. End
  90. #rem monkeydoc Binds an action to a key.
  91. #end
  92. Method SetKeyAction( key:Key,action:Action )
  93. _keyActions[key]=action
  94. End
  95. #rem monkeydoc Opens the dialog.
  96. #end
  97. Method Open()
  98. Assert( Not _window,"Dialog is already open" )
  99. _window=App.ActiveWindow
  100. Local size:=MeasureLayoutSize()
  101. Local origin:=(_window.Rect.Size-size)/2
  102. Frame=New Recti( origin,origin+size )
  103. _window.AddChildView( Self )
  104. End
  105. #rem monkeydoc Closes the dialog.
  106. #end
  107. Method Close()
  108. Assert( _window,"Dialog is not open" )
  109. _window.RemoveChildView( Self )
  110. _window=Null
  111. End
  112. #if __TARGET__<>"emscripten"
  113. #rem monkeydoc Creates and runs a modal dialog.
  114. #end
  115. Function Run:Int( title:String,view:View,actions:String[],onEnter:Int=-1,onEscape:Int=-1 )
  116. Local dialog:=New Dialog( title )
  117. dialog.ContentView=view
  118. Local future:=New Future<Int>
  119. For Local i:=0 Until actions.Length
  120. Local action:=dialog.AddAction( actions[i] )
  121. action.Triggered=Lambda()
  122. future.Set( i )
  123. End
  124. If i=onEnter dialog.SetKeyAction( Key.Enter,action )
  125. If i=onEscape dialog.SetKeyAction( Key.Escape,action )
  126. Next
  127. dialog.Open()
  128. App.BeginModal( dialog )
  129. Local result:=future.Get()
  130. App.EndModal()
  131. dialog.Close()
  132. Return result
  133. End
  134. #end
  135. Protected
  136. Method OnMeasure:Vec2i() Override
  137. Return _docker.LayoutSize
  138. End
  139. Method OnLayout() Override
  140. _docker.Frame=Rect
  141. End
  142. Method OnKeyEvent( event:KeyEvent ) Override
  143. Select event.Type
  144. Case EventType.KeyDown
  145. Local action:=_keyActions[event.Key]
  146. If action action.Trigger()
  147. End
  148. event.Eat()
  149. End
  150. Private
  151. Field _title:DialogTitle
  152. Field _content:DockingView
  153. Field _actions:DockingView
  154. Field _docker:DockingView
  155. Field _keyActions:=New Map<Key,Action>
  156. Field _window:Window
  157. End
  158. #rem monkeydoc The TextDialog class.
  159. #end
  160. Class TextDialog Extends Dialog
  161. #rem monkeydoc Creates a new text dialog.
  162. #end
  163. Method New( title:String="",text:String="" )
  164. Super.New( title )
  165. _label=New Label( text )
  166. _label.TextGravity=New Vec2f( .5,.5 )
  167. ContentView=_label
  168. End
  169. #rem monkeydoc Dialog text.
  170. #end
  171. Property Text:String()
  172. Return _label.Text
  173. Setter( text:String )
  174. _label.Text=text
  175. End
  176. #if __TARGET__<>"emscripten"
  177. #rem monkeydoc Creates and runs a modal text dialog.
  178. #end
  179. Function Run:Int( title:String,text:String,actions:String[],onEnter:Int=-1,onEscape:Int=-1 )
  180. Local dialog:=New TextDialog( title,text )
  181. Local result:=New Future<Int>
  182. For Local i:=0 Until actions.Length
  183. Local action:=dialog.AddAction( actions[i] )
  184. action.Triggered=Lambda()
  185. result.Set( i )
  186. End
  187. If i=onEnter dialog.SetKeyAction( Key.Enter,action )
  188. If i=onEscape dialog.SetKeyAction( Key.Escape,action )
  189. Next
  190. dialog.Open()
  191. App.BeginModal( dialog )
  192. Local r:=result.Get()
  193. App.EndModal()
  194. dialog.Close()
  195. Return r
  196. End
  197. #end
  198. Private
  199. Field _label:Label
  200. End
  201. #rem monkeydoc The ProgressDialog class.
  202. #end
  203. Class ProgressDialog Extends Dialog
  204. #rem monkeydoc Creates a new progress dialog.
  205. #end
  206. Method New( title:String="",text:String="" )
  207. Super.New( title )
  208. _label=New Label( text )
  209. _progress=New ProgressBar
  210. Local docker:=New DockingView
  211. docker.AddView( _label,"top" )
  212. docker.AddView( _progress,"top" )
  213. ContentView=docker
  214. End
  215. #rem monkeydoc Dialog text.
  216. #end
  217. Property Text:String()
  218. Return _label.Text
  219. Setter( text:String )
  220. _label.Text=text
  221. End
  222. Private
  223. Field _label:Label
  224. Field _progress:ProgressBar
  225. End