1 /***************************************************************************
2                           colorselect.cpp  -  A Color Selection tool
3                              -------------------
4     begin                : di sep 05 2006
5     copyright            : (C) 2006 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 <cstdio>
19 
20 #include "SDL.h"
21 #include <GL/gl.h>
22 
23 #include <libintl.h>
24 #define _(String) gettext (String)
25 
26 #include "colorselect.h"
27 #include "console.h"
28 
CColorSelect()29 CColorSelect::CColorSelect()
30 {
31 	loadConsoleFont();
32 
33 	m_Cancelled = false;
34 	m_Title = "Title";
35 	m_Selected = eNone;
36 }
37 
~CColorSelect()38 CColorSelect::~CColorSelect(){
39 }
40 
setTitle(const CString & title)41 void CColorSelect::setTitle(const CString &title)
42 {
43 	m_Title = title;
44 }
45 
onKeyPress(int key)46 int CColorSelect::onKeyPress(int key)
47 {
48 	if(key == SDLK_ESCAPE) m_Cancelled = true;
49 
50 	if(key == SDLK_RETURN || key == SDLK_ESCAPE) return WIDGET_QUIT;
51 
52 	return WIDGET_REDRAW;
53 }
54 
onMouseMove(int x,int y,unsigned int buttons)55 int CColorSelect::onMouseMove(int x, int y, unsigned int buttons)
56 {
57 	if(y - m_Y < 0.3*m_H)
58 	{
59 		if(x - m_X < m_W/2)
60 			{m_Selected = eOK;}
61 		else
62 			{m_Selected = eCancel;}
63 	}
64 	else
65 	{
66 		m_Selected = eNone;
67 
68 		if(buttons & SDL_BUTTON(1))
69 		{
70 			float yrel = y - m_Y - m_H/2;
71 			float value = (x-m_X - 0.1*m_W) / (0.8*m_W);
72 
73 			if(yrel > 0.125*m_H)
74 				{m_Color.x = value;}
75 			else if(yrel < -0.025*m_H)
76 				{m_Color.z = value;}
77 			else
78 				{m_Color.y = value;}
79 
80 			if(m_Color.x > 1.0) m_Color.x = 1.0;
81 			if(m_Color.y > 1.0) m_Color.y = 1.0;
82 			if(m_Color.z > 1.0) m_Color.z = 1.0;
83 			if(m_Color.x < 0.0) m_Color.x = 0.0;
84 			if(m_Color.y < 0.0) m_Color.y = 0.0;
85 			if(m_Color.z < 0.0) m_Color.z = 0.0;
86 		}
87 	}
88 
89 	return WIDGET_REDRAW;
90 }
91 
onMouseClick(int x,int y,unsigned int buttons)92 int CColorSelect::onMouseClick(int x, int y, unsigned int buttons)
93 {
94 	onMouseMove(x, y, buttons);
95 
96 	if(m_Selected == eOK)
97 		{m_Cancelled = false; return WIDGET_QUIT;}
98 	if(m_Selected == eCancel)
99 		{m_Cancelled = true; return WIDGET_QUIT;}
100 
101 	return 0;
102 }
103 
onRedraw()104 int CColorSelect::onRedraw()
105 {
106 	CWidget::onRedraw();
107 
108 	glColor4f(0, 0, 0, 0.7);
109 	drawBackground();
110 	glColor3f(1,1,1);
111 
112 	//centered
113 	glTranslatef(m_W/2, m_H/2, 0);
114 
115 	//A colored square
116 	glPushMatrix();
117 	glTranslatef(0.0*m_W, -0.3*m_H, 0);
118 	glColor3f(m_Color.x, m_Color.y, m_Color.z);
119 	glBegin(GL_QUADS);
120 	glVertex2f(-0.1*m_W, -0.1*m_H);
121 	glVertex2f( 0.1*m_W, -0.1*m_H);
122 	glVertex2f( 0.1*m_W,  0.1*m_H);
123 	glVertex2f(-0.1*m_W,  0.1*m_H);
124 	glEnd();
125 	glPopMatrix();
126 
127 	//The three color slides
128 	glBegin(GL_QUADS);
129 
130 	glColor3f(0.0, m_Color.y, m_Color.z);
131 	glVertex2f(-0.4*m_W, 0.2*m_H +5);
132 	glVertex2f(-0.4*m_W, 0.2*m_H -5);
133 	glColor3f(1.0, m_Color.y, m_Color.z);
134 	glVertex2f( 0.4*m_W, 0.2*m_H -5);
135 	glVertex2f( 0.4*m_W, 0.2*m_H +5);
136 
137 	glColor3f(m_Color.x, 0.0, m_Color.z);
138 	glVertex2f(-0.4*m_W, 0.05*m_H +5);
139 	glVertex2f(-0.4*m_W, 0.05*m_H -5);
140 	glColor3f(m_Color.x, 1.0, m_Color.z);
141 	glVertex2f( 0.4*m_W, 0.05*m_H -5);
142 	glVertex2f( 0.4*m_W, 0.05*m_H +5);
143 
144 	glColor3f(m_Color.x, m_Color.y, 0.0);
145 	glVertex2f(-0.4*m_W, -0.1*m_H +5);
146 	glVertex2f(-0.4*m_W, -0.1*m_H -5);
147 	glColor3f(m_Color.x, m_Color.y, 1.0);
148 	glVertex2f( 0.4*m_W, -0.1*m_H -5);
149 	glVertex2f( 0.4*m_W, -0.1*m_H +5);
150 
151 	glEnd();
152 
153 	//Lines on the color slides
154 	glColor3f(1, 1, 1);
155 	glBegin(GL_LINES);
156 	glVertex2f(0.8*m_W*(m_Color.x-0.5), 0.2*m_H -7);
157 	glVertex2f(0.8*m_W*(m_Color.x-0.5), 0.2*m_H +7);
158 
159 	glVertex2f(0.8*m_W*(m_Color.y-0.5), 0.05*m_H -7);
160 	glVertex2f(0.8*m_W*(m_Color.y-0.5), 0.05*m_H +7);
161 
162 	glVertex2f(0.8*m_W*(m_Color.z-0.5), -0.1*m_H -7);
163 	glVertex2f(0.8*m_W*(m_Color.z-0.5), -0.1*m_H +7);
164 	glEnd();
165 
166 	//Title
167 	glPushMatrix();
168 	glTranslatef(-0.5*theConsoleFont->getFontW()*m_Title.size(), 0.3*m_H, 0);
169 
170 	theConsoleFont->enable();
171 	theConsoleFont->drawString(m_Title);
172 	theConsoleFont->disable();
173 	glPopMatrix();
174 
175 	//OK button
176 	glPushMatrix();
177 	glTranslatef(-0.4*m_W, -0.3*m_H, 0);
178 
179 	if(m_Selected == eOK)
180 		{glColor3f(1, 0.9, 0);}
181 	else
182 		{glColor3f(1, 1, 1);}
183 	theConsoleFont->enable();
184 	theConsoleFont->drawString(_("OK"));
185 	theConsoleFont->disable();
186 	glPopMatrix();
187 
188 	//Cancel button
189 	glPushMatrix();
190 	glTranslatef(0.2*m_W, -0.3*m_H, 0);
191 
192 	if(m_Selected == eCancel)
193 		{glColor3f(1, 0.9, 0);}
194 	else
195 		{glColor3f(1, 1, 1);}
196 	theConsoleFont->enable();
197 	theConsoleFont->drawString(_("Cancel"));
198 	theConsoleFont->disable();
199 	glPopMatrix();
200 
201 	return 0;
202 }
203 
204