music_gui.cpp

Go to the documentation of this file.
00001 /* $Id: music_gui.cpp 26820 2014-09-14 15:24:39Z frosch $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "openttd.h"
00014 #include "base_media_base.h"
00015 #include "music/music_driver.hpp"
00016 #include "window_gui.h"
00017 #include "strings_func.h"
00018 #include "window_func.h"
00019 #include "sound_func.h"
00020 #include "gfx_func.h"
00021 #include "core/random_func.hpp"
00022 #include "error.h"
00023 #include "core/geometry_func.hpp"
00024 #include "string_func.h"
00025 #include "settings_type.h"
00026 
00027 #include "widgets/music_widget.h"
00028 
00029 #include "table/strings.h"
00030 #include "table/sprites.h"
00031 
00037 static const char *GetSongName(int index)
00038 {
00039   return BaseMusic::GetUsedSet()->song_name[index];
00040 }
00041 
00047 static int GetTrackNumber(int index)
00048 {
00049   return BaseMusic::GetUsedSet()->track_nr[index];
00050 }
00051 
00053 static byte _music_wnd_cursong = 1;
00055 static bool _song_is_active = false;
00056 
00058 static byte _cur_playlist[NUM_SONGS_PLAYLIST + 1];
00059 
00061 static byte _playlist_all[NUM_SONGS_AVAILABLE + 1];
00063 static byte _playlist_old_style[NUM_SONGS_CLASS + 1];
00065 static byte _playlist_new_style[NUM_SONGS_CLASS + 1];
00067 static byte _playlist_ezy_street[NUM_SONGS_CLASS + 1];
00068 
00069 assert_compile(lengthof(_settings_client.music.custom_1) == NUM_SONGS_PLAYLIST + 1);
00070 assert_compile(lengthof(_settings_client.music.custom_2) == NUM_SONGS_PLAYLIST + 1);
00071 
00073 static byte * const _playlists[] = {
00074   _playlist_all,
00075   _playlist_old_style,
00076   _playlist_new_style,
00077   _playlist_ezy_street,
00078   _settings_client.music.custom_1,
00079   _settings_client.music.custom_2,
00080 };
00081 
00087 void ValidatePlaylist(byte *playlist, byte *last)
00088 {
00089   while (*playlist != 0 && playlist <= last) {
00090     /* Song indices are saved off-by-one so 0 is "nothing". */
00091     if (*playlist <= NUM_SONGS_AVAILABLE && !StrEmpty(GetSongName(*playlist - 1))) {
00092       playlist++;
00093       continue;
00094     }
00095     for (byte *p = playlist; *p != 0 && p <= last; p++) {
00096       p[0] = p[1];
00097     }
00098   }
00099 
00100   /* Make sure the list is null terminated. */
00101   *last = 0;
00102 }
00103 
00105 void InitializeMusic()
00106 {
00107   uint j = 0;
00108   for (uint i = 0; i < NUM_SONGS_AVAILABLE; i++) {
00109     if (StrEmpty(GetSongName(i))) continue;
00110     _playlist_all[j++] = i + 1;
00111   }
00112   /* Terminate the list */
00113   _playlist_all[j] = 0;
00114 
00115   /* Now make the 'styled' playlists */
00116   for (uint k = 0; k < NUM_SONG_CLASSES; k++) {
00117     j = 0;
00118     for (uint i = 0; i < NUM_SONGS_CLASS; i++) {
00119       int id = k * NUM_SONGS_CLASS + i + 1;
00120       if (StrEmpty(GetSongName(id))) continue;
00121       _playlists[k + 1][j++] = id + 1;
00122     }
00123     /* Terminate the list */
00124     _playlists[k + 1][j] = 0;
00125   }
00126 
00127   ValidatePlaylist(_settings_client.music.custom_1, lastof(_settings_client.music.custom_1));
00128   ValidatePlaylist(_settings_client.music.custom_2, lastof(_settings_client.music.custom_2));
00129 
00130   if (BaseMusic::GetUsedSet()->num_available < _music_wnd_cursong) {
00131     /* If there are less songs than the currently played song,
00132      * just pause and reset to no song. */
00133     _music_wnd_cursong = 0;
00134     _song_is_active = false;
00135   }
00136 }
00137 
00138 static void SkipToPrevSong()
00139 {
00140   byte *b = _cur_playlist;
00141   byte *p = b;
00142   byte t;
00143 
00144   if (b[0] == 0) return; // empty playlist
00145 
00146   do p++; while (p[0] != 0); // find the end
00147 
00148   t = *--p; // and copy the bytes
00149   while (p != b) {
00150     p--;
00151     p[1] = p[0];
00152   }
00153   *b = t;
00154 
00155   _song_is_active = false;
00156 }
00157 
00158 static void SkipToNextSong()
00159 {
00160   byte *b = _cur_playlist;
00161   byte t;
00162 
00163   t = b[0];
00164   if (t != 0) {
00165     while (b[1] != 0) {
00166       b[0] = b[1];
00167       b++;
00168     }
00169     b[0] = t;
00170   }
00171 
00172   _song_is_active = false;
00173 }
00174 
00175 static void MusicVolumeChanged(byte new_vol)
00176 {
00177   MusicDriver::GetInstance()->SetVolume(new_vol);
00178 }
00179 
00180 static void DoPlaySong()
00181 {
00182   char filename[MAX_PATH];
00183   if (FioFindFullPath(filename, lengthof(filename), BASESET_DIR, BaseMusic::GetUsedSet()->files[_music_wnd_cursong - 1].filename) == NULL) {
00184     FioFindFullPath(filename, lengthof(filename), OLD_GM_DIR, BaseMusic::GetUsedSet()->files[_music_wnd_cursong - 1].filename);
00185   }
00186   MusicDriver::GetInstance()->PlaySong(filename);
00187   SetWindowDirty(WC_MUSIC_WINDOW, 0);
00188 }
00189 
00190 static void DoStopMusic()
00191 {
00192   MusicDriver::GetInstance()->StopSong();
00193   SetWindowDirty(WC_MUSIC_WINDOW, 0);
00194 }
00195 
00196 static void SelectSongToPlay()
00197 {
00198   uint i = 0;
00199   uint j = 0;
00200 
00201   memset(_cur_playlist, 0, sizeof(_cur_playlist));
00202   do {
00203     /* File is the index into the file table of the music set. The play list uses 0 as 'no entry',
00204      * so we need to subtract 1. In case of 'no entry' (file = -1), just skip adding it outright. */
00205     int file = _playlists[_settings_client.music.playlist][i] - 1;
00206     if (file >= 0) {
00207       const char *filename = BaseMusic::GetUsedSet()->files[file].filename;
00208       /* We are now checking for the existence of that file prior
00209       * to add it to the list of available songs */
00210       if (!StrEmpty(filename) && FioCheckFileExists(filename, BASESET_DIR)) {
00211         _cur_playlist[j] = _playlists[_settings_client.music.playlist][i];
00212         j++;
00213       }
00214     }
00215   } while (_playlists[_settings_client.music.playlist][++i] != 0 && j < lengthof(_cur_playlist) - 1);
00216 
00217   /* Do not shuffle when on the intro-start window, as the song to play has to be the original TTD Theme*/
00218   if (_settings_client.music.shuffle && _game_mode != GM_MENU) {
00219     i = 500;
00220     do {
00221       uint32 r = InteractiveRandom();
00222       byte *a = &_cur_playlist[GB(r, 0, 5)];
00223       byte *b = &_cur_playlist[GB(r, 8, 5)];
00224 
00225       if (*a != 0 && *b != 0) {
00226         byte t = *a;
00227         *a = *b;
00228         *b = t;
00229       }
00230     } while (--i);
00231   }
00232 }
00233 
00234 static void StopMusic()
00235 {
00236   _music_wnd_cursong = 0;
00237   DoStopMusic();
00238   _song_is_active = false;
00239   SetWindowWidgetDirty(WC_MUSIC_WINDOW, 0, 9);
00240 }
00241 
00242 static void PlayPlaylistSong()
00243 {
00244   if (_cur_playlist[0] == 0) {
00245     SelectSongToPlay();
00246     /* if there is not songs in the playlist, it may indicate
00247      * no file on the gm folder, or even no gm folder.
00248      * Stop the playback, then */
00249     if (_cur_playlist[0] == 0) {
00250       _song_is_active = false;
00251       _music_wnd_cursong = 0;
00252       _settings_client.music.playing = false;
00253       return;
00254     }
00255   }
00256   _music_wnd_cursong = _cur_playlist[0];
00257   DoPlaySong();
00258   _song_is_active = true;
00259 
00260   SetWindowWidgetDirty(WC_MUSIC_WINDOW, 0, 9);
00261 }
00262 
00263 void ResetMusic()
00264 {
00265   _music_wnd_cursong = 1;
00266   DoPlaySong();
00267 }
00268 
00269 void MusicLoop()
00270 {
00271   if (!_settings_client.music.playing && _song_is_active) {
00272     StopMusic();
00273   } else if (_settings_client.music.playing && !_song_is_active) {
00274     PlayPlaylistSong();
00275   }
00276 
00277   if (!_song_is_active) return;
00278 
00279   if (!MusicDriver::GetInstance()->IsSongPlaying()) {
00280     if (_game_mode != GM_MENU) {
00281       StopMusic();
00282       SkipToNextSong();
00283       PlayPlaylistSong();
00284     } else {
00285       ResetMusic();
00286     }
00287   }
00288 }
00289 
00290 static void SelectPlaylist(byte list)
00291 {
00292   _settings_client.music.playlist = list;
00293   InvalidateWindowData(WC_MUSIC_TRACK_SELECTION, 0);
00294   InvalidateWindowData(WC_MUSIC_WINDOW, 0);
00295 }
00296 
00297 struct MusicTrackSelectionWindow : public Window {
00298   MusicTrackSelectionWindow(WindowDesc *desc, WindowNumber number) : Window(desc)
00299   {
00300     this->InitNested(number);
00301     this->LowerWidget(WID_MTS_LIST_LEFT);
00302     this->LowerWidget(WID_MTS_LIST_RIGHT);
00303     this->SetWidgetDisabledState(WID_MTS_CLEAR, _settings_client.music.playlist <= 3);
00304     this->LowerWidget(WID_MTS_ALL + _settings_client.music.playlist);
00305   }
00306 
00307   virtual void SetStringParameters(int widget) const
00308   {
00309     switch (widget) {
00310       case WID_MTS_PLAYLIST:
00311         SetDParam(0, STR_MUSIC_PLAYLIST_ALL + _settings_client.music.playlist);
00312         break;
00313     }
00314   }
00315 
00321   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00322   {
00323     if (!gui_scope) return;
00324     for (int i = 0; i < 6; i++) {
00325       this->SetWidgetLoweredState(WID_MTS_ALL + i, i == _settings_client.music.playlist);
00326     }
00327     this->SetWidgetDisabledState(WID_MTS_CLEAR, _settings_client.music.playlist <= 3);
00328     this->SetDirty();
00329   }
00330 
00331   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00332   {
00333     switch (widget) {
00334       case WID_MTS_PLAYLIST: {
00335         Dimension d = {0, 0};
00336 
00337         for (int i = 0; i < 6; i++) {
00338           SetDParam(0, STR_MUSIC_PLAYLIST_ALL + i);
00339           d = maxdim(d, GetStringBoundingBox(STR_PLAYLIST_PROGRAM));
00340         }
00341         d.width += padding.width;
00342         d.height += padding.height;
00343         *size = maxdim(*size, d);
00344         break;
00345       }
00346 
00347       case WID_MTS_LIST_LEFT: case WID_MTS_LIST_RIGHT: {
00348         Dimension d = {0, 0};
00349 
00350         for (uint i = 0; i < NUM_SONGS_AVAILABLE; i++) {
00351           const char *song_name = GetSongName(i);
00352           if (StrEmpty(song_name)) continue;
00353 
00354           SetDParam(0, GetTrackNumber(i));
00355           SetDParam(1, 2);
00356           SetDParamStr(2, GetSongName(i));
00357           Dimension d2 = GetStringBoundingBox(STR_PLAYLIST_TRACK_NAME);
00358           d.width = max(d.width, d2.width);
00359           d.height += d2.height;
00360         }
00361         d.width += padding.width;
00362         d.height += padding.height;
00363         *size = maxdim(*size, d);
00364         break;
00365       }
00366     }
00367   }
00368 
00369   virtual void DrawWidget(const Rect &r, int widget) const
00370   {
00371     switch (widget) {
00372       case WID_MTS_LIST_LEFT: {
00373         GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK);
00374 
00375         int y = r.top + WD_FRAMERECT_TOP;
00376         for (uint i = 0; i < NUM_SONGS_AVAILABLE; i++) {
00377           const char *song_name = GetSongName(i);
00378           if (StrEmpty(song_name)) continue;
00379 
00380           SetDParam(0, GetTrackNumber(i));
00381           SetDParam(1, 2);
00382           SetDParamStr(2, song_name);
00383           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_PLAYLIST_TRACK_NAME);
00384           y += FONT_HEIGHT_SMALL;
00385         }
00386         break;
00387       }
00388 
00389       case WID_MTS_LIST_RIGHT: {
00390         GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK);
00391 
00392         int y = r.top + WD_FRAMERECT_TOP;
00393         for (const byte *p = _playlists[_settings_client.music.playlist]; *p != 0; p++) {
00394           uint i = *p - 1;
00395           SetDParam(0, GetTrackNumber(i));
00396           SetDParam(1, 2);
00397           SetDParamStr(2, GetSongName(i));
00398           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_PLAYLIST_TRACK_NAME);
00399           y += FONT_HEIGHT_SMALL;
00400         }
00401         break;
00402       }
00403     }
00404   }
00405 
00406   virtual void OnClick(Point pt, int widget, int click_count)
00407   {
00408     switch (widget) {
00409       case WID_MTS_LIST_LEFT: { // add to playlist
00410         int y = this->GetRowFromWidget(pt.y, widget, 0, FONT_HEIGHT_SMALL);
00411 
00412         if (_settings_client.music.playlist < 4) return;
00413         if (!IsInsideMM(y, 0, BaseMusic::GetUsedSet()->num_available)) return;
00414 
00415         byte *p = _playlists[_settings_client.music.playlist];
00416         for (uint i = 0; i != NUM_SONGS_PLAYLIST - 1; i++) {
00417           if (p[i] == 0) {
00418             /* Find the actual song number */
00419             for (uint j = 0; j < NUM_SONGS_AVAILABLE; j++) {
00420               if (GetTrackNumber(j) == y + 1) {
00421                 p[i] = j + 1;
00422                 break;
00423               }
00424             }
00425             p[i + 1] = 0;
00426             this->SetDirty();
00427             SelectSongToPlay();
00428             break;
00429           }
00430         }
00431         break;
00432       }
00433 
00434       case WID_MTS_LIST_RIGHT: { // remove from playlist
00435         int y = this->GetRowFromWidget(pt.y, widget, 0, FONT_HEIGHT_SMALL);
00436 
00437         if (_settings_client.music.playlist < 4) return;
00438         if (!IsInsideMM(y, 0, NUM_SONGS_PLAYLIST)) return;
00439 
00440         byte *p = _playlists[_settings_client.music.playlist];
00441         for (uint i = y; i != NUM_SONGS_PLAYLIST - 1; i++) {
00442           p[i] = p[i + 1];
00443         }
00444 
00445         this->SetDirty();
00446         SelectSongToPlay();
00447         break;
00448       }
00449 
00450       case WID_MTS_CLEAR: // clear
00451         for (uint i = 0; _playlists[_settings_client.music.playlist][i] != 0; i++) _playlists[_settings_client.music.playlist][i] = 0;
00452         this->SetDirty();
00453         StopMusic();
00454         SelectSongToPlay();
00455         break;
00456 
00457       case WID_MTS_ALL: case WID_MTS_OLD: case WID_MTS_NEW:
00458       case WID_MTS_EZY: case WID_MTS_CUSTOM1: case WID_MTS_CUSTOM2: // set playlist
00459         SelectPlaylist(widget - WID_MTS_ALL);
00460         StopMusic();
00461         SelectSongToPlay();
00462         break;
00463     }
00464   }
00465 };
00466 
00467 static const NWidgetPart _nested_music_track_selection_widgets[] = {
00468   NWidget(NWID_HORIZONTAL),
00469     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00470     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_PLAYLIST_MUSIC_PROGRAM_SELECTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00471   EndContainer(),
00472   NWidget(WWT_PANEL, COLOUR_GREY),
00473     NWidget(NWID_HORIZONTAL), SetPIP(2, 4, 2),
00474       /* Left panel. */
00475       NWidget(NWID_VERTICAL),
00476         NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_PLAYLIST_TRACK_INDEX, STR_NULL),
00477         NWidget(WWT_PANEL, COLOUR_GREY, WID_MTS_LIST_LEFT), SetMinimalSize(180, 194), SetDataTip(0x0, STR_PLAYLIST_TOOLTIP_CLICK_TO_ADD_TRACK), EndContainer(),
00478         NWidget(NWID_SPACER), SetMinimalSize(0, 2),
00479       EndContainer(),
00480       /* Middle buttons. */
00481       NWidget(NWID_VERTICAL),
00482         NWidget(NWID_SPACER), SetMinimalSize(60, 30), // Space above the first button from the title bar.
00483         NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_MTS_ALL), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_ALL, STR_MUSIC_TOOLTIP_SELECT_ALL_TRACKS_PROGRAM),
00484         NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_MTS_OLD), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_OLD_STYLE, STR_MUSIC_TOOLTIP_SELECT_OLD_STYLE_MUSIC),
00485         NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_MTS_NEW), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_NEW_STYLE, STR_MUSIC_TOOLTIP_SELECT_NEW_STYLE_MUSIC),
00486         NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_MTS_EZY), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_EZY_STREET, STR_MUSIC_TOOLTIP_SELECT_EZY_STREET_STYLE),
00487         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),
00488         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),
00489         NWidget(NWID_SPACER), SetMinimalSize(0, 16), // Space above 'clear' button
00490         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_MTS_CLEAR), SetFill(1, 0), SetDataTip(STR_PLAYLIST_CLEAR, STR_PLAYLIST_TOOLTIP_CLEAR_CURRENT_PROGRAM_CUSTOM1),
00491         NWidget(NWID_SPACER), SetFill(0, 1),
00492       EndContainer(),
00493       /* Right panel. */
00494       NWidget(NWID_VERTICAL),
00495         NWidget(WWT_LABEL, COLOUR_GREY, WID_MTS_PLAYLIST), SetDataTip(STR_PLAYLIST_PROGRAM, STR_NULL),
00496         NWidget(WWT_PANEL, COLOUR_GREY, WID_MTS_LIST_RIGHT), SetMinimalSize(180, 194), SetDataTip(0x0, STR_PLAYLIST_TOOLTIP_CLICK_TO_REMOVE_TRACK), EndContainer(),
00497         NWidget(NWID_SPACER), SetMinimalSize(0, 2),
00498       EndContainer(),
00499     EndContainer(),
00500   EndContainer(),
00501 };
00502 
00503 static WindowDesc _music_track_selection_desc(
00504   WDP_AUTO, "music_track", 0, 0,
00505   WC_MUSIC_TRACK_SELECTION, WC_NONE,
00506   0,
00507   _nested_music_track_selection_widgets, lengthof(_nested_music_track_selection_widgets)
00508 );
00509 
00510 static void ShowMusicTrackSelection()
00511 {
00512   AllocateWindowDescFront<MusicTrackSelectionWindow>(&_music_track_selection_desc, 0);
00513 }
00514 
00515 struct MusicWindow : public Window {
00516   static const int slider_width = 3;
00517 
00518   MusicWindow(WindowDesc *desc, WindowNumber number) : Window(desc)
00519   {
00520     this->InitNested(number);
00521     this->LowerWidget(_settings_client.music.playlist + WID_M_ALL);
00522     this->SetWidgetLoweredState(WID_M_SHUFFLE, _settings_client.music.shuffle);
00523   }
00524 
00525   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00526   {
00527     switch (widget) {
00528       /* Make sure that WID_M_SHUFFLE and WID_M_PROGRAMME have the same size.
00529        * This can't be done by using NC_EQUALSIZE as the WID_M_INFO is
00530        * between those widgets and of different size. */
00531       case WID_M_SHUFFLE: case WID_M_PROGRAMME: {
00532         Dimension d = maxdim(GetStringBoundingBox(STR_MUSIC_PROGRAM), GetStringBoundingBox(STR_MUSIC_SHUFFLE));
00533         d.width += padding.width;
00534         d.height += padding.height;
00535         *size = maxdim(*size, d);
00536         break;
00537       }
00538 
00539       case WID_M_TRACK_NR: {
00540         Dimension d = GetStringBoundingBox(STR_MUSIC_TRACK_NONE);
00541         d.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
00542         d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
00543         *size = maxdim(*size, d);
00544         break;
00545       }
00546 
00547       case WID_M_TRACK_NAME: {
00548         Dimension d = GetStringBoundingBox(STR_MUSIC_TITLE_NONE);
00549         for (uint i = 0; i < NUM_SONGS_AVAILABLE; i++) {
00550           SetDParamStr(0, GetSongName(i));
00551           d = maxdim(d, GetStringBoundingBox(STR_MUSIC_TITLE_NAME));
00552         }
00553         d.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
00554         d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
00555         *size = maxdim(*size, d);
00556         break;
00557       }
00558 
00559       /* Hack-ish: set the proper widget data; only needs to be done once
00560        * per (Re)Init as that's the only time the language changes. */
00561       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;
00562       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;
00563       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;
00564     }
00565   }
00566 
00567   virtual void DrawWidget(const Rect &r, int widget) const
00568   {
00569     switch (widget) {
00570       case WID_M_TRACK_NR: {
00571         GfxFillRect(r.left + 1, r.top + 1, r.right, r.bottom, PC_BLACK);
00572         StringID str = STR_MUSIC_TRACK_NONE;
00573         if (_song_is_active != 0 && _music_wnd_cursong != 0) {
00574           SetDParam(0, GetTrackNumber(_music_wnd_cursong - 1));
00575           SetDParam(1, 2);
00576           str = STR_MUSIC_TRACK_DIGIT;
00577         }
00578         DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, str);
00579         break;
00580       }
00581 
00582       case WID_M_TRACK_NAME: {
00583         GfxFillRect(r.left, r.top + 1, r.right - 1, r.bottom, PC_BLACK);
00584         StringID str = STR_MUSIC_TITLE_NONE;
00585         if (_song_is_active != 0 && _music_wnd_cursong != 0) {
00586           str = STR_MUSIC_TITLE_NAME;
00587           SetDParamStr(0, GetSongName(_music_wnd_cursong - 1));
00588         }
00589         DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, str, TC_FROMSTRING, SA_HOR_CENTER);
00590         break;
00591       }
00592 
00593       case WID_M_MUSIC_VOL: case WID_M_EFFECT_VOL: {
00594         DrawFrameRect(r.left, r.top + 2, r.right, r.bottom - 2, COLOUR_GREY, FR_LOWERED);
00595         byte volume = (widget == WID_M_MUSIC_VOL) ? _settings_client.music.music_vol : _settings_client.music.effect_vol;
00596         int x = (volume * (r.right - r.left) / 127);
00597         if (_current_text_dir == TD_RTL) {
00598           x = r.right - x;
00599         } else {
00600           x += r.left;
00601         }
00602         DrawFrameRect(x, r.top, x + slider_width, r.bottom, COLOUR_GREY, FR_NONE);
00603         break;
00604       }
00605     }
00606   }
00607 
00613   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00614   {
00615     if (!gui_scope) return;
00616     for (int i = 0; i < 6; i++) {
00617       this->SetWidgetLoweredState(WID_M_ALL + i, i == _settings_client.music.playlist);
00618     }
00619     this->SetDirty();
00620   }
00621 
00622   virtual void OnClick(Point pt, int widget, int click_count)
00623   {
00624     switch (widget) {
00625       case WID_M_PREV: // skip to prev
00626         if (!_song_is_active) return;
00627         SkipToPrevSong();
00628         this->SetDirty();
00629         break;
00630 
00631       case WID_M_NEXT: // skip to next
00632         if (!_song_is_active) return;
00633         SkipToNextSong();
00634         this->SetDirty();
00635         break;
00636 
00637       case WID_M_STOP: // stop playing
00638         _settings_client.music.playing = false;
00639         break;
00640 
00641       case WID_M_PLAY: // start playing
00642         _settings_client.music.playing = true;
00643         break;
00644 
00645       case WID_M_MUSIC_VOL: case WID_M_EFFECT_VOL: { // volume sliders
00646         int x = pt.x - this->GetWidget<NWidgetBase>(widget)->pos_x;
00647 
00648         byte *vol = (widget == WID_M_MUSIC_VOL) ? &_settings_client.music.music_vol : &_settings_client.music.effect_vol;
00649 
00650         byte new_vol = x * 127 / this->GetWidget<NWidgetBase>(widget)->current_x;
00651         if (_current_text_dir == TD_RTL) new_vol = 127 - new_vol;
00652         if (new_vol != *vol) {
00653           *vol = new_vol;
00654           if (widget == WID_M_MUSIC_VOL) MusicVolumeChanged(new_vol);
00655           this->SetDirty();
00656         }
00657 
00658         _left_button_clicked = false;
00659         break;
00660       }
00661 
00662       case WID_M_SHUFFLE: // toggle shuffle
00663         _settings_client.music.shuffle ^= 1;
00664         this->SetWidgetLoweredState(WID_M_SHUFFLE, _settings_client.music.shuffle);
00665         this->SetWidgetDirty(WID_M_SHUFFLE);
00666         StopMusic();
00667         SelectSongToPlay();
00668         this->SetDirty();
00669         break;
00670 
00671       case WID_M_PROGRAMME: // show track selection
00672         ShowMusicTrackSelection();
00673         break;
00674 
00675       case WID_M_ALL: case WID_M_OLD: case WID_M_NEW:
00676       case WID_M_EZY: case WID_M_CUSTOM1: case WID_M_CUSTOM2: // playlist
00677         SelectPlaylist(widget - WID_M_ALL);
00678         StopMusic();
00679         SelectSongToPlay();
00680         this->SetDirty();
00681         break;
00682     }
00683   }
00684 };
00685 
00686 static const NWidgetPart _nested_music_window_widgets[] = {
00687   NWidget(NWID_HORIZONTAL),
00688     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00689     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_MUSIC_JAZZ_JUKEBOX_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00690     NWidget(WWT_SHADEBOX, COLOUR_GREY),
00691     NWidget(WWT_STICKYBOX, COLOUR_GREY),
00692   EndContainer(),
00693 
00694   NWidget(NWID_HORIZONTAL),
00695     NWidget(NWID_VERTICAL),
00696       NWidget(WWT_PANEL, COLOUR_GREY, -1), SetFill(1, 1), EndContainer(),
00697       NWidget(NWID_HORIZONTAL),
00698         NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_M_PREV), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_SKIP_TO_PREV, STR_MUSIC_TOOLTIP_SKIP_TO_PREVIOUS_TRACK),
00699         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),
00700         NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_M_STOP), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_STOP_MUSIC, STR_MUSIC_TOOLTIP_STOP_PLAYING_MUSIC),
00701         NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_M_PLAY), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_PLAY_MUSIC, STR_MUSIC_TOOLTIP_START_PLAYING_MUSIC),
00702       EndContainer(),
00703       NWidget(WWT_PANEL, COLOUR_GREY, -1), SetFill(1, 1), EndContainer(),
00704     EndContainer(),
00705     NWidget(WWT_PANEL, COLOUR_GREY, WID_M_SLIDERS),
00706       NWidget(NWID_HORIZONTAL), SetPIP(20, 20, 20),
00707         NWidget(NWID_VERTICAL),
00708           NWidget(WWT_LABEL, COLOUR_GREY, -1), SetFill(1, 0), SetDataTip(STR_MUSIC_MUSIC_VOLUME, STR_NULL),
00709           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),
00710           NWidget(NWID_HORIZONTAL),
00711             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MIN, STR_NULL),
00712             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
00713             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
00714             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
00715             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
00716             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
00717             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MAX, STR_NULL),
00718           EndContainer(),
00719         EndContainer(),
00720         NWidget(NWID_VERTICAL),
00721           NWidget(WWT_LABEL, COLOUR_GREY, -1), SetFill(1, 0), SetDataTip(STR_MUSIC_EFFECTS_VOLUME, STR_NULL),
00722           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),
00723           NWidget(NWID_HORIZONTAL),
00724             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MIN, STR_NULL),
00725             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
00726             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
00727             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
00728             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
00729             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
00730             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MAX, STR_NULL),
00731           EndContainer(),
00732         EndContainer(),
00733       EndContainer(),
00734     EndContainer(),
00735   EndContainer(),
00736   NWidget(WWT_PANEL, COLOUR_GREY, WID_M_BACKGROUND),
00737     NWidget(NWID_HORIZONTAL), SetPIP(6, 0, 6),
00738       NWidget(NWID_VERTICAL),
00739         NWidget(NWID_SPACER), SetFill(0, 1),
00740         NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_M_SHUFFLE), SetMinimalSize(50, 8), SetDataTip(STR_MUSIC_SHUFFLE, STR_MUSIC_TOOLTIP_TOGGLE_PROGRAM_SHUFFLE),
00741         NWidget(NWID_SPACER), SetFill(0, 1),
00742       EndContainer(),
00743       NWidget(NWID_VERTICAL), SetPadding(0, 0, 3, 3),
00744         NWidget(WWT_LABEL, COLOUR_GREY, WID_M_TRACK), SetFill(0, 0), SetDataTip(STR_MUSIC_TRACK, STR_NULL),
00745         NWidget(WWT_PANEL, COLOUR_GREY, WID_M_TRACK_NR), EndContainer(),
00746       EndContainer(),
00747       NWidget(NWID_VERTICAL), SetPadding(0, 3, 3, 0),
00748         NWidget(WWT_LABEL, COLOUR_GREY, WID_M_TRACK_TITLE), SetFill(1, 0), SetDataTip(STR_MUSIC_XTITLE, STR_NULL),
00749         NWidget(WWT_PANEL, COLOUR_GREY, WID_M_TRACK_NAME), SetFill(1, 0), EndContainer(),
00750       EndContainer(),
00751       NWidget(NWID_VERTICAL),
00752         NWidget(NWID_SPACER), SetFill(0, 1),
00753         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_M_PROGRAMME), SetMinimalSize(50, 8), SetDataTip(STR_MUSIC_PROGRAM, STR_MUSIC_TOOLTIP_SHOW_MUSIC_TRACK_SELECTION),
00754         NWidget(NWID_SPACER), SetFill(0, 1),
00755       EndContainer(),
00756     EndContainer(),
00757   EndContainer(),
00758   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00759     NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_M_ALL), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_ALL, STR_MUSIC_TOOLTIP_SELECT_ALL_TRACKS_PROGRAM),
00760     NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_M_OLD), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_OLD_STYLE, STR_MUSIC_TOOLTIP_SELECT_OLD_STYLE_MUSIC),
00761     NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_M_NEW), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_NEW_STYLE, STR_MUSIC_TOOLTIP_SELECT_NEW_STYLE_MUSIC),
00762     NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_M_EZY), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_EZY_STREET, STR_MUSIC_TOOLTIP_SELECT_EZY_STREET_STYLE),
00763     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),
00764     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),
00765   EndContainer(),
00766 };
00767 
00768 static WindowDesc _music_window_desc(
00769   WDP_AUTO, "music", 0, 0,
00770   WC_MUSIC_WINDOW, WC_NONE,
00771   0,
00772   _nested_music_window_widgets, lengthof(_nested_music_window_widgets)
00773 );
00774 
00775 void ShowMusicWindow()
00776 {
00777   if (BaseMusic::GetUsedSet()->num_available == 0) ShowErrorMessage(STR_ERROR_NO_SONGS, INVALID_STRING_ID, WL_WARNING);
00778   AllocateWindowDescFront<MusicWindow>(&_music_window_desc, 0);
00779 }