forked from galacean/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
23 changed files
with
646 additions
and
311 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
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
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
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
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,41 @@ | ||
import { PointerEventData } from "../input"; | ||
import { SafeLoopArray } from "../utils/SafeLoopArray"; | ||
import { UIInteractive } from "./interactive/UIInteractive"; | ||
|
||
export class Button extends UIInteractive { | ||
private _listeners: SafeLoopArray<IUIListener> = new SafeLoopArray<IUIListener>(); | ||
|
||
/** | ||
* Add a listening function for click. | ||
* @param listener - The listening function | ||
*/ | ||
addClicked(listener: (event: PointerEventData) => void): void { | ||
this._listeners.push({ fn: listener }); | ||
} | ||
|
||
/** | ||
* Remove a listening function of click. | ||
* @param listener - The listening function | ||
*/ | ||
removeClicked(listener: (event: PointerEventData) => void): void { | ||
this._listeners.findAndRemove((value) => (value.fn === listener ? (value.destroyed = true) : false)); | ||
} | ||
|
||
override onPointerClick(event: PointerEventData): void { | ||
const listeners = this._listeners.getLoopArray(); | ||
for (let i = 0, n = listeners.length; i < n; i++) { | ||
const listener = listeners[i]; | ||
!listener.destroyed && listener.fn(event); | ||
} | ||
} | ||
|
||
override onDestroy(): void { | ||
super.onDestroy(); | ||
this._listeners.findAndRemove((value) => (value.destroyed = true)); | ||
} | ||
} | ||
|
||
export interface IUIListener { | ||
fn: (event: PointerEventData) => void; | ||
destroyed?: boolean; | ||
} |
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
Oops, something went wrong.