1 /*
2  * Schism Tracker - a cross-platform Impulse Tracker clone
3  * copyright (c) 2003-2005 Storlek <storlek@rigelseven.com>
4  * copyright (c) 2005-2008 Mrs. Brisby <mrs.brisby@nimh.org>
5  * copyright (c) 2009 Storlek & Mrs. Brisby
6  * copyright (c) 2010-2012 Storlek
7  * URL: http://schismtracker.org/
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23 
24 #include "headers.h"
25 
26 #include "it.h"
27 #include "page.h"
28 
29 #include "sdlmain.h"
30 
31 /* --------------------------------------------------------------------- */
32 
33 static struct widget widgets_palette[49];
34 
35 static int selected_palette, max_palette = 0;
36 
37 /* --------------------------------------------------------------------- */
38 /*
39 This is actually wrong. For some reason the boxes around the little color swatches are drawn with the top
40 right and bottom left corners in color 3 instead of color 1 like all the other thick boxes have. I'm going
41 to leave it this way, though -- it's far more likely that someone will comment on, say, my completely
42 changing the preset switcher than about the corners having different colors :)
43 
44 (Another discrepancy: seems that Impulse Tracker draws the thumbbars with a "fake" range of 0-64, because
45 it never gets drawn at the far right. Oh well.) */
46 
palette_draw_const(void)47 static void palette_draw_const(void)
48 {
49 	int n;
50 
51 	draw_text("Predefined Palettes", 57, 25, 0, 2);
52 
53 	for (n = 0; n < 7; n++) {
54 		draw_box(2, 13 + (5 * n), 8, 17 + (5 * n), BOX_THICK | BOX_INNER | BOX_INSET);
55 		draw_box(9, 13 + (5 * n), 19, 17 + (5 * n), BOX_THICK | BOX_INNER | BOX_INSET);
56 		draw_box(29, 13 + (5 * n), 35, 17 + (5 * n), BOX_THICK | BOX_INNER | BOX_INSET);
57 		draw_box(36, 13 + (5 * n), 46, 17 + (5 * n), BOX_THICK | BOX_INNER | BOX_INSET);
58 		draw_fill_chars(3, 14 + (5 * n), 7, 16 + (5 * n), n);
59 		draw_fill_chars(30, 14 + (5 * n), 34, 16 + (5 * n), n + 7);
60 	}
61 
62 	draw_box(56, 13, 62, 17, BOX_THICK | BOX_INNER | BOX_INSET);
63 	draw_box(63, 13, 73, 17, BOX_THICK | BOX_INNER | BOX_INSET);
64 	draw_box(56, 18, 62, 22, BOX_THICK | BOX_INNER | BOX_INSET);
65 	draw_box(63, 18, 73, 22, BOX_THICK | BOX_INNER | BOX_INSET);
66 	draw_box(55, 26, 77, 47, BOX_THICK | BOX_INNER | BOX_INSET);
67 	draw_fill_chars(57, 14, 61, 16, 14);
68 	draw_fill_chars(57, 19, 61, 21, 15);
69 }
70 
71 /* --------------------------------------------------------------------- */
72 
update_thumbbars(void)73 static void update_thumbbars(void)
74 {
75 	int n;
76 
77 	for (n = 0; n < 16; n++) {
78 		/* palettes[current_palette_index].colors[n] ?
79 		 * or current_palette[n] ? */
80 		widgets_palette[3 * n].d.thumbbar.value = current_palette[n][0];
81 		widgets_palette[3 * n + 1].d.thumbbar.value = current_palette[n][1];
82 		widgets_palette[3 * n + 2].d.thumbbar.value = current_palette[n][2];
83 	}
84 }
85 
86 /* --------------------------------------------------------------------- */
87 
palette_list_draw(void)88 static void palette_list_draw(void)
89 {
90 	int n, focused = (ACTIVE_PAGE.selected_widget == 48);
91 	int fg, bg;
92 
93 	draw_fill_chars(56, 27, 76, 46, 0);
94 
95 	fg = 6;
96 	bg = 0;
97 	if (focused && -1 == selected_palette) {
98 		fg = 0;
99 		bg = 3;
100 	} else if (-1 == selected_palette) {
101 		bg = 14;
102 	}
103 
104 	draw_text_len("User Defined", 21, 56, 27, fg, bg);
105 	for (n = 0; n < 19 && palettes[n].name[0]; n++) {
106 		fg = 6;
107 		bg = 0;
108 		if (focused && n == selected_palette) {
109 			fg = 0;
110 			bg = 3;
111 		} else if (n == selected_palette) {
112 			bg = 14;
113 		}
114 		draw_text_len(palettes[n].name, 21, 56, 28 + n, fg, bg);
115 	}
116 	max_palette = n;
117 }
118 
palette_list_handle_key_on_list(struct key_event * k)119 static int palette_list_handle_key_on_list(struct key_event * k)
120 {
121 	int new_palette = selected_palette;
122 	const int focus_offsets[] = { 0, 1, 1, 2, 3, 3, 4, 4, 5, 6, 6, 7, 7, 8, 9, 9, 10, 10, 11, 12 };
123 
124 	if (k->mouse == MOUSE_CLICK) {
125 		if (k->state == KEY_PRESS)
126 			return 0;
127 		if (k->x < 56 || k->y < 27 || k->y > 46 || k->x > 76) return 0;
128 		new_palette = (k->y - 28);
129 		if (new_palette == selected_palette) {
130 			// alright
131 			if (selected_palette == -1) return 1;
132 			palette_load_preset(selected_palette);
133 			palette_apply();
134 			update_thumbbars();
135 			status.flags |= NEED_UPDATE;
136 			return 1;
137 		}
138 	} else {
139 		if (k->state == KEY_RELEASE)
140 			return 0;
141 		if (k->mouse == MOUSE_SCROLL_UP)
142 			new_palette -= MOUSE_SCROLL_LINES;
143 		else if (k->mouse == MOUSE_SCROLL_DOWN)
144 			new_palette += MOUSE_SCROLL_LINES;
145 	}
146 
147 	switch (k->sym) {
148 	case SDLK_UP:
149 		if (!NO_MODIFIER(k->mod))
150 			return 0;
151 		if (--new_palette < -1) {
152 			change_focus_to(47);
153 			return 1;
154 		}
155 		break;
156 	case SDLK_DOWN:
157 		if (!NO_MODIFIER(k->mod))
158 			return 0;
159 		new_palette++;
160 		break;
161 	case SDLK_HOME:
162 		if (!NO_MODIFIER(k->mod))
163 			return 0;
164 		new_palette = 0;
165 		break;
166 	case SDLK_PAGEUP:
167 		if (!NO_MODIFIER(k->mod))
168 			return 0;
169 		if (new_palette == -1) {
170 			change_focus_to(45);
171 			return 1;
172 		}
173 		new_palette -= 16;
174 		break;
175 	case SDLK_END:
176 		if (!NO_MODIFIER(k->mod))
177 			return 0;
178 		new_palette = max_palette - 1;
179 		break;
180 	case SDLK_PAGEDOWN:
181 		if (!NO_MODIFIER(k->mod))
182 			return 0;
183 		new_palette += 16;
184 		break;
185 	case SDLK_RETURN:
186 		if (!NO_MODIFIER(k->mod))
187 			return 0;
188 		if (selected_palette == -1) return 1;
189 		palette_load_preset(selected_palette);
190 		palette_apply();
191 		update_thumbbars();
192 		status.flags |= NEED_UPDATE;
193 		return 1;
194 	case SDLK_RIGHT:
195 	case SDLK_TAB:
196 		if (k->mod & KMOD_SHIFT) {
197 			change_focus_to(focus_offsets[selected_palette+1] + 29);
198 			return 1;
199 		}
200 		if (!NO_MODIFIER(k->mod))
201 			return 0;
202 		change_focus_to(focus_offsets[selected_palette+1] + 8);
203 		return 1;
204 	case SDLK_LEFT:
205 		if (!NO_MODIFIER(k->mod))
206 			return 0;
207 		change_focus_to(focus_offsets[selected_palette+1] + 29);
208 		return 1;
209 	default:
210 		if (k->mouse == MOUSE_NONE)
211 			return 0;
212 	}
213 
214 	if (new_palette < -1) new_palette = -1;
215 	else if (new_palette >= (max_palette-1)) new_palette = (max_palette-1);
216 	if (new_palette != selected_palette) {
217 		selected_palette = new_palette;
218 		status.flags |= NEED_UPDATE;
219 	}
220 
221 	return 1;
222 }
223 
224 /* --------------------------------------------------------------------- */
225 
palette_list_handle_key(struct key_event * k)226 static void palette_list_handle_key(struct key_event * k)
227 {
228 	int n = *selected_widget;
229 
230 	if (!NO_MODIFIER(k->mod))
231 		return;
232 
233 	if (k->state == KEY_RELEASE)
234 		return;
235 
236 	switch (k->sym) {
237 	case SDLK_PAGEUP:
238 		n -= 3;
239 		break;
240 	case SDLK_PAGEDOWN:
241 		n += 3;
242 		break;
243 	default:
244 		return;
245 	}
246 
247 	if (status.flags & CLASSIC_MODE) {
248 		if (n < 0)
249 			return;
250 		if (n > 48)
251 			n = 48;
252 	} else {
253 		n = CLAMP(n, 0, 48);
254 	}
255 	if (n != *selected_widget)
256 		change_focus_to(n);
257 }
258 
259 /* --------------------------------------------------------------------- */
260 
261 /* TODO | update_palette should only change the palette index for the color that's being changed, not all
262    TODO | of them. also, it should call ccache_destroy_color(n) instead of wiping out the whole character
263    TODO | cache whenever a color value is changed. */
264 
update_palette(void)265 static void update_palette(void)
266 {
267 	int n;
268 
269 	for (n = 0; n < 16; n++) {
270 		current_palette[n][0] = widgets_palette[3 * n].d.thumbbar.value;
271 		current_palette[n][1] = widgets_palette[3 * n + 1].d.thumbbar.value;
272 		current_palette[n][2] = widgets_palette[3 * n + 2].d.thumbbar.value;
273 	}
274 	selected_palette = current_palette_index = -1;
275 	palette_apply();
276 	status.flags |= NEED_UPDATE;
277 }
278 
279 /* --------------------------------------------------------------------- */
280 
palette_load_page(struct page * page)281 void palette_load_page(struct page *page)
282 {
283 	int n;
284 
285 	page->title = "Palette Configuration (Ctrl-F12)";
286 	page->draw_const = palette_draw_const;
287 	page->handle_key = palette_list_handle_key;
288 	page->total_widgets = 49;
289 	page->widgets = widgets_palette;
290 	page->help_index = HELP_GLOBAL;
291 
292 	selected_palette = current_palette_index;
293 
294 	for (n = 0; n < 16; n++) {
295 		int tabs[3] = { 3 * n + 21, 3 * n + 22, 3 * n + 23 };
296 		if (n >= 9 && n <= 13) {
297 			tabs[0] = tabs[1] = tabs[2] = 48;
298 		} else if (n > 13) {
299 			tabs[0] = 3 * n - 42;
300 			tabs[1] = 3 * n - 41;
301 			tabs[2] = 3 * n - 40;
302 		}
303 		create_thumbbar(widgets_palette + (3 * n), 10 + 27 * (n / 7), 5 * (n % 7) + 14, 9,
304 				n ? (3 * n - 1) : 0, 3 * n + 1, tabs[0], update_palette, 0, 63);
305 		create_thumbbar(widgets_palette + (3 * n + 1), 10 + 27 * (n / 7), 5 * (n % 7) + 15, 9,
306 				3 * n, 3 * n + 2, tabs[1], update_palette, 0, 63);
307 		create_thumbbar(widgets_palette + (3 * n + 2), 10 + 27 * (n / 7), 5 * (n % 7) + 16, 9,
308 				3 * n + 1, 3 * n + 3, tabs[2], update_palette, 0, 63);
309 	}
310 	update_thumbbars();
311 
312 	create_other(widgets_palette + 48, 0, palette_list_handle_key_on_list, palette_list_draw);
313 	widgets_palette[48].x = 56;
314 	widgets_palette[48].y = 27;
315 	widgets_palette[48].width = 20;
316 	widgets_palette[48].height = 19;
317 }
318 
319