Skip to content

Commit

Permalink
Simple Event System For New UI (cocos#7)
Browse files Browse the repository at this point in the history
* simple event system, step1

* add button mouse-up and mouse-down unit test

* use hitTest instead of raycast

Co-authored-by: zhangmingzhen <mingzhen.zhang@cocos.com>
  • Loading branch information
Zhmz and zhangmingzhen committed Oct 26, 2022
1 parent 5606fbc commit 2f2102c
Show file tree
Hide file tree
Showing 13 changed files with 741 additions and 15 deletions.
13 changes: 13 additions & 0 deletions cocos/new-ui/base/ui-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,17 @@ export abstract class UIComponent extends Component {
buildUITree (context) {
context.parent.addChild(this._uiElement);
}


public onEnable() {
this._registerEvent();
}

public onDisable() {
this._unregisterEvent();
}

protected abstract _registerEvent();

protected abstract _unregisterEvent();
}
32 changes: 17 additions & 15 deletions cocos/new-ui/base/ui-element.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/*
Copyright (c) 2017-2022 Xiamen Yaji Software Co., Ltd.
Expand Down Expand Up @@ -69,7 +68,6 @@ export class UIElement extends AdvancedObject {
public static MarginProperty = AdvancedProperty.register('Margin', Thickness, UIElement, Thickness.ZERO);
public static PaddingProperty = AdvancedProperty.register('Padding', Thickness, UIElement, Thickness.ZERO);


protected _slot: UISlot | null = null;
protected _parent: UIElement | null = null;
protected _children: Array<UIElement> = [];
Expand Down Expand Up @@ -97,7 +95,7 @@ export class UIElement extends AdvancedObject {
this._layout.set(val);
}
}

get margin () {
return this.getValue(UIElement.MarginProperty) as Thickness;
}
Expand All @@ -107,7 +105,7 @@ export class UIElement extends AdvancedObject {
}

get padding () {
return this.getValue(UIElement.PaddingProperty) as Thickness;
return this.getValue(UIElement.PaddingProperty) as Thickness;
}

set padding (val) {
Expand Down Expand Up @@ -138,7 +136,7 @@ export class UIElement extends AdvancedObject {
}

set visibility (val: Visibility) {
this.setValue(UIElement.VisibilityProperty, val);
this.setValue(UIElement.VisibilityProperty, val);
}

get clipToBounds () {
Expand Down Expand Up @@ -168,7 +166,7 @@ export class UIElement extends AdvancedObject {
}

set eulerAngles (val: Vec3) {
const quat = Quat.fromEuler(new Quat, val.x, val.y, val.z);
const quat = Quat.fromEuler(new Quat(), val.x, val.y, val.z);
this.rotation = quat;
}

Expand Down Expand Up @@ -211,6 +209,7 @@ export class UIElement extends AdvancedObject {
this.setValue(UIElement.RenderTransformPivotProperty, val.clone());
}


get worldTransform (): Readonly<Mat4> {
if (this._worldTransformDirty) {
Mat4.fromTranslation(this._worldTransform, new Vec3(this.layout.x, this.layout.y, 0));
Expand Down Expand Up @@ -281,7 +280,7 @@ export class UIElement extends AdvancedObject {
get document () {
return this._document;
}

//#region hierarchy
get parent () {
return this._parent;
Expand Down Expand Up @@ -319,7 +318,7 @@ export class UIElement extends AdvancedObject {

public addChild (child: UIElement) {
if (child._parent === this) {
throw new UIError(ErrorID.INVALID_INPUT)
throw new UIError(ErrorID.INVALID_INPUT);
}
child.setParent(this);
}
Expand All @@ -329,20 +328,20 @@ export class UIElement extends AdvancedObject {
throw new UIError(ErrorID.OUT_OF_RANGE);
}
if (child._parent === this) {
throw new UIError(ErrorID.INVALID_INPUT)
throw new UIError(ErrorID.INVALID_INPUT);
}
child.setParent(this, index);
}

public removeChild(child: UIElement) {
public removeChild (child: UIElement) {
if (child._parent !== this) {
throw new UIError(ErrorID.INVALID_INPUT)
throw new UIError(ErrorID.INVALID_INPUT);
}
child.setParent(null);
}

public removeChildAt(index: number) {
if (index < 0 || index > this._children.length -1) {
public removeChildAt (index: number) {
if (index < 0 || index > this._children.length - 1) {
throw new UIError(ErrorID.OUT_OF_RANGE);
}
const child = this._children[index];
Expand All @@ -353,7 +352,7 @@ export class UIElement extends AdvancedObject {
this.setParent(null);
}

private setParent (parent: UIElement | null, index: number = -1) {
private setParent (parent: UIElement | null, index = -1) {
if (parent && !parent.getSlotClass()) {
throw new UIError(ErrorID.SLOT_UNMATCHED);
}
Expand Down Expand Up @@ -384,7 +383,7 @@ export class UIElement extends AdvancedObject {
return false;
}

protected getSlotClass (): typeof UISlot | null{
protected getSlotClass (): typeof UISlot | null {
return null;
}

Expand Down Expand Up @@ -447,7 +446,10 @@ export class UIElement extends AdvancedObject {

}

//#region EventSystem
public hitTest (ray: Ray): boolean {
return true;
}
//#endregion EventSystem

}
105 changes: 105 additions & 0 deletions cocos/new-ui/component/ui-button.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
Copyright (c) 2017-2022 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

import { NodeEventType } from "../../core";
import { UIComponent } from "../base/ui-component";
import { UIElement } from "../base/ui-element";



export class UIButton extends UIComponent {
protected declare _uiElement: UIElement;


public static MOUSE_UP_COUNTER = 0;
public static MOUSE_DOWN_COUNTER =0;

constructor() {
super();
UIButton.MOUSE_DOWN_COUNTER = 0;
UIButton.MOUSE_UP_COUNTER = 0;
}

protected _registerEvent() {
this.node.on(NodeEventType.TOUCH_START, this._onTouchBegan, this);
this.node.on(NodeEventType.TOUCH_MOVE, this._onTouchMove, this);
this.node.on(NodeEventType.TOUCH_END, this._onTouchEnded, this);
this.node.on(NodeEventType.TOUCH_CANCEL, this._onTouchCancel, this);

this.node.on(NodeEventType.MOUSE_ENTER, this._onMouseMoveIn, this);
this.node.on(NodeEventType.MOUSE_LEAVE, this._onMouseMoveOut, this);
this.node.on(NodeEventType.MOUSE_UP, this._onMouseUp, this);
this.node.on(NodeEventType.MOUSE_DOWN, this._onMouseDown, this);
}

protected _unregisterEvent() {
this.node.off(NodeEventType.TOUCH_START, this._onTouchBegan, this);
this.node.off(NodeEventType.TOUCH_MOVE, this._onTouchMove, this);
this.node.off(NodeEventType.TOUCH_END, this._onTouchEnded, this);
this.node.off(NodeEventType.TOUCH_CANCEL, this._onTouchCancel, this);

this.node.off(NodeEventType.MOUSE_ENTER, this._onMouseMoveIn, this);
this.node.off(NodeEventType.MOUSE_LEAVE, this._onMouseMoveOut, this);
this.node.on(NodeEventType.MOUSE_UP, this._onMouseUp, this);
this.node.on(NodeEventType.MOUSE_DOWN, this._onMouseDown, this);
}

protected _onTouchBegan() {
console.log('_onTouchBegan');
}

protected _onTouchMove() {
console.log('_onTouchMove');
}

protected _onTouchEnded() {
console.log('_onTouchEnded');
}

protected _onTouchCancel() {
console.log('_onTouchCancel');
}

protected _onMouseMoveIn() {
//TODO: button's reaction
console.log('_onMouseMoveIn');
}

protected _onMouseMoveOut() {
//TODO: button's reaction
console.log('_onMouseMoveOut');
}
protected _onMouseDown() {
console.log('_onMouseDown');
UIButton.MOUSE_DOWN_COUNTER++;
}

protected _onMouseUp() {
console.log('_onMouseUp');
UIButton.MOUSE_UP_COUNTER++;
}


}
135 changes: 135 additions & 0 deletions cocos/new-ui/event-system/event-system.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
Copyright (c) 2017-2022 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

//import { MouseInputSource } from 'pal/input';
import { MouseInputSource } from '../../../pal/input/web';
import { Event, EventMouse } from '../../input/types';
import { InputEventType } from '../../input/types/event-enum';
import { UIElement } from '../base/ui-element';
import { BaseInputModule } from './input-modules/base-input-module';
import { RaycastResult } from './raycast-result';

export class EventSystem {
private _inputModuleList: BaseInputModule[] = [];
private _currInputModule: BaseInputModule | null = null;

// #region event
private _mouseInput = new MouseInputSource();

private _eventMouseList :EventMouse[] = [];

// #endregion

// current event-applied UIElement
private _currSelectedUIElement: UIElement | null = null;

// the threshold of dragging
private _dragPixelThreshold = 10;


get mouseInput():MouseInputSource {
return this._mouseInput;
}

get currInputModule (): BaseInputModule | null {
return this._currInputModule;
}
set currInputModule (val: BaseInputModule | null) {
this._currInputModule = val;
}

get currSelectedUIElement (): UIElement | null {
return this._currSelectedUIElement;
}
set currSelectedUIElement (val: UIElement | null) {
this._currSelectedUIElement = val;
}

get dragPixelThreshold ():number {
return this._dragPixelThreshold;
}
set dragPixelThreshold (val:number) {
this._dragPixelThreshold = val;
}

constructor () {
this._registerEvent();
}

// update input modules
public updateModules () {

}

// event data

// raycast all
public raycastAll () {

}

//raycast comparer
private static raycastComparer (left: RaycastResult, right: RaycastResult) {

}

// trigger events
public _emitEvent (event:Event) {
for (let i = 0; i < this._inputModuleList.length; i++) {
const inputModule = this._inputModuleList[i];
if (!inputModule.dispatchEvent(event)) {
// events block
break;
}
}
}

// register events(temporarily mouseEvent)
private _registerEvent () {

// TODO: touch


this._mouseInput.on(InputEventType.MOUSE_UP, (event) => {
this._emitEvent(event);
});
this._mouseInput.on(InputEventType.MOUSE_MOVE, (event) => {
this._emitEvent(event);
});
this._mouseInput.on(InputEventType.MOUSE_DOWN, (event) => {
this._emitEvent(event);
});
this._mouseInput.on(InputEventType.MOUSE_WHEEL, (event) => {
this._emitEvent(event);
});
}

public registerInputModule (inputModule:BaseInputModule) {
this._inputModuleList.push(inputModule);
this._inputModuleList.sort((a, b) => b.priority - a.priority);
}
}

export const eventSystem = new EventSystem();
2 changes: 2 additions & 0 deletions cocos/new-ui/event-system/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './input-modules';
//export * from '.';
Loading

0 comments on commit 2f2102c

Please sign in to comment.