mirror of
https://github.com/TextPlace/CoreTextPlace.git
synced 2025-05-20 14:11:09 +00:00
fix: apply change for 2-w at alternate position
This commit is contained in:
parent
6dfe7129c4
commit
4999c5096a
3 changed files with 121 additions and 47 deletions
|
@ -7,6 +7,11 @@ export function createSection(
|
||||||
{ sx, sy }: SectionPosition,
|
{ sx, sy }: SectionPosition,
|
||||||
boardConfig: BoardConfig
|
boardConfig: BoardConfig
|
||||||
): SectionData {
|
): SectionData {
|
||||||
|
if (boardConfig.sectionWidth % 2 !== 0)
|
||||||
|
throw new Error(
|
||||||
|
"sectionWidth must be multiple of 2 (least common multiples of all character widths)"
|
||||||
|
);
|
||||||
|
|
||||||
const offsetX = sx * boardConfig.sectionWidth;
|
const offsetX = sx * boardConfig.sectionWidth;
|
||||||
const offsetY = sy * boardConfig.sectionHeight;
|
const offsetY = sy * boardConfig.sectionHeight;
|
||||||
|
|
||||||
|
@ -32,8 +37,10 @@ export function applyChange(change: BoardChange, section: SectionData) {
|
||||||
|
|
||||||
if (change.ch) {
|
if (change.ch) {
|
||||||
const chWidth = getCharacterWidth(change.ch);
|
const chWidth = getCharacterWidth(change.ch);
|
||||||
section.ch[yInSection][xInSection] = change.ch;
|
const xCharacterOffset = xInSection % 2;
|
||||||
section.width[yInSection][xInSection] = chWidth;
|
const offsetAdjustedXInSection = xInSection - xCharacterOffset;
|
||||||
|
section.ch[yInSection][offsetAdjustedXInSection] = change.ch;
|
||||||
|
section.width[yInSection][offsetAdjustedXInSection] = chWidth;
|
||||||
}
|
}
|
||||||
if (change.color) {
|
if (change.color) {
|
||||||
section.color[yInSection][xInSection] = change.color;
|
section.color[yInSection][xInSection] = change.color;
|
||||||
|
|
|
@ -1,53 +1,115 @@
|
||||||
import { assertEquals } from "https://deno.land/std@0.224.0/assert/mod.ts";
|
import {
|
||||||
|
assert,
|
||||||
|
assertEquals,
|
||||||
|
assertThrows,
|
||||||
|
} from "https://deno.land/std@0.224.0/assert/mod.ts";
|
||||||
|
|
||||||
import { createSection } from "../logic/section.ts";
|
import { applyChange, createSection } from "../logic/section.ts";
|
||||||
|
import type { SectionData } from "../types/section.ts";
|
||||||
|
|
||||||
Deno.test("createSection", () => {
|
Deno.test("section", async (t) => {
|
||||||
const section = createSection(
|
let section: SectionData | undefined;
|
||||||
{ sx: 0, sy: 0 },
|
|
||||||
{
|
await t.step("createSection non-lcm", () => {
|
||||||
xSections: 2,
|
assertThrows(() => {
|
||||||
ySections: 2,
|
createSection(
|
||||||
sectionWidth: 3,
|
{ sx: 0, sy: 0 },
|
||||||
sectionHeight: 3,
|
{
|
||||||
defaultCh: " ",
|
xSections: 2,
|
||||||
defaultColor: "F",
|
ySections: 2,
|
||||||
defaultBgColor: "0",
|
sectionWidth: 3,
|
||||||
defaultWidth: 1,
|
sectionHeight: 3,
|
||||||
|
defaultCh: " ",
|
||||||
|
defaultColor: "F",
|
||||||
|
defaultBgColor: "0",
|
||||||
|
defaultWidth: 1,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
await t.step("createSection non-origin section", () => {
|
||||||
|
section = createSection(
|
||||||
|
{ sx: 1, sy: 1 },
|
||||||
|
{
|
||||||
|
xSections: 2,
|
||||||
|
ySections: 2,
|
||||||
|
sectionWidth: 4,
|
||||||
|
sectionHeight: 3,
|
||||||
|
defaultCh: " ",
|
||||||
|
defaultColor: "F",
|
||||||
|
defaultBgColor: "0",
|
||||||
|
defaultWidth: 1,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(section.offsetX, 4);
|
||||||
|
assertEquals(section.offsetY, 3);
|
||||||
|
});
|
||||||
|
|
||||||
|
await t.step("createSection", () => {
|
||||||
|
section = createSection(
|
||||||
|
{ sx: 0, sy: 0 },
|
||||||
|
{
|
||||||
|
xSections: 2,
|
||||||
|
ySections: 2,
|
||||||
|
sectionWidth: 4,
|
||||||
|
sectionHeight: 3,
|
||||||
|
defaultCh: " ",
|
||||||
|
defaultColor: "F",
|
||||||
|
defaultBgColor: "0",
|
||||||
|
defaultWidth: 1,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(section.offsetX, 0);
|
||||||
|
assertEquals(section.offsetY, 0);
|
||||||
|
|
||||||
|
function assertSectionContent<T>(
|
||||||
|
content: T[][],
|
||||||
|
rowCount: number,
|
||||||
|
columnCount: number,
|
||||||
|
value: T
|
||||||
|
) {
|
||||||
|
assertEquals(content.length, rowCount);
|
||||||
|
for (const row of content) {
|
||||||
|
assertEquals(row.length, columnCount);
|
||||||
|
for (const item of row) {
|
||||||
|
assertEquals(item, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
);
|
|
||||||
|
|
||||||
assertEquals(section.offsetX, 0);
|
assertSectionContent(section.ch, 3, 4, " ");
|
||||||
assertEquals(section.offsetY, 0);
|
assertSectionContent(section.color, 3, 4, "F");
|
||||||
assertEquals(section.ch.length, 3);
|
assertSectionContent(section.bgColor, 3, 4, "0");
|
||||||
for (const row of section.ch) {
|
assertSectionContent(section.width, 3, 4, 1);
|
||||||
assertEquals(row.length, 3);
|
});
|
||||||
for (const ch of row) {
|
|
||||||
assertEquals(ch, " ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
assertEquals(section.color.length, 3);
|
await t.step("applyChange 1-width", () => {
|
||||||
for (const row of section.color) {
|
assert(section);
|
||||||
assertEquals(row.length, 3);
|
|
||||||
for (const color of row) {
|
|
||||||
assertEquals(color, "F");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
assertEquals(section.bgColor.length, 3);
|
applyChange({ x: 0, y: 0, ch: "t" }, section);
|
||||||
for (const row of section.bgColor) {
|
assertEquals(section.ch[0][0], "t");
|
||||||
assertEquals(row.length, 3);
|
assertEquals(section.ch[0][1], " ");
|
||||||
for (const bgColor of row) {
|
assertEquals(section.width[0][0], 1);
|
||||||
assertEquals(bgColor, "0");
|
});
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
assertEquals(section.width.length, 3);
|
await t.step("applyChange 2-width at a correct position", () => {
|
||||||
for (const row of section.width) {
|
assert(section);
|
||||||
assertEquals(row.length, 3);
|
|
||||||
for (const width of row) {
|
applyChange({ x: 0, y: 0, ch: "あ" }, section);
|
||||||
assertEquals(width, 1);
|
assertEquals(section.ch[0][0], "あ");
|
||||||
}
|
assertEquals(section.ch[0][1], " ");
|
||||||
}
|
assertEquals(section.width[0][0], 2);
|
||||||
|
});
|
||||||
|
|
||||||
|
await t.step("applyChange 2-width at an alternate position", () => {
|
||||||
|
assert(section);
|
||||||
|
|
||||||
|
applyChange({ x: 1, y: 0, ch: "あ" }, section);
|
||||||
|
assertEquals(section.ch[0][0], "あ");
|
||||||
|
assertEquals(section.ch[0][1], " ");
|
||||||
|
assertEquals(section.width[0][0], 2);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -36,6 +36,11 @@ export interface CharacterPosition {
|
||||||
export interface BoardConfig {
|
export interface BoardConfig {
|
||||||
xSections: number;
|
xSections: number;
|
||||||
ySections: number;
|
ySections: number;
|
||||||
|
/**
|
||||||
|
* Width of each section, in display characters.
|
||||||
|
*
|
||||||
|
* Note that this must be a multiple of the least common multiple of all possible character display width (returned by `getCharacterWidth`).
|
||||||
|
*/
|
||||||
sectionWidth: number;
|
sectionWidth: number;
|
||||||
sectionHeight: number;
|
sectionHeight: number;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue