Skip to content

Commit ebd86d9

Browse files
committed
Make getMouse() work with turtle graphics window
1 parent c45ab06 commit ebd86d9

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

include/actions.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -1664,8 +1664,17 @@ function builtin_getMouse() {
16641664
builtin_getMouse._apply_ = function (rte, cont, this_, params) {
16651665

16661666
var code = function (rte, cont) {
1667-
pos = pixels_window.pageToRelative(cb.mousePos);
1668-
return return_fn_body(rte, { x: pos.x, y: pos.y, down: cb.mouseDown });
1667+
var state;
1668+
if (showing_drawing_window()) {
1669+
var pos = drawing_window.pageToRelative(cb.mousePos);
1670+
state = { x: pos.x, y: pos.y, down: cb.mouseDown };
1671+
} else if (showing_pixels_window()) {
1672+
var pos = pixels_window.pageToRelative(cb.mousePos);
1673+
state = { x: pos.x, y: pos.y, down: cb.mouseDown };
1674+
} else {
1675+
state = { x: cb.mousePos.x, y: cb.mousePos.y, down: cb.mouseDown };
1676+
}
1677+
return return_fn_body(rte, state);
16691678
};
16701679

16711680
return exec_fn_body(code,

include/drawing.js

+14
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,20 @@ DrawingWindow.prototype.drawtext = function (text) {
316316
ctx.restore();
317317
};
318318

319+
DrawingWindow.prototype.pageToRelative = function (coord) {
320+
var rect = getCoords(this.drawing_canvas);
321+
var w = this.drawing_canvas.width;
322+
var h = this.drawing_canvas.height;
323+
var x = Math.max(0, Math.min(w, coord.x - rect.x - 3)) - w/2;
324+
var y = h/2 - Math.max(0, Math.min(h, coord.y - rect.y - 3));
325+
return { x: x, y: y };
326+
};
327+
328+
function getCoords(elem) {
329+
var rect = elem.getBoundingClientRect();
330+
return { x: rect.left + pageXOffset, y: rect.top + pageYOffset };
331+
}
332+
319333
var drawing_window;
320334

321335
function create_drawing_window(width, height) {

0 commit comments

Comments
 (0)