Rework ChoosePylonPosition to simplify the loop
[openttd/fttd.git] / src / music_gui.cpp
blob05ce0e8c6925727023478d872366259eabc2cb85
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file music_gui.cpp GUI for the music playback. */
12 #include "stdafx.h"
13 #include "openttd.h"
14 #include "base_media_base.h"
15 #include "music/music_driver.hpp"
16 #include "window_gui.h"
17 #include "strings_func.h"
18 #include "window_func.h"
19 #include "sound_func.h"
20 #include "gfx_func.h"
21 #include "core/random_func.hpp"
22 #include "error.h"
23 #include "core/geometry_func.hpp"
24 #include "string.h"
25 #include "settings_type.h"
27 #include "widgets/music_widget.h"
29 #include "table/strings.h"
30 #include "table/sprites.h"
32 /**
33 * Get the name of the song.
34 * @param index of the song.
35 * @return the name of the song.
37 static const char *GetSongName(int index)
39 return BaseMusic::GetUsedSet()->song_name[index];
42 /**
43 * Get the track number of the song.
44 * @param index of the song.
45 * @return the track number of the song.
47 static int GetTrackNumber(int index)
49 return BaseMusic::GetUsedSet()->track_nr[index];
52 /** The currently played song */
53 static byte _music_wnd_cursong = 1;
54 /** Whether a song is currently played */
55 static bool _song_is_active = false;
57 /** Indices of the songs in the current playlist */
58 static byte _cur_playlist[NUM_SONGS_PLAYLIST + 1];
60 /** Indices of all songs */
61 static byte _playlist_all[NUM_SONGS_AVAILABLE + 1];
62 /** Indices of all old style songs */
63 static byte _playlist_old_style[NUM_SONGS_CLASS + 1];
64 /** Indices of all new style songs */
65 static byte _playlist_new_style[NUM_SONGS_CLASS + 1];
66 /** Indices of all ezy street songs */
67 static byte _playlist_ezy_street[NUM_SONGS_CLASS + 1];
69 assert_compile(lengthof(_settings_client.music.custom_1) == NUM_SONGS_PLAYLIST + 1);
70 assert_compile(lengthof(_settings_client.music.custom_2) == NUM_SONGS_PLAYLIST + 1);
72 /** The different playlists that can be played. */
73 static byte * const _playlists[] = {
74 _playlist_all,
75 _playlist_old_style,
76 _playlist_new_style,
77 _playlist_ezy_street,
78 _settings_client.music.custom_1,
79 _settings_client.music.custom_2,
82 /**
83 * Validate a playlist.
84 * @param playlist The playlist to validate.
85 * @param last The last location in the list.
87 void ValidatePlaylist(byte *playlist, byte *last)
89 while (*playlist != 0 && playlist <= last) {
90 /* Song indices are saved off-by-one so 0 is "nothing". */
91 if (*playlist <= NUM_SONGS_AVAILABLE && !StrEmpty(GetSongName(*playlist - 1))) {
92 playlist++;
93 continue;
95 for (byte *p = playlist; *p != 0 && p <= last; p++) {
96 p[0] = p[1];
100 /* Make sure the list is null terminated. */
101 *last = 0;
104 /** Initialize the playlists */
105 void InitializeMusic()
107 uint j = 0;
108 for (uint i = 0; i < NUM_SONGS_AVAILABLE; i++) {
109 if (StrEmpty(GetSongName(i))) continue;
110 _playlist_all[j++] = i + 1;
112 /* Terminate the list */
113 _playlist_all[j] = 0;
115 /* Now make the 'styled' playlists */
116 for (uint k = 0; k < NUM_SONG_CLASSES; k++) {
117 j = 0;
118 for (uint i = 0; i < NUM_SONGS_CLASS; i++) {
119 int id = k * NUM_SONGS_CLASS + i + 1;
120 if (StrEmpty(GetSongName(id))) continue;
121 _playlists[k + 1][j++] = id + 1;
123 /* Terminate the list */
124 _playlists[k + 1][j] = 0;
127 ValidatePlaylist(_settings_client.music.custom_1, lastof(_settings_client.music.custom_1));
128 ValidatePlaylist(_settings_client.music.custom_2, lastof(_settings_client.music.custom_2));
130 if (BaseMusic::GetUsedSet()->num_available < _music_wnd_cursong) {
131 /* If there are less songs than the currently played song,
132 * just pause and reset to no song. */
133 _music_wnd_cursong = 0;
134 _song_is_active = false;
138 static void SkipToPrevSong()
140 byte *b = _cur_playlist;
141 byte *p = b;
142 byte t;
144 if (b[0] == 0) return; // empty playlist
146 do p++; while (p[0] != 0); // find the end
148 t = *--p; // and copy the bytes
149 while (p != b) {
150 p--;
151 p[1] = p[0];
153 *b = t;
155 _song_is_active = false;
158 static void SkipToNextSong()
160 byte *b = _cur_playlist;
161 byte t;
163 t = b[0];
164 if (t != 0) {
165 while (b[1] != 0) {
166 b[0] = b[1];
167 b++;
169 b[0] = t;
172 _song_is_active = false;
175 static void MusicVolumeChanged(byte new_vol)
177 MusicDriver::GetActiveDriver()->SetVolume(new_vol);
180 static void DoPlaySong()
182 char filename[MAX_PATH];
183 if (FioFindFullPath(filename, lengthof(filename), BASESET_DIR, BaseMusic::GetUsedSet()->files[_music_wnd_cursong - 1].filename) == NULL) {
184 FioFindFullPath(filename, lengthof(filename), OLD_GM_DIR, BaseMusic::GetUsedSet()->files[_music_wnd_cursong - 1].filename);
186 MusicDriver::GetActiveDriver()->PlaySong(filename);
187 SetWindowDirty(WC_MUSIC_WINDOW, 0);
190 static void DoStopMusic()
192 MusicDriver::GetActiveDriver()->StopSong();
193 SetWindowDirty(WC_MUSIC_WINDOW, 0);
196 static void SelectSongToPlay()
198 uint i = 0;
199 uint j = 0;
201 memset(_cur_playlist, 0, sizeof(_cur_playlist));
202 do {
203 /* File is the index into the file table of the music set.
204 * The play list uses 0 as 'no entry', so we need to
205 * subtract 1. In case of 'no entry', just skip adding it
206 * outright. */
207 uint file = _playlists[_settings_client.music.playlist][i];
208 if (file > 0) {
209 const char *filename = BaseMusic::GetUsedSet()->files[file - 1].filename;
210 /* We are now checking for the existence of that file prior
211 * to add it to the list of available songs */
212 if (!StrEmpty(filename) && FioCheckFileExists(filename, BASESET_DIR)) {
213 _cur_playlist[j] = _playlists[_settings_client.music.playlist][i];
214 j++;
217 } while (_playlists[_settings_client.music.playlist][++i] != 0 && j < lengthof(_cur_playlist) - 1);
219 /* Do not shuffle when on the intro-start window, as the song to play has to be the original TTD Theme*/
220 if (_settings_client.music.shuffle && _game_mode != GM_MENU) {
221 i = 500;
222 do {
223 uint32 r = InteractiveRandom();
224 byte *a = &_cur_playlist[GB(r, 0, 5)];
225 byte *b = &_cur_playlist[GB(r, 8, 5)];
227 if (*a != 0 && *b != 0) {
228 byte t = *a;
229 *a = *b;
230 *b = t;
232 } while (--i);
236 static void StopMusic()
238 _music_wnd_cursong = 0;
239 DoStopMusic();
240 _song_is_active = false;
241 SetWindowWidgetDirty(WC_MUSIC_WINDOW, 0, 9);
244 static void PlayPlaylistSong()
246 if (_cur_playlist[0] == 0) {
247 SelectSongToPlay();
248 /* if there is not songs in the playlist, it may indicate
249 * no file on the gm folder, or even no gm folder.
250 * Stop the playback, then */
251 if (_cur_playlist[0] == 0) {
252 _song_is_active = false;
253 _music_wnd_cursong = 0;
254 _settings_client.music.playing = false;
255 return;
258 _music_wnd_cursong = _cur_playlist[0];
259 DoPlaySong();
260 _song_is_active = true;
262 SetWindowWidgetDirty(WC_MUSIC_WINDOW, 0, 9);
265 void ResetMusic()
267 _music_wnd_cursong = 1;
268 DoPlaySong();
271 void MusicLoop()
273 if (!_settings_client.music.playing && _song_is_active) {
274 StopMusic();
275 } else if (_settings_client.music.playing && !_song_is_active) {
276 PlayPlaylistSong();
279 if (!_song_is_active) return;
281 if (!MusicDriver::GetActiveDriver()->IsSongPlaying()) {
282 if (_game_mode != GM_MENU) {
283 StopMusic();
284 SkipToNextSong();
285 PlayPlaylistSong();
286 } else {
287 ResetMusic();
292 static void SelectPlaylist(byte list)
294 _settings_client.music.playlist = list;
295 InvalidateWindowData(WC_MUSIC_TRACK_SELECTION, 0);
296 InvalidateWindowData(WC_MUSIC_WINDOW, 0);
299 struct MusicTrackSelectionWindow : public Window {
300 MusicTrackSelectionWindow (const WindowDesc *desc, WindowNumber number) : Window(desc)
302 this->InitNested(number);
303 this->LowerWidget(WID_MTS_LIST_LEFT);
304 this->LowerWidget(WID_MTS_LIST_RIGHT);
305 this->SetWidgetDisabledState(WID_MTS_CLEAR, _settings_client.music.playlist <= 3);
306 this->LowerWidget(WID_MTS_ALL + _settings_client.music.playlist);
309 virtual void SetStringParameters(int widget) const
311 switch (widget) {
312 case WID_MTS_PLAYLIST:
313 SetDParam(0, STR_MUSIC_PLAYLIST_ALL + _settings_client.music.playlist);
314 break;
319 * Some data on this window has become invalid.
320 * @param data Information about the changed data.
321 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
323 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
325 if (!gui_scope) return;
326 for (int i = 0; i < 6; i++) {
327 this->SetWidgetLoweredState(WID_MTS_ALL + i, i == _settings_client.music.playlist);
329 this->SetWidgetDisabledState(WID_MTS_CLEAR, _settings_client.music.playlist <= 3);
330 this->SetDirty();
333 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
335 switch (widget) {
336 case WID_MTS_PLAYLIST: {
337 Dimension d = {0, 0};
339 for (int i = 0; i < 6; i++) {
340 SetDParam(0, STR_MUSIC_PLAYLIST_ALL + i);
341 d = maxdim(d, GetStringBoundingBox(STR_PLAYLIST_PROGRAM));
343 d.width += padding.width;
344 d.height += padding.height;
345 *size = maxdim(*size, d);
346 break;
349 case WID_MTS_LIST_LEFT: case WID_MTS_LIST_RIGHT: {
350 Dimension d = {0, 0};
352 for (uint i = 0; i < NUM_SONGS_AVAILABLE; i++) {
353 const char *song_name = GetSongName(i);
354 if (StrEmpty(song_name)) continue;
356 SetDParam(0, GetTrackNumber(i));
357 SetDParam(1, 2);
358 SetDParamStr(2, GetSongName(i));
359 Dimension d2 = GetStringBoundingBox(STR_PLAYLIST_TRACK_NAME);
360 d.width = max(d.width, d2.width);
361 d.height += d2.height;
363 d.width += padding.width;
364 d.height += padding.height;
365 *size = maxdim(*size, d);
366 break;
371 void DrawWidget (BlitArea *dpi, const Rect &r, int widget) const OVERRIDE
373 switch (widget) {
374 case WID_MTS_LIST_LEFT: {
375 GfxFillRect (dpi, r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK);
377 int y = r.top + WD_FRAMERECT_TOP;
378 for (uint i = 0; i < NUM_SONGS_AVAILABLE; i++) {
379 const char *song_name = GetSongName(i);
380 if (StrEmpty(song_name)) continue;
382 SetDParam(0, GetTrackNumber(i));
383 SetDParam(1, 2);
384 SetDParamStr(2, song_name);
385 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_PLAYLIST_TRACK_NAME);
386 y += FONT_HEIGHT_SMALL;
388 break;
391 case WID_MTS_LIST_RIGHT: {
392 GfxFillRect (dpi, r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK);
394 int y = r.top + WD_FRAMERECT_TOP;
395 for (const byte *p = _playlists[_settings_client.music.playlist]; *p != 0; p++) {
396 uint i = *p - 1;
397 SetDParam(0, GetTrackNumber(i));
398 SetDParam(1, 2);
399 SetDParamStr(2, GetSongName(i));
400 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_PLAYLIST_TRACK_NAME);
401 y += FONT_HEIGHT_SMALL;
403 break;
408 virtual void OnClick(Point pt, int widget, int click_count)
410 switch (widget) {
411 case WID_MTS_LIST_LEFT: { // add to playlist
412 int y = this->GetRowFromWidget(pt.y, widget, 0, FONT_HEIGHT_SMALL);
414 if (_settings_client.music.playlist < 4) return;
415 if (!IsInsideMM(y, 0, BaseMusic::GetUsedSet()->num_available)) return;
417 byte *p = _playlists[_settings_client.music.playlist];
418 for (uint i = 0; i != NUM_SONGS_PLAYLIST - 1; i++) {
419 if (p[i] == 0) {
420 /* Find the actual song number */
421 for (uint j = 0; j < NUM_SONGS_AVAILABLE; j++) {
422 if (GetTrackNumber(j) == y + 1) {
423 p[i] = j + 1;
424 break;
427 p[i + 1] = 0;
428 this->SetDirty();
429 SelectSongToPlay();
430 break;
433 break;
436 case WID_MTS_LIST_RIGHT: { // remove from playlist
437 int y = this->GetRowFromWidget(pt.y, widget, 0, FONT_HEIGHT_SMALL);
439 if (_settings_client.music.playlist < 4) return;
440 if (!IsInsideMM(y, 0, NUM_SONGS_PLAYLIST)) return;
442 byte *p = _playlists[_settings_client.music.playlist];
443 for (uint i = y; i != NUM_SONGS_PLAYLIST - 1; i++) {
444 p[i] = p[i + 1];
447 this->SetDirty();
448 SelectSongToPlay();
449 break;
452 case WID_MTS_CLEAR: // clear
453 for (uint i = 0; _playlists[_settings_client.music.playlist][i] != 0; i++) _playlists[_settings_client.music.playlist][i] = 0;
454 this->SetDirty();
455 StopMusic();
456 SelectSongToPlay();
457 break;
459 case WID_MTS_ALL: case WID_MTS_OLD: case WID_MTS_NEW:
460 case WID_MTS_EZY: case WID_MTS_CUSTOM1: case WID_MTS_CUSTOM2: // set playlist
461 SelectPlaylist(widget - WID_MTS_ALL);
462 StopMusic();
463 SelectSongToPlay();
464 break;
469 static const NWidgetPart _nested_music_track_selection_widgets[] = {
470 NWidget(NWID_HORIZONTAL),
471 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
472 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_PLAYLIST_MUSIC_PROGRAM_SELECTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
473 EndContainer(),
474 NWidget(WWT_PANEL, COLOUR_GREY),
475 NWidget(NWID_HORIZONTAL), SetPIP(2, 4, 2),
476 /* Left panel. */
477 NWidget(NWID_VERTICAL),
478 NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_PLAYLIST_TRACK_INDEX, STR_NULL),
479 NWidget(WWT_PANEL, COLOUR_GREY, WID_MTS_LIST_LEFT), SetMinimalSize(180, 194), SetDataTip(0x0, STR_PLAYLIST_TOOLTIP_CLICK_TO_ADD_TRACK), EndContainer(),
480 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
481 EndContainer(),
482 /* Middle buttons. */
483 NWidget(NWID_VERTICAL),
484 NWidget(NWID_SPACER), SetMinimalSize(60, 30), // Space above the first button from the title bar.
485 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_MTS_ALL), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_ALL, STR_MUSIC_TOOLTIP_SELECT_ALL_TRACKS_PROGRAM),
486 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_MTS_OLD), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_OLD_STYLE, STR_MUSIC_TOOLTIP_SELECT_OLD_STYLE_MUSIC),
487 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_MTS_NEW), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_NEW_STYLE, STR_MUSIC_TOOLTIP_SELECT_NEW_STYLE_MUSIC),
488 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_MTS_EZY), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_EZY_STREET, STR_MUSIC_TOOLTIP_SELECT_EZY_STREET_STYLE),
489 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_MTS_CUSTOM1), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_CUSTOM_1, STR_MUSIC_TOOLTIP_SELECT_CUSTOM_1_USER_DEFINED),
490 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_MTS_CUSTOM2), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_CUSTOM_2, STR_MUSIC_TOOLTIP_SELECT_CUSTOM_2_USER_DEFINED),
491 NWidget(NWID_SPACER), SetMinimalSize(0, 16), // Space above 'clear' button
492 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_MTS_CLEAR), SetFill(1, 0), SetDataTip(STR_PLAYLIST_CLEAR, STR_PLAYLIST_TOOLTIP_CLEAR_CURRENT_PROGRAM_CUSTOM1),
493 NWidget(NWID_SPACER), SetFill(0, 1),
494 EndContainer(),
495 /* Right panel. */
496 NWidget(NWID_VERTICAL),
497 NWidget(WWT_LABEL, COLOUR_GREY, WID_MTS_PLAYLIST), SetDataTip(STR_PLAYLIST_PROGRAM, STR_NULL),
498 NWidget(WWT_PANEL, COLOUR_GREY, WID_MTS_LIST_RIGHT), SetMinimalSize(180, 194), SetDataTip(0x0, STR_PLAYLIST_TOOLTIP_CLICK_TO_REMOVE_TRACK), EndContainer(),
499 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
500 EndContainer(),
501 EndContainer(),
502 EndContainer(),
505 static WindowDesc::Prefs _music_track_selection_prefs ("music_track");
507 static const WindowDesc _music_track_selection_desc(
508 WDP_AUTO, 0, 0,
509 WC_MUSIC_TRACK_SELECTION, WC_NONE,
511 _nested_music_track_selection_widgets, lengthof(_nested_music_track_selection_widgets),
512 &_music_track_selection_prefs
515 static void ShowMusicTrackSelection()
517 AllocateWindowDescFront<MusicTrackSelectionWindow>(&_music_track_selection_desc, 0);
520 struct MusicWindow : public Window {
521 static const int slider_width = 3;
523 MusicWindow (const WindowDesc *desc, WindowNumber number) : Window(desc)
525 this->InitNested(number);
526 this->LowerWidget(_settings_client.music.playlist + WID_M_ALL);
527 this->SetWidgetLoweredState(WID_M_SHUFFLE, _settings_client.music.shuffle);
530 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
532 switch (widget) {
533 /* Make sure that WID_M_SHUFFLE and WID_M_PROGRAMME have the same size.
534 * This can't be done by using NC_EQUALSIZE as the WID_M_INFO is
535 * between those widgets and of different size. */
536 case WID_M_SHUFFLE: case WID_M_PROGRAMME: {
537 Dimension d = maxdim(GetStringBoundingBox(STR_MUSIC_PROGRAM), GetStringBoundingBox(STR_MUSIC_SHUFFLE));
538 d.width += padding.width;
539 d.height += padding.height;
540 *size = maxdim(*size, d);
541 break;
544 case WID_M_TRACK_NR: {
545 Dimension d = GetStringBoundingBox(STR_MUSIC_TRACK_NONE);
546 d.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
547 d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
548 *size = maxdim(*size, d);
549 break;
552 case WID_M_TRACK_NAME: {
553 Dimension d = GetStringBoundingBox(STR_MUSIC_TITLE_NONE);
554 for (uint i = 0; i < NUM_SONGS_AVAILABLE; i++) {
555 SetDParamStr(0, GetSongName(i));
556 d = maxdim(d, GetStringBoundingBox(STR_MUSIC_TITLE_NAME));
558 d.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
559 d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
560 *size = maxdim(*size, d);
561 break;
564 /* Hack-ish: set the proper widget data; only needs to be done once
565 * per (Re)Init as that's the only time the language changes. */
566 case WID_M_PREV: this->GetWidget<NWidgetCore>(WID_M_PREV)->widget_data = _current_text_dir == TD_RTL ? SPR_IMG_SKIP_TO_NEXT : SPR_IMG_SKIP_TO_PREV; break;
567 case WID_M_NEXT: this->GetWidget<NWidgetCore>(WID_M_NEXT)->widget_data = _current_text_dir == TD_RTL ? SPR_IMG_SKIP_TO_PREV : SPR_IMG_SKIP_TO_NEXT; break;
568 case WID_M_PLAY: this->GetWidget<NWidgetCore>(WID_M_PLAY)->widget_data = _current_text_dir == TD_RTL ? SPR_IMG_PLAY_MUSIC_RTL : SPR_IMG_PLAY_MUSIC; break;
572 void DrawWidget (BlitArea *dpi, const Rect &r, int widget) const OVERRIDE
574 switch (widget) {
575 case WID_M_TRACK_NR: {
576 GfxFillRect (dpi, r.left + 1, r.top + 1, r.right, r.bottom, PC_BLACK);
577 StringID str = STR_MUSIC_TRACK_NONE;
578 if (_song_is_active != 0 && _music_wnd_cursong != 0) {
579 SetDParam(0, GetTrackNumber(_music_wnd_cursong - 1));
580 SetDParam(1, 2);
581 str = STR_MUSIC_TRACK_DIGIT;
583 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, str);
584 break;
587 case WID_M_TRACK_NAME: {
588 GfxFillRect (dpi, r.left, r.top + 1, r.right - 1, r.bottom, PC_BLACK);
589 StringID str = STR_MUSIC_TITLE_NONE;
590 if (_song_is_active != 0 && _music_wnd_cursong != 0) {
591 str = STR_MUSIC_TITLE_NAME;
592 SetDParamStr(0, GetSongName(_music_wnd_cursong - 1));
594 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, str, TC_FROMSTRING, SA_HOR_CENTER);
595 break;
598 case WID_M_MUSIC_VOL: case WID_M_EFFECT_VOL: {
599 DrawFrameRect (dpi, r.left, r.top + 2, r.right, r.bottom - 2, COLOUR_GREY, FR_LOWERED);
600 byte volume = (widget == WID_M_MUSIC_VOL) ? _settings_client.music.music_vol : _settings_client.music.effect_vol;
601 int x = (volume * (r.right - r.left) / 127);
602 if (_current_text_dir == TD_RTL) {
603 x = r.right - x;
604 } else {
605 x += r.left;
607 DrawFrameRect (dpi, x, r.top, x + slider_width, r.bottom, COLOUR_GREY, FR_NONE);
608 break;
614 * Some data on this window has become invalid.
615 * @param data Information about the changed data.
616 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
618 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
620 if (!gui_scope) return;
621 for (int i = 0; i < 6; i++) {
622 this->SetWidgetLoweredState(WID_M_ALL + i, i == _settings_client.music.playlist);
624 this->SetDirty();
627 virtual void OnClick(Point pt, int widget, int click_count)
629 switch (widget) {
630 case WID_M_PREV: // skip to prev
631 if (!_song_is_active) return;
632 SkipToPrevSong();
633 this->SetDirty();
634 break;
636 case WID_M_NEXT: // skip to next
637 if (!_song_is_active) return;
638 SkipToNextSong();
639 this->SetDirty();
640 break;
642 case WID_M_STOP: // stop playing
643 _settings_client.music.playing = false;
644 break;
646 case WID_M_PLAY: // start playing
647 _settings_client.music.playing = true;
648 break;
650 case WID_M_MUSIC_VOL: case WID_M_EFFECT_VOL: { // volume sliders
651 int x = pt.x - this->GetWidget<NWidgetBase>(widget)->pos_x;
653 byte *vol = (widget == WID_M_MUSIC_VOL) ? &_settings_client.music.music_vol : &_settings_client.music.effect_vol;
655 byte new_vol = x * 127 / this->GetWidget<NWidgetBase>(widget)->current_x;
656 if (_current_text_dir == TD_RTL) new_vol = 127 - new_vol;
657 if (new_vol != *vol) {
658 *vol = new_vol;
659 if (widget == WID_M_MUSIC_VOL) MusicVolumeChanged(new_vol);
660 this->SetDirty();
663 _left_button_clicked = false;
664 break;
667 case WID_M_SHUFFLE: // toggle shuffle
668 _settings_client.music.shuffle ^= 1;
669 this->SetWidgetLoweredState(WID_M_SHUFFLE, _settings_client.music.shuffle);
670 this->SetWidgetDirty(WID_M_SHUFFLE);
671 StopMusic();
672 SelectSongToPlay();
673 this->SetDirty();
674 break;
676 case WID_M_PROGRAMME: // show track selection
677 ShowMusicTrackSelection();
678 break;
680 case WID_M_ALL: case WID_M_OLD: case WID_M_NEW:
681 case WID_M_EZY: case WID_M_CUSTOM1: case WID_M_CUSTOM2: // playlist
682 SelectPlaylist(widget - WID_M_ALL);
683 StopMusic();
684 SelectSongToPlay();
685 this->SetDirty();
686 break;
691 static const NWidgetPart _nested_music_window_widgets[] = {
692 NWidget(NWID_HORIZONTAL),
693 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
694 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_MUSIC_JAZZ_JUKEBOX_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
695 NWidget(WWT_SHADEBOX, COLOUR_GREY),
696 NWidget(WWT_STICKYBOX, COLOUR_GREY),
697 EndContainer(),
699 NWidget(NWID_HORIZONTAL),
700 NWidget(NWID_VERTICAL),
701 NWidget(WWT_PANEL, COLOUR_GREY, -1), SetFill(1, 1), EndContainer(),
702 NWidget(NWID_HORIZONTAL),
703 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_M_PREV), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_SKIP_TO_PREV, STR_MUSIC_TOOLTIP_SKIP_TO_PREVIOUS_TRACK),
704 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_M_NEXT), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_SKIP_TO_NEXT, STR_MUSIC_TOOLTIP_SKIP_TO_NEXT_TRACK_IN_SELECTION),
705 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_M_STOP), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_STOP_MUSIC, STR_MUSIC_TOOLTIP_STOP_PLAYING_MUSIC),
706 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_M_PLAY), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_PLAY_MUSIC, STR_MUSIC_TOOLTIP_START_PLAYING_MUSIC),
707 EndContainer(),
708 NWidget(WWT_PANEL, COLOUR_GREY, -1), SetFill(1, 1), EndContainer(),
709 EndContainer(),
710 NWidget(WWT_PANEL, COLOUR_GREY, WID_M_SLIDERS),
711 NWidget(NWID_HORIZONTAL), SetPIP(20, 20, 20),
712 NWidget(NWID_VERTICAL),
713 NWidget(WWT_LABEL, COLOUR_GREY, -1), SetFill(1, 0), SetDataTip(STR_MUSIC_MUSIC_VOLUME, STR_NULL),
714 NWidget(WWT_EMPTY, COLOUR_GREY, WID_M_MUSIC_VOL), SetMinimalSize(67, 0), SetMinimalTextLines(1, 0), SetFill(1, 0), SetDataTip(0x0, STR_MUSIC_TOOLTIP_DRAG_SLIDERS_TO_SET_MUSIC),
715 NWidget(NWID_HORIZONTAL),
716 NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MIN, STR_NULL),
717 NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
718 NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
719 NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
720 NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
721 NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
722 NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MAX, STR_NULL),
723 EndContainer(),
724 EndContainer(),
725 NWidget(NWID_VERTICAL),
726 NWidget(WWT_LABEL, COLOUR_GREY, -1), SetFill(1, 0), SetDataTip(STR_MUSIC_EFFECTS_VOLUME, STR_NULL),
727 NWidget(WWT_EMPTY, COLOUR_GREY, WID_M_EFFECT_VOL), SetMinimalSize(67, 0), SetMinimalTextLines(1, 0), SetFill(1, 0), SetDataTip(0x0, STR_MUSIC_TOOLTIP_DRAG_SLIDERS_TO_SET_MUSIC),
728 NWidget(NWID_HORIZONTAL),
729 NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MIN, STR_NULL),
730 NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
731 NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
732 NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
733 NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
734 NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
735 NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MAX, STR_NULL),
736 EndContainer(),
737 EndContainer(),
738 EndContainer(),
739 EndContainer(),
740 EndContainer(),
741 NWidget(WWT_PANEL, COLOUR_GREY, WID_M_BACKGROUND),
742 NWidget(NWID_HORIZONTAL), SetPIP(6, 0, 6),
743 NWidget(NWID_VERTICAL),
744 NWidget(NWID_SPACER), SetFill(0, 1),
745 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_M_SHUFFLE), SetMinimalSize(50, 8), SetDataTip(STR_MUSIC_SHUFFLE, STR_MUSIC_TOOLTIP_TOGGLE_PROGRAM_SHUFFLE),
746 NWidget(NWID_SPACER), SetFill(0, 1),
747 EndContainer(),
748 NWidget(NWID_VERTICAL), SetPadding(0, 0, 3, 3),
749 NWidget(WWT_LABEL, COLOUR_GREY, WID_M_TRACK), SetFill(0, 0), SetDataTip(STR_MUSIC_TRACK, STR_NULL),
750 NWidget(WWT_PANEL, COLOUR_GREY, WID_M_TRACK_NR), EndContainer(),
751 EndContainer(),
752 NWidget(NWID_VERTICAL), SetPadding(0, 3, 3, 0),
753 NWidget(WWT_LABEL, COLOUR_GREY, WID_M_TRACK_TITLE), SetFill(1, 0), SetDataTip(STR_MUSIC_XTITLE, STR_NULL),
754 NWidget(WWT_PANEL, COLOUR_GREY, WID_M_TRACK_NAME), SetFill(1, 0), EndContainer(),
755 EndContainer(),
756 NWidget(NWID_VERTICAL),
757 NWidget(NWID_SPACER), SetFill(0, 1),
758 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_M_PROGRAMME), SetMinimalSize(50, 8), SetDataTip(STR_MUSIC_PROGRAM, STR_MUSIC_TOOLTIP_SHOW_MUSIC_TRACK_SELECTION),
759 NWidget(NWID_SPACER), SetFill(0, 1),
760 EndContainer(),
761 EndContainer(),
762 EndContainer(),
763 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
764 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_M_ALL), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_ALL, STR_MUSIC_TOOLTIP_SELECT_ALL_TRACKS_PROGRAM),
765 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_M_OLD), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_OLD_STYLE, STR_MUSIC_TOOLTIP_SELECT_OLD_STYLE_MUSIC),
766 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_M_NEW), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_NEW_STYLE, STR_MUSIC_TOOLTIP_SELECT_NEW_STYLE_MUSIC),
767 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_M_EZY), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_EZY_STREET, STR_MUSIC_TOOLTIP_SELECT_EZY_STREET_STYLE),
768 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_M_CUSTOM1), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_CUSTOM_1, STR_MUSIC_TOOLTIP_SELECT_CUSTOM_1_USER_DEFINED),
769 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_M_CUSTOM2), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_CUSTOM_2, STR_MUSIC_TOOLTIP_SELECT_CUSTOM_2_USER_DEFINED),
770 EndContainer(),
773 static WindowDesc::Prefs _music_window_prefs ("music");
775 static const WindowDesc _music_window_desc(
776 WDP_AUTO, 0, 0,
777 WC_MUSIC_WINDOW, WC_NONE,
779 _nested_music_window_widgets, lengthof(_nested_music_window_widgets),
780 &_music_window_prefs
783 void ShowMusicWindow()
785 if (BaseMusic::GetUsedSet()->num_available == 0) ShowErrorMessage(STR_ERROR_NO_SONGS, INVALID_STRING_ID, WL_WARNING);
786 AllocateWindowDescFront<MusicWindow>(&_music_window_desc, 0);