aboutsummaryrefslogtreecommitdiffstats
path: root/src/ui.c
blob: 5fab8a64b6f851ce8378207f0d122f1e8f858836 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "ui.h"
#include "game.h"
#include "util.h"

TexturedButton createTexturedButton(Texture* texture, Rectangle rect, const char* message,
                                    Color backgroundColor, Color foregroundColor)
{
    TexturedButton button;
    button.texture = texture;
    button.rect = rect;

    strncpy((char*)button.message, message, UI_BUTTON_NAME_MAX);
    button.messageLength = strnlen(button.message, UI_BUTTON_NAME_MAX);

    button.backgroundColor = backgroundColor;
    button.foregroundColor = foregroundColor;
    button.fontSize = UI_BUTTON_DEFAULT_FONT_SIZE;
    button.isPressed = false;

    return button;
}

bool updateTexturedButton(TexturedButton* button)
{
    // Draw the button.
    DrawTexturePro(
        *button->texture,
        (Rectangle){0.0, 0.0, button->texture->width, button->texture->height},
        button->rect,
        Vector2Zero(),
        0.0,
        button->backgroundColor
    );

    // Draw text centered in button if we have a message.
    if (button->message[0] != '\0')
    {
        DrawText(
            button->message,
            button->rect.x + (button->rect.width / 2.0 - (button->messageLength * button->fontSize / 4.0)),
            button->rect.y + (button->rect.height / 2.0 - button->fontSize / 2.0),
            button->fontSize,
            button->foregroundColor
        );
    }

    button->isPressed = false;

    // Outline and detect click stuff.
    if (CheckCollisionPointRec(getScaledMousePosition(), button->rect))
    {
        // Draw outline thingy.
        if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
        {
            DrawRectangleLinesEx(button->rect, 2, button->foregroundColor);
        }

        // Is clicked.
        if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
        {
            button->isPressed = true;
        }
    }

    return button->isPressed;
}