Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function canvasCheckPage() {
- const [px, py, pz] = cursorState(layers.gui);
- if (typeof book === 'undefined') {
- book = `"Puzzlum's Palace" (No.1)`;
- }
- if (typeof page === 'undefined') {
- page = `"Puzzlum's Palace" (No.1)`;
- }
- return true;
- }
- // List of keys to capture
- var keysToCapture = [];
- // Object to hold the state of pressed keys
- var keysPressed = {};
- // Event listener for keydown to update the keysPressed object
- document.addEventListener('keydown', function(event) {
- if (keysToCapture.includes(event.code)) {
- keysPressed[event.code] = true;
- }
- });
- // Event listener for keyup to update the keysPressed object
- document.addEventListener('keyup', function(event) {
- if (keysToCapture.includes(event.code)) {
- keysPressed[event.code] = false;
- }
- });
- // Function to handle key captures and page rendering
- function canvasRenderPage() {
- keysToCapture = [];
- if (typeof history === 'undefined') {
- history = {};
- }
- if (typeof history[book] === 'undefined') {
- history[book] = {};
- }
- if (typeof history[book][page] === 'undefined') {
- history[book][page] = 0;
- }
- if (history[book][page] === 0) {
- history[book][page] = 1;
- }
- const width = backContext.width;
- const height = backContext.height;
- const defaultRowspan = 12;
- let rowspan = defaultRowspan;
- let row = 0;
- let xOffset = 0;
- let yOffset = 0;
- /*
- var imageStack = [];
- for ( let index = 1; index <= books[book][page]['header']['count']; index++ )
- {
- imageStack.push ( `/assets`+books[book][page]['header'][index]['illustration'] );
- }
- var stack = loadImageStack(imageStack);
- for ( let index = 0; index < stack.length; index++ )
- {
- backContext.drawImage(stack[index],stack[index].naturalWidth,stack[index].naturalHeight);
- }
- */
- function incrementRow() {
- yOffset += (row + 1) * rowspan;
- row = 0;
- }
- function renderText(text, font = '12px Arial', color = '#000000') {
- wrapText(xOffset, yOffset + row * rowspan, width, rowspan, font, color, text);
- row++;
- }
- incrementRow();
- renderText(book.split(`"`)[1], `${rowspan}px Arial`);
- incrementRow();
- const captionCount = parseInt(books[book][page]['caption']['count']);
- for (let caption = 1; caption <= captionCount; caption++) {
- let text = books[book][page]['caption'][caption]['caption'];
- if (text) {
- renderText(text, `${rowspan}px Small Fonts`);
- }
- }
- incrementRow();
- const entityCount = parseInt(books[book][page]['entity']['count']);
- for (let entity = 1; entity <= entityCount; entity++) {
- let { name, sprite, text, mood } = books[book][page]['entity'][entity];
- if (text) {
- renderText(`${mood} ${name}: ${text}`, '12px Small Fonts');
- }
- }
- incrementRow();
- const optionCount = parseInt(books[book][page]['option']['count']);
- for (let option = 1; option <= optionCount; option++) {
- keysToCapture[option] = {};
- let { key, dest, preq, caption, condition } = books[book][page]['option'][option];
- let approval = false;
- switch (ucwords(condition)) {
- case "Contrary":
- if (!history[book][page] || history[book][preq] === 0) {
- approval = true;
- }
- break;
- case "Mandatory":
- if (history[book][page] && history[book][preq] > 0) {
- approval = true;
- }
- break;
- case "Elective":
- approval = true;
- break;
- }
- if (approval) {
- keysToCapture[option] = { key, dest };
- renderText(`${key}: ${caption}`, '12px Small Fonts');
- }
- }
- incrementRow();
- const visitedPagesCount = Object.values(history).reduce((acc, value) => acc + (value > 0 ? 1 : 0), 0);
- renderText(`Pages visited: ${visitedPagesCount}`, '12px Small Fonts');
- canvasPageKeys(keysToCapture);
- return true;
- }
- // Function to handle the pressed keys and page navigation
- function canvasPageKeys(keysToCapture) {
- for (let option = 1; option < keysToCapture.length - 1; option++) {
- let key = keysToCapture[option]['key'];
- switch (key) {
- case 'SP':
- case 'SPC':
- case 'SPACE':
- key = 'Space';
- break;
- case 'CR':
- case 'ENTER':
- key = 'Enter';
- break;
- case 'ESC':
- case 'ESCAPE':
- key = 'Escape';
- break;
- case 'TAB':
- case 'HTAB':
- key = 'Tab';
- break;
- case 'DEL':
- case 'DELETE':
- key = 'Delete';
- break;
- case 'BS':
- case 'BKSP':
- key = 'Backspace';
- break;
- }
- page = keysToCapture[option]['dest'];
- }
- }
- // Set up the interval to check key presses every 100 milliseconds
- setInterval(canvasRenderPage, 100);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement