Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixed #1465 add TableView to UI widget

  • Loading branch information...
commit 9b45436e2914205093e1a86d4db3a0a1a78dd051 1 parent 00f2aca
dingpinglv dingpinglv authored
3  cocos2d/base_nodes/CCNode.js
View
@@ -1665,7 +1665,8 @@ cc.Node = cc.Class.extend(/** @lends cc.Node# */{
*/
convertTouchToNodeSpace:function (touch) {
var point = touch.getLocation();
- point = cc.Director.getInstance().convertToGL(point);
+ //TODO in canvas point don't convert to GL
+ //point = cc.Director.getInstance().convertToGL(point);
return this.convertToNodeSpace(point);
},
60 cocos2d/cocoa/CCSet.js
View
@@ -35,12 +35,10 @@ cc.Set = cc.Class.extend(/** @lends cc.Set# */{
*/
ctor:function (setObject) {
if (setObject) {
- this._set = Object.create(setObject._set);
+ this._set = [].concat(setObject._set);
+ } else {
+ this._set = [];
}
- else {
- this._set = new Array();
- }
-
},
/**
@@ -48,7 +46,7 @@ cc.Set = cc.Class.extend(/** @lends cc.Set# */{
* @return {cc.Set}
*/
copy:function () {
- return new this.Set(this);
+ return new cc.Set(this);
},
/**
@@ -57,7 +55,6 @@ cc.Set = cc.Class.extend(/** @lends cc.Set# */{
*/
mutableCopy:function () {
return this.copy();
-
},
/**
@@ -66,7 +63,6 @@ cc.Set = cc.Class.extend(/** @lends cc.Set# */{
*/
count:function () {
return this._set.length;
-
},
/**
@@ -74,8 +70,11 @@ cc.Set = cc.Class.extend(/** @lends cc.Set# */{
* @param {object} obj
*/
addObject:function (obj) {
+ if(cc.ArrayContainsObject(this._set,obj))
+ return;
this._set.push(obj);
-
+ //sort
+ this._set.sort(function(a,b){return a-b;});
},
/**
@@ -83,10 +82,6 @@ cc.Set = cc.Class.extend(/** @lends cc.Set# */{
* @param {object} obj
*/
removeObject:function (obj) {
- /* if(obj in this._set)
- {
- delete this._set[obj]
- } */
var k = 0;
for (var i = 0, n = 0; i < this._set.length; i++) {
if (this._set[i] != obj) {
@@ -94,8 +89,27 @@ cc.Set = cc.Class.extend(/** @lends cc.Set# */{
k++;
}
}
- array.length = k;
+ this._set.length = k;
+ },
+
+ /**
+ * Return the iterator that points to the first element.
+ * @reture Object
+ */
+ begin:function(){
+ if(this._set && this._set.length > 0)
+ return this._set[0];
+ return null;
+ },
+ /**
+ * Return the iterator that points to the poisition after the last element.
+ * @reture Object
+ */
+ end:function(){
+ if(this._set && this._set.length > 0)
+ return this._set[this._set.length -1];
+ return null;
},
/**
@@ -104,34 +118,22 @@ cc.Set = cc.Class.extend(/** @lends cc.Set# */{
* @return {Boolean}
*/
containsObject:function (obj) {
-
- if ((obj in this._set) == true) {
- return true;
- }
- else {
- return false;
- }
-
-
+ return cc.ArrayContainsObject(this._set,obj);
},
-
/**
* Return the first element if it contains elements, or null if it doesn't contain any element.
* @return {object|Null}
*/
anyObject:function () {
- if (this._set.length > 0) {
+ if (this._set && this._set.length > 0) {
return this._set[0];
- }
- else {
+ } else {
return null;
}
-
},
_set:null
-
});
/**
12 cocos2d/menu_nodes/CCMenuItem.js
View
@@ -78,6 +78,7 @@ cc.MenuItem = cc.Node.extend(/** @lends cc.MenuItem# */{
_listener:null,
_selector:null,
_isSelected:false,
+ _isEnabled:false,
/**
* MenuItem is selected
@@ -86,7 +87,16 @@ cc.MenuItem = cc.Node.extend(/** @lends cc.MenuItem# */{
isSelected:function () {
return this._isSelected;
},
- _isEnabled:false,
+
+ /**
+ * set the target/selector of the menu item
+ * @param {cc.Node} rec
+ * @param {function|String} selector
+ */
+ setTarget:function (rec, selector) {
+ this._listener = rec;
+ this._selector = selector;
+ },
/**
* MenuItem is Enabled
15 cocos2d/platform/CCApplication.js
View
@@ -148,26 +148,33 @@ if (!window.console) {
cc.setup = function (el, width, height) {
var element = cc.$(el) || cc.$('#' + el);
if (element.tagName == "CANVAS") {
+ width = width || element.width;
+ height = height || element.height;
+
//it is already a canvas, we wrap it around with a div
cc.container = cc.$new("DIV");
cc.canvas = element;
cc.canvas.parentNode.insertBefore(cc.container, cc.canvas);
cc.canvas.appendTo(cc.container);
- cc.container.style.width = (width || cc.canvas.width || 480) + "px";
- cc.container.style.height = (height || cc.canvas.height || 320) + "px";
+ cc.container.style.width = (width || 480) + "px";
+ cc.container.style.height = (height || 320) + "px";
cc.container.setAttribute('id', 'Cocos2dGameContainer');
cc.canvas.setAttribute("width", width || 480);
cc.canvas.setAttribute("height", height || 320);
- }
- else {//we must make a new canvas and place into this element
+ } else {//we must make a new canvas and place into this element
if (element.tagName != "DIV") {
cc.log("Warning: target element is not a DIV or CANVAS");
}
+ width = width || parseInt(element.style.width);
+ height = height || parseInt(element.style.height);
+
cc.canvas = cc.$new("CANVAS");
cc.canvas.addClass("gameCanvas");
cc.canvas.setAttribute("width", width || 480);
cc.canvas.setAttribute("height", height || 320);
cc.container = element;
+ cc.container.style.width = (width || 480) + "px";
+ cc.container.style.height = (height || 320) + "px";
}
cc.container.style.position = 'relative';
cc.container.style.overflow = 'hidden';
6 cocos2d/platform/CCMacro.js
View
@@ -25,6 +25,12 @@
****************************************************************************/
/**
+ * @constant
+ * @type Number
+ */
+cc.INVALID_INDEX = 0xffffffff;
+
+/**
* PI is the ratio of a circle's circumference to its diameter.
* @constant
* @type Number
4 cocos2d/platform/jsloader.js
View
@@ -111,7 +111,7 @@
var c = d.querySelector('#cocos2d-html5').c;
if(c.loadExtension != null && c.loadExtension == true){
- engine.concat([
+ engine = engine.concat([
'../extensions/GUI/CCControlExtension/CCControl.js',
'../extensions/GUI/CCControlExtension/CCControlButton.js',
'../extensions/GUI/CCControlExtension/CCControlUtils.js',
@@ -124,6 +124,8 @@
'../extensions/GUI/CCControlExtension/CCControlSlider.js',
'../extensions/GUI/CCControlExtension/CCControlSwitch.js',
'../extensions/GUI/CCScrollView/CCScrollView.js',
+ '../extensions/GUI/CCScrollView/CCSorting.js',
+ '../extensions/GUI/CCScrollView/CCTableView.js',
'../extensions/CCBReader/CCNodeLoader.js',
'../extensions/CCBReader/CCBReaderUtil.js',
'../extensions/CCBReader/CCControlLoader.js',
2  extensions/CCBReader/CCNodeLoader.js
View
@@ -602,7 +602,7 @@ cc.NodeLoader = cc.Class.extend({
}
if (target != null) {
- if (selectorName.length() > 0) {
+ if (selectorName.length > 0) {
var selMenuHandler = 0;
var targetAsCCBSelectorResolver = target;
4 extensions/GUI/CCControlExtension/CCControl.js
View
@@ -193,7 +193,7 @@ cc.Control = cc.Layer.extend({
// <CCInvocation*>
var invocationList = this._dispatchListforControlEvent(1 << i);
for (var j = 0; j < invocationList.length; j++) {
- invocationList[i].invoke(this);
+ invocationList[j].invoke(this);
}
}
}
@@ -267,7 +267,7 @@ cc.Control = cc.Layer.extend({
isTouchInside:function (touch) {
var touchLocation = this.getTouchLocation(touch);
var bBox = this.getBoundingBox();
- return cc.CCRectContainsPoint(bBox, touchLocation);
+ return cc.Rect.CCRectContainsPoint(bBox, touchLocation);
},
/**
2  extensions/GUI/CCControlExtension/CCControlButton.js
View
@@ -59,7 +59,7 @@ cc.ControlButton = cc.Control.extend({
init:function (isDirectCall) {
if ((isDirectCall != null) && (isDirectCall == true))
- this._super();
+ return this._super();
return this.initWithLabelAndBackgroundSprite(cc.LabelTTF.create("", "Helvetica", 12), cc.Scale9Sprite.create());
},
2  extensions/GUI/CCControlExtension/CCControlHuePicker.js
View
@@ -125,7 +125,7 @@ cc.ControlHuePicker = cc.Control.extend({
},
_checkSliderPosition:function (location) {
// check that the touch location is within the bounding rectangle before sending updates
- if (cc.CCRectContainsPoint(this._background.getBoundingBox(), location)) {
+ if (cc.Rect.CCRectContainsPoint(this._background.getBoundingBox(), location)) {
this._updateSliderPosition(location);
return true;
}
2  extensions/GUI/CCControlExtension/CCControlUtils.js
View
@@ -164,5 +164,5 @@ cc.ControlUtils.RGBfromHSV = function(hsvValue){
};
cc.ControlUtils.CCRectUnion = function(rect1, rect2){
- return cc.CCRectUnion(rect1,rect2);
+ return cc.Rect.CCRectUnion(rect1,rect2);
};
21 extensions/GUI/CCControlExtension/CCScale9Sprite.js
View
@@ -353,19 +353,24 @@ cc.Scale9Sprite = cc.Node.extend({
// Release old sprites
this.removeAllChildrenWithCleanup(true);
- if (this.scale9Image != batchNode) {
- this.scale9Image = batchNode;
+ if (this._scale9Image != batchNode) {
+ this._scale9Image = batchNode;
}
- this.scale9Image.removeAllChildrenWithCleanup(true);
+ this._scale9Image.removeAllChildrenWithCleanup(true);
this._capInsets = capInsets;
// If there is no given rect
- if (cc.CCRectEqualToRect(rect, cc.RectZero())) {
+ if (cc.Rect.CCRectEqualToRect(rect, cc.RectZero())) {
// Get the texture size as original
- var textureSize = this._scale9Image.getTextureAtlas().getTexture().getContentSize();
- rect = cc.RectMake(0, 0, textureSize.width, textureSize.height);
+ var selTexture = this._scale9Image.getTextureAtlas().getTexture();
+ if(selTexture instanceof cc.Texture2D){
+ var textureSize = selTexture.getContentSize();
+ rect = cc.RectMake(0, 0, textureSize.width, textureSize.height);
+ }else{
+ rect = cc.RectMake(0, 0, selTexture.width, selTexture.height);
+ }
}
// Set the given rect's size as original size
@@ -375,7 +380,7 @@ cc.Scale9Sprite = cc.Node.extend({
this._capInsetsInternal = capInsets;
// If there is no specified center region
- if (cc.CCRectEqualToRect(this._capInsetsInternal, cc.RectZero())) {
+ if (cc.Rect.CCRectEqualToRect(this._capInsetsInternal, cc.RectZero())) {
// Apply the 3x3 grid format
this._capInsetsInternal = cc.RectMake(
rect.origin.x + this._originalSize.width / 3,
@@ -403,7 +408,7 @@ cc.Scale9Sprite = cc.Node.extend({
t,
this._capInsetsInternal.size.width,
this._capInsetsInternal.origin.y - t));
- this._scale9Image.addChild(top, 1, cc.POSITIONS_TOP);
+ this._scale9Image.addChild(this._top, 1, cc.POSITIONS_TOP);
// Bottom
this._bottom = cc.Sprite.createWithTexture(this._scale9Image.getTexture(), cc.RectMake(this._capInsetsInternal.origin.x,
154 extensions/GUI/CCScrollView/CCScrollView.js
View
@@ -25,6 +25,8 @@
Created by Sangwoo Im on 6/3/10.
****************************************************************************/
+cc.SCROLLVIEW_DIRECTION_NONE = -1;
+
cc.SCROLLVIEW_DIRECTION_HORIZONTAL = 0;
cc.SCROLLVIEW_DIRECTION_VERTICAL = 1;
@@ -76,6 +78,7 @@ cc.ScrollView = cc.Layer.extend({
this._touchPoint = new cc.Point(0, 0);
this._touches = [];
this._viewSize = new cc.Size(0, 0);
+ this._isTouched = false;
},
init:function (isDirectCall) {
@@ -100,6 +103,8 @@ cc.ScrollView = cc.Layer.extend({
if (!this._container) {
this._container = cc.Layer.create();
+ this._container.ignoreAnchorPointForPosition(false);
+ this._container.setAnchorPoint(cc.p(0.0, 0.0));
}
this.setViewSize(size);
@@ -141,7 +146,6 @@ cc.ScrollView = cc.Layer.extend({
}
this._container.setPosition(offset);
-
if (this._delegate != null && this._delegate.scrollViewDidScroll) {
this._delegate.scrollViewDidScroll(this);
}
@@ -260,9 +264,9 @@ cc.ScrollView = cc.Layer.extend({
*/
pause:function (sender) {
this._container.pauseSchedulerAndActions();
- var getChildren = this._container.getChildren();
- for (var i = 0; i < getChildren.length; i++) {
- getChildren[i].pauseSchedulerAndActions();
+ var selChildren = this._container.getChildren();
+ for (var i = 0; i < selChildren.length; i++) {
+ selChildren[i].pauseSchedulerAndActions();
}
},
@@ -270,9 +274,9 @@ cc.ScrollView = cc.Layer.extend({
* Provided to make scroll view compatible with SWLayer's resume method
*/
resume:function (sender) {
- var getChildren = this._container.getChildren();
- for (var i = 0; i < getChildren.length; i++) {
- getChildren[i].resumeSchedulerAndActions();
+ var selChildren = this._container.getChildren();
+ for (var i = 0; i < selChildren.length; i++) {
+ selChildren[i].resumeSchedulerAndActions();
}
this._container.resumeSchedulerAndActions();
@@ -302,16 +306,6 @@ cc.ScrollView = cc.Layer.extend({
setViewSize:function (size) {
this._viewSize = size;
-
- if (this._container != null) {
- this._maxInset = this.maxContainerOffset();
- this._maxInset = cc.p(this._maxInset.x + this._viewSize.width * INSET_RATIO,
- this._maxInset.y + this._viewSize.height * INSET_RATIO);
- this._minInset = this.minContainerOffset();
- this._minInset = cc.p(this._minInset.x - this._viewSize.width * INSET_RATIO,
- this._minInset.y - this._viewSize.height * INSET_RATIO);
- }
-
this.setContentSize(size, true);
},
@@ -322,15 +316,14 @@ cc.ScrollView = cc.Layer.extend({
setContainer:function (container) {
this.removeAllChildrenWithCleanup(true);
- if (!container) return;
+ if (!container)
+ return;
this._container = container;
-
this._container.ignoreAnchorPointForPosition(false);
this._container.setAnchorPoint(cc.p(0.0, 0.0));
this.addChild(this._container);
-
this.setViewSize(this._viewSize);
},
@@ -352,6 +345,7 @@ cc.ScrollView = cc.Layer.extend({
},
/** override functions */
+ _isTouched:null,
// optional
onTouchBegan:function (touch, event) {
if (!this.isVisible())
@@ -360,14 +354,14 @@ cc.ScrollView = cc.Layer.extend({
var frame = cc.RectMake(this.getPosition().x, this.getPosition().y, this._viewSize.width, this._viewSize.height);
//dispatcher does not know about clipping. reject touches outside visible bounds.
- if (this._touches.length > 2 || this._touchMoved ||
- !cc.CCRectContainsPoint(frame, this._container.convertToWorldSpace(this._container.convertTouchToNodeSpace(touch)))) {
+ var locPoint = this._container.convertToWorldSpace(this._container.convertTouchToNodeSpace(touch));
+ if (this._touches.length > 2 || this._touchMoved || !cc.Rect.CCRectContainsPoint(frame, locPoint))
return false;
- }
- if (!this._touches.index(touch)) {
- this._touches.push(touch);
- }
+ //if (!cc.ArrayContainsObject(this._touches, touch)) {
+ this._touches.push(touch);
+ //}
+ this._isTouched = true;
if (this._touches.length == 1) { // scrolling
this._touchPoint = this.convertTouchToNodeSpace(touch);
@@ -389,19 +383,17 @@ cc.ScrollView = cc.Layer.extend({
if (!this.isVisible())
return;
- if (this._touches.index(touch)) {
+ //if (cc.ArrayContainsObject(this._touches, touch)) {
+ if(this._isTouched){
if (this._touches.length == 1 && this._dragging) { // scrolling
- var moveDistance, newPoint, maxInset, minInset;
- var frame;
- var newX, newY;
-
this._touchMoved = true;
- frame = cc.RectMake(this.getPosition().x, this.getPosition().y, this._viewSize.width, this._viewSize.height);
- newPoint = this.convertTouchToNodeSpace(this._touches[0]);
- moveDistance = cc.pSub(newPoint, this._touchPoint);
+ var frame = cc.RectMake(this.getPosition().x, this.getPosition().y, this._viewSize.width, this._viewSize.height);
+ //var newPoint = this.convertTouchToNodeSpace(this._touches[0]);
+ var newPoint = this.convertTouchToNodeSpace(touch);
+ var moveDistance = cc.pSub(newPoint, this._touchPoint);
this._touchPoint = newPoint;
- if (cc.CCRectContainsPoint(frame, this.convertToWorldSpace(newPoint))) {
+ if (cc.Rect.CCRectContainsPoint(frame, this.convertToWorldSpace(newPoint))) {
switch (this._direction) {
case cc.SCROLLVIEW_DIRECTION_VERTICAL:
moveDistance = cc.p(0.0, moveDistance.y);
@@ -414,13 +406,13 @@ cc.ScrollView = cc.Layer.extend({
}
this._container.setPosition(cc.pAdd(this._container.getPosition(), moveDistance));
- maxInset = this._maxInset;
- minInset = this._minInset;
+ var maxInset = this._maxInset;
+ var minInset = this._minInset;
//check to see if offset lies within the inset bounds
- newX = Math.min(this._container.getPosition().x, maxInset.x);
+ var newX = Math.min(this._container.getPosition().x, maxInset.x);
newX = Math.max(newX, minInset.x);
- newY = Math.min(this._container.getPosition().y, maxInset.y);
+ var newY = Math.min(this._container.getPosition().y, maxInset.y);
newY = Math.max(newY, minInset.y);
this._scrollDistance = cc.pSub(moveDistance, cc.p(newX - this._container.getPosition().x, newY - this._container.getPosition().y));
@@ -438,7 +430,14 @@ cc.ScrollView = cc.Layer.extend({
if (!this.isVisible())
return;
- if (this._touches.index(touch)) {
+ if (this._touches.length == 1 && this._touchMoved)
+ this.schedule(this._deaccelerateScrolling);
+ this._touches.length = 0;
+ this._isTouched = false;
+ this._dragging = false;
+ this._touchMoved = false;
+
+ /*if (cc.ArrayContainsObject(this._touches, touch)) {
if (this._touches.length == 1 && this._touchMoved)
this.schedule(this._deaccelerateScrolling);
cc.ArrayRemoveObject(this._touches, touch);
@@ -447,31 +446,50 @@ cc.ScrollView = cc.Layer.extend({
if (this._touches.length == 0) {
this._dragging = false;
this._touchMoved = false;
- }
+ }*/
},
onTouchCancelled:function (touch, event) {
if (!this.isVisible())
return;
+ this._touches.length = 0;
+ this._isTouched = false;
+ this._dragging = false;
+ this._touchMoved = false;
- cc.ArrayRemoveObject(this._touches, touch);
+ /*cc.ArrayRemoveObject(this._touches, touch);
if (this._touches.length == 0) {
this._dragging = false;
this._touchMoved = false;
- }
+ }*/
},
setContentSize:function (size, isDirectCall) {
if ((isDirectCall != null) && (isDirectCall == true))
this._super(size);
- else
- this.setViewSize(size);
+ else {
+ if (this.getContainer() != null) {
+ this.getContainer().setContentSize(size);
+ this.updateInset();
+ }
+ }
},
getContentSize:function () {
return this._container.getContentSize();
},
+ updateInset:function () {
+ if (this.getContainer() != null) {
+ this._maxInset = this.maxContainerOffset();
+ this._maxInset = cc.p(this._maxInset.x + this._viewSize.width * INSET_RATIO,
+ this._maxInset.y + this._viewSize.height * INSET_RATIO);
+ this._minInset = this.minContainerOffset();
+ this._minInset = cc.p(this._minInset.x - this._viewSize.width * INSET_RATIO,
+ this._minInset.y - this._viewSize.height * INSET_RATIO);
+ }
+ },
+
/**
* Determines whether it clips its children or not.
*/
@@ -519,6 +537,7 @@ cc.ScrollView = cc.Layer.extend({
this.draw(context); // self draw
this._afterDraw();
+
context.restore();
} else {
//TODO visit by WebGL
@@ -607,15 +626,12 @@ cc.ScrollView = cc.Layer.extend({
* @param animated If YES, relocation is animated
*/
_relocateContainer:function (animated) {
- var oldPoint, min, max;
- var newX, newY;
+ var min = this.minContainerOffset();
+ var max = this.maxContainerOffset();
- min = this.minContainerOffset();
- max = this.maxContainerOffset();
-
- oldPoint = this._container.getPosition();
- newX = oldPoint.x;
- newY = oldPoint.y;
+ var oldPoint = this._container.getPosition();
+ var newX = oldPoint.x;
+ var newY = oldPoint.y;
if (this._direction == cc.SCROLLVIEW_DIRECTION_BOTH || this._direction == cc.SCROLLVIEW_DIRECTION_HORIZONTAL) {
newX = Math.min(newX, max.x);
newX = Math.max(newX, min.x);
@@ -642,11 +658,8 @@ cc.ScrollView = cc.Layer.extend({
return;
}
- var newX, newY;
var maxInset, minInset;
-
this._container.setPosition(cc.pAdd(this._container.getPosition(), this._scrollDistance));
-
if (this._bounceable) {
maxInset = this._maxInset;
minInset = this._minInset;
@@ -656,9 +669,9 @@ cc.ScrollView = cc.Layer.extend({
}
//check to see if offset lies within the inset bounds
- newX = Math.min(this._container.getPosition().x, maxInset.x);
+ var newX = Math.min(this._container.getPosition().x, maxInset.x);
newX = Math.max(newX, minInset.x);
- newY = Math.min(this._container.getPosition().y, maxInset.y);
+ var newY = Math.min(this._container.getPosition().y, maxInset.y);
newY = Math.max(newY, minInset.y);
this._scrollDistance = cc.pSub(this._scrollDistance, cc.p(newX - this._container.getPosition().x, newY - this._container.getPosition().y));
@@ -698,22 +711,33 @@ cc.ScrollView = cc.Layer.extend({
*/
_beforeDraw:function () {
if (this._clippingToBounds) {
- var screenPos = this.convertToWorldSpace(this.getParent().getPosition());
- var s = this.getScale();
- s *= cc.Director.getInstance().getContentScaleFactor();
+ var screenPos = this.getParent().getPosition(); //this.convertToWorldSpace(this.getParent().getPosition());
+ var scaleValue = this.getScale();
if (cc.renderContextType == cc.CANVAS) {
var ctx = cc.renderContext;
+
+ var getWidth = (this._viewSize.width * scaleValue);
+ var getHeight = (this._viewSize.height * scaleValue);
+ var startX = screenPos.x * scaleValue;
+ var startY = screenPos.y * scaleValue;
+
ctx.beginPath();
- ctx.rect(screenPos.x, screenPos.y, (this._viewSize.width * s), (this._viewSize.height * s));
+ ctx.rect(startX, startY, getWidth, -getHeight);
ctx.clip();
ctx.closePath();
+
+/* ctx.strokeStyle = "rgba(0,255,0,1)";
+ var vertices1 = [cc.p(startX, startY), cc.p(startX + getWidth, startY),
+ cc.p(startX + getWidth, startY + getHeight),
+ cc.p(startX, startY + getHeight)];
+ cc.drawingUtil.drawPoly(vertices1, 4, true);*/
} else {
// TODO: This scrollview should respect parents' positions
- glEnable(GL_SCISSOR_TEST);
+ //glEnable(GL_SCISSOR_TEST);
//clip
- glScissor(screenPos.x, screenPos.y, (this._viewSize.width * s), (this._viewSize.height * s));
+ //CCEGLView::sharedOpenGLView()->setScissorInPoints(screenPos.x*s, screenPos.y*s, m_tViewSize.width*s, m_tViewSize.height*s);
}
}
},
@@ -725,7 +749,7 @@ cc.ScrollView = cc.Layer.extend({
if (this._clippingToBounds) {
if (cc.renderContextType == cc.WEBGL) {
//TODO disable clip
- glDisable(GL_SCISSOR_TEST);
+ //glDisable(GL_SCISSOR_TEST);
}
}
},
215 extensions/GUI/CCScrollView/CCSorting.js
View
@@ -0,0 +1,215 @@
+/****************************************************************************
+ Copyright (c) 2012 cocos2d-x.org
+ Copyright (c) 2010 Sangwoo Im
+
+ 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.
+ ****************************************************************************/
+
+cc.SortableObject = cc.Class.extend({
+ setObjectID:function (objectId) {
+ },
+ getObjectID:function () {
+ return 0;
+ }
+});
+
+cc.SortedObject = cc.SortableObject.extend({
+ _objectID:0,
+
+ ctor:function () {
+ this._objectID = 0;
+ },
+
+ setObjectID:function (objectID) {
+ this._objectID = objectID;
+ },
+
+ getObjectID:function () {
+ return this._objectID;
+ }
+});
+
+var _compareObject = function (val1, val2) {
+ return (val1.getObjectID() - val2.getObjectID());
+};
+
+cc.ArrayForObjectSorting = cc.Class.extend({
+ _saveObjectArr:[],
+
+ ctor:function () {
+ this._saveObjectArr = [];
+ },
+ /*!
+ * Inserts a given object into array.
+ *
+ * Inserts a given object into array with key and value that are used in
+ * sorting. "value" must respond to message, compare:, which returns
+ * (NSComparisonResult). If it does not respond to the message, it is appended.
+ * If the compare message does not result NSComparisonResult, sorting behavior
+ * is not defined. It ignores duplicate entries and inserts next to it.
+ *
+ * @param object to insert
+ */
+ insertSortedObject:function (addObject) {
+ cc.Assert(addObject instanceof cc.Class, "Invalid parameter.");
+ var idx = this.indexOfSortedObject(addObject);
+ this.insertObject(addObject, idx);
+ },
+
+ /*!
+ * Removes an object in array.
+ *
+ * Removes an object with given key and value. If no object is found in array
+ * with the key and value, no action is taken.
+ *
+ * @param value to remove
+ */
+ removeSortedObject:function (delObject) {
+ if (this.count() == 0) {
+ return;
+ }
+
+ var idx = this.indexOfSortedObject(delObject);
+ if (idx < this.count() && idx != cc.INVALID_INDEX) {
+ var foundObj = this.objectAtIndex(idx);
+ if (foundObj.getObjectID() == delObject.getObjectID()) {
+ this.removeObjectAtIndex(idx);
+ }
+ }
+ },
+
+ /*!
+ * Sets a new value of the key for the given object.
+ *
+ * In case where sorting value must be changed, this message must be sent to
+ * keep consistency of being sorted. If it is changed externally, it must be
+ * sorted completely again.
+ *
+ * @param value to set
+ * @param object the object which has the value
+ */
+ setObjectID_ofSortedObject:function (tag, setObject) {
+ var idx = this.indexOfSortedObject(setObject);
+ if (idx < this.count() && idx != cc.INVALID_INDEX) {
+ var foundObj = this.objectAtIndex(idx);
+ if (foundObj.getObjectID() == setObject.getObjectID()) {
+ this.removeObjectAtIndex(idx);
+ foundObj.setObjectID(tag);
+ this.insertSortedObject(foundObj);
+ }
+ }
+ },
+
+ objectWithObjectID:function (tag) {
+ if (this.count() == 0) {
+ return null;
+ }
+ var foundObj = new cc.SortedObject();
+ foundObj.setObjectID(tag);
+
+ var idx = this.indexOfSortedObject(foundObj);
+ if (idx < this.count() && idx != cc.INVALID_INDEX) {
+ foundObj = this.objectAtIndex(idx);
+ if (foundObj.getObjectID() != tag) {
+ foundObj = null;
+ }
+ }
+ return foundObj;
+ },
+
+ /*!
+ * Returns an object with given key and value.
+ *
+ * Returns an object with given key and value. If no object is found,
+ * it returns nil.
+ *
+ * @param value to locate object
+ * @return object found or nil.
+ */
+ getObjectWithObjectID:function (tag) {
+ return null;
+ },
+
+ /*!
+ * Returns an index of the object with given key and value.
+ *
+ * Returns the index of an object with given key and value.
+ * If no object is found, it returns an index at which the given object value
+ * would have been located. If object must be located at the end of array,
+ * it returns the length of the array, which is out of bound.
+ *
+ * @param value to locate object
+ * @return index of an object found
+ */
+ indexOfSortedObject:function (idxObj) {
+ var idx = 0;
+ if (idxObj) {
+ // CCObject* pObj = (CCObject*)bsearch((CCObject*)&object, data.arr, data.num, sizeof(CCObject*), _compareObject);
+ // FIXME: need to use binary search to improve performance
+ var uPrevObjectID = 0;
+ var uOfSortObjectID = idxObj.getObjectID();
+
+ for (var i = 0; i < this._saveObjectArr.length; i++) {
+ var pSortableObj = this._saveObjectArr[i];
+ var curObjectID = pSortableObj.getObjectID();
+ if ((uOfSortObjectID == curObjectID) ||
+ (uOfSortObjectID >= uPrevObjectID && uOfSortObjectID < curObjectID)) {
+ break;
+ }
+ uPrevObjectID = curObjectID;
+ idx++;
+ }
+ } else {
+ idx = cc.INVALID_INDEX;
+ }
+ return idx;
+ },
+
+ //implement array method
+ count:function () {
+ return this._saveObjectArr.length;
+ },
+
+ lastObject:function () {
+ if (this._saveObjectArr.length == 0)
+ return null;
+ return this._saveObjectArr[this._saveObjectArr.length - 1];
+ },
+
+ objectAtIndex:function (idx) {
+ return this._saveObjectArr[idx];
+ },
+
+ addObject:function (addObj) {
+ this._saveObjectArr.push(addObj);
+ this._saveObjectArr.sort(_compareObject);
+ },
+
+ removeObjectAtIndex:function (idx) {
+ cc.ArrayRemoveObjectAtIndex(this._saveObjectArr, idx);
+ this._saveObjectArr.sort(_compareObject);
+ },
+
+ insertObject:function (addObj, idx) {
+ this._saveObjectArr = cc.ArrayAppendObjectToIndex(this._saveObjectArr, addObj, idx);
+ this._saveObjectArr.sort(_compareObject);
+ }
+});
528 extensions/GUI/CCScrollView/CCTableView.js
View
@@ -0,0 +1,528 @@
+/****************************************************************************
+ Copyright (c) 2012 cocos2d-x.org
+ Copyright (c) 2010 Sangwoo Im
+
+ 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.
+ ****************************************************************************/
+
+cc.TABLEVIEW_FILL_TOPDOWN = 0;
+cc.TABLEVIEW_FILL_BOTTOMUP = 1;
+
+/**
+ * Abstract class for SWTableView cell node
+ */
+cc.TableViewCell = cc.Node.extend({
+ _idx:0,
+
+ ctor:function () {
+ },
+
+ /**
+ * The index used internally by SWTableView and its subclasses
+ */
+ getIdx:function () {
+ return this._idx;
+ },
+ setIdx:function (idx) {
+ this._idx = idx;
+ },
+
+ /**
+ * Cleans up any resources linked to this cell and resets <code>idx</code> property.
+ */
+ reset:function () {
+ this._idx = cc.INVALID_INDEX;
+ },
+
+ setObjectID:function (idx) {
+ this._idx = idx;
+ },
+ getObjectID:function () {
+ return this._idx;
+ }
+});
+
+/**
+ * Sole purpose of this delegate is to single touch event in this version.
+ */
+cc.TableViewDelegate = cc.ScrollViewDelegate.extend({
+ /**
+ * Delegate to respond touch event
+ *
+ * @param table table contains the given cell
+ * @param cell cell that is touched
+ */
+ tableCellTouched:function (table, cell) {
+ }
+});
+
+/**
+ * Data source that governs table backend data.
+ */
+cc.TableViewDataSource = cc.Class.extend({
+ /**
+ * cell height for a given table.
+ *
+ * @param table table to hold the instances of Class
+ * @return cell size
+ */
+ cellSizeForTable:function (table) {
+ return 0;
+ },
+
+ /**
+ * a cell instance at a given index
+ *
+ * @param idx index to search for a cell
+ * @return cell found at idx
+ */
+ tableCellAtIndex:function (table, idx) {
+ return 0;
+ },
+
+ /**
+ * Returns number of cells in a given table view.
+ *
+ * @return number of cells
+ */
+ numberOfCellsInTableView:function (table) {
+ return 0;
+ }
+});
+
+/**
+ * UITableView counterpart for cocos2d for iphone.
+ *
+ * this is a very basic, minimal implementation to bring UITableView-like component into cocos2d world.
+ *
+ */
+cc.TableView = cc.ScrollView.extend({
+ _vOrdering:null,
+ _indices:null,
+ _cellsFreed:null,
+ _dataSource:null,
+ _tableViewDelegate:null,
+ _oldDirection:null,
+
+ ctor:function () {
+ this._super();
+ this._oldDirection = cc.SCROLLVIEW_DIRECTION_NONE;
+ },
+
+ __indexFromOffset:function (offset) {
+ var index = 0;
+ var cellSize = this._dataSource.cellSizeForTable(this);
+
+ switch (this.getDirection()) {
+ case cc.SCROLLVIEW_DIRECTION_HORIZONTAL:
+ index = offset.x / cellSize.width;
+ break;
+ default:
+ index = offset.y / cellSize.height;
+ break;
+ }
+ return index;
+ },
+
+ _indexFromOffset:function (offset) {
+ var maxIdx = this._dataSource.numberOfCellsInTableView(this) - 1;
+
+ var offset1 = new cc.Point(offset.x,offset.y);
+
+ var cellSize = this._dataSource.cellSizeForTable(this);
+ if (this._vOrdering == cc.TABLEVIEW_FILL_TOPDOWN) {
+ offset1.y = this.getContainer().getContentSize().height - offset.y - cellSize.height;
+ }
+ var index = Math.max(0, this.__indexFromOffset(offset1));
+ index = Math.min(index, maxIdx);
+
+ return index;
+ },
+
+ __offsetFromIndex:function (index) {
+ var offset;
+ var cellSize;
+
+ cellSize = this._dataSource.cellSizeForTable(this);
+ switch (this.getDirection()) {
+ case cc.SCROLLVIEW_DIRECTION_HORIZONTAL:
+ offset = cc.p(cellSize.width * index, 0.0);
+ break;
+ default:
+ offset = cc.p(0.0, cellSize.height * index);
+ break;
+ }
+
+ return offset;
+ },
+
+ _offsetFromIndex:function (index) {
+ var offset = this.__offsetFromIndex(index);
+
+ var cellSize = this._dataSource.cellSizeForTable(this);
+ if (this._vOrdering == cc.TABLEVIEW_FILL_TOPDOWN) {
+ offset.y = this.getContainer().getContentSize().height - offset.y - cellSize.height;
+ }
+ return offset;
+ },
+
+ _updateContentSize:function () {
+ var size;
+
+ var cellSize = this._dataSource.cellSizeForTable(this);
+ var cellCount = this._dataSource.numberOfCellsInTableView(this);
+
+ switch (this.getDirection()) {
+ case cc.SCROLLVIEW_DIRECTION_HORIZONTAL:
+ size = cc.SizeMake(cellCount * cellSize.width, cellSize.height);
+ break;
+ default:
+ size = cc.SizeMake(cellSize.width, cellCount * cellSize.height);
+ break;
+ }
+ this.setContentSize(size);
+
+ if (this._oldDirection != this._direction) {
+ if (this._direction == cc.SCROLLVIEW_DIRECTION_HORIZONTAL) {
+ this.setContentOffset(cc.p(0, 0));
+ } else {
+ this.setContentOffset(cc.p(0, this.minContainerOffset().y));
+ }
+ this._oldDirection = this._direction;
+ }
+ },
+
+ _cellWithIndex:function (cellIndex) {
+ var found = null;
+
+// if ([this._indices containsIndex:cellIndex])
+ if (this._indices.containsObject(cellIndex)) {
+ found = this._cellsUsed.objectWithObjectID(cellIndex);
+ }
+
+ return found;
+ },
+
+ _moveCellOutOfSight:function (cell) {
+ this._cellsFreed.addObject(cell);
+ this._cellsUsed.removeSortedObject(cell);
+ //cc.ArrayRemoveObjectAtIndex(this._indices,cell.getIdx());
+ //this._indices.erase(cell.getIdx());
+ this._indices.removeObject(cell.getIdx());
+
+ cell.reset();
+ if (cell.getParent() == this.getContainer()) {
+ this.getContainer().removeChild(cell, true);
+ }
+ },
+
+ _setIndexForCell:function (index, cell) {
+ cell.setAnchorPoint(cc.p(0.0, 0.0));
+ cell.setPosition(this._offsetFromIndex(index));
+ cell.setIdx(index);
+ },
+
+ _addCellIfNecessary:function (cell) {
+ if (cell.getParent() != this.getContainer()) {
+ this.getContainer().addChild(cell);
+ }
+ this._cellsUsed.insertSortedObject(cell);
+ this._indices.addObject(cell.getIdx());
+ },
+
+ /**
+ * data source
+ */
+ getDataSource:function () {
+ return this._dataSource;
+ },
+ setDataSource:function (source) {
+ this._dataSource = source;
+ },
+
+ /**
+ * delegate
+ */
+ getDelegate:function () {
+ return this._tableViewDelegate;
+ },
+
+ setDelegate:function (delegate, isDirectCall) {
+ if (isDirectCall != null && isDirectCall == true) {
+ this._super(delegate);
+ return;
+ }
+ this._tableViewDelegate = delegate;
+ },
+
+ /**
+ * determines how cell is ordered and filled in the view.
+ */
+ setVerticalFillOrder:function (fillOrder) {
+ if (this._vOrdering != fillOrder) {
+ this._vOrdering = fillOrder;
+ if (this._cellsUsed.count() > 0) {
+ this.reloadData();
+ }
+ }
+ },
+ getVerticalFillOrder:function () {
+ return this._vOrdering;
+ },
+
+ initWithViewSize:function (size, container) {
+ if (this._super(size, container)) {
+ this._cellsUsed = new cc.ArrayForObjectSorting();
+ this._cellsFreed = new cc.ArrayForObjectSorting();
+ this._indices = new cc.Set();
+ this._tableViewDelegate = null;
+ this._vOrdering = cc.TABLEVIEW_FILL_BOTTOMUP;
+ this.setDirection(cc.SCROLLVIEW_DIRECTION_VERTICAL);
+
+ this.setDelegate(this, true);
+ return true;
+ }
+ return false;
+ },
+
+ /**
+ * Updates the content of the cell at a given index.
+ *
+ * @param idx index to find a cell
+ */
+ updateCellAtIndex:function (idx) {
+ if (idx == cc.INVALID_INDEX || idx > this._dataSource.numberOfCellsInTableView(this) - 1) {
+ return;
+ }
+
+ var cell = this._cellWithIndex(idx);
+ if (cell) {
+ this._moveCellOutOfSight(cell);
+ }
+
+ cell = this._dataSource.tableCellAtIndex(this, idx);
+ this._setIndexForCell(idx, cell);
+ this._addCellIfNecessary(cell);
+ },
+
+ /**
+ * Inserts a new cell at a given index
+ *
+ * @param idx location to insert
+ */
+ insertCellAtIndex:function (idx) {
+ if (idx == cc.INVALID_INDEX || idx > this._dataSource.numberOfCellsInTableView(this) - 1) {
+ return;
+ }
+
+ var newIdx;
+
+ var cell = this._cellsUsed.objectWithObjectID(idx);
+ if (cell) {
+ newIdx = this._cellsUsed.indexOfSortedObject(cell);
+ for (var i = newIdx; i < this._cellsUsed.count(); i++) {
+ cell = this._cellsUsed.objectAtIndex(i);
+ this._setIndexForCell(cell.getIdx() + 1, cell);
+ }
+ }
+
+ //insert a new cell
+ cell = this._dataSource.tableCellAtIndex(this, idx);
+ this._setIndexForCell(idx, cell);
+ this._addCellIfNecessary(cell);
+
+ this._updateContentSize();
+ },
+
+ /**
+ * Removes a cell at a given index
+ *
+ * @param idx index to find a cell
+ */
+ removeCellAtIndex:function (idx) {
+ if (idx == cc.INVALID_INDEX || idx > this._dataSource.numberOfCellsInTableView(this) - 1) {
+ return;
+ }
+
+ var cell = this._cellWithIndex(idx);
+ if (!cell) {
+ return;
+ }
+
+ var newIdx = this._cellsUsed.indexOfSortedObject(cell);
+
+ //remove first
+ this._moveCellOutOfSight(cell);
+
+ this._indices.removeObject(idx);
+ //cc.ArrayRemoveObjectAtIndex(this._indices,idx);
+
+ for (var i = this._cellsUsed.count() - 1; i > newIdx; i--) {
+ cell = this._cellsUsed.objectAtIndex(i);
+ this._setIndexForCell(cell.getIdx() - 1, cell);
+ }
+ },
+
+ /**
+ * reloads data from data source. the view will be refreshed.
+ */
+ reloadData:function () {
+ for (var i = 0; i < this._cellsUsed.count(); i++) {
+ var cell = this._cellsUsed.objectAtIndex(i);
+ this._cellsFreed.addObject(cell);
+ cell.reset();
+ if (cell.getParent() == this.getContainer()) {
+ this.getContainer().removeChild(cell, true);
+ }
+ }
+
+ this._indices = new cc.Set();
+ this._cellsUsed = new cc.ArrayForObjectSorting();
+
+ this._updateContentSize();
+ if (this._dataSource.numberOfCellsInTableView(this) > 0) {
+ this.scrollViewDidScroll(this);
+ }
+ },
+
+ /**
+ * Dequeues a free cell if available. nil if not.
+ *
+ * @return free cell
+ */
+ dequeueCell:function () {
+ if (this._cellsFreed.count() == 0) {
+ return null;
+ } else {
+ var cell = this._cellsFreed.objectAtIndex(0);
+ this._cellsFreed.removeObjectAtIndex(0);
+ return cell;
+ }
+ },
+
+ /**
+ * Returns an existing cell at a given index. Returns nil if a cell is nonexistent at the moment of query.
+ *
+ * @param idx index
+ * @return a cell at a given index
+ */
+ cellAtIndex:function (idx) {
+ return this._cellWithIndex(idx);
+ },
+
+ scrollViewDidScroll:function (view) {
+ var idx = 0;
+
+ var offset = cc.pMult(this.getContentOffset(), -1);
+ var maxIdx = Math.max(this._dataSource.numberOfCellsInTableView(this) - 1, 0);
+
+ var cellSize = this._dataSource.cellSizeForTable(this);
+
+ if (this._vOrdering == cc.TABLEVIEW_FILL_TOPDOWN) {
+ offset.y = offset.y + this._viewSize.height / this.getContainer().getScaleY() - cellSize.height;
+ }
+ var startIdx = 0 | this._indexFromOffset(offset);
+
+ if (this._vOrdering == cc.TABLEVIEW_FILL_TOPDOWN) {
+ offset.y -= this._viewSize.height / this.getContainer().getScaleY();
+ } else {
+ offset.y += this._viewSize.height / this.getContainer().getScaleY();
+ }
+ offset.x += this._viewSize.width / this.getContainer().getScaleX();
+
+ var endIdx = 0 | this._indexFromOffset(offset);
+
+ var cell;
+ if (this._cellsUsed.count() > 0) {
+ cell = this._cellsUsed.objectAtIndex(0);
+ idx = cell.getIdx();
+ while (idx < startIdx) {
+ this._moveCellOutOfSight(cell);
+ if (this._cellsUsed.count() > 0) {
+ cell = this._cellsUsed.objectAtIndex(0);
+ idx = cell.getIdx();
+ } else {
+ break;
+ }
+ }
+ }
+
+ if (this._cellsUsed.count() > 0) {
+ cell = this._cellsUsed.lastObject();
+ idx = cell.getIdx();
+ while (idx <= maxIdx && idx > endIdx) {
+ this._moveCellOutOfSight(cell);
+ if (this._cellsUsed.count() > 0) {
+ cell = this._cellsUsed.lastObject();
+ idx = cell.getIdx();
+ } else {
+ break;
+ }
+ }
+ }
+
+ for (var i = startIdx; i <= endIdx; i++) {
+ if (this._indices.containsObject(i)) {
+ continue;
+ }
+ this.updateCellAtIndex(i);
+ }
+ },
+
+ scrollViewDidZoom:function (view) {
+ },
+
+ onTouchEnded:function (touch, event) {
+ if (!this.isVisible()) {
+ return;
+ }
+ if (this._touches.length == 1 && !this.isTouchMoved()) {
+ var point = this.getContainer().convertTouchToNodeSpace(touch);
+ if (this._vOrdering == cc.TABLEVIEW_FILL_TOPDOWN) {
+ var cellSize = this._dataSource.cellSizeForTable(this);
+ point.y -= cellSize.height;
+ }
+ var index = this._indexFromOffset(point);
+ var cell = this._cellWithIndex(index);
+
+ if (cell) {
+ this._tableViewDelegate.tableCellTouched(this, cell);
+ }
+ }
+ this._super(touch, event);
+ }
+});
+
+/**
+ * An initialized table view object
+ *
+ * @param dataSource data source;
+ * @param size view size
+ * @param container parent object for cells
+ * @return table view
+ */
+cc.TableView.create = function (dataSource, size, container) {
+ var table = new cc.TableView();
+ table.initWithViewSize(size, container);
+ table.setDataSource(dataSource);
+ table._updateContentSize();
+ return table;
+};
Please sign in to comment.
Something went wrong with that request. Please try again.