summaryrefslogtreecommitdiff
path: root/engine-ocean/Game/Systems/UI/UITextures
diff options
context:
space:
mode:
Diffstat (limited to 'engine-ocean/Game/Systems/UI/UITextures')
-rw-r--r--engine-ocean/Game/Systems/UI/UITextures/UIButton.h94
-rw-r--r--engine-ocean/Game/Systems/UI/UITextures/uibutton.cpp202
-rw-r--r--engine-ocean/Game/Systems/UI/UITextures/uidisplay.cpp130
-rw-r--r--engine-ocean/Game/Systems/UI/UITextures/uidisplay.h67
-rw-r--r--engine-ocean/Game/Systems/UI/UITextures/uitexture.cpp6
-rw-r--r--engine-ocean/Game/Systems/UI/UITextures/uitexture.h39
6 files changed, 538 insertions, 0 deletions
diff --git a/engine-ocean/Game/Systems/UI/UITextures/UIButton.h b/engine-ocean/Game/Systems/UI/UITextures/UIButton.h
new file mode 100644
index 0000000..8cf02ee
--- /dev/null
+++ b/engine-ocean/Game/Systems/UI/UITextures/UIButton.h
@@ -0,0 +1,94 @@
+#ifndef UIBUTTON_H
+#define UIBUTTON_H
+#include "Game/Systems/UI/ButtonAction/buttonaction.h"
+#include "Game/Systems/UI/UITextures/uidisplay.h"
+#include "Graphics/global.h"
+#include <GLFW/glfw3.h>
+#include <set>
+#include "uitexture.h"
+
+enum CornerPosition {
+ TOPLEFT,
+ TOPRIGHT,
+ BOTTOMLEFT,
+ BOTTOMRIGHT,
+ NONE
+};
+
+class UIButton : public UITexture
+{
+public:
+ UIButton(TextureData tex, glm::vec2 pos, glm::vec2 scale, std::set<std::string>& shownScreens,
+ bool isCloseButton = false, CornerPosition corner = NONE, AspectRatio ratio = LAND_FIT);
+ ~UIButton();
+ void draw() override;
+ GLuint getTexID() override;
+ glm::vec2 getPos() override;
+ glm::vec2 getScale() override;
+ float getTextureRatio() override;
+ AspectRatio getAspectRatio();
+
+
+
+ void setWindowPos(int width, int height) override;
+ glm::vec2 getWindowPos();
+
+ int getHeight() override;
+ int getWidth() override;
+ Bounds2f getBounds() override;
+ float getTextureScaleAspect() override;
+
+ void addButtonAction(std::shared_ptr<ButtonAction> &action);
+ bool onButtonPress();
+ void setWindowToClose(std::string windowID);
+
+ void setParentDisplay(std::string parent);
+ std::string getParentDisplay();
+
+ CornerPosition getCornerPos();
+ bool hasCornerPos = false;
+ void setToCorner(const CornerPosition corner, int width, int height);
+
+ void setTransformationMat(glm::vec2 translation, glm::vec2 scale);
+ glm::mat4 getTransformationMat();
+
+
+
+private:
+ glm::mat4 getScaleMatrix(glm::vec2 scale);
+ int getScreenHeight();
+ int getScreenWidth();
+ void setTexID(GLuint &newTexID);
+
+
+ TextureData m_tex;
+ glm::vec2 m_pos;
+ glm::vec2 m_scale;
+ Bounds2f m_bounds;
+ glm::vec2 m_windowPos; // width, height
+ float m_windowHeight = 480.f;
+ float m_windowWidth = 640.f;
+ float m_toScreenScale = 1.f;
+
+ int m_screenImageHeight;
+ int m_screenImageWidth;
+ float m_tex_aspectRatio = 1.f;
+ bool m_isCloseButton = false;
+ std::string m_attachedWindow;
+
+ std::vector<std::shared_ptr<ButtonAction>> m_actions;
+ std::set<std::string>& m_shownScreens;
+
+ CornerPosition m_cornerPos;
+ std::string m_parentDisplay;
+
+ glm::mat4 m_transformationMat;
+ float m_textureAspect = 1.f;
+
+ AspectRatio m_aspectRatio;
+
+
+
+};
+
+#endif // UIButton_H
diff --git a/engine-ocean/Game/Systems/UI/UITextures/uibutton.cpp b/engine-ocean/Game/Systems/UI/UITextures/uibutton.cpp
new file mode 100644
index 0000000..031be2f
--- /dev/null
+++ b/engine-ocean/Game/Systems/UI/UITextures/uibutton.cpp
@@ -0,0 +1,202 @@
+#include "UIButton.h"
+#include "Game/Systems/UI/UITextures/UIDisplay.h"
+#include <set>
+
+UIButton::UIButton(TextureData tex, glm::vec2 pos, glm::vec2 scale, std::set<std::string>& shownScreens,
+ bool isCloseButton, CornerPosition corner, AspectRatio ratio):
+ m_tex(tex),
+ m_shownScreens(shownScreens),
+ m_aspectRatio(ratio)
+{
+
+ // set variables
+ m_isCloseButton = isCloseButton;
+ m_pos = pos;
+ m_scale = scale;
+ m_tex_aspectRatio = static_cast<float>(m_tex.height)/static_cast<float>(m_tex.width);
+
+ setToCorner(corner, 640, 480);
+ setWindowPos(640, 480);
+ setTransformationMat(m_pos, scale);
+
+}
+UIButton::~UIButton(){
+ glDeleteTextures(1, &m_tex.textureID);
+}
+
+
+AspectRatio UIButton::getAspectRatio(){
+ return m_aspectRatio;
+}
+
+glm::mat4 UIButton::getScaleMatrix(glm::vec2 scale) {
+ glm::mat4 M = glm::mat4(1.f);
+ M[0][0] = scale.x; //* (m_screenImageHeight/m_screenImageWidth);
+ M[1][1] = scale.y; //* (m_tex_aspectRatio);
+ M[2][2] = 1.f;
+ return M;
+}
+
+void UIButton::setTransformationMat(glm::vec2 translation, glm::vec2 scale){
+ glm::mat4 transMat = glm::mat4(1.f);
+ transMat[3] = glm::vec4(translation.x, translation.y, 0.f, 1.f);
+ glm::mat4 scaleMat = getScaleMatrix(glm::vec2(scale));
+ m_transformationMat = transMat*scaleMat;
+}
+
+glm::mat4 UIButton::getTransformationMat(){
+ return m_transformationMat;
+}
+
+float UIButton::getTextureRatio(){
+ return m_tex_aspectRatio;
+}
+
+
+void UIButton::draw(){}
+
+GLuint UIButton::getTexID(){
+ return m_tex.textureID;
+}
+
+glm::vec2 UIButton::getPos(){
+ return m_pos;
+}
+
+glm::vec2 UIButton::getScale(){
+ return m_scale;
+}
+
+//void UIButton::setTexID(GLuint &newTexID){
+// m_tex.textureID = newTexID;
+//}
+
+void UIButton::setWindowPos(int width, int height){
+ m_windowHeight = static_cast<float>(height);
+ m_windowWidth = static_cast<float>(width);
+
+ // find where on window it is, for bound checking
+ float xpos = .5f*(m_pos.x + 1.f)*m_windowWidth;
+ float ypos = (1.f-.5f*(m_pos.y + 1.f))*m_windowHeight;
+ m_windowPos = glm::vec2(xpos, ypos);
+
+ // set everything according to window dimensions -- this is for bound checking
+ m_toScreenScale = m_windowHeight*m_scale.y;
+ m_screenImageHeight = m_toScreenScale;
+ m_screenImageWidth = m_toScreenScale * m_tex_aspectRatio;
+
+ float windowRatio = m_windowHeight/m_windowWidth;
+ m_textureAspect = windowRatio / m_tex_aspectRatio;
+
+ // calculate window bounds
+ glm::vec2 halfDimensions = glm::vec2(m_screenImageWidth, m_screenImageHeight)*.5f;
+ m_bounds.max = glm::vec2(m_windowPos.x + halfDimensions.x, m_windowPos.y - halfDimensions.y);
+ m_bounds.min = glm::vec2(m_windowPos.x - halfDimensions.x, m_windowPos.y + halfDimensions.y);
+}
+
+float UIButton::getTextureScaleAspect(){
+ return m_textureAspect;
+}
+
+void UIButton::setParentDisplay(std::string parent){
+ m_parentDisplay = parent;
+
+}
+
+std::string UIButton::getParentDisplay(){
+ return m_parentDisplay;
+
+}
+
+CornerPosition UIButton::getCornerPos(){
+ return m_cornerPos;
+
+}
+
+void UIButton::setToCorner(const CornerPosition corner, int width, int height){
+
+ m_toScreenScale = static_cast<float>(height)*m_scale.y;
+ m_screenImageHeight = m_toScreenScale;
+ m_screenImageWidth = m_toScreenScale * m_tex_aspectRatio;
+
+ // in texture space
+ float xtrans = m_screenImageWidth/(.5f*static_cast<float>(width));
+ float ytrans = m_screenImageHeight/(.5f*static_cast<float>(height));
+
+ // find where window pos should be
+ switch(corner){
+ case TOPLEFT:
+ m_pos = glm::vec2(-1+xtrans, 1-ytrans);
+
+ break;
+ case TOPRIGHT:
+ m_pos = glm::vec2(1-xtrans, 1-ytrans);
+
+ break;
+ case BOTTOMLEFT:
+ m_pos = glm::vec2(-1+xtrans, -1+ytrans);
+
+ break;
+ case BOTTOMRIGHT:
+ m_pos = glm::vec2(1-xtrans, -1+ytrans);
+ break;
+ default:
+ break;
+ }
+}
+
+glm::vec2 UIButton::getWindowPos(){
+ return m_windowPos;
+}
+
+int UIButton::getHeight(){
+ return m_tex.height;
+}
+int UIButton::getWidth(){
+ return m_tex.width;
+}
+
+int UIButton::getScreenHeight(){
+ return m_screenImageHeight;
+}
+int UIButton::getScreenWidth(){
+ return m_screenImageWidth;
+}
+
+// remember that origin is top left corner!!
+Bounds2f UIButton::getBounds(){
+
+ return m_bounds;
+}
+
+void UIButton::addButtonAction(std::shared_ptr<ButtonAction> &action){
+ m_actions.push_back(action);
+}
+
+//////*/ for close button only
+void UIButton::setWindowToClose(std::string windowID){
+ if (m_isCloseButton){
+ m_attachedWindow = windowID;
+ }
+
+}
+
+bool UIButton::onButtonPress(){
+ // if button is a close button, then deactivate everything
+ if (m_isCloseButton){
+ std::cout << "shownWindowSize: " << m_shownScreens.size() << std::endl;
+ //m_shownScreens.erase(m_attachedWindow);
+ std::cout << "new shownWindowSize: " << m_shownScreens.size() << std::endl;
+ std::cout << "CLOSE WINDOW: " << m_attachedWindow << std::endl;
+ return true;
+
+
+ } else {
+ for (auto &action : m_actions){
+ action->activate();
+ }
+ return false;
+ }
+
+}
+
diff --git a/engine-ocean/Game/Systems/UI/UITextures/uidisplay.cpp b/engine-ocean/Game/Systems/UI/UITextures/uidisplay.cpp
new file mode 100644
index 0000000..2ddaf62
--- /dev/null
+++ b/engine-ocean/Game/Systems/UI/UITextures/uidisplay.cpp
@@ -0,0 +1,130 @@
+#include "uidisplay.h"
+#include <set>
+
+UIDisplay::UIDisplay(TextureData tex, glm::vec2 pos, glm::vec2 scale,
+ std::set<std::string>& shownScreens,
+ AspectRatio ratio):
+ m_tex(tex),
+ m_shownScreens(shownScreens),
+ m_aspectRatio(ratio)
+{
+
+ m_pos = (pos);
+ m_scale = (scale);
+ m_tex_aspectRatio = static_cast<float>(m_tex.height)/static_cast<float>(m_tex.width);
+ setTransformationMat(pos, scale);
+// std::cout << "tex aspect ratio:" << m_tex_aspectRatio << std::endl;
+
+// std::cout << "aspect ratio w: " << m_tex.width << std::endl;
+// std::cout << "aspect ratio h: " << m_tex.height << std::endl;
+
+
+ setWindowPos(640, 480);
+// std::cout << "screen image height: " << m_screenImageHeight << std::endl;
+// std::cout << "screen image width: " << m_screenImageWidth << std::endl;
+
+}
+
+UIDisplay::~UIDisplay(){
+ glDeleteTextures(1, &m_tex.textureID);
+}
+
+void UIDisplay::draw(){}
+
+GLuint UIDisplay::getTexID(){
+ return m_tex.textureID;
+}
+
+glm::vec2 UIDisplay::getPos(){
+ return m_pos;
+}
+
+glm::vec2 UIDisplay::getScale(){
+ return m_scale;
+}
+
+AspectRatio UIDisplay::getAspectRatio(){
+ return m_aspectRatio;
+}
+
+void UIDisplay::setWindowPos(int width, int height){
+ float xpos = .5f*(m_pos.x + 1.f)*static_cast<float>(width);
+ float ypos = (1.f-.5f*(m_pos.y + 1.f))*static_cast<float>(height);
+ m_windowPos = glm::vec2(xpos, ypos);
+
+ // set everything according to window dimensions
+ m_toScreenScale = static_cast<float>(height)*m_scale.y;
+ m_screenImageHeight = m_toScreenScale;
+ m_screenImageWidth = m_toScreenScale * m_tex_aspectRatio;
+
+ float windowRatio = static_cast<float>(height)/static_cast<float>(width);
+ m_textureAspect = windowRatio / m_tex_aspectRatio;
+}
+
+void UIDisplay::setPos(glm::vec2 pos){
+ m_pos = pos;
+ setTransformationMat(m_pos, m_scale);
+}
+void UIDisplay::setScale(glm::vec2 scale){
+ m_scale = scale;
+ setTransformationMat(m_pos, m_scale);
+}
+
+float UIDisplay::getTextureScaleAspect(){
+ return m_textureAspect;
+}
+
+glm::vec2 UIDisplay::getWindowPos(){
+ return m_windowPos;
+}
+
+int UIDisplay::getHeight(){
+ return m_tex.height;
+}
+int UIDisplay::getWidth(){
+ return m_tex.width;
+}
+
+int UIDisplay::getScreenHeight(){
+ return m_screenImageHeight;
+}
+int UIDisplay::getScreenWidth(){
+ return m_screenImageWidth;
+}
+
+glm::mat4 UIDisplay::getScaleMatrix(glm::vec2 scale) {
+ glm::mat4 M = glm::mat4(1.f);
+ M[0][0] = scale.x;//* (m_screenImageHeight/m_screenImageWidth);
+ M[1][1] = scale.y; //* (m_tex_aspectRatio);
+ M[2][2] = 1.f;
+ return M;
+}
+
+void UIDisplay::setTransformationMat(glm::vec2 translation, glm::vec2 scale){
+ glm::mat4 transMat = glm::mat4(1.f);
+ transMat[3] = glm::vec4(translation.x, translation.y, 0.f, 1.f);
+
+ glm::mat4 scaleMat = getScaleMatrix(glm::vec2(scale));
+
+ m_transformationMat = transMat*scaleMat;
+}
+
+glm::mat4 UIDisplay::getTransformationMat(){
+ return m_transformationMat;
+}
+
+// remember that origin is top left corner!!
+Bounds2f UIDisplay::getBounds(){
+ glm::vec2 halfDimensions = glm::vec2(m_screenImageWidth, m_screenImageHeight)*.5f;
+ m_bounds.max = glm::vec2(m_windowPos.x + halfDimensions.x, m_windowPos.y - halfDimensions.y);
+ m_bounds.min = glm::vec2(m_windowPos.x - halfDimensions.x, m_windowPos.y + halfDimensions.y);
+ return m_bounds;
+}
+
+float UIDisplay::getTextureRatio(){
+ return m_tex_aspectRatio;
+}
+
+
+
+
diff --git a/engine-ocean/Game/Systems/UI/UITextures/uidisplay.h b/engine-ocean/Game/Systems/UI/UITextures/uidisplay.h
new file mode 100644
index 0000000..4b739cc
--- /dev/null
+++ b/engine-ocean/Game/Systems/UI/UITextures/uidisplay.h
@@ -0,0 +1,67 @@
+#ifndef UIDISPLAY_H
+#define UIDISPLAY_H
+#include "uitexture.h"
+#include <set>
+
+
+
+class UIDisplay : public UITexture
+{
+public:
+ UIDisplay(TextureData tex, glm::vec2 pos, glm::vec2 scale, std::set<std::string>& shownScreens,
+ AspectRatio ratio = FIT_SCREEN);
+ ~UIDisplay();
+ void draw() override;
+ GLuint getTexID() override;
+ glm::vec2 getPos() override;
+ glm::vec2 getScale() override;
+
+ void setWindowPos(int width, int height) override;
+ glm::vec2 getWindowPos();
+
+ int getHeight() override;
+ int getWidth() override;
+ Bounds2f getBounds() override;
+ float getTextureRatio() override;
+ float getTextureScaleAspect() override;
+ void setPos(glm::vec2 pos);
+ void setScale(glm::vec2 scale);
+
+ AspectRatio getAspectRatio();
+
+
+ // glm::vec2 getCornerPos(CornerPosition corner, glm::vec2 elementDimensions);
+ void setTransformationMat(glm::vec2 translation, glm::vec2 scale);
+ glm::mat4 getTransformationMat();
+
+private:
+ int getScreenHeight();
+ int getScreenWidth();
+ void setTexID(GLuint &newTexID);
+ glm::mat4 getScaleMatrix(glm::vec2 scale);
+
+
+
+ TextureData m_tex;
+ glm::vec2 m_pos;
+ glm::vec2 m_scale;
+ Bounds2f m_bounds;
+ glm::vec2 m_windowPos; // width, height
+ int m_windowHeight = 480.f;
+ int m_windowWidth = 640.f;
+ float m_toScreenScale = 1.f;
+
+ int m_screenImageHeight;
+ int m_screenImageWidth;
+ float m_tex_aspectRatio = 1.f;
+ bool m_isCloseButton = false;
+ std::string m_attachedWindow;
+
+ std::set<std::string>& m_shownScreens;
+ glm::mat4 m_transformationMat;
+ float m_textureAspect = 1.f;
+
+ AspectRatio m_aspectRatio;
+};
+
+#endif // UIDISPLAY_H
diff --git a/engine-ocean/Game/Systems/UI/UITextures/uitexture.cpp b/engine-ocean/Game/Systems/UI/UITextures/uitexture.cpp
new file mode 100644
index 0000000..8680baf
--- /dev/null
+++ b/engine-ocean/Game/Systems/UI/UITextures/uitexture.cpp
@@ -0,0 +1,6 @@
+#include "uitexture.h"
+
+UITexture::UITexture()
+{
+
+}
diff --git a/engine-ocean/Game/Systems/UI/UITextures/uitexture.h b/engine-ocean/Game/Systems/UI/UITextures/uitexture.h
new file mode 100644
index 0000000..53e429d
--- /dev/null
+++ b/engine-ocean/Game/Systems/UI/UITextures/uitexture.h
@@ -0,0 +1,39 @@
+#ifndef UITEXTURE_H
+#define UITEXTURE_H
+#include "Graphics/global.h"
+#include <GLFW/glfw3.h>
+
+struct Bounds2f{
+ glm::vec2 min;
+ glm::vec2 max;
+};
+
+enum AspectRatio {
+ LAND_FIT,
+ LAND_FILL,
+ PORTRAIT_FIT,
+ PORTRAIT_FILL,
+ FIT_SCREEN
+};
+
+class UITexture
+{
+public:
+ UITexture();
+ virtual void draw() = 0;
+ virtual GLuint getTexID() = 0;
+ virtual glm::vec2 getPos() = 0;
+ virtual glm::vec2 getScale() = 0;
+ virtual int getHeight() = 0;
+ virtual int getWidth() = 0;
+ virtual Bounds2f getBounds() = 0;
+ virtual void setWindowPos(int width, int height) = 0;
+ virtual float getTextureRatio() = 0;
+ virtual float getTextureScaleAspect() = 0;
+
+
+
+
+};
+
+#endif // UITEXTURE_H