Skip to content

Commit 3500496

Browse files
committed
Implement size status indicator for all tools
1 parent 3e3fd00 commit 3500496

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/globals.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,8 @@ interface Tool {
739739
reset?(): void,
740740
/** Used by Polygon tool */
741741
complete?(ctx: CanvasRenderingContext2D): void,
742+
/** Used by Polygon tool */
743+
updateStatus?(): void,
742744
/** Used by Magnifier tool */
743745
getProspectiveMagnification?(): number,
744746

src/tools.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @ts-check
22
/* global selection:writable, stroke_size:writable, textbox:writable */
3-
/* global $canvas, $canvas_area, airbrush_size, brush_shape, brush_size, button, canvas_handles, ctrl, eraser_size, fill_color, fill_color_k, get_language, localize, magnification, main_canvas, main_ctx, pencil_size, pointer, pointer_active, pointer_over_canvas, pointer_previous, pointer_start, return_to_magnification, selected_colors, shift, stroke_color, transparency */
3+
/* global $canvas, $canvas_area, $status_size, airbrush_size, brush_shape, brush_size, button, canvas_handles, ctrl, eraser_size, fill_color, fill_color_k, get_language, localize, magnification, main_canvas, main_ctx, pencil_size, pointer, pointer_active, pointer_over_canvas, pointer_previous, pointer_start, return_to_magnification, selected_colors, shift, stroke_color, transparency */
44
import { OnCanvasSelection } from "./OnCanvasSelection.js";
55
import { OnCanvasTextBox } from "./OnCanvasTextBox.js";
66
// import { get_language, localize } from "./app-localization.js";
@@ -85,6 +85,10 @@ const tools = [{
8585
bresenham_line(pointer_previous.x, pointer_previous.y, pointer.x, pointer.y, (x, y) => {
8686
this.ffs_paint_iteration(x, y);
8787
});
88+
89+
// Note: MS Paint in Windows 98 shows the difference between the starting point and the current mouse position
90+
// An absolute bounding box seems more useful though.
91+
$status_size.text(`${this.x_max - this.x_min}x${this.y_max - this.y_min}`);
8892
},
8993
ffs_paint_iteration(x, y) {
9094
// Constrain the inversion paint brush position to the canvas
@@ -115,6 +119,7 @@ const tools = [{
115119
ctx_dest.putImageData(id_dest, rect_x, rect_y);
116120
},
117121
pointerup() {
122+
$status_size.text("");
118123
this.preview_canvas.width = 1;
119124
this.preview_canvas.height = 1;
120125

@@ -745,6 +750,7 @@ const tools = [{
745750
ctx.drawImage(this.preview_canvas, 0, 0);
746751
});
747752
this.points = [];
753+
$status_size.text("");
748754
}
749755
},
750756
pointerdown(_ctx, x, y) {
@@ -804,6 +810,14 @@ const tools = [{
804810
stroke_size
805811
);
806812
}
813+
814+
// MS Paint shows the mouse position relative to the first point
815+
// (and is afraid of the number zero)
816+
const signed_width = x - this.points[0].x || 1;
817+
const signed_height = y - this.points[0].y || 1;
818+
$status_size.text(`${signed_width}x${signed_height}`);
819+
// I don't know how helpful this is, might be more useful to show the number of points:
820+
// $status_size.text(`${this.points.length} / 4 points`);
807821
},
808822
drawPreviewUnderGrid(ctx, _x, _y, _grid_visible, scale, translate_x, translate_y) {
809823
// if (!pointer_active && !pointer_over_canvas) { return; }
@@ -817,10 +831,12 @@ const tools = [{
817831
},
818832
cancel() {
819833
this.points = [];
834+
$status_size.text("");
820835
},
821836
end() {
822837
this.points = [];
823838
update_helper_layer();
839+
$status_size.text("");
824840
},
825841
$options: $choose_stroke_size
826842
}, {
@@ -915,6 +931,8 @@ const tools = [{
915931
}
916932

917933
this.last_click_pointerup = { x, y, time: +(new Date) };
934+
935+
this.updateStatus();
918936
},
919937
pointerdown(ctx, x, y) {
920938
if (this.points.length < 1) {
@@ -977,6 +995,8 @@ const tools = [{
977995
stroke_size
978996
);
979997
}
998+
999+
this.updateStatus();
9801000
},
9811001
drawPreviewUnderGrid(ctx, _x, _y, _grid_visible, scale, translate_x, translate_y) {
9821002
// if (!pointer_active && !pointer_over_canvas) { return; }
@@ -1022,7 +1042,23 @@ const tools = [{
10221042
this.complete(ctx);
10231043
update_helper_layer();
10241044
},
1045+
updateStatus() {
1046+
let x_min = +Infinity;
1047+
let x_max = -Infinity;
1048+
let y_min = +Infinity;
1049+
let y_max = -Infinity;
1050+
for (const point of this.points) {
1051+
x_min = Math.min(point.x, x_min);
1052+
x_max = Math.max(point.x, x_max);
1053+
y_min = Math.min(point.y, y_min);
1054+
y_max = Math.max(point.y, y_max);
1055+
}
1056+
const signed_width = x_max - x_min || 1;
1057+
const signed_height = y_max - y_min || 1;
1058+
$status_size.text(`${signed_width}x${signed_height}`);
1059+
},
10251060
reset() {
1061+
$status_size.text("");
10261062
this.points = [];
10271063
this.last_click_pointerdown = { x: -Infinity, y: -Infinity, time: -Infinity };
10281064
this.last_click_pointerup = { x: -Infinity, y: -Infinity, time: -Infinity };
@@ -1170,8 +1206,10 @@ tools.forEach((tool) => {
11701206
rect_y = ~~Math.max(0, Math.min(drag_start_y, pointer.y));
11711207
rect_width = (~~Math.min(main_canvas.width, Math.max(drag_start_x, pointer.x) + 1)) - rect_x;
11721208
rect_height = (~~Math.min(main_canvas.height, Math.max(drag_start_y, pointer.y + 1))) - rect_y;
1209+
$status_size.text(`${rect_width}x${rect_height}`); // note that OnCanvasObject/OnCanvasTextBox/OnCanvasSelection also manages this status text
11731210
};
11741211
tool.pointerup = () => {
1212+
$status_size.text(""); // note that OnCanvasObject/OnCanvasTextBox/OnCanvasSelection also manages this status text
11751213
canvas_handles.show();
11761214
tool.selectBox(rect_x, rect_y, rect_width, rect_height);
11771215
};
@@ -1206,8 +1244,12 @@ tools.forEach((tool) => {
12061244
tool.shape_canvas.ctx.strokeStyle = main_ctx.strokeStyle;
12071245
tool.shape_canvas.ctx.lineWidth = main_ctx.lineWidth;
12081246
tool.shape(tool.shape_canvas.ctx, pointer_start.x, pointer_start.y, pointer.x - pointer_start.x, pointer.y - pointer_start.y);
1247+
const signed_width = pointer.x - pointer_start.x || 1;
1248+
const signed_height = pointer.y - pointer_start.y || 1;
1249+
$status_size.text(`${signed_width}x${signed_height}`);
12091250
};
12101251
tool.pointerup = () => {
1252+
$status_size.text(""); // also handles canceling with two mouse buttons or escape key
12111253
if (!tool.shape_canvas) { return; }
12121254
undoable({
12131255
name: tool.name,

0 commit comments

Comments
 (0)