Gui.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. let {
  14. CoreSystem,
  15. ViewSystem
  16. } = Library;
  17. export default class Gui {
  18. constructor() {
  19. this.CoreSystem = new CoreSystem();
  20. this.ModuleBar = new ModuleBar(this.CoreSystem); // Add CoreSystem deps
  21. this.Keyboard = new Keyboard();
  22. this.phoneRef = React.createRef();
  23. this.phone = this.phoneRef;
  24. this.toolboxRef = React.createRef();
  25. this.toolbox = this.toolboxRef;
  26. this.Environment = new Environment(this.CoreSystem, this.phoneRef);
  27. this.Environment.onUpdate(() => this.forceUpdate())
  28. this.ToolBox = new ToolBox();
  29. this.MainBar = new MainBar();
  30. this.load();
  31. this.__onUpdate = null;
  32. this.SideBar = new SideBar(this.CoreSystem);
  33. this.SideBar.tool = this.ToolBox.activeTool;
  34. this.SideBar.onUpdate(() => {
  35. this.forceUpdate();
  36. })
  37. this.ToolBox.onUpdate( () =>{
  38. this.SideBar.tool = this.ToolBox.activeTool;
  39. this.forceUpdate();
  40. });
  41. this.MainBar.onSave( () => {
  42. this.saveEnv();
  43. this.forceUpdate()
  44. })
  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. if(this.phone){
  52. // thats fine
  53. this.ToolBoxMouse = new Mouse(this.toolbox.current);
  54. this.phoneMouse = new Mouse(this.phone.current).droppable();
  55. }
  56. if(this.ToolBoxMouse !== undefined){
  57. this.ToolBoxMouse.listen()
  58. .on('LeftDown',(e) => this.ToolManagement(e))
  59. this.phoneMouse.listen()
  60. .on('LeftDown',(e) => this.ToolManagement(e))
  61. .on('Drop', (e) => this.dropContent(e))
  62. }
  63. }
  64. saveEnv(){
  65. let env = this.CoreSystem.export();
  66. localStorage.setItem('environment', JSON.stringify(env));
  67. this.forceUpdate();
  68. try{
  69. this.CoreSystem.import(JSON.parse(js));
  70. this.forceUpdate();
  71. } catch(e) {
  72. console.log(e);
  73. }
  74. }
  75. ToolManagement(e){
  76. if(this.ToolBox.activeTool !== null){
  77. this.processElement(e)
  78. }
  79. }
  80. onUpdate(fn){
  81. // check toolbox is fine
  82. this.__onUpdate = fn
  83. }
  84. forceUpdate(){
  85. this.__onUpdate && this.__onUpdate();
  86. }
  87. load() {
  88. this.ToolBox = new ToolBox(this.CoreSystem);
  89. try{
  90. let env = localStorage.getItem('environment');
  91. if( env === null){
  92. this.CoreSystem.import(JSON.parse(jsLoad));
  93. }else{
  94. this.CoreSystem.import(JSON.parse(env));
  95. }
  96. this.forceUpdate();
  97. } catch(e) {
  98. console.log(e);
  99. }
  100. }
  101. processElement(event){
  102. let Node = this.CoreSystem.ray({event});
  103. let NewNode = this.ToolBox.editNode(Node);
  104. this.forceUpdate();
  105. }
  106. dropContent({event, data}) {
  107. data = JSON.parse(data);
  108. let Node = this.CoreSystem.ray({event});
  109. // Node.content
  110. Node.props.selected = true;
  111. console.log(Node, data);
  112. let node = new ViewNode(Math.random(), data.ctor, {}, data.namespace);
  113. Node.content = node;
  114. console.log(node);
  115. this.forceUpdate();
  116. }
  117. mobileRender(){
  118. return this.Environment.render();
  119. }
  120. renderSideBar(){
  121. return this.SideBar.render();
  122. }
  123. renderMainBar(){
  124. return this.MainBar.render();
  125. }
  126. renderToolBox(){
  127. return (
  128. <div ref = {this.toolboxRef}>
  129. <View style={styles.toolbarView}>
  130. {this.ToolBox.render()}
  131. </View>
  132. </div>)
  133. }
  134. renderModuleBar(){
  135. return (
  136. <View >
  137. {this.ModuleBar.render()}
  138. </View>
  139. )
  140. }
  141. }
  142. //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"}}}}}';
  143. //jsLoad Test
  144. 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}}}}}'
  145. const MudleBarStyle = StyleSheet.create({
  146. container:{
  147. backgroundColor:'red'
  148. }
  149. })
  150. const styles = StyleSheet.create({
  151. container: {
  152. flex: 1,
  153. backgroundColor: '#fff',
  154. alignItems: 'center',
  155. justifyContent: 'space-between',
  156. flexDirection: 'row'
  157. },
  158. toolbarView: {
  159. justifyContent: 'space-around',
  160. alignItems: 'center',
  161. padding: 16
  162. },
  163. mainView: {
  164. backgroundColor: '#065b69',
  165. alignSelf:'stretch',
  166. flex: 1,
  167. justifyContent: 'center',
  168. alignItems: 'center'
  169. },
  170. mobileView: {
  171. backgroundColor: 'white',
  172. borderWidth: 4,
  173. borderColor: 'orange',
  174. flex:1,
  175. alignSelf: 'stretch',
  176. // iphone X
  177. width:1125/3,
  178. height: 2436/3
  179. },
  180. attributesView: {
  181. width:256,
  182. alignSelf: 'stretch',
  183. flexDirection: 'column',
  184. }
  185. });