LumaShow/src/ui.cpp

53 lines
1.7 KiB
C++
Raw Normal View History

2024-08-15 02:11:50 -06:00
//
// Created by illyum on 8/15/2024.
//
#include "ui.h"
void Button::Draw() {
Color currentColor = isHovered ? hoverColor : color;
DrawRectangleRec(bounds, currentColor);
DrawText(text, bounds.x + 10, bounds.y + 10, 20, textColor);
}
void Button::Update() {
Vector2 mousePoint = GetMousePosition();
isHovered = CheckCollisionPointRec(mousePoint, bounds);
isClicked = isHovered && IsMouseButtonPressed(MOUSE_LEFT_BUTTON);
}
void Slider::Draw() {
DrawRectangleRec(bounds, color);
float knobX = bounds.x + (value - minValue) / (maxValue - minValue) * bounds.width;
DrawRectangle(knobX - 5, bounds.y - 5, 10, bounds.height + 10, knobColor);
}
void Slider::Update() {
static bool isDragging = false;
Vector2 mousePoint = GetMousePosition();
if (CheckCollisionPointRec(mousePoint, bounds) && IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
isDragging = true;
}
if (isDragging) {
float newValue = minValue + (mousePoint.x - bounds.x) / bounds.width * (maxValue - minValue);
value = Clamp(newValue, minValue, maxValue);
if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) {
isDragging = false;
}
}
// This will make the slider stop updating when the mouse leave's the bounds of the slider
// Vector2 mousePoint = GetMousePosition();
// if (CheckCollisionPointRec(mousePoint, bounds) && IsMouseButtonDown(MOUSE_LEFT_BUTTON)) {
// float newValue = minValue + (mousePoint.x - bounds.x) / bounds.width * (maxValue - minValue);
// value = Clamp(newValue, minValue, maxValue);
// }
}
float Clamp(float value, float min, float max) {
if (value < min) return min;
if (value > max) return max;
return value;
}