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