added selection feature

This commit is contained in:
Nikolai Fesenko
2025-11-26 05:27:59 +01:00
parent b435f46c8f
commit 8686f6da80
3 changed files with 63 additions and 2 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@@ -1,6 +1,24 @@
class Selectable {
is_selected = false;
select() {
this.is_selected = true;
}
unselect() {
this.is_selected = false;
}
draw_selection() {
}
}
class Screen { class Screen {
elements = []; elements = [];
selectable_objects = [];
current_selected_element;
ctx; ctx;
constructor(ctx) { constructor(ctx) {
@@ -38,6 +56,24 @@ class Screen {
requestAnimationFrame(this.onFrame); requestAnimationFrame(this.onFrame);
} }
select(obj) {
obj.select();
this.current_selected_element = obj;
}
unselect() {
this.current_selected_element.unselect();
this.current_selected_element = null;
}
makeSelectable(obj) {
var select_obj = new Selectable();
Object.assign(obj, select_obj);
obj.select = select_obj.select;
obj.unselect = select_obj.unselect;
obj.draw_selection = select_obj.draw_selection;
}
} }
class MainMenu extends Screen{ class MainMenu extends Screen{
@@ -48,6 +84,8 @@ class MainMenu extends Screen{
load() { load() {
requestAnimationFrame(this.onFrame); requestAnimationFrame(this.onFrame);
} }
@@ -77,14 +115,16 @@ class Shape {
rotation = 0; rotation = 0;
draw(ctx) { draw(ctx) {
console.log("No Draw Function Set");
} }
update() { update() {
console.log("No Update Function Set");
} }
translate(x, y) { translate(x, y) {
this.startX = x; this.startX = x;
this.startY = y; this.startY = y;
@@ -119,6 +159,18 @@ class Rectangle extends Shape{
ctx.translate(-(this.originX), -(this.originY)); ctx.translate(-(this.originX), -(this.originY));
ctx.fillStyle = this.color; ctx.fillStyle = this.color;
ctx.fillRect(this.startX,this.startY, this.width,this.height); ctx.fillRect(this.startX,this.startY, this.width,this.height);
if("is_selected" in this) {
if(this.is_selected) {
this.draw_selection(ctx);
}
}
}
drawStroke(ctx) {
ctx.strokeStyle = "blue";
ctx.lineWidth = 5;
ctx.strokeRect(this.startX, this.startY,this.width,this.height);
} }
setCenterOrigin() { setCenterOrigin() {

View File

@@ -27,8 +27,17 @@ rect.update = () => {
menu.addElement(rect); menu.addElement(rect);
menu.addElement( new Circle()); menu.addElement( new Circle());
menu.makeSelectable(rect);
rect.setCenterOrigin(); rect.setCenterOrigin();
rect.draw_selection = (ctx) => { rect.drawStroke(ctx)}
menu.select(rect);
menu.load(); menu.load();
setTimeout(() => {
menu.unselect();
}, 3000);