Update Kha/Backends/Krom/kha/SystemImpl.hx

This commit is contained in:
LeenkxTeam 2025-05-13 19:35:50 +00:00
parent c5b21a9bb3
commit 387be05826

View File

@ -1,343 +1,343 @@
package kha; package kha;
import kha.graphics4.TextureFormat; import kha.graphics4.TextureFormat;
import kha.input.Gamepad; import kha.input.Gamepad;
import kha.input.Keyboard; import kha.input.Keyboard;
import kha.input.Mouse; import kha.input.Mouse;
import kha.input.MouseImpl; import kha.input.MouseImpl;
import kha.input.Pen; import kha.input.Pen;
import kha.input.Surface; import kha.input.Surface;
import kha.System; import kha.System;
import haxe.ds.Vector; import haxe.ds.Vector;
class SystemImpl { class SystemImpl {
static var start: Float; static var start: Float;
static var framebuffer: Framebuffer; static var framebuffer: Framebuffer;
static var keyboard: Keyboard; static var keyboard: Keyboard;
static var mouse: Mouse; static var mouse: Mouse;
static var pen: Pen; static var pen: Pen;
static var maxGamepads: Int = 4; static var maxGamepads: Int = 4;
static var gamepads: Array<Gamepad>; static var gamepads: Array<Gamepad>;
static var mouseLockListeners: Array<Void->Void> = []; static var mouseLockListeners: Array<Void->Void> = [];
static function renderCallback(): Void { static function renderCallback(): Void {
Scheduler.executeFrame(); Scheduler.executeFrame();
System.render([framebuffer]); System.render([framebuffer]);
} }
static function dropFilesCallback(filePath: String): Void { static function dropFilesCallback(filePath: String): Void {
System.dropFiles(filePath); System.dropFiles(filePath);
} }
static function copyCallback(): String { static function copyCallback(): String {
if (System.copyListener != null) { if (System.copyListener != null) {
return System.copyListener(); return System.copyListener();
} }
else { else {
return null; return null;
} }
} }
static function cutCallback(): String { static function cutCallback(): String {
if (System.cutListener != null) { if (System.cutListener != null) {
return System.cutListener(); return System.cutListener();
} }
else { else {
return null; return null;
} }
} }
static function pasteCallback(data: String): Void { static function pasteCallback(data: String): Void {
if (System.pasteListener != null) { if (System.pasteListener != null) {
System.pasteListener(data); System.pasteListener(data);
} }
} }
static function foregroundCallback(): Void { static function foregroundCallback(): Void {
System.foreground(); System.foreground();
} }
static function resumeCallback(): Void { static function resumeCallback(): Void {
System.resume(); System.resume();
} }
static function pauseCallback(): Void { static function pauseCallback(): Void {
System.pause(); System.pause();
} }
static function backgroundCallback(): Void { static function backgroundCallback(): Void {
System.background(); System.background();
} }
static function shutdownCallback(): Void { static function shutdownCallback(): Void {
System.shutdown(); System.shutdown();
} }
static function keyboardDownCallback(code: Int): Void { static function keyboardDownCallback(code: Int): Void {
keyboard.sendDownEvent(cast code); keyboard.sendDownEvent(cast code);
} }
static function keyboardUpCallback(code: Int): Void { static function keyboardUpCallback(code: Int): Void {
keyboard.sendUpEvent(cast code); keyboard.sendUpEvent(cast code);
} }
static function keyboardPressCallback(charCode: Int): Void { static function keyboardPressCallback(charCode: Int): Void {
keyboard.sendPressEvent(String.fromCharCode(charCode)); keyboard.sendPressEvent(String.fromCharCode(charCode));
} }
static function mouseDownCallback(button: Int, x: Int, y: Int): Void { static function mouseDownCallback(button: Int, x: Int, y: Int): Void {
mouse.sendDownEvent(0, button, x, y); mouse.sendDownEvent(0, button, x, y);
} }
static function mouseUpCallback(button: Int, x: Int, y: Int): Void { static function mouseUpCallback(button: Int, x: Int, y: Int): Void {
mouse.sendUpEvent(0, button, x, y); mouse.sendUpEvent(0, button, x, y);
} }
static function mouseMoveCallback(x: Int, y: Int, mx: Int, my: Int): Void { static function mouseMoveCallback(x: Int, y: Int, mx: Int, my: Int): Void {
mouse.sendMoveEvent(0, x, y, mx, my); mouse.sendMoveEvent(0, x, y, mx, my);
} }
static function mouseWheelCallback(delta: Int): Void { static function mouseWheelCallback(delta: Int): Void {
mouse.sendWheelEvent(0, delta); mouse.sendWheelEvent(0, delta);
} }
static function penDownCallback(x: Int, y: Int, pressure: Float): Void { static function penDownCallback(x: Int, y: Int, pressure: Float): Void {
pen.sendDownEvent(0, x, y, pressure); pen.sendDownEvent(0, x, y, pressure);
} }
static function penUpCallback(x: Int, y: Int, pressure: Float): Void { static function penUpCallback(x: Int, y: Int, pressure: Float): Void {
pen.sendUpEvent(0, x, y, pressure); pen.sendUpEvent(0, x, y, pressure);
} }
static function penMoveCallback(x: Int, y: Int, pressure: Float): Void { static function penMoveCallback(x: Int, y: Int, pressure: Float): Void {
pen.sendMoveEvent(0, x, y, pressure); pen.sendMoveEvent(0, x, y, pressure);
} }
static function gamepadAxisCallback(gamepad: Int, axis: Int, value: Float): Void { static function gamepadAxisCallback(gamepad: Int, axis: Int, value: Float): Void {
gamepads[gamepad].sendAxisEvent(axis, value); gamepads[gamepad].sendAxisEvent(axis, value);
} }
static function gamepadButtonCallback(gamepad: Int, button: Int, value: Float): Void { static function gamepadButtonCallback(gamepad: Int, button: Int, value: Float): Void {
gamepads[gamepad].sendButtonEvent(button, value); gamepads[gamepad].sendButtonEvent(button, value);
} }
static function audioCallback(samples: Int): Void { static function audioCallback(samples: Int): Void {
kha.audio2.Audio._callCallback(samples); kha.audio2.Audio._callCallback(samples);
var buffer = @:privateAccess kha.audio2.Audio.buffer; var buffer = @:privateAccess kha.audio2.Audio.buffer;
Krom.writeAudioBuffer(buffer.data.buffer, samples); Krom.writeAudioBuffer(buffer.data.buffer, samples);
} }
public static function init(options: SystemOptions, callback: Window->Void): Void { public static function init(options: SystemOptions, callback: Window->Void): Void {
Krom.init(options.title, options.width, options.height, options.framebuffer.samplesPerPixel, options.framebuffer.verticalSync, Krom.init(options.title, options.width, options.height, options.framebuffer.samplesPerPixel, options.framebuffer.verticalSync,
cast options.window.mode, options.window.windowFeatures, Krom.KROM_API); cast options.window.mode, options.window.windowFeatures, Krom.KROM_API);
start = Krom.getTime(); start = Krom.getTime();
haxe.Log.trace = function(v: Dynamic, ?infos: haxe.PosInfos) { haxe.Log.trace = function(v: Dynamic, ?infos: haxe.PosInfos) {
var message = haxe.Log.formatOutput(v, infos); var message = haxe.Log.formatOutput(v, infos);
Krom.log(message); Krom.log(message);
}; };
new Window(0); new Window(0);
Scheduler.init(); Scheduler.init();
Shaders.init(); Shaders.init();
var g4 = new kha.krom.Graphics(); var g4 = new kha.krom.Graphics();
framebuffer = new Framebuffer(0, null, null, g4); framebuffer = new Framebuffer(0, null, null, g4);
framebuffer.init(new kha.graphics2.Graphics1(framebuffer), new kha.graphics4.Graphics2(framebuffer), g4); framebuffer.init(new kha.graphics2.Graphics1(framebuffer), new kha.graphics4.Graphics2(framebuffer), g4);
Krom.setCallback(renderCallback); Krom.setCallback(renderCallback);
Krom.setDropFilesCallback(dropFilesCallback); Krom.setDropFilesCallback(dropFilesCallback);
Krom.setCutCopyPasteCallback(cutCallback, copyCallback, pasteCallback); Krom.setCutCopyPasteCallback(cutCallback, copyCallback, pasteCallback);
Krom.setApplicationStateCallback(foregroundCallback, resumeCallback, pauseCallback, backgroundCallback, shutdownCallback); Krom.setApplicationStateCallback(foregroundCallback, resumeCallback, pauseCallback, backgroundCallback, shutdownCallback);
keyboard = new Keyboard(); keyboard = new Keyboard();
mouse = new MouseImpl(); mouse = new MouseImpl();
pen = new Pen(); pen = new Pen();
gamepads = new Array<Gamepad>(); gamepads = new Array<Gamepad>();
for (i in 0...maxGamepads) { for (i in 0...maxGamepads) {
gamepads[i] = new Gamepad(i); gamepads[i] = new Gamepad(i);
} }
Krom.setKeyboardDownCallback(keyboardDownCallback); Krom.setKeyboardDownCallback(keyboardDownCallback);
Krom.setKeyboardUpCallback(keyboardUpCallback); Krom.setKeyboardUpCallback(keyboardUpCallback);
Krom.setKeyboardPressCallback(keyboardPressCallback); Krom.setKeyboardPressCallback(keyboardPressCallback);
Krom.setMouseDownCallback(mouseDownCallback); Krom.setMouseDownCallback(mouseDownCallback);
Krom.setMouseUpCallback(mouseUpCallback); Krom.setMouseUpCallback(mouseUpCallback);
Krom.setMouseMoveCallback(mouseMoveCallback); Krom.setMouseMoveCallback(mouseMoveCallback);
Krom.setMouseWheelCallback(mouseWheelCallback); Krom.setMouseWheelCallback(mouseWheelCallback);
Krom.setPenDownCallback(penDownCallback); Krom.setPenDownCallback(penDownCallback);
Krom.setPenUpCallback(penUpCallback); Krom.setPenUpCallback(penUpCallback);
Krom.setPenMoveCallback(penMoveCallback); Krom.setPenMoveCallback(penMoveCallback);
Krom.setGamepadAxisCallback(gamepadAxisCallback); Krom.setGamepadAxisCallback(gamepadAxisCallback);
Krom.setGamepadButtonCallback(gamepadButtonCallback); Krom.setGamepadButtonCallback(gamepadButtonCallback);
kha.audio2.Audio._init(); kha.audio2.Audio._init();
kha.audio1.Audio._init(); kha.audio1.Audio._init();
Krom.setAudioCallback(audioCallback); Krom.setAudioCallback(audioCallback);
Scheduler.start(); Scheduler.start();
callback(Window.get(0)); callback(Window.get(0));
} }
public static function initEx(title: String, options: Array<WindowOptions>, windowCallback: Int->Void, callback: Void->Void): Void {} public static function initEx(title: String, options: Array<WindowOptions>, windowCallback: Int->Void, callback: Void->Void): Void {}
static function translateWindowMode(value: Null<WindowMode>): Int { static function translateWindowMode(value: Null<WindowMode>): Int {
if (value == null) { if (value == null) {
return 0; return 0;
} }
return switch (value) { return switch (value) {
case Windowed: 0; case Windowed: 0;
case Fullscreen: 1; case Fullscreen: 1;
case ExclusiveFullscreen: 2; case ExclusiveFullscreen: 2;
} }
} }
public static function getScreenRotation(): ScreenRotation { public static function getScreenRotation(): ScreenRotation {
return ScreenRotation.RotationNone; return ScreenRotation.RotationNone;
} }
public static function getTime(): Float { public static function getTime(): Float {
return Krom.getTime() - start; return Krom.getTime() - start;
} }
public static function getVsync(): Bool { public static function getVsync(): Bool {
return true; return true;
} }
public static function getRefreshRate(): Int { public static function getRefreshRate(): Int {
return 60; return Krom.displayFrequency();
} }
public static function getSystemId(): String { public static function getSystemId(): String {
return Krom.systemId(); return Krom.systemId();
} }
public static function vibrate(ms: Int): Void { public static function vibrate(ms: Int): Void {
// TODO: Implement // TODO: Implement
} }
public static function getLanguage(): String { public static function getLanguage(): String {
return "en"; // TODO: Implement return "en"; // TODO: Implement
} }
public static function requestShutdown(): Bool { public static function requestShutdown(): Bool {
Krom.requestShutdown(); Krom.requestShutdown();
return true; return true;
} }
public static function getMouse(num: Int): Mouse { public static function getMouse(num: Int): Mouse {
return mouse; return mouse;
} }
public static function getPen(num: Int): Pen { public static function getPen(num: Int): Pen {
return pen; return pen;
} }
public static function getKeyboard(num: Int): Keyboard { public static function getKeyboard(num: Int): Keyboard {
return keyboard; return keyboard;
} }
public static function lockMouse(): Void { public static function lockMouse(): Void {
if (!isMouseLocked()) { if (!isMouseLocked()) {
Krom.lockMouse(); Krom.lockMouse();
for (listener in mouseLockListeners) { for (listener in mouseLockListeners) {
listener(); listener();
} }
} }
} }
public static function unlockMouse(): Void { public static function unlockMouse(): Void {
if (isMouseLocked()) { if (isMouseLocked()) {
Krom.unlockMouse(); Krom.unlockMouse();
for (listener in mouseLockListeners) { for (listener in mouseLockListeners) {
listener(); listener();
} }
} }
} }
public static function canLockMouse(): Bool { public static function canLockMouse(): Bool {
return Krom.canLockMouse(); return Krom.canLockMouse();
} }
public static function isMouseLocked(): Bool { public static function isMouseLocked(): Bool {
return Krom.isMouseLocked(); return Krom.isMouseLocked();
} }
public static function notifyOfMouseLockChange(func: Void->Void, error: Void->Void): Void { public static function notifyOfMouseLockChange(func: Void->Void, error: Void->Void): Void {
if (canLockMouse() && func != null) { if (canLockMouse() && func != null) {
mouseLockListeners.push(func); mouseLockListeners.push(func);
} }
} }
public static function removeFromMouseLockChange(func: Void->Void, error: Void->Void): Void { public static function removeFromMouseLockChange(func: Void->Void, error: Void->Void): Void {
if (canLockMouse() && func != null) { if (canLockMouse() && func != null) {
mouseLockListeners.remove(func); mouseLockListeners.remove(func);
} }
} }
public static function hideSystemCursor(): Void { public static function hideSystemCursor(): Void {
Krom.showMouse(false); Krom.showMouse(false);
} }
public static function showSystemCursor(): Void { public static function showSystemCursor(): Void {
Krom.showMouse(true); Krom.showMouse(true);
} }
static function unload(): Void {} static function unload(): Void {}
public static function canSwitchFullscreen(): Bool { public static function canSwitchFullscreen(): Bool {
return false; return false;
} }
public static function isFullscreen(): Bool { public static function isFullscreen(): Bool {
return false; return false;
} }
public static function requestFullscreen(): Void {} public static function requestFullscreen(): Void {}
public static function exitFullscreen(): Void {} public static function exitFullscreen(): Void {}
public static function notifyOfFullscreenChange(func: Void->Void, error: Void->Void): Void {} public static function notifyOfFullscreenChange(func: Void->Void, error: Void->Void): Void {}
public static function removeFromFullscreenChange(func: Void->Void, error: Void->Void): Void {} public static function removeFromFullscreenChange(func: Void->Void, error: Void->Void): Void {}
public static function changeResolution(width: Int, height: Int): Void {} public static function changeResolution(width: Int, height: Int): Void {}
public static function setKeepScreenOn(on: Bool): Void {} public static function setKeepScreenOn(on: Bool): Void {}
public static function loadUrl(url: String): Void {} public static function loadUrl(url: String): Void {}
public static function getGamepadId(index: Int): String { public static function getGamepadId(index: Int): String {
return "unknown"; return "unknown";
} }
public static function getGamepadVendor(index: Int): String { public static function getGamepadVendor(index: Int): String {
return "unknown"; return "unknown";
} }
public static function setGamepadRumble(index: Int, leftAmount: Float, rightAmount: Float): Void {} public static function setGamepadRumble(index: Int, leftAmount: Float, rightAmount: Float): Void {}
public static function safeZone(): Float { public static function safeZone(): Float {
return 1.0; return 1.0;
} }
public static function login(): Void {} public static function login(): Void {}
public static function automaticSafeZone(): Bool { public static function automaticSafeZone(): Bool {
return true; return true;
} }
public static function setSafeZone(value: Float): Void {} public static function setSafeZone(value: Float): Void {}
public static function unlockAchievement(id: Int): Void {} public static function unlockAchievement(id: Int): Void {}
public static function waitingForLogin(): Bool { public static function waitingForLogin(): Bool {
return false; return false;
} }
public static function disallowUserChange(): Void {} public static function disallowUserChange(): Void {}
public static function allowUserChange(): Void {} public static function allowUserChange(): Void {}
} }