ToolBox.js 1.8 KB

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