浏览代码

Added ability to rotate gradients

Clement Espeute 3 年之前
父节点
当前提交
ed944a968f
共有 2 个文件被更改,包括 36 次插入7 次删除
  1. 26 3
      hide/comp/GradientEditor.hx
  2. 10 4
      hrt/impl/Gradient.hx

+ 26 - 3
hide/comp/GradientEditor.hx

@@ -87,6 +87,7 @@ class GradientEditor extends Popup {
     var stopLabel : Element;
 
     var resolutionInput : Element;
+    var isVerticalCheckbox : Element;
 
     var keys : hide.ui.Keys;
 
@@ -198,12 +199,15 @@ class GradientEditor extends Popup {
         }
 
         var detailsSection = new Element("<details>").appendTo(editor);
-        new Element("<summary>").text("Settings").appendTo(detailsSection);
+        new Element("<summary>").text("Generated texture settings").appendTo(detailsSection);
 
-        new Element("<label for='resolution'>").text("Texture Width").appendTo(detailsSection);
+        var detailsDiv = new Element("<div>").css("padding", "4px").appendTo(detailsSection);
 
-        resolutionInput = new Element("<select id='resolution' name='resolution'>").appendTo(detailsSection);
+        resolutionInput = new Element("<select id='resolution' name='resolution'>");
 
+        new Element("<div>").appendTo(detailsDiv)
+            .append( new Element("<label for='resolution'>").text("Resolution"))
+            .append(resolutionInput);
         for (i in 1...12) {
             var val = Math.pow(2, i);
             new Element('<option value="$val">').text('$val px').appendTo(resolutionInput);
@@ -216,6 +220,24 @@ class GradientEditor extends Popup {
         });
 
 
+        isVerticalCheckbox = new Element("<select id='isVertical'>");
+        
+        new Element('<option value="0">').text('Horizontal').appendTo(isVerticalCheckbox);
+        new Element('<option value="1">').text('Vertical').appendTo(isVerticalCheckbox);
+
+        isVerticalCheckbox.on("change", function(e : js.jquery.Event) {
+            var val : Bool = isVerticalCheckbox.val() == 1 ? true : false;
+            innerValue.isVertical = val;
+            trace(val);
+            onChange(false);
+        });
+
+        new Element("<div>").appendTo(detailsDiv)
+            .append(new Element("<label for='isVertical'>").text("Orientation").attr("title", "Change the orientation of the generated texture. This don't have any effect on gradients that are used only on the cpu"))
+            .append(isVerticalCheckbox);
+
+
+
         reflow();
         fixInputSelect();
     }
@@ -321,6 +343,7 @@ class GradientEditor extends Popup {
         }
 
         resolutionInput.val('${innerValue.resolution}');
+        isVerticalCheckbox.val('${innerValue.isVertical ? 1 : 0}');
     }
 
     function removeStop(element : Element) {

+ 10 - 4
hrt/impl/Gradient.hx

@@ -9,19 +9,21 @@ typedef ColorStop = {position : Float, color : Int};
 typedef GradientData = {
     var stops : Array<ColorStop>;
     var resolution : Int;
+    var isVertical : Bool;
 };
 
 class Gradient {
     public var data : GradientData = {
         stops: new Array<ColorStop>(),
-        resolution: 32
+        resolution: 32,
+        isVertical: false,
     };
 
     public function new () {
     }
 
     public static function getDefaultGradientData() : GradientData {
-        var data : GradientData = {stops: [{position: 0.0, color:0xFF000000}, {position: 1.0, color:0xFFFFFFFF}], resolution: 64};
+        var data : GradientData = {stops: [{position: 0.0, color:0xFF000000}, {position: 1.0, color:0xFFFFFFFF}], resolution: 64, isVertical : false};
         return data;
     }
 
@@ -74,6 +76,8 @@ class Gradient {
 
     public static function getDataHash(data : GradientData) : Int32 {
         var hash = hashCombine(0, data.resolution);
+        hash = hashCombine(hash, data.isVertical ? 0 : 1);
+
         for (stop in data.stops) {
             hash = hashCombine(hash, stop.color);
             hash = hashCombine(hash, Std.int(stop.position * 214748357));
@@ -102,12 +106,14 @@ class Gradient {
             // and use this copy in the genPixels function. But at this moment we consider that it's a bug
             if(newHash != oldHash) throw "gradient data has changed between first generation and realloc";
             #end
-            var pixels = hxd.Pixels.alloc(data.resolution,1, ARGB);
+            var xScale = data.isVertical ? 1 : 0;
+            var yScale = 1 - xScale;
+            var pixels = hxd.Pixels.alloc(data.resolution * xScale + 1 * yScale,1 * xScale + data.resolution * yScale, ARGB);
 
             var vec = new Vector();
             for (x in 0...data.resolution) {
                 evalData(data, x / data.resolution, vec);
-                pixels.setPixelF(x,0, vec);
+                pixels.setPixelF(x * xScale,x*yScale, vec);
             }
             return pixels;
         }