1 /************************************************************************
2 This file is part of NE.
4 NE is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 NE is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with NE. If not, see <http://www.gnu.org/licenses/>.
16 ************************************************************************/
19 #include "backend/video.h"
20 #include "backend/input.h"
21 #include "backend/shapes.h"
28 Menu::Menu(std::string name
, Menu::MENU_MODE m
)
29 : Element(name
), m_background(0), m_selected(0)
31 m_keyrep
.setDelay(150);
32 m_keyrep
.setTime(500);
42 void Menu::setDefaults()
44 m_entry_spacing
= 2.0; // units step per entry
45 m_entry_padding
= 3.0; // units of padding around the entry
46 m_entry_distance
= -9.0; // z distance from camera
50 m_rot_friction
= 1.0 - 0.2;
54 void Menu::addChild(Element
*e
)
56 m_children
.push_back(e
);
61 if (m_mode
== OVERLAY
) {
62 // take snapshot of background to render later
66 m_background
= Texture::Screenshot();
77 int result
= handleInput();
82 else if (result
== 1) {
83 ret
= m_children
[m_selected
]->run();
97 int Menu::handleInput()
100 keysym_t
*key
= i_keystate();
105 else if (key
[K_UP
]) {
106 if (m_selected
!= 0 && m_keyrep
.repeat()) {
110 else if (key
[K_DOWN
]) {
111 if (m_selected
+1 < m_children
.size() && m_keyrep
.repeat()) {
115 else if (key
[K_RETURN
]) {
116 if (m_keyrep
.repeat()) {
129 static float brightness
= 0.4;
130 static float bg_col
[] = { 0, 0, 0 };
131 static int bg_last
= rand()%3;
132 static int bg_curr
= -1;
133 //bg_col[bg_last] = 1;
135 if (m_mode
== BACKGROUND
) {
138 while (bg_last
== bg_curr
) bg_curr
= rand()%3;
140 if (bg_col
[bg_curr
] >= 1) {
144 bg_col
[bg_curr
] += 0.01;
145 bg_col
[bg_last
] -= 0.01;
147 glClearColor(bg_col
[0]*brightness
, bg_col
[1]*brightness
, bg_col
[2]*brightness
, 0);
150 int draw_start
= m_selected
-m_entry_padding
;
151 int draw_end
= m_selected
+m_entry_padding
+1;
153 int lower_bound
= m_children
.size()-1;
155 glClear(GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
157 if (m_mode
== OVERLAY
) {
159 glDisable(GL_DEPTH_TEST
);
160 glMatrixMode(GL_MODELVIEW
);
162 glMatrixMode(GL_PROJECTION
);
165 m_background
->apply();
166 glScalef(2.0, 2.0, 1.0);
169 glMatrixMode(GL_MODELVIEW
);
173 glMatrixMode(GL_MODELVIEW
);
175 glTranslatef(0,0,-10);
177 for (int i
=draw_start
; i
!=draw_end
; i
++) {
178 //float r = m_rot_curr - m_entry_spacing*i;
179 float r
= m_rot_curr
- i
* m_entry_spacing
;
182 glRotatef(r
*10,1,0,0);
183 glTranslatef(0,0,m_entry_distance
);
187 //glColor4f(1,1,1,1);
190 //glColor4f(1,1,1,0);
195 if (i
>= upper_bound
&& i
<= lower_bound
)
196 m_children
[i
]->draw();
201 void Menu::simulate()
203 m_rot_target
= m_selected
*m_entry_spacing
;
205 // smoothly scroll the list
206 float delta
= m_rot_target
-m_rot_curr
;
211 printf("***DEBUG***\n");
212 printf("large delta detected between current position and target (%.2f)\n", delta
);
213 printf("current rot is %.2f and target rot is %.2f\n", m_rot_curr
, m_rot_target
);
216 if (m_rot_vel
> 1.0) {
217 printf("***DEBUG***\n");
218 printf("large vel detected (%.2f); target is %.2f and curr is %d\n",
219 m_rot_vel
, m_rot_target
, m_rot_curr
);
220 printf("selected %.2f and spacing %d\n", m_selected
, m_entry_spacing
);
223 if (m_rot_friction
> 1.0 || m_rot_friction
< 0.0) {
224 printf("***DEBUG***\n");
225 printf("friction value of %.2f doesn't make sense\n", m_rot_friction
);
231 m_rot_vel
+= m_rot_accel
* delta
;
232 m_rot_curr
+= m_rot_vel
;
235 m_rot_vel
*= m_rot_friction
;
238 void Menu::init_perspective()
240 glMatrixMode(GL_PROJECTION
);
243 double w
= v_info()->width
, h
= v_info()->height
;
246 gluPerspective(45,aspect
,1,1001);
248 glEnable(GL_DEPTH_TEST
);
251 glEnable(GL_CULL_FACE
);
254 glShadeModel(GL_SMOOTH
);
256 glEnable(GL_TEXTURE_2D
);
258 glBlendFunc(GL_SRC_ALPHA
,GL_ONE_MINUS_SRC_ALPHA
);