#region license // This file is part of Vocaluxe. // // Vocaluxe 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, either version 3 of the License, or // (at your option) any later version. // // Vocaluxe 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. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with Vocaluxe. If not, see . #endregion using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Windows.Forms; using VocaluxeLib.Draw; using VocaluxeLib.PartyModes; using VocaluxeLib.Songs; namespace VocaluxeLib.Menu.SongMenu { class CSongMenuTileBoard : CSongMenuFramework { private SRectF _ScrollRect; private List _Tiles; private CStatic _CoverBig; private CStatic _TextBG; private CStatic _DuetIcon; private CStatic _VideoIcon; private CStatic _MedleyCalcIcon; private CStatic _MedleyTagIcon; private CTexture _CoverBigBGTexture; private CTexture _CoverBGTexture; private CText _Artist; private CText _Title; private CText _SongLength; private int _NumW; private int _NumH; private float _SpaceW; private float _SpaceH; private int _TileW; private int _TileH; // Offset is the song or categoryNr of the tile in the left upper corner private int _Offset; private float _Length = -1f; private int _LastKnownNumSongs; private int _LastKnownCategory = -1; public override float SelectedTileZoomFactor { get { return 1.2f; } } protected override int _SelectionNr { set { int max = CBase.Songs.IsInCategory() ? CBase.Songs.GetNumSongsVisible() : CBase.Songs.GetNumCategories(); base._SelectionNr = value.Clamp(-1, max - 1); //Update list in case we scrolled _UpdateList(); foreach (CStatic tile in _Tiles) tile.Selected = false; int tileNr = _SelectionNr - _Offset; if (tileNr >= 0 && tileNr < _Tiles.Count) _Tiles[tileNr].Selected = true; } } public override bool SmallView { set { if (SmallView == value) return; base.SmallView = value; _InitTiles(); _UpdateList(true); } } public CSongMenuTileBoard(int partyModeID) : base(partyModeID) {} protected override int _PreviewNr { set { if (value == base._PreviewNr) return; base._PreviewNr = value; _UpdatePreview(); } } public override void Init() { base.Init(); _SpaceW = _Theme.SongMenuTileBoard.SpaceW; _SpaceH = _Theme.SongMenuTileBoard.SpaceH; _CoverBig = _Theme.SongMenuTileBoard.StaticCoverBig; _TextBG = _Theme.SongMenuTileBoard.StaticTextBG; _DuetIcon = _Theme.SongMenuTileBoard.StaticDuetIcon; _VideoIcon = _Theme.SongMenuTileBoard.StaticVideoIcon; _MedleyCalcIcon = _Theme.SongMenuTileBoard.StaticMedleyCalcIcon; _MedleyTagIcon = _Theme.SongMenuTileBoard.StaticMedleyTagIcon; _Artist = _Theme.SongMenuTileBoard.TextArtist; _Title = _Theme.SongMenuTileBoard.TextTitle; _SongLength = _Theme.SongMenuTileBoard.TextSongLength; _PreviewNr = -1; _InitTiles(); } private void _InitTiles() { if (SmallView) { _NumH = _Theme.SongMenuTileBoard.NumHsmall; _NumW = _Theme.SongMenuTileBoard.NumWsmall; Rect = _Theme.SongMenuTileBoard.TileRectSmall; } else { _NumH = _Theme.SongMenuTileBoard.NumH; _NumW = _Theme.SongMenuTileBoard.NumW; Rect = _Theme.SongMenuTileBoard.TileRect; } _TileW = (int)((Rect.W - _SpaceW * (_NumW - 1)) / _NumW); _TileH = (int)((Rect.H - _SpaceH * (_NumH - 1)) / _NumH); _CoverBGTexture = CBase.Theme.GetSkinTexture(_Theme.CoverBackgroundName, _PartyModeID); _CoverBigBGTexture = CBase.Theme.GetSkinTexture(_Theme.CoverBigBackgroundName, _PartyModeID); _Tiles = new List(); for (int i = 0; i < _NumH; i++) { for (int j = 0; j < _NumW; j++) { var rect = new SRectF(Rect.X + j * (_TileW + _SpaceW), Rect.Y + i * (_TileH + _SpaceH), _TileW, _TileH, Rect.Z); var tile = new CStatic(_PartyModeID, _CoverBGTexture, _Color, rect); _Tiles.Add(tile); } } _ScrollRect = new SRectF(0, 0, CBase.Settings.GetRenderW(), CBase.Settings.GetRenderH(), Rect.Z); } public override void Update(SScreenSongOptions songOptions) { base.Update(songOptions); if (songOptions.Selection.RandomOnly) _PreviewNr = _SelectionNr; if (_Length < 0 && CBase.Songs.IsInCategory() && CBase.Sound.GetLength(_PreviewSongStream) > 0) _UpdateLength(CBase.Songs.GetVisibleSong(_PreviewNr)); } private void _UpdatePreview() { //First hide everything so we just have to set what we actually want _CoverBig.Texture = _CoverBigBGTexture; _Artist.Text = String.Empty; _Title.Text = String.Empty; _SongLength.Text = String.Empty; _DuetIcon.Visible = false; _VideoIcon.Visible = false; _MedleyCalcIcon.Visible = false; _MedleyTagIcon.Visible = false; _Length = -1f; //Check if nothing is selected (for preview) if (_PreviewNr < 0) return; if (CBase.Songs.IsInCategory()) { CSong song = CBase.Songs.GetVisibleSong(_PreviewNr); //Check if we have a valid song (song still visible, index >=0 etc is checked by framework) if (song == null) { //Display at least the category CCategory category = CBase.Songs.GetCategory(CBase.Songs.GetCurrentCategoryIndex()); //Check if we have a valid category if (category == null) return; _CoverBig.Texture = category.CoverTextureBig; _Artist.Text = category.Name; return; } _CoverBig.Texture = song.CoverTextureBig; _Artist.Text = song.Artist; _Title.Text = song.Title; _DuetIcon.Visible = song.IsDuet; _VideoIcon.Visible = song.VideoFileName != ""; _MedleyCalcIcon.Visible = song.Medley.Source == EDataSource.Calculated; _MedleyTagIcon.Visible = song.Medley.Source == EDataSource.Tag; _UpdateLength(song); } else { CCategory category = CBase.Songs.GetCategory(_PreviewNr); //Check if we have a valid category if (category == null) return; _CoverBig.Texture = category.CoverTextureBig; _Artist.Text = category.Name; int num = category.GetNumSongsNotSung(); String songOrSongs = (num == 1) ? "TR_SCREENSONG_NUMSONG" : "TR_SCREENSONG_NUMSONGS"; _Title.Text = CBase.Language.Translate(songOrSongs).Replace("%v", num.ToString()); } } private void _UpdateLength(CSong song) { if (song == null) return; float time = CBase.Sound.GetLength(_PreviewSongStream); if (Math.Abs(song.Finish) > 0.001) time = song.Finish; // The audiobackend is ready to return the length if (time > 0) { time -= song.Start; var min = (int)Math.Floor(time / 60f); var sec = (int)(time - min * 60f); _SongLength.Text = min.ToString("00") + ":" + sec.ToString("00"); _Length = time; } else _SongLength.Text = "..."; } public override void OnShow() { if (CBase.Songs.GetTabs() == EOffOn.TR_CONFIG_OFF && CBase.Songs.GetNumCategories() > 0 && !CBase.Songs.IsInCategory()) _EnterCategory(0); else { if (CBase.Songs.IsInCategory()) { if (_SelectionNr < 0) SetSelectedSong(0); else SetSelectedSong(_SelectionNr); } else { if (_SelectionNr < 0) SetSelectedCategory(0); else SetSelectedCategory(_SelectionNr); } } _PreviewNr = _SelectionNr; _AfterCategoryChange(); } public override bool HandleInput(ref SKeyEvent keyEvent, SScreenSongOptions options) { if (keyEvent.KeyPressed) return false; bool moveAllowed = !options.Selection.RandomOnly || (options.Selection.CategoryChangeAllowed && !CBase.Songs.IsInCategory()); bool catChangePossible = CBase.Songs.GetTabs() == EOffOn.TR_CONFIG_ON && options.Selection.CategoryChangeAllowed; //If nothing selected set a reasonable default value if (keyEvent.IsArrowKey() && moveAllowed && _SelectionNr < 0) _SelectionNr = (_PreviewNr < 0) ? _Offset : _PreviewNr; switch (keyEvent.Key) { case Keys.Enter: if (CBase.Songs.IsInCategory()) { if (_SelectionNr >= 0 && _PreviewNr != _SelectionNr) { _PreviewNr = _SelectionNr; keyEvent.Handled = true; } } else { _EnterCategory(_PreviewNr); keyEvent.Handled = true; } break; case Keys.Escape: case Keys.Back: if (CBase.Songs.IsInCategory() && catChangePossible) { _LeaveCategory(); keyEvent.Handled = true; } break; case Keys.PageUp: if (catChangePossible) { _PrevCategory(); keyEvent.Handled = true; } break; case Keys.PageDown: if (catChangePossible) { _NextCategory(); keyEvent.Handled = true; } break; case Keys.Left: //Check for >0 so we do not allow selection of nothing (-1) if (_SelectionNr > 0 && moveAllowed) { _SelectionNr--; keyEvent.Handled = true; } break; case Keys.Right: if (moveAllowed) { _SelectionNr++; keyEvent.Handled = true; } break; case Keys.Up: if (keyEvent.ModShift) { if (catChangePossible) { _PrevCategory(); keyEvent.Handled = true; } } else if (_SelectionNr >= _NumW && moveAllowed) { _SelectionNr -= _NumW; keyEvent.Handled = true; } break; case Keys.Down: if (keyEvent.ModShift) { if (catChangePossible) { _NextCategory(); keyEvent.Handled = true; } } else if (moveAllowed) { _SelectionNr += _NumW; keyEvent.Handled = true; } break; } if (!CBase.Songs.IsInCategory()) _PreviewNr = _SelectionNr; return keyEvent.Handled; } public override bool HandleMouse(ref SMouseEvent mouseEvent, SScreenSongOptions songOptions) { if (!songOptions.Selection.RandomOnly || (!CBase.Songs.IsInCategory() && songOptions.Selection.CategoryChangeAllowed)) { if (mouseEvent.Wheel != 0 && CHelper.IsInBounds(_ScrollRect, mouseEvent)) _UpdateList(_Offset + _NumW * mouseEvent.Wheel); int lastSelection = _SelectionNr; int i = 0; foreach (CStatic tile in _Tiles) { if (tile.Texture != _CoverBGTexture && CHelper.IsInBounds(tile.Rect, mouseEvent)) { _SelectionNr = i + _Offset; if (!CBase.Songs.IsInCategory()) _PreviewNr = i + _Offset; break; } i++; } if (mouseEvent.Sender == ESender.WiiMote && _SelectionNr != lastSelection && _SelectionNr != -1) CBase.Controller.SetRumble(0.050f); } if (mouseEvent.RB) { if (CBase.Songs.IsInCategory() && CBase.Songs.GetNumCategories() > 0 && CBase.Songs.GetTabs() == EOffOn.TR_CONFIG_ON && songOptions.Selection.CategoryChangeAllowed) { _LeaveCategory(); return true; } if (CBase.Songs.GetTabs() == EOffOn.TR_CONFIG_OFF && !songOptions.Selection.PartyMode) { CBase.Graphics.FadeTo(EScreens.ScreenMain); return true; } } else if (mouseEvent.LB) { if (_SelectionNr >= 0) { if (CBase.Songs.IsInCategory()) { if (_PreviewNr == _SelectionNr) return false; _PreviewNr = _SelectionNr; } else EnterSelectedCategory(); return true; } } return false; } public override void Draw() { foreach (CStatic tile in _Tiles) { if (tile.Selected && Active) tile.Draw(EAspect.Crop, SelectedTileZoomFactor, -0.1f); else { EAspect aspect = (tile.Texture != _CoverBGTexture) ? EAspect.Crop : EAspect.Stretch; tile.Draw(aspect); } } _TextBG.Draw(); if (_Vidtex != null && _PreviewVideoStream != -1) { if (_Vidtex.Color.A < 1) _CoverBig.Draw(EAspect.Crop); var bounds = new RectangleF(_CoverBig.Rect.X, _CoverBig.Rect.Y, _CoverBig.Rect.W, _CoverBig.Rect.H); RectangleF rect; CHelper.SetRect(bounds, out rect, _Vidtex.OrigAspect, EAspect.Crop); var vidRect = new SRectF(rect.X, rect.Y, rect.Width, rect.Height, _CoverBig.Rect.Z); var vidRectBounds = new SRectF(bounds.X, bounds.Y, bounds.Width, bounds.Height, 0f); CBase.Drawing.DrawTexture(_Vidtex, vidRect, _Vidtex.Color, vidRectBounds); CBase.Drawing.DrawTextureReflection(_Vidtex, vidRect, _Vidtex.Color, vidRectBounds, _CoverBig.ReflectionSpace, _CoverBig.ReflectionHeight); } else _CoverBig.Draw(EAspect.Crop); _Artist.Draw(); _Title.Draw(); _SongLength.Draw(); _DuetIcon.Draw(); _VideoIcon.Draw(); _MedleyCalcIcon.Draw(); _MedleyTagIcon.Draw(); } public override CStatic GetSelectedSongCover() { return _Tiles.FirstOrDefault(tile => tile.Selected); } protected override void _EnterCategory(int categoryNr) { base._EnterCategory(categoryNr); _PreviewNr = 0; SetSelectedSong(0); _AfterCategoryChange(); } protected override void _LeaveCategory() { base._LeaveCategory(); _PreviewNr = 0; SetSelectedCategory(0); _AfterCategoryChange(); } private void _NextCategory() { if (CBase.Songs.IsInCategory()) { CBase.Songs.NextCategory(); _EnterCategory(CBase.Songs.GetCurrentCategoryIndex()); } } private void _PrevCategory() { if (CBase.Songs.IsInCategory()) { CBase.Songs.PrevCategory(); _EnterCategory(CBase.Songs.GetCurrentCategoryIndex()); } } private void _AfterCategoryChange() { if ((_LastKnownNumSongs == CBase.Songs.GetNumSongsVisible()) && (_LastKnownCategory == CBase.Songs.GetCurrentCategoryIndex())) return; _LastKnownCategory = CBase.Songs.GetCurrentCategoryIndex(); _LastKnownNumSongs = CBase.Songs.GetNumSongsVisible(); CBase.Songs.UpdateRandomSongList(); _UpdateList(true); } private int _GetOffsetForPosition(int index, int rowNum = 0) { return (index / _NumW) * _NumW - (_NumW * rowNum.Clamp(0, _NumH - 1)); } private void _UpdateList(bool force = false) { int offset; if (_SelectionNr < _Offset && _SelectionNr >= 0) offset = _GetOffsetForPosition(_SelectionNr); else if (_SelectionNr >= _Offset + _NumW * _NumH) offset = _GetOffsetForPosition(_SelectionNr, _NumH); else offset = _Offset; _UpdateList(offset, force); } private void _UpdateList(int offset, bool force = false) { bool isInCategory = CBase.Songs.IsInCategory(); int itemCount = isInCategory ? CBase.Songs.GetNumSongsVisible() : CBase.Songs.GetNumCategories(); offset = offset.Clamp(0, _GetOffsetForPosition(itemCount, _NumH - 1)); if (offset == _Offset && !force) return; for (int i = 0; i < _Tiles.Count; i++) { if (itemCount > i + offset) { _Tiles[i].Texture = isInCategory ? CBase.Songs.GetVisibleSong(i + offset).CoverTextureSmall : CBase.Songs.GetCategory(i + offset).CoverTextureSmall; _Tiles[i].Color = new SColorF(1f, 1f, 1f, 1f); } else { _Tiles[i].Texture = _CoverBGTexture; _Tiles[i].Color = _Color; } } _Offset = offset; } public override void LoadTextures() { base.LoadTextures(); _Theme.SongMenuTileBoard.StaticCoverBig.ReloadTextures(); _Theme.SongMenuTileBoard.StaticTextBG.ReloadTextures(); _Theme.SongMenuTileBoard.StaticDuetIcon.ReloadTextures(); _Theme.SongMenuTileBoard.StaticVideoIcon.ReloadTextures(); _Theme.SongMenuTileBoard.StaticMedleyCalcIcon.ReloadTextures(); _Theme.SongMenuTileBoard.StaticMedleyTagIcon.ReloadTextures(); _Theme.SongMenuTileBoard.TextArtist.ReloadTextures(); _Theme.SongMenuTileBoard.TextTitle.ReloadTextures(); _Theme.SongMenuTileBoard.TextSongLength.ReloadTextures(); } } }