requesters.monkey2 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. Namespace mojox
  2. #if __TARGET__<>"emscripten"
  3. #rem monkeydoc Runs a simple 'alert' dialog.
  4. If this function is called from the main fiber, a new fiber is created to the alert on.
  5. #end
  6. Function Alert( message:String,title:String="Alert!" )
  7. Local alert:=Lambda()
  8. TextDialog.Run( title,message,New String[]( "Okay" ),0,0 )
  9. End
  10. If Fiber.Current()=Fiber.Main()
  11. New Fiber( alert )
  12. Else
  13. alert()
  14. End
  15. End
  16. #rem monkeydoc Runs a simple 'okay/cancel' dialog.
  17. Returns true if the user selects 'Okay', else false.
  18. This function must not be called from the main fiber.
  19. #end
  20. Function RequestOkay:Bool( message:String="Are you sure you want to do this?",title:String="Okay?",yesButton:String="Okay",noButton:String="Cancel" )
  21. Assert( Fiber.Current()<>Fiber.Main(),"RequestOkay cannot be used from the main fiber" )
  22. Return TextDialog.Run( title,message,New String[]( yesButton,noButton ),0,1 )=0
  23. End
  24. #rem monkeydoc Runs a simple string dialog.
  25. Returns the string typed by the user.
  26. This function must not be called from the main fiber.
  27. #end
  28. Function RequestString:String( message:String="Enter a string:",title:String="String requester",initialValue:String="" )
  29. Assert( Fiber.Current()<>Fiber.Main(),"RequestString cannot be used from the main fiber" )
  30. Local future:=New Future<String>
  31. Local textField:=New TextField( initialValue )
  32. Local label:=New Label( message )
  33. label.AddView( textField )
  34. Local dialog:=New Dialog( title )
  35. dialog.MaxSize=New Vec2i( 320,0 )
  36. dialog.ContentView=label
  37. Local okay:=dialog.AddAction( "Okay" )
  38. okay.Triggered=Lambda()
  39. future.Set( textField.Text )
  40. End
  41. Local cancel:=dialog.AddAction( "Cancel" )
  42. cancel.Triggered=Lambda()
  43. future.Set( "" )
  44. End
  45. textField.Entered=okay.Trigger
  46. textField.Escaped=cancel.Trigger
  47. dialog.Open()
  48. textField.MakeKeyView()
  49. App.BeginModal( dialog )
  50. Local str:=future.Get()
  51. App.EndModal()
  52. dialog.Close()
  53. Return str
  54. End
  55. Function RequestInt:Long( message:String="Enter an integer:",title:String="Integer requester",init:Long=0,canceled:Long=0,min:Long=-1000000,max:Long=1000000 )
  56. Assert( Fiber.Current()<>Fiber.Main(),"RequestInt cannot be used from the main fiber" )
  57. Local future:=New Future<Long>
  58. Local textField:=New TextField
  59. textField.Text=String( Clamp( init,min,max ) )
  60. Local result:=New Long[]( init )
  61. textField.TextChanged+=Lambda()
  62. Local text:=textField.Text
  63. If text="-" And min<0 Return
  64. Local value:=Long( text )
  65. If String( value )=text
  66. result[0]=value
  67. Return
  68. Endif
  69. 'Avoids potential 'TextChanged in TextChanged' recursion. Probably OK in this case but...
  70. App.Idle+=Lambda()
  71. textField.Text=String( result[0] )
  72. End
  73. End
  74. Local label:=New Label( message )
  75. label.AddView( textField )
  76. Local dialog:=New Dialog( title )
  77. dialog.MaxSize=New Vec2i( 320,0 )
  78. dialog.ContentView=label
  79. Local okay:=dialog.AddAction( "Okay" )
  80. okay.Triggered=Lambda()
  81. Local value:=Clamp( result[0],min,max )
  82. If value<>result[0]
  83. textField.Text=String( value )
  84. result[0]=value
  85. Return
  86. Endif
  87. future.Set( value )
  88. End
  89. Local cancel:=dialog.AddAction( "Cancel" )
  90. cancel.Triggered=Lambda()
  91. future.Set( canceled )
  92. End
  93. textField.Entered=okay.Trigger
  94. textField.Escaped=cancel.Trigger
  95. dialog.Open()
  96. textField.MakeKeyView()
  97. App.BeginModal( dialog )
  98. Local r:=future.Get()
  99. App.EndModal()
  100. dialog.Close()
  101. Return r
  102. End
  103. #Endif