mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-02 00:22:43 +08:00
add pageview (#4980)
This commit is contained in:
@@ -27,5 +27,7 @@ export { WidgetComponent } from './widget-component';
|
||||
export { LabelOutlineComponent } from './label-outline-component';
|
||||
export { GraphicsComponent } from './graphics-component';
|
||||
export { UIReorderComponent } from './ui-reorder-component';
|
||||
export { PageViewComponent } from './page-view-component';
|
||||
export { PageViewIndicatorComponent } from './page-view-indicator-component';
|
||||
|
||||
export { widgetManager } from './widget-manager';
|
||||
|
||||
@@ -460,7 +460,7 @@ export class LayoutComponent extends Component {
|
||||
private _spacingX = 0;
|
||||
@property
|
||||
private _spacingY = 0;
|
||||
private _layoutSize = cc.size(300, 200);
|
||||
private _layoutSize = new Size(300, 200);
|
||||
@property
|
||||
private _verticalDirection = VerticalDirection.TOP_TO_BOTTOM;
|
||||
@property
|
||||
@@ -491,7 +491,7 @@ export class LayoutComponent extends Component {
|
||||
protected onEnable () {
|
||||
this._addEventListeners();
|
||||
|
||||
if (this.node.getContentSize().equals(cc.size(0, 0))) {
|
||||
if (this.node.getContentSize().equals(new Size())) {
|
||||
this.node.setContentSize(this._layoutSize);
|
||||
}
|
||||
|
||||
@@ -539,7 +539,7 @@ export class LayoutComponent extends Component {
|
||||
for (const child of children) {
|
||||
child.on(NodeEvent.SCALE_PART, this._doScaleDirty, this);
|
||||
child.on(NodeEvent.SIZE_CHANGED, this._doLayoutDirty, this);
|
||||
child.on(NodeEvent.POSITION_PART, this._doLayoutDirty, this);
|
||||
child.on(NodeEvent.TRANSFORM_CHANGED, this._transformDirty, this);
|
||||
child.on(NodeEvent.ANCHOR_CHANGED, this._doLayoutDirty, this);
|
||||
child.on('active-in-hierarchy-changed', this._doLayoutDirty, this);
|
||||
}
|
||||
@@ -550,26 +550,26 @@ export class LayoutComponent extends Component {
|
||||
for (const child of children) {
|
||||
child.off(NodeEvent.SCALE_PART, this._doScaleDirty, this);
|
||||
child.off(NodeEvent.SIZE_CHANGED, this._doLayoutDirty, this);
|
||||
child.off(NodeEvent.POSITION_PART, this._doLayoutDirty, this);
|
||||
child.off(NodeEvent.TRANSFORM_CHANGED, this._transformDirty, this);
|
||||
child.off(NodeEvent.ANCHOR_CHANGED, this._doLayoutDirty, this);
|
||||
child.off('active-in-hierarchy-changed', this._doLayoutDirty, this);
|
||||
}
|
||||
}
|
||||
|
||||
private _childAdded (child) {
|
||||
private _childAdded (child: INode) {
|
||||
child.on(NodeEvent.SCALE_PART, this._doScaleDirty, this);
|
||||
child.on(NodeEvent.SIZE_CHANGED, this._doLayoutDirty, this);
|
||||
child.on(NodeEvent.POSITION_PART, this._doLayoutDirty, this);
|
||||
child.on(NodeEvent.TRANSFORM_CHANGED, this._transformDirty, this);
|
||||
child.on(NodeEvent.ANCHOR_CHANGED, this._doLayoutDirty, this);
|
||||
child.on('active-in-hierarchy-changed', this._doLayoutDirty, this);
|
||||
|
||||
this._doLayoutDirty();
|
||||
}
|
||||
|
||||
private _childRemoved (child) {
|
||||
private _childRemoved (child: INode) {
|
||||
child.off(NodeEvent.SCALE_PART, this._doScaleDirty, this);
|
||||
child.off(NodeEvent.SIZE_CHANGED, this._doLayoutDirty, this);
|
||||
child.off(NodeEvent.POSITION_PART, this._doLayoutDirty, this);
|
||||
child.off(NodeEvent.TRANSFORM_CHANGED, this._transformDirty, this);
|
||||
child.off(NodeEvent.ANCHOR_CHANGED, this._doLayoutDirty, this);
|
||||
child.off('active-in-hierarchy-changed', this._doLayoutDirty, this);
|
||||
|
||||
@@ -1082,6 +1082,14 @@ export class LayoutComponent extends Component {
|
||||
return this._affectedByScale ? Math.abs(value) : 1;
|
||||
}
|
||||
|
||||
private _transformDirty(type: SystemEventType){
|
||||
if(type !== SystemEventType.POSITION_PART){
|
||||
return;
|
||||
}
|
||||
|
||||
this._doLayoutDirty();
|
||||
}
|
||||
|
||||
private _doLayoutDirty () {
|
||||
this._layoutDirty = true;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,736 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
||||
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://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 { EventHandler as ComponentEventHandler } from '../../../components';
|
||||
import { ccclass, executionOrder, menu, property } from '../../../core/data/class-decorator';
|
||||
import { EventTouch, SystemEventType } from '../../../core/platform';
|
||||
import { Vec2, Vec3 } from '../../../core/math';
|
||||
import { ccenum } from '../../../core/value-types/enum';
|
||||
import { LayoutComponent } from './layout-component';
|
||||
import { PageViewIndicatorComponent } from './page-view-indicator-component';
|
||||
import { ScrollViewComponent } from './scroll-view-component';
|
||||
import { ScrollBarComponent } from './scroll-bar-component';
|
||||
import { INode } from '../../../core/utils/interfaces';
|
||||
|
||||
const _temp_vec2 = new Vec2();
|
||||
|
||||
/**
|
||||
* @en The Page View Size Mode
|
||||
* @zh 页面视图每个页面统一的大小类型
|
||||
* @enum PageView.SizeMode
|
||||
*/
|
||||
enum SizeMode {
|
||||
/**
|
||||
* @en Each page is unified in size
|
||||
* @zh 每个页面统一大小
|
||||
*/
|
||||
Unified = 0,
|
||||
/**
|
||||
* @en Each page is in free size
|
||||
* @zh 每个页面大小随意
|
||||
*/
|
||||
Free = 1,
|
||||
}
|
||||
|
||||
ccenum(SizeMode);
|
||||
|
||||
/**
|
||||
* @en The Page View Direction
|
||||
* @zh 页面视图滚动类型
|
||||
*/
|
||||
enum Direction {
|
||||
/**
|
||||
* @en Horizontal scroll.
|
||||
* @zh 水平滚动
|
||||
*/
|
||||
Horizontal = 0,
|
||||
/**
|
||||
* @en Vertical scroll.
|
||||
* @zh 垂直滚动
|
||||
*/
|
||||
Vertical = 1,
|
||||
}
|
||||
|
||||
ccenum(Direction);
|
||||
|
||||
/**
|
||||
* @en Enum for ScrollView event type.
|
||||
* @zh 滚动视图事件类型
|
||||
* @enum PageView.EventType
|
||||
*/
|
||||
enum PageViewEventType {
|
||||
/**
|
||||
* @en The page turning event
|
||||
* @zh 翻页事件
|
||||
* @property {Number} PAGE_TURNING
|
||||
*/
|
||||
PAGE_TURNING = 0,
|
||||
|
||||
}
|
||||
|
||||
ccenum(PageViewEventType);
|
||||
|
||||
/**
|
||||
* @en The PageView control
|
||||
* @zh 页面视图组件
|
||||
*/
|
||||
@ccclass('cc.PageViewComponent')
|
||||
@executionOrder(110)
|
||||
@menu('UI/PageView')
|
||||
// @ts-ignore
|
||||
export class PageViewComponent extends ScrollViewComponent {
|
||||
/**
|
||||
* @en Specify the size type of each page in PageView.
|
||||
* @zh 页面视图中每个页面大小类型
|
||||
*/
|
||||
@property({
|
||||
type: SizeMode,
|
||||
})
|
||||
get sizeMode () {
|
||||
return this._sizeMode;
|
||||
}
|
||||
|
||||
set sizeMode (value) {
|
||||
if (this._sizeMode === value) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._sizeMode = value;
|
||||
this._syncSizeMode();
|
||||
}
|
||||
|
||||
/**
|
||||
* @en The page view direction
|
||||
* @zh 页面视图滚动类型
|
||||
*/
|
||||
@property({
|
||||
type: Direction,
|
||||
})
|
||||
get direction () {
|
||||
return this._direction;
|
||||
}
|
||||
|
||||
set direction (value) {
|
||||
if (this._direction === value) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._direction = value;
|
||||
this._syncScrollDirection();
|
||||
}
|
||||
|
||||
/**
|
||||
* @en
|
||||
* The scroll threshold value, when drag exceeds this value,
|
||||
* release the next page will automatically scroll, less than the restore
|
||||
* @zh 滚动临界值,默认单位百分比,当拖拽超出该数值时,松开会自动滚动下一页,小于时则还原。
|
||||
*/
|
||||
@property({
|
||||
slide: true,
|
||||
range: [0, 1, 0.01],
|
||||
})
|
||||
get scrollThreshold () {
|
||||
return this._scrollThreshold;
|
||||
}
|
||||
|
||||
set scrollThreshold (value) {
|
||||
if (this._scrollThreshold === value) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._scrollThreshold = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Change the PageTurning event timing of PageView.
|
||||
* @zh 设置 PageView PageTurning 事件的发送时机。
|
||||
*/
|
||||
@property({
|
||||
slide: true,
|
||||
range: [0, 1, 0.01],
|
||||
})
|
||||
get pageTurningEventTiming () {
|
||||
return this._pageTurningEventTiming;
|
||||
}
|
||||
|
||||
set pageTurningEventTiming (value) {
|
||||
if (this._pageTurningEventTiming === value) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._pageTurningEventTiming = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @en The Page View Indicator
|
||||
* @zh 页面视图指示器组件
|
||||
*/
|
||||
@property({
|
||||
type: PageViewIndicatorComponent,
|
||||
})
|
||||
get indicator () {
|
||||
return this._indicator;
|
||||
}
|
||||
|
||||
set indicator (value) {
|
||||
if (this._indicator === value) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._indicator = value;
|
||||
if (this.indicator) {
|
||||
this.indicator.setPageView(this);
|
||||
}
|
||||
}
|
||||
|
||||
get curPageIdx (){
|
||||
return this._curPageIdx;
|
||||
}
|
||||
|
||||
public static SizeMode = SizeMode;
|
||||
public static Direction = Direction;
|
||||
public static EventType = PageViewEventType;
|
||||
|
||||
/**
|
||||
* @en
|
||||
* Auto page turning velocity threshold. When users swipe the PageView quickly,
|
||||
* it will calculate a velocity based on the scroll distance and time,
|
||||
* if the calculated velocity is larger than the threshold, then it will trigger page turning.
|
||||
* @zh
|
||||
* 快速滑动翻页临界值。
|
||||
* 当用户快速滑动时,会根据滑动开始和结束的距离与时间计算出一个速度值,
|
||||
* 该值与此临界值相比较,如果大于临界值,则进行自动翻页。
|
||||
*/
|
||||
@property
|
||||
public autoPageTurningThreshold = 100;
|
||||
|
||||
// visible false
|
||||
@property({
|
||||
type: ScrollBarComponent,
|
||||
visible: false,
|
||||
})
|
||||
get verticalScrollBar() {
|
||||
return this._verticalScrollBar;
|
||||
}
|
||||
|
||||
@property({
|
||||
type: ScrollBarComponent,
|
||||
visible: false,
|
||||
})
|
||||
get horizontalScrollBar() {
|
||||
return this._horizontalScrollBar;
|
||||
}
|
||||
|
||||
@property({
|
||||
visible: false,
|
||||
})
|
||||
public horizontal = true;
|
||||
|
||||
@property({
|
||||
visible: false,
|
||||
})
|
||||
public vertical = true;
|
||||
|
||||
@property({
|
||||
visible: false,
|
||||
})
|
||||
public cancelInnerEvents = true;
|
||||
|
||||
@property({
|
||||
visible: false,
|
||||
})
|
||||
public scrollEvents: ComponentEventHandler[] = [];
|
||||
|
||||
/**
|
||||
* @en The time required to turn over a page. unit: second
|
||||
* @zh 每个页面翻页时所需时间。单位:秒
|
||||
* @property {Number} pageTurningSpeed
|
||||
*/
|
||||
@property
|
||||
public pageTurningSpeed = 0.3;
|
||||
|
||||
/**
|
||||
* @en PageView events callback
|
||||
* @zh 滚动视图的事件回调函数
|
||||
*/
|
||||
@property({
|
||||
type: ComponentEventHandler,
|
||||
})
|
||||
public pageEvents: ComponentEventHandler[] = [];
|
||||
|
||||
@property
|
||||
private _sizeMode = SizeMode.Unified;
|
||||
@property
|
||||
private _direction = Direction.Horizontal;
|
||||
@property
|
||||
private _scrollThreshold = 0.5;
|
||||
@property
|
||||
private _pageTurningEventTiming = 0.1;
|
||||
@property
|
||||
private _indicator: PageViewIndicatorComponent | null = null;
|
||||
|
||||
private _curPageIdx = 0;
|
||||
private _lastPageIdx = 0;
|
||||
private _pages: INode[] = [];
|
||||
private _initContentPos = new Vec3();
|
||||
private _scrollCenterOffsetX: number[] = []; // 每一个页面居中时需要的偏移量(X)
|
||||
private _scrollCenterOffsetY: number[] = []; // 每一个页面居中时需要的偏移量(Y)
|
||||
private _touchBeganPosition = new Vec3();
|
||||
private _touchEndPosition = new Vec3();
|
||||
|
||||
public __preload () {
|
||||
this.node.on(SystemEventType.SIZE_CHANGED, this._updateAllPagesSize, this);
|
||||
}
|
||||
|
||||
public onEnable () {
|
||||
super.onEnable();
|
||||
if (!CC_EDITOR) {
|
||||
this.node.on('scroll-ended-with-threshold', this._dispatchPageTurningEvent, this);
|
||||
}
|
||||
}
|
||||
|
||||
public onDisable () {
|
||||
super.onDisable();
|
||||
if (!CC_EDITOR) {
|
||||
this.node.off('scroll-ended-with-threshold', this._dispatchPageTurningEvent, this);
|
||||
}
|
||||
}
|
||||
|
||||
public onLoad () {
|
||||
this._initPages();
|
||||
if (this.indicator) {
|
||||
this.indicator.setPageView(this);
|
||||
}
|
||||
}
|
||||
|
||||
public onDestroy () {
|
||||
this.node.off(SystemEventType.SIZE_CHANGED, this._updateAllPagesSize, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Returns current page index.
|
||||
* @zh 返回当前页面索引。
|
||||
* @returns 当前页面索引。
|
||||
*/
|
||||
public getCurrentPageIndex () {
|
||||
return this._curPageIdx;
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Set current page index.
|
||||
* @zh 设置当前页面索引。
|
||||
* @param index 索引。
|
||||
*/
|
||||
public setCurrentPageIndex (index: number) {
|
||||
this.scrollToPage(index, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Returns all pages of pageview.
|
||||
* @zh 返回视图中的所有页面。
|
||||
* @returns 输=视图所有页面。
|
||||
*/
|
||||
public getPages () {
|
||||
return this._pages;
|
||||
}
|
||||
|
||||
/**
|
||||
* @en At the end of the current page view to insert a new view.
|
||||
* @zh 在当前页面视图的尾部插入一个新视图。
|
||||
* @param page 新视图。
|
||||
*/
|
||||
public addPage (page: INode) {
|
||||
if (!page || this._pages.indexOf(page) !== -1 || !this.content) {
|
||||
return;
|
||||
}
|
||||
this.content.addChild(page);
|
||||
this._pages.push(page);
|
||||
this._updatePageView();
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Inserts a page in the specified location.
|
||||
* @zh 将页面插入指定位置中。
|
||||
* @param page 新视图。
|
||||
* @param index 指定位置。
|
||||
*/
|
||||
public insertPage (page: INode, index: number) {
|
||||
if (index < 0 || !page || this._pages.indexOf(page) !== -1 || !this.content) {
|
||||
return;
|
||||
}
|
||||
const pageCount = this._pages.length;
|
||||
if (index >= pageCount) {
|
||||
this.addPage(page);
|
||||
}
|
||||
else {
|
||||
this._pages.splice(index, 0, page);
|
||||
this.content.insertChild(page, index);
|
||||
this._updatePageView();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Removes a page from PageView.
|
||||
* @zh 移除指定页面。
|
||||
* @param page 指定页面。
|
||||
*/
|
||||
public removePage (page: INode) {
|
||||
if (!page || !this.content) { return; }
|
||||
const index = this._pages.indexOf(page);
|
||||
if (index === -1) {
|
||||
cc.warnID(4300, page.name);
|
||||
return;
|
||||
}
|
||||
this.removePageAtIndex(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Removes a page at index of PageView.
|
||||
* @zh 移除指定下标的页面。
|
||||
* @param index 页面下标。
|
||||
*/
|
||||
public removePageAtIndex (index: number) {
|
||||
const pageList = this._pages;
|
||||
if (index < 0 || index >= pageList.length) { return; }
|
||||
const page = pageList[index];
|
||||
if (!page || !this.content) { return; }
|
||||
this.content.removeChild(page);
|
||||
pageList.splice(index, 1);
|
||||
this._updatePageView();
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Removes all pages from PageView
|
||||
* @zh 移除所有页面。
|
||||
*/
|
||||
public removeAllPages () {
|
||||
if (!this.content) { return; }
|
||||
const locPages = this._pages;
|
||||
for (let i = 0, len = locPages.length; i < len; i++) {
|
||||
this.content.removeChild(locPages[i]);
|
||||
}
|
||||
this._pages.length = 0;
|
||||
this._updatePageView();
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Scroll PageView to index.
|
||||
* @zh 滚动到指定页面
|
||||
* @param idx index of page.
|
||||
* @param timeInSecond scrolling time.
|
||||
*/
|
||||
public scrollToPage (idx: number, timeInSecond = 0) {
|
||||
if (idx < 0 || idx >= this._pages.length) {
|
||||
return;
|
||||
}
|
||||
timeInSecond = timeInSecond !== undefined ? timeInSecond : 0.3;
|
||||
this._curPageIdx = idx;
|
||||
this.scrollToOffset(this._moveOffsetValue(idx), timeInSecond, true);
|
||||
if (this.indicator) {
|
||||
this.indicator._changedState();
|
||||
}
|
||||
}
|
||||
|
||||
// override the method of ScrollView
|
||||
public getScrollEndedEventTiming () {
|
||||
return this.pageTurningEventTiming;
|
||||
}
|
||||
|
||||
// 刷新页面视图
|
||||
protected _updatePageView () {
|
||||
// 当页面数组变化时修改 content 大小
|
||||
const layout = this.content!.getComponent(LayoutComponent);
|
||||
if (layout && layout.enabled) {
|
||||
layout.updateLayout();
|
||||
}
|
||||
|
||||
const pageCount = this._pages.length;
|
||||
if (this._curPageIdx >= pageCount) {
|
||||
this._curPageIdx = pageCount === 0 ? 0 : pageCount - 1;
|
||||
this._lastPageIdx = this._curPageIdx;
|
||||
}
|
||||
// 进行排序
|
||||
const contentPos = this._initContentPos;
|
||||
for (let i = 0; i < pageCount; ++i) {
|
||||
const page = this._pages[i];
|
||||
// page.setSiblingIndex(i);
|
||||
const pos = page.position;
|
||||
if (this.direction === Direction.Horizontal) {
|
||||
this._scrollCenterOffsetX[i] = Math.abs(contentPos.x + pos.x);
|
||||
}
|
||||
else {
|
||||
this._scrollCenterOffsetY[i] = Math.abs(contentPos.y + pos.y);
|
||||
}
|
||||
}
|
||||
|
||||
// 刷新 indicator 信息与状态
|
||||
if (this.indicator) {
|
||||
this.indicator._refresh();
|
||||
}
|
||||
}
|
||||
|
||||
// 刷新所有页面的大小
|
||||
protected _updateAllPagesSize () {
|
||||
if(!this.content || !this.view){
|
||||
return;
|
||||
}
|
||||
|
||||
if (this._sizeMode !== SizeMode.Unified) {
|
||||
return;
|
||||
}
|
||||
const locPages = CC_EDITOR ? this.content.children : this._pages;
|
||||
const selfSize = this.view.getContentSize();
|
||||
for (let i = 0, len = locPages.length; i < len; i++) {
|
||||
locPages[i].setContentSize(selfSize);
|
||||
}
|
||||
}
|
||||
|
||||
protected _handleReleaseLogic () {
|
||||
this._autoScrollToPage();
|
||||
if (this._scrolling) {
|
||||
this._scrolling = false;
|
||||
if (!this._autoScrolling) {
|
||||
this._dispatchEvent('scroll-ended');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected _onTouchBegan (event: EventTouch, captureListeners: any) {
|
||||
event.touch!.getUILocation(_temp_vec2);
|
||||
Vec3.set(this._touchBeganPosition, _temp_vec2.x, _temp_vec2.y, 0);
|
||||
super._onTouchBegan(event, captureListeners);
|
||||
}
|
||||
|
||||
protected _onTouchMoved (event: EventTouch, captureListeners: any) {
|
||||
super._onTouchMoved(event, captureListeners);
|
||||
}
|
||||
|
||||
protected _onTouchEnded (event: EventTouch, captureListeners: any) {
|
||||
event.touch!.getUILocation(_temp_vec2);
|
||||
Vec3.set(this._touchEndPosition, _temp_vec2.x, _temp_vec2.y, 0);
|
||||
super._onTouchEnded(event, captureListeners);
|
||||
}
|
||||
|
||||
protected _onTouchCancelled (event: EventTouch, captureListeners: any) {
|
||||
event.touch!.getUILocation(_temp_vec2);
|
||||
Vec3.set(this._touchEndPosition, _temp_vec2.x, _temp_vec2.y, 0);
|
||||
super._onTouchCancelled(event, captureListeners);
|
||||
}
|
||||
|
||||
protected _onMouseWheel () { }
|
||||
|
||||
private _syncScrollDirection () {
|
||||
this.horizontal = this.direction === Direction.Horizontal;
|
||||
this.vertical = this.direction === Direction.Vertical;
|
||||
}
|
||||
|
||||
private _syncSizeMode () {
|
||||
const view = this.view;
|
||||
if (!this.content || !view) { return; }
|
||||
const layout = this.content.getComponent(LayoutComponent);
|
||||
|
||||
if (layout) {
|
||||
if (this._sizeMode === SizeMode.Free && this._pages.length > 0) {
|
||||
const lastPage = this._pages[this._pages.length - 1];
|
||||
if (this.direction === Direction.Horizontal) {
|
||||
layout.paddingLeft = (view.width - this._pages[0].width) / 2;
|
||||
layout.paddingRight = (view.width - lastPage.width) / 2;
|
||||
}
|
||||
else if (this.direction === Direction.Vertical) {
|
||||
layout.paddingTop = (view.height - this._pages[0].height) / 2;
|
||||
layout.paddingBottom = (view.height - lastPage.height) / 2;
|
||||
}
|
||||
}
|
||||
layout.updateLayout();
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化页面
|
||||
private _initPages () {
|
||||
if (!this.content) { return; }
|
||||
this._initContentPos = this.content.position;
|
||||
const children = this.content.children;
|
||||
for (let i = 0; i < children.length; ++i) {
|
||||
const page = children[i];
|
||||
if (this._pages.indexOf(page) >= 0) { continue; }
|
||||
this._pages.push(page);
|
||||
}
|
||||
this._syncScrollDirection();
|
||||
this._syncSizeMode();
|
||||
this._updatePageView();
|
||||
}
|
||||
|
||||
private _dispatchPageTurningEvent () {
|
||||
if (this._lastPageIdx === this._curPageIdx) { return; }
|
||||
this._lastPageIdx = this._curPageIdx;
|
||||
ComponentEventHandler.emitEvents(this.pageEvents, this, PageViewEventType.PAGE_TURNING);
|
||||
this.node.emit('page-turning', this);
|
||||
}
|
||||
|
||||
// 快速滑动
|
||||
private _isQuicklyScrollable (touchMoveVelocity: Vec3) {
|
||||
if (this.direction === Direction.Horizontal) {
|
||||
if (Math.abs(touchMoveVelocity.x) > this.autoPageTurningThreshold) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if (this.direction === Direction.Vertical) {
|
||||
if (Math.abs(touchMoveVelocity.y) > this.autoPageTurningThreshold) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 通过 idx 获取偏移值数值
|
||||
private _moveOffsetValue (idx: number) {
|
||||
const offset = new Vec3();
|
||||
if (this._sizeMode === SizeMode.Free) {
|
||||
if (this.direction === Direction.Horizontal) {
|
||||
offset.x = this._scrollCenterOffsetX[idx];
|
||||
}
|
||||
else if (this.direction === Direction.Vertical) {
|
||||
offset.y = this._scrollCenterOffsetY[idx];
|
||||
}
|
||||
} else {
|
||||
const view = this.view;
|
||||
if(!view){
|
||||
return offset;
|
||||
}
|
||||
if (this.direction === Direction.Horizontal) {
|
||||
offset.x = idx * view.width;
|
||||
}
|
||||
else if (this.direction === Direction.Vertical) {
|
||||
offset.y = idx * view.height;
|
||||
}
|
||||
}
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
private _getDragDirection(moveOffset: Vec3) {
|
||||
if (this._direction === Direction.Horizontal) {
|
||||
if (moveOffset.x === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (moveOffset.x > 0 ? 1 : -1);
|
||||
}
|
||||
else {
|
||||
// 由于滚动 Y 轴的原点在在右上角所以应该是小于 0
|
||||
if (moveOffset.y === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (moveOffset.y < 0 ? 1 : -1);
|
||||
}
|
||||
}
|
||||
|
||||
// 是否超过自动滚动临界值
|
||||
private _isScrollable (offset: Vec3, index: number, nextIndex: number) {
|
||||
if (this._sizeMode === SizeMode.Free) {
|
||||
let curPageCenter = 0;
|
||||
let nextPageCenter = 0;
|
||||
if (this.direction === Direction.Horizontal) {
|
||||
curPageCenter = this._scrollCenterOffsetX[index];
|
||||
nextPageCenter = this._scrollCenterOffsetX[nextIndex];
|
||||
return Math.abs(offset.x) >= Math.abs(curPageCenter - nextPageCenter) * this.scrollThreshold;
|
||||
}
|
||||
else if (this.direction === Direction.Vertical) {
|
||||
curPageCenter = this._scrollCenterOffsetY[index];
|
||||
nextPageCenter = this._scrollCenterOffsetY[nextIndex];
|
||||
return Math.abs(offset.y) >= Math.abs(curPageCenter - nextPageCenter) * this.scrollThreshold;
|
||||
}
|
||||
}
|
||||
else {
|
||||
const view = this.view;
|
||||
if(!view){
|
||||
return;
|
||||
}
|
||||
if (this.direction === Direction.Horizontal) {
|
||||
return Math.abs(offset.x) >= view.width * this.scrollThreshold;
|
||||
}
|
||||
else if (this.direction === Direction.Vertical) {
|
||||
return Math.abs(offset.y) >= view.height * this.scrollThreshold;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private _autoScrollToPage () {
|
||||
const bounceBackStarted = this._startBounceBackIfNeeded();
|
||||
// Note:
|
||||
const moveOffset = new Vec3();
|
||||
Vec3.sub(moveOffset, this._touchBeganPosition, this._touchEndPosition);
|
||||
// this._touchBeganPosition.subtract(this._touchEndPosition);
|
||||
if (bounceBackStarted) {
|
||||
const dragDirection = this._getDragDirection(moveOffset);
|
||||
if (dragDirection === 0) {
|
||||
return;
|
||||
}
|
||||
if (dragDirection > 0) {
|
||||
this._curPageIdx = this._pages.length - 1;
|
||||
}
|
||||
else {
|
||||
this._curPageIdx = 0;
|
||||
}
|
||||
if (this.indicator) {
|
||||
this.indicator._changedState();
|
||||
}
|
||||
}
|
||||
else {
|
||||
const index = this._curPageIdx;
|
||||
const nextIndex = index + this._getDragDirection(moveOffset);
|
||||
const timeInSecond = this.pageTurningSpeed * Math.abs(index - nextIndex);
|
||||
if (nextIndex < this._pages.length) {
|
||||
if (this._isScrollable(moveOffset, index, nextIndex)) {
|
||||
this.scrollToPage(nextIndex, timeInSecond);
|
||||
return;
|
||||
}
|
||||
else {
|
||||
const touchMoveVelocity = this._calculateTouchMoveVelocity();
|
||||
if (this._isQuicklyScrollable(touchMoveVelocity)) {
|
||||
this.scrollToPage(nextIndex, timeInSecond);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.scrollToPage(index, timeInSecond);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
cc.PageViewComponent = PageViewComponent;
|
||||
|
||||
/**
|
||||
* @en
|
||||
* Note: This event is emitted from the node to which the component belongs.
|
||||
* @zh
|
||||
* 注意:此事件是从该组件所属的 Node 上面派发出来的,需要用 node.on 来监听。
|
||||
* @event page-turning
|
||||
* @param {Event.EventCustom} event
|
||||
* @param {PageView} pageView - The PageView component.
|
||||
*/
|
||||
@@ -0,0 +1,243 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
||||
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://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 { SpriteFrame } from '../../../assets';
|
||||
import { Component } from '../../../components';
|
||||
import { ccclass, executionOrder, menu, property } from '../../../core/data/class-decorator';
|
||||
import { Color, Size } from '../../../core/math';
|
||||
import { ccenum } from '../../../core/value-types/enum';
|
||||
import { Node } from '../../../scene-graph';
|
||||
import { LayoutComponent } from './layout-component';
|
||||
import { PageViewComponent } from './page-view-component';
|
||||
import { SpriteComponent } from './sprite-component';
|
||||
import { UIRenderComponent } from './ui-render-component';
|
||||
import { INode } from '../../../core/utils/interfaces';
|
||||
|
||||
const _color = new Color();
|
||||
|
||||
/**
|
||||
* @en Enum for PageView Indicator direction
|
||||
* @zh 页面视图指示器的摆放方向
|
||||
* @enum PageViewIndicator.Direction
|
||||
*/
|
||||
enum Direction {
|
||||
/**
|
||||
* @en The horizontal direction.
|
||||
* @zh 水平方向
|
||||
*/
|
||||
HORIZONTAL = 0,
|
||||
|
||||
/**
|
||||
* @en The vertical direction.
|
||||
* @zh 垂直方向
|
||||
*/
|
||||
VERTICAL = 1,
|
||||
}
|
||||
|
||||
ccenum(Direction);
|
||||
|
||||
/**
|
||||
* @en The Page View Indicator Component
|
||||
* @zh 页面视图每页标记组件
|
||||
*/
|
||||
@ccclass('cc.PageViewIndicatorComponent')
|
||||
@executionOrder(110)
|
||||
@menu('UI/PageViewIndicator')
|
||||
export class PageViewIndicatorComponent extends Component {
|
||||
/**
|
||||
* @en The spriteFrame for each element.
|
||||
* @zh 每个页面标记显示的图片
|
||||
*/
|
||||
@property({
|
||||
type: SpriteFrame,
|
||||
})
|
||||
get spriteFrame () {
|
||||
return this._spriteFrame;
|
||||
}
|
||||
|
||||
set spriteFrame (value) {
|
||||
if (this._spriteFrame === value) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._spriteFrame = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @en The location direction of PageViewIndicator.
|
||||
* @zh 页面标记摆放方向
|
||||
* @param direction 摆放方向
|
||||
*/
|
||||
@property({
|
||||
type: Direction,
|
||||
})
|
||||
get direction () {
|
||||
return this._direction;
|
||||
}
|
||||
|
||||
set direction (value) {
|
||||
if (this._direction === value) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._direction = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @en The cellSize for each element.
|
||||
* @zh 每个页面标记的大小
|
||||
*/
|
||||
@property({
|
||||
type: Size,
|
||||
})
|
||||
get cellSize () {
|
||||
return this._cellSize;
|
||||
}
|
||||
|
||||
set cellSize (value) {
|
||||
if (this._cellSize === value) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._cellSize = value;
|
||||
}
|
||||
|
||||
public static Direction = Direction;
|
||||
|
||||
/**
|
||||
* @en The distance between each element.
|
||||
* @zh 每个页面标记之间的边距
|
||||
*/
|
||||
@property
|
||||
public spacing = 0;
|
||||
@property
|
||||
public _spriteFrame: SpriteFrame | null = null;
|
||||
@property
|
||||
public _direction: Direction = Direction.HORIZONTAL;
|
||||
@property
|
||||
public _cellSize = new Size(20, 20);
|
||||
private _layout: LayoutComponent | null = null;
|
||||
private _pageView: PageViewComponent | null = null;
|
||||
private _indicators: Node[] = [];
|
||||
|
||||
public onLoad () {
|
||||
this._updateLayout();
|
||||
}
|
||||
|
||||
/**
|
||||
* @en Set Page View
|
||||
* @zh 设置页面视图
|
||||
* @param target 页面视图对象
|
||||
*/
|
||||
public setPageView (target: PageViewComponent) {
|
||||
this._pageView = target;
|
||||
this._refresh();
|
||||
}
|
||||
|
||||
public _updateLayout () {
|
||||
this._layout = this.getComponent(LayoutComponent);
|
||||
if (!this._layout) {
|
||||
this._layout = this.addComponent(LayoutComponent);
|
||||
}
|
||||
|
||||
const layout = this._layout!;
|
||||
if (this.direction === Direction.HORIZONTAL) {
|
||||
layout.type = LayoutComponent.Type.HORIZONTAL;
|
||||
layout.spacingX = this.spacing;
|
||||
}
|
||||
else if (this.direction === Direction.VERTICAL) {
|
||||
layout.type = LayoutComponent.Type.VERTICAL;
|
||||
layout.spacingY = this.spacing;
|
||||
}
|
||||
layout.resizeMode = LayoutComponent.ResizeMode.CONTAINER;
|
||||
}
|
||||
|
||||
public _createIndicator () {
|
||||
const node = new Node();
|
||||
const sprite = node.addComponent(SpriteComponent);
|
||||
sprite!.spriteFrame = this.spriteFrame;
|
||||
node.parent = this.node as unknown as Node;
|
||||
node.width = this.cellSize.width;
|
||||
node.height = this.cellSize.height;
|
||||
return node;
|
||||
}
|
||||
|
||||
public _changedState () {
|
||||
const indicators = this._indicators;
|
||||
if (indicators.length === 0 || !this._pageView) { return; }
|
||||
const idx = this._pageView.curPageIdx;
|
||||
if (idx >= indicators.length) { return; }
|
||||
for (let i = 0; i < indicators.length; ++i) {
|
||||
const node = indicators[i];
|
||||
if (!node._uiComp) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const uiComp = node._uiComp as UIRenderComponent;
|
||||
_color.set(uiComp.color);
|
||||
_color.a = 255 / 2;
|
||||
uiComp.color = _color;
|
||||
}
|
||||
|
||||
if (indicators[idx]._uiComp) {
|
||||
const comp = indicators[idx]._uiComp as UIRenderComponent;
|
||||
_color.set(comp.color);
|
||||
_color.a = 255;
|
||||
comp.color = _color;
|
||||
}
|
||||
}
|
||||
|
||||
public _refresh () {
|
||||
if (!this._pageView) { return; }
|
||||
const indicators = this._indicators;
|
||||
const pages = this._pageView.getPages();
|
||||
if (pages.length === indicators.length) {
|
||||
return;
|
||||
}
|
||||
let i = 0;
|
||||
if (pages.length > indicators.length) {
|
||||
for (i = 0; i < pages.length; ++i) {
|
||||
if (!indicators[i]) {
|
||||
indicators[i] = this._createIndicator();
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
const count = indicators.length - pages.length;
|
||||
for (i = count; i > 0; --i) {
|
||||
const node = indicators[i - 1];
|
||||
this.node.removeChild(node);
|
||||
indicators.splice(i - 1, 1);
|
||||
}
|
||||
}
|
||||
if (this._layout && this._layout.enabledInHierarchy) {
|
||||
this._layout.updateLayout();
|
||||
}
|
||||
this._changedState();
|
||||
}
|
||||
}
|
||||
|
||||
cc.PageViewIndicatorComponent = PageViewIndicatorComponent;
|
||||
@@ -64,7 +64,7 @@ const getTimeInMilliseconds = () => {
|
||||
* @zh
|
||||
* 滚动视图事件类型。
|
||||
*/
|
||||
enum EventType {
|
||||
enum ScrollViewEventType {
|
||||
/**
|
||||
* @zh
|
||||
* 滚动视图滚动到顶部边界事件。
|
||||
@@ -132,22 +132,22 @@ enum EventType {
|
||||
SCROLL_BEGAN = 12,
|
||||
}
|
||||
|
||||
ccenum(EventType);
|
||||
ccenum(ScrollViewEventType);
|
||||
|
||||
const eventMap = {
|
||||
'scroll-to-top': EventType.SCROLL_TO_TOP,
|
||||
'scroll-to-bottom': EventType.SCROLL_TO_BOTTOM,
|
||||
'scroll-to-left': EventType.SCROLL_TO_LEFT,
|
||||
'scroll-to-right': EventType.SCROLL_TO_RIGHT,
|
||||
'scrolling': EventType.SCROLLING,
|
||||
'bounce-bottom': EventType.BOUNCE_BOTTOM,
|
||||
'bounce-left': EventType.BOUNCE_LEFT,
|
||||
'bounce-right': EventType.BOUNCE_RIGHT,
|
||||
'bounce-top': EventType.BOUNCE_TOP,
|
||||
'scroll-ended': EventType.SCROLL_ENDED,
|
||||
'touch-up': EventType.TOUCH_UP,
|
||||
'scroll-ended-with-threshold': EventType.AUTOSCROLL_ENDED_WITH_THRESHOLD,
|
||||
'scroll-began': EventType.SCROLL_BEGAN,
|
||||
'scroll-to-top': ScrollViewEventType.SCROLL_TO_TOP,
|
||||
'scroll-to-bottom': ScrollViewEventType.SCROLL_TO_BOTTOM,
|
||||
'scroll-to-left': ScrollViewEventType.SCROLL_TO_LEFT,
|
||||
'scroll-to-right': ScrollViewEventType.SCROLL_TO_RIGHT,
|
||||
'scrolling': ScrollViewEventType.SCROLLING,
|
||||
'bounce-bottom': ScrollViewEventType.BOUNCE_BOTTOM,
|
||||
'bounce-left': ScrollViewEventType.BOUNCE_LEFT,
|
||||
'bounce-right': ScrollViewEventType.BOUNCE_RIGHT,
|
||||
'bounce-top': ScrollViewEventType.BOUNCE_TOP,
|
||||
'scroll-ended': ScrollViewEventType.SCROLL_ENDED,
|
||||
'touch-up': ScrollViewEventType.TOUCH_UP,
|
||||
'scroll-ended-with-threshold': ScrollViewEventType.AUTOSCROLL_ENDED_WITH_THRESHOLD,
|
||||
'scroll-began': ScrollViewEventType.SCROLL_BEGAN,
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -237,7 +237,7 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
return this._content.parent;
|
||||
}
|
||||
|
||||
public static EventType = EventType;
|
||||
public static EventType = ScrollViewEventType;
|
||||
/**
|
||||
* @zh
|
||||
* 是否开启水平滚动。
|
||||
@@ -288,9 +288,7 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
* @zh
|
||||
* 滚动视图的事件回调函数。
|
||||
*/
|
||||
@property({
|
||||
type: ComponentEventHandler,
|
||||
})
|
||||
@property
|
||||
public scrollEvents: ComponentEventHandler[] = [];
|
||||
|
||||
/**
|
||||
@@ -300,12 +298,15 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
*/
|
||||
@property
|
||||
public cancelInnerEvents = true;
|
||||
|
||||
protected _autoScrolling = false;
|
||||
protected _scrolling = false;
|
||||
@property
|
||||
private _content: INode | null = null;
|
||||
@property
|
||||
private _horizontalScrollBar: ScrollBarComponent | null = null;
|
||||
protected _horizontalScrollBar: ScrollBarComponent | null = null;
|
||||
@property
|
||||
private _verticalScrollBar: ScrollBarComponent | null = null;
|
||||
protected _verticalScrollBar: ScrollBarComponent | null = null;
|
||||
|
||||
private _topBoundary = 0;
|
||||
private _bottomBoundary = 0;
|
||||
@@ -316,8 +317,6 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
private _touchMoveTimeDeltas: number[] = [];
|
||||
private _touchMovePreviousTimestamp = 0;
|
||||
private _touchMoved = false;
|
||||
|
||||
private _autoScrolling = false;
|
||||
private _autoScrollAttenuate = false;
|
||||
private _autoScrollStartPosition = new Vec3();
|
||||
private _autoScrollTargetDelta = new Vec3();
|
||||
@@ -335,7 +334,6 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
// use bit wise operations to indicate the direction
|
||||
private _scrollEventEmitMask = 0;
|
||||
private _isBouncing = false;
|
||||
private _scrolling = false;
|
||||
private _contentPos = new Vec3();
|
||||
private _deltaPos = new Vec3();
|
||||
|
||||
@@ -806,7 +804,7 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
}
|
||||
|
||||
// private methods
|
||||
private _registerEvent () {
|
||||
protected _registerEvent () {
|
||||
this.node.on(NodeEvent.TOUCH_START, this._onTouchBegan, this, true);
|
||||
this.node.on(NodeEvent.TOUCH_MOVE, this._onTouchMoved, this, true);
|
||||
this.node.on(NodeEvent.TOUCH_END, this._onTouchEnded, this, true);
|
||||
@@ -814,7 +812,7 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
this.node.on(NodeEvent.MOUSE_WHEEL, this._onMouseWheel, this, true);
|
||||
}
|
||||
|
||||
private _unregisterEvent () {
|
||||
protected _unregisterEvent () {
|
||||
this.node.off(NodeEvent.TOUCH_START, this._onTouchBegan, this, true);
|
||||
this.node.off(NodeEvent.TOUCH_MOVE, this._onTouchMoved, this, true);
|
||||
this.node.off(NodeEvent.TOUCH_END, this._onTouchEnded, this, true);
|
||||
@@ -829,7 +827,7 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
* @param event - 鼠标事件信息。
|
||||
* @param captureListeners
|
||||
*/
|
||||
private _onMouseWheel (event: EventMouse, captureListeners?: any) {
|
||||
protected _onMouseWheel (event: EventMouse, captureListeners?: any) {
|
||||
if (!this.enabledInHierarchy) {
|
||||
return;
|
||||
}
|
||||
@@ -863,7 +861,7 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
}
|
||||
|
||||
// touch event handler
|
||||
private _onTouchBegan (event?: EventTouch, captureListeners?: any) {
|
||||
protected _onTouchBegan (event: EventTouch, captureListeners?: any) {
|
||||
if (!this.enabledInHierarchy || !this._content) { return; }
|
||||
if (this._hasNestedViewGroup(event, captureListeners)) { return; }
|
||||
|
||||
@@ -875,7 +873,7 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
this._stopPropagationIfTargetIsMe(event);
|
||||
}
|
||||
|
||||
private _onTouchMoved (event?: EventTouch, captureListeners?: any) {
|
||||
protected _onTouchMoved (event: EventTouch, captureListeners?: any) {
|
||||
if (!this.enabledInHierarchy || !this._content) { return; }
|
||||
if (this._hasNestedViewGroup(event, captureListeners)) { return; }
|
||||
|
||||
@@ -909,7 +907,7 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
this._stopPropagationIfTargetIsMe(event);
|
||||
}
|
||||
|
||||
private _onTouchEnded (event?: EventTouch, captureListeners?: any) {
|
||||
protected _onTouchEnded (event: EventTouch, captureListeners?: any) {
|
||||
if (!this.enabledInHierarchy || !this._content || !event) { return; }
|
||||
if (this._hasNestedViewGroup(event, captureListeners)) { return; }
|
||||
|
||||
@@ -926,7 +924,7 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
}
|
||||
}
|
||||
|
||||
private _onTouchCancelled (event?: EventTouch, captureListeners?: any) {
|
||||
protected _onTouchCancelled (event: EventTouch, captureListeners?: any) {
|
||||
if (!this.enabledInHierarchy || !this._content) { return; }
|
||||
if (this._hasNestedViewGroup(event, captureListeners)) { return; }
|
||||
|
||||
@@ -944,7 +942,7 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
* @zh
|
||||
* 重新计算内容活动边界(view)
|
||||
*/
|
||||
private _calculateBoundary () {
|
||||
protected _calculateBoundary () {
|
||||
if (this.content) {
|
||||
// refresh content size
|
||||
const layout = this.content.getComponent(LayoutComponent);
|
||||
@@ -968,7 +966,7 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
}
|
||||
|
||||
// this is for nested scrollview
|
||||
private _hasNestedViewGroup (event?: Event, captureListeners?: any) {
|
||||
protected _hasNestedViewGroup (event?: Event, captureListeners?: any) {
|
||||
if (!event || event.eventPhase !== cc.Event.CAPTURING_PHASE) {
|
||||
return;
|
||||
}
|
||||
@@ -993,13 +991,27 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
return false;
|
||||
}
|
||||
|
||||
private _startInertiaScroll (touchMoveVelocity: Vec3) {
|
||||
protected _handleReleaseLogic (touch) {
|
||||
const delta = touch.getUIDelta();
|
||||
Vec3.set(this._deltaPos, delta.x, delta.y, 0);
|
||||
this._gatherTouchMove(this._deltaPos);
|
||||
this._processInertiaScroll();
|
||||
|
||||
if (this._scrolling) {
|
||||
this._scrolling = false;
|
||||
if (!this._autoScrolling) {
|
||||
this._dispatchEvent('scroll-ended');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected _startInertiaScroll (touchMoveVelocity: Vec3) {
|
||||
const inertiaTotalMovement = new Vec3(touchMoveVelocity);
|
||||
inertiaTotalMovement.scale(MOVEMENT_FACTOR);
|
||||
this._startAttenuatingAutoScroll(inertiaTotalMovement, touchMoveVelocity);
|
||||
}
|
||||
|
||||
private _calculateAttenuatedFactor (distance: number) {
|
||||
protected _calculateAttenuatedFactor (distance: number) {
|
||||
if (this.brake <= 0) {
|
||||
return (1 - this.brake);
|
||||
}
|
||||
@@ -1008,10 +1020,10 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
return (1 - this.brake) * (1 / (1 + distance * 0.000014 + distance * distance * 0.000000008));
|
||||
}
|
||||
|
||||
private _startAttenuatingAutoScroll (deltaMove: Vec3, initialVelocity: Vec3) {
|
||||
protected _startAttenuatingAutoScroll (deltaMove: Vec3, initialVelocity: Vec3) {
|
||||
let time = this._calculateAutoScrollTimeByInitalSpeed(initialVelocity.mag());
|
||||
|
||||
let targetDelta = new Vec3(deltaMove);
|
||||
const targetDelta = new Vec3(deltaMove);
|
||||
targetDelta.normalize();
|
||||
const contentSize = this._content!.getContentSize();
|
||||
const scrollviewSize = this.node.getContentSize();
|
||||
@@ -1050,11 +1062,11 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
this._startAutoScroll(targetDelta, time, true);
|
||||
}
|
||||
|
||||
private _calculateAutoScrollTimeByInitalSpeed (initalSpeed) {
|
||||
protected _calculateAutoScrollTimeByInitalSpeed (initalSpeed) {
|
||||
return Math.sqrt(Math.sqrt(initalSpeed / 5));
|
||||
}
|
||||
|
||||
private _startAutoScroll (deltaMove: Vec3, timeInSecond: number, attenuated: boolean = false) {
|
||||
protected _startAutoScroll (deltaMove: Vec3, timeInSecond: number, attenuated: boolean = false) {
|
||||
const adjustedDeltaMove = this._flattenVectorByDirection(deltaMove);
|
||||
|
||||
this._autoScrolling = true;
|
||||
@@ -1073,7 +1085,7 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
}
|
||||
}
|
||||
|
||||
private _calculateTouchMoveVelocity () {
|
||||
protected _calculateTouchMoveVelocity () {
|
||||
let totalTime = 0;
|
||||
totalTime = this._touchMoveTimeDeltas.reduce((a, b) => {
|
||||
return a + b;
|
||||
@@ -1093,14 +1105,14 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
totalMovement.y * (1 - this.brake) / totalTime, 0);
|
||||
}
|
||||
|
||||
private _flattenVectorByDirection (vector: Vec3) {
|
||||
protected _flattenVectorByDirection (vector: Vec3) {
|
||||
const result = vector;
|
||||
result.x = this.horizontal ? result.x : 0;
|
||||
result.y = this.vertical ? result.y : 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
private _moveContent (deltaMove: Vec3, canStartBounceBack?: boolean) {
|
||||
protected _moveContent (deltaMove: Vec3, canStartBounceBack?: boolean) {
|
||||
const adjustedMove = this._flattenVectorByDirection(deltaMove);
|
||||
_tempPos.set(this.getContentPosition());
|
||||
_tempPos.add(adjustedMove);
|
||||
@@ -1115,27 +1127,27 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
}
|
||||
}
|
||||
|
||||
private _getContentLeftBoundary () {
|
||||
protected _getContentLeftBoundary () {
|
||||
const contentPos = this.getContentPosition();
|
||||
return contentPos.x - this._content!.getAnchorPoint().x * this._content!.getContentSize().width;
|
||||
}
|
||||
|
||||
private _getContentRightBoundary () {
|
||||
protected _getContentRightBoundary () {
|
||||
const contentSize = this._content!.getContentSize();
|
||||
return this._getContentLeftBoundary() + contentSize.width;
|
||||
}
|
||||
|
||||
private _getContentTopBoundary () {
|
||||
protected _getContentTopBoundary () {
|
||||
const contentSize = this._content!.getContentSize();
|
||||
return this._getContentBottomBoundary() + contentSize.height;
|
||||
}
|
||||
|
||||
private _getContentBottomBoundary () {
|
||||
protected _getContentBottomBoundary () {
|
||||
const contentPos = this.getContentPosition();
|
||||
return contentPos.y - this._content!.getAnchorPoint().y * this._content!.getContentSize().height;
|
||||
}
|
||||
|
||||
private _getHowMuchOutOfBoundary (addition?: Vec3) {
|
||||
protected _getHowMuchOutOfBoundary (addition?: Vec3) {
|
||||
addition = addition || new Vec3();
|
||||
if (addition.equals(ZERO, EPSILON) && !this._outOfBoundaryAmountDirty) {
|
||||
return this._outOfBoundaryAmount;
|
||||
@@ -1163,7 +1175,7 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
return outOfBoundaryAmount;
|
||||
}
|
||||
|
||||
private _updateScrollBar (outOfBoundary: Vec3) {
|
||||
protected _updateScrollBar (outOfBoundary: Vec3) {
|
||||
if (this._horizontalScrollBar) {
|
||||
this._horizontalScrollBar.onScroll(outOfBoundary);
|
||||
}
|
||||
@@ -1173,7 +1185,7 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
}
|
||||
}
|
||||
|
||||
private _onScrollBarTouchBegan () {
|
||||
protected _onScrollBarTouchBegan () {
|
||||
if (this._horizontalScrollBar) {
|
||||
this._horizontalScrollBar.onTouchBegan();
|
||||
}
|
||||
@@ -1183,7 +1195,7 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
}
|
||||
}
|
||||
|
||||
private _onScrollBarTouchEnded () {
|
||||
protected _onScrollBarTouchEnded () {
|
||||
if (this._horizontalScrollBar) {
|
||||
this._horizontalScrollBar.onTouchEnded();
|
||||
}
|
||||
@@ -1193,7 +1205,7 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
}
|
||||
}
|
||||
|
||||
private _dispatchEvent (event) {
|
||||
protected _dispatchEvent (event) {
|
||||
if (event === 'scroll-ended') {
|
||||
this._scrollEventEmitMask = 0;
|
||||
|
||||
@@ -1214,7 +1226,7 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
this.node.emit(event, this);
|
||||
}
|
||||
|
||||
private _adjustContentOutOfBoundary () {
|
||||
protected _adjustContentOutOfBoundary () {
|
||||
if (!this._content){
|
||||
return;
|
||||
}
|
||||
@@ -1222,7 +1234,7 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
this._outOfBoundaryAmountDirty = true;
|
||||
if (this._isOutOfBoundary()) {
|
||||
const outOfBoundary = this._getHowMuchOutOfBoundary(new Vec3());
|
||||
let newPosition = new Vec3(this.getContentPosition());
|
||||
const newPosition = new Vec3(this.getContentPosition());
|
||||
newPosition.add(outOfBoundary);
|
||||
if (this._content) {
|
||||
this._content.setPosition(newPosition);
|
||||
@@ -1232,7 +1244,7 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
}
|
||||
}
|
||||
|
||||
private _hideScrollbar () {
|
||||
protected _hideScrollbar () {
|
||||
if (this._horizontalScrollBar) {
|
||||
this._horizontalScrollBar.hide();
|
||||
}
|
||||
@@ -1242,7 +1254,7 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
}
|
||||
}
|
||||
|
||||
private _showScrollbar () {
|
||||
protected _showScrollbar () {
|
||||
if (this._horizontalScrollBar) {
|
||||
this._horizontalScrollBar.show();
|
||||
}
|
||||
@@ -1253,27 +1265,27 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
}
|
||||
|
||||
// This is for Scrollview as children of a Button
|
||||
private _stopPropagationIfTargetIsMe (event?: Event) {
|
||||
protected _stopPropagationIfTargetIsMe (event?: Event) {
|
||||
if (event && event.eventPhase === cc.Event.AT_TARGET && event.target === this.node) {
|
||||
event.propagationStopped = true;
|
||||
}
|
||||
}
|
||||
|
||||
private _processDeltaMove (deltaMove: Vec3) {
|
||||
protected _processDeltaMove (deltaMove: Vec3) {
|
||||
this._scrollChildren(deltaMove);
|
||||
this._gatherTouchMove(deltaMove);
|
||||
}
|
||||
|
||||
private _handleMoveLogic (touch: Touch) {
|
||||
protected _handleMoveLogic (touch: Touch) {
|
||||
const delta = touch.getUIDelta();
|
||||
Vec3.set(this._deltaPos, delta.x, delta.y, 0);
|
||||
this._processDeltaMove(this._deltaPos);
|
||||
}
|
||||
|
||||
private _scrollChildren (deltaMove: Vec3) {
|
||||
protected _scrollChildren (deltaMove: Vec3) {
|
||||
deltaMove = this._clampDelta(deltaMove);
|
||||
|
||||
let realMove = deltaMove;
|
||||
const realMove = deltaMove;
|
||||
let outOfBoundary;
|
||||
if (this.elastic) {
|
||||
outOfBoundary = this._getHowMuchOutOfBoundary();
|
||||
@@ -1333,7 +1345,7 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
|
||||
}
|
||||
|
||||
private _handlePressLogic () {
|
||||
protected _handlePressLogic () {
|
||||
if (this._autoScrolling) {
|
||||
this._dispatchEvent('scroll-ended');
|
||||
}
|
||||
@@ -1347,7 +1359,7 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
this._onScrollBarTouchBegan();
|
||||
}
|
||||
|
||||
private _clampDelta (delta: Vec3) {
|
||||
protected _clampDelta (delta: Vec3) {
|
||||
const contentSize = this._content!.getContentSize();
|
||||
const scrollViewSize = this.node.getContentSize();
|
||||
if (contentSize.width < scrollViewSize.width) {
|
||||
@@ -1360,7 +1372,7 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
return delta;
|
||||
}
|
||||
|
||||
private _gatherTouchMove (delta: Vec3) {
|
||||
protected _gatherTouchMove (delta: Vec3) {
|
||||
delta = this._clampDelta(delta);
|
||||
|
||||
while (this._touchMoveDisplacements.length >= NUMBER_OF_GATHERED_TOUCHES_FOR_MOVE_SPEED) {
|
||||
@@ -1375,7 +1387,7 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
this._touchMovePreviousTimestamp = timeStamp;
|
||||
}
|
||||
|
||||
private _startBounceBackIfNeeded () {
|
||||
protected _startBounceBackIfNeeded () {
|
||||
if (!this.elastic) {
|
||||
return false;
|
||||
}
|
||||
@@ -1383,7 +1395,7 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
let bounceBackAmount = this._getHowMuchOutOfBoundary();
|
||||
bounceBackAmount = this._clampDelta(bounceBackAmount);
|
||||
|
||||
if (bounceBackAmount.equals(_tempPos, EPSILON)) {
|
||||
if (bounceBackAmount.equals(ZERO, EPSILON)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1401,7 +1413,7 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
return true;
|
||||
}
|
||||
|
||||
private _processInertiaScroll () {
|
||||
protected _processInertiaScroll () {
|
||||
const bounceBackStarted = this._startBounceBackIfNeeded();
|
||||
if (!bounceBackStarted && this.inertia) {
|
||||
const touchMoveVelocity = this._calculateTouchMoveVelocity();
|
||||
@@ -1413,26 +1425,12 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
this._onScrollBarTouchEnded();
|
||||
}
|
||||
|
||||
private _handleReleaseLogic (touch) {
|
||||
const delta = touch.getUIDelta();
|
||||
Vec3.set(this._deltaPos, delta.x, delta.y, 0);
|
||||
this._gatherTouchMove(this._deltaPos);
|
||||
this._processInertiaScroll();
|
||||
|
||||
if (this._scrolling) {
|
||||
this._scrolling = false;
|
||||
if (!this._autoScrolling) {
|
||||
this._dispatchEvent('scroll-ended');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private _isOutOfBoundary () {
|
||||
protected _isOutOfBoundary () {
|
||||
const outOfBoundary = this._getHowMuchOutOfBoundary();
|
||||
return !outOfBoundary.equals(ZERO, EPSILON);
|
||||
}
|
||||
|
||||
private _isNecessaryAutoScrollBrake () {
|
||||
protected _isNecessaryAutoScrollBrake () {
|
||||
if (this._autoScrollBraking) {
|
||||
return true;
|
||||
}
|
||||
@@ -1452,7 +1450,7 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
return false;
|
||||
}
|
||||
|
||||
private _processAutoScrolling (dt) {
|
||||
protected _processAutoScrolling (dt) {
|
||||
const isAutoScrollBrake = this._isNecessaryAutoScrollBrake();
|
||||
const brakingFactor = isAutoScrollBrake ? OUT_OF_BOUNDARY_BREAKING_FACTOR : 1;
|
||||
this._autoScrollAccumulatedTime += dt * (1 / brakingFactor);
|
||||
@@ -1462,9 +1460,9 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
percentage = quintEaseOut(percentage);
|
||||
}
|
||||
|
||||
let a = new Vec3(this._autoScrollTargetDelta);
|
||||
const a = new Vec3(this._autoScrollTargetDelta);
|
||||
a.scale(percentage);
|
||||
let newPosition = new Vec3(this._autoScrollStartPosition);
|
||||
const newPosition = new Vec3(this._autoScrollStartPosition);
|
||||
newPosition.add(a);
|
||||
let reachedEnd = Math.abs(percentage - 1) <= EPSILON;
|
||||
|
||||
@@ -1475,7 +1473,7 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
}
|
||||
|
||||
if (this.elastic) {
|
||||
let brakeOffsetPosition = new Vec3(newPosition);
|
||||
const brakeOffsetPosition = new Vec3(newPosition);
|
||||
brakeOffsetPosition.subtract(this._autoScrollBrakingStartPosition);
|
||||
if (isAutoScrollBrake) {
|
||||
brakeOffsetPosition.scale(brakingFactor);
|
||||
@@ -1508,7 +1506,7 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
}
|
||||
}
|
||||
|
||||
private _checkMouseWheel (dt: number) {
|
||||
protected _checkMouseWheel (dt: number) {
|
||||
const currentOutOfBoundary = this._getHowMuchOutOfBoundary();
|
||||
const maxElapsedTime = 0.1;
|
||||
|
||||
@@ -1529,8 +1527,8 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
}
|
||||
}
|
||||
|
||||
private _calculateMovePercentDelta (options) {
|
||||
let anchor = options.anchor;
|
||||
protected _calculateMovePercentDelta (options) {
|
||||
const anchor = options.anchor;
|
||||
const applyToHorizontal = options.applyToHorizontal;
|
||||
const applyToVertical = options.applyToVertical;
|
||||
this._calculateBoundary();
|
||||
@@ -1566,7 +1564,7 @@ export class ScrollViewComponent extends ViewGroupComponent {
|
||||
*
|
||||
* @param scrollViewSize - 可视区域尺寸。
|
||||
*/
|
||||
private _moveContentToTopLeft (scrollViewSize: Size) {
|
||||
protected _moveContentToTopLeft (scrollViewSize: Size) {
|
||||
const contentSize = this._content!.getContentSize();
|
||||
|
||||
let bottomDeta = this._getContentBottomBoundary() - this._bottomBoundary;
|
||||
|
||||
@@ -598,11 +598,15 @@ export class SpriteComponent extends UIRenderComponent {
|
||||
// this.markForRender(false);
|
||||
// }
|
||||
|
||||
if (oldFrame && spriteFrame) {
|
||||
this._renderData!.uvDirty = oldFrame.uvHash !== spriteFrame.uvHash;
|
||||
}
|
||||
else {
|
||||
this._renderData!.uvDirty = true;
|
||||
if(this._renderData){
|
||||
if (oldFrame && spriteFrame) {
|
||||
this._renderData!.uvDirty = oldFrame.uvHash !== spriteFrame.uvHash;
|
||||
}
|
||||
else {
|
||||
this._renderData!.uvDirty = true;
|
||||
}
|
||||
|
||||
this._renderDataDirty = this._renderData!.uvDirty
|
||||
}
|
||||
|
||||
if (spriteFrame) {
|
||||
|
||||
@@ -173,6 +173,18 @@ export interface IBaseNode {
|
||||
*/
|
||||
addChild (child: this): void;
|
||||
|
||||
/**
|
||||
* @zh 移除节点中指定的子节点
|
||||
* @param child 孩子节点
|
||||
*/
|
||||
removeChild(child: this, cleanup?: boolean): void;
|
||||
|
||||
/**
|
||||
* @zh 插入子节点到指定位置
|
||||
* @param siblingIndex 指定位置
|
||||
*/
|
||||
insertChild(child: this, siblingIndex: number): void;
|
||||
|
||||
/**
|
||||
* @en Returns a child from the container given its uuid.
|
||||
* @zh 通过 uuid 获取节点的子节点。
|
||||
|
||||
Reference in New Issue
Block a user