From e806d41c8703ddc4bcaf2186d0c1701bd1e1ada3 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 22 Dec 2021 12:16:38 +0000 Subject: Initial commit with some basic UI --- ui/iconButton.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 ui/iconButton.cpp (limited to 'ui/iconButton.cpp') diff --git a/ui/iconButton.cpp b/ui/iconButton.cpp new file mode 100644 index 0000000..c35303f --- /dev/null +++ b/ui/iconButton.cpp @@ -0,0 +1,59 @@ +#include "iconButton.h" +#include "ui/icon.h" +#include "ui/uiComponent.h" +#include +#include +#include +#include +#include +#include + +IconButton::IconButton(const std::string & icon_, glm::vec2 position_, UIEvent click_) : + UIComponent {{position_, ICON_SIZE}}, icon {icon_}, click {std::move(click_)}, m_vertexArrayObject {}, + m_vertexArrayBuffer {} +{ + glGenVertexArrays(1, &m_vertexArrayObject); + glBindVertexArray(m_vertexArrayObject); + + glGenBuffers(1, &m_vertexArrayBuffer); + + glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffer); + glBufferData(GL_ARRAY_BUFFER, static_cast(sizeof(glm::vec4) * 4), nullptr, GL_DYNAMIC_DRAW); + + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(glm::vec4), nullptr); + + glBindVertexArray(0); +} + +void +IconButton::render(const UIShader &, const Position & parentPos) const +{ + icon.Bind(); + glBindVertexArray(m_vertexArrayObject); + glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffer); + const auto abs = parentPos.origin + position.origin; + const auto limit = abs + ICON_SIZE; + std::array vertices {{ + {abs.x, abs.y, 0, 0}, + {limit.x, abs.y, 1, 0}, + {limit.x, limit.y, 1, 1}, + {abs.x, limit.y, 0, 1}, + }}; + glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), glm::value_ptr(vertices.front())); + glDrawArrays(GL_TRIANGLE_FAN, 0, 4); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); +} + +bool +IconButton::handleInput(const SDL_Event & e, const Position & parentPos) +{ + const auto absPos = position + parentPos; + if (absPos & e.button) { + if (e.button.type == SDL_MOUSEBUTTONUP && e.button.button == SDL_BUTTON_LEFT) { + click(e); + } + } + return false; +} -- cgit v1.2.3