make devices movable
This commit is contained in:
parent
7465b18ae8
commit
c169fab420
|
@ -13,8 +13,11 @@ public:
|
|||
void Render(SDL_Renderer* renderer) const;
|
||||
void Deselect();
|
||||
|
||||
bool PropagateEvent(SDL_Event* event);
|
||||
|
||||
private:
|
||||
SDL_FRect bbox;
|
||||
bool selected;
|
||||
bool grabbed;
|
||||
std::shared_ptr<Device> device;
|
||||
};
|
|
@ -11,6 +11,8 @@ public:
|
|||
void AddDevice(std::shared_ptr<Device> device, uint16_t x, uint16_t y);
|
||||
void Render(SDL_Renderer* renderer) const;
|
||||
|
||||
void PropagateEvent(SDL_Event* event);
|
||||
|
||||
private:
|
||||
Network& network;
|
||||
std::vector<DeviceRenderer> devices;
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
#include <SDL.h>
|
||||
|
||||
DeviceRenderer::DeviceRenderer(std::shared_ptr<Device> device, uint16_t x, uint16_t y) :
|
||||
device(device), selected(false)
|
||||
device(device), selected(false), grabbed(false)
|
||||
{
|
||||
bbox.x = x;
|
||||
bbox.y = y;
|
||||
|
@ -15,6 +15,35 @@ void DeviceRenderer::Deselect() {
|
|||
selected = false;
|
||||
}
|
||||
|
||||
bool DeviceRenderer::PropagateEvent(SDL_Event* event) {
|
||||
if (event->type == SDL_EVENT_MOUSE_BUTTON_UP) {
|
||||
if (event->button.button == SDL_BUTTON_LEFT) {
|
||||
grabbed = false;
|
||||
}
|
||||
}
|
||||
else if (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
|
||||
float x, y;
|
||||
SDL_GetMouseState(&x, &y);
|
||||
|
||||
if (x >= bbox.x && x <= bbox.x + bbox.w && y >= bbox.y && y <= bbox.y + bbox.h) {
|
||||
selected = true;
|
||||
grabbed = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
} else if (grabbed && event->type == SDL_EVENT_MOUSE_MOTION) {
|
||||
float x, y;
|
||||
SDL_GetMouseState(&x, &y);
|
||||
|
||||
bbox.x += event->motion.xrel;
|
||||
bbox.y += event->motion.yrel;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void DeviceRenderer::Render(SDL_Renderer* renderer) const {
|
||||
SDL_SetRenderDrawColor(renderer, 150, 150, 150, 255);
|
||||
SDL_RenderFillRect(renderer, &bbox);
|
||||
|
|
|
@ -12,6 +12,20 @@ void NetworkRenderer::AddDevice(std::shared_ptr<Device> device, uint16_t x, uint
|
|||
devices.push_back(DeviceRenderer(device, x, y));
|
||||
}
|
||||
|
||||
void NetworkRenderer::PropagateEvent(SDL_Event* event) {
|
||||
if (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
|
||||
for (DeviceRenderer& device : devices) {
|
||||
device.Deselect();
|
||||
}
|
||||
}
|
||||
|
||||
for (auto it = devices.rbegin(); it != devices.rend(); it++) {
|
||||
if (it->PropagateEvent(event)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkRenderer::Render(SDL_Renderer* renderer) const {
|
||||
for (const DeviceRenderer& device : devices) {
|
||||
device.Render(renderer);
|
||||
|
|
|
@ -36,6 +36,7 @@ int main(int argc, char** argv) {
|
|||
while (!shouldClose) {
|
||||
while (SDL_PollEvent(&event)) {
|
||||
ImGui_ImplSDL3_ProcessEvent(&event);
|
||||
networkRenderer.PropagateEvent(&event);
|
||||
|
||||
switch (event.type) {
|
||||
case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
|
||||
|
|
Loading…
Reference in a new issue