music_gui.cpp

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