Polyphase Game Engine
Loading...
Searching...
No Matches
TextureCropEditor.h
Go to the documentation of this file.
1#pragma once
2
3#if EDITOR
4
5#include <cstdint>
6#include <functional>
7#include "imgui.h"
8#include "glm/glm.hpp"
9
10class Texture;
11
12class TextureCropEditor
13{
14public:
15 ~TextureCropEditor();
16
17 void Open(Texture* texture, glm::vec2 currentUVScale, glm::vec2 currentUVOffset,
18 std::function<void(glm::vec2 uvScale, glm::vec2 uvOffset)> onApply);
19
20 void DrawPopup();
21
22 bool IsOpen() const { return mIsOpen; }
23
24private:
25 enum class DragMode
26 {
27 None,
28 Move,
29 ResizeTL,
30 ResizeTR,
31 ResizeBL,
32 ResizeBR,
33 ResizeT,
34 ResizeB,
35 ResizeL,
36 ResizeR
37 };
38
39 void SetTexture(Texture* tex);
40 void ClearTexture();
41 void DrawCropCanvas();
42 void HandleCropInteraction(ImVec2 canvasPos, float drawW, float drawH, bool canvasHovered, bool canvasActive);
43
44 // Convert between crop rect and UV scale/offset
45 // Quad formula: finalUV = (baseUV + offset) * scale
46 glm::vec2 CalcUVScale() const;
47 glm::vec2 CalcUVOffset() const;
48 void SetFromUV(glm::vec2 uvScale, glm::vec2 uvOffset);
49
50 // Texture state
51 Texture* mTexture = nullptr;
52 ImTextureID mImGuiTexId = 0;
53 uint32_t mTexWidth = 0;
54 uint32_t mTexHeight = 0;
55
56 // Crop rectangle in normalized [0,1] texture coords
57 glm::vec2 mCropMin = glm::vec2(0.0f);
58 glm::vec2 mCropMax = glm::vec2(1.0f);
59
60 // Interaction state
61 DragMode mDragMode = DragMode::None;
62 glm::vec2 mDragStartMouse = glm::vec2(0.0f);
63 glm::vec2 mDragStartCropMin = glm::vec2(0.0f);
64 glm::vec2 mDragStartCropMax = glm::vec2(0.0f);
65
66 // Display
67 float mZoom = 1.0f;
68 uint64_t mLastDrawFrame = 0;
69
70 // Callback
71 std::function<void(glm::vec2, glm::vec2)> mOnApply;
72 bool mIsOpen = false;
73 bool mJustOpened = false;
74};
75
76TextureCropEditor* GetTextureCropEditor();
77
78#endif
Definition Texture.h:10