Current state. Cleaning and fixing

This commit is contained in:
Daniel Collin 2012-10-21 20:02:58 +02:00
parent c597471c1c
commit 804aad808d

View File

@ -1,90 +1,82 @@
//
#import "RocketView.h" #import "RocketView.h"
#include "../GFXBackend.h" #include "../Editor.h"
#include "../RocketGui.h" #include <emgui/emgui.h>
extern void Editor_init();
extern void Editor_guiUpdate();
extern RocketGuiState g_rocketGuiState;
@implementation RocketView @implementation RocketView
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (id)initWithFrame:(NSRect)frame - (id)initWithFrame:(NSRect)frame
{ {
self = [super initWithFrame:frame]; self = [super initWithFrame:frame];
if (self == nil) if (self == nil)
return nil; return nil;
// create and activate the context object which maintains the OpenGL state
oglContext = [[NSOpenGLContext alloc] initWithFormat: [NSOpenGLView defaultPixelFormat] shareContext: nil]; oglContext = [[NSOpenGLContext alloc] initWithFormat: [NSOpenGLView defaultPixelFormat] shareContext: nil];
[oglContext makeCurrentContext]; [oglContext makeCurrentContext];
GFXBackend_create(); EMGFXBackend_create();
Editor_init(); Editor_create();
return self; return self;
} }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)lockFocus - (void)lockFocus
{ {
NSOpenGLContext* context = oglContext; NSOpenGLContext* context = oglContext;
// make sure we are ready to draw
[super lockFocus]; [super lockFocus];
// when we are about to draw, make sure we are linked to the view
// It is not possible to call setView: earlier (will yield 'invalid drawable')
if ([context view] != self) if ([context view] != self)
{
[context setView:self]; [context setView:self];
}
// make us the current OpenGL context
[context makeCurrentContext]; [context makeCurrentContext];
} }
// this is called whenever the view changes (is unhidden or resized) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)drawRect:(NSRect)frameRect - (void)drawRect:(NSRect)frameRect
{ {
// inform the context that the view has been resized
[oglContext update]; [oglContext update];
GFXBackend_updateViewPort(frameRect.size.width, frameRect.size.height); EMGFXBackend_updateViewPort((int)frameRect.size.width, (int)frameRect.size.height);
Editor_guiUpdate(); Editor_update();
} }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)keyDown:(NSEvent *)theEvent - (void)keyDown:(NSEvent *)theEvent
{ {
if ([theEvent modifierFlags] & NSNumericPadKeyMask) { // arrow keys have this mask NSString* key = [theEvent charactersIgnoringModifiers];
NSString *theArrow = [theEvent charactersIgnoringModifiers]; unichar keyChar = 0;
unichar keyChar = 0; if ([key length] == 0)
if ( [theArrow length] == 0 ) return;
return; // reject dead keys
if ( [theArrow length] == 1 ) { keyChar = [key characterAtIndex:0];
keyChar = [theArrow characterAtIndex:0];
if ( keyChar == NSLeftArrowFunctionKey ) { int keyCode = keyChar;
printf("LeftArrow\n");
return; if ([theEvent modifierFlags] & NSNumericPadKeyMask)
} {
if ( keyChar == NSRightArrowFunctionKey ) { switch (keyChar)
printf("RightArrow\n"); {
return; case NSLeftArrowFunctionKey: keyCode = EMGUI_ARROW_LEFT; break;
} case NSRightArrowFunctionKey: keyCode = EMGUI_ARROW_RIGHT; break;
if ( keyChar == NSUpArrowFunctionKey ) { case NSUpArrowFunctionKey: keyCode = EMGUI_ARROW_UP; break;
printf("UpArrow\n"); case NSDownArrowFunctionKey: keyCode = EMGUI_ARROW_DOWN; break;
return; }
} }
if ( keyChar == NSDownArrowFunctionKey ) {
printf("DownArrow\n"); Editor_keyDown(keyCode);
return; Editor_update();
}
[super keyDown:theEvent];
}
}
[super keyDown:theEvent]; [super keyDown:theEvent];
} }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)acceptsFirstResponder - (BOOL)acceptsFirstResponder
{ {
return YES; return YES;
@ -94,7 +86,6 @@ extern RocketGuiState g_rocketGuiState;
-(void) viewWillMoveToWindow:(NSWindow *)newWindow -(void) viewWillMoveToWindow:(NSWindow *)newWindow
{ {
// Setup a new tracking area when the view is added to the window.
NSTrackingArea* trackingArea = [[NSTrackingArea alloc] initWithRect:[self frame] NSTrackingArea* trackingArea = [[NSTrackingArea alloc] initWithRect:[self frame]
options: (NSTrackingMouseMoved | NSTrackingActiveAlways) owner:self userInfo:nil]; options: (NSTrackingMouseMoved | NSTrackingActiveAlways) owner:self userInfo:nil];
[self addTrackingArea:trackingArea]; [self addTrackingArea:trackingArea];
@ -105,95 +96,50 @@ extern RocketGuiState g_rocketGuiState;
- (void)mouseMoved:(NSEvent *)event - (void)mouseMoved:(NSEvent *)event
{ {
NSWindow* window = [self window]; NSWindow* window = [self window];
//NSPoint originalMouseLocation = [window convertBaseToScreen:[event locationInWindow]];
NSRect originalFrame = [window frame]; NSRect originalFrame = [window frame];
NSPoint location = [window mouseLocationOutsideOfEventStream]; NSPoint location = [window mouseLocationOutsideOfEventStream];
g_rocketGuiState.mousex = (int)location.x; Emgui_setMousePos((int)location.x, (int)originalFrame.size.height - (int)location.y);
g_rocketGuiState.mousey = (int)originalFrame.size.height - (int)location.y - 23; Editor_update();
printf("mouseMoved %d %d\n", g_rocketGuiState.mousex, g_rocketGuiState.mousey);
Editor_guiUpdate();
}
static NSPoint s_prevDragPos;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)mouseDragged:(NSEvent *)event
{
NSWindow* window = [self window];
NSRect originalFrame = [window frame];
NSPoint location = [window mouseLocationOutsideOfEventStream];
g_rocketGuiState.mousex = (int)location.x;
g_rocketGuiState.mousey = (int)originalFrame.size.height - (int)location.y;
if (g_rocketGuiState.activeItem != -1)
{
Editor_guiUpdate();
return;
}
NSPoint newMouseLocation = [window convertBaseToScreen:[event locationInWindow]];
NSPoint delta = NSMakePoint(newMouseLocation.x - s_prevDragPos.x,
newMouseLocation.y - s_prevDragPos.y);
NSRect newFrame = originalFrame;
newFrame.origin.x += delta.x;
newFrame.origin.y += delta.y;
s_prevDragPos = newMouseLocation;
printf("mouseDragged\n");
[window setFrame:newFrame display:YES animate:NO];
} }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)mouseUp:(NSEvent *)event - (void)mouseUp:(NSEvent *)event
{ {
g_rocketGuiState.mouseDown = 0; Emgui_setMouseLmb(0);
printf("mouseUp\n"); Editor_update();
Editor_guiUpdate();
} }
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// mouseDown:
//
// Handles mouse clicks in our frame. Two actions:
// - click in the resize box should resize the window
// - click anywhere else will drag the window.
//
- (void)mouseDown:(NSEvent *)event - (void)mouseDown:(NSEvent *)event
{ {
NSWindow *window = [self window]; NSWindow *window = [self window];
s_prevDragPos = [window convertBaseToScreen:[event locationInWindow]];
NSRect originalFrame = [window frame]; NSRect originalFrame = [window frame];
NSPoint location = [window mouseLocationOutsideOfEventStream]; NSPoint location = [window mouseLocationOutsideOfEventStream];
g_rocketGuiState.mousex = (int)location.x; Emgui_setMousePos((int)location.x, (int)originalFrame.size.height - (int)location.y);
g_rocketGuiState.mousey = (int)originalFrame.size.height - (int)location.y - 23; Emgui_setMouseLmb(1);
g_rocketGuiState.mouseDown = 1;
printf("mouseDown\n");
Editor_guiUpdate(); Editor_guiUpdate();
} }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-(BOOL) isOpaque -(BOOL) isOpaque
{ {
return YES; return YES;
} }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-(void) dealloc -(void) dealloc
{ {
GFXBackend_destroy(); Example_destroy();
EMGFXBackend_destroy();
[super dealloc]; [super dealloc];
} }
@end @end