2
0

TextureChoice.hx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package hide.comp;
  2. import hrt.impl.TextureType;
  3. import hide.comp.GradientEditor.GradientBox;
  4. import hrt.impl.Gradient;
  5. // Allow the user to choose between picking a texture on disk,
  6. // creating a gradient, and future other choices of texture generation
  7. class TextureChoice extends Component {
  8. public var value(get, set) : Any;
  9. var innerValue : Any;
  10. public function new(?parent : Element,?root : Element) {
  11. var e = new Element("<div class='texture-choice'>");
  12. if (root != null)
  13. root.replaceWith(e);
  14. super(parent, e);
  15. rebuildUi();
  16. }
  17. public dynamic function onValueChange() {
  18. }
  19. public function rebuildUi() {
  20. element.empty();
  21. switch (Utils.getTextureType(innerValue)) {
  22. case TextureType.path: {
  23. // Small fix for the texture preview
  24. var wrapper = new Element("<div>").css({position: "relative"}).appendTo(element);
  25. var select = new hide.comp.TextureSelect(wrapper,null);
  26. select.element.width("auto");
  27. select.path = innerValue;
  28. select.onChange = function() {
  29. set_value(select.path);
  30. onChange(true);
  31. }
  32. onValueChange = function() {
  33. select.path = innerValue;
  34. }
  35. }
  36. case TextureType.gradient: {
  37. var gradient = new GradientBox(element, null);
  38. gradient.onChange = function(isDragging:Bool) {
  39. set_value({type:TextureType.gradient, data:gradient.value});
  40. onChange(!isDragging);
  41. }
  42. onValueChange = function() {
  43. gradient.value = Utils.getGradientData(innerValue);
  44. }
  45. }
  46. default : {
  47. new Element("<div>").text("Unhandled data (check log)").appendTo(element);
  48. trace("Unhandled data", innerValue);
  49. onValueChange = function() {};
  50. }
  51. }
  52. addChangeBtn();
  53. }
  54. function addChangeBtn() {
  55. var btn = new Element("<div class='hide-button change-button' title='Actions ...'>").appendTo(element);
  56. new Element("<div class='icon ico ico-ellipsis-h'>").appendTo(btn);
  57. btn.click(function(e) {
  58. new hide.comp.ContextMenu([
  59. { label : "Change to Texturepath", click : function() changeTextureType(TextureType.path), enabled: Utils.getTextureType(innerValue) != TextureType.path},
  60. { label : "Change to Gradient", click : function() changeTextureType(TextureType.gradient), enabled: Utils.getTextureType(innerValue) != TextureType.gradient},
  61. ]);
  62. });
  63. }
  64. function changeTextureType(newType : TextureType) {
  65. switch (newType) {
  66. case TextureType.path:
  67. set_value(null);
  68. case TextureType.gradient: {
  69. set_value({type:TextureType.gradient, data:Gradient.getDefaultGradientData()});
  70. }
  71. default :
  72. throw "unhandeld TextureType change";
  73. }
  74. onChange(true);
  75. }
  76. public function set_value(value : Any) {
  77. if (value == innerValue)
  78. return value;
  79. var prevValue = innerValue;
  80. innerValue = value;
  81. if (Type.typeof(value) != Type.typeof(prevValue)) {
  82. rebuildUi();
  83. }
  84. else if (Type.typeof(value) == TObject) {
  85. if ((value:Dynamic).type != (prevValue:Dynamic).type) {
  86. rebuildUi();
  87. }
  88. }
  89. onValueChange();
  90. return innerValue;
  91. }
  92. public function get_value() {
  93. return innerValue;
  94. }
  95. public dynamic function onChange(shouldUndo : Bool) {
  96. }
  97. }