322 lines
13 KiB
JavaScript
322 lines
13 KiB
JavaScript
class inputElement extends Element {
|
|
constructor(_Container, RestoreData = null,logicengine) {
|
|
super(_Container, RestoreData,logicengine,0);
|
|
this.Name = "InputElement";
|
|
this.Output = false;
|
|
this.Width = 100;
|
|
this.removeProperty("Inputs");
|
|
}
|
|
|
|
getOutput(Output=0) {
|
|
if (super.getOutput() === 0) return false;
|
|
return this.Output;
|
|
}
|
|
|
|
drawInputs(ctx, x, y, borderColor = "#000", circleColorFalse = "#ff0000", circleColorTrue = "#00ff00", circleColorHover = "#00ffff") {
|
|
// There are no inputs
|
|
}
|
|
|
|
drawElement(x, y, ctx) {
|
|
if (this.LogicEngine.ActiveContainer !== this._Container) return; // No point if we aren't visible
|
|
this.StaticCanvas.width = this.Width;
|
|
this.StaticCanvas.height = this.Height;
|
|
|
|
this.drawBorderBox(ctx, x,y,this.Width,this.Height);
|
|
this.drawTextCentered(ctx,x,y,this.Width,this.Height,"IN");
|
|
this.drawOutputs(ctx,x,y);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
class InputSwitch extends inputElement {
|
|
constructor(_Container, RestoreData = null, logicengine) {
|
|
super(_Container, RestoreData,logicengine);
|
|
this.Name = "Switch";
|
|
this.Height = 70;
|
|
|
|
if (RestoreData) this.Output = RestoreData.Output;
|
|
this.drawElement(0,0,this.StaticCtx);
|
|
}
|
|
|
|
MouseClick(mousePos) {
|
|
mousePos = this.LogicEngine.getCanvasMousePos(mousePos);
|
|
super.MouseClick(mousePos);
|
|
if ((mousePos.x >= (this.X + 5)) && (mousePos.x <= (this.X + 55)) && (mousePos.y >= (this.Y + 5)) && (mousePos.y <= (this.Y + 55))) {
|
|
this.Output = !this.Output;
|
|
this.drawElement(0,0,this.StaticCtx);
|
|
for (let a = 0; a < this.OutputConnections.length; a++) {
|
|
this.LogicEngine.RecursionCount = 0;
|
|
this.OutputConnections[a].Element.setInput(this.OutputConnections[a].Input, this.getOutput());
|
|
}
|
|
}
|
|
}
|
|
|
|
drawElement(x, y, ctx) {
|
|
if (this.LogicEngine.ActiveContainer !== this._Container) return; // No point if we aren't visible
|
|
this.StaticCanvas.width = this.Width;
|
|
this.StaticCanvas.height = this.Height;
|
|
|
|
this.drawBorderBox(ctx, x,y,this.Width-20,this.Height,1,"#000","#f7e979",this.LogicEngine.Settings.ShadowColor);
|
|
//this.drawBorderBox(ctx,x+5,y+5,50,50,1,"#ccc","#777");
|
|
this.drawBorderBox(ctx,x+5,y+25,50,10,1,"#ccc","#777");
|
|
this.drawTextCentered(ctx,x,y+(this.Height-14),this.Width-(this.outputCircleRadius*2),12,this.Designator,"12px Console","#000");
|
|
if (this.getOutput()) {
|
|
this.drawBorderBox(ctx,x+15,y+5,30,25,1,"#ccc","#777");
|
|
this.drawTextCentered(ctx,x,y+this.Height - 30,this.Width-(this.outputCircleRadius*2)-20,12,"OFF","12px Console","#000");
|
|
} else {
|
|
this.drawBorderBox(ctx,x+15,y+30,30,25,1,"#ccc","#777");
|
|
this.drawTextCentered(ctx,x,y+7,this.Width-(this.outputCircleRadius*2)-20,12,"ON","12px Console","#000");
|
|
}
|
|
this.drawOutputs(ctx,x,y);
|
|
}
|
|
}
|
|
let ElementCatalog_SWITCH = new ElementCatalog_Element("Switch","The switch allows for toggling an output low or high.","|-",InputSwitch,[]);
|
|
ElementReferenceTable.push(ElementCatalog_SWITCH);
|
|
ElementCategory_Inputs.addElement(ElementCatalog_SWITCH);
|
|
|
|
class InputButton extends inputElement {
|
|
constructor(_Container, RestoreData = null, logicengine) {
|
|
super(_Container, RestoreData,logicengine);
|
|
this.Name = "Button";
|
|
this.Height = 70;
|
|
this.drawElement(0,0,this.StaticCtx);
|
|
}
|
|
|
|
MouseDown(mousePos) {
|
|
mousePos = this.LogicEngine.getCanvasMousePos(mousePos);
|
|
if ((mousePos.x >= (this.X + 5)) && (mousePos.x <= (this.X + 55)) && (mousePos.y >= (this.Y + 5)) && (mousePos.y <= (this.Y + 55))) {
|
|
let old_output = this.Output;
|
|
this.Output = true;
|
|
this.LogicEngine.MouseDown = false; // Prevent movement on the button when its being pushed
|
|
this.drawElement(0,0,this.StaticCtx);
|
|
if (old_output != this.Output) {
|
|
for (let a = 0; a < this.OutputConnections.length; a++) {
|
|
this.LogicEngine.RecursionCount = 0;
|
|
this.OutputConnections[a].Element.setInput(this.OutputConnections[a].Input, this.getOutput());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
MouseUp(mousePos) {
|
|
let old_output = this.Output;
|
|
this.Output = false;
|
|
if (old_output != this.Output) {
|
|
this.drawElement(0,0,this.StaticCtx);
|
|
for (let a = 0; a < this.OutputConnections.length; a++) {
|
|
this.LogicEngine.RecursionCount = 0;
|
|
this.OutputConnections[a].Element.setInput(this.OutputConnections[a].Input, this.getOutput());
|
|
}
|
|
}
|
|
}
|
|
|
|
mouseInside(mousePos) {
|
|
let mousestat = super.mouseInside(mousePos);
|
|
if (this.Output && !this.MouseOver) {
|
|
this.Output = false;
|
|
this.drawElement(0,0,this.StaticCtx);
|
|
for (let a = 0; a < this.OutputConnections.length; a++) {
|
|
this.LogicEngine.RecursionCount = 0;
|
|
this.OutputConnections[a].Element.setInput(this.OutputConnections[a].Input, this.getOutput());
|
|
}
|
|
}
|
|
return mousestat;
|
|
}
|
|
|
|
drawElement(x, y, ctx) {
|
|
if (this.LogicEngine.ActiveContainer !== this._Container) return; // No point if we aren't visible
|
|
this.StaticCanvas.width = this.Width;
|
|
this.StaticCanvas.height = this.Height;
|
|
|
|
this.drawBorderBox(ctx, x,y,this.Width-20,this.Height,1,"#000","#f7e979",this.LogicEngine.Settings.ShadowColor);
|
|
this.drawBorderBox(ctx,x+5,y+5,50,50,1,"#ccc","#777");
|
|
this.drawTextCentered(ctx,x,y+(this.Height-14),this.Width-(this.outputCircleRadius*2),12,this.Designator,"12px Console","#000");
|
|
this.drawOutputs(ctx,x,y);
|
|
}
|
|
}
|
|
|
|
let ElementCatalog_BUTTON = new ElementCatalog_Element("Button","The button only outputs high when the button is pressed.","[o]",InputButton,[]);
|
|
ElementReferenceTable.push(ElementCatalog_BUTTON);
|
|
ElementCategory_Inputs.addElement(ElementCatalog_BUTTON);
|
|
|
|
class InputKeypad extends inputElement {
|
|
constructor(_Container, RestoreData = null, logicengine) {
|
|
super(_Container, RestoreData, logicengine);
|
|
this.Name = "Keypad";
|
|
this.Height = 210;
|
|
this.Width = 200;
|
|
|
|
this.Outputs = new Array(false,false,false,false,false,false,false,false);
|
|
this.OutputLabels = new Array("O0","O1","O2","O3","F1","F2","F3","Clr");
|
|
this.ButtonRows = 5;
|
|
this.ButtonColumns = 4;
|
|
this.Buttons = new Array(
|
|
{x: 0, y: 0,Value: 7, Text: "7"}, {x: 1, y: 0,Value: 8, Text: "8"}, {x: 2, y: 0,Value: 9, Text: "9"}, {x: 3, y: 0,Value: 16, Text: "F1"},
|
|
{x: 0, y: 1,Value: 4, Text: "4"}, {x: 1, y: 1,Value: 5, Text: "5"}, {x: 2, y: 1,Value: 6, Text: "6"}, {x: 3, y: 1,Value: 32, Text: "F2"},
|
|
{x: 0, y: 2,Value: 1, Text: "1"}, {x: 1, y: 2,Value: 2, Text: "2"}, {x: 2, y: 2,Value: 3, Text: "3"}, {x: 3, y: 2,Value: 64, Text: "F3"},
|
|
{x: 0, y: 3,Value: 10, Text: "A"}, {x: 1, y: 3,Value: 0, Text: "0"}, {x: 2, y: 3,Value: 11, Text: "B"}, {x: 3, y: 3,Value: 128, Text: "Clr"},
|
|
{x: 0, y: 1,Value: 12, Text: "C"}, {x: 1, y: 1,Value: 13, Text: "D"}, {x: 2, y: 1,Value: 14, Text: "E"}, {x: 3, y: 1,Value: 15, Text: "F"}
|
|
);
|
|
let typeProperty = new ElementProperty("Function Type","list",{CBObject: this,CBFunction: "setType"},true,true,[{Value: true, String: "Switch"},{Value: false, String: "Button"}],0,0);
|
|
this.Properties.push(typeProperty);
|
|
|
|
if (RestoreData) {
|
|
if (RestoreData.OutputValues) {
|
|
this.Outputs = RestoreData.OutputValues;
|
|
}
|
|
if (RestoreData.Properties?.length > 0) {
|
|
this.Properties[0].CurrentValue = RestoreData.Properties[0].CurrentValue;
|
|
}
|
|
}
|
|
this.drawElement(0,0,this.StaticCtx);
|
|
}
|
|
|
|
toJSON(key) {
|
|
let superjson = super.toJSON(key);
|
|
superjson.OutputValues = this.Outputs;
|
|
return superjson;
|
|
}
|
|
|
|
setType(type) {
|
|
if (type == "false") type = false;
|
|
if (type == "true") type = true;
|
|
this.Properties[0].CurrentValue = type;
|
|
if (!type) {
|
|
this.Outputs[4] = false;
|
|
this.Outputs[5] = false;
|
|
this.Outputs[6] = false;
|
|
this.drawElement(0,0,this.StaticCtx);
|
|
for (let a = 0; a < this.OutputConnections.length; a++) {
|
|
this.LogicEngine.RecursionCount = 0;
|
|
this.OutputConnections[a].Element.setInput(this.OutputConnections[a].Input, this.getOutput(this.OutputConnections[a].Output));
|
|
}
|
|
}
|
|
}
|
|
|
|
MouseDown(mousePos) {
|
|
if (!this.Properties[0].CurrentValue) {
|
|
mousePos = this.LogicEngine.getCanvasMousePos(mousePos);
|
|
let buttonWidth = 34;
|
|
let buttonHeight = 34;
|
|
let x = this.X;
|
|
let y = this.Y;
|
|
|
|
for (let pY = 0; pY < this.ButtonRows; pY++) {
|
|
for (let pX = 0; pX < this.ButtonColumns; pX++) {
|
|
|
|
if ((mousePos.x >= (x + (5 * (pX + 1)) + (buttonWidth * pX))) && (mousePos.x <= ((x + (5 * (pX + 1)) + (buttonWidth * pX)) + buttonWidth)) && (mousePos.y >= (y + (5 * (pY + 1)) + (buttonHeight * pY))) && (mousePos.y <= ((y + (5 * (pY + 1)) + (buttonHeight * pY)) + buttonHeight))) {
|
|
let button = this.Buttons[(pY * this.ButtonColumns) + pX];
|
|
if (button.Value & 0b0010000) this.Outputs[4] = true;
|
|
if (button.Value & 0b0100000) this.Outputs[5] = true;
|
|
if (button.Value & 0b1000000) this.Outputs[6] = true;
|
|
for (let a = 0; a < this.OutputConnections.length; a++) {
|
|
this.LogicEngine.RecursionCount = 0;
|
|
this.OutputConnections[a].Element.setInput(this.OutputConnections[a].Input, this.getOutput(this.OutputConnections[a].Output));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
this.drawElement(0,0,this.StaticCtx);
|
|
}
|
|
|
|
MouseUp(mousePos) {
|
|
if (!this.Properties[0].CurrentValue) {
|
|
this.Outputs[4] = false;
|
|
this.Outputs[5] = false;
|
|
this.Outputs[6] = false;
|
|
this.drawElement(0,0,this.StaticCtx);
|
|
}
|
|
}
|
|
|
|
MouseClick(mousePos) {
|
|
mousePos = this.LogicEngine.getCanvasMousePos(mousePos);
|
|
super.MouseClick(mousePos);
|
|
let buttonWidth = 34;
|
|
let buttonHeight = 34;
|
|
let x = this.X;
|
|
let y = this.Y;
|
|
|
|
for (let pY = 0; pY < this.ButtonRows; pY++) {
|
|
for (let pX = 0; pX < this.ButtonColumns; pX++) {
|
|
|
|
if ((mousePos.x >= (x + (5*(pX+1))+(buttonWidth*pX))) && (mousePos.x <= ((x + (5*(pX+1))+(buttonWidth*pX)) + buttonWidth)) && (mousePos.y >= (y + (5*(pY+1))+(buttonHeight*pY))) && (mousePos.y <= ((y + (5*(pY+1))+(buttonHeight*pY))+buttonHeight))) {
|
|
let button = this.Buttons[(pY*this.ButtonColumns) + pX];
|
|
|
|
if (button.Value <= 15) {
|
|
if (button.Value & 0b0000001) this.Outputs[0] = true; else this.Outputs[0] = false;
|
|
if (button.Value & 0b0000010) this.Outputs[1] = true; else this.Outputs[1] = false;
|
|
if (button.Value & 0b0000100) this.Outputs[2] = true; else this.Outputs[2] = false;
|
|
if (button.Value & 0b0001000) this.Outputs[3] = true; else this.Outputs[3] = false;
|
|
}
|
|
|
|
if (this.Properties[0].CurrentValue) {
|
|
if (button.Value & 0b0010000) this.Outputs[4] = !this.Outputs[4];
|
|
if (button.Value & 0b0100000) this.Outputs[5] = !this.Outputs[5];
|
|
if (button.Value & 0b1000000) this.Outputs[6] = !this.Outputs[6];
|
|
}
|
|
if (button.Value & 0b10000000) {
|
|
for (let a = 0; a < this.Outputs.length; a++) {
|
|
this.Outputs[a] = false;
|
|
}
|
|
this.Outputs[7] = true;
|
|
let tempTimer = new Task("keypadClearTask","Keypad Clear",0,100,this.ClockTick.bind(this),true);
|
|
tempTimer.LastCall = Date.now();
|
|
tempTimer.Enabled = true;
|
|
this.LogicEngine.Scheduler.addTask(tempTimer);
|
|
}
|
|
|
|
for (let a = 0; a < this.OutputConnections.length; a++) {
|
|
this.LogicEngine.RecursionCount = 0;
|
|
this.OutputConnections[a].Element.setInput(this.OutputConnections[a].Input, this.getOutput(this.OutputConnections[a].Output));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
this.drawElement(0,0,this.StaticCtx);
|
|
}
|
|
|
|
ClockTick() {
|
|
console.log("Keypad clear signal");
|
|
this.Outputs[7] = false;
|
|
this.drawElement(0,0,this.StaticCtx);
|
|
|
|
for (let a = 0; a < this.OutputConnections.length; a++) {
|
|
this.LogicEngine.RecursionCount = 0;
|
|
this.OutputConnections[a].Element.setInput(this.OutputConnections[a].Input, this.getOutput(this.OutputConnections[a].Output));
|
|
}
|
|
}
|
|
|
|
getOutput(Output = 0) {
|
|
if (super.getOutput() === 0) return false;
|
|
return (this.Outputs[Output]) ? true : false;
|
|
}
|
|
|
|
drawElement(x, y, ctx) {
|
|
if (this.LogicEngine.ActiveContainer !== this._Container) return; // No point if we aren't visible
|
|
let buttonWidth = 34;
|
|
let buttonHeight = 34;
|
|
|
|
this.StaticCanvas.width = this.Width;
|
|
this.StaticCanvas.height = this.Height;
|
|
|
|
this.drawBorderBox(ctx, x,y,this.Width-20,this.Height,1,"#000","#f7e979",this.LogicEngine.Settings.ShadowColor);
|
|
|
|
for (let pY = 0; pY < this.ButtonRows; pY++) {
|
|
for (let pX = 0; pX < this.ButtonColumns; pX++) {
|
|
this.drawBorderBox(ctx, x + (5*(pX+1))+(buttonWidth*pX), y + (5*(pY+1))+(buttonHeight*pY), buttonWidth, buttonHeight, 1, "#ccc", "#777");
|
|
this.drawTextCentered(ctx,x + (5*(pX+1))+(buttonWidth*pX), y + (5*(pY+1))+(buttonHeight*pY), buttonWidth, buttonHeight, this.Buttons[(pY*this.ButtonColumns) + pX].Text,"16px Console", "#fff");
|
|
|
|
}
|
|
}
|
|
|
|
this.drawTextCentered(ctx,x,y+(this.Height-14),this.Width-(this.outputCircleRadius*2),12,this.Designator,"12px Console","#000");
|
|
this.drawOutputs(ctx,x,y);
|
|
}
|
|
}
|
|
|
|
let ElementCatalog_Keypad = new ElementCatalog_Element("Keypad","A hex entry keypad with 4 bit output","[][]",InputKeypad,[]);
|
|
ElementReferenceTable.push(ElementCatalog_Keypad);
|
|
ElementCategory_Inputs.addElement(ElementCatalog_Keypad);
|