Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Helco committed Nov 27, 2018
1 parent 0211fa6 commit 1bed775
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 61 deletions.
30 changes: 11 additions & 19 deletions pcmockup/debugwindow.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "pcmockup.h"
#include "renderer.h"
#include "platform.h"

#include <string.h>

Expand Down Expand Up @@ -27,6 +28,8 @@ SDL_Surface* createSDLSurface(int w, int h, Uint32 format)
return surface;
}

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

DebugWindow* debugWindow_init(WindowContainer* parent, SDL_Rect bounds, const DebugView* view, Renderer* podRenderer)
{
DebugWindow* me = (DebugWindow*)malloc(sizeof(DebugWindow));
Expand All @@ -41,6 +44,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);

me->surface = createSDLSurface(bounds.w, bounds.h, SDL_PIXELFORMAT_ABGR8888);
if (me->surface == NULL)
Expand Down Expand Up @@ -98,27 +102,15 @@ void debugWindow_update(DebugWindow* me)
debugWindow_endUpdate(me);
}

void debugWindow_handleEvent(DebugWindow* me, const SDL_Event* ev)
void debugWindow_onDrag(Window* window, int button, ImVec2 delta, void* userdata)
{
Uint32 windowID = 1234;
if (ev->type == SDL_MOUSEMOTION && ev->motion.windowID == windowID && (ev->motion.state & SDL_BUTTON_LMASK) > 0)
{
xz_t move = xz(real_from_int(ev->motion.xrel), real_from_int(ev->motion.yrel));
move = xz_invScale(move, me->zoom);
me->position = xz_sub(me->position, move);
}
if (ev->type == SDL_MOUSEWHEEL && ev->wheel.windowID == windowID)
{
real_t zoom = real_from_int(ev->wheel.y);
zoom = real_mul(real_div(zoom, real_from_int(10)), me->zoom);
me->zoom = real_add(me->zoom, zoom);
}
if (ev->type == SDL_KEYDOWN && ev->key.windowID == windowID && ev->key.keysym.sym == SDLK_r)
{
me->position = xz(real_zero, real_zero);
}
UNUSED(window);
if (button != 0)
return;
DebugWindow* me = (DebugWindow*)userdata;
xz_t move = xz_invScale((xz_t) { real_from_int(delta.x), real_from_int(delta.y) }, me->zoom);
me->position = xz_sub(me->position, move);

// update render offset
xz_t halfSize = xz(real_from_int(me->surface->w / 2), real_from_int(me->surface->h / 2));
me->offset = xz_sub(
xz_invScale(halfSize, me->zoom),
Expand Down
6 changes: 0 additions & 6 deletions pcmockup/debugwindowset.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,3 @@ void debugWindowSet_updateMenubar(DebugWindowSet* me)
igEndMenu();
#endif
}

void debugWindowSet_handleEvent(DebugWindowSet* me, const SDL_Event* ev)
{
for (int i = 0; i < me->count; i++)
debugWindow_handleEvent(me->windows[i], ev);
}
18 changes: 10 additions & 8 deletions pcmockup/imagewindow.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <glad/glad.h>
#include "cimgui.include.h"
#include <float.h>
#include "platform.h"

struct ImageWindow
{
Expand All @@ -15,8 +16,8 @@ struct ImageWindow

const Uint32 imageWindow_SDLPixelFormat = SDL_PIXELFORMAT_ABGR8888;

void imageWindow_beforeUpdate(Window* me);
void imageWindow_contentUpdate(Window* me);
void imageWindow_beforeUpdate(Window* me, void* userdata);
void imageWindow_contentUpdate(Window* me, void* userdata);

ImageWindow* imageWindow_init(WindowContainer* parent, const char* title, GRect initialBounds, bool_t isEssential)
{
Expand All @@ -32,7 +33,6 @@ ImageWindow* imageWindow_init(WindowContainer* parent, const char* title, GRect
imageWindow_free(me);
return NULL;
}
window_setUserdata(me->window, me);
window_setInitialBounds(me->window, initialBounds);
window_setOpenState(me->window, isEssential ? WindowOpenState_Unclosable : WindowOpenState_Open);
window_setFlags(me->window,
Expand All @@ -41,7 +41,8 @@ ImageWindow* imageWindow_init(WindowContainer* parent, const char* title, GRect
ImGuiWindowFlags_NoCollapse);
window_setUpdateCallbacks(me->window, (WindowUpdateCallbacks) {
.before = imageWindow_beforeUpdate,
.content = imageWindow_contentUpdate
.content = imageWindow_contentUpdate,
.userdata = me
});

glGenTextures(1, &me->textureID);
Expand Down Expand Up @@ -102,9 +103,10 @@ void imageWindow_constrainWindowSize(ImGuiSizeCallbackData* data)
data->DesiredSize = byWidthArea > byHeightArea ? byWidth : byHeight;
}

void imageWindow_beforeUpdate(Window* window)
void imageWindow_beforeUpdate(Window* window, void* userdata)
{
ImageWindow* me = (ImageWindow*)window_getUserdata(window);
UNUSED(window);
ImageWindow* me = (ImageWindow*)userdata;
const ImVec2
zero = { 0, 0 },
minSize = { (float)me->imageSize.w, (float)me->imageSize.h },
Expand All @@ -113,9 +115,9 @@ void imageWindow_beforeUpdate(Window* window)
igPushStyleVarVec2(ImGuiStyleVar_WindowPadding, zero); // space between image and window border
}

void imageWindow_contentUpdate(Window* window)
void imageWindow_contentUpdate(Window* window, void* userdata)
{
const ImageWindow* me = (const ImageWindow*)window_getUserdata(window);
const ImageWindow* me = (const ImageWindow*)userdata;
const GSize windowSize = window_getBounds(window).size;
const ImVec2
zero = { 0, 0 },
Expand Down
1 change: 0 additions & 1 deletion pcmockup/pcmockup.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ void pcmockup_update(PCMockup *me)
break;
}
}
debugWindowSet_handleEvent(me->debugWindowSet, &event);
windowContainer_handleEvent(me->windowContainer, &event);
}
}
Expand Down
2 changes: 0 additions & 2 deletions pcmockup/pcmockup.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ typedef struct DebugWindow DebugWindow;
DebugWindow* debugWindow_init(WindowContainer* parent, SDL_Rect bounds, const DebugView* debugView, Renderer* renderer);
void debugWindow_free(DebugWindow* window);
void debugWindow_update(DebugWindow* window);
void debugWindow_handleEvent(DebugWindow* window, const SDL_Event* ev);
const DebugView* debugWindow_getDebugView(const DebugWindow* window);
ImageWindow* debugWindow_asImageWindow(DebugWindow* window);

Expand All @@ -45,7 +44,6 @@ DebugWindowSet* debugWindowSet_init(WindowContainer* parent, const WindowGrid* g
void debugWindowSet_free(DebugWindowSet* set);
void debugWindowSet_update(DebugWindowSet* set);
void debugWindowSet_updateMenubar(DebugWindowSet* me);
void debugWindowSet_handleEvent(DebugWindowSet* set, const SDL_Event* ev);

typedef struct PCMockup PCMockup;
PCMockup* pcmockup_init();
Expand Down
29 changes: 10 additions & 19 deletions pcmockup/window.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct Window
bool wasDragging[MOUSE_BUTTON_COUNT];
GPoint draggingOrigin[MOUSE_BUTTON_COUNT];

void* userdata;
void* onDragUserdata;
WindowUpdateCallbacks onUpdate;
WindowDragCallback onDrag;
WindowKeyCallbacks onKey;
Expand Down Expand Up @@ -47,7 +47,7 @@ void window_update(Window* me)
if (me->openState == WindowOpenState_Closed)
return;
if (me->onUpdate.before != NULL)
me->onUpdate.before(me);
me->onUpdate.before(me, me->onUpdate.userdata);

bool isOpen = (me->openState == WindowOpenState_Open);
bool* isOpenPtr = me->openState == WindowOpenState_Unclosable ? NULL : &isOpen;
Expand All @@ -58,17 +58,17 @@ void window_update(Window* me)
igBegin(me->title, isOpenPtr, me->flags);
igGetCursorScreenPos_nonUDT(&me->currentPos); // handles toolbar height
igGetWindowSize_nonUDT(&me->currentSize);
me->isFocused = igIsWindowFocused(ImGuiFocusedFlags_AnyWindow);
me->isFocused = igIsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows);

if (me->onUpdate.content != NULL)
me->onUpdate.content(me);
me->onUpdate.content(me, me->onUpdate.userdata);

igEnd();
if (isOpenPtr != NULL)
me->openState = isOpen ? WindowOpenState_Open : WindowOpenState_Closed;

if (me->onUpdate.after != NULL)
me->onUpdate.after(me);
me->onUpdate.after(me, me->onUpdate.userdata);
}

void window_handleDragEvent(Window* me)
Expand All @@ -79,16 +79,16 @@ void window_handleDragEvent(Window* me)
continue;
ImVec2 delta;
igGetMouseDragDelta_nonUDT(&delta, button, -1.0f);
me->onDrag(me, button, delta);
me->onDrag(me, button, delta, me->onDragUserdata);
}
}

void window_handleKeyEvent(Window* me, SDL_Keysym sym, bool isDown)
{
if (isDown && me->onKey.down != NULL)
me->onKey.down(me, sym);
me->onKey.down(me, sym, me->onKey.userdata);
if (!isDown && me->onKey.up != NULL)
me->onKey.up(me, sym);
me->onKey.up(me, sym, me->onKey.userdata);
}

GRect window_getBounds(const Window* me)
Expand All @@ -109,16 +109,6 @@ bool window_isFocused(const Window* me)
return me->isFocused;
}

void* window_getUserdata(const Window* me)
{
return me->userdata;
}

void window_setUserdata(Window* me, void* userdata)
{
me->userdata = userdata;
}

void window_setTitle(Window* me, const char* title)
{
if (me->title != NULL)
Expand Down Expand Up @@ -151,9 +141,10 @@ void window_setUpdateCallbacks(Window* me, WindowUpdateCallbacks callbacks)
me->onUpdate = callbacks;
}

void window_setDragCallback(Window* me, WindowDragCallback callback)
void window_setDragCallback(Window* me, WindowDragCallback callback, void* userdata)
{
me->onDrag = callback;
me->onDragUserdata = userdata;
}

void window_setKeyCallbacks(Window* me, WindowKeyCallbacks callbacks)
Expand Down
12 changes: 6 additions & 6 deletions pcmockup/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,23 @@
#include "sdl.include.h"

typedef struct Window Window;
typedef void (*WindowUpdateCallback)(Window* window);
typedef void (*WindowUpdateCallback)(Window* window, void* userdata);
typedef struct WindowUpdateCallbacks
{
WindowUpdateCallback
before,
content,
after;
void* userdata;
} WindowUpdateCallbacks;
typedef void (*WindowDragCallback)(Window* window, int mouseKey, ImVec2 delta);
typedef void (*WindowKeyCallback)(Window* window, SDL_Keysym sym);
typedef void (*WindowDragCallback)(Window* window, int mouseKey, ImVec2 delta, void* userdata);
typedef void (*WindowKeyCallback)(Window* window, SDL_Keysym sym, void* userdata);
typedef struct WindowKeyCallbacks
{
WindowKeyCallback
down,
up;
void* userdata;
} WindowKeyCallbacks;

typedef enum WindowOpenState
Expand All @@ -36,14 +38,12 @@ Uint32 getWindowIDByEvent(const SDL_Event* ev);
GRect window_getBounds(const Window* window);
bool window_isFocused(const Window* window);
WindowOpenState window_getOpenState(const Window* window);
void* window_getUserdata(const Window* window);
void window_setUserdata(Window* window, void* userdata);
void window_setTitle(Window* window, const char* title);
void window_setFlags(Window* window, ImGuiWindowFlags flags);
void window_setOpenState(Window* window, WindowOpenState state);
void window_setInitialBounds(Window* window, GRect bounds);
void window_setUpdateCallbacks(Window* window, WindowUpdateCallbacks callbacks);
void window_setDragCallback(Window* window, WindowDragCallback callback);
void window_setDragCallback(Window* window, WindowDragCallback callback, void* userdata);
void window_setKeyCallbacks(Window* window, WindowKeyCallbacks callbacks);

typedef struct WindowContainer WindowContainer;
Expand Down

0 comments on commit 1bed775

Please sign in to comment.