This commit is contained in:
Jorijn van der Graaf 2025-11-10 20:25:50 +01:00
commit 2254ad53c5
11 changed files with 300 additions and 466 deletions

View file

@ -1,381 +0,0 @@
/*
Crafter.CppDOM
Copyright (C) 2025 Catcrafts
Catcrafts.net
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
export module Crafter.CppDOM:Bindings;
import std;
import :EventTypes;
export namespace Crafter::CppDOMBindings {
int clickHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* clickHandlers = new std::unordered_map<int, std::function<void(void)>>();
int mouseOverHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* mouseOverHandlers = new std::unordered_map<int, std::function<void(void)>>();
int mouseOutHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* mouseOutHandlers = new std::unordered_map<int, std::function<void(void)>>();
int mouseMoveHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* mouseMoveHandlers = new std::unordered_map<int, std::function<void(void)>>();
int mouseDownHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* mouseDownHandlers = new std::unordered_map<int, std::function<void(void)>>();
int mouseUpHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* mouseUpHandlers = new std::unordered_map<int, std::function<void(void)>>();
int focusHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* focusHandlers = new std::unordered_map<int, std::function<void(void)>>();
int blurHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* blurHandlers = new std::unordered_map<int, std::function<void(void)>>();
int keyDownHandlerMaxId = 0;
std::unordered_map<int, std::function<void(Crafter::KeyboardEvent)>>* keyDownHandlers = new std::unordered_map<int, std::function<void(Crafter::KeyboardEvent)>>();
int keyUpHandlerMaxId = 0;
std::unordered_map<int, std::function<void(Crafter::KeyboardEvent)>>* keyUpHandlers = new std::unordered_map<int, std::function<void(Crafter::KeyboardEvent)>>();
int keyPressHandlerMaxId = 0;
std::unordered_map<int, std::function<void(Crafter::KeyboardEvent)>>* keyPressHandlers = new std::unordered_map<int, std::function<void(Crafter::KeyboardEvent)>>();
int changeHandlerMaxId = 0;
std::unordered_map<int, std::function<void(Crafter::ChangeEvent)>>* changeHandlers = new std::unordered_map<int, std::function<void(Crafter::ChangeEvent)>>();
int submitHandlerMaxId = 0;
std::unordered_map<int, std::function<void(void)>>* submitHandlers = new std::unordered_map<int, std::function<void(void)>>();
int inputHandlerMaxId = 0;
std::unordered_map<int, std::function<void(Crafter::InputEvent)>>* inputHandlers = new std::unordered_map<int, std::function<void(Crafter::InputEvent)>>();
int resizeHandlerMaxId = 0;
std::unordered_map<int, std::function<void(Crafter::ResizeEvent)>>* resizeHandlers = new std::unordered_map<int, std::function<void(Crafter::ResizeEvent)>>();
int scrollHandlerMaxId = 0;
std::unordered_map<int, std::function<void(Crafter::ScrollEvent)>>* scrollHandlers = new std::unordered_map<int, std::function<void(Crafter::ScrollEvent)>>();
int contextMenuHandlerMaxId = 0;
std::unordered_map<int, std::function<void(Crafter::MouseEvent)>>* contextMenuHandlers = new std::unordered_map<int, std::function<void(Crafter::MouseEvent)>>();
int dragStartHandlerMaxId = 0;
std::unordered_map<int, std::function<void(Crafter::MouseEvent)>>* dragStartHandlers = new std::unordered_map<int, std::function<void(Crafter::MouseEvent)>>();
int dragEndHandlerMaxId = 0;
std::unordered_map<int, std::function<void(Crafter::MouseEvent)>>* dragEndHandlers = new std::unordered_map<int, std::function<void(Crafter::MouseEvent)>>();
int dropHandlerMaxId = 0;
std::unordered_map<int, std::function<void(Crafter::MouseEvent)>>* dropHandlers = new std::unordered_map<int, std::function<void(Crafter::MouseEvent)>>();
int wheelHandlerMaxId = 0;
std::unordered_map<int, std::function<void(Crafter::WheelEvent)>>* wheelHandlers = new std::unordered_map<int, std::function<void(Crafter::WheelEvent)>>();
int dragOverHandlerMaxId = 0;
std::unordered_map<int, std::function<void(Crafter::MouseEvent)>>* dragOverHandlers = new std::unordered_map<int, std::function<void(Crafter::MouseEvent)>>();
int dragEnterHandlerMaxId = 0;
std::unordered_map<int, std::function<void(Crafter::MouseEvent)>>* dragEnterHandlers = new std::unordered_map<int, std::function<void(Crafter::MouseEvent)>>();
int dragLeaveHandlerMaxId = 0;
std::unordered_map<int, std::function<void(Crafter::MouseEvent)>>* dragLeaveHandlers = new std::unordered_map<int, std::function<void(Crafter::MouseEvent)>>();
__attribute__((import_module("env"), import_name("freeJs"))) void FreeJs(void* ptr);
__attribute__((import_module("env"), import_name("getElementById"))) void* GetElementById(const char* id, std::size_t idLenght);
void* GetElementById(const std::string_view id) {
return GetElementById(id.data(), id.size());
}
__attribute__((import_module("env"), import_name("setInnerHTML"))) void SetInnerHTML(void* ptr, const char* html, std::size_t htmlLenght);
void SetInnerHTML(void* ptr, const std::string_view html) {
SetInnerHTML(ptr, html.data(), html.size());
}
// Event handling functions
__attribute__((import_module("env"), import_name("addClickListener"))) void AddClickListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeClickListener"))) void RemoveClickListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addMouseOverListener"))) void AddMouseOverListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeMouseOverListener"))) void RemoveMouseOverListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addMouseOutListener"))) void AddMouseOutListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeMouseOutListener"))) void RemoveMouseOutListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addMouseMoveListener"))) void AddMouseMoveListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeMouseMoveListener"))) void RemoveMouseMoveListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addMouseDownListener"))) void AddMouseDownListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeMouseDownListener"))) void RemoveMouseDownListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addMouseUpListener"))) void AddMouseUpListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeMouseUpListener"))) void RemoveMouseUpListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addFocusListener"))) void AddFocusListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeFocusListener"))) void RemoveFocusListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addBlurListener"))) void AddBlurListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeBlurListener"))) void RemoveBlurListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addKeyDownListener"))) void AddKeyDownListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeKeyDownListener"))) void RemoveKeyDownListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addKeyUpListener"))) void AddKeyUpListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeKeyUpListener"))) void RemoveKeyUpListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addKeyPressListener"))) void AddKeyPressListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeKeyPressListener"))) void RemoveKeyPressListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addChangeListener"))) void AddChangeListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeChangeListener"))) void RemoveChangeListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addSubmitListener"))) void AddSubmitListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeSubmitListener"))) void RemoveSubmitListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addInputListener"))) void AddInputListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeInputListener"))) void RemoveInputListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addResizeListener"))) void AddResizeListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeResizeListener"))) void RemoveResizeListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addScrollListener"))) void AddScrollListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeScrollListener"))) void RemoveScrollListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addContextMenuListener"))) void AddContextMenuListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeContextMenuListener"))) void RemoveContextMenuListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addDragStartListener"))) void AddDragStartListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeDragStartListener"))) void RemoveDragStartListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addDragEndListener"))) void AddDragEndListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeDragEndListener"))) void RemoveDragEndListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addDropListener"))) void AddDropListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeDropListener"))) void RemoveDropListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addWheelListener"))) void AddWheelListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeWheelListener"))) void RemoveWheelListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addDragOverListener"))) void AddDragOverListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeDragOverListener"))) void RemoveDragOverListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addDragEnterListener"))) void AddDragEnterListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeDragEnterListener"))) void RemoveDragEnterListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("addDragLeaveListener"))) void AddDragLeaveListener(void* ptr, int id);
__attribute__((import_module("env"), import_name("removeDragLeaveListener"))) void RemoveDragLeaveListener(void* ptr, int id);
}
extern "C" {
__attribute__((export_name("WasmAlloc"))) void* WasmAlloc(std::int32_t size) {
std::cout << std::format("Alloc: {}", size) << std::endl;
return std::malloc(size);
}
__attribute__((export_name("WasmFree"))) void WasmFree(void* ptr) {
std::free(ptr);
}
__attribute__((export_name("ExecuteClickHandler"))) void ExecuteClickHandler(std::int32_t handlerID) {
Crafter::CppDOMBindings::clickHandlers->find(handlerID)->second();
}
__attribute__((export_name("ExecuteMouseOverHandler"))) void ExecuteMouseOverHandler(std::int32_t handlerID) {
Crafter::CppDOMBindings::mouseOverHandlers->find(handlerID)->second();
}
__attribute__((export_name("ExecuteMouseOutHandler"))) void ExecuteMouseOutHandler(std::int32_t handlerID) {
Crafter::CppDOMBindings::mouseOutHandlers->find(handlerID)->second();
}
__attribute__((export_name("ExecuteMouseMoveHandler"))) void ExecuteMouseMoveHandler(std::int32_t handlerID) {
Crafter::CppDOMBindings::mouseMoveHandlers->find(handlerID)->second();
}
__attribute__((export_name("ExecuteMouseDownHandler"))) void ExecuteMouseDownHandler(std::int32_t handlerID) {
Crafter::CppDOMBindings::mouseDownHandlers->find(handlerID)->second();
}
__attribute__((export_name("ExecuteMouseUpHandler"))) void ExecuteMouseUpHandler(std::int32_t handlerID) {
Crafter::CppDOMBindings::mouseUpHandlers->find(handlerID)->second();
}
__attribute__((export_name("ExecuteFocusHandler"))) void ExecuteFocusHandler(std::int32_t handlerID) {
Crafter::CppDOMBindings::focusHandlers->find(handlerID)->second();
}
__attribute__((export_name("ExecuteBlurHandler"))) void ExecuteBlurHandler(std::int32_t handlerID) {
Crafter::CppDOMBindings::blurHandlers->find(handlerID)->second();
}
__attribute__((export_name("ExecuteKeyDownHandler"))) void ExecuteKeyDownHandler(std::int32_t handlerID, const char* key, std::int32_t keyCode, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) {
Crafter::CppDOMBindings::keyDownHandlers->find(handlerID)->second(Crafter::KeyboardEvent(key, keyCode, altKey, ctrlKey, shiftKey, metaKey));
}
__attribute__((export_name("ExecuteKeyUpHandler"))) void ExecuteKeyUpHandler(std::int32_t handlerID, const char* key, std::int32_t keyCode, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) {
Crafter::CppDOMBindings::keyUpHandlers->find(handlerID)->second(Crafter::KeyboardEvent(key, keyCode, altKey, ctrlKey, shiftKey, metaKey));
}
__attribute__((export_name("ExecuteKeyPressHandler"))) void ExecuteKeyPressHandler(std::int32_t handlerID, const char* key, std::int32_t keyCode, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) {
Crafter::CppDOMBindings::keyPressHandlers->find(handlerID)->second(Crafter::KeyboardEvent(key, keyCode, altKey, ctrlKey, shiftKey, metaKey));
}
__attribute__((export_name("ExecuteChangeHandler"))) void ExecuteChangeHandler(std::int32_t handlerID, const char* value) {
Crafter::ChangeEvent event;
event.value = value;
Crafter::CppDOMBindings::changeHandlers->find(handlerID)->second(event);
}
__attribute__((export_name("ExecuteSubmitHandler"))) void ExecuteSubmitHandler(std::int32_t handlerID) {
Crafter::CppDOMBindings::submitHandlers->find(handlerID)->second();
}
__attribute__((export_name("ExecuteInputHandler"))) void ExecuteInputHandler(std::int32_t handlerID, const char* data, bool isComposing) {
Crafter::InputEvent event;
event.data = data;
event.isComposing = isComposing;
Crafter::CppDOMBindings::inputHandlers->find(handlerID)->second(event);
}
__attribute__((export_name("ExecuteResizeHandler"))) void ExecuteResizeHandler(std::int32_t handlerID, unsigned int width, unsigned int height) {
Crafter::ResizeEvent event;
event.width = width;
event.height = height;
Crafter::CppDOMBindings::resizeHandlers->find(handlerID)->second(event);
}
__attribute__((export_name("ExecuteScrollHandler"))) void ExecuteScrollHandler(std::int32_t handlerID, double scrollX, double scrollY) {
Crafter::ScrollEvent event;
event.scrollX = scrollX;
event.scrollY = scrollY;
Crafter::CppDOMBindings::scrollHandlers->find(handlerID)->second(event);
}
__attribute__((export_name("ExecuteContextMenuHandler"))) void ExecuteContextMenuHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, int button, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) {
Crafter::MouseEvent event;
event.clientX = clientX;
event.clientY = clientY;
event.screenX = screenX;
event.screenY = screenY;
event.button = button;
event.altKey = altKey;
event.ctrlKey = ctrlKey;
event.shiftKey = shiftKey;
event.metaKey = metaKey;
Crafter::CppDOMBindings::contextMenuHandlers->find(handlerID)->second(event);
}
__attribute__((export_name("ExecuteDragStartHandler"))) void ExecuteDragStartHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, int button, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) {
Crafter::MouseEvent event;
event.clientX = clientX;
event.clientY = clientY;
event.screenX = screenX;
event.screenY = screenY;
event.button = button;
event.altKey = altKey;
event.ctrlKey = ctrlKey;
event.shiftKey = shiftKey;
event.metaKey = metaKey;
Crafter::CppDOMBindings::dragStartHandlers->find(handlerID)->second(event);
}
__attribute__((export_name("ExecuteDragEndHandler"))) void ExecuteDragEndHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, int button, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) {
Crafter::MouseEvent event;
event.clientX = clientX;
event.clientY = clientY;
event.screenX = screenX;
event.screenY = screenY;
event.button = button;
event.altKey = altKey;
event.ctrlKey = ctrlKey;
event.shiftKey = shiftKey;
event.metaKey = metaKey;
Crafter::CppDOMBindings::dragEndHandlers->find(handlerID)->second(event);
}
__attribute__((export_name("ExecuteDropHandler"))) void ExecuteDropHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, int button, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) {
Crafter::MouseEvent event;
event.clientX = clientX;
event.clientY = clientY;
event.screenX = screenX;
event.screenY = screenY;
event.button = button;
event.altKey = altKey;
event.ctrlKey = ctrlKey;
event.shiftKey = shiftKey;
event.metaKey = metaKey;
Crafter::CppDOMBindings::dropHandlers->find(handlerID)->second(event);
}
__attribute__((export_name("ExecuteWheelHandler"))) void ExecuteWheelHandler(std::int32_t handlerID, double deltaX, double deltaY, double deltaZ, int deltaMode, double clientX, double clientY, double screenX, double screenY, int button, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) {
Crafter::WheelEvent event;
event.deltaX = deltaX;
event.deltaY = deltaY;
event.deltaZ = deltaZ;
event.clientX = clientX;
event.clientY = clientY;
event.screenX = screenX;
event.screenY = screenY;
event.button = button;
event.altKey = altKey;
event.ctrlKey = ctrlKey;
event.shiftKey = shiftKey;
event.metaKey = metaKey;
Crafter::CppDOMBindings::wheelHandlers->find(handlerID)->second(event);
}
__attribute__((export_name("ExecuteDragOverHandler"))) void ExecuteDragOverHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, int button, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) {
Crafter::MouseEvent event;
event.clientX = clientX;
event.clientY = clientY;
event.screenX = screenX;
event.screenY = screenY;
event.button = button;
event.altKey = altKey;
event.ctrlKey = ctrlKey;
event.shiftKey = shiftKey;
event.metaKey = metaKey;
Crafter::CppDOMBindings::dragOverHandlers->find(handlerID)->second(event);
}
__attribute__((export_name("ExecuteDragEnterHandler"))) void ExecuteDragEnterHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, int button, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) {
Crafter::MouseEvent event;
event.clientX = clientX;
event.clientY = clientY;
event.screenX = screenX;
event.screenY = screenY;
event.button = button;
event.altKey = altKey;
event.ctrlKey = ctrlKey;
event.shiftKey = shiftKey;
event.metaKey = metaKey;
Crafter::CppDOMBindings::dragEnterHandlers->find(handlerID)->second(event);
}
__attribute__((export_name("ExecuteDragLeaveHandler"))) void ExecuteDragLeaveHandler(std::int32_t handlerID, double clientX, double clientY, double screenX, double screenY, int button, bool altKey, bool ctrlKey, bool shiftKey, bool metaKey) {
Crafter::MouseEvent event;
event.clientX = clientX;
event.clientY = clientY;
event.screenX = screenX;
event.screenY = screenY;
event.button = button;
event.altKey = altKey;
event.ctrlKey = ctrlKey;
event.shiftKey = shiftKey;
event.metaKey = metaKey;
Crafter::CppDOMBindings::dragLeaveHandlers->find(handlerID)->second(event);
}
}