ToolBox.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. this.activeTool && (this.activeTool.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. })