-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#154 DisplayObjectContainerの関数を実装(WIP)
- Loading branch information
Showing
12 changed files
with
332 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
packages/display/src/DisplayObject/service/DisplayObjectRemoveService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
.../display/src/DisplayObjectContainer/service/DisplayObjectContainerContainsService.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
45 changes: 45 additions & 0 deletions
45
packages/display/src/DisplayObjectContainer/service/DisplayObjectContainerContainsService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
27 changes: 27 additions & 0 deletions
27
...ay/src/DisplayObjectContainer/service/DisplayObjectContainerGetChildByNameService.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
Oops, something went wrong.