2
0

TextureType.hx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package hrt.impl;
  2. import hrt.impl.Gradient;
  3. @:enum
  4. abstract TextureType(String) from String to String {
  5. var gradient;
  6. var path; // Not used as a type inside the json (the playload is a string), default value
  7. }
  8. class Utils {
  9. public static function getTextureFromValue(val : Any) : h3d.mat.Texture {
  10. if (Std.isOfType(val, String)) {
  11. var t = hxd.res.Loader.currentInstance.load(val).toTexture();
  12. t.wrap = Repeat;
  13. return t;
  14. }
  15. else if (Type.typeof(val) == TObject) {
  16. var val = (val:Dynamic);
  17. if (val.type != null && Std.isOfType(val.type, String)) {
  18. switch((val.type:String):TextureType) {
  19. case TextureType.gradient:
  20. {
  21. var gradData = Utils.getGradientData((val:Dynamic));
  22. if (gradData != null) {
  23. var t = Gradient.textureFromData(gradData);
  24. return t;
  25. }
  26. }
  27. default:
  28. }
  29. }
  30. }
  31. return null;
  32. }
  33. // Returns null if value is not a GradientData
  34. public static function getGradientData(value : Any) : Null<Gradient.GradientData> {
  35. if (getTextureType(value) == gradient) {
  36. var gradientData = ((value:Dynamic).data:GradientData);
  37. gradientData.interpolation = gradientData.interpolation != null ? gradientData.interpolation : Linear;
  38. gradientData.colorMode = (gradientData.colorMode:Dynamic) != null ? gradientData.colorMode : 0;
  39. return gradientData;
  40. }
  41. return null;
  42. }
  43. public static function getTextureType(value : Any) : Null<TextureType> {
  44. if (value == null || Std.isOfType(value, String)) {
  45. return TextureType.path;
  46. }
  47. else if (Type.typeof(value) == TObject) {
  48. var v : Dynamic = (value:Dynamic);
  49. if (v.type != null && Std.isOfType(v.type, String)) {
  50. switch ((v.type:String):TextureType) {
  51. case TextureType.gradient: return TextureType.gradient;
  52. default:
  53. return null;
  54. }
  55. }
  56. }
  57. return null;
  58. }
  59. public static function copyTextureData(val : Any) : Any {
  60. if (Type.typeof(val) == TObject)
  61. return haxe.Json.parse(haxe.Json.stringify(val));
  62. return val;
  63. }
  64. }