This commit is contained in:
Ilusha 2024-01-18 09:15:22 +03:00 committed by Ilya Shurupov
parent 466c0c36bd
commit 5106cc3b71
30 changed files with 1186 additions and 208 deletions

View file

@ -1,103 +0,0 @@
#include "Animations.hpp"
using namespace tp;
bool AnimValue::gInTransition = false;
halnf AnimValue::interpolate() const {
if (!mTimeAnim) {
return mVal;
}
auto dt = gCurrentTime - mTimeStart;
if (dt > mTimeAnim) dt = mTimeAnim;
auto t = (halnf) dt / (halnf) mTimeAnim;
t = (halnf) (0.511 + atan((t - 0.36) * 28) / 2.9);
t = clamp(t, 0.f, 1.f);
auto out = mValPrev + (mVal - mValPrev) * t;
return out;
}
AnimValue::AnimValue() {
mTimeStart = gCurrentTime;
}
void AnimValue::setAnimTime(halni time) { mTimeAnim = time; }
AnimValue::AnimValue(halnf val) {
mVal = val;
mValPrev = mVal;
mTimeStart = gCurrentTime;
}
bool AnimValue::inTransition() const {
auto timePassed = gCurrentTime - mTimeStart >= mTimeAnim;
return !timePassed;
}
void AnimValue::set(halnf val) {
if (!inTransition()) mValPrev = mVal;
if (val == mVal) return;
mValPrev = get();
mVal = val;
if (!inTransition()) {
mTimeStart = gCurrentTime;
}
}
halnf AnimValue::getTarget() const { return mVal; }
void AnimValue::setNoTransition(halnf val) {
mValPrev = val;
mVal = val;
mTimeStart = gCurrentTime - mTimeAnim;
}
halnf AnimValue::get() const {
if (inTransition()) {
gInTransition = true;
return interpolate();
}
return mVal;
}
void AnimValue::operator=(halnf val) {
setNoTransition(val);
}
AnimValue::operator halnf() const { return get(); }
RectF AnimRect::get() const { return { x.get(), y.get(), z.get(), w.get() }; }
RectF AnimRect::getTarget() const { return { x.getTarget(), y.getTarget(), z.getTarget(), w.getTarget() }; }
void AnimRect::setNoTransition(RectF rec) {
x.setNoTransition(rec.x);
y.setNoTransition(rec.y);
z.setNoTransition(rec.z);
w.setNoTransition(rec.w);
}
void AnimRect::setAnimTime(const halni time) {
x.setAnimTime(time);
y.setAnimTime(time);
z.setAnimTime(time);
w.setAnimTime(time);
}
void AnimRect::set(const RectF& in) {
x.set(in.x);
y.set(in.y);
z.set(in.z);
w.set(in.w);
}
AnimColor::AnimColor() : mColor() {}
RGBA AnimColor::get() const {
auto col = mColor.get();
return { col.x, col.y, col.z, col.w };
}
void AnimColor::set(const RGBA& col) { mColor.set(RectF(col.r, col.g, col.b, col.a)); }

View file

@ -24,25 +24,21 @@ Graphics::Canvas::Canvas() { mContext = new Context(); }
Graphics::Canvas::~Canvas() { delete mContext; }
void Graphics::Canvas::init() {
mContext->vg = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES);
void Graphics::Canvas::init() {
mContext->vg = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES);
if (nvgCreateFont(mContext->vg, "default", "Font.ttf") == -1) {
// TODO
// TODO
}
}
void Graphics::Canvas::deinit() {
nvgDeleteGL3(mContext->vg);
}
void Graphics::Canvas::deinit() { nvgDeleteGL3(mContext->vg); }
RectF Graphics::Canvas::getAvaliableArea() {
return { (halnf) 0, (halnf) 0, mWidth, mHeight };
}
RectF Graphics::Canvas::getAvaliableArea() { return { (halnf) 0, (halnf) 0, mWidth, mHeight }; }
void Graphics::Canvas::rect(const RectF& rec, const RGBA& col, halnf round) {
nvgBeginPath(mContext->vg);
if (round == 0) {
nvgRect(mContext->vg, rec.x, rec.y, rec.z, rec.w);
} else {
@ -53,49 +49,69 @@ void Graphics::Canvas::rect(const RectF& rec, const RGBA& col, halnf round) {
nvgFill(mContext->vg);
}
void Graphics::Canvas::text(const String& string, const RectF& aRec, halnf size, Align align, halnf marging, const RGBA& col) {
void Graphics::Canvas::pushClamp(const RectF& rec) {
RectF intersection = rec;
if (mScissors.size()) {
mScissors.last().calcIntersection(rec, intersection);
}
nvgScissor(mContext->vg, intersection.x, intersection.y, intersection.z, intersection.w);
mScissors.append(rec);
}
void Graphics::Canvas::popClamp() {
DEBUG_ASSERT(mScissors.size());
mIsClamping = false;
mScissors.pop();
if (mScissors.size()) {
const auto& rec = mScissors.last();
nvgScissor(mContext->vg, rec.x, rec.y, rec.z, rec.w);
} else {
nvgResetScissor(mContext->vg);
}
}
void Graphics::Canvas::text(
const String& 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 };
nvgScissor(mContext->vg, rec.x, rec.y, rec.z, rec.w);
pushClamp(rec);
nvgFontSize(mContext->vg, size);
nvgFontFace(mContext->vg, "default");
nvgFillColor(mContext->vg, { col.r, col.g, col.b, col.a } );
nvgFillColor(mContext->vg, { col.r, col.g, col.b, col.a });
float centerX = rec.x;
float centerY = rec.y;
int alignNVG = 0;
if (((int1*)&align)[1] == 0x00) { // center x
if (((int1*) &align)[1] == 0x00) { // center x
alignNVG |= NVG_ALIGN_CENTER;
centerX += rec.z * 0.5f;
} else if (((int1*)&align)[1] == 0x01) { // left x
} else if (((int1*) &align)[1] == 0x01) { // left x
alignNVG |= NVG_ALIGN_LEFT;
} else if (((int1*)&align)[1] == 0x02) { // right x
} else if (((int1*) &align)[1] == 0x02) { // right x
alignNVG |= NVG_ALIGN_RIGHT;
centerX += rec.z;
}
if (((int1*)&align)[0] == 0x00) { // center y
if (((int1*) &align)[0] == 0x00) { // center y
alignNVG |= NVG_ALIGN_MIDDLE;
centerY += rec.w * 0.5f;
} else if (((int1*)&align)[0] == 0x01) { // top y
} else if (((int1*) &align)[0] == 0x01) { // top y
alignNVG |= NVG_ALIGN_TOP;
centerY += rec.w;
} else if (((int1*)&align)[0] == 0x02) { // bottom y
} else if (((int1*) &align)[0] == 0x02) { // bottom y
alignNVG |= NVG_ALIGN_BOTTOM;
}
nvgTextAlign(mContext->vg, alignNVG);
nvgText(mContext->vg, centerX, centerY, string.read(), nullptr);
nvgResetScissor(mContext->vg);
popClamp();
}
void Graphics::Canvas::proc() {
nvgBeginFrame(mContext->vg, mWidth, mHeight, 1.0);
}
void Graphics::Canvas::proc() { nvgBeginFrame(mContext->vg, mWidth, mHeight, 1.0); }
void Graphics::Canvas::draw() {
nvgEndFrame(mContext->vg);
}
void Graphics::Canvas::draw() { nvgEndFrame(mContext->vg); }

View file

@ -4,9 +4,9 @@
// -------- Debug UI -------- //
#include <imgui.h>
#include <imgui_internal.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
#include <imgui_internal.h>
namespace tp {
class Graphics::GUI::Context {
@ -33,6 +33,9 @@ void Graphics::GUI::init(Window* window) {
// ImGui_ImplGlfw_GetBackendData();
ImGui_ImplOpenGL3_Init("#version 330");
ImGuiIO& io = ImGui::GetIO();
io.Fonts->AddFontFromFileTTF("Font.ttf", 20.f);
}
void Graphics::GUI::deinit() {

View file

@ -47,8 +47,7 @@ void Graphics::GL::deinit() {
glDeleteVertexArrays(1, &mContext->vao);
}
void Graphics::GL::proc() {
}
void Graphics::GL::proc() {}
void Graphics::GL::draw() {
glClearColor(0.f, 0.f, 0.f, 0.f);
@ -102,7 +101,7 @@ Window::Window(int width, int height, const char* title) {
}
mContext->inputManager.init(mContext->window);
mGraphics.init(this);
mEvents.mContext = mContext;
@ -149,7 +148,7 @@ bool Window::shouldClose() const { return glfwWindowShouldClose(mContext->window
void Window::processEvents() {
mContext->inputManager.isEvent = false;
glfwPollEvents();
int w, h;
glfwGetWindowSize(mContext->window, &w, &h);
@ -176,22 +175,14 @@ auto Window::getContext() -> Context* { return mContext; }
Graphics::Canvas& Window::getCanvas() { return mGraphics.mCanvas; }
const Window::Events& Window::getEvents() { return mEvents; }
const Vec2F& Window::Events::getPos() const {
return mContext->inputManager.getPos();
}
const Vec2F& Window::Events::getPos() const { return mContext->inputManager.getPos(); }
bool Window::Events::isPressed() const {
return mContext->inputManager.isPressed();
}
bool Window::Events::isPressed() const { return mContext->inputManager.isPressed(); }
bool Window::Events::isDown() const {
return mContext->inputManager.isDown();
}
bool Window::Events::isReleased() const { return mContext->inputManager.isReleased(); }
halnf Window::Events::getScrollY() const {
return mContext->inputManager.getScrollY();
}
bool Window::Events::isDown() const { return mContext->inputManager.isDown(); }
bool Window::Events::isEvent() const {
return mContext->inputManager.isEvents();
}
halnf Window::Events::getScrollY() const { return mContext->inputManager.getScrollY(); }
bool Window::Events::isEvent() const { return mContext->inputManager.isEvents(); }

View file

@ -11,8 +11,8 @@ namespace tp {
class InputManager {
public:
InputManager() = default;
void init(GLFWwindow* aWindow) {
void init(GLFWwindow* aWindow) {
window = aWindow;
// Set up key and mouse button callbacks
@ -24,25 +24,17 @@ namespace tp {
const Vec2F& getPos() const { return mousePos; }
bool isKeyPressed(int key) const {
return keyStates[key] == GLFW_PRESS;
}
bool isKeyPressed(int key) const { return keyStates[key] == GLFW_PRESS; }
bool isKeyDown(int key) const {
return keyStates[key] == GLFW_REPEAT || keyStates[key] == GLFW_PRESS;
}
bool isKeyDown(int key) const { return keyStates[key] == GLFW_REPEAT || keyStates[key] == GLFW_PRESS; }
bool isPressed() const {
return mouseButtonStates[GLFW_MOUSE_BUTTON_LEFT] == GLFW_PRESS;
}
bool isPressed() const { return mouseButtonStates[GLFW_MOUSE_BUTTON_LEFT] == GLFW_PRESS; }
bool isDown() const {
return mouseButtonStates[GLFW_MOUSE_BUTTON_LEFT] == GLFW_REPEAT || mouseButtonStates[GLFW_MOUSE_BUTTON_LEFT] == GLFW_PRESS;
}
bool isReleased() const { return mouseButtonStates[GLFW_MOUSE_BUTTON_LEFT] == GLFW_RELEASE; }
bool isEvents() const {
return isEvent;
}
bool isDown() const { return mouseButtonStates[GLFW_MOUSE_BUTTON_LEFT] == GLFW_REPEAT || mouseButtonStates[GLFW_MOUSE_BUTTON_LEFT] == GLFW_PRESS; }
bool isEvents() const { return isEvent; }
double getScrollY() const { return scrollY; }
@ -105,7 +97,7 @@ namespace tp {
bool isEvent = false;
double scrollX = 0.0;
double scrollY = 0.0;
double scrollY = 0.0;
GLFWwindow* window;
Vec2F mousePos;
Vec2F mousePosPrev;

View file

@ -1,55 +0,0 @@
#pragma once
#include "GraphicsCommon.hpp"
namespace tp {
class AnimValue {
halnf mValPrev = 0;
halnf mVal = 0;
time_ms mTimeStart = 0;
halni mTimeAnim = 250;
static bool gInTransition;
private:
[[nodiscard]] halnf interpolate() const;
public:
AnimValue();
explicit AnimValue(halnf val);
[[nodiscard]] halnf prev() const { return mValPrev; }
void set(halnf val);
void setNoTransition(halnf);
void setAnimTime(halni time);
[[nodiscard]] halnf get() const;
[[nodiscard]] halnf getTarget() const;
[[nodiscard]] bool inTransition() const;
explicit operator halnf() const;
void operator=(halnf val);
};
class AnimRect : Rect<AnimValue> {
public:
AnimRect() {
setAnimTime(450);
setNoTransition({ 0, 0, 0, 0 });
}
[[nodiscard]] RectF get() const;
[[nodiscard]] RectF getTarget() const;
void setNoTransition(RectF);
void set(const RectF&);
void setAnimTime(halni time_ms);
};
class AnimColor {
public:
AnimColor();
AnimRect mColor;
[[nodiscard]] RGBA get() const;
void set(const RGBA&);
};
};

View file

@ -69,19 +69,21 @@ namespace tp {
void draw();
public:
enum Align : int2 {
CC = 0x0000,
CT = 0x0001,
CB = 0x0002,
LC = 0x0100,
LT = 0x0101,
LB = 0x0102,
RC = 0x0200,
RT = 0x0201,
enum Align : int2 {
CC = 0x0000,
CT = 0x0001,
CB = 0x0002,
LC = 0x0100,
LT = 0x0101,
LB = 0x0102,
RC = 0x0200,
RT = 0x0201,
RB = 0x0202,
};
RectF getAvaliableArea();
void pushClamp(const RectF& rec);
void popClamp();
void rect(const RectF& rec, const RGBA& col, halnf round = 0);
void text(const String&, const RectF&, halnf size, Align, halnf marging, const RGBA&);
@ -90,6 +92,8 @@ namespace tp {
private:
halnf mWidth = 600;
halnf mHeight = 600;
Buffer<RectF> mScissors;
bool mIsClamping = false;
};
private:

View file

@ -21,9 +21,10 @@ namespace tp {
struct Events {
Vec2F mPointer;
const Vec2F& getPos() const;
bool isPressed() const;
bool isReleased() const;
bool isDown() const;
halnf getScrollY() const;
bool isEvent() const;