Skip to content

Commit 90cc8fe

Browse files
author
SeanLin
committed
Merge pull request cocos2d#513 from ShengxiangChen/issue1444_UserDefault
fixed cocos2d#1444: Add UserData default feature
2 parents 41a4e85 + 7a274b3 commit 90cc8fe

File tree

2 files changed

+265
-0
lines changed

2 files changed

+265
-0
lines changed

cocos2d/platform/jsloader.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ var cc = cc || cc || {};
4343
'cocoa/CCNS.js',
4444
'cocoa/CCAffineTransform.js',
4545
'support/CCPointExtension.js',
46+
'support/CCUserDefault.js',
4647
'base_nodes/CCNode.js',
4748
'base_nodes/CCAtlasNode.js',
4849
'textures/CCTexture2D.js',

cocos2d/support/CCUserDefault.js

Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
/****************************************************************************
2+
Copyright (c) 2010-2012 cocos2d-x.org
3+
Copyright (c) 2008-2010 Ricardo Quesada
4+
Copyright (c) 2011 Zynga Inc.
5+
6+
http://www.cocos2d-x.org
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a copy
9+
of this software and associated documentation files (the "Software"), to deal
10+
in the Software without restriction, including without limitation the rights
11+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
copies of the Software, and to permit persons to whom the Software is
13+
furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in
16+
all copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
THE SOFTWARE.
25+
****************************************************************************/
26+
27+
/**
28+
* <p>cc.UserDefault acts as a tiny localStorage. You can save and get base type values by it. <br/>
29+
* For example, setBoolForKey("played", true) will add a bool value true into the localStorage. <br/>
30+
* Its key is "played". You can get the value of the key by getBoolForKey("played").</p>
31+
*
32+
* <p>It supports the following base types: <br/>
33+
* bool, int, float, double, string</p>
34+
*
35+
* @class
36+
* @extends cc.Class
37+
*/
38+
39+
cc.UserDefault = cc.Class.extend(/** @lends cc.UserDefault# */{
40+
_db:null,
41+
/*
42+
* init user default
43+
* */
44+
init:function () {
45+
this._db = this._getLocalStorage();
46+
return true;
47+
},
48+
49+
_getLocalStorage:function () {
50+
try {
51+
if (!!window.localStorage) {
52+
return window.localStorage;
53+
}
54+
} catch (e) {
55+
return undefined;
56+
}
57+
},
58+
59+
_getWebSqlDatabase:function () {
60+
61+
},
62+
63+
/**
64+
* Get bool value by key, if the key doesn't exist, a default value will return. <br/>
65+
* You can set the default value, or it is false.
66+
*
67+
* @param {String} key
68+
* @param {Boolean} defaultValue
69+
* @return {Boolean}
70+
*/
71+
getBoolForKey:function (key, defaultValue) {
72+
var value = this._getValueForKey(key);
73+
var ret = defaultValue || false;
74+
if (value == "true") {
75+
return true;
76+
}
77+
else if (value == "false") {
78+
return false;
79+
}
80+
else if (value) {
81+
return Boolean(value);
82+
}
83+
84+
return ret;
85+
},
86+
87+
/**
88+
* Get integer value by key, if the key doesn't exist, a default value will return.<br/>
89+
* You can set the default value, or it is 0.
90+
*
91+
* @param {String} key
92+
* @param {Number} defaultValue
93+
* @return {Number}
94+
*/
95+
getIntegerForKey:function (key, defaultValue) {
96+
var value = this._getValueForKey(key);
97+
var ret = defaultValue || 0;
98+
99+
if (value) {
100+
return parseInt(value);
101+
}
102+
103+
return ret;
104+
},
105+
106+
/**
107+
* Get float value by key, if the key doesn't exist, a default value will return.<br/>
108+
* You can set the default value, or it is 0.0f.
109+
*
110+
* @param {String} key
111+
* @param {Number} defaultValue
112+
* @return {Number}
113+
*/
114+
getFloatForKey:function (key, defaultValue) {
115+
var value = this._getValueForKey(key);
116+
var ret = defaultValue || 0.0;
117+
118+
if (value) {
119+
return parseFloat(value);
120+
}
121+
122+
return ret;
123+
},
124+
125+
/**
126+
* Get double value by key, if the key doesn't exist, a default value will return.<br/>
127+
* You can set the default value, or it is 0.0.
128+
*
129+
* @param {String} key
130+
* @param {Number} defaultValue
131+
* @return {Number}
132+
*/
133+
getDoubleForKey:function (key, defaultValue) {
134+
return this.getFloatForKey(key, defaultValue);
135+
},
136+
137+
/**
138+
* Get string value by key, if the key doesn't exist, a default value will return.<br/>
139+
* You can set the default value, or it is "".
140+
*
141+
* @param {String} key
142+
* @param {String} defaultValue
143+
* @return {String}
144+
*/
145+
getStringForKey:function (key, defaultValue) {
146+
var value = this._getValueForKey(key);
147+
var ret = defaultValue || "";
148+
149+
if (value) {
150+
return String(value);
151+
}
152+
153+
return ret;
154+
},
155+
156+
_getValueForKey:function (key) {
157+
var ret;
158+
if (this._db) {
159+
ret = this._db.getItem(key);
160+
}
161+
162+
return ret;
163+
},
164+
165+
/**
166+
* Set bool value by key.
167+
*
168+
* @param {String} key
169+
* @param {Boolean} value
170+
*/
171+
setBoolForKey:function (key, value) {
172+
// save bool value as sring
173+
this.setStringForKey(key, String(value));
174+
},
175+
176+
/**
177+
* Set integer value by key.
178+
*
179+
* @param {String} key
180+
* @param {Number} value
181+
*/
182+
setIntegerForKey:function (key, value) {
183+
// check key
184+
if (!key) {
185+
return;
186+
}
187+
188+
this._setValueForKey(key, parseInt(value));
189+
},
190+
191+
/**
192+
* Set float value by key.
193+
*
194+
* @param {String} key
195+
* @param {Number} value
196+
*/
197+
setFloatForKey:function (key, value) {
198+
// check key
199+
if (!key) {
200+
return;
201+
}
202+
203+
this._setValueForKey(key, parseFloat(value));
204+
},
205+
206+
/**
207+
* Set double value by key.
208+
*
209+
* @param {String} key
210+
* @param {Number} value
211+
*/
212+
setDoubleForKey:function (key, value) {
213+
return this.setFloatForKey(key, value);
214+
},
215+
216+
/**
217+
* Set string value by key.
218+
*
219+
* @param {String} key
220+
* @param {String} value
221+
*/
222+
setStringForKey:function (key, value) {
223+
// check key
224+
if (!key) {
225+
return;
226+
}
227+
228+
this._setValueForKey(key, String(value));
229+
},
230+
231+
_setValueForKey:function (key, value) {
232+
if (this._db) {
233+
this._db.setItem(key, value);
234+
}
235+
}
236+
});
237+
238+
/**
239+
* returns a shared instance of the UserDefault
240+
* @function
241+
* @return {cc.UserDefault|}
242+
*/
243+
cc.UserDefault.getInstance = function () {
244+
if (!this._sUserDefault) {
245+
this._sUserDefault = new cc.UserDefault();
246+
this._sUserDefault.init();
247+
}
248+
249+
return this._sUserDefault;
250+
};
251+
252+
/**
253+
* purge a shared instance of the UserDefault
254+
* @function
255+
* @return {cc.UserDefault|}
256+
*/
257+
cc.UserDefault.purgeInstanceUserDefault = function () {
258+
if (this._db) {
259+
this._db.clear();
260+
}
261+
};
262+
263+
cc.UserDefault._sUserDefault = null;
264+
cc.UserDefault._isFilePathInitialized = false;

0 commit comments

Comments
 (0)