EditorSDL
This commit is contained in:
49
editor/button.cpp
Normal file
49
editor/button.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
#include "Button.h"
|
||||
|
||||
Button::Button(float x, float y, float w, float h) {
|
||||
rect = {x, y, w, h};
|
||||
hovered = false;
|
||||
pressed = false;
|
||||
clicked = false;
|
||||
}
|
||||
|
||||
void Button::updateHover(float mouseX, float mouseY) {
|
||||
// Corrected logic: check mouse against x + width and y + height
|
||||
hovered = (mouseX >= rect.x && mouseX <= rect.x + rect.w &&
|
||||
mouseY >= rect.y && mouseY <= rect.y + rect.h);
|
||||
}
|
||||
|
||||
void Button::handleEvent(const SDL_Event& e) {
|
||||
clicked = false; // Reset every frame
|
||||
|
||||
if (e.type == SDL_EVENT_MOUSE_MOTION) {
|
||||
updateHover(e.motion.x, e.motion.y);
|
||||
}
|
||||
|
||||
if (e.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
|
||||
if (e.button.button == SDL_BUTTON_LEFT && hovered) {
|
||||
pressed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (e.type == SDL_EVENT_MOUSE_BUTTON_UP) {
|
||||
if (e.button.button == SDL_BUTTON_LEFT) {
|
||||
// Only a "click" if we release while still over the button
|
||||
if (pressed && hovered) {
|
||||
clicked = true;
|
||||
}
|
||||
pressed = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Button::render(SDL_Renderer* renderer) {
|
||||
if (pressed)
|
||||
SDL_SetRenderDrawColor(renderer, 100, 100, 100, 255); // Darkest
|
||||
else if (hovered)
|
||||
SDL_SetRenderDrawColor(renderer, 180, 180, 180, 255); // Lightest
|
||||
else
|
||||
SDL_SetRenderDrawColor(renderer, 140, 140, 140, 255); // Default
|
||||
|
||||
SDL_RenderFillRect(renderer, &rect);
|
||||
}
|
||||
Reference in New Issue
Block a user