Cursor.hx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. class Cursor extends SampleApp {
  2. override function init() {
  3. super.init();
  4. engine.backgroundColor = 0xFF202020;
  5. var bmp = new hxd.BitmapData(32, 32);
  6. bmp.clear(0x80FF0000);
  7. bmp.line(0, 0, 31, 0, 0xFFFFFFFF);
  8. bmp.line(0, 0, 0, 31, 0xFF0000FF);
  9. bmp.line(0, 31, 31, 31, 0xFFFF0000);
  10. bmp.line(31, 0, 31, 31, 0xFF00FF00);
  11. var animationSupported = false;
  12. #if (flash || js || hldx || hlsdl)
  13. animationSupported = true;
  14. #end
  15. var animatedFrames = [bmp];
  16. if (animationSupported)
  17. {
  18. var bmp2 = new hxd.BitmapData(32, 32);
  19. bmp2.clear(0x80800000);
  20. bmp2.line(0, 0, 31, 0, 0xFFFFFFFF);
  21. bmp2.line(0, 0, 0, 31, 0xFF0000FF);
  22. bmp2.line(0, 31, 31, 31, 0xFFFF0000);
  23. bmp2.line(31, 0, 31, 31, 0xFF00FF00);
  24. bmp2.fill(15, 15, 2, 2, 0xFFFF00FF);
  25. animatedFrames.push(bmp2);
  26. }
  27. 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))];
  28. var pos = 3;
  29. for( c in cursors ) {
  30. var i = new h2d.Interactive(120, 20, s2d);
  31. var tf = new h2d.Text(hxd.res.DefaultFont.get(), i);
  32. tf.text = c.getName();
  33. tf.dropShadow = { dx: 1, dy: 1, color: 0, alpha: 1 };
  34. tf.x = 5;
  35. i.x = 0;
  36. i.y = pos++ * 20;
  37. i.cursor = c;
  38. i.backgroundColor = Std.random(0x1000000) | 0xFF000000;
  39. i.onOver = function(_) tf.alpha = 0.5;
  40. i.onOut = function(_) tf.alpha = 1;
  41. switch(c) {
  42. case Custom(cur):
  43. if (@:privateAccess cur.frames.length > 1) {
  44. tf.text = "Custom (animated)";
  45. if (!animationSupported) {
  46. i.cursor = Default;
  47. tf.textColor = 0xFF0000;
  48. }
  49. }
  50. default:
  51. }
  52. }
  53. // It's possible to override default cursors by custom ones by setting
  54. // `hxd.System.setCursor` function.
  55. // Useful when game utilizes stylized cursors for everything.
  56. // HLSDL note: Cursor offsetX and offsetY should remain inside frame bounds.
  57. // This is a limitation of SDL (most likely for portability reasons).
  58. var doOverride = false;
  59. var defOverride = new hxd.BitmapData(10, 10);
  60. defOverride.line(0, 0, 5, 0, 0xffff0000);
  61. defOverride.line(0, 1, 0, 5, 0xffff0000);
  62. defOverride.line(0, 0, 10, 10, 0xffff0000);
  63. var overrideCursor:hxd.Cursor = Custom(new hxd.Cursor.CustomCursor([defOverride], 0, 0, 0));
  64. hxd.System.setCursor = function( cur : hxd.Cursor ) {
  65. if (doOverride && cur == Default) {
  66. hxd.System.setNativeCursor(overrideCursor);
  67. } else {
  68. hxd.System.setNativeCursor(cur);
  69. }
  70. }
  71. addText("Override Default cursor by Custom");
  72. addCheck("Enable", function() { return doOverride; }, function(v) { doOverride = v; });
  73. }
  74. static function main() {
  75. new Cursor();
  76. }
  77. }