Răsfoiți Sursa

changed h2d.Tile.from and h3d.mat.Texture.fromColor/clear to take a separate alpha component as parameter (close #60)

Nicolas Cannasse 11 ani în urmă
părinte
comite
eb0076a578

+ 2 - 2
h2d/Console.hx

@@ -39,13 +39,13 @@ class Console extends h2d.Sprite {
 		logTxt.visible = false;
 		logs = [];
 		logIndex = -1;
-		bg = new h2d.Bitmap(h2d.Tile.fromColor(0x80000000), this);
+		bg = new h2d.Bitmap(h2d.Tile.fromColor(0,1,1,0.5), this);
 		bg.visible = false;
 		tf = new h2d.Text(font, bg);
 		tf.x = 2;
 		tf.y = 1;
 		tf.textColor = 0xFFFFFFFF;
-		cursor = new h2d.Bitmap(h2d.Tile.fromColor(tf.textColor | 0xFF000000, 1, font.lineHeight), tf);
+		cursor = new h2d.Bitmap(h2d.Tile.fromColor(tf.textColor, 1, font.lineHeight), tf);
 		commands = new Map();
 		aliases = new Map();
 		addCommand("help", "Show help", [ { name : "command", t : AString, opt : true } ], showHelp);

+ 1 - 1
h2d/Graphics.hx

@@ -123,7 +123,7 @@ class Graphics extends Drawable {
 	public function new(?parent) {
 		super(parent);
 		content = new GraphicsContent();
-		tile = h2d.Tile.fromColor(0xFFFFFFFF);
+		tile = h2d.Tile.fromColor(0xFFFFFF);
 		clear();
 	}
 

+ 1 - 1
h2d/Interactive.hx

@@ -33,7 +33,7 @@ class Interactive extends Drawable {
 	}
 
 	override function draw( ctx : RenderContext ) {
-		if( backgroundColor != null ) emitTile(ctx,h2d.Tile.fromColor(backgroundColor,Std.int(width),Std.int(height)));
+		if( backgroundColor != null ) emitTile(ctx, h2d.Tile.fromColor(backgroundColor, Std.int(width), Std.int(height), (backgroundColor>>>24)/255 ));
 	}
 
 	override function getBoundsRec( relativeTo, out ) {

+ 1 - 1
h2d/RenderContext.hx

@@ -74,7 +74,7 @@ class RenderContext {
 	}
 
 	public function beforeDraw() {
-		if( texture == null ) texture = h3d.mat.Texture.fromColor(0xFFFF00FF);
+		if( texture == null ) texture = h3d.mat.Texture.fromColor(0xFF00FF);
 		baseShader.texture = texture;
 		texture.filter = currentObj.filter ? Linear : Nearest;
 		texture.wrap = currentObj.tileWrap ? Repeat : Clamp;

+ 2 - 2
h2d/Tile.hx

@@ -179,8 +179,8 @@ class Tile {
 	}
 
 
-	public static function fromColor( color : Int, ?width = 1, ?height = 1, ?allocPos : h3d.impl.AllocPos ) {
-		var t = new Tile(h3d.mat.Texture.fromColor(color,allocPos),0,0,1,1);
+	public static function fromColor( color : Int, ?width = 1, ?height = 1, ?alpha = 1., ?allocPos : h3d.impl.AllocPos ) {
+		var t = new Tile(h3d.mat.Texture.fromColor(color,alpha,allocPos),0,0,1,1);
 		// scale to size
 		t.width = width;
 		t.height = height;

+ 1 - 1
h2d/comp/Input.hx

@@ -106,7 +106,7 @@ class Input extends Interactive {
 		super.resize(ctx);
 		if( !ctx.measure ) {
 			cursor.y = extTop() - 1;
-			cursor.tile = h2d.Tile.fromColor(style.cursorColor | 0xFF000000, 1, Std.int(height - extTop() - extBottom() + 2));
+			cursor.tile = h2d.Tile.fromColor(style.cursorColor, 1, Std.int(height - extTop() - extBottom() + 2));
 		}
 	}
 

+ 2 - 2
h2d/comp/ItemList.hx

@@ -46,7 +46,7 @@ class ItemList extends Box {
 				}
 				if( selected ) {
 					if( cursor != null ) cursor.remove();
-					cursor = new h2d.Bitmap(h2d.Tile.fromColor(style.selectionColor, Std.int(int.width), Std.int(int.height)), int);
+					cursor = new h2d.Bitmap(h2d.Tile.fromColor(style.selectionColor, Std.int(int.width), Std.int(int.height), (style.selectionColor>>>24)/255), int);
 					int.onOver = function(_) {
 						onItemOver(i);
 					};
@@ -58,7 +58,7 @@ class ItemList extends Box {
 				} else {
 					int.onOver = function(_) {
 						if( cursor != null ) cursor.remove();
-						cursor = new h2d.Bitmap(h2d.Tile.fromColor(style.cursorColor, Std.int(int.width), Std.int(int.height)), int);
+						cursor = new h2d.Bitmap(h2d.Tile.fromColor(style.cursorColor, Std.int(int.width), Std.int(int.height), (style.cursorColor>>>24)/255 ), int);
 						onItemOver(i);
 					};
 					int.onOut = function(_) {

+ 1 - 1
h2d/css/Fill.hx

@@ -4,7 +4,7 @@ import h2d.css.Defs;
 class Fill extends h2d.TileGroup {
 
 	public function new(?parent) {
-		super(h2d.Tile.fromColor(0xFFFFFFFF), parent);
+		super(h2d.Tile.fromColor(0xFFFFFF), parent);
 	}
 
 	public inline function fillRectColor(x, y, w, h, c) {

+ 1 - 1
h3d/impl/Stage3dDriver.hx

@@ -453,7 +453,7 @@ class Stage3dDriver extends Driver {
 			for( i in 0...curShader.s.fragment.textures.length ) {
 				var t = buffers.fragment.tex[i];
 				if( t == null || t.isDisposed() )
-					t = h3d.mat.Texture.fromColor(0xFFFF00FF);
+					t = h3d.mat.Texture.fromColor(0xFF00FF);
 				if( t != null && t.t == null && t.realloc != null ) {
 					t.alloc();
 					t.realloc();

+ 12 - 8
h3d/mat/Texture.hx

@@ -124,11 +124,12 @@ class Texture {
 			alloc();
 	}
 
-	public function clear( color : Int ) {
+	public function clear( color : Int, alpha = 1. ) {
 		alloc();
 		var p = hxd.Pixels.alloc(width, height, BGRA);
 		var k = 0;
-		var b = color & 0xFF, g = (color >> 8) & 0xFF, r = (color >> 16) & 0xFF, a = color >>> 24;
+		var b = color & 0xFF, g = (color >> 8) & 0xFF, r = (color >> 16) & 0xFF, a = Std.int(alpha * 255);
+		if( a < 0 ) a = 0 else if( a > 255 ) a = 255;
 		for( i in 0...width * height ) {
 			p.bytes.set(k++,b);
 			p.bytes.set(k++,g);
@@ -172,16 +173,19 @@ class Texture {
 
 	static var COLOR_CACHE = new Map<Int,h3d.mat.Texture>();
 	/**
-		Creates a 1x1 texture using the ARGB color passed as parameter.
+		Creates a 1x1 texture using the RGB color passed as parameter.
 	**/
-	public static function fromColor( color : Int, ?allocPos : h3d.impl.AllocPos ) {
-		var t = COLOR_CACHE.get(color);
+	public static function fromColor( color : Int, alpha = 1., ?allocPos : h3d.impl.AllocPos ) {
+		var aval = Std.int(alpha * 255);
+		if( aval < 0 ) aval = 0 else if( aval > 255 ) aval = 255;
+		var key = (color&0xFFFFFF) | (aval << 24);
+		var t = COLOR_CACHE.get(key);
 		if( t != null )
 			return t;
 		var t = new Texture(1, 1, null, allocPos);
-		t.clear(color);
-		t.realloc = function() t.clear(color);
-		COLOR_CACHE.set(color, t);
+		t.clear(color, alpha);
+		t.realloc = function() t.clear(color, alpha);		
+		COLOR_CACHE.set(key, t);
 		return t;
 	}
 

+ 1 - 1
h3d/parts/Editor.hx

@@ -136,7 +136,7 @@ class Editor extends h2d.Sprite implements Randomized {
 				var s = State.load(data, function(name) {
 					var t = loadTexture(name);
 					if( t == null ) {
-						t = h2d.Tile.fromColor(0xFF800000);
+						t = h2d.Tile.fromColor(0x800000);
 						// try dynamic loading. Will most likely fail since the path is relative
 						hxd.File.load(name, function(bytes) {
 							setTexture(hxd.res.Any.fromBytes(name, bytes).toTile());

+ 1 - 1
h3d/shader/Manager.hx

@@ -125,7 +125,7 @@ class Manager {
 			var tid = 0;
 			for( p in s.textures ) {
 				var t = getParamValue(p, shaders);
-				if( t == null ) t = h3d.mat.Texture.fromColor(0xFFFF00FF);
+				if( t == null ) t = h3d.mat.Texture.fromColor(0xFF00FF);
 				buf.tex[tid++] = t;
 			}
 		}

+ 2 - 2
hxd/fmt/fbx/Library.hx

@@ -795,7 +795,7 @@ class Library {
 			var tmpTex = null;
 			textureLoader = function(_,_) {
 				if( tmpTex == null )
-					tmpTex = h3d.mat.Texture.fromColor(0xFFFF00FF);
+					tmpTex = h3d.mat.Texture.fromColor(0xFF00FF);
 				return new h3d.mat.MeshMaterial(tmpTex);
 			}
 		}
@@ -884,7 +884,7 @@ class Library {
 				while( tmats.length > lastAdded )
 					tmats.pop();
 				if( tmats.length == 0 )
-					tmats.push(new h3d.mat.MeshMaterial(h3d.mat.Texture.fromColor(0xFFFF00FF)));
+					tmats.push(new h3d.mat.MeshMaterial(h3d.mat.Texture.fromColor(0xFF00FF)));
 				// create object
 				if( tmats.length == 1 )
 					o.obj = new h3d.scene.Mesh(prim, tmats[0], scene);

+ 10 - 10
samples/bounds/Bounds.hx

@@ -7,13 +7,13 @@ class Bounds extends hxd.App {
 
 	override function init() {
 		boxes = [];
-		
+
 		g = new h2d.Graphics(s2d);
 		for( i in 0...colors.length ) {
 			var size = Std.int(200 / (i + 4));
 			var c = colors[i];
-			var b = new h2d.Bitmap(h2d.Tile.fromColor(c | 0x80000000, size, size).sub(0, 0, size, size, -Std.random(size), -Std.random(size)), i == 0 ? s2d : boxes[i - 1]);
-			b.addChild(new h2d.Bitmap(h2d.Tile.fromColor(0xFFFFFFFF, 8, 8).sub(0, 0, 8, 8, -4, -4)));
+			var b = new h2d.Bitmap(h2d.Tile.fromColor(c, size, size, 0.5).sub(0, 0, size, size, -Std.random(size), -Std.random(size)), i == 0 ? s2d : boxes[i - 1]);
+			b.addChild(new h2d.Bitmap(h2d.Tile.fromColor(0xFFFFFF, 8, 8).sub(0, 0, 8, 8, -4, -4)));
 			if( i == 0 ) {
 				b.x = s2d.width * 0.5;
 				b.y = s2d.height * 0.5;
@@ -33,17 +33,17 @@ class Bounds extends hxd.App {
 		tf.x = -5;
 		tf.y = 15;
 		tf.filter = true;
-		
-		
-		
-		var g = new h2d.Bitmap( h2d.Tile.fromColor(0xFFFF0000,32, 32), s2d);
+
+
+
+		var g = new h2d.Bitmap( h2d.Tile.fromColor(0xFF0000,32, 32), s2d);
 		g.x += 32;
 		g.y += 32;
 		trace(g.getBounds(g.parent).width);
 		g.scaleX = 2.0;
 		trace(g.getBounds(g.parent).width);
-		
-		
+
+
 		var g =  new h2d.Graphics(s2d );
 		g.drawRect(0, 0, 32, 32);
 		g.x += 32;
@@ -51,7 +51,7 @@ class Bounds extends hxd.App {
 		trace(g.getBounds(g.parent).width);
 		g.scaleX = 2.0;
 		trace(g.getBounds(g.parent).width);
-		
+
 		g.scaleY = 3.0;
 		trace(g.getBounds(g.parent).height);
 	}

+ 10 - 10
samples/quat/QuatDemo.hx

@@ -1,22 +1,22 @@
 class QuatDemo extends hxd.App {
-	
+
 	var cube : h3d.scene.Mesh;
-	
+
 	var tx : Float = 0.;
 	var ty : Float = 0.;
 	var tz : Float = 0.;
-	
+
 	override function init() {
 		var p = new h3d.prim.Cube(1, 1, 1);
 		p.translate( -0.25, -0.5, -0.5);
 		p.addUVs();
 		p.addNormals();
-		
+
 		cube = new h3d.scene.Mesh(p, s3d);
-		cube.material.texture = h2d.Tile.fromColor(0xFF808080).getTexture();
-		
+		cube.material.texture = h2d.Tile.fromColor(0x808080).getTexture();
+
 		var axis = new h3d.scene.Object(s3d);
-		
+
 		var ax = new h3d.scene.Box(0xFFFF0000, true, axis);
 		ax.x = 0.5;
 		ax.scaleY = 0.001;
@@ -49,7 +49,7 @@ class QuatDemo extends hxd.App {
 
 		var ldir = new h3d.Vector( -1, -2, -5);
 		ldir.normalize();
-		
+
 		/*
 		cube.material.lightSystem = {
 			ambient : new h3d.Vector(0.5, 0.5, 0.5),
@@ -61,9 +61,9 @@ class QuatDemo extends hxd.App {
 		};
 		*/
 	}
-	
+
 	var time = 0.;
-	
+
 	override function update(dt:Float) {
 		time += dt * 0.01;
 		var q = new h3d.Quat();