RenderedInstance.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // @flow
  2. import * as PIXI from 'pixi.js-legacy';
  3. import PixiResourcesLoader from '../../ObjectsRendering/PixiResourcesLoader';
  4. /**
  5. * RenderedInstance is the base class used for creating 2D renderers of instances,
  6. * which display on the scene editor, using Pixi.js, the instance of an object (see InstancesEditor).
  7. */
  8. export default class RenderedInstance {
  9. _project: gdProject;
  10. _layout: gdLayout;
  11. _instance: gdInitialInstance;
  12. _associatedObjectConfiguration: gdObjectConfiguration;
  13. _pixiContainer: PIXI.Container;
  14. _pixiResourcesLoader: Class<PixiResourcesLoader>;
  15. _pixiObject: PIXI.DisplayObject;
  16. wasUsed: boolean;
  17. constructor(
  18. project: gdProject,
  19. layout: gdLayout,
  20. instance: gdInitialInstance,
  21. associatedObjectConfiguration: gdObjectConfiguration,
  22. pixiContainer: PIXI.Container,
  23. pixiResourcesLoader: Class<PixiResourcesLoader>
  24. ) {
  25. this._pixiObject = null;
  26. this._instance = instance;
  27. this._associatedObjectConfiguration = associatedObjectConfiguration;
  28. this._pixiContainer = pixiContainer;
  29. this._project = project;
  30. this._layout = layout;
  31. this._pixiResourcesLoader = pixiResourcesLoader;
  32. this.wasUsed = true; //Used by InstancesRenderer to track rendered instance that are not used anymore.
  33. }
  34. /**
  35. * Convert an angle from degrees to radians.
  36. */
  37. static toRad(angleInDegrees: number) {
  38. return (angleInDegrees / 180) * Math.PI;
  39. }
  40. /**
  41. * Called when the scene editor is rendered.
  42. */
  43. update() {
  44. //Nothing to do.
  45. }
  46. getPixiObject(): PIXI.DisplayObject | null {
  47. return this._pixiObject;
  48. }
  49. getInstance(): gdInitialInstance {
  50. return this._instance;
  51. }
  52. /**
  53. * Called to notify the instance renderer that its associated instance was removed from
  54. * the scene. The PIXI object should probably be removed from the container: This is what
  55. * the default implementation of the method does.
  56. */
  57. onRemovedFromScene(): void {
  58. if (this._pixiObject !== null)
  59. this._pixiContainer.removeChild(this._pixiObject);
  60. }
  61. getOriginX(): number {
  62. return 0;
  63. }
  64. getOriginY(): number {
  65. return 0;
  66. }
  67. getCenterX(): number {
  68. return this.getWidth() / 2;
  69. }
  70. getCenterY(): number {
  71. return this.getHeight() / 2;
  72. }
  73. getCustomWidth(): number {
  74. return this._instance.getCustomWidth();
  75. }
  76. getCustomHeight(): number {
  77. return this._instance.getCustomHeight();
  78. }
  79. getWidth(): number {
  80. return this._instance.hasCustomSize()
  81. ? this.getCustomWidth()
  82. : this.getDefaultWidth();
  83. }
  84. getHeight(): number {
  85. return this._instance.hasCustomSize()
  86. ? this.getCustomHeight()
  87. : this.getDefaultHeight();
  88. }
  89. getDepth(): number {
  90. return 0;
  91. }
  92. /**
  93. * Return the width of the instance when the instance doesn't have a custom size.
  94. */
  95. getDefaultWidth(): number {
  96. return 32;
  97. }
  98. /**
  99. * Return the height of the instance when the instance doesn't have a custom size.
  100. */
  101. getDefaultHeight(): number {
  102. return 32;
  103. }
  104. getDefaultDepth(): number {
  105. return 0;
  106. }
  107. }