ToolBox.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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(<View onClick={() => this.selectTool(key) } style={toolStyle}>{this.tools[key].render()}</View>)
  59. });
  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. })