not really known
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.

125 lines
3.6 KiB

  1. // Utility functions
  2. // Force Enyo to process ondragover event
  3. document.ondragover = enyo.dispatch;
  4. // Namespace
  5. FoodChain = {};
  6. // Game context handling
  7. FoodChain.context = {
  8. score: 0,
  9. game: "",
  10. level: 0,
  11. language: "en",
  12. object: null
  13. };
  14. FoodChain.saveContext = function() {
  15. var datastoreObject = FoodChain.activity.getDatastoreObject();
  16. var jsonData = JSON.stringify({
  17. score: FoodChain.context.score,
  18. game: FoodChain.context.game,
  19. level: FoodChain.context.level,
  20. language: l10n.language.code
  21. });
  22. datastoreObject.setDataAsText(jsonData);
  23. datastoreObject.save(function() {
  24. });
  25. };
  26. FoodChain.loadContext = function(callback) {
  27. var datastoreObject = FoodChain.activity.getDatastoreObject();
  28. datastoreObject.loadAsText(function (error, metadata, data) {
  29. var context = JSON.parse(data);
  30. if (context == null) return;
  31. if (context.score) FoodChain.context.score = parseInt(context.score);
  32. if (context.game) FoodChain.context.game = context.game;
  33. if (context.level) FoodChain.context.level = parseInt(context.level);
  34. if (context.language) FoodChain.context.language = l10n.language.code = context.language;
  35. callback();
  36. });
  37. };
  38. // Home handling
  39. FoodChain.goHome = function() {
  40. if (FoodChain.context.home != null) {
  41. FoodChain.context.game = "";
  42. FoodChain.context.home.setLocale()
  43. FoodChain.context.home.renderInto(document.getElementById("body"));
  44. }
  45. };
  46. // Sugar interface
  47. FoodChain.setLocale = function() {
  48. document.getElementById("en-button").classList.remove('active');
  49. document.getElementById("fr-button").classList.remove('active');
  50. document.getElementById("pt_BR-button").classList.remove('active');
  51. if (l10n.language.code.indexOf("en") == 0) { document.getElementById("en-button").classList.add('active'); }
  52. else if (l10n.language.code.indexOf("fr") == 0) { document.getElementById("fr-button").classList.add('active'); }
  53. else if (l10n.language.code.indexOf("pt_BR") == 0) { document.getElementById("pt_BR-button").classList.add('active'); }
  54. if (FoodChain.context.object != null)
  55. FoodChain.context.object.setLocale();
  56. }
  57. FoodChain.log = function(msg) {
  58. console.log(msg);
  59. };
  60. // Add and remove a class to an element
  61. FoodChain.addRemoveClass = function(element, toAdd, toRemove) {
  62. element.removeClass(toRemove);
  63. element.addClass(toAdd);
  64. };
  65. // "Old style" sleep function
  66. FoodChain.sleep = function(delay) {
  67. var start = new Date().getTime();
  68. while (new Date().getTime() < start + delay);
  69. };
  70. // Create a object respecting a condition on a set of object
  71. FoodChain.createWithCondition = function(create, condition, set) {
  72. var conditionValue;
  73. var newObject;
  74. var time = 0;
  75. do {
  76. conditionValue = true;
  77. newObject = create();
  78. for (var i = 0; conditionValue && i < set.length ; i++) {
  79. conditionValue = condition(newObject, set[i]);
  80. }
  81. time++;
  82. } while (!conditionValue && time < 12); // time to avoid infinite or too long loop in very complex situation
  83. if (!conditionValue)
  84. FoodChain.log("WARNING: out of pre-requisite creating "+newObject.id);
  85. return newObject;
  86. };
  87. // Test if two sound strings matchs ignoring audio directory
  88. FoodChain.soundMatch = function(s1, s2) {
  89. var l1 = s1.length-1;
  90. while (l1 > 0 && s1[l1] != '/') l1--;
  91. var l2 = s2.length-1;
  92. while (l2 > 0 && s2[l2] != '/') l2--;
  93. return s1.substring(l1) == s2.substring(l2);
  94. };
  95. // Get zoom level
  96. FoodChain.getZoomLevel = function() {
  97. var wsize = window.innerWidth;
  98. var zoom;
  99. if (wsize <= 480) {
  100. zoom = 0.4;
  101. } else if (wsize <= 640) {
  102. zoom = 0.5;
  103. } else if (wsize <= 854) {
  104. zoom = 0.62;
  105. } else if (wsize <= 960) {
  106. zoom = 0.72;
  107. } else if (wsize <= 1024) {
  108. zoom = 0.88;
  109. } else {
  110. zoom = 1;
  111. }
  112. return zoom;
  113. };