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