mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-11-24 20:23:08 +02:00
Optimization (frontend): MkModPlayer Don't waste memory on a static pattern
Fix: Inconsistent pattern height offset.
This commit is contained in:
parent
452c5668ea
commit
00e118c0ce
1 changed files with 182 additions and 67 deletions
|
@ -7,7 +7,7 @@
|
|||
</div>
|
||||
|
||||
<div v-else class="mod-player-enabled">
|
||||
<div class="pattern-display" @click="togglePattern()">
|
||||
<div class="pattern-display" @click="togglePattern()" @scroll="scrollHandler" @scrollend="scrollEndHandle">
|
||||
<div v-if="patternHide" class="pattern-hide">
|
||||
<b><i class="ph-eye ph-bold ph-lg"></i> Pattern Hidden</b>
|
||||
<span>{{ i18n.ts.clickToShow }}</span>
|
||||
|
@ -17,6 +17,7 @@
|
|||
<canvas ref="displayCanvas" class="pattern-canvas"></canvas>
|
||||
</div>
|
||||
<div class="controls">
|
||||
<input v-if="patternScrollSliderShow" ref="patternScrollSlider" v-model="patternScrollSliderPos" class="pattern-slider" type="range" min="0" max="100" step="0.01" style=""/>
|
||||
<button class="play" @click="playPause()">
|
||||
<i v-if="playing" class="ph-pause ph-bold ph-lg"></i>
|
||||
<i v-else class="ph-play ph-bold ph-lg"></i>
|
||||
|
@ -35,11 +36,12 @@
|
|||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { ref, nextTick, computed } from 'vue';
|
||||
import { ref, nextTick, computed, watch } from 'vue';
|
||||
import * as Misskey from 'misskey-js';
|
||||
import { i18n } from '@/i18n.js';
|
||||
import { defaultStore } from '@/store.js';
|
||||
import { ChiptuneJsPlayer, ChiptuneJsConfig } from '@/scripts/chiptune2.js';
|
||||
import { isTouchUsing } from '@/scripts/touch.js';
|
||||
|
||||
const colours = {
|
||||
background: '#000000',
|
||||
|
@ -69,6 +71,9 @@ let playing = ref(false);
|
|||
let displayCanvas = ref<HTMLCanvasElement>();
|
||||
let progress = ref<HTMLProgressElement>();
|
||||
let position = ref(0);
|
||||
let patternScrollSlider = ref<HTMLProgressElement>();
|
||||
let patternScrollSliderShow = ref(false);
|
||||
let patternScrollSliderPos = ref(0);
|
||||
const player = ref(new ChiptuneJsPlayer(new ChiptuneJsConfig()));
|
||||
|
||||
const maxRowNumbers = 0xFF;
|
||||
|
@ -179,20 +184,22 @@ function toggleVisible() {
|
|||
|
||||
function togglePattern() {
|
||||
patternHide.value = !patternHide.value;
|
||||
if (!patternHide.value) {
|
||||
handleScrollBarEnable();
|
||||
|
||||
if (player.value.getRow() === 0) {
|
||||
try {
|
||||
player.value.play(buffer);
|
||||
display();
|
||||
display(true);
|
||||
} catch (err) {
|
||||
console.warn(err);
|
||||
}
|
||||
player.value.stop();
|
||||
}
|
||||
} else {
|
||||
display(true);
|
||||
}
|
||||
}
|
||||
|
||||
function DrawPattern() {
|
||||
function drawPattern() {
|
||||
if (!displayCanvas.value) return;
|
||||
const canvas = displayCanvas.value;
|
||||
|
||||
|
@ -202,8 +209,6 @@ function DrawPattern() {
|
|||
const halfbuf = rowBuffer / 2;
|
||||
const minRow = row - halfbuf;
|
||||
const maxRow = row + halfbuf;
|
||||
const spacer = 11;
|
||||
const space = ' ';
|
||||
|
||||
let rowDif = 0;
|
||||
|
||||
|
@ -215,8 +220,8 @@ function DrawPattern() {
|
|||
rowDif = row - lastDrawnRow;
|
||||
} else {
|
||||
alreadyDrawn = [];
|
||||
canvas.width = 12 + 84 * nbChannels + 2;
|
||||
canvas.height = 12 * nbRows;
|
||||
if (canvas.width !== (12 + 84 * nbChannels + 2)) canvas.width = 12 + 84 * nbChannels + 2;
|
||||
if (canvas.height !== (12 * nbRows)) canvas.height = 12 * nbRows;
|
||||
}
|
||||
|
||||
const ctx = canvas.getContext('2d', { alpha: false }) as CanvasRenderingContext2D;
|
||||
|
@ -235,43 +240,9 @@ function DrawPattern() {
|
|||
if (alreadyDrawn[rowToDraw] === true) continue;
|
||||
|
||||
if (rowToDraw >= 0 && rowToDraw < nbRows) {
|
||||
let seperators = '';
|
||||
let note = '';
|
||||
let instr = '';
|
||||
let volume = '';
|
||||
let fx = '';
|
||||
let op = '';
|
||||
for (let channel = 0; channel < nbChannels; channel++) {
|
||||
const part = player.value.getPatternRowChannel(pattern, rowToDraw, channel);
|
||||
|
||||
seperators += '|' + space.repeat( spacer + 2 );
|
||||
note += part.substring(0, 3) + space.repeat( spacer );
|
||||
instr += part.substring(4, 6) + space.repeat( spacer + 1 );
|
||||
volume += part.substring(6, 9) + space.repeat( spacer );
|
||||
fx += part.substring(10, 11) + space.repeat( spacer + 2 );
|
||||
op += part.substring(11, 13) + space.repeat( spacer + 1 );
|
||||
}
|
||||
|
||||
const baseOffset = 2 * CHAR_WIDTH;
|
||||
const baseRowOffset = ROW_OFFSET_Y + rowToDraw * CHAR_HEIGHT;
|
||||
|
||||
ctx.fillStyle = colours.foreground.default;
|
||||
ctx.fillText(seperators, baseOffset, baseRowOffset);
|
||||
|
||||
ctx.fillStyle = colours.foreground.default;
|
||||
ctx.fillText(note, baseOffset + CHAR_WIDTH, baseRowOffset);
|
||||
|
||||
ctx.fillStyle = colours.foreground.instr;
|
||||
ctx.fillText(instr, baseOffset + CHAR_WIDTH * 5, baseRowOffset);
|
||||
|
||||
ctx.fillStyle = colours.foreground.volume;
|
||||
ctx.fillText(volume, baseOffset + CHAR_WIDTH * 7, baseRowOffset);
|
||||
|
||||
ctx.fillStyle = colours.foreground.fx;
|
||||
ctx.fillText(fx, baseOffset + CHAR_WIDTH * 11, baseRowOffset);
|
||||
|
||||
ctx.fillStyle = colours.foreground.operant;
|
||||
ctx.fillText(op, baseOffset + CHAR_WIDTH * 12, baseRowOffset);
|
||||
drawRow(ctx, rowToDraw, nbChannels, pattern, baseOffset, baseRowOffset);
|
||||
|
||||
alreadyDrawn[rowToDraw] = true;
|
||||
}
|
||||
|
@ -281,30 +252,155 @@ function DrawPattern() {
|
|||
lastPattern = pattern;
|
||||
}
|
||||
|
||||
function display() {
|
||||
if (!displayCanvas.value) {
|
||||
function drawPetternPreview() {
|
||||
if (!displayCanvas.value) return;
|
||||
const canvas = displayCanvas.value;
|
||||
|
||||
const pattern = player.value.getPattern();
|
||||
const nbRows = player.value.getPatternNumRows(pattern);
|
||||
const row = player.value.getRow();
|
||||
const halfbuf = rowBuffer / 2;
|
||||
|
||||
let nbChannels = 0;
|
||||
if (player.value.currentPlayingNode) {
|
||||
nbChannels = player.value.currentPlayingNode.nbChannels;
|
||||
}
|
||||
if (canvas.width !== (12 + 84 * nbChannels + 2)) canvas.width = 12 + 84 * nbChannels + 2;
|
||||
if (canvas.height !== (12 * rowBuffer)) canvas.height = 12 * rowBuffer;
|
||||
|
||||
const ctx = canvas.getContext('2d', { alpha: false }) as CanvasRenderingContext2D;
|
||||
ctx.font = '10px monospace';
|
||||
ctx.imageSmoothingEnabled = false;
|
||||
ctx.fillStyle = colours.background;
|
||||
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
||||
ctx.drawImage( numberRowCanvas, 0, (halfbuf - row) * CHAR_HEIGHT );
|
||||
|
||||
ctx.fillStyle = colours.foreground.default;
|
||||
for (let rowOffset = 0; rowOffset < rowBuffer; rowOffset++) {
|
||||
const rowToDraw = rowOffset + row - halfbuf;
|
||||
|
||||
if (rowToDraw >= 0 && rowToDraw < nbRows) {
|
||||
const baseOffset = 2 * CHAR_WIDTH;
|
||||
const baseRowOffset = ROW_OFFSET_Y + rowOffset * CHAR_HEIGHT;
|
||||
drawRow(ctx, rowToDraw, nbChannels, pattern, baseOffset, baseRowOffset);
|
||||
} else if (rowToDraw >= 0) {
|
||||
const baseRowOffset = ROW_OFFSET_Y + rowOffset * CHAR_HEIGHT;
|
||||
ctx.fillStyle = colours.background;
|
||||
ctx.fillRect(0, baseRowOffset - CHAR_HEIGHT, CHAR_WIDTH * 2, baseRowOffset);
|
||||
}
|
||||
}
|
||||
|
||||
lastPattern = -1;
|
||||
lastDrawnRow = -1;
|
||||
}
|
||||
|
||||
function drawRow(ctx: CanvasRenderingContext2D, row: number, channels: number, pattern: number, drawX = (2 * CHAR_WIDTH), drawY = ROW_OFFSET_Y) {
|
||||
const spacer = 11;
|
||||
const space = ' ';
|
||||
let seperators = '';
|
||||
let note = '';
|
||||
let instr = '';
|
||||
let volume = '';
|
||||
let fx = '';
|
||||
let op = '';
|
||||
for (let channel = 0; channel < channels; channel++) {
|
||||
const part = player.value.getPatternRowChannel(pattern, row, channel);
|
||||
|
||||
seperators += '|' + space.repeat( spacer + 2 );
|
||||
note += part.substring(0, 3) + space.repeat( spacer );
|
||||
instr += part.substring(4, 6) + space.repeat( spacer + 1 );
|
||||
volume += part.substring(6, 9) + space.repeat( spacer );
|
||||
fx += part.substring(10, 11) + space.repeat( spacer + 2 );
|
||||
op += part.substring(11, 13) + space.repeat( spacer + 1 );
|
||||
}
|
||||
|
||||
ctx.fillStyle = colours.foreground.default;
|
||||
ctx.fillText(seperators, drawX, drawY);
|
||||
|
||||
ctx.fillStyle = colours.foreground.default;
|
||||
ctx.fillText(note, drawX + CHAR_WIDTH, drawY);
|
||||
|
||||
ctx.fillStyle = colours.foreground.instr;
|
||||
ctx.fillText(instr, drawX + CHAR_WIDTH * 5, drawY);
|
||||
|
||||
ctx.fillStyle = colours.foreground.volume;
|
||||
ctx.fillText(volume, drawX + CHAR_WIDTH * 7, drawY);
|
||||
|
||||
ctx.fillStyle = colours.foreground.fx;
|
||||
ctx.fillText(fx, drawX + CHAR_WIDTH * 11, drawY);
|
||||
|
||||
ctx.fillStyle = colours.foreground.operant;
|
||||
ctx.fillText(op, drawX + CHAR_WIDTH * 12, drawY);
|
||||
}
|
||||
|
||||
function display(skipOptimizationChecks = false) {
|
||||
if (!displayCanvas.value || !displayCanvas.value.parentElement) {
|
||||
stop();
|
||||
return;
|
||||
}
|
||||
|
||||
if (patternHide.value) return;
|
||||
if (patternHide.value && !skipOptimizationChecks) return;
|
||||
|
||||
if (firstFrame) {
|
||||
firstFrame = false;
|
||||
// Change the next line to false in order to show the pattern by default.
|
||||
// Changing it to false should enable pattern display by default.
|
||||
patternHide.value = true;
|
||||
handleScrollBarEnable();
|
||||
firstFrame = false;
|
||||
}
|
||||
|
||||
const row = player.value.getRow();
|
||||
const pattern = player.value.getPattern();
|
||||
|
||||
if ( row === lastDrawnRow && pattern === lastPattern ) return;
|
||||
if ( row === lastDrawnRow && pattern === lastPattern && !skipOptimizationChecks) return;
|
||||
|
||||
DrawPattern();
|
||||
// Size vs speed
|
||||
if (patternHide.value) drawPetternPreview();
|
||||
else drawPattern();
|
||||
|
||||
displayCanvas.value.style.top = 'calc( 50% - ' + (row * CHAR_HEIGHT) + 'px )';
|
||||
displayCanvas.value.style.top = !patternHide.value ? 'calc( 50% - ' + (row * CHAR_HEIGHT) + 'px )' : '0%';
|
||||
}
|
||||
|
||||
let suppressScrollSliderWatcher = false;
|
||||
|
||||
function scrollHandler() {
|
||||
suppressScrollSliderWatcher = true;
|
||||
|
||||
if (!patternScrollSlider.value) return;
|
||||
if (!displayCanvas.value) return;
|
||||
if (!displayCanvas.value.parentElement) return;
|
||||
|
||||
patternScrollSliderPos.value = (displayCanvas.value.parentElement.scrollLeft) / (displayCanvas.value.width - displayCanvas.value.parentElement.offsetWidth) * 100;
|
||||
patternScrollSlider.value.style.opacity = '1';
|
||||
}
|
||||
|
||||
function scrollEndHandle() {
|
||||
suppressScrollSliderWatcher = false;
|
||||
|
||||
if (!patternScrollSlider.value) return;
|
||||
patternScrollSlider.value.style.opacity = '';
|
||||
}
|
||||
|
||||
function handleScrollBarEnable() {
|
||||
patternScrollSliderShow.value = (!patternHide.value && !isTouchUsing);
|
||||
if (patternScrollSliderShow.value !== true) return;
|
||||
|
||||
if (!displayCanvas.value) return;
|
||||
if (!displayCanvas.value.parentElement) return;
|
||||
if (firstFrame) {
|
||||
patternScrollSliderShow.value = (12 + 84 * player.value.getPatternNumRows(player.value.getPattern()) + 2 > displayCanvas.value.parentElement.offsetWidth);
|
||||
} else {
|
||||
patternScrollSliderShow.value = (displayCanvas.value.width > displayCanvas.value.parentElement.offsetWidth);
|
||||
}
|
||||
}
|
||||
|
||||
watch(patternScrollSliderPos, () => {
|
||||
if (suppressScrollSliderWatcher) return;
|
||||
if (!displayCanvas.value) return;
|
||||
if (!displayCanvas.value.parentElement) return;
|
||||
|
||||
displayCanvas.value.parentElement.scrollLeft = (displayCanvas.value.width - displayCanvas.value.parentElement.offsetWidth) * patternScrollSliderPos.value / 100;
|
||||
});
|
||||
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
|
@ -347,6 +443,12 @@ function display() {
|
|||
text-align: center;
|
||||
max-height: 312px; /* magic_number = CHAR_HEIGHT * rowBuffer, needs to be in px */
|
||||
|
||||
scrollbar-width: none;
|
||||
|
||||
&::-webkit-scrollbar {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.pattern-canvas {
|
||||
position: relative;
|
||||
background-color: black;
|
||||
|
@ -357,36 +459,37 @@ function display() {
|
|||
|
||||
.patternShadowTop {
|
||||
background: #00000080;
|
||||
pointer-events: none;
|
||||
width: 100%;
|
||||
height: 50%;
|
||||
height: calc( 50% - 14px );
|
||||
translate: 0 -100%;
|
||||
top: calc( 50% - 13px );
|
||||
top: calc( 50% - 14px );
|
||||
position: absolute;
|
||||
pointer-events: none;
|
||||
z-index: 2;
|
||||
}
|
||||
|
||||
.patternShadowBottom {
|
||||
background: #00000080;
|
||||
pointer-events: none;
|
||||
width: 100%;
|
||||
height: calc( 50% + 12px );
|
||||
height: calc( 50% - 12px );
|
||||
top: calc( 50% - 1px );
|
||||
position: absolute;
|
||||
pointer-events: none;
|
||||
z-index: 2;
|
||||
}
|
||||
|
||||
.pattern-hide {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
background: rgba(64, 64, 64, 0.3);
|
||||
backdrop-filter: blur(2em);
|
||||
backdrop-filter: var(--modalBgFilter);
|
||||
color: #fff;
|
||||
font-size: 12px;
|
||||
|
||||
position: absolute;
|
||||
z-index: 3;
|
||||
z-index: 4;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
|
||||
|
@ -400,7 +503,7 @@ function display() {
|
|||
display: flex;
|
||||
width: 100%;
|
||||
background-color: var(--bg);
|
||||
z-index: 3;
|
||||
z-index: 5;
|
||||
|
||||
> * {
|
||||
padding: 4px 8px;
|
||||
|
@ -425,6 +528,18 @@ function display() {
|
|||
margin: 4px 8px;
|
||||
overflow-x: hidden;
|
||||
|
||||
&.pattern-slider {
|
||||
position: absolute;
|
||||
width: calc( 100% - 8px * 2 );
|
||||
top: calc( 100% - 21px * 3 );
|
||||
opacity: 0%;
|
||||
transition: opacity 0.2s;
|
||||
|
||||
&:hover {
|
||||
opacity: 100%;
|
||||
}
|
||||
}
|
||||
|
||||
&:focus {
|
||||
outline: none;
|
||||
|
||||
|
|
Loading…
Reference in a new issue