diff --git a/src/editor.c b/src/editor.c index 3becdc32..24c80e2d 100644 --- a/src/editor.c +++ b/src/editor.c @@ -27,6 +27,8 @@ #define ED_GL_BACKGROUND_COLOR GLW_BACKGROUND_COLOR +#define ED_BONE_COLOR 0xffff00 + typedef struct editor { glwWin *win; float cameraAngleX; @@ -34,20 +36,37 @@ typedef struct editor { float cameraDistance; int newImId; int showBoneChecked; + int showBallChecked; + int showMeshChecked; int width; int height; int mode; - int glLeft; - int glTop; - int glWidth; - int glHeight; - int mouseX; - int mouseY; + int renderLeft; + int renderTop; + int renderWidth; + int renderHeight; + int moveMouseX; + int moveMouseY; bmesh *bm; } editor; #include "../data/bmesh_test_2.h" +static int mouseInRenderRect(glwWin *win) { + editor *ed = glwGetUserData(win); + return glwPointTest(glwMouseX(win), glwMouseY(win), + ed->renderLeft, ed->renderTop, + ed->renderWidth, ed->renderHeight, 0); +} + +static int drawBmeshBone(bmesh *bm, bmeshBone *bone) { + glColor3f(glwR(ED_BONE_COLOR), glwG(ED_BONE_COLOR), glwB(ED_BONE_COLOR)); + bmeshBall *firstBall = bmeshGetBall(bm, bone->firstBallIndex); + bmeshBall *secondBall = bmeshGetBall(bm, bone->secondBallIndex); + drawCylinder(&firstBall->position, &secondBall->position, 0.1, 36, 24); + return 0; +} + static void display(glwWin *win) { editor *ed = glwGetUserData(win); @@ -85,15 +104,25 @@ static void display(glwWin *win) { int glWinY = glwImNextY(win); int bottomY = y + height - ED_TOOLBAR_HEIGHT; glwImBottomBar(win, GEN_ID, x, bottomY, width, ED_TOOLBAR_HEIGHT); - ed->showBoneChecked = glwImCheck(win, GEN_ID, x + ED_SPACING + ED_SPACING, + ed->showBallChecked = glwImCheck(win, GEN_ID, x + ED_SPACING + ED_SPACING, bottomY + (ED_TOOLBAR_HEIGHT - glwImLineHeight(win)) / 2 + 2, - "Show Bone", + "Ball", + ed->showBallChecked); + ed->showBoneChecked = glwImCheck(win, GEN_ID, + glwImNextX(win) + ED_SPACING, + bottomY + (ED_TOOLBAR_HEIGHT - glwImLineHeight(win)) / 2 + 2, + "Bone", ed->showBoneChecked); + ed->showMeshChecked = glwImCheck(win, GEN_ID, + glwImNextX(win) + ED_SPACING, + bottomY + (ED_TOOLBAR_HEIGHT - glwImLineHeight(win)) / 2 + 2, + "Mesh", + ed->showMeshChecked); - ed->glLeft = x + 1; - ed->glTop = glWinY; - ed->glWidth = width - 3; - ed->glHeight = bottomY - glWinY; + ed->renderLeft = x + 1; + ed->renderTop = glWinY; + ed->renderWidth = width - 3; + ed->renderHeight = bottomY - glWinY; if (0 == ed->bm) { bmeshBall ball; @@ -122,19 +151,19 @@ static void display(glwWin *win) { } glEnable(GL_SCISSOR_TEST); - glScissor(ed->glLeft, ED_SPACING / 2 + ED_TOOLBAR_HEIGHT + 1, - ed->glWidth, ed->glHeight); + glScissor(ed->renderLeft, ED_SPACING / 2 + ED_TOOLBAR_HEIGHT + 1, + ed->renderWidth, ed->renderHeight); glPushMatrix(); glTranslatef(x + 1, glWinY, 0); glColor3f(glwR(ED_GL_BACKGROUND_COLOR), glwG(ED_GL_BACKGROUND_COLOR), glwB(ED_GL_BACKGROUND_COLOR)); - glRecti(0, 0, ed->glWidth, ed->glHeight); + glRecti(0, 0, ed->renderWidth, ed->renderHeight); glMatrixMode(GL_PROJECTION); glLoadIdentity(); - gluPerspective(60.0f, (float)ed->glWidth / ed->glHeight, 1, 1000); + gluPerspective(60.0f, (float)ed->renderWidth / ed->renderHeight, 1, 1000); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glPushMatrix(); @@ -146,7 +175,23 @@ static void display(glwWin *win) { glEnable(GL_LIGHTING); glEnable(GL_CULL_FACE); - bmeshDraw(ed->bm); + + if (ed->showBallChecked) { + + } + + if (ed->showBoneChecked) { + int index; + for (index = 0; index < bmeshGetBoneNum(ed->bm); ++index) { + bmeshBone *bone = bmeshGetBone(ed->bm, index); + drawBmeshBone(ed->bm, bone); + } + } + + if (ed->showMeshChecked) { + bmeshDraw(ed->bm); + } + glDisable(GL_CULL_FACE); glDisable(GL_LIGHTING); @@ -189,41 +234,52 @@ static void reshape(glwWin *win, int width, int height) { static void mouse(glwWin *win, int button, int state, int x, int y){ editor *ed = glwGetUserData(win); - if (!glwPointTest(x, y, ed->glLeft, ed->glTop, - ed->glWidth, ed->glHeight, 0)) { + if (!mouseInRenderRect(win)) { return; } if (GLW_DOWN == state) { - ed->mouseX = x; - ed->mouseY = y; + ed->moveMouseX = x; + ed->moveMouseY = y; } } static void motion(glwWin *win, int x, int y) { editor *ed = glwGetUserData(win); - if (!glwPointTest(x, y, ed->glLeft, ed->glTop, - ed->glWidth, ed->glHeight, 0)) { + if (!mouseInRenderRect(win)) { return; } - ed->cameraAngleY += (x - ed->mouseX); - ed->cameraAngleX += (y - ed->mouseY); - ed->mouseX = x; - ed->mouseY = y; + ed->cameraAngleY += (x - ed->moveMouseX); + ed->cameraAngleX += (y - ed->moveMouseY); + ed->moveMouseX = x; + ed->moveMouseY = y; +} + +static void wheel(glwWin *win, float delta) { + editor *ed = glwGetUserData(win); + if (!mouseInRenderRect(win)) { + return; + } + ed->cameraDistance -= delta * 0.01f; } int main(int argc, char *argv[]) { editor ed; glwInit(); + drawInit(); + memset(&ed, 0, sizeof(ed)); ed.cameraAngleX = 30; ed.cameraAngleY = -312; ed.cameraDistance = 14.4; + ed.showMeshChecked = 1; + ed.win = glwCreateWindow(0, 0, 0, 0); glwSetUserData(ed.win, &ed); glwReshapeFunc(ed.win, reshape); glwDisplayFunc(ed.win, display); glwMouseFunc(ed.win, mouse); glwMotionFunc(ed.win, motion); + glwWheelFunc(ed.win, wheel); glwMainLoop(); return 0; } diff --git a/src/glw.c b/src/glw.c index 9b1ed943..24b25255 100644 --- a/src/glw.c +++ b/src/glw.c @@ -688,3 +688,57 @@ int glwImLineHeight(glwWin *win) { glwSystemFontTexture *systemFontTexture = glwGetSystemFontTexture(win); return systemFontTexture->originSize.height * (1 + GLW_VER_AUTO_MARGIN * 2); } + +void glwSetUserData(glwWin *win, void *userData) { + glwWinContext *ctx = glwGetWinContext(win); + ctx->userData = userData; +} + +void *glwGetUserData(glwWin *win) { + glwWinContext *ctx = glwGetWinContext(win); + return ctx->userData; +} + +void glwDisplayFunc(glwWin *win, void (*func)(glwWin *win)) { + glwWinContext *ctx = glwGetWinContext(win); + ctx->onDisplay = func; +} + +void glwReshapeFunc(glwWin *win, void (*func)(glwWin *win, int width, + int height)) { + glwWinContext *ctx = glwGetWinContext(win); + ctx->onReshape = func; +} + +void glwMouseFunc(glwWin *win, void (*func)(glwWin *win, int button, int state, + int x, int y)) { + glwWinContext *ctx = glwGetWinContext(win); + ctx->onMouse = func; +} + +void glwMotionFunc(glwWin *win, + void (*func)(glwWin *win, int x, int y)) { + glwWinContext *ctx = glwGetWinContext(win); + ctx->onMotion = func; +} + +void glwPassiveMotionFunc(glwWin *win, + void (*func)(glwWin *win, int x, int y)) { + glwWinContext *ctx = glwGetWinContext(win); + ctx->onPassiveMotion = func; +} + +void glwWheelFunc(glwWin *win, void(*func)(glwWin *win, float delta)) { + glwWinContext *ctx = glwGetWinContext(win); + ctx->onWheel = func; +} + +int glwMouseX(glwWin *win) { + glwWinContext *ctx = glwGetWinContext(win); + return ctx->x; +} + +int glwMouseY(glwWin *win) { + glwWinContext *ctx = glwGetWinContext(win); + return ctx->y; +} diff --git a/src/glw.h b/src/glw.h index e4106261..6120e39b 100644 --- a/src/glw.h +++ b/src/glw.h @@ -44,6 +44,7 @@ void glwMouseFunc(glwWin *win, void (*func)(glwWin *win, int button, int state, int x, int y)); void glwMotionFunc(glwWin *win, void (*func)(glwWin *win, int x, int y)); void glwPassiveMotionFunc(glwWin *win, void (*func)(glwWin *win, int x, int y)); +void glwWheelFunc(glwWin *win, void(*func)(glwWin *win, float delta)); #define glwR(rgb) ((float)(((rgb) >> 16) & 0xff) / 255) #define glwG(rgb) ((float)(((rgb) >> 8) & 0xff) / 255) #define glwB(rgb) ((float)(((rgb)) & 0xff) / 255) @@ -62,6 +63,8 @@ int glwImNextY(glwWin *win); int glwImLineHeight(glwWin *win); int glwPointTest(int x, int y, int left, int top, int width, int height, int allowOffset); +int glwMouseX(glwWin *win); +int glwMouseY(glwWin *win); #ifdef __cplusplus } diff --git a/src/glw_internal.h b/src/glw_internal.h index 746467f9..4fe6ab8f 100644 --- a/src/glw_internal.h +++ b/src/glw_internal.h @@ -16,6 +16,22 @@ typedef enum glwCtrlState { GLW_CTRL_STATE_PRESS, } glwCtrlState; +typedef struct glwWinContext { + void (*onReshape)(glwWin *win, int width, int height); + void (*onDisplay)(glwWin *win); + void (*onMouse)(glwWin *win, int button, int state, int x, int y); + void (*onMotion)(glwWin *win, int x, int y); + void (*onPassiveMotion)(glwWin *win, int x, int y); + void(*onWheel)(glwWin *win, float delta); + int viewWidth; + int viewHeight; + float scaleX; + float scaleY; + void *userData; + int x; + int y; +} glwWinContext; + typedef struct glwSystemFontTexture { int texId; glwSize size; @@ -50,5 +66,6 @@ void glwDrawButtonBackground(float x, float y, float width, float height, glwCtrlState state); void glwMouseEvent(glwWin *win, int button, int state, int x, int y); glwImGui *glwGetImGUI(glwWin *win); +glwWinContext *glwGetWinContext(glwWin *win); #endif diff --git a/src/glw_osx.m b/src/glw_osx.m index 2c8107bf..546e5fc1 100644 --- a/src/glw_osx.m +++ b/src/glw_osx.m @@ -6,17 +6,8 @@ @interface GLView : NSOpenGLView { CVDisplayLinkRef displayLink; @public - void (*onReshape)(glwWin *win, int width, int height); - void (*onDisplay)(glwWin *win); - void (*onMouse)(glwWin *win, int button, int state, int x, int y); - void (*onMotion)(glwWin *win, int x, int y); - void (*onPassiveMotion)(glwWin *win, int x, int y); - void *userData; int pendingReshape; - int viewWidth; - int viewHeight; - float scaleX; - float scaleY; + glwWinContext context; glwSystemFontTexture systemFontTexture; glwImGui imGUI; } @@ -38,6 +29,11 @@ glwSystemFontTexture *glwGetSystemFontTexture(glwWin *win) { return &view->systemFontTexture; } +glwWinContext *glwGetWinContext(glwWin *win) { + GLView *view = ((NSWindow *)win).contentView; + return &view->context; +} + @implementation GLView - (id)initWithFrame:(NSRect)frameRect { NSOpenGLPixelFormatAttribute attribs[] = {NSOpenGLPFAMultisample, @@ -82,17 +78,17 @@ glwSystemFontTexture *glwGetSystemFontTexture(glwWin *win) { [currentContext makeCurrentContext]; CGLLockContext([currentContext CGLContextObj]); if (self->pendingReshape) { - if (self->onReshape) { + if (self->context.onReshape) { self->pendingReshape = 0; if (!self->systemFontTexture.texId) { glwInitSystemFontTexture((glwWin *)self.window); } - self->onReshape((glwWin *)self.window, self->viewWidth, - self->viewHeight); + self->context.onReshape((glwWin *)self.window, self->context.viewWidth, + self->context.viewHeight); } } - if (self->onDisplay) { - self->onDisplay((glwWin *)self.window); + if (self->context.onDisplay) { + self->context.onDisplay((glwWin *)self.window); } CGLFlushDrawable([[self openGLContext] CGLContextObj]); CGLUnlockContext([currentContext CGLContextObj]); @@ -116,10 +112,10 @@ glwSystemFontTexture *glwGetSystemFontTexture(glwWin *win) { - (void)reshape { NSRect bounds = [self bounds]; NSRect backingBounds = [self convertRectToBacking:[self bounds]]; - viewWidth = (int)backingBounds.size.width; - viewHeight = (int)backingBounds.size.height; - self->scaleX = (float)viewWidth / bounds.size.width; - self->scaleY = (float)viewHeight / bounds.size.height; + self->context.viewWidth = (int)backingBounds.size.width; + self->context.viewHeight = (int)backingBounds.size.height; + self->context.scaleX = (float)self->context.viewWidth / bounds.size.width; + self->context.scaleY = (float)self->context.viewHeight / bounds.size.height; self->pendingReshape = 1; [self drawFrame]; } @@ -140,49 +136,66 @@ glwSystemFontTexture *glwGetSystemFontTexture(glwWin *win) { CGLUnlockContext([currentContext CGLContextObj]); } -- (void)mouseMoved:(NSEvent*)event { +- (void)saveMousePosFromEvent:(NSEvent*)event { NSPoint loc = [self convertPoint:[event locationInWindow] fromView:nil]; - int x = loc.x * self->scaleX; - int y = loc.y * self->scaleY; + self->context.x = loc.x * self->context.scaleX; + self->context.y = loc.y * self->context.scaleY; +} + +- (void)mouseMoved:(NSEvent*)event { + [self saveMousePosFromEvent:event]; if (GLW_DOWN == self->imGUI.mouseState) { - if (self->onMotion) { - self->onMotion((glwWin *)self.window, x, y); + if (self->context.onMotion) { + self->context.onMotion((glwWin *)self.window, + self->context.x, self->context.y); } } else { - if (self->onPassiveMotion) { - self->onPassiveMotion((glwWin *)self.window, x, y); + if (self->context.onPassiveMotion) { + self->context.onPassiveMotion((glwWin *)self.window, + self->context.x, self->context.y); } } } - (void)mouseDown:(NSEvent*)event { - NSPoint loc = [self convertPoint:[event locationInWindow] fromView:nil]; - int x = loc.x * self->scaleX; - int y = loc.y * self->scaleY; - glwMouseEvent((glwWin *)self.window, GLW_LEFT_BUTTON, GLW_DOWN, x, y); - if (self->onMouse) { - self->onMouse((glwWin *)self.window, GLW_LEFT_BUTTON, GLW_DOWN, x, y); + [self saveMousePosFromEvent:event]; + glwMouseEvent((glwWin *)self.window, GLW_LEFT_BUTTON, GLW_DOWN, + self->context.x, self->context.y); + if (self->context.onMouse) { + self->context.onMouse((glwWin *)self.window, + GLW_LEFT_BUTTON, GLW_DOWN, + self->context.x, self->context.y); + } +} + +- (void)scrollWheel:(NSEvent *)event { + [self saveMousePosFromEvent:event]; + if (self->context.onWheel) { + self->context.onWheel((glwWin *)self.window, [event deltaY] * 10); } } - (void)mouseDragged:(NSEvent *)event { - NSPoint loc = [self convertPoint:[event locationInWindow] fromView:nil]; - int x = loc.x * self->scaleX; - int y = loc.y * self->scaleY; + [self saveMousePosFromEvent:event]; if (GLW_DOWN == self->imGUI.mouseState) { if (GLW_DOWN == self->imGUI.mouseState) { - if (self->onMotion) { - self->onMotion((glwWin *)self.window, x, y); + if (self->context.onMotion) { + self->context.onMotion((glwWin *)self.window, + self->context.x, self->context.y); } } else { - if (self->onPassiveMotion) { - self->onPassiveMotion((glwWin *)self.window, x, y); + if (self->context.onPassiveMotion) { + self->context.onPassiveMotion((glwWin *)self.window, + self->context.x, self->context.y); } } } else { - glwMouseEvent((glwWin *)self.window, GLW_LEFT_BUTTON, GLW_DOWN, x, y); - if (self->onMouse) { - self->onMouse((glwWin *)self.window, GLW_LEFT_BUTTON, GLW_DOWN, x, y); + glwMouseEvent((glwWin *)self.window, GLW_LEFT_BUTTON, GLW_DOWN, + self->context.x, self->context.y); + if (self->context.onMouse) { + self->context.onMouse((glwWin *)self.window, + GLW_LEFT_BUTTON, GLW_DOWN, + self->context.x, self->context.y); } } } @@ -192,12 +205,12 @@ glwSystemFontTexture *glwGetSystemFontTexture(glwWin *win) { } - (void)mouseUp:(NSEvent*)event { - NSPoint loc = [self convertPoint:[event locationInWindow] fromView:nil]; - int x = loc.x * self->scaleX; - int y = loc.y * self->scaleY; - glwMouseEvent((glwWin *)self.window, GLW_LEFT_BUTTON, GLW_UP, x, y); - if (self->onMouse) { - self->onMouse((glwWin *)self.window, GLW_LEFT_BUTTON, GLW_UP, x, y); + [self saveMousePosFromEvent:event]; + glwMouseEvent((glwWin *)self.window, GLW_LEFT_BUTTON, GLW_UP, + self->context.x, self->context.y); + if (self->context.onMouse) { + self->context.onMouse((glwWin *)self.window, GLW_LEFT_BUTTON, GLW_UP, + self->context.x, self->context.y); } } @end @@ -235,16 +248,6 @@ void glwMainLoop(void) { } } -void glwSetUserData(glwWin *win, void *userData) { - GLView *view = ((NSWindow *)win).contentView; - view->userData = userData; -} - -void *glwGetUserData(glwWin *win) { - GLView *view = ((NSWindow *)win).contentView; - return view->userData; -} - glwWin *glwCreateWindow(int x, int y, int width, int height) { NSUInteger windowStyle = NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask | NSMiniaturizableWindowMask; @@ -264,19 +267,15 @@ glwWin *glwCreateWindow(int x, int y, int width, int height) { defer:NO]; GLView *view = [[GLView alloc] initWithFrame:viewRect]; - view->onReshape = 0; - view->onDisplay = 0; - view->onMouse = 0; - view->userData = 0; - view->pendingReshape = 0; - view->viewWidth = width; - view->viewHeight = height; - view->systemFontTexture.texId = 0; + memset(&view->context, 0, sizeof(view->context)); + memset(&view->systemFontTexture, 0, sizeof(view->systemFontTexture)); memset(&view->imGUI, 0, sizeof(view->imGUI)); - view->scaleX = 1; - view->scaleY = 1; - view->onPassiveMotion = 0; - view->onMotion = 0; + view->pendingReshape = 0; + view->context.viewWidth = width; + view->context.viewHeight = height; + view->systemFontTexture.texId = 0; + view->context.scaleX = 1; + view->context.scaleY = 1; [window setAcceptsMouseMovedEvents:YES]; [window setContentView:view]; @@ -287,35 +286,6 @@ glwWin *glwCreateWindow(int x, int y, int width, int height) { return (glwWin *)window; } -void glwDisplayFunc(glwWin *win, void (*func)(glwWin *win)) { - GLView *view = ((NSWindow *)win).contentView; - view->onDisplay = func; -} - -void glwReshapeFunc(glwWin *win, void (*func)(glwWin *win, int width, - int height)) { - GLView *view = ((NSWindow *)win).contentView; - view->onReshape = func; -} - -void glwMouseFunc(glwWin *win, void (*func)(glwWin *win, int button, int state, - int x, int y)) { - GLView *view = ((NSWindow *)win).contentView; - view->onMouse = func; -} - -void glwMotionFunc(glwWin *win, - void (*func)(glwWin *win, int x, int y)) { - GLView *view = ((NSWindow *)win).contentView; - view->onMotion = func; -} - -void glwPassiveMotionFunc(glwWin *win, - void (*func)(glwWin *win, int x, int y)) { - GLView *view = ((NSWindow *)win).contentView; - view->onPassiveMotion = func; -} - unsigned char *glwRenderTextToRGBA(char *text, glwFont *font, glwSize size, int *pixelsWide, int *pixelsHigh) { NSString *aString = [NSString stringWithCString:text encoding:NSMacOSRomanStringEncoding]; diff --git a/src/glw_style.h b/src/glw_style.h index cd71d4fc..d80cc598 100644 --- a/src/glw_style.h +++ b/src/glw_style.h @@ -3,7 +3,7 @@ #define GLW_SYSTEM_FONT_NAME "Helvetica" #define GLW_SYSTEM_FONT_WEIGHT 5 -#define GLW_SYSTEM_FONT_SIZE 20 +#define GLW_SYSTEM_FONT_SIZE 21 #define GLW_SMALL_ROUNDED_CORNER_SLICES 5