Ver Fonte

added hl native cursor support

Nicolas Cannasse há 9 anos atrás
pai
commit
7308819441
2 ficheiros alterados com 90 adições e 4 exclusões
  1. 46 4
      hxd/System.hx
  2. 44 0
      samples/Cursor.hx

+ 46 - 4
hxd/System.hx

@@ -430,8 +430,50 @@ class System {
 
 	#elseif hxsdl
 
+	static var currentNativeCursor : Cursor = Default;
+	static var currentCursor : sdl.Cursor;
+	static var cursorVisible = true;
+
 	public static function setNativeCursor( c : Cursor ) {
-		//trace("TODO " + c);
+		if( c.equals(currentNativeCursor) )
+			return;
+		currentNativeCursor = c;
+		if( c == Hide ) {
+			cursorVisible = false;
+			sdl.Cursor.show(false);
+			return;
+		}
+		var cur : sdl.Cursor;
+		switch( c ) {
+		case Default:
+			cur = sdl.Cursor.createSystem(Arrow);
+		case Button:
+			cur = sdl.Cursor.createSystem(Hand);
+		case Move:
+			throw "Cursor not supported";
+		case TextInput:
+			cur = sdl.Cursor.createSystem(IBeam);
+		case Hide:
+			throw "assert";
+		case Custom(frames, speed, offsetX, offsetY):
+			if( frames.length > 1 ) throw "Animated cursor not supported";
+			var pixels = frames[0].getPixels();
+			pixels.convert(BGRA);
+			var surf = sdl.Surface.fromBGRA(pixels.bytes, pixels.width, pixels.height);
+			cur = sdl.Cursor.create(surf, offsetX, offsetY);
+			surf.free();
+			pixels.dispose();
+		}
+		if( currentCursor != null ) {
+			currentCursor.free();
+			currentCursor = null;
+		}
+		currentCursor = cur;
+		cur.set();
+		if( !cursorVisible ) {
+			cursorVisible = true;
+			sdl.Cursor.show(true);
+		}
 	}
 
 	static function get_screenDPI() {
@@ -473,11 +515,11 @@ class System {
 	public static function getClipboard() : String {
 		return "";
 	}
-	
+
 	public static function getCurrentLoop() {
 		return currentLoop;
 	}
-	
+
 	static var win : sdl.Window;
 	static var windowWidth = 800;
 	static var windowHeight = 600;
@@ -657,7 +699,7 @@ class System {
 	public static function getCurrentLoop() {
 		return LOOP;
 	}
-	
+
 	public static function setNativeCursor( c : Cursor ) {
 	}
 

+ 44 - 0
samples/Cursor.hx

@@ -0,0 +1,44 @@
+class Cursor extends hxd.App {
+
+	override function init() {
+
+		engine.backgroundColor = 0xFF202020;
+
+		var bmp = new hxd.BitmapData(32, 32);
+		bmp.clear(0x80FF0000);
+		bmp.line(0, 0, 31, 0, 0xFFFFFFFF);
+		bmp.line(0, 0, 0, 31, 0xFF0000FF);
+		bmp.line(0, 31, 31, 31, 0xFFFF0000);
+		bmp.line(31, 0, 31, 31, 0xFF00FF00);
+
+		var cursors : Array<hxd.System.Cursor> = [Default,Button,Move,TextInput,Hide,Custom([bmp],0.,16,16)];
+		var pos = 0;
+		for( c in cursors ) {
+			var i = new h2d.Interactive(100, 20, s2d);
+			var tf = new h2d.Text(hxd.res.DefaultFont.get(), i);
+			tf.text = c.getName();
+			tf.x = 5;
+			i.x = 0;
+			i.y = pos++ * 20;
+			i.cursor = c;
+			i.backgroundColor = Std.random(0x1000000) | 0xFF000000;
+
+			var supported = true;
+			#if hl
+			if( c == Move ) supported = false;
+			#elseif js
+			if( c.match(Custom(_)) ) supported = false;
+			#end
+
+			if( !supported ) {
+				tf.textColor = 0xFF0000;
+				i.cursor = Default;
+			}
+		}
+	}
+
+	static function main() {
+		new Cursor();
+	}
+
+}