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 std::string GetSelectedPresetName();
46
47 // Capture the current color target into RGBA bytes (no disk write).
48 // Returns false if the preview is not currently rendered (no color target).
49 // Must be called from the main thread.
50 bool CaptureScreenshotToMemory(std::vector<uint8_t>& outRgba,
51 uint32_t& outWidth,
52 uint32_t& outHeight);
53
54 void BeginInputRemap();
55 void EndInputRemap();
56
57 // Hook support for addon resolution presets
58 void AddResolutionPreset(const std::string& name, uint32_t w, uint32_t h);
59 void RemoveResolutionPreset(const std::string& name);
60 const std::vector<ResolutionPreset>& GetAddonPresets() const { return mAddonPresets; }
61
62 // Returns the canonical preview resolution for a target platform.
63 // Used by Game Preview's "Target Platform" preset and the Canvas inspector's
64 // "Set Size To Build Profile Resolution" button.
65 static void GetPlatformResolution(Platform platform, uint32_t& outWidth, uint32_t& outHeight, const char*& outName);
66
67private:
68 bool mEnabled = true;
69 int32_t mSelectedPresetIndex = 0;
70 // -1 is a reserved sentinel meaning "Active Camera" (follow whichever
71 // camera is actually active, same as Viewport3D / fullscreen PIE),
72 // rather than a pinned index into mCachedCameras.
73 int32_t mSelectedCameraIndex = -1;
74 bool mShowGizmos = false;
75
76 // Render targets (recreated on resolution change)
77 Image* mColorTarget = nullptr;
78 Image* mDepthTarget = nullptr;
79 ImTextureID mImGuiTexId = 0;
80 uint32_t mCurrentWidth = 0;
81 uint32_t mCurrentHeight = 0;
82
83 // Screenshot
84 bool mScreenshotRequested = false;
85
86 // Built-in presets
87 static const std::vector<ResolutionPreset> sBuiltInPresets;
88
89 // Addon presets (populated by hooks)
90 std::vector<ResolutionPreset> mAddonPresets;
91
92 // Custom user presets (persisted to editor preferences)
93 std::vector<ResolutionPreset> mCustomPresets;
94 bool mCustomPresetsLoaded = false;
95
96 // UI state for add-preset popup
97 char mNewPresetName[128] = {};
98 int32_t mNewPresetWidth = 1280;
99 int32_t mNewPresetHeight = 720;
100
101 // Cached camera list
102 std::vector<Camera3D*> mCachedCameras;
103
104 // Image display rect in window coords (captured by DrawPanel, used next frame)
105 ImVec2 mImageMin = {0, 0};
106 ImVec2 mImageMax = {0, 0};
107
108 // Saved state for input remap
109 int32_t mSavedMouseX = 0;
110 int32_t mSavedMouseY = 0;
111 uint32_t mSavedVpX = 0;
112 uint32_t mSavedVpY = 0;
113 uint32_t mSavedVpW = 0;
114 uint32_t mSavedVpH = 0;
115 bool mInputRemapActive = false;
116
117 void CreateRenderTargets(uint32_t w, uint32_t h);
118 void DestroyRenderTargets();
119 void RefreshCameraList();
120 void CaptureScreenshot();
121 void LoadCustomPresets();
122 void SaveCustomPresets();
123 ResolutionPreset GetCurrentPreset();
124 std::vector<ResolutionPreset> GetAllPresets();
125};
126
127GamePreview* GetGamePreview();
128
129#endif
Platform
Definition EngineTypes.h:32
Definition Camera3d.h:12
void Enable(bool enable)
Definition ImGuizmo.cpp:1099