Esempio n. 1
0
        private void HandleFunctionKeys()
        {
            InputHandler.HandleKeypress(loadRomKey, () =>
            {
                if (!fileDialogVisible && !helpMenuVisible)
                {
                    fileDialog = new FileDialog(FileDialogMode.OpenFile)
                    {
                        Filter = "*.ch8"
                    };

                    fileDialog.Closed += (s, a) =>
                    {
                        isPaused          = false;
                        fileDialogVisible = false;
                        if (!fileDialog.Result)
                        {
                            return;
                        }

                        currentRomPath = fileDialog.FilePath;

                        cpu.Reset();

                        currentRom               = File.ReadAllBytes(currentRomPath);
                        currentRomName           = Path.GetFileNameWithoutExtension(currentRomPath);
                        SavestateManager.romName = currentRomName;
                        cpu.LoadProgram(currentRom);
                        RewindManager.Initialize(rewindBufferSize);

                        isRunning = true;
                        RemoveAllMessages();
                    };
                    isPaused = true;

                    fileDialog.ShowModal();
                    fileDialogVisible = true;
                }
                else if (fileDialogVisible)
                {
                    fileDialog.Close();
                    fileDialogVisible = false;
                }
            });

            InputHandler.HandleKeypress(resetKey, () =>
            {
                if (isRunning && !helpMenuVisible && !fileDialogVisible)
                {
                    cpu.Reset();
                    cpu.LoadProgram(currentRom);
                    ShowMessage("Reset", 2.5f);
                }
            });

            InputHandler.HandleKeypress(nextSaveStateSlotKey, () =>
            {
                if (isRunning && !helpMenuVisible && !fileDialogVisible)
                {
                    SavestateManager.SelectNextSlot();
                    ShowMessage($"Selected slot {SavestateManager.selectedSlot} {(SavestateManager.IsSelectedSlotEmpty() ? "(empty)" : "")}", 2.5f);
                }
            });
            InputHandler.HandleKeypress(previousSaveStateSlotKey, () =>
            {
                if (isRunning && !helpMenuVisible && !fileDialogVisible)
                {
                    SavestateManager.SelectPreviousSlot();
                    ShowMessage($"Selected slot {SavestateManager.selectedSlot} {(SavestateManager.IsSelectedSlotEmpty() ? "(empty)" : "")}", 2.5f);
                }
            });

            InputHandler.HandleKeypress(helpKey, () =>
            {
                if (!helpMenuVisible && !fileDialogVisible)
                {
                    helpMenu = new Window
                    {
                        Title = "Welcome to Crispy!"
                    };
                    Label text = new Label
                    {
                        Text = readme,
                        VerticalAlignment = VerticalAlignment.Stretch,
                        Wrap = true
                    };
                    ScrollViewer scrollViewer = new ScrollViewer
                    {
                        Content = text
                    };

                    helpMenu.Content = scrollViewer;

                    helpMenu.Closed += (s, a) =>
                    {
                        helpMenuVisible = false;
                    };

                    helpMenu.ShowModal();
                    helpMenuVisible = true;
                }
                else if (helpMenuVisible)
                {
                    helpMenu.Close();
                    helpMenuVisible = false;
                }
            });

            InputHandler.HandleKeypress(screenshotKey, () =>
            {
                if (!helpMenuVisible && !fileDialogVisible)
                {
                    string screenshotPath = $"Screenshots/{DateTime.Now.ToString().Replace("/", "-").Replace(":", "-")}.jpg";

                    if (!File.Exists(screenshotPath))
                    {
                        try
                        {
                            File.Create(screenshotPath).Dispose();
                        }
                        catch (DirectoryNotFoundException)
                        {
                            Directory.CreateDirectory(Path.GetDirectoryName(screenshotPath));
                        }
                    }

                    using (Texture2D screenshot = TakeScreenshot())
                        SaveScreenshot(screenshot, File.OpenWrite(screenshotPath));

                    ShowMessage($"Saved screenshot as {Path.GetFileName(screenshotPath)}", 2.5f);
                }
            });
        }