Commit b68b29df authored by rockyl's avatar rockyl

增加ScrollView

parent ce8afae0
...@@ -20,6 +20,14 @@ import {afterConstructor} from "../../zeroing/decorators/after-constructor"; ...@@ -20,6 +20,14 @@ import {afterConstructor} from "../../zeroing/decorators/after-constructor";
@applyScript @applyScript
@applyEvents @applyEvents
export default class Container extends DisplayObject { export default class Container extends DisplayObject {
percentWidth: number;
percentHeight: number;
left: number;
top: number;
right: number;
bottom: number;
horizonCenter: number;
verticalCenter: number;
/** /**
* 为false鼠标事件不再向下传递 * 为false鼠标事件不再向下传递
...@@ -312,7 +320,7 @@ export default class Container extends DisplayObject { ...@@ -312,7 +320,7 @@ export default class Container extends DisplayObject {
//自己先算 //自己先算
super.updateTransform(); super.updateTransform();
//考虑是否要加, //考虑是否要加,
//this._boundsID++; this._boundsID++;
//children遍历计算 //children遍历计算
for (let i = 0, j = this.children.length; i < j; ++i) { for (let i = 0, j = this.children.length; i < j; ++i) {
const child = this.children[i]; const child = this.children[i];
......
...@@ -653,18 +653,18 @@ export class Shape extends Sprite { ...@@ -653,18 +653,18 @@ export class Shape extends Sprite {
leftX = 0; leftX = 0;
leftY = 0; leftY = 0;
} }
leftX -= 20 + lineWidth >> 1; leftX -= /*20 +*/ lineWidth >> 1;
leftY -= 20 + lineWidth >> 1; leftY -= /*20 +*/ lineWidth >> 1;
buttonRightX += 20 + lineWidth >> 1; buttonRightX += /*20 +*/ lineWidth >> 1;
buttonRightY += 20 + lineWidth >> 1; buttonRightY += /*20 +*/ lineWidth >> 1;
let w = buttonRightX - leftX; let w = buttonRightX - leftX;
let h = buttonRightY - leftY; let h = buttonRightY - leftY;
s.offsetX = leftX; s.offsetX = leftX;
s.offsetY = leftY; s.offsetY = leftY;
s._localBoundsSelf.x = leftX + 10; s._localBoundsSelf.x = leftX/* + 10*/;
s._localBoundsSelf.y = leftY + 10; s._localBoundsSelf.y = leftY/* + 10*/;
s._localBoundsSelf.width = w - 20; s._localBoundsSelf.width = w/* - 20*/;
s._localBoundsSelf.height = h - 20; s._localBoundsSelf.height = h/* - 20*/;
let _canvas: any = s.canvas; let _canvas: any = s.canvas;
let ctx = s.context; let ctx = s.context;
_canvas.width = w; _canvas.width = w;
......
...@@ -12,7 +12,7 @@ import {Event} from "../events/Event"; ...@@ -12,7 +12,7 @@ import {Event} from "../events/Event";
* @extends Container * @extends Container
* @since 1.0.0 * @since 1.0.0
*/ */
export class ScrollPage extends Container { export class ScrollContainer extends Container {
/** /**
* 横向还是纵向 默认为纵向 * 横向还是纵向 默认为纵向
* @property isVertical * @property isVertical
...@@ -30,7 +30,7 @@ export class ScrollPage extends Container { ...@@ -30,7 +30,7 @@ export class ScrollPage extends Container {
* @since 1.0.0 * @since 1.0.0
* @default 0 * @default 0
*/ */
private viewWidth: number = 0; //private viewWidth: number = 0;
/** /**
* 可见区域的高 * 可见区域的高
* @property viewHeight * @property viewHeight
...@@ -39,7 +39,7 @@ export class ScrollPage extends Container { ...@@ -39,7 +39,7 @@ export class ScrollPage extends Container {
* @since 1.0.0 * @since 1.0.0
* @default 0 * @default 0
*/ */
private viewHeight: number = 0; //private viewHeight: number = 0;
// private _tweenId: number = 0; // private _tweenId: number = 0;
/** /**
* 整个滚动的最大距离值 * 整个滚动的最大距离值
...@@ -49,7 +49,7 @@ export class ScrollPage extends Container { ...@@ -49,7 +49,7 @@ export class ScrollPage extends Container {
* @since 1.0.0 * @since 1.0.0
* @default 1040 * @default 1040
*/ */
public maxDistance: number = 1040; //public maxDistance: number = 1040;
/** /**
* @property 滚动距离 * @property 滚动距离
* @type {number} * @type {number}
...@@ -78,7 +78,7 @@ export class ScrollPage extends Container { ...@@ -78,7 +78,7 @@ export class ScrollPage extends Container {
* @since 1.0.0 * @since 1.0.0
* @type {Container} * @type {Container}
*/ */
public view: Container = new Container(); public viewPort: Container = new Container();
/** /**
* 最后鼠标经过的坐标值 * 最后鼠标经过的坐标值
* @property lastValue * @property lastValue
...@@ -160,27 +160,29 @@ export class ScrollPage extends Container { ...@@ -160,27 +160,29 @@ export class ScrollPage extends Container {
* sPage.y=stage.viewRect.y; * sPage.y=stage.viewRect.y;
* *
*/ */
constructor(vW: number, vH: number, maxDistance: number, isVertical: boolean = true, isFull = false) { constructor(maxDistance?: number, isVertical: boolean = true, isFull = false) {
super(); super();
let s: ScrollPage = this; let s: ScrollContainer = this;
s._instanceType = "ScrollPage"; s._instanceType = "ScrollContainer";
s.addChild(s.maskObj); super.addChild(s.maskObj);
s.addChild(s.view); super.addChild(s.viewPort);
if (!isFull) { if (!isFull) {
//不全屏才设置mask //不全屏才设置mask
s.view.mask = s.maskObj; s.viewPort.mask = s.maskObj;
//为了能接收鼠标事件设置isUsedToMask //为了能接收鼠标事件设置isUsedToMask
s.maskObj.isUsedToMask = false; s.maskObj.isUsedToMask = false;
} }
s.maskObj.alpha = 0; s.maskObj.alpha = 0;
s.maxDistance = maxDistance; if(maxDistance !== undefined){
s.setViewRect(vW, vH, isVertical); //s.maxDistance = maxDistance;
}
s.updateViewRect(isVertical);
// s.addEventListener(MouseEvent.MOUSE_DOWN, s.onMouseEvent.bind(s)); // s.addEventListener(MouseEvent.MOUSE_DOWN, s.onMouseEvent.bind(s));
s.addEventListener(MouseEvent.MOUSE_MOVE, s.onMouseEvent.bind(s)); s.addEventListener(MouseEvent.MOUSE_MOVE, s.onMouseEvent, s);
s.addEventListener(MouseEvent.MOUSE_UP, s.onMouseEvent.bind(s)); s.addEventListener(MouseEvent.MOUSE_UP, s.onMouseEvent, s);
s.addEventListener(MouseEvent.MOUSE_OUT, s.onMouseEvent.bind(s)); s.addEventListener(MouseEvent.MOUSE_OUT, s.onMouseEvent, s);
s.addEventListener(Event.ENTER_FRAME, function () { s.addEventListener(Event.ENTER_FRAME, function () {
let view: any = s.view; let view: any = s.viewPort;
if (s.autoScroll) return; if (s.autoScroll) return;
if (!s.isSpringBack) { if (!s.isSpringBack) {
if (view[s.paramXY] > 0) { if (view[s.paramXY] > 0) {
...@@ -237,7 +239,7 @@ export class ScrollPage extends Container { ...@@ -237,7 +239,7 @@ export class ScrollPage extends Container {
} }
} else { } else {
if (s.stopTimes >= 0) { if (s.stopTimes >= 0) {
s.stopTimes++; //s.stopTimes++;
if (s.stopTimes >= 15) { if (s.stopTimes >= 15) {
s.speed = 0; s.speed = 0;
if (view[s.paramXY] > 0 || view[s.paramXY] < s.distance - s.maxDistance) { if (view[s.paramXY] > 0 || view[s.paramXY] < s.distance - s.maxDistance) {
...@@ -247,7 +249,12 @@ export class ScrollPage extends Container { ...@@ -247,7 +249,12 @@ export class ScrollPage extends Container {
} }
} }
} }
}) });
s.addEventListener(Event.RESIZE, this.updateViewRect, s);
}
get maxDistance(){
return this.viewPort.height;
} }
/** /**
...@@ -259,28 +266,27 @@ export class ScrollPage extends Container { ...@@ -259,28 +266,27 @@ export class ScrollPage extends Container {
* @public * @public
* @since 1.1.1 * @since 1.1.1
*/ */
public setViewRect(w: number, h: number, isVertical: boolean): void { public updateViewRect(isVertical?: boolean): void {
let s: ScrollPage = this; let s: ScrollContainer = this;
s.maskObj.clear(); s.maskObj.clear();
s.maskObj.beginFill("#000000"); s.maskObj.beginFill("#000000");
s.maskObj.drawRect(0, 0, w, h); s.maskObj.drawRect(0, 0, s.width, s.height);
s.viewWidth = w;
s.viewHeight = h;
s.maskObj.endFill(); s.maskObj.endFill();
s.isVertical = isVertical; if(isVertical !== undefined){
s.isVertical = isVertical;
}
if (isVertical) { if (isVertical) {
s.distance = s.viewHeight; s.distance = s.height;
s.paramXY = "y"; s.paramXY = "y";
} else { } else {
s.distance = s.viewWidth; s.distance = s.width;
s.paramXY = "x"; s.paramXY = "x";
} }
s.isVertical = isVertical;
} }
private onMouseEvent(e: MouseEvent): void { private onMouseEvent(e: MouseEvent): void {
let s = this; let s = this;
let view: any = s.view; let view: any = s.viewPort;
// if (s.distance < s.maxDistance) { // if (s.distance < s.maxDistance) {
if (e.type == MouseEvent.MOUSE_MOVE) { if (e.type == MouseEvent.MOUSE_MOVE) {
if (s.isMouseDownState < 1) { if (s.isMouseDownState < 1) {
...@@ -355,13 +361,13 @@ export class ScrollPage extends Container { ...@@ -355,13 +361,13 @@ export class ScrollPage extends Container {
*/ */
public scrollTo(dis: number, time: number = 0): void { public scrollTo(dis: number, time: number = 0): void {
let s: any = this; let s: any = this;
let newDis = s.paramXY == "x" ? s.viewWidth : s.viewHeight; let newDis = s.paramXY == "x" ? s.width : s.height;
if (dis < 0) { if (dis < 0) {
dis = 0; dis = 0;
} else if (dis > s.maxDistance - newDis) { } else if (dis > s.maxDistance - newDis) {
dis = s.maxDistance - newDis; dis = s.maxDistance - newDis;
} }
if (Math.abs(s.view[s.paramXY] + dis) > 2) { if (Math.abs(s.viewPort[s.paramXY] + dis) > 2) {
// s.autoScroll = true; // s.autoScroll = true;
// s.isStop = true; // s.isStop = true;
// s.isMouseDownState = 0; // s.isMouseDownState = 0;
...@@ -383,9 +389,9 @@ export class ScrollPage extends Container { ...@@ -383,9 +389,9 @@ export class ScrollPage extends Container {
public destroy(): void { public destroy(): void {
let s = this; let s = this;
s.maskObj.destroy(); s.maskObj.destroy();
s.view.destroy(); s.viewPort.destroy();
s.maskObj = null; s.maskObj = null;
s.view = null; s.viewPort = null;
super.destroy(); super.destroy();
} }
} }
import {ScrollPage} from "./ScrollPage"; import {ScrollContainer} from "./ScrollContainer";
import {DisplayObject} from "../display/DisplayObject"; import {DisplayObject} from "../display/DisplayObject";
import {Event} from "../events/Event"; import {Event} from "../events/Event";
...@@ -21,10 +21,10 @@ export interface IScrollListItem extends DisplayObject { ...@@ -21,10 +21,10 @@ export interface IScrollListItem extends DisplayObject {
* 滚动列表 * 滚动列表
* @class ScrollList * @class ScrollList
* @public * @public
* @extends ScrollPage * @extends ScrollContainer
* @since 1.0.9 * @since 1.0.9
*/ */
export class ScrollList extends ScrollPage { export class ScrollList extends ScrollContainer {
private _items: Array<IScrollListItem> = null; private _items: Array<IScrollListItem> = null;
private _itemW: number; private _itemW: number;
private _itemH: number; private _itemH: number;
...@@ -102,8 +102,8 @@ export class ScrollList extends ScrollPage { ...@@ -102,8 +102,8 @@ export class ScrollList extends ScrollPage {
private flushData() { private flushData() {
let s: any = this; let s: any = this;
if (s._isInit > 0) { if (s._isInit > 0) {
if (s.view._UI.UM) { if (s.viewPort._UI.UM) {
let id: number = (Math.abs(Math.floor(s.view[s.paramXY] / s._itemRow)) - 1) * s._cols; let id: number = (Math.abs(Math.floor(s.viewPort[s.paramXY] / s._itemRow)) - 1) * s._cols;
id = id < 0 ? 0 : id; id = id < 0 ? 0 : id;
if (id != s._lastFirstId) { if (id != s._lastFirstId) {
s._lastFirstId = id; s._lastFirstId = id;
...@@ -151,8 +151,8 @@ export class ScrollList extends ScrollPage { ...@@ -151,8 +151,8 @@ export class ScrollList extends ScrollPage {
* @public * @public
* @since 1.1.1 * @since 1.1.1
*/ */
public setViewRect(w: number, h: number, isVertical: boolean): void { public updateViewRect(w: number, h: number, isVertical: boolean): void {
super.setViewRect(w, h, isVertical); super.updateViewRect(w, h, isVertical);
let s = this; let s = this;
if (s._itemRow && s._itemCol) { if (s._itemRow && s._itemCol) {
s._updateViewRect(); s._updateViewRect();
...@@ -178,11 +178,11 @@ export class ScrollList extends ScrollPage { ...@@ -178,11 +178,11 @@ export class ScrollList extends ScrollPage {
item.id = -1; item.id = -1;
item.data = null; item.data = null;
s._items.push(item); s._items.push(item);
s.view.addChild(item); s.viewPort.addChild(item);
} }
} else { } else {
for (let i = 0; i < s._itemCount - newCount; i++) { for (let i = 0; i < s._itemCount - newCount; i++) {
s.view.removeChild(s._items.pop()); s.viewPort.removeChild(s._items.pop());
} }
} }
s._itemCount = newCount; s._itemCount = newCount;
...@@ -199,14 +199,14 @@ export class ScrollList extends ScrollPage { ...@@ -199,14 +199,14 @@ export class ScrollList extends ScrollPage {
public setLoading(downLoading: DisplayObject): void { public setLoading(downLoading: DisplayObject): void {
let s: any = this; let s: any = this;
if (s.downL) { if (s.downL) {
s.view.removeChild(s.downL); s.viewPort.removeChild(s.downL);
let wh = s.downL.getWH(); let wh = s.downL.getWH();
s.maxDistance -= (s.paramXY == "x" ? wh.width : wh.height); s.maxDistance -= (s.paramXY == "x" ? wh.width : wh.height);
s.downL = null; s.downL = null;
} }
if (downLoading) { if (downLoading) {
s.downL = downLoading; s.downL = downLoading;
s.view.addChild(downLoading); s.viewPort.addChild(downLoading);
s.downL[s.paramXY] = Math.max(s.distance, s.maxDistance); s.downL[s.paramXY] = Math.max(s.distance, s.maxDistance);
let wh = s.downL.getWH(); let wh = s.downL.getWH();
s.maxDistance += (s.paramXY == "x" ? wh.width : wh.height); s.maxDistance += (s.paramXY == "x" ? wh.width : wh.height);
......
...@@ -3,5 +3,5 @@ ...@@ -3,5 +3,5 @@
// export {BitmapNumbers} from "./BitmapNumbers"; // export {BitmapNumbers} from "./BitmapNumbers";
export {ScrollList} from "./ScrollList"; export {ScrollList} from "./ScrollList";
export {ScrollPage} from "./ScrollPage"; export {ScrollContainer} from "./ScrollContainer";
export {Button} from "./Button"; export {Button} from "./Button";
\ No newline at end of file
/**
* Created by rockyl on 2019-11-24.
*/
import {ScrollContainer} from "../../../2d/ui";
const proxyMethods = [
'onChildrenChange',
'addChild',
'addChildAt',
'swapChildren',
'getChildIndex',
'setChildIndex',
'getChildAt',
'getChildByName',
'removeChild',
'removeChildAt',
'removeChildren',
];
export class ScrollView extends ScrollContainer {
constructor() {
super();
for(let item of proxyMethods){
this[item] = this.viewPort[item].bind(this.viewPort);
}
}
}
...@@ -4,4 +4,5 @@ ...@@ -4,4 +4,5 @@
export * from './shapes' export * from './shapes'
export * from './Image' export * from './Image'
export * from './Label' export * from './Label'
\ No newline at end of file export * from './ScrollView'
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment