Skip to content

Commit

Permalink
#154 DisplayObjectContainerの関数を実装(WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
ienaga committed Dec 4, 2024
1 parent c3df4f5 commit 45576cc
Show file tree
Hide file tree
Showing 12 changed files with 332 additions and 153 deletions.
87 changes: 13 additions & 74 deletions packages/display/src/DisplayObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { execute as displayObjectLocalToGlobalService } from "./DisplayObject/se
import { execute as displayObjectGlobalToLocalService } from "./DisplayObject/service/DisplayObjectGlobalToLocalService";
import { execute as displayObjectGetHeightUseCase } from "./DisplayObject/usecase/DisplayObjectGetHeightUseCase";
import { execute as displayObjectSetHeightUseCase } from "./DisplayObject/usecase/DisplayObjectSetHeightUseCase";
import { execute as displayObjectRemoveService } from "./DisplayObject/service/DisplayObjectRemoveService";
import {
$getInstanceId,
$parentMap,
Expand Down Expand Up @@ -1114,78 +1115,16 @@ export class DisplayObject extends EventDispatcher
return $variables.clear();
}

// /**
// * @param {Float32Array} multi_matrix
// * @returns {object}
// * @private
// */
// _$getLayerBounds (multi_matrix: Float32Array): BoundsImpl
// {
// const baseBounds: BoundsImpl = "_$getBounds" in this && typeof this._$getBounds === "function"
// ? this._$getBounds() as BoundsImpl
// : $getBoundsObject();

// const bounds: BoundsImpl = $boundsMatrix(baseBounds, multi_matrix);
// $poolBoundsObject(baseBounds);

// const filters: FilterArrayImpl = this._$filters || this.filters;
// if (!filters.length) {
// return bounds;
// }

// let filterBounds: BoundsImpl = $getBoundsObject(
// 0,
// $Math.abs(bounds.xMax - bounds.xMin),
// 0,
// $Math.abs(bounds.yMax - bounds.yMin)
// );
// $poolBoundsObject(bounds);

// let xScale: number = +$Math.sqrt(
// multi_matrix[0] * multi_matrix[0]
// + multi_matrix[1] * multi_matrix[1]
// );
// let yScale: number = +$Math.sqrt(
// multi_matrix[2] * multi_matrix[2]
// + multi_matrix[3] * multi_matrix[3]
// );

// xScale /= $devicePixelRatio;
// yScale /= $devicePixelRatio;

// xScale *= 2;
// yScale *= 2;

// for (let idx: number = 0; idx < filters.length; ++idx) {
// filterBounds = filters[idx]
// ._$generateFilterRect(filterBounds, xScale, yScale);
// }

// return filterBounds;
// }

// /**
// * @description AnimationToolのシンボルと同期
// * Synchronize with AnimationTool symbol
// *
// * @param {number} character_id
// * @param {object} character
// * @param {LoaderInfo} loaderInfo
// * @return {void}
// * @method
// * @protected
// */
// _$sync (
// character_id: number,
// character: ICharacter,
// loaderInfo: LoaderInfo
// ): void {

// // setup
// this.characterId = character_id;
// $loaderInfoMap.set(this, loaderInfo);

// // build
// this._$buildCharacter(character);
// }
/**
* @description 親子関係を解除します。
* Removes the parent-child relationship.
*
* @return {void}
* @method
* @public
*/
remove (): void
{
displayObjectRemoveService(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { DisplayObject } from "../../DisplayObject";

/**
* @description 親子関係を解除する。
* Break the parent-child relationship.
*
* @param {DisplayObject} display_object
* @return {void}
* @method
* @protected
*/
export const execute = <D extends DisplayObject>(display_object: D): void =>
{
const parent = display_object.parent;
if (!parent) {
return ;
}
parent.removeChild(display_object);
};
104 changes: 25 additions & 79 deletions packages/display/src/DisplayObjectContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import type { IDisplayObject } from "./interface/IDisplayObject";
import { execute as displayObjectApplyChangesService } from "./DisplayObject/service/DisplayObjectApplyChangesService";
import { execute as displayObjectContainerAddChildUseCase } from "./DisplayObjectContainer/usecase/DisplayObjectContainerAddChildUseCase";
import { execute as displayObjectContainerRemoveChildUseCase } from "./DisplayObjectContainer/usecase/DisplayObjectContainerRemoveChildUseCase";
import { execute as displayObjectContainerRemoveChildAtUseCase } from "./DisplayObjectContainer/usecase/DisplayObjectContainerRemoveChildAtUseCase";
import { execute as displayObjectContainerGetChildAtService } from "./DisplayObjectContainer/service/DisplayObjectContainerGetChildAtService";
import { execute as displayObjectContainerContainsService } from "./DisplayObjectContainer/service/DisplayObjectContainerContainsService";
import { execute as displayObjectContainerGetChildByNameService } from "./DisplayObjectContainer/service/DisplayObjectContainerGetChildByNameService";
import { execute as displayObjectContainerRemoveChildrenUseCase } from "./DisplayObjectContainer/usecase/DisplayObjectContainerRemoveChildrenUseCase";
import { $getArray } from "./DisplayObjectUtil";
import { InteractiveObject } from "./InteractiveObject";

Expand Down Expand Up @@ -159,43 +163,20 @@ export class DisplayObjectContainer extends InteractiveObject
}

/**
* @description 指定された表示オブジェクトが、DisplayObjectContainer インスタンスの子であるか
* インスタンス自体であるかを指定します。
* Determines whether the specified display object is a child
* of the DisplayObjectContainer instance or the instance itself.
* @description 指定された DisplayObject が、DisplayObjectContainer インスタンスの子孫であるか
* もしくは、インスタンス自体であるかを指定します。
* Whether the specified DisplayObject is a descendant of a DisplayObjectContainer instance.
* or the instance itself.
*
* @param {DisplayObject} child
* @param {DisplayObject} display_object
* @return {boolean}
* @method
* @public
*/
// contains (child: DisplayObjectImpl<any>): boolean
// {
// if (this._$instanceId === child._$instanceId) {
// return true;
// }

// const children: DisplayObjectImpl<any>[] = this._$getChildren();
// for (let idx: number = 0; idx < children.length; ++idx) {

// const instance: DisplayObjectImpl<any> = children[idx];

// if (instance._$instanceId === child._$instanceId) {
// return true;
// }

// if (instance instanceof DisplayObjectContainer) {

// if (instance.contains(child)) {
// return true;
// }

// }

// }

// return false;
// }
contains<D extends DisplayObject> (display_object: D): boolean
{
return displayObjectContainerContainsService(this, display_object);
}

/**
* @description 指定のインデックス位置にある子表示オブジェクトインスタンスを返します。
Expand All @@ -220,26 +201,10 @@ export class DisplayObjectContainer extends InteractiveObject
* @method
* @public
*/
// getChildByName (name: string): DisplayObjectImpl<any> | null
// {
// if (!name) {
// return null;
// }

// // fixed logic
// const children: DisplayObjectImpl<any>[] = this._$getChildren();
// for (let idx: number = 0; idx < children.length; ++idx) {

// const child: DisplayObjectImpl<any> = children[idx];
// if (child.name !== name) {
// continue;
// }

// return child;
// }

// return null;
// }
getChildByName<D extends DisplayObject> (name: string): D | null
{
return displayObjectContainerGetChildByNameService(this, name);
}

/**
* @description 子 DisplayObject インスタンスのインデックス位置を返します。
Expand Down Expand Up @@ -283,41 +248,22 @@ export class DisplayObjectContainer extends InteractiveObject
*/
removeChildAt (index: number): void
{
const child = this.getChildAt(index);
if (child) {
this.removeChild(child);
}
displayObjectContainerRemoveChildAtUseCase(this, index);
}

/**
* @description DisplayObjectContainer インスタンスの子リストから
* すべての child DisplayObject インスタンスを削除します。
* Removes all child DisplayObject instances from
* the child list of the DisplayObjectContainer instance.
* @description 配列で指定されたインデックスの子をコンテナから削除します
* Removes children of the index specified in the array from the container
*
* @param {number} [begin_index=0]
* @param {number} [end_index=0x7fffffff]
* @param {number[]} indexes
* @return {void}
* @method
* @public
*/
// removeChildren (
// begin_index: number = 0,
// end_index: number = 0x7fffffff
// ): void {

// const children: DisplayObjectImpl<any>[] = this._$getChildren();
// if (!children.length) {
// return ;
// }

// begin_index = $clamp(begin_index, 0, 0x7ffffffe, 0) - 1;
// end_index = $clamp(end_index, 1, 0x7ffffff, 0x7ffffff);

// for (let idx: number = $Math.min(end_index, children.length - 1); idx > begin_index; --idx) {
// this._$remove(children[idx]);
// }
// }
removeChildren (...indexes: number[]): void
{
displayObjectContainerRemoveChildrenUseCase(this, indexes);
}

/**
* @description 表示オブジェクトコンテナの既存の子の位置を変更します。
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { execute } from "./DisplayObjectContainerContainsService";
import { Shape } from "../../Shape";
import { DisplayObjectContainer } from "../../DisplayObjectContainer";
import { TextField } from "@next2d/text";
import { Video } from "@next2d/media";
import { describe, expect, it } from "vitest";

describe("DisplayObjectContainerContainsService.js test", () =>
{
it("execute test case1", () =>
{
const container = new DisplayObjectContainer();
const shape = container.addChild(new Shape());

const sprite1 = container.addChild(new DisplayObjectContainer());
const textField = sprite1.addChild(new TextField());

const sprite2 = sprite1.addChild(new DisplayObjectContainer());
const video = sprite2.addChild(new Video(100, 300));

expect(execute(container, shape)).toBe(true);
expect(execute(container, textField)).toBe(true);
expect(execute(container, video)).toBe(true);
expect(execute(container, container)).toBe(true);
expect(execute(container, sprite1)).toBe(true);
expect(execute(container, sprite2)).toBe(true);

expect(execute(sprite1, shape)).toBe(false);
expect(execute(sprite2, textField)).toBe(false);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { DisplayObject } from "../../DisplayObject";
import type { DisplayObjectContainer } from "../../DisplayObjectContainer";

/**
* @description DisplayObjectContainer とその子孫が指定の DisplayObject を含むかどうか
* Whether DisplayObjectContainer and its descendants contain the specified DisplayObject
*
* @param {DisplayObjectContainer} display_object_container
* @param {DisplayObject} display_object
* @return {boolean}
* @method
* @protected
*/
export const execute = <C extends DisplayObjectContainer, D extends DisplayObject>(
display_object_container: C,
display_object: D
): boolean => {

if (display_object_container.instanceId === display_object.instanceId) {
return true;
}

const children = display_object_container.children;
for (let idx = 0; idx < children.length; ++idx) {

const child = children[idx];
if (!child) {
continue;
}

if (child.instanceId === display_object.instanceId) {
return true;
}

if (!child.isContainerEnabled) {
continue;
}

if ((child as DisplayObjectContainer).contains(display_object)) {
return true;
}
}

return false;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { execute } from "./DisplayObjectContainerGetChildByNameService";
import { Shape } from "../../Shape";
import { DisplayObjectContainer } from "../../DisplayObjectContainer";
import { TextField } from "@next2d/text";
import { Video } from "@next2d/media";
import { describe, expect, it } from "vitest";

describe("DisplayObjectContainerGetChildByNameService.js test", () =>
{
it("execute test case1", () =>
{
const container = new DisplayObjectContainer();
const shape = container.addChild(new Shape());
shape.name = "shape";

const textField = container.addChild(new TextField());
textField.name = "textField";

const video = container.addChild(new Video(100, 300));
video.name = "video";

expect(execute(container, "")).toBe(null);
expect(execute(container, "shape")).toBe(shape);
expect(execute(container, "textField")).toBe(textField);
expect(execute(container, "video")).toBe(video);
});
});
Loading

0 comments on commit 45576cc

Please sign in to comment.