private void OnPlayerStateUpdate(CurrentlyPlayingContext currentlyPlaying, FullTrack playbackItem)
        {
            if (playbackItem.Id != _lastId)
            {
                _plugin.DisplayMessage($"Playing '{playbackItem.Name}'...");
            }
            _lastId = playbackItem.Id;


            var playerStateStruct = PlayerState;

            playerStateStruct.ProgressMs   = currentlyPlaying.ProgressMs;
            playerStateStruct.IsPlaying    = currentlyPlaying.IsPlaying;
            playerStateStruct.RepeatState  = currentlyPlaying.RepeatState;
            playerStateStruct.ShuffleState = currentlyPlaying.ShuffleState;

            playerStateStruct.CurrentlyPlaying = new TrackStruct
            {
                Id         = playbackItem.Id,
                Name       = playbackItem.Name,
                Artists    = playbackItem.Artists.Select(artist => artist.Name).ToArray(),
                DurationMs = playbackItem.DurationMs,
                Album      = new AlbumStruct
                {
                    Name = playbackItem.Album.Name
                }
            };

            PlayerState = playerStateStruct;
        }
        public void StartAuth()
        {
            _loginThread = new Thread(_spotifyState.StartAuth);
            _loginThread.Start();

            var playerStateStruct = PlayerState;

            playerStateStruct.IsAuthenticating = true;
            PlayerState = playerStateStruct;
        }
Exemple #3
0
        private void DebugWindow(PlayerStateStruct currentPlayerState)
        {
            if (!ImGui.Begin("Fantasy Player: Debug Window"))
            {
                return;
            }

            if (ImGui.Button("Reload providers"))
            {
                _playerManager.ReloadProviders();
            }

            foreach (var provider in _playerManager.PlayerProviders
                     .Where(provider => provider.Value.PlayerState.ServiceName != null))
            {
                var playerState  = provider.Value.PlayerState;
                var providerText = playerState.ServiceName;

                if (playerState.ServiceName == currentPlayerState.ServiceName)
                {
                    providerText += " (Current)";
                }

                if (!ImGui.CollapsingHeader(providerText))
                {
                    continue;
                }
                ImGui.Text("RequiresLogin: "******"IsLoggedIn: " + playerState.IsLoggedIn);
                ImGui.Text("IsAuthenticating: " + playerState.IsAuthenticating);
                ImGui.Text("RepeatState: " + playerState.RepeatState);
                ImGui.Text("ShuffleState: " + playerState.ShuffleState);
                ImGui.Text("IsPlaying: " + playerState.IsPlaying);
                ImGui.Text("ProgressMs: " + playerState.ProgressMs);

                if (ImGui.CollapsingHeader(providerText + ": CurrentlyPlaying"))
                {
                    RenderTrackStructDebug(playerState.CurrentlyPlaying);
                }

                if (playerState.ServiceName == currentPlayerState.ServiceName)
                {
                    continue;
                }
                if (ImGui.Button($"Set {playerState.ServiceName} as current provider"))
                {
                    _playerManager.CurrentPlayerProvider = provider.Value;
                }
            }

            ImGui.End();
        }
        private void OnLoggedIn(PrivateUser privateUser, PKCETokenResponse tokenResponse)
        {
            var playerStateStruct = PlayerState;

            playerStateStruct.IsLoggedIn = true;
            PlayerState = playerStateStruct;

            _plugin.Configuration.SpotifySettings.TokenResponse = tokenResponse;

            if (_spotifyState.IsPremiumUser)
            {
                _plugin.Configuration.SpotifySettings.LimitedAccess = false;
            }

            if (!_spotifyState.IsPremiumUser)
            {
                if (!_plugin.Configuration.SpotifySettings.LimitedAccess
                    ) //Do a check to not spam the user, I don't want to force it down their throats. (f**k marketing)
                {
                    _plugin.PluginInterface.Framework.Gui.Chat.PrintError(
                        "Uh-oh, it looks like you're not premium on Spotify. Some features in Fantasy Player have been disabled.");
                }

                _plugin.Configuration.SpotifySettings.LimitedAccess = true;

                //Change configs
                if (_plugin.Configuration.PlayerSettings.CompactPlayer)
                {
                    _plugin.Configuration.PlayerSettings.CompactPlayer = false;
                }
                if (!_plugin.Configuration.PlayerSettings.NoButtons)
                {
                    _plugin.Configuration.PlayerSettings.NoButtons = true;
                }
            }

            _plugin.Configuration.Save();
        }
        public void Initialize(Plugin plugin)
        {
            _plugin     = plugin;
            PlayerState = new PlayerStateStruct
            {
                ServiceName   = "Spotify",
                RequiresLogin = true
            };

            _spotifyState = new SpotifyState(_plugin.RemoteConfigManager.Config.SpotifyLoginUri, _plugin.RemoteConfigManager.Config.SpotifyClientId,
                                             _plugin.RemoteConfigManager.Config.SpotifyLoginPort, _plugin.RemoteConfigManager.Config.SpotifyPlayerRefreshTime);

            _spotifyState.OnLoggedIn          += OnLoggedIn;
            _spotifyState.OnPlayerStateUpdate += OnPlayerStateUpdate;

            if (_plugin.Configuration.SpotifySettings.TokenResponse == null)
            {
                return;
            }
            _spotifyState.TokenResponse = _plugin.Configuration.SpotifySettings.TokenResponse;
            _spotifyState.RequestToken();
            _startThread = new Thread(_spotifyState.Start);
            _startThread.Start();
        }
Exemple #6
0
        private void MainWindow(PlayerStateStruct playerState, IPlayerProvider currentProvider)
        {
            ImGui.SetNextWindowBgAlpha(_plugin.Configuration.PlayerSettings.Transparency);
            SetDefaultWindowSize(_plugin.Configuration.PlayerSettings);


            var lockFlags = (_plugin.Configuration.PlayerSettings.PlayerLocked)
                ? ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoResize
                : ImGuiWindowFlags.None;

            var clickThroughFlags = (_plugin.Configuration.PlayerSettings.DisableInput)
                ? ImGuiWindowFlags.NoMouseInputs | ImGuiWindowFlags.NoResize
                : ImGuiWindowFlags.None;

            var playerSettings = _plugin.Configuration.PlayerSettings;

            if (!ImGui.Begin($"Fantasy Player##C{playerSettings.CompactPlayer}&N{playerSettings.NoButtons}",
                             ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.NoScrollbar | lockFlags |
                             clickThroughFlags))
            {
                return;
            }

            //Disable FirstRun
            if (_plugin.Configuration.PlayerSettings.FirstRunNone)
            {
                _plugin.Configuration.PlayerSettings.FirstRunNone = false;
                _plugin.Configuration.Save();
            }

            //////////////// Right click popup ////////////////

            if (ImGui.BeginPopupContextWindow())
            {
                if (_playerManager.PlayerProviders.Count > 1)
                {
                    if (ImGui.BeginMenu("Switch provider"))
                    {
                        foreach (var provider in _playerManager.PlayerProviders)
                        {
                            if (provider.Value == _playerManager.CurrentPlayerProvider)
                            {
                                continue;
                            }
                            if (ImGui.MenuItem(provider.Key.Name.Replace("Provider", "")))
                            {
                                _playerManager.CurrentPlayerProvider = provider.Value;
                                _plugin.Configuration.PlayerSettings.DefaultProvider = provider.Key.FullName;
                                _plugin.Configuration.Save();
                            }
                        }
                        ImGui.EndMenu();
                    }

                    ImGui.Separator();
                }

                if (!_plugin.Configuration.SpotifySettings.LimitedAccess)
                {
                    if (ImGui.MenuItem("Compact mode", null, ref _plugin.Configuration.PlayerSettings.CompactPlayer))
                    {
                        if (_plugin.Configuration.PlayerSettings.NoButtons)
                        {
                            _plugin.Configuration.PlayerSettings.NoButtons = false;
                        }
                    }

                    if (ImGui.MenuItem("Hide Buttons", null, ref _plugin.Configuration.PlayerSettings.NoButtons))
                    {
                        if (_plugin.Configuration.PlayerSettings.CompactPlayer)
                        {
                            _plugin.Configuration.PlayerSettings.CompactPlayer = false;
                        }
                    }

                    ImGui.Separator();
                }

                ImGui.MenuItem("Lock player", null, ref _plugin.Configuration.PlayerSettings.PlayerLocked);
                ImGui.MenuItem("Show player", null, ref _plugin.Configuration.PlayerSettings.PlayerWindowShown);
                ImGui.MenuItem("Show config", null, ref _plugin.Configuration.ConfigShown);

                ImGui.EndPopup();
            }

            //////////////// Window Basics ////////////////

            if (playerState.CurrentlyPlaying.Id == null)
            {
                InterfaceUtils.TextCentered($"Nothing is playing on {playerState.ServiceName}.");
                return;
            }

            {
                //////////////// Window Setup ////////////////

                ImGui.PushStyleColor(ImGuiCol.Button, InterfaceUtils.TransparentColor);
                ImGui.PushStyleColor(ImGuiCol.ButtonActive, InterfaceUtils.TransparentColor);
                ImGui.PushStyleColor(ImGuiCol.ButtonHovered, InterfaceUtils.DarkenButtonColor);

                var track = playerState.CurrentlyPlaying;

                if (playerState.IsPlaying)
                {
                    _progressDelta += ImGui.GetIO().DeltaTime;
                }

                if (_progressMs != playerState.ProgressMs)
                {
                    _progressDelta = 0;
                }
                _progressMs = playerState.ProgressMs;

                var percent = playerState.ProgressMs * 100f / track.DurationMs +
                              (_progressDelta / (track.DurationMs / 100000f)); //me good maths

                _progressMs = playerState.ProgressMs;

                var artists = track.Artists.Aggregate("", (current, artist) => current + (artist + ", "));

                if (!_plugin.Configuration.PlayerSettings.NoButtons)
                {
                    //////////////// Play and Pause ////////////////

                    var stateIcon = (playerState.IsPlaying)
                        ? FontAwesomeIcon.Pause.ToIconString()
                        : FontAwesomeIcon.Play.ToIconString();

                    ImGui.PushFont(UiBuilder.IconFont);

                    if (ImGui.Button(FontAwesomeIcon.Backward.ToIconString()))
                    {
                        currentProvider.SetSkip(false);
                    }

                    if (InterfaceUtils.ButtonCentered(stateIcon))
                    {
                        currentProvider.SetPauseOrPlay(!playerState.IsPlaying);
                    }

                    //////////////// Shuffle and Repeat ////////////////

                    ImGui.SameLine(ImGui.GetWindowSize().X / 2 +
                                   (ImGui.GetFontSize() + ImGui.CalcTextSize(FontAwesomeIcon.Random.ToIconString()).X));

                    if (playerState.ShuffleState)
                    {
                        ImGui.PushStyleColor(ImGuiCol.Text, _plugin.Configuration.PlayerSettings.AccentColor);
                    }

                    if (ImGui.Button(FontAwesomeIcon.Random.ToIconString()))
                    {
                        currentProvider.SetShuffle(!playerState.ShuffleState);
                    }

                    if (playerState.ShuffleState)
                    {
                        ImGui.PopStyleColor();
                    }

                    if (playerState.RepeatState != "off")
                    {
                        ImGui.PushStyleColor(ImGuiCol.Text, _plugin.Configuration.PlayerSettings.AccentColor);
                    }

                    var buttonIcon = FontAwesomeIcon.Retweet.ToIconString();

                    if (playerState.RepeatState == "track")
                    {
                        buttonIcon = FontAwesomeIcon.Music.ToIconString();
                    }

                    ImGui.SameLine(ImGui.GetWindowSize().X / 2 -
                                   (ImGui.GetFontSize() + ImGui.CalcTextSize(buttonIcon).X +
                                    ImGui.CalcTextSize(FontAwesomeIcon.Random.ToIconString()).X));

                    if (ImGui.Button(buttonIcon))
                    {
                        currentProvider.SwapRepeatState();
                    }

                    if (playerState.RepeatState != "off")
                    {
                        ImGui.PopStyleColor();
                    }

                    ImGui.SameLine(ImGui.GetWindowSize().X -
                                   (ImGui.GetFontSize() +
                                    ImGui.CalcTextSize(FontAwesomeIcon.Forward.ToIconString()).X));
                    if (ImGui.Button(FontAwesomeIcon.Forward.ToIconString()))
                    {
                        currentProvider.SetSkip(true);
                    }

                    ImGui.PopFont();
                }

                if (!_plugin.Configuration.PlayerSettings.CompactPlayer)
                {
                    //////////////// Progress Bar ////////////////

                    ImGui.PushStyleColor(ImGuiCol.PlotHistogram, _plugin.Configuration.PlayerSettings.AccentColor);
                    ImGui.ProgressBar(percent / 100f, new Vector2(-1, 2f));
                    ImGui.PopStyleColor();

                    Vector2 imageSize = new Vector2(100 * ImGui.GetIO().FontGlobalScale,
                                                    100 * ImGui.GetIO().FontGlobalScale);

                    //////////////// Text ////////////////

                    InterfaceUtils.TextCentered(track.Name);

                    ImGui.PushStyleColor(ImGuiCol.Text, InterfaceUtils.DarkenColor);

                    ImGui.Spacing();
                    InterfaceUtils.TextCentered(artists.Remove(artists.Length - 2));


                    ImGui.PopStyleColor();
                }

                ImGui.PopStyleColor(3);
            }

            ImGui.End();
        }