Merge pull request #15398 from 1scaR1/up_v3

Added JS tests for scroll widgets
This commit is contained in:
zilongshanren 2016-04-09 15:07:51 +08:00
commit 78e9dd187b
4 changed files with 509 additions and 1 deletions

View File

@ -350,3 +350,279 @@ var UIListViewTest_TouchIntercept = UIMainLayer.extend({
return false;
}
});
var UIListViewTest_ScrollToItem = UIMainLayer.extend({
_nextIndex: 0,
init: function () {
if(this._super()) {
var widgetSize = this._widget.getContentSize();
var background = this._widget.getChildByName("background_Panel");
var backgroundSize = background.getContentSize();
this._topDisplayLabel.setString("");
this._topDisplayLabel.x = widgetSize.width / 2.0;
this._topDisplayLabel.y = widgetSize.height / 2.0 + this._topDisplayLabel.height * 1.5;
this._bottomDisplayLabel.setString("");
this._bottomDisplayLabel.x = widgetSize.width / 2;
this._bottomDisplayLabel.y = widgetSize.height / 2 - this._bottomDisplayLabel.height * 3;
// Create the list view
var listView = new ccui.ListView();
// set list view ex direction
listView.setDirection(this._getListViewDirection());
listView.setBounceEnabled(true);
listView.setBackGroundImage("ccs-res/cocosui/green_edit.png");
listView.setBackGroundImageScale9Enabled(true);
listView.setContentSize(widgetSize.width / 2, widgetSize.height / 2);
listView.setScrollBarPositionFromCorner(cc.p(7, 7));
listView.setItemsMargin(2.0);
listView.x = (widgetSize.width - backgroundSize.width) / 2 + (backgroundSize.width - listView.width) / 2;
listView.y = (widgetSize.height - backgroundSize.height) / 2 + (backgroundSize.height - listView.height) / 2;
this._mainNode.addChild(listView);
{
var pNode = new cc.DrawNode();
var center = cc.p(widgetSize.width / 2, widgetSize.height / 2);
if(this._getListViewDirection() == ccui.ScrollView.DIR_HORIZONTAL)
{
var halfY = 110;
pNode.drawSegment(cc.p(center.x, center.y - halfY), cc.p(center.x, center.y + halfY), 2, cc.color(0, 0, 0, 255));
}
else
{
var halfX = 150;
pNode.drawSegment(cc.p(center.x - halfX, center.y), cc.p(center.x + halfX, center.y), 2, cc.color(0, 0, 0, 255));
}
pNode.setContentSize(listView.getContentSize());
this._mainNode.addChild(pNode);
}
var NUMBER_OF_ITEMS = 31;
// Button
var pButton = new ccui.Button("ccs-res/cocosui/backtotoppressed.png", "ccs-res/cocosui/backtotopnormal.png");
pButton.setAnchorPoint(cc.p(0, 0.5));
pButton.setScale(0.8);
pButton.setPosition(cc.pAdd(cc.p(widgetSize.width / 2, widgetSize.height / 2), cc.p(120, -60)));
pButton.setTitleText("Go to '" +this._nextIndex + "'");
pButton.addClickEventListener(function(pButton) {
listView.scrollToItem(this._nextIndex, cc.p(0.5, 0.5), cc.p(0.5, 0.5));
this._nextIndex = (this._nextIndex + Math.floor(NUMBER_OF_ITEMS / 2)) % NUMBER_OF_ITEMS;
pButton.setTitleText("Go to '" + this._nextIndex+ "'");
}.bind(this));
this._mainNode.addChild(pButton);
// Add list items
var default_button = new ccui.Button();
default_button.setName("TextButton");
default_button.setTouchEnabled(true);
default_button.loadTextures("ccs-res/cocosui/backtotoppressed.png", "ccs-res/cocosui/backtotopnormal.png", "");
var default_item = new ccui.Layout();
default_item.setTouchEnabled(true);
default_item.setContentSize(default_button.getContentSize());
default_button.x = default_item.width / 2;
default_button.y = default_item.height / 2;
default_item.addChild(default_button);
// set model
listView.setItemModel(default_item);
// Add list items
for (var i = 0; i < NUMBER_OF_ITEMS; ++i)
{
var item = default_item.clone();
item.getChildByName("TextButton").setTitleText("Button-" + i);
listView.pushBackCustomItem(item);
}
return true;
}
return false;
},
_getListViewDirection: function()
{
}
});
var UIListViewTest_ScrollToItemVertical = UIListViewTest_ScrollToItem.extend({
_getListViewDirection: function()
{
return ccui.ScrollView.DIR_VERTICAL;
}
});
var UIListViewTest_ScrollToItemHorizontal = UIListViewTest_ScrollToItem.extend({
_getListViewDirection: function()
{
return ccui.ScrollView.DIR_HORIZONTAL;
}
});
var UIListViewTest_Magnetic = UIMainLayer.extend({
_listView: null,
init: function () {
if(this._super()) {
var widgetSize = this._widget.getContentSize();
var background = this._widget.getChildByName("background_Panel");
var backgroundSize = background.getContentSize();
this._topDisplayLabel.setString("1");
this._topDisplayLabel.setFontSize(14);
this._topDisplayLabel.x = widgetSize.width / 2.0;
this._topDisplayLabel.y = widgetSize.height / 2 + 90;
this._bottomDisplayLabel.setString("");
this._bottomDisplayLabel.x = widgetSize.width / 2;
this._bottomDisplayLabel.y = widgetSize.height / 2 - this._bottomDisplayLabel.height * 3;
// Create the list view
this._listView = new ccui.ListView();
// set list view ex direction
this._listView.setDirection(this._getListViewDirection());
this._listView.setTouchEnabled(true);
this._listView.setBounceEnabled(true);
this._listView.setBackGroundImage("ccs-res/cocosui/green_edit.png");
this._listView.setBackGroundImageScale9Enabled(true);
this._listView.setContentSize(widgetSize.width / 2, widgetSize.height / 2);
this._listView.setScrollBarPositionFromCorner(cc.p(7, 7));
this._listView.setItemsMargin(2.0);
this._listView.setAnchorPoint(cc.p(0.5 ,0.5));
this._listView.x = widgetSize.width / 2;
this._listView.y = widgetSize.height / 2;
this._listView.setGravity(ccui.ListView.GRAVITY_CENTER_VERTICAL);
this._mainNode.addChild(this._listView);
{
var pNode = new cc.DrawNode();
var center = cc.p(widgetSize.width / 2, widgetSize.height / 2);
if(this._getListViewDirection() == ccui.ScrollView.DIR_HORIZONTAL)
{
var halfY = 110;
pNode.drawSegment(cc.p(center.x, center.y - halfY), cc.p(center.x, center.y + halfY), 2, cc.color(0, 0, 0, 255));
}
else
{
var halfX = 150;
pNode.drawSegment(cc.p(center.x - halfX, center.y), cc.p(center.x + halfX, center.y), 2, cc.color(0, 0, 0, 255));
}
pNode.setContentSize(this._listView.getContentSize());
this._mainNode.addChild(pNode);
}
// Initial magnetic type
this._listView.setMagneticType(ccui.ListView.MAGNETIC_NONE);
this._topDisplayLabel.setString("MagneticType - NONE");
// Magnetic change button
var pButton = new ccui.Button("ccs-res/cocosui/backtotoppressed.png", "ccs-res/cocosui/backtotopnormal.png");
pButton.setAnchorPoint(cc.p(0.5, 0.5));
pButton.setScale(0.8);
pButton.setPosition(cc.pAdd(cc.p(widgetSize.width / 2, widgetSize.height / 2), cc.p(130, -60)));
pButton.setTitleText("Next Magnetic");
pButton.addClickEventListener(function() {
var eCurrentType = this._listView.getMagneticType();
var eNextType;
var sString;
if(eCurrentType == ccui.ListView.MAGNETIC_NONE)
{
eNextType = ccui.ListView.MAGNETIC_CENTER;
sString = "CENTER";
}
else if(eCurrentType == ccui.ListView.MAGNETIC_CENTER)
{
eNextType = ccui.ListView.MAGNETIC_BOTH_END;
sString = "BOTH_END";
}
else if(eCurrentType == ccui.ListView.MAGNETIC_BOTH_END)
{
if(this._getListViewDirection() == ccui.ScrollView.DIR_HORIZONTAL)
{
eNextType = ccui.ListView.MAGNETIC_LEFT;
sString = "LEFT";
}
else
{
eNextType = ccui.ListView.MAGNETIC_TOP;
sString = "TOP";
}
}
else if(eCurrentType == ccui.ListView.MAGNETIC_LEFT)
{
eNextType = ccui.ListView.MAGNETIC_RIGHT;
sString = "RIGHT";
}
else if(eCurrentType == ccui.ListView.MAGNETIC_RIGHT)
{
eNextType = ccui.ListView.MAGNETIC_NONE;
sString = "NONE";
}
else if(eCurrentType == ccui.ListView.MAGNETIC_TOP)
{
eNextType = ccui.ListView.MAGNETIC_BOTTOM;
sString = "BOTTOM";
}
else if(eCurrentType == ccui.ListView.MAGNETIC_BOTTOM)
{
eNextType = ccui.ListView.MAGNETIC_NONE;
sString = "NONE";
}
this._listView.setMagneticType(eNextType);
this._topDisplayLabel.setString("MagneticType - " + sString);
}.bind(this));
this._mainNode.addChild(pButton);
var default_button = new ccui.Button();
default_button.setName("TextButton");
default_button.setTouchEnabled(true);
default_button.loadTextures("ccs-res/cocosui/backtotoppressed.png", "ccs-res/cocosui/backtotopnormal.png", "");
var default_item = new ccui.Layout();
default_item.setTouchEnabled(true);
default_item.setContentSize(default_button.getContentSize());
default_button.x = default_item.width / 2;
default_button.y = default_item.height / 2;
default_item.addChild(default_button);
// set model
this._listView.setItemModel(default_item);
// Add list items
for (var i = 0; i < 40; ++i)
{
var item = default_item.clone();
item.getChildByName("TextButton").setTitleText("Button-" + i);
this._listView.pushBackCustomItem(item);
}
return true;
}
return false;
},
_getListViewDirection: function()
{
}
});
var UIListViewTest_MagneticVertical = UIListViewTest_Magnetic.extend({
_getListViewDirection: function()
{
return ccui.ScrollView.DIR_VERTICAL;
}
});
var UIListViewTest_MagneticHorizontal = UIListViewTest_Magnetic.extend({
_getListViewDirection: function()
{
return ccui.ScrollView.DIR_HORIZONTAL;
}
});

View File

@ -586,3 +586,149 @@ var UIPageViewDisableTouchTest = UIMainLayer.extend({
return false;
}
});
var UIPageViewJumpToPageTest = UIMainLayer.extend({
init: function () {
if (this._super()) {
var widgetSize = this._widget.getContentSize();
//init text
this._topDisplayLabel.setString("setCurrentPageIndex API Test");
this._topDisplayLabel.setFontSize(14);
this._topDisplayLabel.x = widgetSize.width / 2.0;
this._topDisplayLabel.y = widgetSize.height / 2.0 + this._topDisplayLabel.height * 4;
this._bottomDisplayLabel.setString("");
this._bottomDisplayLabel.x = widgetSize.width / 2;
this._bottomDisplayLabel.y = widgetSize.height / 2 - this._bottomDisplayLabel.height * 3;
var background = this._widget.getChildByName("background_Panel");
// Create the page view
var pageView = new ccui.PageView();
pageView.setTouchEnabled(true);
pageView.setContentSize(cc.size(240, 130));
pageView.x = (widgetSize.width - background.width) / 2 + (background.width - pageView.width) / 2;
pageView.y = (widgetSize.height - background.height) / 2 + (background.height - pageView.height) / 2;
pageView.setIndicatorEnabled(true);
pageView.removeAllItems();
var pageCount = 4;
for (var i = 0; i < pageCount; ++i)
{
var layout = new ccui.Layout();
layout.setContentSize(cc.size(240.0, 130.0));
var imageView = new ccui.ImageView("ccs-res/cocosui/scrollviewbg.png");
imageView.setScale9Enabled(true);
imageView.setContentSize(cc.size(240, 130));
imageView.setPosition(cc.p(layout.width / 2.0, layout.height / 2.0));
layout.addChild(imageView);
var label = new ccui.Text("page " + (i+1), "Arial", 30);
label.setColor(cc.color(192, 192, 192));
label.setPosition(cc.p(layout.width / 2.0, layout.height / 2.0));
layout.addChild(label);
pageView.insertCustomItem(layout, i);
}
pageView.setCurrentPageIndex(1);
//add buttons to jump to specific page
var button1 = new ccui.Button();
button1.setPosition(cc.p(pageView.x - 50, pageView.y + pageView.height));
button1.setTitleText("Jump to Page1");
button1.addClickEventListener(function(){
pageView.setCurrentPageIndex(0);
});
this._mainNode.addChild(button1);
var button2 = button1.clone();
button2.setTitleText("Jump to Page2");
button2.setPosition(cc.p(pageView.x - 50, pageView.y + pageView.height - 50));
button2.addClickEventListener(function(){
pageView.setCurrentPageIndex(1);
});
this._mainNode.addChild(button2);
var button3 = button2.clone();
button3.setTitleText("Jump to Page3");
button3.setPosition(cc.p(pageView.x + pageView.width + 50, pageView.y + pageView.height));
button3.addClickEventListener(function(){
pageView.setCurrentPageIndex(2);
});
this._mainNode.addChild(button3);
var button4 = button3.clone();
button4.setTitleText("Jump to Page4");
button4.setPosition(cc.p(pageView.x + pageView.width + 50, pageView.y + pageView.height - 50));
button4.addClickEventListener(function(){
pageView.setCurrentPageIndex(3);
});
this._mainNode.addChild(button4);
this._mainNode.addChild(pageView);
return true;
}
return false;
}
});
var UIPageViewChildSizeTest = UIMainLayer.extend({
init: function () {
if (this._super()) {
var widgetSize = this._widget.getContentSize();
//init text
this._topDisplayLabel.setString("Move by horizontal direction");
this._topDisplayLabel.setFontSize(14);
this._topDisplayLabel.x = widgetSize.width / 2.0;
this._topDisplayLabel.y = widgetSize.height / 2.0 + this._topDisplayLabel.height * 4;
this._bottomDisplayLabel.setString("");
this._bottomDisplayLabel.x = widgetSize.width / 2;
this._bottomDisplayLabel.y = widgetSize.height / 2 - this._bottomDisplayLabel.height * 3;
var background = this._widget.getChildByName("background_Panel");
// Create the page view
var pageView = new ccui.PageView();
pageView.setTouchEnabled(true);
pageView.setContentSize(cc.size(240, 130));
pageView.x = (widgetSize.width - background.width) / 2 + (background.width - pageView.width) / 2;
pageView.y = (widgetSize.height - background.height) / 2 + (background.height - pageView.height) / 2;
pageView.setIndicatorEnabled(true);
pageView.removeAllItems();
var pageCount = 4;
for (var i = 0; i < pageCount; ++i)
{
var imageView = new ccui.ImageView("ccs-res/cocosui/scrollviewbg.png");
var label = new ccui.Text("page " + (i + 1), "Arial", 30);
imageView.setScale9Enabled(true);
label.setColor(cc.color(192, 192, 192));
label.setAnchorPoint(cc.p(0,0));
imageView.addChild(label);
pageView.insertCustomItem(imageView, i);
}
pageView.addEventListener(this.pageViewEvent, this);
this._mainNode.addChild(pageView);
return true;
}
return false;
},
pageViewEvent: function (sender, type) {
switch (type) {
case ccui.PageView.EVENT_TURNING:
var pageView = sender;
this._topDisplayLabel.setString("page = " + (pageView.getCurPageIndex().valueOf()-0 + 1));
break;
default:
break;
}
}
});

View File

@ -477,6 +477,12 @@
func: function () {
return new UIScrollViewTest_Vertical_Multiple();
}
},
{
title:"UIScrollView Scroll Bar Test",
func: function () {
return new UIScrollViewTest_ScrollBar();
}
}
],
"UIPageView": [
@ -515,6 +521,18 @@
func: function () {
return new UIPageViewDisableTouchTest();
}
},
{
title: "UIPageViewJumpToPageTest",
func: function () {
return new UIPageViewJumpToPageTest();
}
},
{
title: "UIPageViewChildSizeTest",
func: function () {
return new UIPageViewChildSizeTest();
}
}
],
"UIListView": [
@ -535,6 +553,30 @@
func: function() {
return new UIListViewTest_TouchIntercept();
}
},
{
title:"UIListViewTest Scroll to item vertical",
func: function() {
return new UIListViewTest_ScrollToItemVertical();
}
},
{
title:"UIListViewTest Scroll to item horizontal",
func: function() {
return new UIListViewTest_ScrollToItemHorizontal();
}
},
{
title:"UIListViewTest magnetic vertical",
func: function() {
return new UIListViewTest_MagneticVertical();
}
},
{
title:"UIListViewTest magnetic horizontal",
func: function() {
return new UIListViewTest_MagneticHorizontal();
}
}
],
"UIWidget": [

View File

@ -603,4 +603,48 @@ var UIScrollViewTest_Vertical_Multiple = UIMainLayer.extend({
scrollView.addChild(button_scale9);
}
}
});
});
var UIScrollViewTest_ScrollBar = UIMainLayer.extend({
init: function () {
if (this._super()) {
var widgetSize = this._widget.getContentSize();
//init text
this._topDisplayLabel.setString("Scroll bar is red, 65% opacity, auto hide time: 5 sec");
this._topDisplayLabel.setFontSize(14);
this._topDisplayLabel.x = widgetSize.width / 2.0;
this._topDisplayLabel.y = widgetSize.height / 2.0 + this._topDisplayLabel.height * 1.5;
this._bottomDisplayLabel.setString("");
this._bottomDisplayLabel.x = widgetSize.width / 2;
this._bottomDisplayLabel.y = widgetSize.height / 2 - this._bottomDisplayLabel.height * 3;
var background = this._widget.getChildByName("background_Panel");
// Create the scrollview
var scrollView = new ccui.ScrollView();
scrollView.setTouchEnabled(true);
scrollView.setBounceEnabled(true);
scrollView.setBackGroundColor(cc.color.GREEN);
scrollView.setBackGroundColorType(ccui.Layout.BG_COLOR_SOLID);
scrollView.setDirection(ccui.ScrollView.DIR_BOTH);
scrollView.setInnerContainerSize(cc.size(480, 320));
scrollView.setContentSize(cc.size(100, 100));
var scrollViewSize = scrollView.getContentSize();
scrollView.x = (widgetSize.width - background.width) / 2 + (background.width - scrollViewSize.width) / 2;
scrollView.y = (widgetSize.height - background.height) / 2 + (background.height - scrollViewSize.height) / 2;
this._mainNode.addChild(scrollView);
scrollView.setScrollBarAutoHideTime(5);
scrollView.setScrollBarColor(cc.color.RED);
scrollView.setScrollBarOpacity(255 * 0.65);
scrollView.setScrollBarWidth(5);
return true;
}
return false;
}
});