UI.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. "atomic component";
  2. var WIDTH = Atomic.graphics.width - 100;
  3. var HEIGHT = Atomic.graphics.height - 100;
  4. var home = "https://www.atomicgameengine.com";
  5. var bookmarks = {
  6. "Atomic": "https://www.atomicgameengine.com",
  7. "Google": "https://www.google.com",
  8. "YouTube": "https://www.youtube.com",
  9. "Steam": "https://store.steampowered.com",
  10. "Reddit": "https://www.reddit.com/r/gamedev",
  11. "Penny Arcade": "https://www.penny-arcade.com/"
  12. };
  13. // Create the UI view
  14. var view = new Atomic.UIView();
  15. var newTabButton;
  16. var newTabContent;
  17. // WebBrowser example component
  18. exports.component = function(self) {
  19. var window = new Atomic.UIWindow();
  20. window.text = "UIPlatformWebView Example";
  21. window.setSize(WIDTH, HEIGHT);
  22. var mainLayout = new Atomic.UILayout();
  23. mainLayout.axis = Atomic.UI_AXIS.UI_AXIS_Y;
  24. mainLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  25. mainLayout.gravity = Atomic.UI_GRAVITY.UI_GRAVITY_ALL;
  26. var tabContainer = new Atomic.UITabContainer();
  27. tabContainer.gravity = Atomic.UI_GRAVITY.UI_GRAVITY_ALL;
  28. newTabButton = new Atomic.UIButton();
  29. newTabButton.text = " ";
  30. newTabButton.onClick = function() {
  31. createBrowserTab(tabContainer, home);
  32. // set the current page to the created page (ignoring the new tab dummy page)
  33. tabContainer.currentPage = tabContainer.numPages - 2;
  34. return true;
  35. };
  36. newTabContent = new Atomic.UIWidget();
  37. tabContainer.tabLayout.addChild(newTabButton);
  38. tabContainer.contentRoot.addChild(newTabContent);
  39. // change id, so we don't initiate a page change when clicked
  40. newTabButton.id = "NewTabButton";
  41. // create a startup with our home address
  42. createBrowserTab(tabContainer, home);
  43. tabContainer.currentPage = 0;
  44. // Add to main UI view and center
  45. mainLayout.addChild(tabContainer);
  46. window.addChild(mainLayout);
  47. view.addChild(window);
  48. window.center();
  49. };
  50. function createBookmarks(webView, layout, bookmarks) {
  51. for (var text in bookmarks) {
  52. var button = new Atomic.UIButton();
  53. button.text = text;
  54. button.skinBg = "TBButton.flat";
  55. // button layout and font desc
  56. var buttonLP = new Atomic.UILayoutParams();
  57. buttonLP.width = 120,
  58. buttonLP.height = 18;
  59. button.layoutParams = buttonLP;
  60. var fontDesc = new Atomic.UIFontDescription();
  61. fontDesc.size = 11;
  62. fontDesc.id = "Vera";
  63. button.fontDescription = fontDesc;
  64. layout.addChild(button);
  65. (function() {
  66. var url = bookmarks[text];
  67. button.onClick = function() { webView.loadURL(url); };
  68. })();
  69. }
  70. }
  71. function createBrowserTab(tabContainer, url) {
  72. var contentRoot = tabContainer.contentRoot;
  73. var tabLayout = tabContainer.tabLayout;
  74. var layout = new Atomic.UILayout();
  75. layout.axis = Atomic.UI_AXIS_Y;
  76. layout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  77. layout.gravity = Atomic.UI_GRAVITY.UI_GRAVITY_ALL;
  78. layout.spacing = 8;
  79. var tabButton = new Atomic.UIButton();
  80. tabButton.text = "Atomic Game Engine";
  81. // button layout and font desc
  82. var buttonLP = new Atomic.UILayoutParams();
  83. buttonLP.width = 160,
  84. buttonLP.height = 32;
  85. tabButton.layoutParams = buttonLP;
  86. var fontDesc = new Atomic.UIFontDescription();
  87. fontDesc.size = 11;
  88. fontDesc.id = "Vera";
  89. tabButton.fontDescription = fontDesc;
  90. // tabButtons with URL text by default open the URL upon clicking them
  91. // we don't want this behaviour
  92. tabButton.urlEnabled = false;
  93. tabLayout.addChildBefore(tabButton, newTabButton);
  94. // address bar
  95. var addressLayout = new Atomic.UILayout();
  96. addressLayout.gravity = Atomic.UI_GRAVITY.UI_GRAVITY_ALL;
  97. addressLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  98. // navigation buttons
  99. var backButton = new Atomic.UIButton();
  100. backButton.text = "Back";
  101. addressLayout.addChild(backButton);
  102. var fwdButton = new Atomic.UIButton();
  103. fwdButton.text = "Forward";
  104. addressLayout.addChild(fwdButton);
  105. var reloadButton = new Atomic.UIButton();
  106. reloadButton.text = "Reload";
  107. addressLayout.addChild(reloadButton);
  108. var homeButton = new Atomic.UIButton();
  109. homeButton.text = "Home";
  110. addressLayout.addChild(homeButton);
  111. var addressEdit = new Atomic.UIEditField();
  112. addressEdit.text = "https://www.atomicgameengine.com";
  113. addressEdit.gravity = Atomic.UI_GRAVITY.UI_GRAVITY_ALL;
  114. addressLayout.addChild(addressEdit);
  115. layout.addChild(addressLayout);
  116. var webView = new Atomic.UIPlatformWebView();
  117. webView.gravity = Atomic.UI_GRAVITY.UI_GRAVITY_ALL;
  118. webView.setFocus(true);
  119. webView.loadURL(home);
  120. // bookmark bar
  121. var bookmarkLayout = new Atomic.UILayout();
  122. bookmarkLayout.gravity = Atomic.UI_GRAVITY.UI_GRAVITY_ALL;
  123. createBookmarks( webView, bookmarkLayout, bookmarks);
  124. layout.addChild(bookmarkLayout);
  125. layout.addChild(webView);
  126. contentRoot.addChildBefore(layout, newTabContent);
  127. // initial state
  128. // reloadButton.disable();
  129. // backButton.disable();
  130. // fwdButton.disable();
  131. // go home
  132. homeButton.onClick = function() { webView.loadURL(home); }
  133. // reload
  134. reloadButton.onClick = function() { webView.reload(); };
  135. // Forward/Back
  136. fwdButton.onClick = function() { webView.goForward(); };
  137. backButton.onClick = function() { webView.goBack(); };
  138. webView.subscribeToEvent(addressEdit, Atomic.UIWidgetEditCompleteEvent(function(ev) {
  139. var url = addressEdit.text;
  140. url = url.replace(" ", "%20");
  141. if (url.indexOf(".") == -1) {
  142. url = "http://www.google.com/search?q=" + url;
  143. }
  144. if (url.indexOf("://") == -1) {
  145. url = "https://" + url;
  146. }
  147. if (!url.length)
  148. return;
  149. webView.request(url);
  150. }));
  151. }