VectorSupplier.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2009-2021 jMonkeyEngine
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. package com.jme3.vectoreffect;
  33. import com.jme3.math.ColorRGBA;
  34. import com.jme3.math.Vector2f;
  35. import com.jme3.math.Vector3f;
  36. import com.jme3.math.Vector4f;
  37. public interface VectorSupplier extends Cloneable {
  38. Vector4f get();
  39. void set(Vector4f newVal);
  40. VectorSupplier clone();
  41. static class Vector4fSupplier implements VectorSupplier {
  42. private final Vector4f vector;
  43. Vector4fSupplier(Vector4f vector) {
  44. this.vector = vector;
  45. }
  46. @Override
  47. public Vector4f get() {
  48. return vector;
  49. }
  50. @Override
  51. public void set(Vector4f newVal) {
  52. this.vector.set(newVal);
  53. }
  54. @Override
  55. public Vector4fSupplier clone() {
  56. return new Vector4fSupplier(vector.clone());
  57. }
  58. }
  59. static class Vector3fSupplier implements VectorSupplier {
  60. private final Vector3f vector;
  61. private final Vector4f store = new Vector4f();
  62. Vector3fSupplier(Vector3f vector) {
  63. this.vector = vector;
  64. }
  65. @Override
  66. public Vector4f get() {
  67. store.set(vector.x, vector.y, vector.z, 0);
  68. return store;
  69. }
  70. @Override
  71. public void set(Vector4f newVal) {
  72. this.vector.set(newVal.x, newVal.y, newVal.z);
  73. get(); //update store
  74. }
  75. @Override
  76. public Vector3fSupplier clone() {
  77. return new Vector3fSupplier(vector.clone());
  78. }
  79. }
  80. static class ColorRGBASupplier implements VectorSupplier {
  81. private ColorRGBA color;
  82. private final Vector4f store = new Vector4f();
  83. ColorRGBASupplier(ColorRGBA color) {
  84. this.color = color;
  85. }
  86. @Override
  87. public Vector4f get() {
  88. store.set(color.r, color.g, color.b, color.a);
  89. return store;
  90. }
  91. @Override
  92. public void set(Vector4f newVal) {
  93. this.color.set(newVal.x, newVal.y, newVal.z, newVal.w);
  94. get(); //update store
  95. }
  96. @Override
  97. public ColorRGBASupplier clone() {
  98. return new ColorRGBASupplier(color.clone());
  99. }
  100. }
  101. static class Vector2fSupplier implements VectorSupplier {
  102. private final Vector2f vector;
  103. private final Vector4f store = new Vector4f();
  104. Vector2fSupplier(Vector2f vector) {
  105. this.vector = vector;
  106. }
  107. @Override
  108. public Vector4f get() {
  109. store.set(vector.x, vector.y, 0, 0);
  110. return store;
  111. }
  112. @Override
  113. public void set(Vector4f newVal) {
  114. this.vector.set(newVal.x, newVal.y);
  115. get(); //update store
  116. }
  117. @Override
  118. public Vector2fSupplier clone() {
  119. return new Vector2fSupplier(vector.clone());
  120. }
  121. }
  122. public static VectorSupplier of(Vector4f vector) {
  123. return new Vector4fSupplier(vector);
  124. }
  125. public static VectorSupplier of(Vector3f vector) {
  126. return new Vector3fSupplier(vector);
  127. }
  128. public static VectorSupplier of(Vector2f vector) {
  129. return new Vector2fSupplier(vector);
  130. }
  131. public static VectorSupplier of(ColorRGBA color) {
  132. return new ColorRGBASupplier(color);
  133. }
  134. }