class ViewportPannel : DockPanel {
    FrameBuffer frameBuffer;
    SceneCamera sceneCamera;
    Environment mainEnv;

    float pitch = 0;
    float yaw = 0;
    float vel = 0.02f;

    void onRender() {
        if (!frameBuffer.isValid())
            return;
            
        int x = UI::getAvailableSizeX();
        int y = UI::getAvailableSizeY();
        frameBuffer.resize(x, y);
        frameBuffer.clearRGBA(0, 0, 0, 255);

        sceneCamera.camera.aspect = float(x) / y;
        mainEnv.render(frameBuffer, sceneCamera);

        UI::drawFrameBufferCentered(frameBuffer, x, y);

        if (!UI::isPannelActive())
            return;

        if (UI::isMouseDraggin(key::MouseRight) && !UI::isKeyDown(key::MouseMiddle)) {
            pitch += UI::getMouseDeltaY() * 0.1f;
            yaw += UI::getMouseDeltaX() * 0.1f;

            sceneCamera.transform.rotation.setEuler(vec3(pitch, yaw, 0));
        }

        if (UI::isMouseDraggin(key::MouseMiddle) && !UI::isKeyDown(key::MouseRight)) {
            vec3 panDir = vec3();

            panDir.x -= UI::getMouseDeltaX();
            panDir.y += UI::getMouseDeltaY();

            panDir = panDir * vel * 0.4f;
            sceneCamera.transform.position = sceneCamera.transform.relative(panDir);
        }

        vec3 relDir = vec3();

        if (UI::isKeyDown(key::W))
            relDir.z++;
        if (UI::isKeyDown(key::S))
            relDir.z--;

        if (UI::isKeyDown(key::D))
            relDir.x++;
        if (UI::isKeyDown(key::A))
            relDir.x--;

        relDir = relDir * vel;
        sceneCamera.transform.position = sceneCamera.transform.relative(relDir);

        float vertically = 0;
        if (UI::isKeyDown(key::Space))
            vertically++;
        if (UI::isKeyDown(key::LeftCtrl))
            vertically--;

        sceneCamera.transform.position = sceneCamera.transform.position + vec3(0, vertically * vel, 0);
    }

    void onInit() {
        frameBuffer = Engine::createRGBA8FrameBuffer("MainFrameBuffer", 400, 400);
        mainEnv = Engine::getMainEnvironment();

        sceneCamera.transform.position = vec3(0, 1, -2);
        UI::disablePannelPadding(true);
    }

    void onMenuBar() {
        if (UI::menuItem("Start")) {

        }

        if (UI::menuItem("Camera Props")) {
            UI::openPopup("ViewportCameraProps", any());
        }

        UI::simplePopup("ViewportCameraProps", ReciverFunc(this.viewportCameraProps));
    }

    void viewportCameraProps(any@ data) {
        sceneCamera.camera.fov = UI::slider("Fov", sceneCamera.camera.fov / 3.14f * 180, 10, 160) / 180 * 3.14f;
    }
}
                                                