You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 line
3.4 KiB

  1. local device = {}
  2. device.platform = "unknown"
  3. device.model = "unknown"
  4. local sharedApplication = cc.Application:getInstance()
  5. local target = sharedApplication:getTargetPlatform()
  6. if target == cc.PLATFORM_OS_WINDOWS then
  7. device.platform = "windows"
  8. elseif target == cc.PLATFORM_OS_MAC then
  9. device.platform = "mac"
  10. elseif target == cc.PLATFORM_OS_ANDROID then
  11. device.platform = "android"
  12. elseif target == cc.PLATFORM_OS_ANDROID then
  13. device.platform = "android"
  14. elseif target == cc.PLATFORM_OS_IPHONE or target == cc.PLATFORM_OS_IPAD then
  15. device.platform = "ios"
  16. if target == cc.PLATFORM_OS_IPHONE then
  17. device.model = "iphone"
  18. else
  19. device.model = "ipad"
  20. end
  21. elseif target == cc.PLATFORM_OS_WINRT or target == cc.PLATFORM_OS_WIN8 then
  22. device.platform = "wp"
  23. if target == cc.PLATFORM_OS_WINRT then
  24. device.model = "winrt"
  25. else
  26. device.model = "wp8"
  27. end
  28. end
  29. local language_ = sharedApplication:getCurrentLanguage()
  30. if language_ == cc.LANGUAGE_CHINESE then
  31. language_ = "cn"
  32. else
  33. language_ = "en"
  34. end
  35. device.language = language_
  36. device.writablePath = cc.FileUtils:getInstance():getWritablePath()
  37. device.directorySeparator = "/"
  38. device.pathSeparator = ":"
  39. if device.platform == "windows" then
  40. device.directorySeparator = "\\"
  41. device.pathSeparator = ";"
  42. end
  43. print("# device.platform = " .. device.platform)
  44. print("# device.model = " .. device.model)
  45. print("# device.language = " .. device.language)
  46. print("# device.writablePath = " .. device.writablePath)
  47. print("# device.directorySeparator = " .. device.directorySeparator)
  48. print("# device.pathSeparator = " .. device.pathSeparator)
  49. print("#")
  50. function device.showActivityIndicator()
  51. cc.Native:showActivityIndicator()
  52. end
  53. function device.hideActivityIndicator()
  54. cc.Native:hideActivityIndicator()
  55. end
  56. function device.showAlert(title, message, buttonLabels, listener)
  57. if type(buttonLabels) ~= "table" then
  58. buttonLabels = {tostring(buttonLabels)}
  59. end
  60. if device.platform == "android" then
  61. local tempListner = function(event)
  62. if type(event) == "string" then
  63. event = require("framework.json").decode(event)
  64. event.buttonIndex = tonumber(event.buttonIndex)
  65. end
  66. if listener then listener(event) end
  67. end
  68. luaj.callStaticMethod("org/cocos2dx/utils/PSNative", "createAlert", {title, message, buttonLabels, tempListner}, "(Ljava/lang/String;Ljava/lang/String;Ljava/util/Vector;I)V");
  69. else
  70. local defaultLabel = ""
  71. if #buttonLabels > 0 then
  72. defaultLabel = buttonLabels[1]
  73. table.remove(buttonLabels, 1)
  74. end
  75. cc.Native:createAlert(title, message, defaultLabel)
  76. for i, label in ipairs(buttonLabels) do
  77. cc.Native:addAlertButton(label)
  78. end
  79. if type(listener) ~= "function" then
  80. listener = function() end
  81. end
  82. cc.Native:showAlert(listener)
  83. end
  84. end
  85. function device.cancelAlert()
  86. cc.Native:cancelAlert()
  87. end
  88. function device.getOpenUDID()
  89. return cc.Native:getOpenUDID()
  90. end
  91. function device.openURL(url)
  92. cc.Native:openURL(url)
  93. end
  94. function device.showInputBox(title, message, defaultValue)
  95. title = title or "INPUT TEXT"
  96. message = message or "INPUT TEXT, CLICK OK BUTTON"
  97. defaultValue = defaultValue or ""
  98. return cc.Native:getInputText(title, message, defaultValue)
  99. end
  100. return device