Selaa lähdekoodia

Add docs to `System.setCursor` (#518)

Add an example of using `setCursor`.
Pavel Alexandrov 6 vuotta sitten
vanhempi
commit
fb86a02137
2 muutettua tiedostoa jossa 30 lisäystä ja 3 poistoa
  1. 5 1
      hxd/System.hx
  2. 25 2
      samples/Cursor.hx

+ 5 - 1
hxd/System.hx

@@ -22,7 +22,11 @@ class System {
 	public static var lang(get, never) : String;
 	public static var lang(get, never) : String;
 	public static var platform(get, never) : Platform;
 	public static var platform(get, never) : Platform;
 	public static var screenDPI(get,never) : Float;
 	public static var screenDPI(get,never) : Float;
-	public static var setCursor = setNativeCursor;
+	/**
+		Sets current cursor and can be replaced by custom function to manually operate displayed cursor.
+		When called, it should call `hxd.System.setNativeCursor` and pass desired `hxd.Cursor` to it.
+	**/
+	public static var setCursor : Cursor -> Void = setNativeCursor;
 
 
 	/**
 	/**
 		Can be used to temporarly disable infinite loop check
 		Can be used to temporarly disable infinite loop check

+ 25 - 2
samples/Cursor.hx

@@ -1,6 +1,7 @@
-class Cursor extends hxd.App {
+class Cursor extends SampleApp {
 	
 	
 	override function init() {
 	override function init() {
+		super.init();
 
 
 		engine.backgroundColor = 0xFF202020;
 		engine.backgroundColor = 0xFF202020;
 
 
@@ -30,7 +31,7 @@ class Cursor extends hxd.App {
 		}
 		}
 		
 		
 		var cursors : Array<hxd.Cursor> = [Default,Button,Move,TextInput,Hide,Custom(new hxd.Cursor.CustomCursor([bmp],10,16,16)),Custom(new hxd.Cursor.CustomCursor(animatedFrames,10,16,16))];
 		var cursors : Array<hxd.Cursor> = [Default,Button,Move,TextInput,Hide,Custom(new hxd.Cursor.CustomCursor([bmp],10,16,16)),Custom(new hxd.Cursor.CustomCursor(animatedFrames,10,16,16))];
-		var pos = 0;
+		var pos = 3;
 		for( c in cursors ) {
 		for( c in cursors ) {
 			var i = new h2d.Interactive(120, 20, s2d);
 			var i = new h2d.Interactive(120, 20, s2d);
 			var tf = new h2d.Text(hxd.res.DefaultFont.get(), i);
 			var tf = new h2d.Text(hxd.res.DefaultFont.get(), i);
@@ -57,6 +58,28 @@ class Cursor extends hxd.App {
 				default:
 				default:
 			}
 			}
 		}
 		}
+
+		// It's possible to override default cursors by custom ones by setting
+		// `hxd.System.setCursor` function.
+		// Useful when game utilizes stylized cursors for everything.
+
+		var doOverride = false;
+		var defOverride = new hxd.BitmapData(10, 10);
+		defOverride.line(0, 0, 5, 0, 0xffff0000);
+		defOverride.line(0, 1, 0, 5, 0xffff0000);
+		defOverride.line(0, 0, 10, 10, 0xffff0000);
+		var overrideCursor:hxd.Cursor = Custom(new hxd.Cursor.CustomCursor([defOverride], 0, 0, 0));
+
+		hxd.System.setCursor = function( cur : hxd.Cursor ) {
+			if (doOverride && cur == Default) {
+				hxd.System.setNativeCursor(overrideCursor);
+			} else {
+				hxd.System.setNativeCursor(cur);
+			}
+		}
+
+		addText("Override Default cursor by Custom");
+		addCheck("Enable", function() { return doOverride; }, function(v) { doOverride = v; });
 	}
 	}
 
 
 	static function main() {
 	static function main() {