Skip to content

Commit

Permalink
converted view to c
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdefreyne committed Dec 18, 2011
1 parent 0c3ef35 commit c03a89a
Show file tree
Hide file tree
Showing 7 changed files with 345 additions and 196 deletions.
16 changes: 8 additions & 8 deletions include/Monet/MOButton.h
@@ -1,10 +1,10 @@
#import <Monet/MOView.h>

@interface MOButton : MOView
{
struct MOButtonData *buttonData;
}

- (void)clicked;

@end
// @interface MOButton : MOView
// {
// struct MOButtonData *buttonData;
// }
//
// - (void)clicked;
//
// @end
5 changes: 2 additions & 3 deletions include/Monet/MOState.h
Expand Up @@ -2,8 +2,7 @@

#import <Monet/MOApplication.h>
#import <Monet/MOTicking.h>

@class MOView;
#import <Monet/MOView.h>

@interface MOState : NSObject <MOTicking>
{
Expand All @@ -12,7 +11,7 @@

+ (Class)viewClass;

- (id)initWithApp: (MOApplication *)aApp;
- (id)initWithApp: (MOApplication *)aApp view: (MOView *)aView;

- (NSObject <MOTicking> *)world;
- (void)setWorld: (NSObject <MOTicking> *)aWorld;
Expand Down
79 changes: 49 additions & 30 deletions include/Monet/MOView.h
@@ -1,46 +1,65 @@
#import <Foundation/Foundation.h>
#import <SeaBase/SeaBase.h>

typedef struct _MOView MOView;

#import <Monet/MOApplication.h>
#import <Monet/MOEvent.h>
#import <Monet/MORect.h>

@interface MOView : NSObject
{
struct MOViewData *viewData;
}
typedef void (*MOViewDrawRectCallback)(MOView *self, MORect aRect);
typedef void (*MOViewTickCallback)(MOView *self, double aSeconds);

typedef bool (*MOViewKeyPressedCallback)(MOView *self, MOEvent *aEvent);
typedef bool (*MOViewKeyReleasedCallback)(MOView *self, MOEvent *aEvent);
typedef bool (*MOViewMouseButtonPressedCallback)(MOView *self, MOEvent *aEvent);
typedef bool (*MOViewMouseButtonReleasedCallback)(MOView *self, MOEvent *aEvent);
typedef bool (*MOViewMouseDraggedCallback)(MOView *self, MOEvent *aEvent);
typedef bool (*MOViewTimerFiredCallback)(MOView *self, MOEvent *aEvent);

MOView *MOViewCreate(MORect aFrame, MOApplication *aApplication);

- (id)initWithFrame: (MORect)aFrame app: (MOApplication *)aApp;
void MOViewSetDrawRectCallback(MOView *self, MOViewDrawRectCallback aCallback);
void MOViewSetTickCallback(MOView *self, MOViewTickCallback aCallback);

- (MOApplication *)app;
void MOViewSetKeyPressedCallback(MOView *self, MOViewKeyPressedCallback aCallback);
void MOViewSetKeyReleasedCallback(MOView *self, MOViewKeyReleasedCallback aCallback);
void MOViewSetMouseButtonPressedCallback(MOView *self, MOViewMouseButtonPressedCallback aCallback);
void MOViewSetMouseButtonReleasedCallback(MOView *self, MOViewMouseButtonReleasedCallback aCallback);
void MOViewSetMouseDraggedCallback(MOView *self, MOViewMouseDraggedCallback aCallback);
void MOViewSetTimerFiredCallback(MOView *self, MOViewTimerFiredCallback aCallback);

- (MOView *)superview;
- (NSArray *)subviews;
- (void)addSubview: (MOView *)aSubview;
MOApplication *MOViewGetApplication(MOView *self);
void *MOViewGetExtra(MOView *self);
void MOViewSetExtra(MOView *self, void *aExtra);

- (MOPoint)convertPointFromScreen: (MOPoint)aPoint;
- (MOPoint)convertPointToScreen: (MOPoint)aPoint;
MOView *MOViewGetSuperview(MOView *self);
void MOViewSetSuperview(MOView *self, MOView *aSuperview);
SBArray *MOViewGetSubviews(MOView *self);
void MOViewAddSubview(MOView *self, MOView *aSubview);

- (MOView *)subviewAtPoint: (MOPoint)aPoint;
- (MOView *)deepestSubviewAtPoint: (MOPoint)aPoint;
MOPoint MOViewGetAbsoluteOrigin(MOView *self);
MOPoint MOViewConvertPointFromScreen(MOView *self, MOPoint aPoint);
MOPoint MOViewConvertPointToScreen(MOView *self, MOPoint aPoint);

- (MORect)frame;
- (MORect)bounds;
- (MORect)boundsRelativeToWindow;
MOView *MOViewGetSubviewAtPoint(MOView *self, MOPoint aPoint);
MOView *MOViewGetDeepestSubviewAtPoint(MOView *self, MOPoint aPoint);

- (void)lockFocus;
- (void)unlockFocus;
MORect MOViewGetFrame(MOView *self);
MORect MOViewGetBounds(MOView *self);
MORect MOViewGetBoundsRelativeToWindow(MOView *self);

- (void)display;
- (void)clear;
- (void)drawRect: (MORect)aRect;
void MOViewLockFocus(MOView *self);
void MOViewUnlockFocus(MOView *self);

- (void)tick: (double)aSeconds;
void MOViewDisplay(MOView *self);
void MOViewClear(MOView *self);

- (BOOL)keyDown: (MOEvent *)aEvent;
- (BOOL)keyUp: (MOEvent *)aEvent;
- (void)mouseDown: (MOEvent *)aEvent;
- (void)mouseUp: (MOEvent *)aEvent;
- (void)mouseDragged: (MOEvent *)aEvent;
- (void)timerFired: (MOEvent *)aEvent;
void MOViewDrawRect(MOView *self, MORect aRect);
void MOViewTick(MOView *self, double aSeconds);

@end
bool MOViewKeyPressed(MOView *self, MOEvent *aEvent);
bool MOViewKeyReleased(MOView *self, MOEvent *aEvent);
void MOViewMouseButtonPressed(MOView *self, MOEvent *aEvent);
void MOViewMouseButtonReleased(MOView *self, MOEvent *aEvent);
void MOViewMouseDragged(MOView *self, MOEvent *aEvent);
void MOViewTimerFired(MOView *self, MOEvent *aEvent);
47 changes: 30 additions & 17 deletions src/Monet/MOApplication.m
Expand Up @@ -33,9 +33,9 @@
float interpolation;

// Recent views receiving mouse button events
MOView *lastLeftMouseButtonDownView;
MOView *lastMiddleMouseButtonDownView;
MOView *lastRightMouseButtonDownView;
MOView *lastLeftMouseButtonDownView;
MOView *lastMiddleMouseButtonDownView;
MOView *lastRightMouseButtonDownView;
};

void _MOApplicationDestroy(void *self);
Expand All @@ -46,6 +46,15 @@ void _MOApplicationRefreshAutoreleasePool(MOApplication *self)
self->autoreleasePool = [[NSAutoreleasePool alloc] init];
}

/*
bool MOViewKeyPressed(MOView *self, MOEvent *aEvent);
bool MOViewKeyReleased(MOView *self, MOEvent *aEvent);
bool MOViewMouseButtonPressed(MOView *self, MOEvent *aEvent);
bool MOViewMouseButtonReleased(MOView *self, MOEvent *aEvent);
bool MOViewMouseDragged(MOView *self, MOEvent *aEvent);
bool MOViewTimerFired(MOView *self, MOEvent *aEvent);
*/

void _MOApplicationHandleEvents(MOApplication *self)
{
SDL_Event event;
Expand All @@ -55,9 +64,9 @@ void _MOApplicationHandleEvents(MOApplication *self)
{
case SDL_ACTIVEEVENT:
if (event.active.gain)
; // Resume
; // TODO Resume
else
; // Pause
; // TODO Pause
break;

case SDL_KEYDOWN:
Expand All @@ -77,7 +86,8 @@ void _MOApplicationHandleEvents(MOApplication *self)
MOSDLKeyToMOKey(event.key.keysym.sym));

// Dispatch event
[[MOApplicationGetCurrentState(self) view] keyDown: moEvent];
MOState *state = MOApplicationGetCurrentState(self);
MOViewKeyPressed([state view], moEvent);

// Cleanup
[character release];
Expand All @@ -95,7 +105,8 @@ void _MOApplicationHandleEvents(MOApplication *self)
MOSDLKeyToMOKey(event.key.keysym.sym));

// Dispatch event
[[MOApplicationGetCurrentState(self) view] keyUp: moEvent];
MOState *state = MOApplicationGetCurrentState(self);
MOViewKeyReleased([state view], moEvent);

// Cleanup
CORelease(moEvent);
Expand All @@ -108,16 +119,15 @@ void _MOApplicationHandleEvents(MOApplication *self)
MOEvent *moEvent = MOEventCreateMouseMotion(
MOSDLModToMOKeyModifierMask(SDL_GetModState()),
MOPointMake(event.motion.x, self->screenSize.h-event.motion.y-1),
MOPointMake(event.motion.xrel, event.motion.yrel)
);
MOPointMake(event.motion.xrel, event.motion.yrel));

// Dispatch event to subviews that want it
if (self->lastLeftMouseButtonDownView)
[self->lastLeftMouseButtonDownView mouseDragged: moEvent];
MOViewMouseDragged(self->lastLeftMouseButtonDownView, moEvent);
if (self->lastMiddleMouseButtonDownView)
[self->lastMiddleMouseButtonDownView mouseDragged: moEvent];
MOViewMouseDragged(self->lastMiddleMouseButtonDownView, moEvent);
if (self->lastRightMouseButtonDownView)
[self->lastRightMouseButtonDownView mouseDragged: moEvent];
MOViewMouseDragged(self->lastRightMouseButtonDownView, moEvent);

// Cleanup
CORelease(moEvent);
Expand All @@ -134,7 +144,8 @@ void _MOApplicationHandleEvents(MOApplication *self)
uint8_t modifiers = MOSDLModToMOKeyModifierMask(SDL_GetModState());

// Find deepest subview
MOView *subview = [[MOApplicationGetCurrentState(self) view] deepestSubviewAtPoint: mouseLocation];
MOState *state = MOApplicationGetCurrentState(self);
MOView *subview = MOViewGetDeepestSubviewAtPoint([state view], mouseLocation);

// Set last view receiving event
switch(mouseButton)
Expand All @@ -161,7 +172,7 @@ void _MOApplicationHandleEvents(MOApplication *self)
1); // FIXME set correct click count

// Dispatch event
[subview mouseDown: moEvent];
MOViewMouseButtonPressed(subview, moEvent);

// Cleanup
CORelease(moEvent);
Expand Down Expand Up @@ -204,7 +215,7 @@ void _MOApplicationHandleEvents(MOApplication *self)
1); // FIXME set correct click count

// Dispatch event
[subview mouseUp: moEvent];
MOViewMouseButtonReleased(subview, moEvent);

// Clear relevant subview
switch(mouseButton)
Expand Down Expand Up @@ -232,7 +243,8 @@ void _MOApplicationHandleEvents(MOApplication *self)
{
MOTimer *timer = event.user.data1;
MOEvent *moEvent = MOEventCreateTimer(timer);
[[MOApplicationGetCurrentState(self) view] timerFired: moEvent];
MOState *state = MOApplicationGetCurrentState(self);
MOViewTimerFired([state view], moEvent);
CORelease(moEvent);
}
break;
Expand Down Expand Up @@ -424,7 +436,8 @@ void MOApplicationEnterRunloop(MOApplication *self)

// Redraw
glClear(GL_COLOR_BUFFER_BIT);
[[MOApplicationGetCurrentState(self) view] display];
MOState *state = MOApplicationGetCurrentState(self);
MOViewDisplay([state view]);
SDL_GL_SwapBuffers();

// Empty autorelease pool
Expand Down
4 changes: 4 additions & 0 deletions src/Monet/MOButton.m
Expand Up @@ -5,6 +5,8 @@
#import <Monet/MORect.h>
#import <Monet/MOView.h>

/*
struct MOButtonData
{
BOOL isMouseDown;
Expand Down Expand Up @@ -57,3 +59,5 @@ - (void)clicked
}
@end
*/
35 changes: 29 additions & 6 deletions src/Monet/MOState.m
Expand Up @@ -10,6 +10,23 @@
MOView *view;
};

// FIXME view struct definition here is UGLY
#include <cobject/cobject.h>
struct _MOView
{
COGuts *guts;

MOApplication *application;

MOView *superview;
SBArray *subviews;

MORect frame;
MORect bounds;

// more irrelevant stuff here
};

@implementation MOState

+ (Class)viewClass
Expand All @@ -19,23 +36,29 @@ + (Class)viewClass

#pragma mark -

- (id)initWithApp: (MOApplication *)aApp
- (id)initWithApp: (MOApplication *)aApp view: (MOView *)aView
{
if ((self = [super init]))
{
stateData = calloc(1, sizeof (struct MOStateData));

Class viewClass = [[self class] viewClass];
MORect frame = MORectMake(0, 0, MOApplicationGetScreenSize(aApp).w, MOApplicationGetScreenSize(aApp).h);
stateData->view = [[viewClass alloc] initWithFrame: frame app: aApp];
MOSize screenSize = MOApplicationGetScreenSize(aApp);
MORect frame = MORectMake(0, 0, screenSize.w, screenSize.h);
// FIXME modifying view is ugly
aView->application = aApp;
aView->frame = frame;
aView->bounds = frame;
aView->bounds.x = 0;
aView->bounds.y = 0;
stateData->view = CORetain(aView);
}

return self;
}

- (void)dealloc
{
[stateData->view release];
CORelease(stateData->view);
free(stateData);

[super dealloc];
Expand Down Expand Up @@ -65,7 +88,7 @@ - (MOView *)view
- (void)tick: (double)aSeconds
{
[stateData->world tick: aSeconds];
[stateData->view tick: aSeconds];
MOViewTick(stateData->view, aSeconds);
}

@end

0 comments on commit c03a89a

Please sign in to comment.