Skip to content

Commit

Permalink
pcmockup: reintroduce dragging with MMB
Browse files Browse the repository at this point in the history
  • Loading branch information
Helco committed Dec 7, 2018
1 parent 1f9ba44 commit 05aaf48
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 3 additions & 1 deletion pcmockup/debugwindow.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ SDL_Surface* createSDLSurface(int w, int h, Uint32 format)
return surface;
}

void debugWindow_onDrag(Window* window, int button, ImVec2 delta, void* userdata);
void debugWindow_onKeyDown(Window* window, SDL_Keysym sym, void* userdata);

DebugWindow* debugWindow_init(WindowContainer* parent, SDL_Rect bounds, const DebugView* view, Renderer* podRenderer)
Expand All @@ -44,6 +45,7 @@ DebugWindow* debugWindow_init(WindowContainer* parent, SDL_Rect bounds, const De
debugWindow_free(me);
return NULL;
}
window_setDragCallback(imageWindow_asWindow(me->window), debugWindow_onDrag, me);
window_setKeyCallbacks(imageWindow_asWindow(me->window), (WindowKeyCallbacks) {
.down = debugWindow_onKeyDown,
.userdata = me
Expand Down Expand Up @@ -117,7 +119,7 @@ void debugWindow_updateOffset(DebugWindow* me)
void debugWindow_onDrag(Window* window, int button, ImVec2 delta, void* userdata)
{
UNUSED(window);
if (button != 0)
if (button != 2) // middle mouse button
return;
DebugWindow* me = (DebugWindow*)userdata;
xz_t move = xz_invScale((xz_t) { real_from_int(delta.x), real_from_int(delta.y) }, me->zoom);
Expand Down
12 changes: 11 additions & 1 deletion pcmockup/window.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,21 @@ void window_update(Window* me)
me->onUpdate.after(me, me->onUpdate.userdata);
}

static bool window_shouldHandleDrag(const Window* me, int button)
{
if (!igIsMouseDragging(button, -1.0f) || me->onDrag == NULL)
return false;
const ImVec2 dragSource = igGetIO()->MouseClickedPos[button];
return
dragSource.x >= me->currentPos.x && dragSource.x < me->currentPos.x + me->currentSize.x &&
dragSource.y >= me->currentPos.y && dragSource.y < me->currentPos.y + me->currentSize.y;
}

void window_handleDragEvent(Window* me)
{
for (int button = 0; button < 4; button++)
{
if (!igIsMouseDragging(button, -1.0f) || me->onDrag == NULL)
if (!window_shouldHandleDrag(me, button))
{
me->lastDragDelta[button] = (ImVec2) { 0, 0 };
continue;
Expand Down

0 comments on commit 05aaf48

Please sign in to comment.