Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/html/play.js
blob: 65a1ec8c5274624c04addcdcdda776e7f2d279ee (plain)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257


// Collections size on the screen
var entriesByGame = 4;


// Learn app class
enyo.kind({
	name: "Abcd.Play",
	kind: enyo.Control,
	classes: "board",
	components: [
		{kind: "Signals", onEndOfSound: "endSound"},
		{components: [
			{name: "colorBar", classes: "colorBar"},		
			{name: "home", kind: "Abcd.HomeButton"},
			{kind: "Abcd.CaseButton"},
			{kind: "Abcd.LanguageButton"}
		]},
		{components: [
			{name: "itemCount", content: "-/-", classes: "pageCount", showing: false},		
			{name: "back", kind: "Image", src: "images/back.png", showing: false, classes: "backButton", ontap: "backTaped"},
			{name: "check", kind: "Image", src: "images/check.png", showing: false, classes: "checkButton", ontap: "checkTaped"}
		]},		
		{name: "box", classes: "playbox", components: [
		]}	
	],
	
	// Constructor
	create: function() {
		this.inherited(arguments);
		
		this.theme = -1;
		this.themeButton = null;
		this.gamecount = 0;
		this.playing = null;
		this.from = null;
		this.selected = null;
		this.forbidentry = false;
		
		this.displayButtons();
	},
	
	// Delete all the box content
	cleanBox: function() {
		var items = [];
		enyo.forEach(this.$.box.getControls(), function(item) {
			items.push(item);
		});		
		for (var i = 0 ; i < items.length ; i++) {
			items[i].destroy();
		}	
	},
	
	// Display game choice buttons
	displayButtons: function() {
		this.cleanBox();
		Abcd.changeVisibility(this, {home: true, back: false, check: false, itemCount: false});
		this.$.colorBar.removeClass("themeColor"+this.theme);
		this.theme = -1;
		this.$.colorBar.addClass("themeColor"+this.theme);
		
		// Draw From picture buttons
		this.$.box.createComponent(
			{kind: "Abcd.PlayTypeButton", from: "picture", to: "letter"+Abcd.context.casevalue, ontap: "doGame", theme: "play-button-color1"},
			{owner: this}
		).render();
		this.$.box.createComponent(
			{kind: "Abcd.PlayTypeButton", from: "picture", to: "listen", ontap: "doGame", theme: "play-button-color1"},
			{owner: this}
		).render();
		
		// Draw From letter buttons
		this.$.box.createComponent(
			{kind: "Abcd.PlayTypeButton", from: "letter"+Abcd.context.casevalue, to: "picture", ontap: "doGame", theme: "play-button-color2"},
			{owner: this}
		).render();
		this.$.box.createComponent(
			{kind: "Abcd.PlayTypeButton", from: "letter"+Abcd.context.casevalue, to: "listen", ontap: "doGame", theme: "play-button-color2"},
			{owner: this}
		).render();

		// Draw From listen buttons
		this.$.box.createComponent(
			{ kind: "Abcd.PlayTypeButton", from: "listen", to: "picture", ontap: "doGame", theme: "play-button-color3" },
			{ owner: this }
		).render();
		this.$.box.createComponent(
			{ kind: "Abcd.PlayTypeButton", from: "listen", to: "letter"+Abcd.context.casevalue, ontap: "doGame", theme: "play-button-color3" },
			{ owner: this }
		).render();		
	},
	
	// Localization changed
	setLocale: function() {
		// If playing, change game because could inexist in the current language
		if (this.theme != -1)
			this.computeGame();
	},
	
	// Case changed
	setCase: function() {
		enyo.forEach(this.$.box.getControls(), function(item) {
			if (item.kind == 'Abcd.Entry')
				item.indexChanged();
			else
				item.setCase();				
		});		
	},
	
	// Convert value to entry option
	convertToEntryOption: function(value) {
		return {
			soundonly: value == "listen",
			imageonly: value == "picture",
			textonly: value.substr(0, 6) == "letter"
		};
	},
	
	// Start game
	doGame: function(button, event) {		
		// Redraw bar
		Abcd.changeVisibility(this, {home: false, back: true, check: true, itemCount: true});
		this.themeButton = button;
		if (this.themeButton.from == "picture") this.theme = 5;
		else if (this.themeButton.from == "listen") this.theme = 7;
		else this.theme = 6;
		this.$.colorBar.removeClass("themeColor-1");
		this.$.colorBar.addClass("themeColor"+this.theme);
		
		// Compute game
		this.gamecount = 0;
		this.computeGame();
	},
	
	// Compute game
	computeGame: function() {
		// Compute card to find
		this.cleanBox();
		this.forbidentry = false;
		this.$.itemCount.setContent((this.gamecount+1)+"/"+entriesByGame);
		var tofind = Abcd.randomEntryIndex();
		var options = this.convertToEntryOption(this.themeButton.from);		
		var fromEntry = this.from = this.$.box.createComponent(
			{kind: "Abcd.Entry", index:tofind, soundonly: options["soundonly"], imageonly: options["imageonly"], textonly: options["textonly"], ontap: "entryTaped"},
			{owner: this}
		);
		fromEntry.addClass("entryPlayFrom");
		fromEntry.render();
		
		// Play from card if its a sound
		if (options["soundonly"]) {
			this.playing = fromEntry;
			this.playing.play(Abcd.sound);	
		}
		
		// Compute cards to choose
		var excludes = [];
		excludes.push(tofind);
		for (var i = 0 ; i < 2 ; i++) {
			var wrong = Abcd.randomEntryIndex(excludes);
			excludes.push(wrong);
		}
		excludes = Abcd.mix(excludes);
		
		// Draw cards
		var len = excludes.length;
		for (var i = 0 ; i < len ; i++) {
			options = this.convertToEntryOption(this.themeButton.to);
			var toEntry = this.$.box.createComponent(
				{kind: "Abcd.Entry", index:excludes[i], soundonly: options["soundonly"], imageonly: options["imageonly"], textonly: options["textonly"], ontap: "entryTaped"},
				{owner: this}
			);
			toEntry.addClass("entryPlayTo");
			toEntry.render();
		}	
	},
		
	// Entry taped play sound and/or select entry
	entryTaped: function(entry, event) {
		// No selection now
		if (this.forbidentry)
			return;
			
		// Play sound
		if (entry.soundonly) {
			if (this.playing != null)
				this.playing.abort();
			this.playing = entry;
			this.playing.play(Abcd.sound);
		}
		
		// Don't select the from entry
		if (entry.hasClass("entryPlayFrom"))
			return;
		
		// Select the entry
		if (!entry.hasClass("entryPlaySelected")) {
			if (this.selected != null)
				this.selected.removeClass("entryPlaySelected");
			entry.addClass("entryPlaySelected");
			this.selected = entry;
		}
	},
	
	// Go to the home of the game
	backTaped: function() {
		this.$.colorBar.removeClass("themeColor"+this.theme);
		this.theme = -1;
		this.displayButtons();
	},
	
	// Check taped
	checkTaped: function() {
		this.forbidentry = true;
		if (this.playing != null)
			this.playing.abort();		
		if (this.selected == null) {
			Abcd.sound.play("audio/disappointed");
			this.selected = this.from;
			this.selected.addClass("entryPlayWrong");
		} else if (this.selected.index == this.from.index) {
			this.check4end = true;
			Abcd.sound.play("audio/applause");
			this.selected.removeClass("entryPlaySelected");
			this.selected.addClass("entryPlayRight");
		} else {
			Abcd.sound.play("audio/disappointed");
			this.selected.removeClass("entryPlaySelected");
			this.selected.addClass("entryPlayWrong");
		}
	},
	
	// End sound
	endSound: function(e, s) {
		// Bad check, retry
		if (s == "audio/disappointed") {
			this.selected.removeClass("entryPlaySelected");
			this.selected.removeClass("entryPlayWrong");
			this.selected = null;
			this.forbidentry = false;
			
		// Good check
		} else if (s == "audio/applause") {
			// Clean state
			this.selected.removeClass("entryPlaySelected");
			this.selected.removeClass("entryPlayRight");
			this.selected = null;	
			
			// Next game or try another game
			if ( ++this.gamecount == entriesByGame )
				this.displayButtons();
			else
				this.computeGame();
		}
	}
});