TextureType.hx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package hrt.impl;
  2. import hrt.impl.Gradient;
  3. enum abstract TextureType(String) from String to String {
  4. var gradient;
  5. var path; // Not used as a type inside the json (the playload is a string), default value
  6. }
  7. class Utils {
  8. public static function getTextureFromValue(val : Any, wrap : h3d.mat.Data.Wrap = Repeat) : h3d.mat.Texture {
  9. if (Std.isOfType(val, String)) {
  10. var t = hxd.res.Loader.currentInstance.load(val).toTexture();
  11. t.wrap = wrap;
  12. return t;
  13. }
  14. else if (Type.typeof(val) == TObject) {
  15. var val = (val:Dynamic);
  16. if (val.type != null && Std.isOfType(val.type, String)) {
  17. switch((val.type:String):TextureType) {
  18. case TextureType.gradient:
  19. {
  20. var gradData = Utils.getGradientData((val:Dynamic));
  21. if (gradData != null) {
  22. var t = Gradient.textureFromData(gradData);
  23. t.wrap = wrap;
  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. }