ToolBox.js 1.5 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){
  11. this.CoreSystem = CoreSystem;
  12. this.tools = {};
  13. this.loadTools();
  14. this.activeTool = null;
  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(){
  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. }
  45. render(){
  46. let tools = Object.keys(this.tools).map((key) => {
  47. let toolStyle = styles.tool;
  48. if(this.activeTool !== null){
  49. if(this.activeTool.constructor.name === key){
  50. toolStyle = styles.activeTool;
  51. }
  52. }
  53. return(
  54. <View onClick={() => this.selectTool(key) } style={ toolStyle}>{this.tools[key].render()}</View>)});
  55. return tools;
  56. }
  57. }
  58. /** Must be remove from here**/
  59. const styles = StyleSheet.create({
  60. tool:{
  61. padding: 5
  62. },
  63. activeTool:{
  64. backgroundColor:'green',
  65. padding: 5
  66. }
  67. })