Gui.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //import CoreSystem from 'core-system';
  2. import ToolBox from './ToolBox';
  3. import Keyboard from './keyboard';
  4. import Mouse from './mouse';
  5. import React from 'react';
  6. import {Dimensions, StyleSheet, View, Text} from 'react-native';
  7. import Library from 'trapilib/dist/lib';
  8. import SideBar from './SideBar';
  9. import MainBar from './MainBar';
  10. import ModuleBar from './ModuleBar';
  11. import { ViewNode } from 'trapilib/dist/lib/systems/ViewSystem';
  12. import Environment from './Environment';
  13. import BaseSystem from './System';
  14. let {
  15. CoreSystem,
  16. ViewSystem
  17. } = Library;
  18. export default class Gui extends BaseSystem {
  19. constructor() {
  20. super();
  21. this.phoneRef = React.createRef();
  22. this.phone = this.phoneRef;
  23. this.toolboxRef = React.createRef();
  24. this.toolbox = this.toolboxRef;
  25. this.CoreSystem = new CoreSystem();
  26. this.Keyboard = new Keyboard();
  27. this.ToolBox = new ToolBox(this.CoreSystem);
  28. this.MainBar = new MainBar();
  29. this.ModuleBar = new ModuleBar(this.CoreSystem); // Add CoreSystem deps
  30. this.SideBar = new SideBar(this.CoreSystem, this.ToolBox);
  31. this.Environment = new Environment(this.CoreSystem, this.ToolBox, this.phoneRef);
  32. this.Environment.onUpdate(() => this.forceUpdate())
  33. this.SideBar.onUpdate(() => this.forceUpdate())
  34. this.ToolBox.onUpdate( () => {
  35. this.SideBar.tool = this.ToolBox.activeTool;
  36. this.forceUpdate();
  37. });
  38. this.MainBar.onUpdate(() => this.forceUpdate())
  39. this.MainBar.onSave( () => {
  40. this.saveEnv();
  41. this.forceUpdate()
  42. })
  43. this.load();
  44. // Setup mainContainer
  45. let cview = this.CoreSystem.getCurrentView();
  46. let container = cview.getDefaultContainer();
  47. let mainContent = new ViewNode(Math.random(), 'BaseContainer', {width: 1125/3, height: 2436/3 - 10});
  48. cview.setContent(mainContent, container);
  49. }
  50. onMount() {
  51. this.forceUpdate();
  52. if(this.phone){
  53. // thats fine
  54. this.ToolBoxMouse = new Mouse(this.toolbox.current);
  55. this.phoneMouse = new Mouse(this.phone.current).droppable();
  56. }
  57. if(this.ToolBoxMouse !== undefined){
  58. this.ToolBoxMouse.listen()
  59. .on('LeftDown',(e) => this.ToolManagement(e))
  60. this.phoneMouse.listen()
  61. .on('LeftDown',(e) => this.ToolManagement(e))
  62. .on('Drop', (e) => this.dropContent(e))
  63. }
  64. }
  65. saveEnv(){
  66. let env = this.CoreSystem.export();
  67. localStorage.setItem('environment', JSON.stringify(env));
  68. this.forceUpdate();
  69. try{
  70. this.CoreSystem.import(JSON.parse(js));
  71. this.forceUpdate();
  72. } catch(e) {
  73. console.log(e);
  74. }
  75. }
  76. ToolManagement(e){
  77. if(this.ToolBox.activeTool !== null){
  78. this.processElement(e)
  79. }
  80. }
  81. load() {
  82. try{
  83. let env = localStorage.getItem('environment');
  84. if( env === null){
  85. this.CoreSystem.import(JSON.parse(jsLoad));
  86. }else{
  87. this.CoreSystem.import(JSON.parse(env));
  88. }
  89. this.forceUpdate();
  90. } catch(e) {
  91. console.log(e);
  92. }
  93. }
  94. processElement(event){
  95. let Node = this.CoreSystem.ray({event});
  96. let NewNode = this.ToolBox.editNode(Node);
  97. this.forceUpdate();
  98. }
  99. dropContent({event, data}) {
  100. data = JSON.parse(data);
  101. let Node = this.CoreSystem.ray({event});
  102. // Node.content
  103. Node.props.selected = true;
  104. console.log(Node, data);
  105. let node = new ViewNode(Math.random(), data.ctor, {}, data.namespace);
  106. Node.content = node;
  107. console.log(node);
  108. this.forceUpdate();
  109. }
  110. mobileRender(){
  111. return this.Environment.render();
  112. }
  113. renderSideBar(){
  114. return this.SideBar.render();
  115. }
  116. renderMainBar(){
  117. return this.MainBar.render();
  118. }
  119. renderToolBox(){
  120. return (
  121. <div ref = {this.toolboxRef}>
  122. <View style={styles.toolbarView}>
  123. {this.ToolBox.render()}
  124. </View>
  125. </div>)
  126. }
  127. renderModuleBar(){
  128. return (
  129. <View >
  130. {this.ModuleBar.render()}
  131. </View>
  132. )
  133. }
  134. }
  135. //const jsLoad = '{"Routing":{"topology":{"nodes":{"test":{"id":"test","routeName":"test","uri":"test","view":"VS1","defaultLink":null},"sample":{"id":"sample","routeName":"sample","uri":"sample","view":"VS2","defaultLink":null}},"links":{"test":{"out":[],"in":[]},"sample":{"out":[],"in":[]}}},"home":"test"},"Views":{"views":{"VS1":{"tree":{"nodes":{"A":{"id":"A","depth":0,"value":"RootComp","namespace":"default","props":{}},"B":{"id":"B","depth":1,"value":"ViewComp","namespace":"default","props":{"text":"ASD LOOK AT ME LOOK OO O.O (O.O)"}},"C":{"id":"C","depth":1,"value":"ViewComp","namespace":"default","props":{}}},"links":{"A":{"out":[{"to":"B","from":"A"},{"to":"C","from":"A"}],"in":[]},"B":{"out":[],"in":[{"to":"B","from":"A"}]},"C":{"out":[],"in":[{"to":"C","from":"A"}]}},"levels":[["A"],["B","C"]],"rootId":"A"}},"VS2":{"tree":{"nodes":{"P":{"id":"P","depth":0,"value":"RootComp","namespace":"default","props":{}}},"links":{"P":{"out":[],"in":[]}},"levels":[["P"]],"rootId":"P"}}}}}';
  136. //jsLoad Test
  137. const jsLoad = '{"Routing":{"topology":{"nodes":{"test":{"id":"test","routeName":"test","uri":"test","view":"VS1","defaultLink":null}},"links":{"test":{"out":[],"in":[]}}},"home":"test"},"Views":{"views":{"VS1":{"tree":{"nodes":{"0.5907657036474692":{"id":0.5907657036474692,"depth":0,"isRow":true,"isCol":false},"0.51066596548815":{"id":0.51066596548815,"depth":1,"isRow":false,"isCol":true},"0.8855478722619232":{"id":0.8855478722619232,"depth":1,"isRow":false,"isCol":true,"content":{"id":"A","depth":null,"value":"RootComp","namespace":"default","props":{"text":"This is the Starting point!!!Try add on me ","style":{"container":22,"text":23}}}},"0.4630979404426283":{"id":0.4630979404426283,"depth":1,"isRow":false,"isCol":true},"0.7144981784945621":{"id":0.7144981784945621,"depth":2,"isRow":true,"isCol":false},"0.0666081688424911":{"id":0.0666081688424911,"depth":2,"isRow":true,"isCol":false}},"links":{"0.5907657036474692":{"out":[{"to":0.51066596548815,"from":0.5907657036474692},{"to":0.8855478722619232,"from":0.5907657036474692},{"to":0.4630979404426283,"from":0.5907657036474692}],"in":[]},"0.51066596548815":{"out":[],"in":[{"to":0.51066596548815,"from":0.5907657036474692}]},"0.8855478722619232":{"out":[{"to":0.7144981784945621,"from":0.8855478722619232},{"to":0.0666081688424911,"from":0.8855478722619232}],"in":[{"to":0.8855478722619232,"from":0.5907657036474692}]},"0.4630979404426283":{"out":[],"in":[{"to":0.4630979404426283,"from":0.5907657036474692}]},"0.7144981784945621":{"out":[],"in":[{"to":0.7144981784945621,"from":0.8855478722619232}]},"0.0666081688424911":{"out":[],"in":[{"to":0.0666081688424911,"from":0.8855478722619232}]}},"levels":[[0.5907657036474692],[0.51066596548815,0.8855478722619232,0.4630979404426283],[0.7144981784945621,0.0666081688424911]],"rootId":0.5907657036474692}}}}}'
  138. const MudleBarStyle = StyleSheet.create({
  139. container:{
  140. backgroundColor:'red'
  141. }
  142. })
  143. const styles = StyleSheet.create({
  144. container: {
  145. flex: 1,
  146. backgroundColor: '#fff',
  147. alignItems: 'center',
  148. justifyContent: 'space-between',
  149. flexDirection: 'row'
  150. },
  151. toolbarView: {
  152. justifyContent: 'space-around',
  153. alignItems: 'center',
  154. padding: 16
  155. },
  156. mainView: {
  157. backgroundColor: '#065b69',
  158. alignSelf:'stretch',
  159. flex: 1,
  160. justifyContent: 'center',
  161. alignItems: 'center'
  162. },
  163. mobileView: {
  164. backgroundColor: 'white',
  165. borderWidth: 4,
  166. borderColor: 'orange',
  167. flex:1,
  168. alignSelf: 'stretch',
  169. // iphone X
  170. width:1125/3,
  171. height: 2436/3
  172. },
  173. attributesView: {
  174. width:256,
  175. alignSelf: 'stretch',
  176. flexDirection: 'column',
  177. }
  178. });