Buy.qml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /**
  2. * Copyright (c) 2011 Nokia Corporation.
  3. */
  4. import QtQuick 1.0
  5. import "Game.js" as GameScript
  6. import IAP 1.0 // In-App Purchase API, custom QML item
  7. Item {
  8. id: container
  9. width: gameArea.width * 0.8
  10. height: gameArea.height * 0.6
  11. opacity: 0
  12. state: "hidden"
  13. property bool levelDownloaded: false
  14. property int levelCountBeforePurchase: 0
  15. /**
  16. * Hides the list of purchasable items.
  17. */
  18. function doBack()
  19. {
  20. messageProgress.hideOngoingMessage();
  21. container.state = "hidden";
  22. // Cancels only level sis package loading from backend
  23. //iap.cancel();
  24. }
  25. /**
  26. * Close view on error
  27. */
  28. function error()
  29. {
  30. console.log("Error, close view");
  31. gameArea.closeBuyView();
  32. }
  33. /**
  34. * If a level was downloaded, reloads the plugins (i.e. updates the list
  35. * of levels in the main menu) and closes the buy view.
  36. */
  37. function foreground()
  38. {
  39. // If application gets to foreground even when level is downloaded
  40. // and installing is ongoing or ended, the level list updated.
  41. if (levelDownloaded) {
  42. console.log("Level was loaded");
  43. gameArea.reloadPlugins();
  44. // Check is level already installed
  45. if (levelCountBeforePurchase != GameEngine.pluginList().length) {
  46. // Level was installed. Close Buy view and go to main menu
  47. console.log("Level was installed");
  48. gameArea.closeBuyView();
  49. }
  50. }
  51. }
  52. /**
  53. * Finds product IDs from the back-end server for the "quickhit" product.
  54. */
  55. function searchIapDataFromBackend()
  56. {
  57. console.log("searchIapDataFromBackend()");
  58. iap.getIapDataFromBackend("quickhit");
  59. // Response to onIapBackendDataReaded
  60. }
  61. /**
  62. * Finds product metadata from Nokia Store.
  63. */
  64. function searchProductMetaDataFromStore()
  65. {
  66. if (iap.iapready) {
  67. messageProgress.showMessageOngoing("Searching products from Store...");
  68. var count = iap.productDataCount();
  69. for (var i = 0; i < count; i++) {
  70. var product = iap.productDataAt(i);
  71. iap.getIapDataFromStore(product.id);
  72. // Response to onProductDataRead
  73. }
  74. }
  75. }
  76. /**
  77. * Adds a product to the list model containing the purchasable items.
  78. * @param product The product to add.
  79. */
  80. function setProductDataToModel(product)
  81. {
  82. // Get current level list
  83. var plugins = GameEngine.pluginList();
  84. levelCountBeforePurchase = plugins.length;
  85. // Do not show existing levels in the purchase list
  86. var levelExists = false;
  87. if (plugins.length > 0) {
  88. for (var i = 0; i < plugins.length; i++) {
  89. var level = plugins[i];
  90. if (level.iapid == product.id) {
  91. levelExists = true;
  92. break;
  93. }
  94. }
  95. }
  96. // Add project to model
  97. if (!levelExists) {
  98. // Fill model with Nokia Store Publishing data and
  99. // data from our own backend server
  100. // Level for sale
  101. listModel.append({ "iapId" : product.id,
  102. "displayName" : product.title,
  103. "price" : product.price,
  104. "thumbnail" : product.thumbnail,
  105. "valid" : "1" });
  106. }
  107. if (listModel.count < 1) {
  108. message.showMessage("No new levels available", 5000);
  109. noLevelsTimer.restart();
  110. }
  111. }
  112. /**
  113. * Remove purchased product from the list model.
  114. * @param productId The ID of the purchased product to remove.
  115. */
  116. function removeProductFromDataModel(productId)
  117. {
  118. for (var i = 0; i < listModel.count; i++) {
  119. if (listModel.get(i).iapId == productId) {
  120. listModel.remove(i);
  121. break;
  122. }
  123. }
  124. }
  125. Timer {
  126. id: noLevelsTimer
  127. interval: 4000
  128. repeat: false
  129. running: false
  130. onTriggered: {
  131. gameArea.closeBuyView();
  132. }
  133. }
  134. Rectangle {
  135. id: background
  136. color: "black"
  137. border.color: "white"
  138. border.width: 2
  139. radius: 8
  140. smooth: true
  141. anchors.fill: parent
  142. opacity: listView.opacity > 0 ? 0.7 : 0
  143. }
  144. IAPurchase {
  145. id: iap
  146. onIapCreatedAndReady: {
  147. console.log("onIapCreatedAndReady");
  148. messageProgress.showMessageOngoing("Reading product information");
  149. searchIapDataFromBackend();
  150. }
  151. onIapBackendDataReaded: {
  152. console.log("onIapBackendDataReaded");
  153. messageProgress.hideOngoingMessage();
  154. message.showMessage("Product information read", 5000);
  155. console.log("Backend product count is " + productDataCount());
  156. if (productDataCount() > 0) {
  157. searchProductMetaDataFromStore();
  158. }
  159. }
  160. onIapBackendDataReadError: {
  161. console.log("onIapBackendDataReadError");
  162. messageProgress.hideOngoingMessage();
  163. message.showErrorMessage("Failed to read product information", 5000);
  164. container.error();
  165. }
  166. onProductDataRead: {
  167. console.log("onProductDataRead: " + product.title);
  168. messageProgress.hideOngoingMessage();
  169. setProductDataToModel(product);
  170. if (listModel.count > 0) {
  171. container.state = "levelsShown";
  172. }
  173. }
  174. onProductDataReadError: {
  175. console.log("onProductDataReadError");
  176. messageProgress.hideOngoingMessage();
  177. message.showErrorMessage("Failed to read product information", 5000);
  178. container.error();
  179. }
  180. onProductDoesNotExists: {
  181. console.log("onProductDoesNotExists: " + productId);
  182. messageProgress.hideOngoingMessage();
  183. message.showErrorMessage("Product does not exist", 5000);
  184. container.error();
  185. }
  186. onProductPurchaseDone: {
  187. console.log("onProductPurchaseDone: " + productId);
  188. messageProgress.hideOngoingMessage();
  189. message.showMessage("Product purchased", 5000);
  190. }
  191. onProductPurchaseError: {
  192. console.log("onProductPurchaseError");
  193. messageProgress.hideOngoingMessage();
  194. message.showErrorMessage("Purchase cancelled", 5000);
  195. container.error();
  196. }
  197. onDownloadingProduct: {
  198. console.log("onDownloadingProduct: " + productId);
  199. messageProgress.showMessageOngoing("Downloading...");
  200. }
  201. onDownloadProgress: {
  202. messageProgress.showMessageProgress(bytesReceived, bytesTotal);
  203. }
  204. onDownloaded: {
  205. levelDownloaded = true;
  206. console.log("onDownloaded");
  207. messageProgress.hideOngoingMessage();
  208. }
  209. onDownloadingError: {
  210. levelDownloaded = false;
  211. console.log("onDownloadingError");
  212. messageProgress.hideOngoingMessage();
  213. message.showErrorMessage("Failed to download product", 5000);
  214. container.error();
  215. }
  216. onInstallingProduct: {
  217. console.log("onInstallingProduct");
  218. messageProgress.hideOngoingMessage();
  219. }
  220. onBusy: {
  221. console.log("onBusy");
  222. messageProgress.hideOngoingMessage();
  223. message.showErrorMessage("In-App Payment is busy", 5000);
  224. }
  225. }
  226. Component {
  227. id: listDelegate
  228. Item {
  229. id: listItem
  230. height: 180
  231. width: listView.width
  232. property bool isValid: valid
  233. Rectangle {
  234. radius: 4
  235. border.color: itemMouseArea.pressed ? "red": "white"
  236. border.width: itemMouseArea.pressed ? 4: 2
  237. anchors.fill: parent
  238. color: "transparent"
  239. gradient: Gradient {
  240. GradientStop { position: 0.0; color: "transparent" }
  241. GradientStop { position: 0.7; color: "black" }
  242. GradientStop { position: 0.9; color: "black" }
  243. GradientStop { position: 1.0; color: "#ee01e6" }
  244. }
  245. }
  246. Row {
  247. anchors.fill: listItem.paddingItem
  248. anchors.verticalCenter: parent.verticalCenter
  249. Image {
  250. smooth: true
  251. source: thumbnail
  252. fillMode: Image.PreserveAspectFit
  253. height: listItem.height - 25
  254. width: listItem.height - 25
  255. }
  256. Column {
  257. id: column
  258. anchors.verticalCenter: parent.verticalCenter
  259. Image {
  260. smooth: true
  261. source: "qrc:/gfx/shopping_cart.png"
  262. fillMode: Image.PreserveAspectFit
  263. }
  264. Text {
  265. smooth: true
  266. font.pixelSize: 18
  267. color: "white"
  268. text: displayName
  269. width: column.width * 0.5
  270. wrapMode: Text.WordWrap
  271. }
  272. Text {
  273. smooth: true
  274. font.pixelSize: 18
  275. color: "white"
  276. text: isValid ? "Buy " + price : ""
  277. wrapMode: Text.WordWrap
  278. }
  279. }
  280. }
  281. MouseArea {
  282. id: itemMouseArea
  283. anchors.fill: parent
  284. onClicked: {
  285. var currentItem = listModel.get(index);
  286. if (currentItem.valid) {
  287. iap.buyProduct(currentItem.iapId);
  288. }
  289. else {
  290. message.showErrorMessage("No products", 4000);
  291. }
  292. }
  293. }
  294. }
  295. }
  296. ListModel {
  297. id: listModel
  298. }
  299. ListView {
  300. id: listView
  301. anchors.centerIn: parent
  302. width: parent.width * 0.9
  303. height: parent.height * 0.9
  304. clip: true
  305. delegate: listDelegate
  306. model: listModel
  307. spacing: 15
  308. }
  309. Rectangle {
  310. id: scrollbar
  311. clip: true
  312. anchors.left: listView.right
  313. anchors.margins: 5
  314. opacity: 0.4
  315. y: listView.visibleArea.yPosition * listView.height + listView.y
  316. width: 5
  317. height: listView.visibleArea.heightRatio * listView.height
  318. color: "gray"
  319. }
  320. states: [
  321. State {
  322. name: "hidden"
  323. },
  324. State {
  325. name: "levelsShown"
  326. }
  327. ]
  328. transitions: [
  329. Transition {
  330. from: "*"; to: "hidden"
  331. ParallelAnimation {
  332. NumberAnimation { target: container; property: "x"; to: container.width * -0.8;
  333. duration: 500; easing.type: Easing.InOutBack }
  334. PropertyAnimation { target: container; property: "opacity"; to: 0; duration:500 }
  335. }
  336. },
  337. Transition {
  338. from: "*"; to: "levelsShown"
  339. SequentialAnimation {
  340. SequentialAnimation {
  341. PropertyAction { target: container; property: "x"; value: container.width * -1.2 }
  342. PropertyAction { target: container; property: "y";
  343. value: (gameArea.height - container.height) *0.7 }
  344. PropertyAction { target: container; property: "opacity"; value: 1 }
  345. NumberAnimation { target: container; property: "x";
  346. to: (gameArea.width - container.width) *0.5;
  347. duration: 500; easing.type: Easing.InOutBack }
  348. }
  349. }
  350. }
  351. ]
  352. }