stash commit for Crafter.Web
This commit is contained in:
parent
2798e9567e
commit
219716aab9
7 changed files with 177 additions and 12 deletions
42
Crafter.Web-Canvas.cpp
Normal file
42
Crafter.Web-Canvas.cpp
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
/*
|
||||||
|
Crafter.Web
|
||||||
|
Copyright (C) 2024 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
|
||||||
|
*/
|
||||||
|
|
||||||
|
module;
|
||||||
|
#include <string>
|
||||||
|
#include <format>
|
||||||
|
#include <vector>
|
||||||
|
#include <iostream>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <cstdint>
|
||||||
|
module Crafter.Web;
|
||||||
|
using namespace Crafter::Web;
|
||||||
|
|
||||||
|
Canvas::Canvas(std::uint32_t width, std::uint32_t height, std::string value, std::unordered_map<std::string, std::string> style, std::vector<HtmlElement*> children , std::unordered_map<std::string, std::string> attributes) : HtmlElement("canvas", value, style, children, attributes), width(width), height(height) {
|
||||||
|
this->attributes.insert({"width",std::to_string(width)});
|
||||||
|
this->attributes.insert({"height",std::to_string(height)});
|
||||||
|
}
|
||||||
|
|
||||||
|
void Canvas::DrawRect(std::uint32_t x, std::uint32_t y, std::uint32_t width, std::uint32_t height, std::string color) {
|
||||||
|
Crafter::Web::Bindings::StrokeRect(id, x, y, width, height, color.c_str(), color.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
void Canvas::SetBuffer(Pixel* buffer) {
|
||||||
|
Crafter::Web::Bindings::PutImageData(id, buffer, width, height);
|
||||||
|
}
|
||||||
47
Crafter.Web-Canvas.cppm
Normal file
47
Crafter.Web-Canvas.cppm
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
/*
|
||||||
|
Crafter.Web
|
||||||
|
Copyright (C) 2024 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
|
||||||
|
*/
|
||||||
|
|
||||||
|
module;
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <cstdint>
|
||||||
|
export module Crafter.Web:Canvas;
|
||||||
|
import :HtmlElement;
|
||||||
|
|
||||||
|
export namespace Crafter::Web {
|
||||||
|
|
||||||
|
struct Pixel {
|
||||||
|
std::uint8_t r;
|
||||||
|
std::uint8_t g;
|
||||||
|
std::uint8_t b;
|
||||||
|
std::uint8_t a;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Canvas : public Crafter::Web::HtmlElement {
|
||||||
|
public:
|
||||||
|
std::uint32_t width;
|
||||||
|
std::uint32_t height;
|
||||||
|
Canvas(std::uint32_t width, std::uint32_t height, std::string value = "", std::unordered_map<std::string, std::string> style = {}, std::vector<HtmlElement*> children = {}, std::unordered_map<std::string, std::string> attributes = {});
|
||||||
|
void DrawRect(std::uint32_t x, std::uint32_t y, std::uint32_t width, std::uint32_t height, std::string color);
|
||||||
|
void SetBuffer(Pixel* buffer);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
Crafter.Build
|
Crafter.Web
|
||||||
Copyright (C) 2024 Catcrafts
|
Copyright (C) 2024 Catcrafts
|
||||||
Catcrafts.net
|
Catcrafts.net
|
||||||
|
|
||||||
|
|
@ -28,15 +28,15 @@ module;
|
||||||
module Crafter.Web;
|
module Crafter.Web;
|
||||||
using namespace Crafter::Web;
|
using namespace Crafter::Web;
|
||||||
|
|
||||||
HtmlElement::HtmlElement(std::string tag, std::string value, std::unordered_map<std::string, std::string> style, std::vector<HtmlElement> children, std::unordered_map<std::string, std::string> attributes) : id(idCounter++), tag(tag), children(children), style(style), value(value), attributes(attributes) {
|
HtmlElement::HtmlElement(std::string tag, std::string value, std::unordered_map<std::string, std::string> style, std::vector<HtmlElement*> children, std::unordered_map<std::string, std::string> attributes) : id(idCounter++), tag(tag), children(children), style(style), value(value), attributes(attributes) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::string HtmlElement::Render() const {
|
std::string HtmlElement::Render() const {
|
||||||
std::string childrenRendered;
|
std::string childrenRendered;
|
||||||
for(const HtmlElement& child : children){
|
for(HtmlElement* child : children){
|
||||||
childrenRendered+=child.Render();
|
childrenRendered+=child->Render();
|
||||||
}
|
}
|
||||||
std::string styleRender;
|
std::string styleRender;
|
||||||
for(const auto& [key, value] : style){
|
for(const auto& [key, value] : style){
|
||||||
|
|
@ -46,7 +46,7 @@ std::string HtmlElement::Render() const {
|
||||||
for(const auto& [key, value] : attributes){
|
for(const auto& [key, value] : attributes){
|
||||||
attributesRender+=std::format("{}=\"{}\"",key,value);
|
attributesRender+=std::format("{}=\"{}\"",key,value);
|
||||||
}
|
}
|
||||||
return std::format("<{} style=\"{}\" {}>{}{}</{}>",tag, styleRender, attributesRender, value, childrenRendered, tag);
|
return std::format("<{} id=\"{}\" style=\"{}\" {}>{}{}</{}>",tag, id, styleRender, attributesRender, value, childrenRendered, tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
void HtmlElement::RenderAsRoot() const {
|
void HtmlElement::RenderAsRoot() const {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
Crafter.Build
|
Crafter.Web
|
||||||
Copyright (C) 2024 Catcrafts
|
Copyright (C) 2024 Catcrafts
|
||||||
Catcrafts.net
|
Catcrafts.net
|
||||||
|
|
||||||
|
|
@ -33,9 +33,9 @@ export namespace Crafter::Web {
|
||||||
std::string tag;
|
std::string tag;
|
||||||
std::string value;
|
std::string value;
|
||||||
std::unordered_map<std::string, std::string> style;
|
std::unordered_map<std::string, std::string> style;
|
||||||
std::vector<HtmlElement> children;
|
std::vector<HtmlElement*> children;
|
||||||
std::unordered_map<std::string, std::string> attributes;
|
std::unordered_map<std::string, std::string> attributes;
|
||||||
HtmlElement(std::string tag, std::string value = "", std::unordered_map<std::string, std::string> style = {}, std::vector<HtmlElement> children = {}, std::unordered_map<std::string, std::string> attributes = {});
|
HtmlElement(std::string tag, std::string value = "", std::unordered_map<std::string, std::string> style = {}, std::vector<HtmlElement*> children = {}, std::unordered_map<std::string, std::string> attributes = {});
|
||||||
std::string Render() const;
|
std::string Render() const;
|
||||||
void RenderAsRoot() const;
|
void RenderAsRoot() const;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
Crafter.Build
|
Crafter.Web
|
||||||
Copyright (C) 2024 Catcrafts
|
Copyright (C) 2024 Catcrafts
|
||||||
Catcrafts.net
|
Catcrafts.net
|
||||||
|
|
||||||
|
|
@ -20,8 +20,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
|
||||||
module;
|
module;
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstdlib>
|
||||||
export module Crafter.Web;
|
export module Crafter.Web;
|
||||||
export import :HtmlElement;
|
export import :HtmlElement;
|
||||||
|
export import :Canvas;
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
void __cxa_allocate_exception() {
|
void __cxa_allocate_exception() {
|
||||||
|
|
@ -33,8 +36,15 @@ extern "C" {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export namespace Crafter::Web::Bindings {
|
export namespace Crafter::Web::Bindings {
|
||||||
__attribute__((import_module("env"), import_name("setInnerHTML"))) void SetInnerHTML(const char* id, std::size_t idLenght, const char* html, std::size_t htmlLenght);
|
__attribute__((import_module("env"), import_name("setInnerHTML"))) void SetInnerHTML(const char* id, std::size_t idLenght, const char* html, std::size_t htmlLenght);
|
||||||
|
__attribute__((import_module("env"), import_name("strokeRect"))) void StrokeRect(std::uint64_t id, std::uint32_t x, std::uint32_t y, std::uint32_t width, std::uint32_t height, const char* color, std::size_t colorLenght);
|
||||||
|
__attribute__((import_module("env"), import_name("putImageData"))) void PutImageData(std::uint64_t id, Crafter::Web::Pixel* buffer, std::uint32_t width, std::uint32_t height);
|
||||||
|
__attribute__((import_module("env"), import_name("fetch"))) void Fetch(void* obj, const char* url, std::size_t urlCallback, char* buffer, void (*listener)(void* obj, std::uint32_t, char*));
|
||||||
|
__attribute__((import_module("env"), import_name("setTimeout"))) void SetTimeout(void* obj, std::uint32_t time, void (*listener)(void* obj));
|
||||||
|
__attribute__((import_module("env"), import_name("draw"))) void Draw(std::uint64_t id, std::uint32_t x, std::uint32_t y, const char* text, std::uint32_t textLenght);
|
||||||
//__attribute__((import_module("env"), import_name("addEventListener"))) void AddEventListener(std::string id, std::string eventId, void (*listener)(void));
|
//__attribute__((import_module("env"), import_name("addEventListener"))) void AddEventListener(std::string id, std::string eventId, void (*listener)(void));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,15 +23,81 @@ function setInnerHTML(id, idLenght, html, htmlLenght) {
|
||||||
document.getElementById(decoder.decode(new Int8Array(window.crafter_webbuild_wasi.instance.exports.memory.buffer, id, idLenght))).innerHTML = decoder.decode(new Int8Array(window.crafter_webbuild_wasi.instance.exports.memory.buffer, html, htmlLenght));
|
document.getElementById(decoder.decode(new Int8Array(window.crafter_webbuild_wasi.instance.exports.memory.buffer, id, idLenght))).innerHTML = decoder.decode(new Int8Array(window.crafter_webbuild_wasi.instance.exports.memory.buffer, html, htmlLenght));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function strokeRect(id, x, y, width, height, color, colorLenght) {
|
||||||
|
const decoder = new TextDecoder();
|
||||||
|
const canvas = document.getElementById(id.toString());
|
||||||
|
const ctx = canvas.getContext("2d");
|
||||||
|
ctx.strokeStyle = decoder.decode(new Int8Array(window.crafter_webbuild_wasi.instance.exports.memory.buffer, color, colorLenght));
|
||||||
|
ctx.strokeRect(x, y, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
function putImageData(id, buffer, width, height) {
|
||||||
|
const decoder = new TextDecoder();
|
||||||
|
const canvas = document.getElementById(id.toString());
|
||||||
|
const ctx = canvas.getContext("2d");
|
||||||
|
const view = new Uint8ClampedArray(window.crafter_webbuild_wasi.instance.exports.memory.buffer, buffer, 640*360*4);
|
||||||
|
const imageData = new ImageData(view, 640, 360);
|
||||||
|
ctx.putImageData(imageData, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
function draw(id, x, y, text, textLenght){
|
||||||
|
const decoder = new TextDecoder();
|
||||||
|
const canvas = document.getElementById("1");
|
||||||
|
let ctx = canvas.getContext("2d")
|
||||||
|
|
||||||
|
ctx.fillStyle = "red";
|
||||||
|
ctx.font = '20px sans-serif';
|
||||||
|
|
||||||
|
var textString = decoder.decode(new Int8Array(window.crafter_webbuild_wasi.instance.exports.memory.buffer, text, textLenght))
|
||||||
|
let textWidth = ctx.measureText(textString).width;
|
||||||
|
|
||||||
|
ctx.fillText(textString , x - (textWidth / 2), y);
|
||||||
|
}
|
||||||
|
|
||||||
|
function setTimeoutt(obj, time, callback) {
|
||||||
|
setTimeout(() => {
|
||||||
|
window.crafter_webbuild_wasi.instance.exports.__indirect_function_table.get(callback)(obj);
|
||||||
|
}, time);
|
||||||
|
}
|
||||||
|
|
||||||
function addEventListener(id, eventId, listener) {
|
function addEventListener(id, eventId, listener) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function fetchh(obj, url, urlLenght, buffer, callback) {
|
||||||
|
const decoder = new TextDecoder();
|
||||||
|
const view = new Uint8Array(window.crafter_webbuild_wasi.instance.exports.memory.buffer, buffer);
|
||||||
|
fetch(decoder.decode(new Int8Array(window.crafter_webbuild_wasi.instance.exports.memory.buffer, url, urlLenght)), {
|
||||||
|
// Adding Get request
|
||||||
|
method: "GET",
|
||||||
|
|
||||||
|
// Setting headers
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/octet-stream',
|
||||||
|
},
|
||||||
|
// Setting response type to arraybuffer
|
||||||
|
responseType: "arraybuffer"
|
||||||
|
}).then(x => x.arrayBuffer()).then(
|
||||||
|
y => {
|
||||||
|
const outputBytes = new Uint8Array(y);
|
||||||
|
for(let i = 0; i < outputBytes.length; i++) {
|
||||||
|
view[i] = outputBytes[i]
|
||||||
|
}
|
||||||
|
window.crafter_webbuild_wasi.instance.exports.__indirect_function_table.get(callback)(obj, outputBytes.length, buffer);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
let env = {
|
let env = {
|
||||||
setInnerHTML:setInnerHTML,
|
setInnerHTML:setInnerHTML,
|
||||||
addEventListener:addEventListener
|
addEventListener:addEventListener,
|
||||||
|
strokeRect:strokeRect,
|
||||||
|
fetch:fetchh,
|
||||||
|
putImageData:putImageData,
|
||||||
|
setTimeout:setTimeoutt,
|
||||||
|
draw:draw
|
||||||
}
|
}
|
||||||
|
|
||||||
if(window.crafter_webbuild_env){
|
if(window.crafter_webbuild_env){
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,8 @@
|
||||||
{
|
{
|
||||||
"name": "base",
|
"name": "base",
|
||||||
"standard": "c++26",
|
"standard": "c++26",
|
||||||
"source_files": ["Crafter.Web-HtmlElement"],
|
"source_files": ["Crafter.Web-HtmlElement", "Crafter.Web-Canvas"],
|
||||||
"module_files": ["Crafter.Web-HtmlElement", "Crafter.Web"],
|
"module_files": ["Crafter.Web-Canvas", "Crafter.Web-HtmlElement", "Crafter.Web"],
|
||||||
"additional_files": ["Crafter.Web.js"],
|
"additional_files": ["Crafter.Web.js"],
|
||||||
"build_dir": "./build",
|
"build_dir": "./build",
|
||||||
"output_dir": "./bin",
|
"output_dir": "./bin",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue