-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.js
150 lines (113 loc) · 3.15 KB
/
menu.js
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
function Menu(w, h){
this.width = w;
this.height = h;
this.title = "";
this.current_option = 0;
this.options = [];
this.settings = [];
this.settings.bg_color = "white";
this.settings.options_x_pos = 50;
this.settings.options_y_pos = 150;
this.settings.line_height = 20;
this.settings.selector_radius = 4;
this.settings.title_font ="64px Arial";
this.settings.title_font_color = "black";
this.settings.title_x_pos = 40;
this.settings.title_y_pos = 100;
this.font = "14px Arial";
this.font_color = "black";
this.child_nodes = [];
this.init = function(){
/* used to change settings */
}
this.optionUp = function(){
this.current_option--;
if(this.current_option < 0 ){
this.current_option = this.options.length - 1;
}
}
this.optionDown = function(){
this.current_option++;
this.current_option = this.current_option % this.options.length;
}
this.renderTitle = function(ctx){
//render title
ctx.font = this.settings.title_font;
ctx.fillStyle = this.settings.title_font_color;
ctx.fillText(this.title, this.settings.title_x_pos, this.settings.title_y_pos);
}
this.renderSelector = function(ctx,x, y){
ctx.beginPath();
ctx.arc(x, y, this.settings.selector_radius, 0, Math.PI*2);
ctx.fill();
ctx.closePath();
}
this.renderOptions = function(ctx){
// loop through options....write text in middle of screen
ctx.font = this.font;//
ctx.fillStyle=this.font_color;
for(var i = 0; i < this.options.length;i++){
// display current choice
if(this.current_option == i){
this.renderSelector(ctx, this.settings.options_x_pos - 3*this.settings.selector_radius,
this.settings.options_y_pos-this.settings.selector_radius*2 +this.settings.line_height*i);
}
if(typeof(this.options[i]) == "string"){
ctx.fillText(this.options[i], this.settings.options_x_pos, this.settings.options_y_pos + this.settings.line_height * i );
}else{
this.options[i].render(ctx);
}
}
}
this.renderBackground = function(ctx){
ctx.fillStyle = this.settings.bg_color;
ctx.fillRect(0,0, this.width, this.height);
}
this.render = function(ctx){
this.renderBackground(ctx);
this.renderTitle(ctx);
this.renderOptions(ctx);
for(let i=0;i<this.child_nodes.length;i++){
this.child_nodes[i].render(ctx);
}
}
}
function MenuOption(x, y){
this.type = "text";
this.value = "";
this.x = x;
this.y = y;
this.line_height = 20;
this.render = function(ctx){
if(this.type == "text"){
ctx.fillText(this.value, this.x, this.y - this.line_height );
}
}
}
function InputMenuOption(x, y ){
MenuOption.call(this, x, y);
this.type = "input";
this.max_length = 255;
this.render = function(ctx){
ctx.fillText(this.value, this.x, this.y);
}
}
function InitialsMenuOption(x,y){
InputMenuOption.call(this,x,y);
this.type = "input-initials";
this.max_length = 3;
this.render = function(ctx){
ctx.save();
ctx.font = "32px 'Press Start 2p'";
ctx.lineWidth = 3;
for(var i=0;i<this.max_length;i++){
ctx.beginPath();
ctx.moveTo(this.x + 32*i, this.y);
ctx.lineTo(this.x+32*i+28, this.y);
ctx.stroke();
ctx.closePath();
}
ctx.fillText(this.value, this.x, this.y);
ctx.restore();
}
}