summaryrefslogtreecommitdiff
path: root/ui/iconButton.cpp
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2021-12-22 12:16:38 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2021-12-22 12:16:38 +0000
commite806d41c8703ddc4bcaf2186d0c1701bd1e1ada3 (patch)
tree1612baf22456c0b5a1bef82980177afb34b2756c /ui/iconButton.cpp
parentWindow handles UIComponent rendering (diff)
downloadilt-e806d41c8703ddc4bcaf2186d0c1701bd1e1ada3.tar.bz2
ilt-e806d41c8703ddc4bcaf2186d0c1701bd1e1ada3.tar.xz
ilt-e806d41c8703ddc4bcaf2186d0c1701bd1e1ada3.zip
Initial commit with some basic UI
Diffstat (limited to 'ui/iconButton.cpp')
-rw-r--r--ui/iconButton.cpp59
1 files changed, 59 insertions, 0 deletions
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 <SDL2/SDL.h>
+#include <array>
+#include <filesystem>
+#include <functional>
+#include <glm/gtc/type_ptr.hpp>
+#include <utility>
+
+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<GLsizeiptr>(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<glm::vec4, 4> 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;
+}