Improving mouse and tasks.

This commit is contained in:
Brad Nelson
2022-11-18 22:53:09 -08:00
parent 8ee1eafef6
commit e9181479fc
3 changed files with 62 additions and 15 deletions

View File

@ -111,13 +111,33 @@ if (!globalObj.write) {
context.screen.appendChild(context.canvas); context.screen.appendChild(context.canvas);
context.ctx = context.canvas.getContext('2d'); context.ctx = context.canvas.getContext('2d');
context.mouse_x = 0;
context.mouse_y = 0;
context.mouse_b = 0;
context.canvas.onpointermove = function(e) {
context.mouse_x = e.clientX;
context.mouse_y = e.clientY;
};
context.canvas.onpointerdown = function(e) {
context.mouse_x = e.clientX;
context.mouse_y = e.clientY;
context.mouse_b = 1;
e.target.setPointerCapture(e.pointerId);
};
context.canvas.onpointerup = function(e) {
context.mouse_x = e.clientX;
context.mouse_y = e.clientY;
context.mouse_b = 0;
e.target.releasePointerCapture(e.pointerId);
};
context.cursor = null; context.cursor = null;
context.cursor_time = new Date().getTime(); context.cursor_time = new Date().getTime();
setInterval(function() { setInterval(function() {
if (context.cursor) { if (context.cursor) {
var now = new Date().getTime(); var now = new Date().getTime();
var state = Math.floor((now - context.cursor_time) / 250) % 2; var state = Math.floor((now - context.cursor_time) / 250) % 2;
if (state) { if (state || context.min_text_portion === 0) {
context.cursor.style.visibility = 'hidden'; context.cursor.style.visibility = 'hidden';
} else { } else {
context.cursor.style.visibility = 'visible'; context.cursor.style.visibility = 'visible';
@ -319,6 +339,7 @@ if (!globalObj.write) {
cursor.style.backgroundColor = span.style.backgroundColor; cursor.style.backgroundColor = span.style.backgroundColor;
span.appendChild(cursor); span.appendChild(cursor);
context.cursor = cursor; context.cursor = cursor;
context.cursor.style.visibility = 'hidden';
} }
ntag.appendChild(span); ntag.appendChild(span);
if (i === parts.length - 1) { if (i === parts.length - 1) {
@ -552,8 +573,6 @@ JSWORD: web-type-raw { a n -- yld }
return newline ? -1 : 0; return newline ? -1 : 0;
} }
~ ~
: web-type ( a n -- ) web-type-raw if yield then ;
' web-type is type
JSWORD: web-key-raw { -- n } JSWORD: web-key-raw { -- n }
context.Update(); context.Update();
@ -570,8 +589,6 @@ JSWORD: web-key-raw { -- n }
return 0; return 0;
} }
~ ~
: web-key ( -- n ) begin yield web-key-raw dup if exit then drop again ;
' web-key is key
JSWORD: web-key?-raw { -- f } JSWORD: web-key?-raw { -- f }
context.Update(); context.Update();
@ -580,8 +597,6 @@ JSWORD: web-key?-raw { -- f }
} }
return context.inbuffer.length ? -1 : 0; return context.inbuffer.length ? -1 : 0;
~ ~
: web-key? ( -- f ) yield web-key?-raw ;
' web-key? is key?
JSWORD: terminate { retval } JSWORD: terminate { retval }
if (globalObj.quit) { if (globalObj.quit) {
@ -606,21 +621,28 @@ JSWORD: grmode { mode }
: gr 1 grmode ; : gr 1 grmode ;
: text 0 grmode ; : text 0 grmode ;
JSWORD: rawbox { x y w h c } JSWORD: fillcolor! { c }
function HexDig(n) { function HexDig(n) {
return ('0' + n.toString(16)).slice(-2); return ('0' + n.toString(16)).slice(-2);
} }
context.ctx.fillStyle = '#' + HexDig((c >> 16) & 0xff) + context.ctx.fillStyle = '#' + HexDig((c >> 16) & 0xff) +
HexDig((c >> 8) & 0xff) + HexDig((c >> 8) & 0xff) +
HexDig(c & 0xff); HexDig(c & 0xff);
~
JSWORD: rawbox { x y w h }
context.ctx.fillRect(x, y, w, h); context.ctx.fillRect(x, y, w, h);
~ ~
$ffffff value color $ffffff value color
: box ( x y w h -- ) color rawbox ; : box ( x y w h -- ) color fillcolor! rawbox ;
JSWORD: window { w h } JSWORD: window { w h }
if (context.canvas.width !== w ||
context.canvas.height) {
context.canvas.width = w; context.canvas.width = w;
context.canvas.height = h; context.canvas.height = h;
}
~ ~
JSWORD: viewport@ { -- w h } JSWORD: viewport@ { -- w h }
@ -814,6 +836,23 @@ JSWORD: log { a n -- }
console.log(GetString(a, n)); console.log(GetString(a, n));
~ ~
JSWORD: font { a n -- }
context.ctx.font = GetString(a, n);
~
JSWORD: rawFillText { a n x y -- }
context.ctx.fillText(GetString(a, n), x, y);
~
: fillText ( a n x y ) color fillcolor! rawFillText ;
JSWORD: mouse { -- x y }
return [context.mouse_x, context.mouse_y];
~
JSWORD: button { -- b }
return context.mouse_b;
~
forth definitions web forth definitions web
: bye 0 terminate ; : bye 0 terminate ;

View File

@ -14,6 +14,13 @@
web definitions web definitions
: web-type ( a n -- ) web-type-raw if pause then ;
' web-type is type
: web-key ( -- n ) begin pause web-key-raw dup if exit then drop again ;
' web-key is key
: web-key? ( -- f ) pause web-key?-raw ;
' web-key? is key?
: upload-file ( a n -- ) : upload-file ( a n -- )
upload-start upload-start
begin yield upload-done? until begin yield upload-done? until
@ -31,4 +38,8 @@ web definitions
: import s" _temp.fs" 2dup upload-file include-file ; : import s" _temp.fs" 2dup upload-file include-file ;
: yielding begin 50 ms yield again ;
' yielding 10 10 task yielding-task
yielding-task start-task
forth definitions forth definitions

View File

@ -593,10 +593,7 @@ var globalObj = getGlobalObj();
var module = VM(globalObj, ffi, heap); var module = VM(globalObj, ffi, heap);
function run() { function run() {
var lastTimeout = Date.now();
while (Date.now() - lastTimeout < 50) {
module.run(); module.run();
}
setTimeout(run, 0); setTimeout(run, 0);
} }
if (globalObj.write) { if (globalObj.write) {