Skip to content

Commit

Permalink
refactor: split out gui.cpp into separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
f0e committed Nov 25, 2024
1 parent 48fe7b0 commit 5872ee9
Show file tree
Hide file tree
Showing 17 changed files with 462 additions and 534 deletions.
1 change: 1 addition & 0 deletions .clang-format-ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/gui/resources/fonts.h
93 changes: 0 additions & 93 deletions src/gui/console.cpp

This file was deleted.

13 changes: 0 additions & 13 deletions src/gui/console.h

This file was deleted.

31 changes: 31 additions & 0 deletions src/gui/drag_handler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

#include "drag_handler.h"
#include "tasks.h"

void gui::drag_handler::DragTarget::dragEnter(os::DragEvent& ev) {
// v.dropResult(os::DropOperation::None); // TODO: what does this do? is it needed?

drag_position = ev.position();
dragging = true;
}

void gui::drag_handler::DragTarget::dragLeave(os::DragEvent& ev) {
// todo: not triggering on windows?
drag_position = ev.position();
dragging = false;
}

void gui::drag_handler::DragTarget::drag(os::DragEvent& ev) {
drag_position = ev.position();
}

void gui::drag_handler::DragTarget::drop(os::DragEvent& ev) {
ev.acceptDrop(true);

if (ev.dataProvider()->contains(os::DragDataItemType::Paths)) {
std::vector<std::string> paths = ev.dataProvider()->getPaths();
tasks::add_files(paths);
}

dragging = false;
}
15 changes: 15 additions & 0 deletions src/gui/drag_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

#pragma once

namespace gui::drag_handler {
inline bool dragging = false;
inline gfx::Point drag_position;

class DragTarget : public os::DragTarget {
public:
void dragEnter(os::DragEvent& ev) override;
void dragLeave(os::DragEvent& ev) override;
void drag(os::DragEvent& ev) override;
void drop(os::DragEvent& ev) override;
};
}
81 changes: 81 additions & 0 deletions src/gui/event_handler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@

#include "event_handler.h"

#include "gui.h"

#include "ui/keys.h"

bool gui::event_handler::process_event(const os::Event& ev) {
switch (ev.type()) {
case os::Event::CloseApp:
case os::Event::CloseWindow: {
closing = true;
return false;
}

case os::Event::ResizeWindow:
break;

case os::Event::MouseMove: {
keys::on_mouse_move(
ev.position(),
ev.modifiers(),
ev.pointerType(),
ev.pressure()
);
return true;
}

case os::Event::MouseDown: {
keys::on_mouse_down(
ev.position(),
ev.button(),
ev.modifiers(),
ev.pointerType(),
ev.pressure()
);
return true;
}

case os::Event::MouseUp: {
keys::on_mouse_up(
ev.position(),
ev.button(),
ev.modifiers(),
ev.pointerType()
);
return true;
}

default:
break;
}

return false;
}

bool gui::event_handler::generate_messages_from_os_events(bool rendered_last) { // https://github.com/aseprite/aseprite/blob/45c2a5950445c884f5d732edc02989c3fb6ab1a6/src/ui/manager.cpp#L393
bool processed_an_event = false;

double timeout;
if (rendered_last) // an animation is playing or something, so be quick
timeout = 0.0;
else // nothing's happening so take your time - but still be fast enough for the ui to update occasionally to see if it wants to render
timeout = 1.f / 60;

while (true) {
os::Event event;

event_queue->getEvent(event, timeout);

if (event.type() == os::Event::None)
break;

if (process_event(event))
processed_an_event = true;

timeout = 0.0; // i think this is correct, allow blocking for first event but any subsequent one should be instant
}

return processed_an_event;
}
7 changes: 7 additions & 0 deletions src/gui/event_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

#pragma once

namespace gui::event_handler {
bool process_event(const os::Event& ev);
bool generate_messages_from_os_events(bool rendered_last);
}
Loading

0 comments on commit 5872ee9

Please sign in to comment.