1 /***************************************************************************
2                           guipage.cpp  -  A page of a menu interface
3                              -------------------
4     begin                : wo dec 15 2004
5     copyright            : (C) 2004 by CJP
6     email                : cornware-cjp@users.sourceforge.net
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #include <GL/gl.h>
19 #include <cstdio>
20 #include "SDL.h"
21 
22 #include "guipage.h"
23 #include "console.h"
24 #include "texture.h"
25 
26 CTexture *_thePageBackground = NULL;
27 
CGUIPage()28 CGUIPage::CGUIPage()
29 {
30 	loadConsoleFont();
31 
32 	m_Title = "Ultimate Stunts menu";
33 	m_OnlyTopGetsMouseEvents = true;
34 	m_DrawBackground = true;
35 
36 	m_EventWidget = 0;
37 
38 	if(_thePageBackground == NULL)
39 	{
40 		CParamList plist;
41 		SParameter p;
42 		p.name = "sizex";
43 		p.value = 4096; //at maximum of course :-)
44 		plist.push_back(p);
45 		p.name = "sizey";
46 		p.value = 4096;
47 		plist.push_back(p);
48 		_thePageBackground = new CTexture(NULL);
49 		_thePageBackground->load("misc/menubackground.rgb", plist);
50 	}
51 }
52 
~CGUIPage()53 CGUIPage::~CGUIPage()
54 {
55 	for(unsigned int i=0; i < m_Widgets.size(); i++)
56 		delete m_Widgets[i];
57 }
58 
onKeyPress(int key)59 int CGUIPage::onKeyPress(int key)
60 {
61 	if(m_Widgets.size() == 0) return 0;
62 
63 	m_EventWidget = m_Widgets.size()-1;
64 	return m_Widgets[m_Widgets.size()-1]->onKeyPress(key);
65 }
66 
onMouseMove(int x,int y,unsigned int buttons)67 int CGUIPage::onMouseMove(int x, int y, unsigned int buttons)
68 {
69 	if(m_Widgets.size() == 0) return 0;
70 
71 	m_EventWidget = m_Widgets.size()-1;
72 	if(m_Widgets[m_Widgets.size()-1]->isInWidget(x, y))
73 		return m_Widgets[m_Widgets.size()-1]->onMouseMove(x, y, buttons);
74 
75 	if(!m_OnlyTopGetsMouseEvents && m_Widgets.size() > 1)
76 		for(int i = m_Widgets.size()-2; i >= 0; i--)
77 			if(m_Widgets[i]->isInWidget(x, y))
78 			{
79 				m_EventWidget = i;
80 				return m_Widgets[i]->onMouseMove(x, y, buttons);
81 			}
82 
83 	return 0;
84 }
85 
onMouseClick(int x,int y,unsigned int buttons)86 int CGUIPage::onMouseClick(int x, int y, unsigned int buttons)
87 {
88 	if(m_Widgets.size() == 0) return 0;
89 
90 	m_EventWidget = m_Widgets.size()-1;
91 	if(m_Widgets[m_Widgets.size()-1]->isInWidget(x, y))
92 		return m_Widgets[m_Widgets.size()-1]->onMouseClick(x, y, buttons);
93 
94 	if(!m_OnlyTopGetsMouseEvents && m_Widgets.size() > 1)
95 		for(int i = m_Widgets.size()-2; i >= 0; i--)
96 			if(m_Widgets[i]->isInWidget(x, y))
97 			{
98 				m_EventWidget = i;
99 				return m_Widgets[i]->onMouseClick(x, y, buttons);
100 			}
101 
102 	return 0;
103 }
104 
onRedraw()105 int CGUIPage::onRedraw()
106 {
107 	CWidget::onRedraw();
108 
109 	//draw background:
110 	if(m_DrawBackground)
111 	{
112 		_thePageBackground->draw();
113 		glBegin(GL_QUADS);
114 		glTexCoord2f(0,0);
115 		glVertex2f(0,0);
116 		glTexCoord2f(1,0);
117 		glVertex2f(m_W,0);
118 		glTexCoord2f(1,1);
119 		glVertex2f(m_W,m_H);
120 		glTexCoord2f(0,1);
121 		glVertex2f(0,m_H);
122 		glEnd();
123 	}
124 	else
125 	{
126 		glColor3f(0,0,0);
127 		drawBackground();
128 		glColor3f(1,1,1);
129 	}
130 
131 	if(m_Title.length() > 0)
132 	{
133 		theConsoleFont->enable();
134 
135 		//draw the title:
136 		glTranslatef(0.5*m_W, 0.8*m_H, 0); //set cursor
137 
138 		glColor3f(1,1,1);
139 
140 		glTranslatef(-((int)m_Title.length())*theConsoleFont->getFontW(), 0, 0); //centered
141 		glScalef(2,2,2);
142 		theConsoleFont->drawString(m_Title);
143 
144 		theConsoleFont->disable();
145 	}
146 
147 	if(m_Widgets.size() == 0) return 0;
148 
149 	for(unsigned int i=0; i < m_Widgets.size(); i++)
150 		m_Widgets[i]->onRedraw();
151 
152 	return 0;
153 }
154 
onIdle()155 int CGUIPage::onIdle()
156 {
157 	if(m_Widgets.size() == 0) return 0;
158 
159 	int ret = 0;
160 
161 	for(unsigned int i=0; i < m_Widgets.size(); i++)
162 		ret |= m_Widgets[i]->onIdle();
163 
164 	return ret;
165 }
166 
onResize(int x,int y,int w,int h)167 int CGUIPage::onResize(int x, int y, int w, int h)
168 {
169 	int ret = CWidget::onResize(x, y, w, h);
170 
171 	if(m_Widgets.size() == 0) return ret;
172 
173 	for(unsigned int i=0; i < m_Widgets.size(); i++)
174 		ret |= m_Widgets[i]->onResize(
175 			int(x+m_Widgets[i]->m_Xrel*w),
176 			int(y+m_Widgets[i]->m_Yrel*h),
177 			int(m_Widgets[i]->m_Wrel*w),
178 			int(m_Widgets[i]->m_Hrel*h)
179 			);
180 
181 	return ret;
182 }
183