Fixes and improvements to the graphics module

This commit is contained in:
IlyaShurupov 2024-07-17 09:40:49 +03:00
parent 2fb319840c
commit e3757770c8
19 changed files with 161 additions and 55 deletions

View file

@ -19,7 +19,7 @@ public:
~EditorGUI() override { delete mGui; }
void processFrame(EventHandler* eventHandler) override {
void processFrame(EventHandler* eventHandler, halnf delta) override {
auto rec = RectF({ 0, 0 }, mWindow->getSize());
mGui->setArea(rec);

View file

@ -11,7 +11,7 @@ class ExampleApplication : public Application {
public:
ExampleApplication() = default;
void processFrame(EventHandler* eventHandler) override {
void processFrame(EventHandler* eventHandler, halnf) override {
// example
}
@ -20,10 +20,11 @@ public:
ImGui::ShowDemoWindow();
ImGui::Text("Frames processed per second: %f", this->mFramesProcessedPerSecond);
ImGui::Text("Frames drawn per second: %f", this->mFramesDrawnPerSecond);
drawDebug();
}
bool forceNewFrame() override { return false; }
virtual ~ExampleApplication() = default;
};

View file

@ -37,7 +37,9 @@ Canvas::~Canvas() {
delete mContext;
}
void Canvas::rect(const RectF& rec, const RGBA& col, halnf round) {
void Canvas::rect(RectF rec, const RGBA& col, halnf round) {
rec.pos += mOrigin;
nvgBeginPath(mContext->vg);
if (round == 0) {
@ -50,7 +52,9 @@ void Canvas::rect(const RectF& rec, const RGBA& col, halnf round) {
nvgFill(mContext->vg);
}
void Canvas::circle(const Vec2F& pos, halnf size, const RGBA& col) {
void Canvas::circle(Vec2F pos, halnf size, const RGBA& col) {
pos += mOrigin;
nvgBeginPath(mContext->vg);
nvgCircle(mContext->vg, pos.x, pos.y, size);
@ -58,6 +62,10 @@ void Canvas::circle(const Vec2F& pos, halnf size, const RGBA& col) {
nvgFill(mContext->vg);
}
void Canvas::setOrigin(const Vec2F& origin) {
mOrigin = origin;
}
void Canvas::pushClamp(const RectF& rec) {
RectF intersection = rec;
if (mScissors.size()) {
@ -82,7 +90,9 @@ void Canvas::popClamp() {
void Canvas::text(
const char* string, const RectF& aRec, halnf size, Align align, halnf marging, const RGBA& col
) {
RectF rec = { aRec.x + marging, aRec.y + marging, aRec.z - marging * 2, aRec.w - marging * 2 };
rec.pos += mOrigin;
pushClamp(rec);
@ -121,7 +131,9 @@ void Canvas::text(
popClamp();
}
void Canvas::drawImage(const RectF& rec, ImageHandle* image, halnf angle, halnf alpha, halnf rounding) {
void Canvas::drawImage(RectF rec, ImageHandle* image, halnf angle, halnf alpha, halnf rounding) {
rec.pos += mOrigin;
auto imgPaint = nvgImagePattern(mContext->vg, rec.x, rec.y, rec.z, rec.w, angle, image->id, alpha);
nvgBeginPath(mContext->vg);
nvgRoundedRect(mContext->vg, rec.x, rec.y, rec.z, rec.w, rounding);

View file

@ -42,6 +42,14 @@ bool transitionsReduce[4][4] = {
{ true, true, true, true },
};
void EventHandler::processAllEvent() {
mMutex.lock();
while (mEventQueue.size()) {
processEventUnguarded();
}
mMutex.unlock();
}
void EventHandler::processEvent() {
mMutex.lock();
@ -50,10 +58,17 @@ void EventHandler::processEvent() {
return;
}
auto lastEvent = &mEventQueue.last();
processEventUnguarded();
const auto& eventData = lastEvent->second;
const auto& inputId = lastEvent->first;
mMutex.unlock();
}
void EventHandler::processEventUnguarded() {
auto firstEvent = &mEventQueue.first();
const auto& eventData = firstEvent->second;
const auto& inputId = firstEvent->first;
switch (eventData.type) {
case InputEvent::Type::MOUSE_POS:
@ -71,8 +86,6 @@ void EventHandler::processEvent() {
mInputStates[(int) inputId].mCurrentState = transitions[currentState][reportedEvent];
// printf("%i - %i \n", reportedEvent, mInputStates[(int) InputID::MOUSE1].mCurrentState);
if (transitionsReduce[currentState][reportedEvent]) {
mEventQueue.popFront();
}
@ -98,13 +111,15 @@ void EventHandler::processEvent() {
}
mPointerPressure = mInputStates[(int) InputID::MOUSE1].mCurrentState != InputState::State::NONE;
mMutex.unlock();
}
const Vec2F& EventHandler::getPointer() const { return mPointer; }
void EventHandler::setCursorOrigin(const Vec2F& origin) {
mPointerOrigin = origin;
}
const Vec2F& EventHandler::getPointerPrev() const { return mPointerPrev; }
Vec2F EventHandler::getPointer() const { return mPointer - mPointerOrigin; }
Vec2F EventHandler::getPointerPrev() const { return mPointerPrev - mPointerOrigin; }
bool EventHandler::isPressed(InputID id) const {
return mInputStates[(int) id].mCurrentState == InputState::State::PRESSED;

View file

@ -1,6 +1,8 @@
#include "GraphicApplication.hpp"
#include "imgui.h"
using namespace tp;
Application::Application() {
@ -12,7 +14,7 @@ Application::Application() {
mPerSecondTimer.setDuration(1000.f);
}
void Application::run() {
void Application::runDefaultLoop() {
auto eventHandler = new EventHandler();
mWindow->setEventHandler(eventHandler);
@ -25,8 +27,8 @@ void Application::run() {
// proc first frame by default
{
mWindow->processEvents();
processFrame(eventHandler);
mWindow->processEvents(false);
processFrame(eventHandler, 0);
mGraphics->drawBegin();
drawFrame(mGraphics->getCanvas());
@ -34,17 +36,33 @@ void Application::run() {
mWindow->draw();
}
time_ms prevProcTime = get_time();
bool isForcedNewFrame = true;
while (!mWindow->shouldClose()) {
mWindow->processEvents();
mWindow->processEvents(!isForcedNewFrame);
updateGlobalTime();
if (mProcTimer.isTimeout() || eventHandler->isEvents()) {
if (mProcTimer.isTimeout() || eventHandler->isEvents() || isForcedNewFrame) {
while ((eventHandler->isEvents() || isForcedNewFrame)) {
while (eventHandler->isEvents()) {
eventHandler->processEvent();
processFrame(eventHandler);
time_ms currentTime = get_time();
processFrame(eventHandler, halnf(currentTime - prevProcTime));
prevProcTime = currentTime;
redrawNeeded = true;
mFramesProcessed++;
isForcedNewFrame = forceNewFrame();
if (isForcedNewFrame) {
// mWindow->processEvents();
break;
}
}
mProcTimer.wait();
@ -82,13 +100,46 @@ void Application::run() {
delete eventHandler;
}
void Application::processFrame(EventHandler* eventHandler) {}
void Application::runDebugLoop() {
auto eventHandler = new EventHandler();
mWindow->setEventHandler(eventHandler);
time_ms prevProcTime = get_time();
while (!mWindow->shouldClose()) {
mWindow->processEvents();
eventHandler->processAllEvent();
time_ms currentTime = get_time();
processFrame(eventHandler, halnf(currentTime - prevProcTime));
prevProcTime = currentTime;
mGraphics->drawBegin();
drawFrame(mGraphics->getCanvas());
mGraphics->drawEnd();
mWindow->draw();
}
delete eventHandler;
}
void Application::run() {
runDefaultLoop();
// runDebugLoop();
}
void Application::processFrame(EventHandler* eventHandler, halnf deltaTime) {}
void Application::drawFrame(Canvas* canvas) {
// ImGui::Text("Frames processed per second: %f", mFramesProcessedPerSecond);
// ImGui::Text("Frames drawn per second: %f", mFramesDrawnPerSecond);
}
void Application::drawDebug() {
ImGui::Text("Frames processed per second: %f", this->mFramesProcessedPerSecond);
ImGui::Text("Frames drawn per second: %f", this->mFramesDrawnPerSecond);
}
Application::~Application() {
delete mGraphics;
Window::destroyWindow(mWindow);

View file

@ -90,9 +90,9 @@ void Window::destroyWindow(Window* window) {
bool Window::shouldClose() const { return glfwWindowShouldClose(mContext->window); }
void Window::processEvents() {
// glfwPollEvents();
glfwWaitEvents();
void Window::processEvents(bool wait) {
if (wait) glfwWaitEvents();
else glfwPollEvents();
checkAxisUpdates();
}
@ -166,12 +166,12 @@ static void mouseButtonCallback(GLFWwindow* window, int button, int action, int
auto id = (InputID) ((int) InputID::MOUSE1 + button);
if (action == GLFW_PRESS) {
eventHandler->postEvent(id, { InputEvent::Type::BUTTON_ACTION, InputEvent::ButtonAction::PRESS, {} }
);
// printf("mouse\n");
eventHandler->postEvent(id, { InputEvent::Type::BUTTON_ACTION, InputEvent::ButtonAction::PRESS, {} });
} else if (action == GLFW_RELEASE) {
eventHandler->postEvent(id, { InputEvent::Type::BUTTON_ACTION, InputEvent::ButtonAction::RELEASE, {} });
// eventHandler->postEvent(id, { InputEvent::Type::BUTTON_ACTION, InputEvent::ButtonAction::NONE, {} });
}
}
static void scrollCallback(GLFWwindow* window, double xOffset, double yOffset) {

View file

@ -71,9 +71,13 @@ namespace tp {
public: // User interface
bool isEvents();
void processEvent();
void processAllEvent();
[[nodiscard]] const Vec2F& getPointer() const;
[[nodiscard]] const Vec2F& getPointerPrev() const;
void setCursorOrigin(const Vec2F& origin);
[[nodiscard]] Vec2F getPointer() const;
[[nodiscard]] Vec2F getPointerPrev() const;
[[nodiscard]] Vec2F getPointerDelta() const;
[[nodiscard]] bool isPressed(InputID id) const;
@ -83,6 +87,9 @@ namespace tp {
[[nodiscard]] halnf getPointerPressure() const;
private:
void processEventUnguarded();
private:
std::mutex mMutex = {};
@ -90,6 +97,8 @@ namespace tp {
List<std::pair<InputID, InputEvent>> mEventQueue;
// input states
Vec2F mPointerOrigin = { 0, 0 };
Vec2F mPointer;
Vec2F mPointerPrev;
Vec2F mScrollDelta;

View file

@ -12,16 +12,24 @@ namespace tp {
void run();
virtual void processFrame(EventHandler* eventHandler);
virtual bool forceNewFrame() { return false; }
virtual void processFrame(EventHandler* eventHandler, halnf deltaTime);
virtual void drawFrame(Canvas* canvas);
virtual ~Application();
void drawDebug();
private:
void runDefaultLoop();
void runDebugLoop();
protected:
bool mInitialized = false;
ualni mDrawPerSecond = 60;
ualni mProcPerSecond = 160;
ualni mProcPerSecond = 100;
Timer mDrawTimer;
Timer mProcTimer;

View file

@ -55,21 +55,25 @@ namespace tp {
ualni id = 0;
};
void setOrigin(const Vec2F& origin);
void pushClamp(const RectF& rec);
void popClamp();
void rect(const RectF& rec, const RGBA& col, halnf round = 0);
void circle(const Vec2F& pos, halnf size, const RGBA& col);
void rect(RectF rec, const RGBA& col, halnf round = 0);
void circle(Vec2F pos, halnf size, const RGBA& col);
void text(const char*, const RectF&, halnf size, Align, halnf padding, const RGBA&);
ImageHandle createImageFromTextId(ualni id, Vec2F size);
void updateTextureID(ImageHandle handle, ualni id);
void deleteImageHandle(ImageHandle image);
void drawImage(const RectF& rec, ImageHandle* image, halnf angle = 0, halnf alpha = 1.f, halnf rounding = 0.f);
void drawImage(RectF rec, ImageHandle* image, halnf angle = 0, halnf alpha = 1.f, halnf rounding = 0.f);
private:
Buffer<RectF> mScissors;
bool mIsClamping = false;
Vec2F mOrigin = { 0, 0 };
};
class Graphics {

View file

@ -17,7 +17,7 @@ namespace tp {
public:
void draw();
void processEvents();
void processEvents(bool wait = true);
void setEventHandler(EventHandler* eventHandler);
[[nodiscard]] EventHandler* getEventHandler();

View file

@ -14,7 +14,7 @@ public:
gui.updateTracks();
}
void processFrame(EventHandler* eventHandler) override {
void processFrame(EventHandler* eventHandler, halnf delta) override {
auto rec = RectF{ { 0, 0 }, mWindow->getSize() };
gui.setVisible(true);

View file

@ -10,7 +10,7 @@ class SimpleGUI : public Application {
public:
SimpleGUI() { gui.cd(objects_api::create<DictObject>(), "root"); }
void processFrame(EventHandler* eventHandler) override {}
void processFrame(EventHandler* eventHandler, halnf delta) override {}
void drawFrame(Canvas* canvas) override {
canvas->rect({ { 0, 0 }, mWindow->getSize() }, RGBA(0.f, 0.f, 0.f, 1.f), 0);

View file

@ -11,7 +11,7 @@ public:
Sketch3DApplication() :
mGui(*mGraphics->getCanvas(), { 1920, 1080 }) {}
void processFrame(EventHandler* eventHandler) override {
void processFrame(EventHandler* eventHandler, halnf delta) override {
auto rec = RectF({ 0, 0 }, mWindow->getSize());
mGui.setVisible(true);

View file

@ -9,7 +9,7 @@ class ExampleGUI : public Application {
public:
ExampleGUI() = default;
void processFrame(EventHandler* eventHandler) override {
void processFrame(EventHandler* eventHandler, halnf delta) override {
auto rec = RectF({ 0, 0 }, mWindow->getSize());
mGui.updateConfigWrapper(mWidgetManager);

View file

@ -12,7 +12,7 @@ public:
// mGui.mPreview = true;
}
void processFrame(EventHandler* eventHandler) override {
void processFrame(EventHandler* eventHandler, halnf) override {
const auto rec = RectF({ 0, 0 }, mWindow->getSize());
mGui.setArea(rec);

View file

@ -37,6 +37,8 @@ void FloatingWidget::eventUpdateConfiguration(WidgetManager& wm) {
}
void FloatingWidget::checkFloating(const Events& events) {
mDropped = false;
if (this->mHeader.isHolding() && events.getPointerDelta().length2() > 4) {
mFloating = true;
}
@ -44,6 +46,7 @@ void FloatingWidget::checkFloating(const Events& events) {
if (mFloating && this->mHeader.isReleased()) {
mFloating = false;
this->mHeader.clearEvents();
mDropped = true;
}
if (mFloating) {

View file

@ -19,11 +19,11 @@ void Widget::procWrapper(const Events& events, const RectF& parentArea) {
if (mHandlesEvents) {
eventProcess(events);
for (auto child : mChildWidgets) {
child->procWrapper(events, mVisibleArea);
}
eventProcess(events);
}
}

View file

@ -17,8 +17,8 @@ void WorkspaceWidget::eventProcess(const Events& events) {
for (auto floatingChild : mFloatingLayer.mChildWidgets) {
auto widget = dynamic_cast<FloatingWidget*>(floatingChild.data());
if (!widget) continue;
if (widget->isFloating()) {
if (widget->isReleased()) {
if (widget->mDropped) {
auto side = mDockSpace.getPreviewSide();
if (side != GridLayoutWidget::NONE) {
mFloatingLayer.mChildWidgets.removeNode(mFloatingLayer.mChildWidgets.find(widget));
@ -28,6 +28,7 @@ void WorkspaceWidget::eventProcess(const Events& events) {
}
}
if (widget->isFloating()) {
mDockSpace.mHandlesEvents = true;
mDockSpace.mPreview = true;
}

View file

@ -32,5 +32,7 @@ namespace tp {
public:
bool mResizable = true;
bool mDropped = false;
};
}