Skip to content

Commit

Permalink
Merge pull request #58 from requizm/master
Browse files Browse the repository at this point in the history
Button added
  • Loading branch information
Jelle Vos committed Jul 20, 2021
2 parents 16aa4ec + b770511 commit bb1206e
Show file tree
Hide file tree
Showing 7 changed files with 346 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Expand Up @@ -39,6 +39,7 @@ add_library(wase-engine
src/wase/core/draw.h
src/wase/core/draw.cpp
src/wase/core/vector2.h
src/wase/core/size.h
src/wase/core/utils/log_utils.h
src/wase/core/utils/log_utils.cpp
src/wase/core/managers/scene_manager.h
Expand All @@ -65,6 +66,8 @@ add_library(wase-engine
src/wase/ecs/components/colliders/box_collider_2d.cpp
src/wase/ecs/components/ui/label.h
src/wase/ecs/components/ui/label.cpp
src/wase/ecs/components/ui/button.h
src/wase/ecs/components/ui/button.cpp
)

# Group the core files
Expand All @@ -87,6 +90,7 @@ source_group(core FILES
src/wase/core/draw.h
src/wase/core/draw.cpp
src/wase/core/vector2.h
src/wase/core/size.h
src/wase/core/managers/scene_manager.h
src/wase/core/managers/scene_manager.cpp
src/wase/core/managers/resource_manager.h
Expand Down Expand Up @@ -123,6 +127,8 @@ source_group(ecs/components FILES
src/wase/ecs/components/colliders/box_collider_2d.cpp
src/wase/ecs/components/ui/label.h
src/wase/ecs/components/ui/label.cpp
src/wase/ecs/components/ui/button.h
src/wase/ecs/components/ui/button.cpp
)

target_link_libraries(wase-engine ${SDL2_LIBRARY}
Expand Down
6 changes: 6 additions & 0 deletions src/wase/core/size.h
@@ -0,0 +1,6 @@
#pragma once

struct Size
{
int w, h;
};
5 changes: 5 additions & 0 deletions src/wase/ecs/components/colliders/box_collider_2d.cpp
Expand Up @@ -39,6 +39,11 @@ void BoxCollider2D::render()
}
}

bool BoxCollider2D::isHovering()
{
return mouseHover;
}

bool BoxCollider2D::onMouseEnter()
{
if (entered)
Expand Down
1 change: 1 addition & 0 deletions src/wase/ecs/components/colliders/box_collider_2d.h
Expand Up @@ -22,6 +22,7 @@ class BoxCollider2D : public Collider
void update(float dt) override;
void render() override;

bool isHovering();
bool onMouseEnter();
bool onMouseExit();

Expand Down
241 changes: 241 additions & 0 deletions src/wase/ecs/components/ui/button.cpp
@@ -0,0 +1,241 @@
#include "button.h"

#include "../../../core/managers/resource_manager.h"
#include "../../../core/renderer.h"
#include "../../../core/input.h"

#include <SDL_ttf.h>

Button::Button(const char* text, const Size &buttonSize, const char *font, TextAlignment textAlignment, const SDL_Color &textColor, const SDL_Color &buttonColor)
{
this->text = text;
this->buttonSize = buttonSize;
this->font = font;
this->textAlignment = textAlignment;
textCurrentColor = textColor;
textColors.color = textColor;
buttonCurrentColor = buttonColor;
buttonColors.color = buttonColor;

updateText();
}

void Button::start()
{
transform = owner->getComponent<Transform>();
boxCollider = owner->addComponent<BoxCollider2D>(buttonSize.w, buttonSize.h);
camera = &transform->owner->entityManager->camera;

rect.w = buttonSize.w;
rect.h = buttonSize.h;

rectangle = Draw::rectangle(rect, buttonCurrentColor.r, buttonCurrentColor.g, buttonCurrentColor.b, buttonCurrentColor.a, false);
}

void Button::update(float dt)
{
if (owner->parent && !parentTransform)
parentTransform = owner->parent->getComponent<Transform>();

if (!owner->parent)
parentTransform = nullptr;

rectangle->rect.x = !parentTransform ? (int)(transform->position.x - camera->position.x) : (int)(transform->position.x + parentTransform->position.x - camera->position.x);
rectangle->rect.y = !parentTransform ? (int)(transform->position.y - camera->position.y) : (int)(transform->position.y + parentTransform->position.y - camera->position.y);

textPosition.x = rectangle->rect.x + textOffset.x;
textPosition.y = rectangle->rect.y + textOffset.y;

buttonEffects();
}

void Button::render()
{
SDL_RenderCopy(Renderer::getRenderer(), texture, NULL, &textPosition);
}

void Button::buttonEffects()
{
if (!onMouseHold(SDL_BUTTON_LEFT))
{
buttonCurrentColor = buttonColors.color;
textCurrentColor = textColors.color;
}

if (buttonHoverEffect)
{
if (onMouseHover())
{
buttonCurrentColor = buttonColors.hoverColor;
textCurrentColor = textColors.hoverColor;
}
}

if (buttonPressEffect)
{
if (onMouseHold(SDL_BUTTON_LEFT))
{
buttonCurrentColor = buttonColors.pressColor;
textCurrentColor = textColors.pressColor;
}
}

rectangle->r = buttonCurrentColor.r;
rectangle->g = buttonCurrentColor.g;
rectangle->b = buttonCurrentColor.b;
rectangle->a = buttonCurrentColor.a;

SDL_SetTextureColorMod(texture, textCurrentColor.r, textCurrentColor.g, textCurrentColor.b);
SDL_SetTextureAlphaMod(texture, textCurrentColor.a);
}

void Button::setText(const char* text)
{
this->text = text;

updateText();
}

void Button::setTextAlignment(const TextAlignment &textAlignment)
{
this->textAlignment = textAlignment;

updateText();
}

void Button::setFont(const char *font)
{
this->font = font;

updateText();
}

void Button::setTextColor(const SDL_Color &color)
{
textColors.color = color;
}

void Button::setTextHoverColor(const SDL_Color &color)
{
textColors.hoverColor = color;
}

void Button::setTextPressColor(const SDL_Color &color)
{
textColors.pressColor = color;
}

Size Button::getTextSize()
{
Size size;

TTF_SizeText(ResourceManager::getFont(font), text, &size.w, &size.h);

return size;
}

void Button::setButtonPressEffect(const bool buttonPressEffect)
{
this->buttonPressEffect = buttonPressEffect;
}

void Button::setButtonHoverEffect(const bool buttonHoverEffect)
{
this->buttonHoverEffect = buttonHoverEffect;
}

void Button::setButtonColor(const SDL_Color &color)
{
buttonColors.color = color;
}

void Button::setButtonHoverColor(const SDL_Color &color)
{
buttonColors.hoverColor = color;
}

void Button::setButtonPressColor(const SDL_Color &color)
{
buttonColors.pressColor = color;
}

bool Button::onMouseEnter()
{
return boxCollider->onMouseEnter();
}

bool Button::onMouseExit()
{
return boxCollider->onMouseExit();
}

bool Button::onMouseHold(const int button)
{
return boxCollider->onMouseHold(button);
}

bool Button::onMouseHover()
{
return boxCollider->isHovering();
}

bool Button::onMouseDown(const int button)
{
return boxCollider->onMouseDown(button);
}

bool Button::onMouseUp(const int button)
{
return boxCollider->onMouseUp(button);
}

void Button::updateText()
{
switch (textAlignment)
{
case TextAlignment::TOP_LEFT:
textOffset.x = 0;
textOffset.y = 0;
break;
case TextAlignment::TOP_CENTER:
textOffset.x = (buttonSize.w / 2) - (getTextSize().w / 2);
textOffset.y = 0;
break;
case TextAlignment::TOP_RIGHT:
textOffset.x = buttonSize.w - getTextSize().w;
textOffset.y = 0;
break;
case TextAlignment::MIDDLE_LEFT:
textOffset.x = 0;
textOffset.y = (buttonSize.h / 2) - (getTextSize().h / 2);
break;
case TextAlignment::MIDDLE_CENTER:
textOffset.x = (buttonSize.w / 2) - (getTextSize().w / 2);
textOffset.y = (buttonSize.h / 2) - (getTextSize().h / 2);
break;
case TextAlignment::MIDDLE_RIGHT:
textOffset.x = buttonSize.w - getTextSize().w;
textOffset.y = (buttonSize.h / 2) - (getTextSize().h / 2);
break;
case TextAlignment::BOTTOM_LEFT:
textOffset.x = 0;
textOffset.y = buttonSize.h - getTextSize().h;
break;
case TextAlignment::BOTTOM_CENTER:
textOffset.x = (buttonSize.w / 2) - (getTextSize().w / 2);
textOffset.y = buttonSize.h - getTextSize().h;
break;
case TextAlignment::BOTTOM_RIGHT:
textOffset.x = buttonSize.w - getTextSize().w;
textOffset.y = buttonSize.h - getTextSize().h;
break;
}

SDL_Surface *surface = TTF_RenderText_Blended(ResourceManager::getFont(font), text, textCurrentColor);

texture = SDL_CreateTextureFromSurface(Renderer::getRenderer(), surface);

SDL_FreeSurface(surface);

SDL_QueryTexture(texture, nullptr, nullptr, &textPosition.w, &textPosition.h);
}
86 changes: 86 additions & 0 deletions src/wase/ecs/components/ui/button.h
@@ -0,0 +1,86 @@
#pragma once

#include "../../component.h"
#include "../transform.h"
#include "../colliders/box_collider_2d.h"
#include "../../../core/draw.h"
#include "../../../core/size.h"

enum TextAlignment
{
TOP_LEFT,
TOP_CENTER,
TOP_RIGHT,
MIDDLE_LEFT,
MIDDLE_CENTER,
MIDDLE_RIGHT,
BOTTOM_LEFT,
BOTTOM_CENTER,
BOTTOM_RIGHT
};

struct MouseStateColors
{
SDL_Color color, hoverColor, pressColor;
};

class Button : public Component
{
public:
Button(const char* text, const Size& buttonSize, const char* font, TextAlignment textAlignment, const SDL_Color& textColor, const SDL_Color& buttonColor);

void start() override;
void update(float dt) override;
void render() override;

void setText(const char* text);
void setTextAlignment(const TextAlignment &textAlignment);
void setFont(const char* font);
void setTextColor(const SDL_Color& color);
void setTextHoverColor(const SDL_Color& color);
void setTextPressColor(const SDL_Color& color);
Size getTextSize();

void setButtonPressEffect(const bool buttonPressEffect);
void setButtonHoverEffect(const bool buttonHoverEffect);
void setButtonColor(const SDL_Color& color);
void setButtonHoverColor(const SDL_Color& color);
void setButtonPressColor(const SDL_Color& color);

bool onMouseEnter();
bool onMouseExit();
bool onMouseHold(const int button);
bool onMouseHover();
bool onMouseDown(const int button);
bool onMouseUp(const int button);

private:
Camera* camera = nullptr;
Transform* transform = nullptr;
Transform* parentTransform = nullptr;
BoxCollider2D* boxCollider = nullptr;

const char* text;
const char* font = nullptr;
SDL_Rect textPosition;
Vector2 textOffset;
Size buttonSize;
SDL_Texture* texture = nullptr;

bool buttonPressEffect = false;
bool buttonHoverEffect = false;
SDL_Color textCurrentColor;
MouseStateColors textColors;

SDL_Color buttonCurrentColor;
MouseStateColors buttonColors;

TextAlignment textAlignment;

SDL_Rect rect;
Rectangle* rectangle = nullptr;

private:
void buttonEffects();
void updateText();
};

0 comments on commit bb1206e

Please sign in to comment.