Skip to content

Commit

Permalink
put the listeners in a more sane place... again.
Browse files Browse the repository at this point in the history
  • Loading branch information
hellopir2 authored Sep 20, 2024
1 parent d34a233 commit bbce591
Showing 1 changed file with 159 additions and 157 deletions.
316 changes: 159 additions & 157 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,165 @@ function init() {
updateStats();

loadTheMod();

// modified from orteils code.
document.getElementById('import').addEventListener('keydown',function(e){
if (e.keyCode!=13)
{
e.stopPropagation();
}
else importButton.click();
},true);

document.getElementById('xDim').addEventListener('keydown',function(e){
if (e.keyCode!=13)
{
e.stopPropagation();
}
else {
yDim.focus();
yDim.click();
}
},true);

document.getElementById('yDim').addEventListener('keydown',function(e){
if (e.keyCode!=13)
{
e.stopPropagation();
}
else document.getElementById('resize').click();
},true);

var keys=[];
window.addEventListener('keyup',function(e){
keys[e.keyCode]=0;
});

window.addEventListener('keydown',function(e){
if (e.keyCode==9)
{
var next;
//tab to shift through ages
if (e.shiftKey) next = (ageSelected + 4) % 5;
else next = (ageSelected + 1) % 5;
eval(`stage${next}.click()`); // i'm lazy ok
e.preventDefault();
}
else if (e.keyCode==37)
{
var next;
if (!e.shiftKey) {
if (selected != null) next = selected - (selected % 7) + (((selected % 7) + 6) % 7);
else next = 0;
if (next == 34) next = 33;
plants[selected].deselect();
selected = next;
plants[selected].select();
updatestages(selected);
e.preventDefault();
}
else {
next = (ageSelected + 4) % 5;
eval(`stage${next}.click()`); // i'm lazy ok
}
}
else if (e.keyCode==39)
{
var next;
if (!e.shiftKey) {
if (selected != null) next = selected - (selected % 7) + (((selected % 7) + 1) % 7);
else next = 0;
if (next == 34) next = 28;
plants[selected].deselect();
selected = next;
plants[selected].select();
updatestages(selected);
e.preventDefault();
}
else {
next = (ageSelected + 1) % 5;
eval(`stage${next}.click()`); // i'm lazy ok
}
}
else if (e.keyCode==38)
{
var next;
if (selected != null) next = (selected % 7) + (((Math.floor(selected/7) % 5) + 4) % 5) * 7;
else next = 0;
if (next == 34) next = 27;
plants[selected].deselect();
selected = next;
plants[selected].select();
updatestages(selected);
e.preventDefault();
}
else if (e.keyCode==40)
{
var next;
if (selected != null) next = (selected % 7) + (((Math.floor(selected/7) % 5) + 1) % 5) * 7;
else next = 0;
if (next == 34) next = 6;
plants[selected].deselect();
selected = next;
plants[selected].select();
updatestages(selected);
e.preventDefault();
}
else if (e.keyCode==65)
{
var next;
if (selected != null) next = selected - (selected % 7) + (((selected % 7) + 6) % 7);
else next = 0;
if (next == 34) next = 33;
plants[selected].deselect();
selected = next;
plants[selected].select();
updatestages(selected);
e.preventDefault();
}
else if (e.keyCode==68)
{
var next;
if (selected != null) next = selected - (selected % 7) + (((selected % 7) + 1) % 7);
else next = 0;
if (next == 34) next = 28;
plants[selected].deselect();
selected = next;
plants[selected].select();
updatestages(selected);
e.preventDefault();
}
else if (e.keyCode==87)
{
var next;
if (selected != null) next = (selected % 7) + (((Math.floor(selected/7) % 5) + 4) % 5) * 7;
else next = 0;
if (next == 34) next = 27;
plants[selected].deselect();
selected = next;
plants[selected].select();
updatestages(selected);
e.preventDefault();
}
else if (e.keyCode==83)
{
var next;
if (selected != null) next = (selected % 7) + (((Math.floor(selected/7) % 5) + 1) % 5) * 7;
else next = 0;
if (next == 34) next = 6;
plants[selected].deselect();
selected = next;
plants[selected].select();
updatestages(selected);
e.preventDefault();
}
keys[e.keyCode]=1;
});

window.addEventListener('visibilitychange',function(e){
keys=[];//reset all key pressed on visibility change (should help prevent ctrl still being down after ctrl-tab)
});

}

//mod stuff below
Expand Down Expand Up @@ -868,160 +1027,3 @@ function loadTheMod() {
}

window.addEventListener("DOMContentLoaded", init);
// modified from orteils code.
document.getElementById('import').addEventListener('keydown',function(e){
if (e.keyCode!=13)
{
e.stopPropagation();
}
else importButton.click();
},true);

document.getElementById('xDim').addEventListener('keydown',function(e){
if (e.keyCode!=13)
{
e.stopPropagation();
}
else {
yDim.focus();
yDim.click();
}
},true);

document.getElementById('yDim').addEventListener('keydown',function(e){
if (e.keyCode!=13)
{
e.stopPropagation();
}
else document.getElementById('resize').click();
},true);

var keys=[];
window.addEventListener('keyup',function(e){
keys[e.keyCode]=0;
});

window.addEventListener('keydown',function(e){
if (e.keyCode==9)
{
var next;
//tab to shift through ages
if (e.shiftKey) next = (ageSelected + 4) % 5;
else next = (ageSelected + 1) % 5;
eval(`stage${next}.click()`); // i'm lazy ok
e.preventDefault();
}
else if (e.keyCode==37)
{
var next;
if (!e.shiftKey) {
if (selected != null) next = selected - (selected % 7) + (((selected % 7) + 6) % 7);
else next = 0;
if (next == 34) next = 33;
plants[selected].deselect();
selected = next;
plants[selected].select();
updatestages(selected);
e.preventDefault();
}
else {
next = (ageSelected + 4) % 5;
eval(`stage${next}.click()`); // i'm lazy ok
}
}
else if (e.keyCode==39)
{
var next;
if (!e.shiftKey) {
if (selected != null) next = selected - (selected % 7) + (((selected % 7) + 1) % 7);
else next = 0;
if (next == 34) next = 28;
plants[selected].deselect();
selected = next;
plants[selected].select();
updatestages(selected);
e.preventDefault();
}
else {
next = (ageSelected + 1) % 5;
eval(`stage${next}.click()`); // i'm lazy ok
}
}
else if (e.keyCode==38)
{
var next;
if (selected != null) next = (selected % 7) + (((Math.floor(selected/7) % 5) + 4) % 5) * 7;
else next = 0;
if (next == 34) next = 27;
plants[selected].deselect();
selected = next;
plants[selected].select();
updatestages(selected);
e.preventDefault();
}
else if (e.keyCode==40)
{
var next;
if (selected != null) next = (selected % 7) + (((Math.floor(selected/7) % 5) + 1) % 5) * 7;
else next = 0;
if (next == 34) next = 6;
plants[selected].deselect();
selected = next;
plants[selected].select();
updatestages(selected);
e.preventDefault();
}
else if (e.keyCode==65)
{
var next;
if (selected != null) next = selected - (selected % 7) + (((selected % 7) + 6) % 7);
else next = 0;
if (next == 34) next = 33;
plants[selected].deselect();
selected = next;
plants[selected].select();
updatestages(selected);
e.preventDefault();
}
else if (e.keyCode==68)
{
var next;
if (selected != null) next = selected - (selected % 7) + (((selected % 7) + 1) % 7);
else next = 0;
if (next == 34) next = 28;
plants[selected].deselect();
selected = next;
plants[selected].select();
updatestages(selected);
e.preventDefault();
}
else if (e.keyCode==87)
{
var next;
if (selected != null) next = (selected % 7) + (((Math.floor(selected/7) % 5) + 4) % 5) * 7;
else next = 0;
if (next == 34) next = 27;
plants[selected].deselect();
selected = next;
plants[selected].select();
updatestages(selected);
e.preventDefault();
}
else if (e.keyCode==83)
{
var next;
if (selected != null) next = (selected % 7) + (((Math.floor(selected/7) % 5) + 1) % 5) * 7;
else next = 0;
if (next == 34) next = 6;
plants[selected].deselect();
selected = next;
plants[selected].select();
updatestages(selected);
e.preventDefault();
}
keys[e.keyCode]=1;
});

window.addEventListener('visibilitychange',function(e){
keys=[];//reset all key pressed on visibility change (should help prevent ctrl still being down after ctrl-tab)
});

0 comments on commit bbce591

Please sign in to comment.