controls.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import * as React from 'react';
  7. import { Transform, State } from 'mol-state';
  8. import { ParametersComponent } from 'mol-app/component/parameters';
  9. import { StateAction } from 'mol-state/action';
  10. import { PluginCommands } from 'mol-plugin/command';
  11. import { UpdateTrajectory } from 'mol-plugin/state/actions/basic';
  12. import { PluginComponent } from './base';
  13. export class Controls extends PluginComponent<{ }, { }> {
  14. render() {
  15. return <>
  16. </>;
  17. }
  18. }
  19. export class _test_TrajectoryControls extends PluginComponent {
  20. render() {
  21. return <div>
  22. <b>Trajectory: </b>
  23. <button onClick={() => PluginCommands.State.ApplyAction.dispatch(this.plugin, {
  24. state: this.plugin.state.data,
  25. action: UpdateTrajectory.create({ action: 'advance', by: -1 })
  26. })}>&lt;&lt;</button>
  27. <button onClick={() => PluginCommands.State.ApplyAction.dispatch(this.plugin, {
  28. state: this.plugin.state.data,
  29. action: UpdateTrajectory.create({ action: 'reset' })
  30. })}>Reset</button>
  31. <button onClick={() => PluginCommands.State.ApplyAction.dispatch(this.plugin, {
  32. state: this.plugin.state.data,
  33. action: UpdateTrajectory.create({ action: 'advance', by: +1 })
  34. })}>&gt;&gt;</button><br />
  35. </div>
  36. }
  37. }
  38. export class _test_ApplyAction extends PluginComponent<{ nodeRef: Transform.Ref, state: State, action: StateAction }, { params: any }> {
  39. private getObj() {
  40. const obj = this.props.state.cells.get(this.props.nodeRef)!;
  41. return obj;
  42. }
  43. private getDefaultParams() {
  44. const p = this.props.action.definition.params;
  45. if (!p || !p.default) return {};
  46. const obj = this.getObj();
  47. if (!obj.obj) return {};
  48. return p.default(obj.obj, this.plugin);
  49. }
  50. private getParamDef() {
  51. const p = this.props.action.definition.params;
  52. if (!p || !p.controls) return {};
  53. const obj = this.getObj();
  54. if (!obj.obj) return {};
  55. return p.controls(obj.obj, this.plugin);
  56. }
  57. private create() {
  58. console.log('Apply Action', this.state.params);
  59. PluginCommands.State.ApplyAction.dispatch(this.plugin, {
  60. state: this.props.state,
  61. action: this.props.action.create(this.state.params),
  62. ref: this.props.nodeRef
  63. });
  64. // this.context.applyTransform(this.props.state, this.props.nodeRef, this.props.transformer, this.state.params);
  65. }
  66. state = { params: this.getDefaultParams() }
  67. render() {
  68. const obj = this.getObj();
  69. if (obj.status !== 'ok') {
  70. // TODO filter this elsewhere
  71. return <div />;
  72. }
  73. const action = this.props.action;
  74. return <div key={`${this.props.nodeRef} ${this.props.action.id}`}>
  75. <div style={{ borderBottom: '1px solid #999', marginBottom: '5px' }}><h3>{(action.definition.display && action.definition.display.name) || action.id}</h3></div>
  76. <ParametersComponent params={this.getParamDef()} values={this.state.params as any} onChange={(k, v) => {
  77. this.setState({ params: { ...this.state.params, [k]: v } });
  78. }} />
  79. <div style={{ textAlign: 'right' }}>
  80. <button onClick={() => this.create()}>Create</button>
  81. </div>
  82. </div>
  83. }
  84. }
  85. export class _test_UpdateTransform extends PluginComponent<{ state: State, nodeRef: Transform.Ref }, { params: any }> {
  86. private getCell(ref?: string) {
  87. return this.props.state.cells.get(ref || this.props.nodeRef)!;
  88. }
  89. private getDefParams() {
  90. const cell = this.getCell();
  91. if (!cell) return {};
  92. return cell.transform.params;
  93. }
  94. private getParamDef() {
  95. const cell = this.getCell();
  96. const def = cell.transform.transformer.definition;
  97. if (!cell.sourceRef || !def.params || !def.params.controls) return void 0;
  98. const src = this.getCell(cell.sourceRef);
  99. if (!src || !src.obj) return void 0;
  100. return def.params.controls(src.obj, this.plugin);
  101. }
  102. private update() {
  103. console.log(this.props.nodeRef, this.state.params);
  104. this.plugin.updateTransform(this.props.state, this.props.nodeRef, this.state.params);
  105. }
  106. // componentDidMount() {
  107. // const t = this.context.state.data.tree.nodes.get(this.props.nodeRef)!;
  108. // if (t) this.setState({ params: t.value.params });
  109. // }
  110. state = { params: this.getDefParams() };
  111. render() {
  112. const cell = this.getCell();
  113. const transform = cell.transform;
  114. if (!transform || transform.ref === Transform.RootRef) {
  115. return <div />;
  116. }
  117. const params = this.getParamDef();
  118. if (!params) return <div />;
  119. const tr = transform.transformer;
  120. return <div key={`${this.props.nodeRef} ${tr.id}`} style={{ marginBottom: '10ox' }}>
  121. <div style={{ borderBottom: '1px solid #999' }}><h3>{(tr.definition.display && tr.definition.display.name) || tr.definition.name}</h3></div>
  122. <ParametersComponent params={params} values={this.state.params as any} onChange={(k, v) => {
  123. this.setState({ params: { ...this.state.params, [k]: v } });
  124. }} />
  125. <button onClick={() => this.update()} style={{ width: '100%' }}>Update</button>
  126. </div>
  127. }
  128. }