/
OptionsLayer.cpp
207 lines (155 loc) · 5.67 KB
/
OptionsLayer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "pch.h"
#include "OptionsLayer.h"
#include "cocos2d.h"
int _nameTag = 1234;
int goalTag = 111;
int timerTag = 222;
using namespace SoccerGlobal;
OptionsLayer::OptionsLayer()
{
init();
}
OptionsLayer::~OptionsLayer()
{
}
bool OptionsLayer::init()
{
bool bRet = false;
do
{
CC_BREAK_IF(! OverlayBase::init());
CCSize size = CCDirector::sharedDirector()->getWinSize();
int leftSpace = 100;
setTouchEnabled(true);
CCSprite* bg = CCSprite::create(SETTINGS_BG_NAME.c_str());
bg->setAnchorPoint(ccp(0,0));
bg->setPosition(ccp(0, 0));
addChild(bg, -1);
//Cancel - Go Menu
CCMenuItemSprite* mRightArrow = sgl_SpriteFactory.createMenuItemSprite("btnArrow_Back128x128.png", "btnArrow_Back128x128Pressed.png", menu_selector(OverlayBase::onQuit), this);
CCMenu* menuReturn = CCMenu::create(mRightArrow, NULL);
CCMenuItemSprite* mLeftArrow =
sgl_SpriteFactory.createMenuItemSprite("btnArrow128x128.png", "btnArrow128x128.png", menu_selector(OptionsLayer::onApply), this);
CCMenu* mStartGame = CCMenu::create(mLeftArrow, NULL);
menuReturn->setPosition(leftSpace, 150);
mStartGame->setPosition(size.width - leftSpace - mLeftArrow->boundingBox().size.width * 0.5f, 150);
addChild(mStartGame);
addChild(menuReturn);
initControls();
bRet = true;
} while (0);
return bRet;
}
void OptionsLayer::onApply(CCObject* pSender)
{
GameOptions opt;
int goals = 0;
goals = _goalNoSlider->getValue();
opt.Score = goals;
CCUserDefault::sharedUserDefault()->setIntegerForKey(GOALS_DATA_NAME, goals);
std::string name = _txtName->getText();
if (!name.empty())
{
CCUserDefault::sharedUserDefault()->setStringForKey(USER_DATA_NAME, name);
}
sgl_Settings.setOptions(opt);
OverlayBase::onQuit(pSender);
}
/*
* Controls Events
*/
void OptionsLayer::sliderEvent(CCObject *pSender, CCControlEvent controlEvent)
{
CCControlSlider* pSlider = (CCControlSlider*)pSender;
int percent = pSlider->getValue();
std::string s = "g:";
s += percent;
_lblGoalSelection->setString(s.c_str());
}
/*
* Control initialization
*/
void OptionsLayer::initControls()
{
int xPos = 100;
int fontSize = 35;
CCSize visibleSize = CCEGLView::sharedOpenGLView()->getVisibleSize();
//Labels
CCLabelTTF* lblName = sgl_ControlFactory.createTTFLabel("Name:", ccp(xPos, 500));
this->addChild(lblName);
CCLabelTTF* lblGoal = sgl_ControlFactory.createTTFLabel("Goals:", ccp(xPos, 400));
this->addChild(lblGoal);
CCLabelTTF* lblColor = sgl_ControlFactory.createTTFLabel("Time:", ccp(xPos, 300));
this->addChild(lblColor);
//Controls
int controlLimit = xPos + lblGoal->boundingBox().size.width + 20;
std::string userName = CCUserDefault::sharedUserDefault()->getStringForKey(USER_DATA_NAME);
if (userName.empty())
{
userName = "anonymus";
}
CCSize editBoxSize = CCSizeMake(visibleSize.width * 0.2f, 50);
CCScale9Sprite* sp = CCScale9Sprite::create("textbg.png");
_txtName = CCEditBox::create(editBoxSize, sp);
_txtName->setPosition(ccp(controlLimit+10, 480));
_txtName->setFontName(S_F_TYPE.c_str());
_txtName->setFontSize(fontSize);
_txtName->setFontColor(ccRED);
_txtName->setPlaceHolder(userName.c_str());
_txtName->setPlaceholderFontColor(ccWHITE);
_txtName->setMaxLength(15);
_txtName->setReturnType(kKeyboardReturnTypeDone);
_txtName->setDelegate(this);
_txtName->setZOrder(1);
_txtName->setAnchorPoint(ccp(0,0));
addChild(_txtName);
int goals = CCUserDefault::sharedUserDefault()->getIntegerForKey(GOALS_DATA_NAME);
if (goals < 1) goals = 10;
_goalNoSlider = CCControlSlider::create("sliderTrack.png", "sliderProgress.png", "sliderThumb.png");
_goalNoSlider->setTouchEnabled(true);
_goalNoSlider->setMinimumValue(1.0f); // Sets the min value of range
_goalNoSlider->setMaximumValue(goals); // Sets the max value of range
_goalNoSlider->setPosition(ccp(controlLimit+100, 400));
_goalNoSlider->setAnchorPoint(ccp(0,0));
_goalNoSlider->addTargetWithActionForControlEvents(this, cccontrol_selector(OptionsLayer::sliderEvent), CCControlEventValueChanged);
_goalNoSlider->setTag(goalTag);
_lblGoalSelection = sgl_ControlFactory.createTTFLabel("", ccp(controlLimit+_goalNoSlider->boundingBox().size.width+100, 400));
this->addChild(_lblGoalSelection);
_timeSlider = CCControlSlider::create("sliderTrack.png", "sliderProgress.png", "sliderThumb.png");
_timeSlider->setTouchEnabled(true);
_timeSlider->setPosition(ccp(controlLimit+100, 300));
_timeSlider->setAnchorPoint(ccp(0,0));
_timeSlider->addTargetWithActionForControlEvents(this, cccontrol_selector(OptionsLayer::sliderEvent), CCControlEventValueChanged);
_timeSlider->setTag(timerTag);
addChild(_goalNoSlider);
addChild(_timeSlider);
}
/* Delegate thinggy */
void OptionsLayer::editBoxEditingDidBegin(cocos2d::extension::CCEditBox* editBox)
{
CCLog("editBox %p DidBegin !", editBox);
}
void OptionsLayer::editBoxEditingDidEnd(cocos2d::extension::CCEditBox* editBox)
{
CCLog("editBox %p DidEnd !", editBox);
}
void OptionsLayer::editBoxTextChanged(cocos2d::extension::CCEditBox* editBox, const std::string& text)
{
CCLog("editBox %p TextChanged, text: %s ", editBox, text.c_str());
}
void OptionsLayer::editBoxReturn(CCEditBox* editBox)
{
CCLog("editBox %p was returned !",editBox);
/*if (m_pEditName == editBox)
{
m_pTTFShowEditReturn->setString("Name EditBox return !");
}
else if (m_pEditPassword == editBox)
{
m_pTTFShowEditReturn->setString("Password EditBox return !");
}
else if (m_pEditEmail == editBox)
{
m_pTTFShowEditReturn->setString("Email EditBox return !");
}*/
}