scene-ui.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { scene } from "./scene.js";
  2. import { push } from "./push.js";
  3. import { material_icon } from "./icons.js";
  4. import { container } from "./container.js";
  5. class scene_ui {
  6. #box;
  7. #worker;
  8. #step_left;
  9. #step_right;
  10. #step_front;
  11. #step_back;
  12. #rotate_clockwise;
  13. #rotate_countclockwise;
  14. constructor(worker) {
  15. if (!(worker instanceof scene)) {
  16. throw new TypeError("Worker must be instance of scene.");
  17. }
  18. this.#worker = worker;
  19. this.#step_front = this.#create_push(
  20. "front", "arrow_drop_up", position => { position.move_front(); }
  21. );
  22. this.#step_back = this.#create_push(
  23. "back", "arrow_drop_down", position => { position.move_back(); }
  24. );
  25. this.#step_left = this.#create_push(
  26. "left", "arrow_left", position => { position.move_left(); }
  27. );
  28. this.#step_right = this.#create_push(
  29. "right", "arrow_right", position => { position.move_right(); }
  30. );
  31. this.#rotate_clockwise = this.#create_push(
  32. "clockwise", "rotate_right",
  33. position => { position.rotate_clockwise(); }
  34. );
  35. this.#rotate_countclockwise = this.#create_push(
  36. "countclockwise", "rotate_left",
  37. position => { position.rotate_countclockwise(); }
  38. );
  39. this.#box = container("controls", (root) => {
  40. root.appendChild(container("top", (top) => {
  41. top.appendChild(this.#rotate_countclockwise);
  42. top.appendChild(this.#step_front);
  43. top.appendChild(this.#rotate_clockwise);
  44. }));
  45. root.appendChild(container("bottom", (bottom) => {
  46. bottom.appendChild(this.#step_left);
  47. bottom.appendChild(this.#step_back);
  48. bottom.appendChild(this.#step_right);
  49. }));
  50. });
  51. }
  52. get box() {
  53. return this.#box;
  54. }
  55. #create_push(name, icon, move) {
  56. return push(name, () => {
  57. move(this.#worker.position);
  58. }, (target) => {
  59. target.innerText = "";
  60. target.appendChild(material_icon(icon));
  61. });
  62. }
  63. }
  64. export { scene_ui };