ToolBox.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import Tool from './Tool';
  2. import {Tools} from '../Tools/index'
  3. import React from 'react';
  4. import {StyleSheet,View , Text} from 'react-native';
  5. import Mouse from './mouse';
  6. import Keyboard from './keyboard';
  7. import BaseSystem from './System';
  8. function log(args){
  9. console.log(args)
  10. }
  11. export default class ToolBox extends BaseSystem {
  12. constructor(CoreSystem, defaultTool){
  13. super();
  14. this.CoreSystem = CoreSystem;
  15. this.tools = {};
  16. this.activeTool = null;
  17. this.selectedNode = null;
  18. this.loadTools(defaultTool);
  19. this.Keyboard = new Keyboard().listen();
  20. this.Keyboard.on(27, () => {
  21. if(this.activeTool) {
  22. this.activeTool.active = false;
  23. this.activeTool = null;
  24. this.forceUpdate();
  25. }
  26. });
  27. }
  28. on(e) { return this.activeTool.on(e) }
  29. editNode(node){
  30. this.selectedNode = node;
  31. return this.activeTool.editViewNode(this.CoreSystem,node);
  32. }
  33. selectNode(node) {
  34. this.selectedNode && (this.selectedNode.props.selected = false);
  35. this.selectedNode = node;
  36. this.selectedNode.props.selected = true;
  37. }
  38. selectTool(key){
  39. this.activeTool && (this.activeTool.active = false);
  40. let tool = this.tools[key];
  41. this.activeTool = tool;
  42. tool.active = true;
  43. this.forceUpdate()
  44. }
  45. loadTools(defaultTool){
  46. log("Loading Tools")
  47. Object.keys(Tools).map((key) => {
  48. if( Tools[key].prototype instanceof Tool === false ){
  49. throw new Error("This is not a Tool")
  50. }
  51. if(this.tools[key]){
  52. log("Tool already Exists: ",key);
  53. }
  54. this.tools[key] = new Tools[key]();
  55. })
  56. if(defaultTool && this.tools[defaultTool]) {
  57. this.activeTool = this.tools[defaultTool];
  58. }
  59. }
  60. render(){
  61. let tools = Object.keys(this.tools).map((key) => {
  62. let toolStyle = styles.tool;
  63. if(this.activeTool !== null){
  64. if(this.activeTool.constructor.name === key){
  65. toolStyle = styles.activeTool;
  66. }
  67. }
  68. return(<View onClick={() => this.selectTool(key) } style={toolStyle} key={key}>
  69. {this.tools[key].render()}
  70. </View>)
  71. });
  72. return tools;
  73. }
  74. }
  75. /** Must be remove from here**/
  76. const styles = StyleSheet.create({
  77. tool:{
  78. },
  79. activeTool:{
  80. }
  81. })