1
0
Fork 0
mirror of https://github.com/ninjamuffin99/Funkin.git synced 2024-11-10 08:44:47 +00:00
Funkin/source/funkin/input/Cursor.hx

283 lines
7.4 KiB
Haxe
Raw Normal View History

package funkin.input;
import openfl.utils.Assets;
import lime.app.Future;
import openfl.display.BitmapData;
class Cursor
{
public static var cursorMode(default, set):CursorMode;
static final CURSOR_DEFAULT_PARAMS:CursorParams =
{
graphic: "assets/images/cursor/cursor-default.png",
scale: 1.0,
offsetX: 0,
offsetY: 0,
};
static var assetCursorDefault:BitmapData = null;
2022-11-08 22:28:09 +00:00
static final CURSOR_CROSS_PARAMS:CursorParams =
{
graphic: "assets/images/cursor/cursor-cross.png",
scale: 1.0,
offsetX: 0,
offsetY: 0,
};
static var assetCursorCross:BitmapData = null;
2022-11-08 22:28:09 +00:00
static final CURSOR_ERASER_PARAMS:CursorParams =
{
graphic: "assets/images/cursor/cursor-eraser.png",
scale: 1.0,
offsetX: 0,
offsetY: 0,
};
static var assetCursorEraser:BitmapData = null;
2022-11-08 22:28:09 +00:00
static final CURSOR_GRABBING_PARAMS:CursorParams =
{
graphic: "assets/images/cursor/cursor-grabbing.png",
scale: 1.0,
offsetX: 32,
offsetY: 0,
};
static var assetCursorGrabbing:BitmapData = null;
2022-11-08 22:28:09 +00:00
static final CURSOR_HOURGLASS_PARAMS:CursorParams =
{
graphic: "assets/images/cursor/cursor-hourglass.png",
scale: 1.0,
offsetX: 0,
offsetY: 0,
};
static var assetCursorHourglass:BitmapData = null;
2022-11-08 22:28:09 +00:00
static final CURSOR_POINTER_PARAMS:CursorParams =
{
graphic: "assets/images/cursor/cursor-pointer.png",
scale: 1.0,
offsetX: 8,
offsetY: 0,
};
static var assetCursorPointer:BitmapData = null;
2022-11-08 22:28:09 +00:00
static final CURSOR_TEXT_PARAMS:CursorParams =
{
graphic: "assets/images/cursor/cursor-text.png",
scale: 1.0,
offsetX: 0,
offsetY: 0,
};
static var assetCursorText:BitmapData = null;
2022-11-08 22:28:09 +00:00
static final CURSOR_ZOOM_IN_PARAMS:CursorParams =
{
graphic: "assets/images/cursor/cursor-zoom-in.png",
scale: 1.0,
offsetX: 0,
offsetY: 0,
};
static var assetCursorZoomIn:BitmapData = null;
2022-11-08 22:28:09 +00:00
static final CURSOR_ZOOM_OUT_PARAMS:CursorParams =
{
graphic: "assets/images/cursor/cursor-zoom-out.png",
scale: 1.0,
offsetX: 0,
offsetY: 0,
};
static var assetCursorZoomOut:BitmapData = null;
static function set_cursorMode(value:CursorMode):CursorMode
{
if (cursorMode != value)
{
cursorMode = value;
setCursorGraphic(cursorMode);
}
return cursorMode;
}
public static inline function show():Void
{
FlxG.mouse.visible = true;
}
public static inline function hide():Void
{
FlxG.mouse.visible = false;
}
static function setCursorGraphic(?value:CursorMode = null):Void
{
if (value == null)
{
FlxG.mouse.unload();
return;
}
switch (value)
{
case Default:
if (assetCursorDefault == null)
{
var future:Future<BitmapData> = Assets.loadBitmapData(CURSOR_DEFAULT_PARAMS.graphic);
2023-06-08 20:30:45 +00:00
future.onComplete(function(bitmapData:BitmapData) {
assetCursorDefault = bitmapData;
applyCursorParams(assetCursorDefault, CURSOR_DEFAULT_PARAMS);
});
}
else
{
applyCursorParams(assetCursorDefault, CURSOR_DEFAULT_PARAMS);
}
case Cross:
if (assetCursorCross == null)
{
var future:Future<BitmapData> = Assets.loadBitmapData(CURSOR_CROSS_PARAMS.graphic);
2023-06-08 20:30:45 +00:00
future.onComplete(function(bitmapData:BitmapData) {
assetCursorCross = bitmapData;
applyCursorParams(assetCursorCross, CURSOR_CROSS_PARAMS);
});
}
else
{
applyCursorParams(assetCursorCross, CURSOR_CROSS_PARAMS);
}
2022-11-08 22:28:09 +00:00
case Eraser:
if (assetCursorEraser == null)
{
var future:Future<BitmapData> = Assets.loadBitmapData(CURSOR_ERASER_PARAMS.graphic);
2023-06-08 20:30:45 +00:00
future.onComplete(function(bitmapData:BitmapData) {
assetCursorEraser = bitmapData;
applyCursorParams(assetCursorEraser, CURSOR_ERASER_PARAMS);
});
}
else
{
applyCursorParams(assetCursorEraser, CURSOR_ERASER_PARAMS);
}
2022-11-08 22:28:09 +00:00
case Grabbing:
if (assetCursorGrabbing == null)
{
var future:Future<BitmapData> = Assets.loadBitmapData(CURSOR_GRABBING_PARAMS.graphic);
2023-06-08 20:30:45 +00:00
future.onComplete(function(bitmapData:BitmapData) {
assetCursorGrabbing = bitmapData;
applyCursorParams(assetCursorGrabbing, CURSOR_GRABBING_PARAMS);
});
}
else
{
applyCursorParams(assetCursorGrabbing, CURSOR_GRABBING_PARAMS);
}
2022-11-08 22:28:09 +00:00
case Hourglass:
if (assetCursorHourglass == null)
{
var future:Future<BitmapData> = Assets.loadBitmapData(CURSOR_HOURGLASS_PARAMS.graphic);
2023-06-08 20:30:45 +00:00
future.onComplete(function(bitmapData:BitmapData) {
assetCursorHourglass = bitmapData;
applyCursorParams(assetCursorHourglass, CURSOR_HOURGLASS_PARAMS);
});
}
else
{
applyCursorParams(assetCursorHourglass, CURSOR_HOURGLASS_PARAMS);
}
2022-11-08 22:28:09 +00:00
case Pointer:
if (assetCursorPointer == null)
{
var future:Future<BitmapData> = Assets.loadBitmapData(CURSOR_POINTER_PARAMS.graphic);
2023-06-08 20:30:45 +00:00
future.onComplete(function(bitmapData:BitmapData) {
assetCursorPointer = bitmapData;
applyCursorParams(assetCursorPointer, CURSOR_POINTER_PARAMS);
});
}
else
{
applyCursorParams(assetCursorPointer, CURSOR_POINTER_PARAMS);
}
2022-11-08 22:28:09 +00:00
case Text:
if (assetCursorText == null)
{
var future:Future<BitmapData> = Assets.loadBitmapData(CURSOR_TEXT_PARAMS.graphic);
2023-06-08 20:30:45 +00:00
future.onComplete(function(bitmapData:BitmapData) {
assetCursorText = bitmapData;
applyCursorParams(assetCursorText, CURSOR_TEXT_PARAMS);
});
}
else
{
applyCursorParams(assetCursorText, CURSOR_TEXT_PARAMS);
}
2022-11-08 22:28:09 +00:00
case ZoomIn:
if (assetCursorZoomIn == null)
{
var future:Future<BitmapData> = Assets.loadBitmapData(CURSOR_ZOOM_IN_PARAMS.graphic);
2023-06-08 20:30:45 +00:00
future.onComplete(function(bitmapData:BitmapData) {
assetCursorZoomIn = bitmapData;
applyCursorParams(assetCursorZoomIn, CURSOR_ZOOM_IN_PARAMS);
});
}
else
{
applyCursorParams(assetCursorZoomIn, CURSOR_ZOOM_IN_PARAMS);
}
2022-11-08 22:28:09 +00:00
case ZoomOut:
if (assetCursorZoomOut == null)
{
var future:Future<BitmapData> = Assets.loadBitmapData(CURSOR_ZOOM_OUT_PARAMS.graphic);
2023-06-08 20:30:45 +00:00
future.onComplete(function(bitmapData:BitmapData) {
assetCursorZoomOut = bitmapData;
applyCursorParams(assetCursorZoomOut, CURSOR_ZOOM_OUT_PARAMS);
});
}
else
{
applyCursorParams(assetCursorZoomOut, CURSOR_ZOOM_OUT_PARAMS);
}
2022-11-08 22:28:09 +00:00
default:
setCursorGraphic(null);
}
}
2022-11-08 22:28:09 +00:00
static inline function applyCursorParams(graphic:BitmapData, params:CursorParams):Void
{
FlxG.mouse.load(graphic, params.scale, params.offsetX, params.offsetY);
}
}
// https://developer.mozilla.org/en-US/docs/Web/CSS/cursor
enum CursorMode
{
Default;
Cross;
Eraser;
Grabbing;
Hourglass;
Pointer;
Text;
ZoomIn;
ZoomOut;
2022-11-08 22:28:09 +00:00
}
/**
* Static data describing how a cursor should be rendered.
*/
typedef CursorParams =
{
graphic:String,
scale:Float,
offsetX:Int,
offsetY:Int,
}