Polyphase Game Engine
Loading...
Searching...
No Matches
GamePreview.h
Go to the documentation of this file.
1#pragma once
2
3#if EDITOR
4
5#include <cstdint>
6#include <string>
7#include <vector>
8#include "imgui.h"
9#include "EngineTypes.h"
10
11class Image;
12class Buffer;
13class Camera3D;
14
15struct ResolutionPreset
16{
17 std::string mName;
18 uint32_t mWidth;
19 uint32_t mHeight;
20};
21
22class GamePreview
23{
24public:
25 void Enable();
26 void Disable();
27 void Render();
28 void DrawPanel();
29
30 // Called after a project loads. Picks the preferred preset (Project Settings
31 // from EngineConfig, otherwise Build Target, otherwise built-in default) and
32 // rebuilds the render targets so the panel matches immediately.
33 void ApplyProjectDefaults();
34
35 bool IsEnabled() const { return mEnabled; }
36 uint32_t GetCurrentWidth() const { return mCurrentWidth; }
37 uint32_t GetCurrentHeight() const { return mCurrentHeight; }
38
39 // Selected preset resolution — persists across Disable() (unlike
40 // GetCurrentWidth/Height, which track live render targets and reset to
41 // 0 when the panel is closed). Returns the currently-selected preset,
42 // or the current build profile's target platform, or a 640x480 default.
43 uint32_t GetSelectedPresetWidth();
44 uint32_t GetSelectedPresetHeight();
45
46 // Capture the current color target into RGBA bytes (no disk write).
47 // Returns false if the preview is not currently rendered (no color target).
48 // Must be called from the main thread.
49 bool CaptureScreenshotToMemory(std::vector<uint8_t>& outRgba,
50 uint32_t& outWidth,
51 uint32_t& outHeight);
52
53 void BeginInputRemap();
54 void EndInputRemap();
55
56 // Hook support for addon resolution presets
57 void AddResolutionPreset(const std::string& name, uint32_t w, uint32_t h);
58 void RemoveResolutionPreset(const std::string& name);
59 const std::vector<ResolutionPreset>& GetAddonPresets() const { return mAddonPresets; }
60
61 // Returns the canonical preview resolution for a target platform.
62 // Used by Game Preview's "Target Platform" preset and the Canvas inspector's
63 // "Set Size To Build Profile Resolution" button.
64 static void GetPlatformResolution(Platform platform, uint32_t& outWidth, uint32_t& outHeight, const char*& outName);
65
66private:
67 bool mEnabled = true;
68 int32_t mSelectedPresetIndex = 0;
69 // -1 is a reserved sentinel meaning "Active Camera" (follow whichever
70 // camera is actually active, same as Viewport3D / fullscreen PIE),
71 // rather than a pinned index into mCachedCameras.
72 int32_t mSelectedCameraIndex = -1;
73 bool mShowGizmos = false;
74
75 // Render targets (recreated on resolution change)
76 Image* mColorTarget = nullptr;
77 Image* mDepthTarget = nullptr;
78 ImTextureID mImGuiTexId = 0;
79 uint32_t mCurrentWidth = 0;
80 uint32_t mCurrentHeight = 0;
81
82 // Screenshot
83 bool mScreenshotRequested = false;
84
85 // Built-in presets
86 static const std::vector<ResolutionPreset> sBuiltInPresets;
87
88 // Addon presets (populated by hooks)
89 std::vector<ResolutionPreset> mAddonPresets;
90
91 // Custom user presets (persisted to editor preferences)
92 std::vector<ResolutionPreset> mCustomPresets;
93 bool mCustomPresetsLoaded = false;
94
95 // UI state for add-preset popup
96 char mNewPresetName[128] = {};
97 int32_t mNewPresetWidth = 1280;
98 int32_t mNewPresetHeight = 720;
99
100 // Cached camera list
101 std::vector<Camera3D*> mCachedCameras;
102
103 // Image display rect in window coords (captured by DrawPanel, used next frame)
104 ImVec2 mImageMin = {0, 0};
105 ImVec2 mImageMax = {0, 0};
106
107 // Saved state for input remap
108 int32_t mSavedMouseX = 0;
109 int32_t mSavedMouseY = 0;
110 uint32_t mSavedVpX = 0;
111 uint32_t mSavedVpY = 0;
112 uint32_t mSavedVpW = 0;
113 uint32_t mSavedVpH = 0;
114 bool mInputRemapActive = false;
115
116 void CreateRenderTargets(uint32_t w, uint32_t h);
117 void DestroyRenderTargets();
118 void RefreshCameraList();
119 void CaptureScreenshot();
120 void LoadCustomPresets();
121 void SaveCustomPresets();
122 ResolutionPreset GetCurrentPreset();
123 std::vector<ResolutionPreset> GetAllPresets();
124};
125
126GamePreview* GetGamePreview();
127
128#endif
Platform
Definition EngineTypes.h:32
Definition Camera3d.h:12
void Enable(bool enable)
Definition ImGuizmo.cpp:1055