view/View.Frame.js

  1. /*
  2. * Copyright 2020 WICKLETS LLC
  3. *
  4. * This file is part of Wick Engine.
  5. *
  6. * Wick Engine is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Wick Engine is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Wick Engine. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. Wick.View.Frame = class extends Wick.View {
  20. /**
  21. * A multiplier for the resolution for the rasterization process.
  22. * E.g. a multiplier of 2 will make a path 100 pixels wide rasterize into an image 200 pixels wide.
  23. */
  24. static get RASTERIZE_RESOLUTION_MODIFIER() {
  25. return 1;
  26. }
  27. static get RASTERIZE_RESOLUTION_MODIFIER_FOR_DEVICE() {
  28. return Wick.View.Frame.RASTERIZE_RESOLUTION_MODIFIER / window.devicePixelRatio;
  29. }
  30. /**
  31. * Create a frame view.
  32. */
  33. constructor() {
  34. super();
  35. this.objectsLayer = new this.paper.Layer();
  36. this.objectsLayer.remove();
  37. }
  38. /**
  39. * Write the changes made to the view to the frame.
  40. */
  41. applyChanges() {
  42. this._applyDrawableChanges();
  43. }
  44. /**
  45. * Update the view based on the model
  46. */
  47. render() {
  48. this._renderObjects();
  49. }
  50. _renderObjects() {
  51. this.objectsLayer.data.wickUUID = this.model.uuid;
  52. this.objectsLayer.data.wickType = 'clipsandpaths';
  53. this.objectsLayer.removeChildren();
  54. // Remove placeholder paths if
  55. // 1) this frame is focused, or
  56. // 2) the project is playing
  57. if(this.model.parentClip.isFocus || (this.model.project&&this.model.project.playing)) {
  58. this.model.paths.forEach(path => {
  59. if(path.isPlaceholder) {
  60. path.remove();
  61. }
  62. });
  63. }
  64. let children = this.model.drawable.map(object => {
  65. object.view.render();
  66. if (object.view.model instanceof Wick.Path) {
  67. return object.view.item;
  68. } else {
  69. return object.view.group;
  70. }
  71. });
  72. this.objectsLayer.addChildren(children);
  73. }
  74. _applyDrawableChanges() {
  75. this.model.drawable.filter(path => {
  76. return path instanceof Wick.Path && path.isDynamicText;
  77. }).forEach(path => {
  78. path.view.item.bringToFront();
  79. }); // Clear all WickPaths from the frame
  80. // Reorder clips
  81. var drawables = this.model.drawable.concat([]);
  82. drawables.forEach(drawable => {
  83. // should realkly be remove child
  84. this.model.removeClip(drawable);
  85. });
  86. this.objectsLayer.children.filter(child => {
  87. return child.data.wickType !== 'gui';
  88. }).forEach(child => {
  89. if (child instanceof paper.Group || child instanceof Wick.Clip) {
  90. this.model.addClip(drawables.find(g => {
  91. return g.uuid === child.data.wickUUID;
  92. }));
  93. } else {
  94. var originalWickPath = child.data.wickUUID ? Wick.ObjectCache.getObjectByUUID(child.data.wickUUID) : null;
  95. var pathJSON = Wick.View.Path.exportJSON(child);
  96. var wickPath = new Wick.Path({
  97. project: this.model.project,
  98. json: pathJSON
  99. });
  100. this.model.addPath(wickPath);
  101. wickPath.fontWeight = originalWickPath ? originalWickPath.fontWeight : 400;
  102. wickPath.fontStyle = originalWickPath ? originalWickPath.fontStyle : 'normal';
  103. wickPath.identifier = originalWickPath ? originalWickPath.identifier : null;
  104. child.name = wickPath.uuid;
  105. }
  106. }); // Update clip transforms
  107. this.objectsLayer.children.filter(child => {
  108. return child.data.wickType !== 'gui';
  109. }).forEach(child => {
  110. if (child instanceof paper.Group || child instanceof Wick.Clip) {
  111. var wickClip = Wick.ObjectCache.getObjectByUUID(child.data.wickUUID);
  112. wickClip.transformation = new Wick.Transformation({
  113. x: child.position.x,
  114. y: child.position.y,
  115. scaleX: child.scaling.x,
  116. scaleY: child.scaling.y,
  117. rotation: child.rotation,
  118. opacity: child.opacity
  119. });
  120. }
  121. });
  122. /*
  123. var originalWickPath = child.data.wickUUID ? Wick.ObjectCache.getObjectByUUID(child.data.wickUUID) : null;
  124. var pathJSON = Wick.View.Path.exportJSON(child);
  125. var wickPath = new Wick.Path({json:pathJSON});
  126. this.model.addPath(wickPath);
  127. wickPath.fontWeight = originalWickPath ? originalWickPath.fontWeight : 400;
  128. wickPath.fontStyle = originalWickPath ? originalWickPath.fontStyle : 'normal';
  129. wickPath.identifier = originalWickPath ? originalWickPath.identifier : null;
  130. wickPath.isPlaceholder = originalWickPath ? originalWickPath.isPlaceholder : false;
  131. child.name = wickPath.uuid;
  132. */
  133. }
  134. };