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