ToolBox.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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} >{this.tools[key].render()}</View>)
  61. });
  62. return tools;
  63. }
  64. }
  65. /** Must be remove from here**/
  66. const styles = StyleSheet.create({
  67. tool:{
  68. },
  69. activeTool:{
  70. }
  71. })