1
0
forked from sent/waves
waves/public/assets/g/pick/src/listview.js
2025-04-09 17:11:14 -05:00

1 line
9.6 KiB
JavaScript

var SCROLL_DEACCEL_RATE=.95,SCROLL_DEACCEL_DIST=1,BOUNCE_DURATION=.15,INSET_RATIO=.2,MOVE_INCH=7/160,BOUNCE_BACK_FACTOR=.35;cc.convertDistanceFromPointToInch=function(t){var e=cc.view;return t*((e.getScaleX()+e.getScaleY())/2)/160},cc.ScrollView=cc.Layer.extend({_dragging:!1,_container:null,_touchMoved:!1,_maxInset:null,_minInset:null,_bounceable:!1,_clippingToBounds:!1,_scrollDistance:null,_touchPoint:null,_viewSize:null,_parentScissorRect:null,_scissorRestored:!1,_tmpViewRect:null,_touchListener:null,_className:"ScrollView",ctor:function(t,e){cc.Layer.prototype.ctor.call(this),this._maxInset=cc.p(0,0),this._minInset=cc.p(0,0),this._scrollDistance=cc.p(0,0),this._touchPoint=cc.p(0,0),this._viewSize=cc.size(0,0),this._parentScissorRect=new cc.Rect(0,0,0,0),this._tmpViewRect=new cc.Rect(0,0,0,0),cc._renderType===cc._RENDER_TYPE_CANVAS&&(this.startCmd=new cc.CustomRenderCmdCanvas(this,function(t,e,i){(t=t||cc.context).save(),t.save(),this.transform();var n=this._transformWorld;t.transform(n.a,n.b,n.c,n.d,n.tx*e,-n.ty*i),cc.ScrollView.prototype._beforeDraw.call(this)}),this.endCmd=new cc.CustomRenderCmdCanvas(this,function(t){(t=t||cc.context).restore()})),void 0!=e?this.initWithViewSize(t,e):this.initWithViewSize(cc.size(200,200),null)},_initRendererCmd:function(){},init:function(){return this.initWithViewSize(cc.size(200,200),null)},initWithViewSize:function(t,e){var i=cc.p(0,0);return!!cc.Layer.prototype.init.call(this)&&(this._container=e,this._container||(this._container=new cc.Layer,this._container.ignoreAnchorPointForPosition(!1),this._container.setAnchorPoint(i)),this.setViewSize(t),this.setTouchEnabled(!0),this._bounceable=!0,this._clippingToBounds=!0,this._container.setPosition(i),this.addChild(this._container),!0)},setContentOffset:function(t,e){if(e)this.setContentOffsetInDuration(t,BOUNCE_DURATION);else{if(!this._bounceable){var i=this.minContainerOffset(),n=this.maxContainerOffset();t.x=Math.max(i.x,Math.min(n.x,t.x)),t.y=Math.max(i.y,Math.min(n.y,t.y))}this._container.setPosition(t)}},getContentOffset:function(){var t=this._container.getPosition();return cc.p(t.x,t.y)},setContentOffsetInDuration:function(t,e){var i=cc.moveTo(e,t),n=cc.callFunc(this._stoppedAnimatedScroll,this);this._container.runAction(cc.sequence(i,n)),this.schedule(this._performedAnimatedScroll)},minContainerOffset:function(){return cc.p(0,0)},maxContainerOffset:function(){var t=this._container.getContentSize(),e=this._viewSize;return cc.p(e.width-t.width,t.height-e.height)},isNodeVisible:function(t){var e=this.getContentOffset(),i=this.getViewSize(),n=this.getZoomScale(),s=cc.rect(-e.x/n,-e.y/n,i.width/n,i.height/n);return cc.rectIntersectsRect(s,t.getBoundingBox())},pause:function(t){this._container.pause();for(var e=this._container.getChildren(),i=0;i<e.length;i++)e[i].pause();this._super()},resume:function(t){for(var e=this._container.getChildren(),i=0,n=e.length;i<n;i++)e[i].resume();this._container.resume(),this._super()},isDragging:function(){return this._dragging},isTouchMoved:function(){return this._touchMoved},isBounceable:function(){return this._bounceable},setBounceable:function(t){this._bounceable=t},getViewSize:function(){return this._viewSize},setViewSize:function(t){this._viewSize=t,cc.Node.prototype.setContentSize.call(this,t)},getContainer:function(){return this._container},setContainer:function(t){t&&(this.removeAllChildren(!0),this._container=t,t.ignoreAnchorPointForPosition(!1),t.setAnchorPoint(0,0),this.addChild(t),this.setViewSize(this._viewSize))},onTouchBegan:function(t,e){if(!this.isVisible())return!1;var i=this._getViewRect(),n=this._container,s=n.convertToWorldSpace(n.convertTouchToNodeSpace(t));return!(this._touchMoved||!cc.rectContainsPoint(i,s)||(this._touchPoint=this.convertTouchToNodeSpace(t),this._touchMoved=!1,this._dragging=!0,this._scrollDistance.x=0,this._scrollDistance.y=0,0))},onTouchMoved:function(t,e){if(this.isVisible()&&(this.setNodeDirty(),this._dragging)){this._touchMoved=!0,this._getViewRect();var i,n,s=this.convertTouchToNodeSpace(t),o=cc.pSub(s,this._touchPoint);if(i=o.y,n=this._container.getPositionY(),this.minContainerOffset().y<=n&&n<=this.maxContainerOffset().y||(o.y*=BOUNCE_BACK_FACTOR),!this._touchMoved&&Math.abs(cc.convertDistanceFromPointToInch(i))<MOVE_INCH)return;this._touchMoved||(o.x=0,o.y=0),this._touchPoint=s,this._touchMoved=!0,o.x=0;var c=this._container.getPosition(),r=c.y+o.y;this._scrollDistance=o,this.setContentOffset(cc.p(c.x,r))}},onTouchEnded:function(t,e){this.isVisible()&&(this._touchMoved&&this.schedule(this._deaccelerateScrolling),this._dragging=!1,this._touchMoved=!1)},onTouchCancelled:function(t,e){this.isVisible()&&(this._dragging=!1,this._touchMoved=!1)},setContentSize:function(t,e){null!=this.getContainer()&&(void 0===e?this.getContainer().setContentSize(t):this.getContainer().setContentSize(t,e),this.updateInset())},_setWidth:function(t){var e=this.getContainer();null!=e&&(e._setWidth(t),this.updateInset())},_setHeight:function(t){var e=this.getContainer();null!=e&&(e._setHeight(t),this.updateInset())},getContentSize:function(){return this._container.getContentSize()},updateInset:function(){if(null!=this.getContainer()){var t=this._viewSize,e=this.maxContainerOffset();this._maxInset.x=e.x+t.width*INSET_RATIO,this._maxInset.y=e.y+t.height*INSET_RATIO,e=this.minContainerOffset(),this._minInset.x=e.x-t.width*INSET_RATIO,this._minInset.y=e.y-t.height*INSET_RATIO}},isClippingToBounds:function(){return this._clippingToBounds},setClippingToBounds:function(t){this._clippingToBounds=t},visit:function(t){if(this.isVisible()){var e,i,n,s=t||cc._renderContext,o=this._children;if(cc._renderType===cc._RENDER_TYPE_CANVAS){if(this.transform(s),this.startCmd&&cc.renderer.pushRenderCommand(this.startCmd),o&&o.length>0){for(n=o.length,this.sortAllChildren(),e=0;e<n&&(i=o[e])&&i._localZOrder<0;e++)i.visit(s);for(this._rendererCmd&&cc.renderer.pushRenderCommand(this._rendererCmd);e<n;e++)o[e].visit(s)}else this._rendererCmd&&cc.renderer.pushRenderCommand(this._rendererCmd);this.endCmd&&cc.renderer.pushRenderCommand(this.endCmd)}}},addChild:function(t,e,i){if(!t)throw new Error("child must not nil!");e=e||t.getLocalZOrder(),i=i||t.getTag(),this._container!=t?this._container.addChild(t,e,i):cc.Layer.prototype.addChild.call(this,t,e,i)},isTouchEnabled:function(){return null!=this._touchListener},setTouchEnabled:function(t){if(this._touchListener&&cc.eventManager.removeListener(this._touchListener),this._touchListener=null,t){var e=cc.EventListener.create({event:cc.EventListener.TOUCH_ONE_BY_ONE});this.onTouchBegan&&(e.onTouchBegan=this.onTouchBegan.bind(this)),this.onTouchMoved&&(e.onTouchMoved=this.onTouchMoved.bind(this)),this.onTouchEnded&&(e.onTouchEnded=this.onTouchEnded.bind(this)),this.onTouchCancelled&&(e.onTouchCancelled=this.onTouchCancelled.bind(this)),this._touchListener=e,cc.eventManager.addListener(e,this)}else this._dragging=!1,this._touchMoved=!1},_relocateContainer:function(t){var e=this.minContainerOffset(),i=this.maxContainerOffset(),n=this._container.getPosition(),s=n.x,o=n.y;o=Math.min(o,i.y),(o=Math.max(o,e.y))==n.y&&s==n.x||this.setContentOffset(cc.p(s,o),t)},_deaccelerateScrolling:function(t){if(this._dragging)this.unschedule(this._deaccelerateScrolling);else{var e,i,n=this._container.getPosition(),s=this._scrollDistance;this._container.setPosition(n.x+s.x,n.y+s.y),this._bounceable?(e=this._maxInset,i=this._minInset):(e=this.maxContainerOffset(),i=this.minContainerOffset());var o=this._container.getPositionX(),c=this._container.getPositionY();s.x*=SCROLL_DEACCEL_RATE,s.y*=SCROLL_DEACCEL_RATE,this.setContentOffset(cc.p(o,c)),(Math.abs(s.x)<=SCROLL_DEACCEL_DIST&&Math.abs(s.y)<=SCROLL_DEACCEL_DIST||c>e.y||c<i.y||o>e.x||o<i.x||o==e.x||o==i.x||c==e.y||c==i.y)&&(this.unschedule(this._deaccelerateScrolling),this._relocateContainer(!0))}},_performedAnimatedScroll:function(t){this._dragging&&this.unschedule(this._performedAnimatedScroll)},_stoppedAnimatedScroll:function(t){this.unschedule(this._performedAnimatedScroll)},_beforeDraw:function(t){if(this._clippingToBounds){this._scissorRestored=!1;var e=cc.view,i=this.getScaleX(),n=this.getScaleY(),s=t||cc._renderContext;if(cc._renderType===cc._RENDER_TYPE_CANVAS){var o=this._viewSize.width*i*e.getScaleX(),c=this._viewSize.height*n*e.getScaleY();s.beginPath(),s.rect(0,0,o,-c),s.restore(),s.clip(),s.closePath()}}},_getViewRect:function(){for(var t=this.convertToWorldSpace(cc.p(0,0)),e=this._viewSize,i=this.getScaleX(),n=this.getScaleY(),s=this._parent;null!=s;s=s.getParent())i*=s.getScaleX(),n*=s.getScaleY();i<0&&(t.x+=e.width*i,i=-i),n<0&&(t.y+=e.height*n,n=-n);var o=this._tmpViewRect;return o.x=t.x,o.y=t.y,o.width=e.width*i,o.height=e.height*n,o},showContentBottom:function(t){var e=this._viewSize,i=this._container.getContentSize();if(i.height>e.height){var n=i.height-e.height+t,s=this._container.getPosition();this._container.setPosition(s.x,n)}},scrollContent:function(t,e){var i=this._viewSize,n=this._container.getContentSize();if(n.height>i.height){var s=n.height-this._viewSize.height,o=this._container.getPosition();o.y+=t,o.y<0?o.y=0:o.y>s&&(o.y=s),this.setContentOffsetInDuration(o,e)}}});var _p=cc.ScrollView.prototype;_p.minOffset,cc.defineGetterSetter(_p,"minOffset",_p.minContainerOffset),_p.maxOffset,cc.defineGetterSetter(_p,"maxOffset",_p.maxContainerOffset),_p.bounceable,cc.defineGetterSetter(_p,"bounceable",_p.isBounceable,_p.setBounceable),_p.viewSize,cc.defineGetterSetter(_p,"viewSize",_p.getViewSize,_p.setViewSize),_p.container,cc.defineGetterSetter(_p,"container",_p.getContainer,_p.setContainer),_p.direction,cc.defineGetterSetter(_p,"direction",_p.getDirection,_p.setDirection),_p.delegate,cc.defineGetterSetter(_p,"delegate",_p.getDelegate,_p.setDelegate),_p.clippingToBounds,cc.defineGetterSetter(_p,"clippingToBounds",_p.isClippingToBounds,_p.setClippingToBounds),_p=null;