Skip to content

Commit

Permalink
EditButton has focus
Browse files Browse the repository at this point in the history
Now the editButton has the variable focus, it sets when clic on hover.
  • Loading branch information
vandalo committed Mar 15, 2018
1 parent 3151dd4 commit 29a17ee
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 21 deletions.
16 changes: 16 additions & 0 deletions LCSEngine/ElementGameUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include "ElementGameUI.h"
#include "Imgui/imgui.h"
#include "ModuleSceneMain.h"
#include "MathGeoLib/src/Math/MathFunc.h"
#include "MathGeoLib/src/Math/float3x4.h"
#include "ModuleInput.h"


ElementGameUI::ElementGameUI(GameObject* parent, int x, int y, int h, int w, bool isVisible)
Expand All @@ -26,3 +29,16 @@ void ElementGameUI::startGUI()
void ElementGameUI::endGUI()
{
}

bool ElementGameUI::isHover()
{
iPoint mousePosition = App->input->getMousePosition();
if (mousePosition.x > rect.x && mousePosition.x < rect.x + rect.w)
{
if (mousePosition.y > rect.y && mousePosition.y < rect.y + rect.h)
{
return true;
}
}
return false;
}
1 change: 1 addition & 0 deletions LCSEngine/ElementGameUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ElementGameUI
virtual void drawGUI() = 0;
void startGUI();
void endGUI();
bool isHover();

public:
TypeElemeneGametUI type;
Expand Down
28 changes: 28 additions & 0 deletions LCSEngine/ModuleGameUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,34 @@ void ModuleGameUI::printGameUI()

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, background->idIdxVAO);

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, NULL);

UILabel* label = (UILabel*)((UIEditText*)(*it))->label;
UIImage* imageLabel = (UIImage*)label->textImage;

glUniform1i(glGetUniformLocation(program, "useText"), true);

if (label->fontData != nullptr)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, label->fontData->idTexture);
glUniform1i(glGetUniformLocation(program, "text"), 0);

glBindBuffer(GL_ARRAY_BUFFER, imageLabel->idTexCoords);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid*)0);
}

glBindBuffer(GL_ARRAY_BUFFER, imageLabel->idVertVBO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);

glBindBuffer(GL_ARRAY_BUFFER, imageLabel->idColors);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, imageLabel->idIdxVAO);

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, NULL);
break;
}
Expand Down
23 changes: 4 additions & 19 deletions LCSEngine/UIButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
#include "ModuleSceneMain.h"
#include "ElementGameUI.h"
#include "UIButton.h"
#include "MathGeoLib/src/Math/MathFunc.h"
#include "MathGeoLib/src/Math/float3x4.h"
#include "Imgui/imgui.h"
#include "GameObject.h"
#include "UIImage.h"
#include "UILabel.h"
#include "ElementFactory.h"
#include "ModuleInput.h"


UIButton::UIButton(GameObject* parent, int x, int y, int h, int w, bool isVisible) : ElementGameUI(parent, x, y, h, w, isVisible)
{
type = BUTTON;
Expand Down Expand Up @@ -104,23 +103,21 @@ void UIButton::update()
label->update();

updateState();


}

void UIButton::updateState()
{
KeyState click = App->input->getMouseButtonDown(1);
bool mouseHoverButton = isHover();
if (disabled->textureName != "" && !enable)
if (disabled->textureName[0] != '\0' && !enable)
{
activeImage = disabled;
}
else if (pressed->textureName != "" && ((click == KEY_DOWN && mouseHoverButton) || (click == KEY_REPEAT && activeImage == pressed && mouseHoverButton)))
else if (pressed->textureName[0] != '\0' && ((click == KEY_DOWN && mouseHoverButton) || (click == KEY_REPEAT && activeImage == pressed && mouseHoverButton)))
{
activeImage = pressed;
}
else if (mouseHoverButton && hover->textureName != "")
else if (mouseHoverButton && hover->textureName[0] != '\0')
{
activeImage = hover;
}
Expand All @@ -130,15 +127,3 @@ void UIButton::updateState()
}
}

bool UIButton::isHover()
{
iPoint mousePosition = App->input->getMousePosition();
if (mousePosition.x > rect.x && mousePosition.x < rect.x + rect.w)
{
if (mousePosition.y > rect.y && mousePosition.y < rect.y + rect.h)
{
return true;
}
}
return false;
}
1 change: 0 additions & 1 deletion LCSEngine/UIButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class UIButton : public ElementGameUI

private:
void updateState();
bool isHover();

private:
UIImage* background = nullptr;
Expand Down
14 changes: 13 additions & 1 deletion LCSEngine/UIEditText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "ElementFactory.h"
#include "UIImage.h"
#include "UILabel.h"
#include "ModuleInput.h"

UIEditText::UIEditText(GameObject* parent, int x, int y, int h, int w, bool isVisible) : ElementGameUI(parent, x, y, h, w, isVisible)
{
Expand Down Expand Up @@ -55,7 +56,7 @@ void UIEditText::drawGUI()
}

ImGui::InputText("Selected", &selected->textureName[0], IM_ARRAYSIZE(selected->textureName));
if (ImGui::Button("Set hover") && selected->textureName != selected->texName)
if (ImGui::Button("Set selected") && selected->textureName != selected->texName)
{
selected->textureChanged = true;
}
Expand All @@ -68,4 +69,15 @@ void UIEditText::update()
selected->rect = rect;
background->updateCoords();
selected->updateCoords();

KeyState click = App->input->getMouseButtonDown(1);
bool mouseHoverButton = isHover();
if (mouseHoverButton && click == KEY_DOWN)
{
focus = true;
}
else if (!mouseHoverButton && click == KEY_DOWN)
{
focus = false;
}
}
3 changes: 3 additions & 0 deletions LCSEngine/UIEditText.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class UIEditText : public ElementGameUI
UILabel* label = nullptr;
UIImage* background = nullptr;
UIImage* selected = nullptr;
bool focus = false;


};

#endif //__UIEDITTEXT_H__

0 comments on commit 29a17ee

Please sign in to comment.