-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCategoriesController.ts
More file actions
127 lines (114 loc) · 3.49 KB
/
CategoriesController.ts
File metadata and controls
127 lines (114 loc) · 3.49 KB
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
/// <reference path="GameLoop.ts" />
module Game{
export class CategoriesController{
gameloop;
canvas;
width;
height;
model;
categoriesView;
startX;
startY;
endX;
endY;
oldY;
startingHeight;
fingerLifted:boolean;
constructor(gameloop,canvas,width,height,model,categoriesView){
this.gameloop = gameloop;
this.canvas = canvas;
this.width = width;
this.height = height;
this.startingHeight = 0;
this.oldY = 0;
this.model = model;
this.categoriesView = categoriesView;
this.categoriesView.setCategories(this.model.Categories,this.model.chosenCategories);
}
takeInput(){
this.Scrolling = <any>this.Scrolling.bind(this);
this.endScrolling = <any>this.endScrolling.bind(this);
this.startClick = <any>this.startClick.bind(this);
this.canvas.addEventListener("touchstart",this.startClick);
this.canvas.addEventListener("touchmove",this.Scrolling);
this.canvas.addEventListener("touchend",this.endScrolling);
}
startClick(event){
event.preventDefault();
var canvas_x = event.targetTouches[0].pageX;
var canvas_y = event.targetTouches[0].pageY;
this.startX = canvas_x;
this.startY = canvas_y;
this.endY = canvas_y;
this.endX = canvas_x;
}
Scrolling(event){
event.preventDefault();
var screenHeight = this.height - (this.height/4);
var buttonHeight = screenHeight/7;
var maxHeight = buttonHeight * this.model.Categories.length;
var canvas_x = event.targetTouches[0].pageX;
var canvas_y = event.targetTouches[0].pageY;
console.log(this.startingHeight + "sh");
if(this.fingerLifted){
this.startX = canvas_x;
this.startY = canvas_y;
}
if(!this.fingerLifted){
var difference = this.oldY - canvas_y;
var newStartingHeight = this.startingHeight+difference;
if(newStartingHeight < 0){
this.startingHeight = 0;
}
else if(newStartingHeight + buttonHeight*4 + -5 > (maxHeight)){
this.startingHeight = this.startingHeight;
}
else{
this.startingHeight = newStartingHeight;
}
this.categoriesView.renderCategories(Math.round(this.startingHeight),this.model.chosenCategories);
}
this.oldY = canvas_y;
this.endY = canvas_y;
this.endX = canvas_x;
this.fingerLifted = false;
}
endScrolling(event){
this.fingerLifted = true;
console.log(this.startX + " " + this.endX);
if(Math.abs(this.startX - this.endX)< 5){
if(Math.abs(this.startY - this.endY) < 5){
this.updateGame(this.endY)
}
}
}
updateGame(canvas_y){
//var canvas_y = event.y;
//canvas_y -= this.canvas.offsetTop;
var screenHeight = this.height - (this.height/4);
var buttonHeight = screenHeight/7;
var gap = 0;
var startingGap = this.height/2.8;
var menuButton = (560/667)*this.height;
var click = this.startingHeight + canvas_y;
if(canvas_y > startingGap && canvas_y <= menuButton){
var i = Math.floor((click - startingGap) / (buttonHeight + gap)); // i
this.model.changeChosenCat(i);
this.categoriesView.renderCategories(this.startingHeight,this.model.chosenCategories)
}
if(canvas_y > menuButton){
var time = new Date().getTime();
this.switchToMenuState(time,1);
}
}
switchStates(){
this.canvas.removeEventListener("touchmove",this.Scrolling);
this.canvas.removeEventListener("touchend",this.endScrolling);
this.canvas.removeEventListener("touchstart",this.startClick);
}
switchToMenuState(time,count){
this.switchStates();
this.gameloop.switchToMenuState();
}
}
}