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.

App.cpp 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. #include "App.h"
  2. #include "../tools/Scheduler.h"
  3. #include "../interfaces/components/TabbedComponent.h"
  4. #include "../parsers/markup/ntrml/NTRMLParser.h"
  5. #include "../parsers/markup/html/HTMLParser.h"
  6. #include "../interfaces/components/DocumentComponent.h"
  7. //#include "interfaces/components/ComponentBuilder.h"
  8. #include "../interfaces/components/InputComponent.h"
  9. #include "../interfaces/components/ImageComponent.h"
  10. #include "../interfaces/components/TabbedComponent.h"
  11. #include "../interfaces/components/ButtonComponent.h"
  12. #include <fstream>
  13. #ifndef _WIN32
  14. #include <glob.h>
  15. #else
  16. #include "../win32-glob.h"
  17. #endif
  18. // why can't I const this?
  19. std::unique_ptr<Scheduler> scheduler = std::make_unique<Scheduler>();
  20. void doOnClick(std::string type, Component *component, Window *win) {
  21. if (type=="back") {
  22. component->onClick=[win]() {
  23. std::cout << "Browser::doOnClick.navBackButton->onClick - Back" << std::endl;
  24. TabbedComponent *pTabComponent = dynamic_cast<TabbedComponent*>(win->tabComponent.get());
  25. if (pTabComponent) {
  26. if (pTabComponent->selectedTabId) {
  27. pTabComponent->selectedTab->get()->history->print();
  28. pTabComponent->selectedTab->get()->history->back();
  29. } else {
  30. std::cout << "Browser::doOnClick.navBackButton->onClick - no tab selected" << std::endl;
  31. }
  32. } else {
  33. std::cout << "Browser::doOnClick.navBackButton->onClick - no tabbed component" << std::endl;
  34. }
  35. };
  36. } else if (type=="forward") {
  37. component->onClick=[win]() {
  38. //std::cout << "Forward" << std::endl;
  39. TabbedComponent *pTabComponent = dynamic_cast<TabbedComponent*>(win->tabComponent.get());
  40. if (pTabComponent) {
  41. if (pTabComponent->selectedTabId) {
  42. pTabComponent->selectedTab->get()->history->forward();
  43. }
  44. }
  45. };
  46. } else if (type=="refresh") {
  47. component->onClick=[win]() {
  48. std::shared_ptr<DocumentComponent> docComponent = win->getActiveDocumentComponent();
  49. if (docComponent) {
  50. std::cout << "Refreshing " << docComponent->currentURL << std::endl;
  51. // now tell it to navigate somewhere
  52. docComponent->navTo(docComponent->currentURL.toString());
  53. }
  54. };
  55. } else if (type=="go") {
  56. component->onClick=[win]() {
  57. // get address bar value and nav away
  58. std::string value;
  59. InputComponent *p_addressComponent = dynamic_cast<InputComponent*>(win->addressComponent.get());
  60. if (!p_addressComponent) {
  61. std::cout << "Browser:zdoOnClick.navAddressBar->onClick - can't find address bar in windows " << std::endl;
  62. return;
  63. }
  64. //std::cout << "Browser:zdoOnClick.navAddressBar->onClick - got " << value << std::endl;
  65. TabbedComponent *p_tabComponent = dynamic_cast<TabbedComponent*>(win->tabComponent.get());
  66. if (p_tabComponent) {
  67. if (!p_tabComponent->tabs.size()) {
  68. p_tabComponent->addTab("Loading...");
  69. p_tabComponent->selectTab(p_tabComponent->tabs.back());
  70. win->renderDirty = true;
  71. win->render(); // display loading tab before IO
  72. }
  73. }
  74. std::shared_ptr<DocumentComponent> docComponent = win->getActiveDocumentComponent();
  75. if (docComponent) {
  76. //std::cout << "Browser:zdoOnClick.navAddressBar->onClick - Found an active document component" << std::endl;
  77. // now tell it to navigate somewhere
  78. docComponent->navTo(value);
  79. } else {
  80. std::cout << "Browser:zdoOnClick.navAddressBar->onClick - No active document component" << std::endl;
  81. }
  82. };
  83. } else if (type=="nextTab") {
  84. component->onClick=[win]() {
  85. // get tab value and nav away
  86. };
  87. } else if (type=="prevTab") {
  88. component->onClick=[win]() {
  89. // get tab value and nav away
  90. };
  91. }
  92. }
  93. std::shared_ptr<Node> App::loadTheme(std::string filename) {
  94. std::string ntrml, line;
  95. std::ifstream myfile(filename);
  96. if (myfile.is_open()) {
  97. while(getline(myfile, line)) {
  98. ntrml += line;
  99. }
  100. myfile.close();
  101. } else {
  102. std::cout << "Couldnt read " << filename << std::endl;
  103. }
  104. NTRMLParser uiParser;
  105. //std::cout << "Browser read [" << ntrml << "]" << std::endl;
  106. return uiParser.parse(ntrml);
  107. }
  108. void App::destroyTheme() {
  109. for(auto win: this->windows) {
  110. if (win->tabComponent) {
  111. TabbedComponent *tabbedComponent = dynamic_cast<TabbedComponent *>(win->tabComponent.get());
  112. for(auto tab: tabbedComponent->tabs) {
  113. tabbedComponent->removeTab(tab->id);
  114. }
  115. }
  116. win->addressComponent = nullptr;
  117. win->tabComponent = nullptr;
  118. for(auto layer: win->ui->layers) {
  119. layer.reset();
  120. }
  121. win->ui->layers.clear();
  122. }
  123. }
  124. void App::rebuildTheme() {
  125. for(auto win: this->windows) {
  126. // convert uiRootNode into a component list
  127. std::shared_ptr<Component> rootComponent = std::make_shared<Component>();
  128. rootComponent->name = "rootComponent of browser";
  129. // we build one global tree that each layer has a child node in
  130. this->layers.clear(); // nuke any layers we have
  131. this->createComponentTree(this->uiRootNode, rootComponent, win);
  132. //Component::printComponentTree(rootComponent, 0);
  133. // these probably should be fatal
  134. if (!win->addressComponent) {
  135. std::cout << "ERROR: no addressComponent" << std::endl;
  136. }
  137. if (!win->tabComponent) {
  138. std::cout << "ERROR: no tabComponent" << std::endl;
  139. }
  140. // we want each window to has it's own component tree, so each address bar can have different values
  141. size_t layerCount = 0;
  142. for(auto layer: this->layers) {
  143. // set proper vertices
  144. layer->resize(win->windowWidth, win->windowHeight);
  145. // transfer layer
  146. win->ui->layers.push_back(layer);
  147. // debug
  148. /*
  149. std::cout << "layer: " << layerCount << std::endl;
  150. Component::printComponentTree(layer, 0);
  151. */
  152. layerCount++;
  153. layer.reset(); // deallocate our local layers
  154. }
  155. rootComponent.reset();
  156. //newWindow->rootComponent = rootComponent;
  157. //newWindow->ui->rootComponent = rootComponent;
  158. win->renderDirty = true;
  159. }
  160. }
  161. void App::transferTheme(std::string filename) {
  162. // assuming we don't need the NodeTree to get our values
  163. this->uiRootNode.reset();
  164. this->uiRootNode = this->loadTheme(filename);
  165. for(auto win: this->windows) {
  166. glfwMakeContextCurrent(win->window);
  167. TabbedComponent *oldTabbedComponent = nullptr;
  168. if (win->tabComponent) {
  169. oldTabbedComponent = dynamic_cast<TabbedComponent *>(win->tabComponent.get());
  170. }
  171. win->tabComponent = nullptr; // release to release
  172. win->addressComponent = nullptr; // I think this is ok to release now
  173. // convert uiRootNode into a component list
  174. std::shared_ptr<Component> rootComponent = std::make_shared<Component>();
  175. rootComponent->name = "rootComponent of browser";
  176. this->layers.clear(); // nuke any layers we had (doesn't nuke window layers)
  177. // we build one global tree that each layer has a child node in
  178. this->createComponentTree(this->uiRootNode, rootComponent, win);
  179. //Component::printComponentTree(rootComponent, 0);
  180. // these probably should be fatal
  181. if (!win->addressComponent) {
  182. std::cout << "ERROR: no addressComponent" << std::endl;
  183. }
  184. if (!win->tabComponent) {
  185. std::cout << "ERROR: no tabComponent" << std::endl;
  186. }
  187. // ok now we need to transfer the tabs over
  188. if (oldTabbedComponent) {
  189. // should have been set in createComponentTree
  190. TabbedComponent *newTabbedComponent = dynamic_cast<TabbedComponent *>(win->tabComponent.get());
  191. if (newTabbedComponent) {
  192. size_t oldSelectedTabId = oldTabbedComponent->selectedTabId;
  193. std::shared_ptr<Tab> newSelectedTab = nullptr;
  194. std::cout << "going to copy " << oldTabbedComponent->tabs.size() << " tab(s) over to new theme" << std::endl;
  195. for(auto tab: oldTabbedComponent->tabs) {
  196. DocumentComponent *oldDocComponent = dynamic_cast<DocumentComponent *>(tab->contents.get());
  197. if (oldDocComponent) {
  198. //std::cout << "Reading DOM" << std::endl;
  199. //printNode(oldDocComponent->domRootNode, 0);
  200. std::shared_ptr<Tab> nTab = newTabbedComponent->addTab(tab->titleBox->text);
  201. newTabbedComponent->selectTab(nTab);
  202. // maybe faster if we can skip the component tree rebuild
  203. newTabbedComponent->loadDomIntoTab(oldDocComponent->domRootNode, tab->titleBox->text);
  204. //newTabbedComponent->documentComponent.get()
  205. DocumentComponent *newDocComponent = dynamic_cast<DocumentComponent *>(newTabbedComponent->tabs.back()->contents.get());
  206. if (newDocComponent) {
  207. // really slow and we don't need to do this for all tabs
  208. // not sure why this is required...
  209. //newDocComponent->setDOM(oldDocComponent->domRootNode);
  210. //std::cout << "Checking DOM" << std::endl;
  211. //printNode(newTabbedComponent->documentComponent->, 0);
  212. newDocComponent->render(); // flush out components
  213. //newDocComponent->domDirty = true; // is most accurate
  214. } else {
  215. std::cout << "new tab had no doc" << std::endl;
  216. }
  217. if (tab->id == oldSelectedTabId) {
  218. // translate old selected id into new id
  219. newSelectedTab = nTab;
  220. }
  221. } else {
  222. std::cout << "old tab had no doc" << std::endl;
  223. }
  224. // this breaks everything
  225. //oldTabbedComponent->removeTab(tab->id);
  226. }
  227. if (newSelectedTab) {
  228. newTabbedComponent->selectTab(newSelectedTab);
  229. /*
  230. DocumentComponent *newDocComponent = dynamic_cast<DocumentComponent *>(newTabbedComponent->documentComponent.get());
  231. if (newDocComponent) {
  232. // FIXME: can only in render active window/ogl context
  233. newDocComponent->render(); // flush out components
  234. } else {
  235. std::cout << "new selected tab had no doc" << std::endl;
  236. }
  237. */
  238. } else {
  239. std::cout << "no new selected tab" << std::endl;
  240. }
  241. /*
  242. std::cout << "new tabbed component has " << newTabbedComponent->tabs.size() << " tab(s) over from old theme" << std::endl;
  243. for(auto tab: newTabbedComponent->tabs) {
  244. DocumentComponent *newDocComponent = dynamic_cast<DocumentComponent *>(tab->contents.get());
  245. std::cout << "checking tab DOM" << std::endl;
  246. printNode(newDocComponent->domRootNode, 0);
  247. }
  248. if (newTabbedComponent->documentComponent) {
  249. DocumentComponent *newDocComponent = dynamic_cast<DocumentComponent *>(newTabbedComponent->documentComponent.get());
  250. std::cout << "checking selected DOM" << std::endl;
  251. printNode(newDocComponent->domRootNode, 0);
  252. }
  253. */
  254. } else {
  255. std::cout << "new theme didnt have tab component" << std::endl;
  256. }
  257. } else {
  258. std::cout << "old theme didnt have tab component" << std::endl;
  259. }
  260. // now we're officially done with old layer, nuked them
  261. for(auto layer: win->ui->layers) {
  262. layer.reset();
  263. }
  264. win->ui->layers.clear();
  265. // we want each window to has it's own component tree, so each address bar can have different values
  266. size_t layerCount = 0;
  267. for(auto layer: this->layers) {
  268. // set proper vertices
  269. layer->resize(win->windowWidth, win->windowHeight);
  270. // transfer layer
  271. win->ui->layers.push_back(layer);
  272. // debug
  273. /*
  274. std::cout << "layer: " << layerCount << std::endl;
  275. Component::printComponentTree(layer, 0);
  276. */
  277. layerCount++;
  278. layer.reset(); // deallocate our local layers
  279. }
  280. rootComponent.reset();
  281. //newWindow->rootComponent = rootComponent;
  282. //newWindow->ui->rootComponent = rootComponent;
  283. win->renderDirty = true;
  284. }
  285. }
  286. void App::NextTheme() {
  287. glob_t glob_result;
  288. static unsigned int t = 0;
  289. #ifndef _WIN32
  290. glob("*.ntrml", GLOB_TILDE, nullptr, &glob_result);
  291. #else
  292. glob("*.ntrml", GLOB_NOCHECK, nullptr, &glob_result);
  293. #endif
  294. t++;
  295. if (t == glob_result.gl_pathc) t = 0;
  296. /*
  297. for(unsigned int i = 0; i < glob_result.gl_pathc; ++i){
  298. if (t == i) std::cout << "*";
  299. std::cout << glob_result.gl_pathv[i] << std::endl;
  300. }
  301. */
  302. // this works decently, but only destroys first window
  303. /*
  304. this->destroyTheme();
  305. //deleteNode(this->uiRootNode);
  306. this->uiRootNode.reset();
  307. this->uiRootNode = this->loadTheme(std::string(glob_result.gl_pathv[t]));
  308. this->rebuildTheme();
  309. */
  310. this->transferTheme(glob_result.gl_pathv[t]);
  311. // start with removing existing theme
  312. // and replacing with new version of existing theme
  313. }
  314. // Previously defined components with functionality
  315. // 1+ image (no functionality)
  316. // 1+ tabbed document
  317. // 1+ back/fwd/reload/stop components
  318. // 1+ address bar
  319. void App::createComponentTree(const std::shared_ptr<Node> node, std::shared_ptr<Component> &parentComponent, std::shared_ptr<Window> win) {
  320. // FIXME: remove these 2 vars
  321. int winWidth = win->windowWidth;
  322. int winHeight = win->windowHeight;
  323. std::string tag;
  324. if (node == nullptr) {
  325. std::cout << "ComponentBuilder::build - node is null" << std::endl;
  326. return;
  327. }
  328. TagNode *tagNode = nullptr;
  329. if (node->nodeType == NodeType::TAG) {
  330. tagNode = dynamic_cast<TagNode*>(node.get());
  331. if (tagNode) {
  332. tag = tagNode->tag;
  333. }
  334. } else if (node->nodeType == NodeType::TEXT) {
  335. // just ignore text blocks
  336. return;
  337. tagNode = dynamic_cast<TagNode*>(node->parent.get());
  338. if (tagNode) {
  339. tag = tagNode->tag;
  340. }
  341. }
  342. std::shared_ptr<Component> component = nullptr;
  343. if (!tagNode) {
  344. // usually the root node...
  345. //std::cout << "!tagnode" << std::endl;
  346. component = std::make_shared<Component>();
  347. for (std::shared_ptr<Node> child : node->children) {
  348. createComponentTree(child, component, win);
  349. }
  350. return;
  351. }
  352. //std::cout << "Looking at tag[" << tag << "]" << std::endl;
  353. if (tag == "layer") {
  354. // the layering only works for default layout due to the ordering we're doign in window.render
  355. component = std::make_shared<Component>();
  356. this->layers.push_back(component);
  357. //parentComponent = component;
  358. //std::swap(component, parentComponent);
  359. //std::cout << "there are now " << this->layers.size() << " layers" << std::endl;
  360. } else if (tag == "body") {
  361. if (tagNode->properties.find("bgcolor") != tagNode->properties.end()) {
  362. std::stringstream ss;
  363. ss << std::hex << tagNode->properties["bgcolor"];
  364. ss >> win->clearColor;
  365. //std::cout << "set clear color " << std::hex << win->clearColor << std::dec << std::endl;
  366. float r = (static_cast<float>((win->clearColor >> 24) & 0xFF)) / 255;
  367. float g = (static_cast<float>((win->clearColor >> 16) & 0xFF)) / 255;
  368. float b = (static_cast<float>((win->clearColor >> 8) & 0xFF)) / 255;
  369. float a = (static_cast<float>((win->clearColor >> 0) & 0xFF)) / 255;
  370. glClearColor(r, g, b, a);
  371. }
  372. } else if (tag == "img") {
  373. std::string src = "anime.pnm";
  374. if (tagNode->properties.find("src") != tagNode->properties.end()) {
  375. src = tagNode->properties["src"];
  376. }
  377. //std::shared_ptr<ImageComponent> img = std::make_unique<ImageComponent>(src, winWidth * imgSetup.left.pct * 0.01 + imgSetup.left.px, winHeight * imgSetup.top.pct * 0.01 + imgSetup.top.px, imgSetup.width.px, imgSetup.height.px, winWidth, winHeight);
  378. std::shared_ptr<ImageComponent> img = std::make_unique<ImageComponent>(src, 0, 0, 512, 512, winWidth, winHeight);
  379. img->boundToPage = false; // have to set this before setUpUI
  380. img->setUpUI(tagNode->properties, win.get());
  381. // set up interactivity
  382. if (tagNode->properties.find("onClick") != tagNode->properties.end()) {
  383. img->isPickable = true;
  384. //doOnClick(std::string type, Component &component, Window *win)
  385. doOnClick(tagNode->properties["onClick"], img.get(), win.get());
  386. }
  387. img->isPickable = false;
  388. img->name = "img";
  389. if (tagNode->properties.find("name") != tagNode->properties.end()) {
  390. img->name = tagNode->properties["name"];
  391. }
  392. component = img;
  393. //std::cout << "Added img component to ui" << std::endl;
  394. } else if (tag == "font") {
  395. // TextComponent(const std::string &rawText, const int rawX, const int rawY, const unsigned int size, const bool bolded, const unsigned int hexColor, const int passedWindowWidth, const int passedWindowHeight);
  396. std::string label = "NeTRunner";
  397. if (node->children.size()) {
  398. TextNode *textNode = dynamic_cast<TextNode*>(node->children.front().get());
  399. if (textNode) {
  400. label = textNode->text;
  401. }
  402. }
  403. std::shared_ptr<TextComponent> text = std::make_unique<TextComponent>(label, 0, 0, 12, false, 0x000000FF, winWidth, winHeight);
  404. text->boundToPage = false; // have to set this before setUpUI
  405. text->win = win;
  406. // this may not work for the text shader... yea doesn't seem to
  407. text->setUpUI(tagNode->properties, win.get());
  408. text->isPickable = false;
  409. //text->resize(win->windowWidth, win->windowHeight); // force a re-raster
  410. // set up interactivity
  411. if (tagNode->properties.find("onClick") != tagNode->properties.end()) {
  412. text->isPickable = true;
  413. //doOnClick(std::string type, Component &component, Window *win)
  414. doOnClick(tagNode->properties["onClick"], text.get(), win.get());
  415. }
  416. component = text;
  417. } else if (tag == "box") {
  418. unsigned int color = 0x888888FF;
  419. if (tagNode->properties.find("color") != tagNode->properties.end()) {
  420. std::stringstream ss;
  421. ss << std::hex << tagNode->properties["color"];
  422. ss >> color;
  423. //std::cout << "read color " << tagNode->properties["color"] << " as " << std::hex << color << std::dec << std::endl;
  424. }
  425. // winHeight minus because box coordinates? yes
  426. //std::cout << "placing box at " << cX << "," << cY << " " << cW << "x" << cH << " color: " << std::hex << color << std::dec << std::endl;
  427. //std::shared_ptr<BoxComponent> box = std::make_unique<BoxComponent>(cX, winHeight - cH - cY, cW, cH, color, winWidth, winHeight);
  428. std::shared_ptr<BoxComponent> box = nullptr;
  429. if (node->children.size()) {
  430. box = std::make_unique<ButtonComponent>(0, 0, 1, 1, color, winWidth, winHeight);
  431. } else {
  432. box = std::make_unique<BoxComponent>(0, 0, 1, 1, color, winWidth, winHeight);
  433. }
  434. box->boundToPage = false; // have to set this before setUpUI
  435. box->setUpUI(tagNode->properties, win.get());
  436. box->isPickable = false;
  437. box->name = "box";
  438. if (tagNode->properties.find("name") != tagNode->properties.end()) {
  439. box->name = tagNode->properties["name"];
  440. }
  441. // set up interactivity
  442. if (tagNode->properties.find("hover") != tagNode->properties.end()) {
  443. box->isPickable = true;
  444. unsigned int hoverColor = 0;
  445. std::stringstream ss;
  446. ss << std::hex << tagNode->properties["hover"];
  447. ss >> hoverColor;
  448. //std::cout << "setHover Color " << std::hex << hoverColor << std::dec << std::endl;
  449. box->onMouseover = [box, win, hoverColor]() {
  450. //std::cout << "box->onMouseover" << std::endl;
  451. box->changeColor(hoverColor);
  452. win->renderDirty = true;
  453. };
  454. box->onMouseout = [box, win, color]() {
  455. //std::cout << "box->onMouseout" << std::endl;
  456. box->changeColor(color);
  457. win->renderDirty = true;
  458. };
  459. }
  460. if (tagNode->properties.find("onClick") != tagNode->properties.end()) {
  461. box->isPickable = true;
  462. //doOnClick(std::string type, Component &component, Window *win)
  463. doOnClick(tagNode->properties["onClick"], box.get(), win.get());
  464. }
  465. component = box;
  466. // if this button node has children, extract the text before discarding it
  467. if (node->children.size()) {
  468. TextNode *textNode = dynamic_cast<TextNode*>(node->children.front().get());
  469. if (textNode) {
  470. // so now cast back to a ButtonComponent and set it
  471. std::cout << "Box text: " << textNode->text << std::endl;
  472. /*
  473. ButtonComponent *buttonComponent = dynamic_cast<ButtonComponent*>(component.get());
  474. if (buttonComponent) {
  475. buttonComponent->value = textNode->text;
  476. buttonComponent->resizeToTextSize();
  477. buttonComponent->updateText();
  478. }
  479. */
  480. }
  481. }
  482. // manual quick add before we bail
  483. component->setParent(parentComponent);
  484. parentComponent->children.push_back(component);
  485. // skip my children
  486. return;
  487. } else if (tag == "input") {
  488. //std::shared_ptr<InputComponent> navAddressBar = std::make_unique<InputComponent>(192.0f, winHeight - 48.0f, winWidth - 384.0f, 24.0f, winWidth, winHeight);
  489. std::shared_ptr<InputComponent> navAddressBar = std::make_unique<InputComponent>(0, 0, 384.0f, 24.0f, winWidth, winHeight);
  490. navAddressBar->boundToPage = false; // have to set this before setUpUI
  491. navAddressBar->setUpUI(tagNode->properties, win.get());
  492. navAddressBar->name = "navAddressBar";
  493. navAddressBar->onEnter=[win](std::string value) {
  494. std::cout << "navAddressBar::onEnter got " << value << std::endl;
  495. TabbedComponent *p_tabComponent = dynamic_cast<TabbedComponent*>(win->tabComponent.get());
  496. if (p_tabComponent) {
  497. if (!p_tabComponent->tabs.size()) {
  498. p_tabComponent->addTab("Loading...");
  499. p_tabComponent->selectTab(p_tabComponent->tabs.back());
  500. win->renderDirty = true;
  501. win->render(); // display loading tab before IO
  502. }
  503. }
  504. std::shared_ptr<DocumentComponent> docComponent = win->getActiveDocumentComponent();
  505. if (docComponent) {
  506. std::cout << "Found an active document component" << std::endl;
  507. // now tell it to navigate somewhere
  508. docComponent->navTo(value);
  509. } else {
  510. std::cout << "No active document component" << std::endl;
  511. }
  512. };
  513. navAddressBar->win = win;
  514. win->addressComponent = navAddressBar;
  515. component = navAddressBar;
  516. } else if (tag == "tabselector") {
  517. std::shared_ptr<TabbedComponent> tabbedComponent = std::make_shared<TabbedComponent>(0, 0, static_cast<float>(winWidth), static_cast<float>(winHeight - 64), winWidth, winHeight);
  518. tabbedComponent->win = win;
  519. win->tabComponent = tabbedComponent;
  520. tabbedComponent->boundToPage = false; // have to set this before setUpUI
  521. tabbedComponent->setUpUI(tagNode->properties, win.get());
  522. tabbedComponent->name = "tabbedComponent";
  523. /*
  524. tabbedComponent->y = -64;
  525. tabbedComponent->uiControl.x = { 0 , 0 }; // 0
  526. tabbedComponent->uiControl.y = { 0 , -64 }; // -64px
  527. tabbedComponent->uiControl.w = { 100, 0 }; // 100%
  528. tabbedComponent->uiControl.h = { 100, -64 }; // 100% - 64px
  529. tabbedComponent->boundToPage = false;
  530. */
  531. component = tabbedComponent;
  532. // make the UI focus on it to relay keyboard events
  533. win->ui->relayKeyboardComponent = tabbedComponent;
  534. } else {
  535. std::cout << "Browser::createComponentTree - unknown tag[" << tag << "]" << std::endl;
  536. }
  537. if (!component) {
  538. component = std::make_shared<Component>();
  539. }
  540. // add it to our parent
  541. component->setParent(parentComponent);
  542. parentComponent->children.push_back(component);
  543. // , children count: " << parentComponent->children.size()
  544. // create children elements
  545. for (std::shared_ptr<Node> child : node->children) {
  546. createComponentTree(child, component, win);
  547. }
  548. component.reset();
  549. }
  550. void App::addJSDebuggerWindow() {
  551. windowCounter++;
  552. std::shared_ptr<Window> newWindow = std::make_shared<Window>();
  553. newWindow->id = windowCounter;
  554. newWindow->windowWidth = 1024;
  555. newWindow->windowHeight = 640;
  556. newWindow->init(); // load our UI into it
  557. std::shared_ptr<Component> p_rootComponent = std::make_shared<Component>();
  558. p_rootComponent->name = "rootComponent of jsconsole";
  559. std::shared_ptr<TextComponent> outputComp = std::make_shared<TextComponent>("NeTRunner JavaScript console", 0, 0, 12, false, 0x000000FF, newWindow->windowWidth, newWindow->windowHeight);
  560. outputComp->win = newWindow;
  561. newWindow->ui->layers.push_back(outputComp);
  562. outputComp.reset();
  563. /*
  564. std::shared_ptr<InputComponent> inputComp = std::make_shared<InputComponent>(0, 0, 1024, 12, newWindow->windowWidth, newWindow->windowHeight);
  565. inputComp->win = newWindow;
  566. newWindow->ui->layers.push_back(inputComp);
  567. inputComp.reset();
  568. */
  569. this->windows.push_back(newWindow);
  570. newWindow.reset();
  571. }
  572. void App::addWindow() {
  573. windowCounter++;
  574. std::shared_ptr<Window> newWindow = std::make_shared<Window>();
  575. newWindow->id = windowCounter;
  576. newWindow->windowWidth = 1024;
  577. newWindow->windowHeight = 640;
  578. newWindow->init(); // load our UI into it
  579. newWindow->openglWindow->app = this;
  580. // convert uiRootNode into a component list
  581. std::shared_ptr<Component> p_rootComponent = std::make_shared<Component>();
  582. p_rootComponent->name = "rootComponent of browser";
  583. // we build one global tree that each layer has a child node in
  584. this->createComponentTree(this->uiRootNode, p_rootComponent, newWindow);
  585. //Component::printComponentTree(p_rootComponent, 0);
  586. // these probably should be fatal
  587. if (!newWindow->addressComponent) {
  588. std::cout << "ERROR: no addressComponent" << std::endl;
  589. }
  590. if (!newWindow->tabComponent) {
  591. std::cout << "ERROR: no tabComponent" << std::endl;
  592. }
  593. // we want each window to has it's own component tree, so each address bar can have different values
  594. size_t layerCount = 0;
  595. for(auto layer: this->layers) {
  596. // set proper vertices
  597. layer->resize(newWindow->windowWidth, newWindow->windowHeight);
  598. // transfer layer
  599. newWindow->ui->layers.push_back(layer);
  600. // debug
  601. /*
  602. std::cout << "layer: " << layerCount << std::endl;
  603. Component::printComponentTree(layer, 0);
  604. */
  605. layerCount++;
  606. layer.reset(); // deallocate our local layers
  607. }
  608. this->layers.clear(); // nuke any layers we have
  609. p_rootComponent.reset();
  610. //newWindow->rootComponent = rootComponent;
  611. //newWindow->ui->rootComponent = rootComponent;
  612. newWindow->renderDirty = true;
  613. this->windows.push_back(newWindow);
  614. newWindow.reset();
  615. if (!activeWindow) {
  616. //std::cout << "Browser::addWindow - setting active window" << std::endl;
  617. activeWindow = this->windows.back().get();
  618. }
  619. }
  620. // FIXME: put quit check in here and clean up windows
  621. void App::render() {
  622. // guessing during render this iterator can be invalidated
  623. size_t w = this->windows.size();
  624. //for(std::vector<std::shared_ptr<Window>>::iterator it = this->windows.begin(); it != this->windows.end(); ++it) {
  625. //it->get()->render();
  626. // definitely safer
  627. for(size_t i = 0; i < w; ++i) {
  628. glfwMakeContextCurrent(this->windows[i]->window);
  629. this->windows[i]->render();
  630. }
  631. }
  632. void App::loop() {
  633. bool shouldQuit = false;
  634. while (!shouldQuit) {
  635. //const std::clock_t begin = clock();
  636. this->render();
  637. scheduler->fireTimers(); // render may have taken some time
  638. double next = scheduler->getNext();
  639. //if (!next) std::cout << "timer starvation, refiring" << std::endl;
  640. /*
  641. while(!next) {
  642. std::cout << "timer starvation, refiring" << std::endl;
  643. scheduler->fireTimers(); // render may have taken some time
  644. next = scheduler->getNext();
  645. }
  646. */
  647. //std::cout << "Browser::loop - next timer at " << next << std::endl;
  648. if (next == LONG_MAX) {
  649. glfwWaitEvents(); // block until something changes
  650. } else {
  651. glfwWaitEventsTimeout(next / 1000);
  652. }
  653. scheduler->fireTimers(); // check before we go into render again
  654. //glfwWaitEventsTimeout(1.0 / 60.0); // increase the cpu from 0% to 2% on idle
  655. //const std::clock_t end = clock();
  656. //std::cout << '\r' << std::fixed << (((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) * 1000) << std::scientific << " ms/f " << std::flush;
  657. // FIXME: one close shouldn't close the app
  658. for(std::vector<std::shared_ptr<Window>>::iterator it = this->windows.begin(); it != this->windows.end(); ++it) {
  659. if (glfwWindowShouldClose(it->get()->window)) {
  660. // FIXME: just remove window from stack
  661. shouldQuit = true;
  662. break;
  663. }
  664. }
  665. }
  666. }