2024-04-18 00:14:07 +02:00
|
|
|
//Make sure canvasses are in the correct position
|
|
|
|
var mouseIsDown=false;
|
|
|
|
var selectedMenuItemName="";
|
|
|
|
var selectedMenuYOffset=0;
|
|
|
|
|
2024-04-19 20:33:37 +02:00
|
|
|
const selectMenuNames = ["link", "router", "firewall", "hub", "switch",
|
|
|
|
"pc", "laptop", "server", "ip_phone", "printer",
|
|
|
|
"copier", "microwave", "fluorescent",
|
|
|
|
"wap", "wrepeater", "wbridge", "wrepeater"];
|
|
|
|
|
|
|
|
var cachedSelectMenuCanvas=null;
|
2024-04-18 00:14:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
var menuItemSize=50;
|
|
|
|
|
|
|
|
|
2024-04-19 20:33:37 +02:00
|
|
|
function InitializeSelectMenu() {
|
|
|
|
cachedSelectMenuCanvas = document.createElement('canvas');
|
|
|
|
cachedSelectMenuCanvas.width = menuItemSize;
|
|
|
|
console.log("length= " + selectMenuNames.length);
|
|
|
|
cachedSelectMenuCanvas.height = menuItemSize * selectMenuNames.length;
|
|
|
|
var cSMCctx = cachedSelectMenuCanvas.getContext('2d');
|
|
|
|
var starty = 0;
|
|
|
|
|
|
|
|
selectMenuNames.forEach((element) => {
|
|
|
|
//console.log("Printing to select: "+ element);
|
|
|
|
cSMCctx.drawImage(imageCollection[element], 0, starty, menuItemSize, menuItemSize);
|
|
|
|
starty += menuItemSize;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-04-18 00:14:07 +02:00
|
|
|
function drawSelectMenu()
|
|
|
|
{
|
2024-04-30 17:58:44 +02:00
|
|
|
//console.log("Printing on main canvass");
|
2024-04-18 00:14:07 +02:00
|
|
|
|
2024-04-30 17:58:44 +02:00
|
|
|
MainCanvas_ctx.fillStyle = maincanvasBackground;
|
|
|
|
MainCanvas_ctx.fillRect(0, 0, menuItemSize, MainCanvas.height);
|
2024-04-19 20:33:37 +02:00
|
|
|
if (selectedMenuYOffset < 0) selectedMenuYOffset = 0;
|
|
|
|
|
|
|
|
if (cachedSelectMenuCanvas == null) InitializeSelectMenu();//initialize it if not done yet
|
2024-04-30 17:58:44 +02:00
|
|
|
if(selectedMenuYOffset + MainCanvas.height > cachedSelectMenuCanvas.height) selectedMenuYOffset = cachedSelectMenuCanvas.height -MainCanvas.height;
|
|
|
|
MainCanvas_ctx.drawImage(cachedSelectMenuCanvas,0,selectedMenuYOffset,50,MainCanvas.height,0,0,50,MainCanvas.height);
|
|
|
|
let index = selectMenuNames.indexOf(selectedMenuItemName);
|
2024-04-18 00:14:07 +02:00
|
|
|
|
2024-04-30 17:58:44 +02:00
|
|
|
if(index > -1)
|
|
|
|
{
|
2024-04-18 00:14:07 +02:00
|
|
|
//We have a selected menu item
|
|
|
|
const cachedSelectMenuCanvasOverlay = document.createElement('canvas');
|
|
|
|
let boxwidth=3;
|
|
|
|
cachedSelectMenuCanvasOverlay.width = cachedSelectMenuCanvas.width;
|
|
|
|
cachedSelectMenuCanvasOverlay.height = cachedSelectMenuCanvas.height;
|
|
|
|
var overlay = cachedSelectMenuCanvasOverlay.getContext("2d");
|
|
|
|
let ty = index * menuItemSize;
|
|
|
|
overlay.beginPath();
|
|
|
|
overlay.lineWidth = boxwidth;
|
|
|
|
overlay.strokeStyle = "green";
|
|
|
|
overlay.rect(boxwidth,ty,menuItemSize-(boxwidth * 2),menuItemSize);
|
|
|
|
overlay.stroke();
|
|
|
|
MainCanvas_ctx.drawImage(cachedSelectMenuCanvasOverlay,0,selectedMenuYOffset,50,MainCanvas.height,0,0,50,MainCanvas.height);
|
|
|
|
//console.log("drawing select box around 0," + ty + " -> " + menuItemSize);
|
2024-04-30 17:58:44 +02:00
|
|
|
}
|
2024-04-18 00:14:07 +02:00
|
|
|
|
2024-04-30 17:58:44 +02:00
|
|
|
//Now, if we have more to the top or bottom, draw the arrows showing as much
|
|
|
|
if(selectedMenuYOffset > (menuItemSize / 2))
|
|
|
|
{
|
2024-04-18 00:14:07 +02:00
|
|
|
//We have one item above us. Draw the arrow
|
|
|
|
//console.log("Drawing up arrow");
|
|
|
|
MainCanvas_ctx.drawImage(imageFromName("ArrowUp"),0,0,50,25);
|
2024-04-30 17:58:44 +02:00
|
|
|
}
|
|
|
|
//Now, if we have more to the top or bottom, draw the arrows showing as much
|
|
|
|
if(selectedMenuYOffset + (MainCanvas.height + 25) < cachedSelectMenuCanvas.height)
|
|
|
|
{
|
2024-04-18 00:14:07 +02:00
|
|
|
//We have one item below us. Draw the arrow
|
|
|
|
//console.log("Drawing down arrow");
|
|
|
|
MainCanvas_ctx.drawImage(imageFromName("ArrowDown"),0,MainCanvas.height - 25,50,25);
|
2024-04-30 17:58:44 +02:00
|
|
|
}
|
2024-04-18 00:14:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function SelectMenu_handleMouseDown(evt)
|
|
|
|
{
|
2024-04-30 17:58:44 +02:00
|
|
|
mouseDownLocation = copyLocation(evt);
|
|
|
|
mouseIsDown=true;
|
2024-04-18 00:14:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function SelectMenu_handleMouseUp(evt)
|
|
|
|
{
|
2024-04-30 17:58:44 +02:00
|
|
|
mouseIsDown=false;
|
|
|
|
//console.log("mouseup");
|
|
|
|
//See if we have a mouse click
|
|
|
|
let deltaX = Math.abs(evt.pageX - mouseDownLocation.pageX);
|
|
|
|
let deltaY = Math.abs(evt.pageY - mouseDownLocation.pageY);
|
|
|
|
//console.log("delta " + deltaX + "," + deltaY);
|
|
|
|
if(deltaY < 3 && deltaX <3)
|
|
|
|
{
|
2024-04-18 00:14:07 +02:00
|
|
|
//We did not move much. Assume click
|
|
|
|
if(evt.pageX <= menuItemSize && !mouseDidMovement)
|
|
|
|
{
|
2024-04-30 17:58:44 +02:00
|
|
|
//We are in the item select menu.
|
|
|
|
let actualMenuLocation = selectedMenuYOffset + evt.pageY;
|
|
|
|
let newitemindex = Math.floor( actualMenuLocation / menuItemSize);
|
|
|
|
console.log("clicked on item " + selectMenuNames[newitemindex]);
|
|
|
|
selectedMenuItemName = selectMenuNames[newitemindex];
|
|
|
|
drawSelectMenu();
|
|
|
|
}
|
2024-04-18 00:14:07 +02:00
|
|
|
}
|
2024-04-30 17:58:44 +02:00
|
|
|
mouseDidMovement=false; //reset it after we raise the button
|
2024-04-18 00:14:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function SelectMenu_handleMouseMove(evt)
|
|
|
|
{
|
2024-04-30 17:58:44 +02:00
|
|
|
if(mouseIsDown)
|
|
|
|
{
|
2024-04-18 00:14:07 +02:00
|
|
|
let deltaX = evt.pageX - mouseDownLocation.pageX;
|
|
|
|
let deltaY = evt.pageY - mouseDownLocation.pageY;
|
|
|
|
if(isNaN(deltaY)) deltaY=0;
|
|
|
|
if(isNaN(deltaX)) deltaX=0;
|
|
|
|
//we are dragging
|
|
|
|
//console.log('mousemove ' + evt.pageX + " " + evt.pageY + " delta " + deltaY );
|
|
|
|
selectedMenuYOffset-=deltaY;
|
|
|
|
mouseDownLocation = copyLocation(evt); //reset the pointer
|
|
|
|
drawSelectMenu();
|
|
|
|
mouseDidMovement=true;
|
2024-04-30 17:58:44 +02:00
|
|
|
}
|
2024-04-18 00:14:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function copyLocation({ pageX, pageY }) {
|
|
|
|
return { pageX, pageY };
|
|
|
|
}
|