music_gui.cpp

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