ToolBox.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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(ViewSystem){
  11. this.ViewSystem = ViewSystem;
  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.ViewSystem,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. backgroundColor: 'gray',
  62. padding: 10
  63. },
  64. activeTool:{
  65. backgroundColor:'green',
  66. padding: 10
  67. }
  68. })