iOSAlerts.mm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platformiOS/platformiOS.h"
  23. #include "platform/platformSemaphore.h"
  24. #include "platform/platformVideo.h"
  25. #include "platform/threads/thread.h"
  26. #include "console/console.h"
  27. #include "platformiOS/iOSEvents.h"
  28. #include "platform/nativeDialogs/msgBox.h"
  29. #include "platformiOS/iOSAlerts.h"
  30. @implementation iOSAlertDelegate
  31. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
  32. buttonNumber = buttonIndex;
  33. }
  34. //Luma: Added delegate for dismissed call by system
  35. - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
  36. buttonNumber = buttonIndex;
  37. }
  38. - (void)didPresentAlertView:(UIAlertView *)alertView {
  39. }
  40. @end
  41. bool iOSButtonBox(const char *windowTitle, const char *message, int numButtons = 0, NSArray *buttons = nil, iOSAlertDelegate *delegate = nil)
  42. {
  43. UIAlertView *Alert = [[UIAlertView alloc] initWithTitle: @(windowTitle)
  44. message: @(message)
  45. delegate: delegate
  46. cancelButtonTitle: nil
  47. otherButtonTitles: nil ];
  48. if(numButtons > 0)
  49. {
  50. NSString *current = nil;
  51. for( int i = 1; i < numButtons ; i++ )
  52. {
  53. current = buttons[i];
  54. [Alert addButtonWithTitle: current ];
  55. }
  56. }
  57. else
  58. {
  59. [Alert addButtonWithTitle: @"OK" ];
  60. }
  61. [Alert show];
  62. // PUAP -Mat NOTE: NSRunLoop is not Thread-Safe, see documentation
  63. while (Alert.visible)
  64. {
  65. [[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate: [NSDate dateWithTimeIntervalSinceNow: 0.100]];
  66. }
  67. return true;
  68. }
  69. //-----------------------------------------------------------------------------
  70. void Platform::AlertOK(const char *windowTitle, const char *message)
  71. {
  72. iOSAlertDelegate *delegate = [[iOSAlertDelegate alloc] init];
  73. iOSButtonBox( windowTitle, message, 0, nil, delegate );
  74. }
  75. //-----------------------------------------------------------------------------
  76. bool Platform::AlertOKCancel(const char *windowTitle, const char *message)
  77. {
  78. iOSAlertDelegate *delegate = [[iOSAlertDelegate alloc] init];
  79. NSArray *buttons = @[ @"OK", @"Cancel" ];
  80. //Luma: Need to pass the delegate in as well
  81. iOSButtonBox( windowTitle, message, 2, buttons, delegate );
  82. //Luma: Zero is NOT the cancel button index... it is based on the order of the buttons in the above array
  83. bool returnValue = (delegate->buttonNumber != 1 );
  84. return returnValue;
  85. }
  86. //-----------------------------------------------------------------------------
  87. bool Platform::AlertRetry(const char *windowTitle, const char *message)
  88. {//retry/cancel
  89. iOSAlertDelegate *delegate = [[iOSAlertDelegate alloc] init];
  90. //Luma: Should be Retry / Cancel, not Cancel / Retry
  91. NSArray *buttons = @[@"Retry", @"Cancel"];
  92. //Luma: Need to pass the delegate in as well
  93. iOSButtonBox( windowTitle, message, 2, buttons, delegate );
  94. //Luma: Zero is NOT the cancel button index... it is based on the order of the buttons in the above array
  95. bool returnValue = (delegate->buttonNumber != 1 );
  96. return returnValue;
  97. }
  98. bool Platform::AlertYesNo(const char *windowTitle, const char *message)
  99. {
  100. iOSAlertDelegate *delegate = [[iOSAlertDelegate alloc] init];
  101. NSArray *buttons = @[@"Yes", @"No"];
  102. iOSButtonBox( windowTitle, message, 2, buttons, delegate );
  103. bool returnValue = (delegate->buttonNumber != 1 );
  104. return returnValue;
  105. }