This commit is contained in:
Jorijn van der Graaf 2026-04-16 23:03:24 +02:00
commit c9ebd448f9
7 changed files with 278 additions and 73 deletions

View file

@ -272,49 +272,65 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
PostQuitMessage(0);
break;
}
case WM_KEYDOWN:{
if ((lParam & (1 << 30)) == 0) { // only first press
CrafterKeys crafterKey = vk_to_crafter_key(wParam);
if(window->heldkeys[static_cast<std::uint8_t>(crafterKey)]) {
window->onKeyHold[static_cast<std::uint8_t>(crafterKey)].Invoke();
window->onAnyKeyHold.Invoke(crafterKey);
} else{
window->heldkeys[static_cast<std::uint8_t>(crafterKey)] = true;
window->onKeyDown[static_cast<std::uint8_t>(crafterKey)].Invoke();
window->onAnyKeyDown.Invoke(crafterKey);
}
case WM_KEYDOWN:
case WM_SYSKEYDOWN: { // SYSKEYDOWN catches Alt combos, F10, etc.
CrafterKeys crafterKey = vk_to_crafter_key(wParam);
bool isRepeat = (lParam & (1 << 30)) != 0;
if (isRepeat) {
window->onKeyHold[(uint8_t)crafterKey].Invoke();
window->onAnyKeyHold.Invoke(crafterKey);
} else {
window->heldkeys[(uint8_t)crafterKey] = true;
window->onKeyDown[(uint8_t)crafterKey].Invoke();
window->onAnyKeyDown.Invoke(crafterKey);
}
break;
}
case WM_KEYUP: {
case WM_KEYUP:
case WM_SYSKEYUP: {
CrafterKeys crafterKey = vk_to_crafter_key(wParam);
window->heldkeys[static_cast<std::uint8_t>(crafterKey)] = false;
window->onKeyUp[static_cast<std::uint8_t>(crafterKey)].Invoke();
window->heldkeys[(uint8_t)crafterKey] = false;
window->onKeyUp[(uint8_t)crafterKey].Invoke();
window->onAnyKeyUp.Invoke(crafterKey);
break;
}
case WM_MOUSEMOVE: {
int x = LOWORD(lParam);
int y = HIWORD(lParam);
Vector<float, 2> pos(x, y);
window->currentMousePos = pos;
window->onMouseMove.Invoke();
for(MouseElement* element : window->mouseElements) {
if(element) {
if(window->currentMousePos.x >= element->scaled.position.x && window->currentMousePos.x <= element->scaled.position.x+element->scaled.size.x && window->currentMousePos.y > element->scaled.position.y && window->currentMousePos.y < element->scaled.position.y+element->scaled.size.y) {
element->onMouseMove.Invoke();
if(!element->mouseHover) {
element->mouseHover = true;
element->onMouseEnter.Invoke();
}
} else if(element->mouseHover) {
element->mouseHover = false;
element->onMouseLeave.Invoke();
}
}
}
window->mouseElements.erase(std::remove(window->mouseElements.begin(), window->mouseElements.end(), static_cast<MouseElement*>(nullptr)), window->mouseElements.end());
case WM_CHAR: {
// wParam is a UTF-16 code unit. May be a surrogate — buffer until we have a pair.
wchar_t wc = (wchar_t)wParam;
// Filter control characters (backspace=0x08, tab=0x09, enter=0x0D, escape=0x1B, etc.)
if (wc < 0x20 || wc == 0x7f) break;
// Handle UTF-16 surrogate pairs (characters outside the BMP, e.g. emoji).
static wchar_t highSurrogate = 0;
wchar_t utf16[2];
int utf16Len;
if (wc >= 0xD800 && wc <= 0xDBFF) {
// High surrogate — stash it and wait for the low surrogate.
highSurrogate = wc;
break;
} else if (wc >= 0xDC00 && wc <= 0xDFFF) {
// Low surrogate — pair with the stashed high surrogate.
if (highSurrogate == 0) break; // orphaned low surrogate, ignore
utf16[0] = highSurrogate;
utf16[1] = wc;
utf16Len = 2;
highSurrogate = 0;
} else {
utf16[0] = wc;
utf16Len = 1;
}
// Convert UTF-16 to UTF-8.
char utf8[8];
int n = WideCharToMultiByte(CP_UTF8, 0, utf16, utf16Len, utf8, sizeof(utf8), nullptr, nullptr);
if (n > 0) {
window->onTextInput.Invoke(std::string(utf8, n));
}
break;
}
case WM_LBUTTONDOWN: {