Skip to content

Commit

Permalink
Add an option to hide instances
Browse files Browse the repository at this point in the history
  • Loading branch information
krypciak committed Nov 25, 2024
1 parent 5d70266 commit 7a6ae88
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
14 changes: 9 additions & 5 deletions src/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ type SetFunc = (name: string, to?: any) => void
export class Instance {
private static instanceIdCounter = 0

static currentReference(): Instance {
return new Instance(ig, sc)
static currentReference(display?: boolean): Instance {
return new Instance(ig, sc, display)
}

static async copy(s: Instance): Promise<Instance> {
static async copy(s: Instance, display?: boolean): Promise<Instance> {
const ig: any = {}
const igToInit: string[] = []
for (const key in s.ig) {
Expand Down Expand Up @@ -82,7 +82,7 @@ export class Instance {
scset('skilltree')
scset('version')

const ns = new Instance(ig, sc)
const ns = new Instance(ig, sc, display)
ns.apply()

const canvasId = `canvas${ns.id}`
Expand All @@ -97,6 +97,9 @@ export class Instance {
divE.appendChild(canvasE)

document.body.appendChild(divE)
if (!ns.display) {
divE.style.display = 'none'
}

igset(
'system',
Expand Down Expand Up @@ -205,7 +208,8 @@ export class Instance {

private constructor(
public ig: typeof window.ig,
public sc: typeof window.sc
public sc: typeof window.sc,
public display: boolean = true
) {
this.id = Instance.instanceIdCounter
Instance.instanceIdCounter++
Expand Down
12 changes: 8 additions & 4 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ export default class CCInstanceinator implements PluginClass {
let counter = 0
ig.System.inject({
run() {
// if (Object.keys(inst.instances).length <= 1) return this.parent()
const instances = Object.values(inst.instances).sort((a, b) => a.id - b.id)

if (instances.length > 0) {
Expand All @@ -96,11 +95,16 @@ export default class CCInstanceinator implements PluginClass {
async poststart() {
this.instances[0] = Instance.currentReference()

for (let i = 1; i < 1; i++) {
this.instances[i] = await Instance.copy(this.instances[0])
this.instances[i].apply()
for (let i = 1; i < 2; i++) {
const instance = await Instance.copy(this.instances[0], false)
this.append(instance)
instance.apply()
}
}

append(instance: Instance) {
this.instances[instance.id] = instance
}
}

declare global {
Expand Down
21 changes: 12 additions & 9 deletions src/tiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ declare global {
}
}
}

function getDisplayInstances() {
return Object.values(inst.instances).filter(i => i.display)
}

export function injectTiling() {
ig.System.inject({
setCanvasSize(width, height, hideBorder) {
if (Object.keys(inst.instances).length <= 1) return this.parent(width, height, hideBorder)
if (getDisplayInstances().length <= 1) return this.parent(width, height, hideBorder)
this.canvas.style.width = '100%'
this.canvas.style.height = '100%'
this.canvas.className = 'borderHidden'
Expand All @@ -17,28 +22,26 @@ export function injectTiling() {

sc.OptionModel.inject({
_setDisplaySize(schedule = true) {
if (Object.keys(inst.instances).length <= 1) return this.parent()
const instances = getDisplayInstances().sort((a, b) => a.id - b.id)
if (instances.length <= 1) return this.parent()

if (!schedule) return this.parent()

for (const instId of Object.keys(inst.instances).map(Number)) {
const instance = inst.instances[instId]
for (const instance of instances) {
instance.ig.game.scheduledTasks.push(() => {
sc.options?._setDisplaySize(false)
})
}

const insts = Object.values(inst.instances).sort((a, b) => a.id - b.id)

const ws = document.body.clientWidth
const hs = document.body.clientHeight
function fitRectangles() {
let bestWi = 0
let bestGrid = [0, 0]

const aspectRatioRev = 320 / 568
for (let nx = 1; nx <= Math.ceil(insts.length); nx++) {
const ny = Math.ceil(insts.length / nx)
for (let nx = 1; nx <= Math.ceil(instances.length); nx++) {
const ny = Math.ceil(instances.length / nx)
const wi = Math.min(ws / nx, hs / ny / aspectRatioRev)

if (wi > bestWi) {
Expand All @@ -61,7 +64,7 @@ export function injectTiling() {
let itemI = 0
for (let column = 0; column < grid[1]; column++) {
for (let row = 0; row < grid[0]; row++) {
const instance = insts[itemI]
const instance = instances[itemI]
if (!instance) break
const item = instance.ig.system.inputDom
item.style.position = 'absolute'
Expand Down

0 comments on commit 7a6ae88

Please sign in to comment.