Files
cocos4/cocos2d/core/components/CCScrollBar.js
T
zilongshanren ee024d428e Add ScrollView and ScrollBar
Squashed commit of the following:

commit 34fea7fd4ea962f041ebadf4a5b17b3710a1fa02
Author: zilongshanren <guanghui8827@126.com>
Date:   Fri Dec 18 14:47:00 2015 +0800

    finish scrollview feature

commit 373eff672e4f81b79951cd583fc63865c76a0093
Author: zilongshanren <guanghui8827@126.com>
Date:   Fri Dec 18 14:34:49 2015 +0800

    fix float point compare issue

commit 4701cea8b488ae169407fca5be203d9a5e5fcccc
Author: zilongshanren <guanghui8827@126.com>
Date:   Fri Dec 18 10:56:09 2015 +0800

    fix component naming

commit 20f86a505aa09630b0bdf2ad6bdf4287777951aa
Merge: 279d2fc 3c6522e
Author: zilongshanren <guanghui8827@126.com>
Date:   Fri Dec 18 10:55:23 2015 +0800

    Merge branch 'master' into addScrollView

commit 279d2fc1143f6ec473e2ccfcaecc86a50087e033
Author: zilongshanren <guanghui8827@126.com>
Date:   Fri Dec 18 10:40:32 2015 +0800

    make elastic work

commit ed5c3cdb600bdc3d998280ab7e50d2703b50ed71
Author: zilongshanren <guanghui8827@126.com>
Date:   Fri Dec 18 10:11:00 2015 +0800

    fix bouncing issue

commit 8a1d3be8aff624597592954a58ce3b48c289c9f9
Author: zilongshanren <guanghui8827@126.com>
Date:   Thu Dec 17 15:26:57 2015 +0800

    make brake factor working

commit 7e3099de4aad72fbf60d3930e9e4337b1c5af828
Author: zilongshanren <guanghui8827@126.com>
Date:   Thu Dec 17 14:56:21 2015 +0800

    make scrollview inertia scroll working

commit 3c3b29935e1dcdcd9eb2f96637ce168ec35a4ac1
Author: zilongshanren <guanghui8827@126.com>
Date:   Thu Dec 17 14:31:51 2015 +0800

    fix convertToWorldSpace

commit 9d58b715f56f010a84e6903962169eea6d450d00
Author: zilongshanren <guanghui8827@126.com>
Date:   Thu Dec 17 11:18:51 2015 +0800

    add inertia scroll skeleton

commit 1523775943394139e723f9f7fdca4ad89a49cc70
Author: zilongshanren <guanghui8827@126.com>
Date:   Thu Dec 17 10:48:14 2015 +0800

    add a few scrollview variables

commit c5231fa52e580d80bd47de60133242cd5c9f0901
Author: zilongshanren <guanghui8827@126.com>
Date:   Thu Dec 17 10:08:23 2015 +0800

    finish scrollbar auto hide

commit 8c79eb33c0a2e25c108c3e03c90c1343469659ea
Author: zilongshanren <guanghui8827@126.com>
Date:   Thu Dec 17 09:44:03 2015 +0800

    fix toggle scrollbar

commit b6a24f82ee1ec4e391adf0e5ecc8c3d0477682d6
Author: zilongshanren <guanghui8827@126.com>
Date:   Thu Dec 17 09:34:46 2015 +0800

    finish scroll bar position

commit ba1dab366e94020bddcb0f3b4243c574ff60afa7
Author: zilongshanren <guanghui8827@126.com>
Date:   Wed Dec 16 20:22:23 2015 +0800

    fix convert to node space issue

commit e596d37aedb963bfd0f21f383d12f7c828c6c2bf
Author: zilongshanren <guanghui8827@126.com>
Date:   Wed Dec 16 17:54:26 2015 +0800

    fuck

commit 3b238be9d0e72a6414e3c578190ed6722dee9899
Author: zilongshanren <guanghui8827@126.com>
Date:   Wed Dec 16 16:42:05 2015 +0800

    make handle's position correct

commit e7c21508d1a08c0df761cf945e8d9714391e6f6b
Merge: a882517 3f58254
Author: zilongshanren <guanghui8827@126.com>
Date:   Wed Dec 16 10:52:02 2015 +0800

    Merge remote-tracking branch 'origin/master' into addScrollView

commit a882517765f5fae042cfde472202c22e0cc09416
Author: zilongshanren <guanghui8827@126.com>
Date:   Wed Dec 16 10:51:33 2015 +0800

    update scrollview

commit 90af1f848509325d317f0df0edb203697a9c04e5
Author: zilongshanren <guanghui8827@126.com>
Date:   Tue Dec 15 21:17:27 2015 +0800

    make horizontal and vertical move work

commit b5274cb9248e6eebf29e0ba8ceb3e96cb2d7f483
Author: zilongshanren <guanghui8827@126.com>
Date:   Tue Dec 15 20:57:00 2015 +0800

    update scrollbar

commit 43ae08aed730588303fe41857def7d51aea890e6
Author: zilongshanren <guanghui8827@126.com>
Date:   Tue Dec 15 16:26:50 2015 +0800

    add scrollbar skeleton

commit 6a4c1a39eeabad06f871a32f77d442f4fcd9b487
Merge: 1184415 306f66a
Author: zilongshanren <guanghui8827@126.com>
Date:   Mon Dec 14 20:42:05 2015 +0800

    Merge branch 'master' into addScrollView

commit 1184415480d1b08c589365c6ba919b438a760aa5
Author: zilongshanren <guanghui8827@126.com>
Date:   Mon Dec 14 20:01:18 2015 +0800

    add touch event handler

commit 510eba29001073b8c1215d50ae12f3e6d2bc37d6
Merge: d0e44ad e89a6f7
Author: zilongshanren <guanghui8827@126.com>
Date:   Mon Dec 14 17:55:27 2015 +0800

    Merge branch 'master' into addScrollView

commit d0e44ad90740706fa34480a3c8228fd1250c0c74
Author: zilongshanren <guanghui8827@126.com>
Date:   Fri Dec 11 18:22:23 2015 +0800

    add scrollbar components

commit 8a812def219517d6daab857e997084f7e0bea878
Author: zilongshanren <guanghui8827@126.com>
Date:   Fri Dec 11 17:48:42 2015 +0800

    add scrollview skeleton
2015-12-18 14:49:12 +08:00

266 lines
8.3 KiB
JavaScript

/****************************************************************************
Copyright (c) 2015 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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.
****************************************************************************/
/**
* Enum for Scrollbar direction
* @enum Scrollbar.Direction
*/
var Direction = cc.Enum({
/**
* @property {Number} HORIZONTAL
*/
HORIZONTAL: 0,
/**
* @property {Number} VERTICAL
*/
VERTICAL: 1
});
/**
* The Scrollbar control allows the user to scroll an image or other view that is too large to see completely
*
* @class Scrollbar
* @extends Component
*/
var Scrollbar = cc.Class({
name: 'cc.Scrollbar',
extends: require('./CCComponent'),
editor: CC_EDITOR && {
menu: 'UI/Scrollbar'
},
properties: {
_scrollView: null,
_touching: false,
_autoHideRemainingTime: 0,
_opacity: 255,
handle: {
default: null,
type: cc.SpriteRenderer,
notify: function() {
this._onScroll(cc.p(0, 0));
}
},
direction: {
default: Direction.HORIZONTAL,
type: Direction,
notify: function() {
this._onScroll(cc.p(0, 0));
}
},
enableAutoHide: {
default: true
},
autoHideTime: {
default: 1.0
}
},
setTargetScrollView: function(scrollView) {
this._scrollView = scrollView;
},
_convertToScrollViewSpace: function(content) {
var worldSpacePos = content.convertToWorldSpaceAR(cc.p(0, 0));
var scrollViewSpacePos = this._scrollView.node.convertToNodeSpace(worldSpacePos);
return scrollViewSpacePos;
},
_setOpacity: function(opacity) {
if (this.handle) {
this.node.setOpacity(opacity);
}
},
_onScroll: function(outOfBoundary) {
if (this._scrollView) {
if (this.enableAutoHide) {
this._autoHideRemainingTime = this.autoHideTime;
this._setOpacity(this._opacity);
}
var content = this._scrollView.content;
var contentSize = content.getContentSize();
var scrollViewSize = this._scrollView.node.getContentSize();
var contentMeasure = 0;
var scrollViewMeasure = 0;
var outOfBoundaryValue = 0;
var contentPosition = 0;
if (this.direction === Direction.HORIZONTAL) {
contentMeasure = contentSize.width;
scrollViewMeasure = scrollViewSize.width;
outOfBoundaryValue = outOfBoundary.x;
contentPosition = -this._convertToScrollViewSpace(content).x;
} else if (this.direction === Direction.VERTICAL) {
contentMeasure = contentSize.height;
scrollViewMeasure = scrollViewSize.height;
outOfBoundaryValue = outOfBoundary.y;
contentPosition = -this._convertToScrollViewSpace(content).y;
}
var length = this._calculateLength(contentMeasure, scrollViewMeasure, outOfBoundaryValue);
var position = this._calculatePosition(contentMeasure, scrollViewMeasure, contentPosition, outOfBoundaryValue, length);
this._updateLength(length);
this._updateHanlderPosition(position);
}
},
_updateHanlderPosition: function(position) {
if (this.handle) {
var oldPosition = this._fixupHandlerPosition();
this.handle.node.setPosition(cc.pAdd(position, oldPosition));
}
},
_fixupHandlerPosition: function() {
var barSize = this.node.getContentSize();
var barAnchor = this.node.getAnchorPoint();
var barPosition = this.node.getPosition();
var fixupPosition;
var handleParent = this.handle.node.parent;
if (this.direction === Direction.HORIZONTAL) {
var leftSideWorldPosition = this.node.convertToWorldSpaceAR(cc.p(-barSize.width * barAnchor.x, -barSize.height * barAnchor.y));
fixupPosition = handleParent.convertToNodeSpaceAR(leftSideWorldPosition);
} else if (this.direction === Direction.VERTICAL) {
var bottomSideWorldPosition = this.node.convertToWorldSpaceAR(cc.p(-barSize.width * barAnchor.x, -barSize.height * barAnchor.y));
fixupPosition = handleParent.convertToNodeSpaceAR(bottomSideWorldPosition);
}
this.handle.node.setPosition(fixupPosition);
return fixupPosition;
},
_onTouchBegan: function() {
if (!this.enableAutoHide) {
return;
}
this._touching = true;
},
_onTouchEnded: function() {
if (!this.enableAutoHide) {
return;
}
this._touching = false;
if (this.autoHideTime <= 0) {
return;
}
this._autoHideRemainingTime = this.autoHideTime;
},
_calculateLength: function(contentMeasure, scrollViewMeasure, outOfBoundary) {
var denominatorValue = contentMeasure;
if (outOfBoundary) {
var GETTINGSHORTERFACTOR = 20;
denominatorValue += (outOfBoundary > 0 ? outOfBoundary : -outOfBoundary) * GETTINGSHORTERFACTOR;
}
var lengthRation = scrollViewMeasure / denominatorValue;
return scrollViewMeasure * lengthRation;
},
_calculatePosition: function(contentMeasure, scrollViewMeasure, contentPosition, outOfBoundary, actualLenth) {
var denominatorValue = contentMeasure - scrollViewMeasure;
if (outOfBoundary) {
denominatorValue += Math.abs(outOfBoundary);
}
var positionRatio = 0;
if (denominatorValue) {
positionRatio = contentPosition / denominatorValue;
positionRatio = Math.max(positionRatio, 0);
positionRatio = Math.min(positionRatio, 1);
}
var position = (scrollViewMeasure - actualLenth) * positionRatio;
if (this.direction === Direction.VERTICAL) {
return cc.p(0, position);
} else {
return cc.p(position, 0);
}
},
_updateLength: function(length) {
if (this.handle) {
var handleNode = this.handle.node;
var handleNodeSize = this.node.getContentSize();
handleNode.setAnchorPoint(cc.p(0, 0));
if (this.direction === Direction.HORIZONTAL) {
handleNode.setContentSize(length, handleNodeSize.height);
} else {
handleNode.setContentSize(handleNodeSize.width, length);
}
}
},
_processAutoHide: function(deltaTime) {
if (!this.enableAutoHide || this._autoHideRemainingTime <= 0) {
return;
} else if (this._touching) {
return;
}
this._autoHideRemainingTime -= deltaTime;
if (this._autoHideRemainingTime <= this.autoHideTime) {
this._autoHideRemainingTime = Math.max(0, this._autoHideRemainingTime);
var opacity = this._opacity * (this._autoHideRemainingTime / this.autoHideTime);
this._setOpacity(opacity);
}
},
start: function() {
if (this.enableAutoHide) {
this._setOpacity(0);
}
},
update: function(dt) {
this._processAutoHide(dt);
}
});
cc.Scrollbar = module.exports = Scrollbar;