Moved many parts of Cocoa backend into backend-independent UI code
parent
80857c7a36
commit
b9e823362f
|
@ -1,9 +1,9 @@
|
|||
! Copyright (C) 2006 Slava Pestov
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
IN: cocoa
|
||||
USING: alien errors gadgets io kernel namespaces
|
||||
USING: alien errors gadgets io kernel namespaces objc
|
||||
objc-NSApplication objc-NSAutoreleasePool objc-NSException
|
||||
objc-NSObject objc-NSView threads ;
|
||||
objc-NSNotificationCenter objc-NSObject objc-NSView threads ;
|
||||
|
||||
: with-autorelease-pool ( quot -- )
|
||||
NSAutoreleasePool [new] slip [release] ; inline
|
||||
|
@ -34,6 +34,10 @@ objc-NSObject objc-NSView threads ;
|
|||
NSApplication [sharedApplication] do-events world-step
|
||||
] with-autorelease-pool 10 sleep event-loop ;
|
||||
|
||||
: add-observer ( observer selector name object -- )
|
||||
>r >r >r >r NSNotificationCenter [defaultCenter] r> r>
|
||||
sel_registerName r> r> [addObserver:selector:name:object:] ;
|
||||
|
||||
IN: errors
|
||||
|
||||
: objc-error. ( alien -- )
|
||||
|
|
|
@ -9,6 +9,7 @@ USING: compiler io parser sequences words ;
|
|||
"/library/cocoa/init-cocoa.factor"
|
||||
"/library/cocoa/application-utils.factor"
|
||||
"/library/cocoa/window-utils.factor"
|
||||
"/library/cocoa/view-utils.factor"
|
||||
} [
|
||||
run-resource
|
||||
] each
|
||||
|
|
|
@ -15,42 +15,10 @@ IN: gadgets
|
|||
IN: gadgets-cocoa
|
||||
|
||||
! Cocoa backend for Factor UI
|
||||
: init-gl ( rect -- )
|
||||
0.0 0.0 0.0 0.0 glClearColor
|
||||
{ 1.0 0.0 0.0 0.0 } gl-color
|
||||
GL_COLOR_BUFFER_BIT glClear
|
||||
GL_PROJECTION glMatrixMode
|
||||
glLoadIdentity
|
||||
GL_MODELVIEW glMatrixMode
|
||||
glLoadIdentity
|
||||
{ 0 0 0 } over NSRect-w pick NSRect-h 0 3array <rect>
|
||||
clip set
|
||||
dup NSRect-w over NSRect-h 0 0 2swap glViewport
|
||||
dup NSRect-w swap NSRect-h >r >r 0 r> r> 0 gluOrtho2D
|
||||
GL_SMOOTH glShadeModel
|
||||
GL_BLEND glEnable
|
||||
GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA glBlendFunc
|
||||
GL_SCISSOR_TEST glEnable
|
||||
GL_MODELVIEW glMatrixMode ;
|
||||
|
||||
: with-gl-context ( context quot -- )
|
||||
swap
|
||||
[ [makeCurrentContext] call glFlush ] keep
|
||||
[flushBuffer] ; inline
|
||||
|
||||
: button ( event -- n )
|
||||
#! Cocoa -> Factor UI button mapping
|
||||
[buttonNumber] H{ { 0 1 } { 2 2 } { 1 3 } } hash ;
|
||||
|
||||
: send-button-down ( event -- )
|
||||
update-clicked
|
||||
button dup hand get hand-buttons push
|
||||
[ button-down ] button-gesture ;
|
||||
|
||||
: send-button-up ( event -- )
|
||||
button dup hand get hand-buttons delete
|
||||
[ button-up ] button-gesture ;
|
||||
|
||||
: mouse-location ( window -- loc )
|
||||
dup [contentView] [
|
||||
swap [mouseLocationOutsideOfEventStream] f
|
||||
|
@ -61,11 +29,6 @@ IN: gadgets-cocoa
|
|||
: send-mouse-moved ( -- )
|
||||
world get world-handle mouse-location move-hand ;
|
||||
|
||||
: send-scroll-wheel ( event -- )
|
||||
[deltaY] 0 >
|
||||
[ wheel-up ] [ wheel-down ] ?
|
||||
hand get hand-clicked handle-gesture drop ;
|
||||
|
||||
: modifiers
|
||||
{
|
||||
{ "SHIFT" HEX: 10000 }
|
||||
|
@ -97,89 +60,81 @@ IN: gadgets-cocoa
|
|||
dup [modifierFlags] modifier swap [keyCode] key-codes
|
||||
[ add >list ] [ drop f ] if* ;
|
||||
|
||||
: send-user-input ( event -- )
|
||||
[characters] CF>string dup empty?
|
||||
[ hand get hand-focus user-input ] unless drop ;
|
||||
|
||||
: send-key-event ( event -- )
|
||||
dup event>binding
|
||||
[ hand get hand-focus handle-gesture ] [ t ] if*
|
||||
[ send-user-input ] [ drop ] if ;
|
||||
[ [characters] CF>string send-user-input ] [ drop ] if ;
|
||||
|
||||
: resize-world ( world -- )
|
||||
>r [bounds] dup NSRect-w swap NSRect-h 0 3array r>
|
||||
set-gadget-dim ;
|
||||
"NSOpenGLView" "FactorView" {
|
||||
{ "drawRect:" "void" { "id" "SEL" "NSRect" }
|
||||
[
|
||||
2drop dup [openGLContext] [
|
||||
view-dim init-gl world get draw-gadget
|
||||
] with-gl-context
|
||||
]
|
||||
}
|
||||
|
||||
: init-FactorView-class
|
||||
"NSOpenGLView" "FactorView" {
|
||||
{ "drawRect:" "void" { "id" "SEL" "NSRect" }
|
||||
[
|
||||
2drop dup [openGLContext] [
|
||||
[bounds] init-gl world get draw-gadget
|
||||
] with-gl-context
|
||||
]
|
||||
}
|
||||
{ "mouseMoved:" "void" { "id" "SEL" "id" }
|
||||
[ 3drop send-mouse-moved ]
|
||||
}
|
||||
|
||||
{ "mouseMoved:" "void" { "id" "SEL" "id" }
|
||||
[ 3drop send-mouse-moved ]
|
||||
}
|
||||
{ "mouseDragged:" "void" { "id" "SEL" "id" }
|
||||
[ 3drop send-mouse-moved ]
|
||||
}
|
||||
|
||||
{ "mouseDragged:" "void" { "id" "SEL" "id" }
|
||||
[ 3drop send-mouse-moved ]
|
||||
}
|
||||
{ "rightMouseDragged:" "void" { "id" "SEL" "id" }
|
||||
[ 3drop send-mouse-moved ]
|
||||
}
|
||||
|
||||
{ "rightMouseDragged:" "void" { "id" "SEL" "id" }
|
||||
[ 3drop send-mouse-moved ]
|
||||
}
|
||||
{ "otherMouseDragged:" "void" { "id" "SEL" "id" }
|
||||
[ 3drop send-mouse-moved ]
|
||||
}
|
||||
|
||||
{ "otherMouseDragged:" "void" { "id" "SEL" "id" }
|
||||
[ 3drop send-mouse-moved ]
|
||||
}
|
||||
{ "mouseDown:" "void" { "id" "SEL" "id" }
|
||||
[ 2nip button send-button-down ]
|
||||
}
|
||||
|
||||
{ "mouseDown:" "void" { "id" "SEL" "id" }
|
||||
[ 2nip send-button-down ]
|
||||
}
|
||||
{ "mouseUp:" "void" { "id" "SEL" "id" }
|
||||
[ 2nip button send-button-up ]
|
||||
}
|
||||
|
||||
{ "mouseUp:" "void" { "id" "SEL" "id" }
|
||||
[ 2nip send-button-up ]
|
||||
}
|
||||
{ "rightMouseDown:" "void" { "id" "SEL" "id" }
|
||||
[ 2nip button send-button-down ]
|
||||
}
|
||||
|
||||
{ "rightMouseDown:" "void" { "id" "SEL" "id" }
|
||||
[ 2nip send-button-down ]
|
||||
}
|
||||
{ "rightMouseUp:" "void" { "id" "SEL" "id" }
|
||||
[ 2nip button send-button-up ]
|
||||
}
|
||||
|
||||
{ "rightMouseUp:" "void" { "id" "SEL" "id" }
|
||||
[ 2nip send-button-up ]
|
||||
}
|
||||
{ "otherMouseDown:" "void" { "id" "SEL" "id" }
|
||||
[ 2nip button send-button-down ]
|
||||
}
|
||||
|
||||
{ "otherMouseDown:" "void" { "id" "SEL" "id" }
|
||||
[ 2nip send-button-down ]
|
||||
}
|
||||
{ "otherMouseUp:" "void" { "id" "SEL" "id" }
|
||||
[ 2nip button send-button-up ]
|
||||
}
|
||||
|
||||
{ "otherMouseUp:" "void" { "id" "SEL" "id" }
|
||||
[ 2nip send-button-up ]
|
||||
}
|
||||
{ "scrollWheel:" "void" { "id" "SEL" "id" }
|
||||
[ 2nip [deltaY] 0 > send-scroll-wheel ]
|
||||
}
|
||||
|
||||
{ "scrollWheel:" "void" { "id" "SEL" "id" }
|
||||
[ 2nip send-scroll-wheel ]
|
||||
}
|
||||
{ "keyDown:" "void" { "id" "SEL" "id" }
|
||||
[ 2nip send-key-event ]
|
||||
}
|
||||
|
||||
{ "keyDown:" "void" { "id" "SEL" "id" }
|
||||
[ 2nip send-key-event ]
|
||||
}
|
||||
{ "updateFactorGadgetSize:" "void" { "id" "SEL" "id" }
|
||||
[ 2drop view-dim world get set-gadget-dim ]
|
||||
}
|
||||
|
||||
{ "updateFactorGadgetSize:" "void" { "id" "SEL" "id" }
|
||||
[ 2drop world get resize-world ]
|
||||
}
|
||||
{ "acceptsFirstResponder" "bool" { "id" "SEL" }
|
||||
[ 2drop 1 ]
|
||||
}
|
||||
} { } define-objc-class
|
||||
|
||||
{ "acceptsFirstResponder" "bool" { "id" "SEL" }
|
||||
[ 2drop 1 ]
|
||||
}
|
||||
} { } define-objc-class ; parsing
|
||||
IN: objc-FactorView
|
||||
DEFER: FactorView
|
||||
|
||||
init-FactorView-class
|
||||
|
||||
USE: objc-FactorView
|
||||
IN: gadgets-cocoa
|
||||
|
||||
: <FactorView> ( gadget -- view )
|
||||
drop
|
||||
|
@ -187,18 +142,18 @@ USE: objc-FactorView
|
|||
0 0 100 100 <NSRect> NSOpenGLView [defaultPixelFormat]
|
||||
[initWithFrame:pixelFormat:]
|
||||
dup 1 [setPostsBoundsChangedNotifications:]
|
||||
dup 1 [setPostsFrameChangedNotifications:] ;
|
||||
dup 1 [setPostsFrameChangedNotifications:]
|
||||
dup "updateFactorGadgetSize:" add-resize-observer ;
|
||||
|
||||
: <FactorWindow> ( gadget title -- window )
|
||||
over rect-dim first2 0 0 2swap <NSRect> <NSWindow>
|
||||
[ swap <FactorView> [setContentView:] ] 2keep
|
||||
[ swap set-world-handle ] keep ;
|
||||
[ swap set-world-handle ] keep
|
||||
dup 1 [setAcceptsMouseMovedEvents:]
|
||||
dup dup [contentView] [setInitialFirstResponder:]
|
||||
dup f [makeKeyAndOrderFront:] ;
|
||||
|
||||
: NSViewBoundsDidChangeNotification
|
||||
"NSViewBoundsDidChangeNotification" <NSString> ;
|
||||
|
||||
: NSViewFrameDidChangeNotification
|
||||
"NSViewFrameDidChangeNotification" <NSString> ;
|
||||
IN: shells
|
||||
|
||||
: ui
|
||||
[
|
||||
|
@ -207,23 +162,10 @@ USE: objc-FactorView
|
|||
|
||||
world get ui-title <FactorWindow>
|
||||
|
||||
dup 1 [setAcceptsMouseMovedEvents:]
|
||||
|
||||
dup dup [contentView] [setInitialFirstResponder:]
|
||||
|
||||
NSNotificationCenter [defaultCenter]
|
||||
over [contentView]
|
||||
"updateFactorGadgetSize:" sel_registerName
|
||||
NSViewFrameDidChangeNotification
|
||||
pick
|
||||
[addObserver:selector:name:object:]
|
||||
|
||||
dup f [makeKeyAndOrderFront:]
|
||||
|
||||
[contentView] [openGLContext] [makeCurrentContext]
|
||||
listener-application
|
||||
|
||||
NSApplication [sharedApplication] [finishLaunching]
|
||||
|
||||
event-loop
|
||||
] with-cocoa
|
||||
] with-freetype ;
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
! Copyright (C) 2006 Slava Pestov
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
IN: cocoa
|
||||
USING: arrays kernel objc-NSOpenGLContext objc-NSView opengl ;
|
||||
|
||||
: with-gl-context ( context quot -- )
|
||||
swap
|
||||
[ [makeCurrentContext] call glFlush ] keep
|
||||
[flushBuffer] ; inline
|
||||
|
||||
: view-dim [bounds] dup NSRect-w swap NSRect-h 0 3array ;
|
||||
|
||||
: NSViewFrameDidChangeNotification
|
||||
"NSViewFrameDidChangeNotification" <NSString> ;
|
||||
|
||||
: add-resize-observer ( view selector -- )
|
||||
NSViewFrameDidChangeNotification pick add-observer ;
|
|
@ -1,5 +1,5 @@
|
|||
! Copyright (C) 2005 Slava Pestov.
|
||||
! See http://factor.sf.net/license.txt for BSD license.
|
||||
! Copyright (C) 2005, 2006 Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
IN: gadgets
|
||||
USING: kernel math namespaces sequences ;
|
||||
|
||||
|
@ -21,6 +21,19 @@ C: hand ( -- hand )
|
|||
swap hand get hand-clicked 3dup >r add r> handle-gesture
|
||||
[ nip handle-gesture drop ] [ 3drop ] if ;
|
||||
|
||||
: send-button-down ( event -- )
|
||||
update-clicked
|
||||
dup hand get hand-buttons push
|
||||
[ button-down ] button-gesture ;
|
||||
|
||||
: send-button-up ( event -- )
|
||||
dup hand get hand-buttons delete
|
||||
[ button-up ] button-gesture ;
|
||||
|
||||
: send-scroll-wheel ( up/down -- )
|
||||
[ wheel-up ] [ wheel-down ] ?
|
||||
hand get hand-clicked handle-gesture drop ;
|
||||
|
||||
: drag-gesture ( -- )
|
||||
#! Send a gesture like [ drag 2 ]; if nobody handles it,
|
||||
#! send [ drag ].
|
||||
|
@ -33,6 +46,10 @@ C: hand ( -- hand )
|
|||
[ motion ] over hand-gadget handle-gesture drop
|
||||
hand-buttons empty? [ drag-gesture ] unless ;
|
||||
|
||||
: send-user-input ( string -- )
|
||||
dup empty?
|
||||
[ hand get hand-focus user-input ] unless drop ;
|
||||
|
||||
: each-gesture ( gesture seq -- )
|
||||
[ handle-gesture* drop ] each-with ;
|
||||
|
||||
|
|
|
@ -1,10 +1,27 @@
|
|||
! Copyright (C) 2005 Slava Pestov.
|
||||
! See http://factor.sf.net/license.txt for BSD license.
|
||||
! Copyright (C) 2005, 2006 Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: alien arrays freetype gadgets-layouts generic hashtables
|
||||
io kernel lists math namespaces opengl sequences strings
|
||||
styles vectors ;
|
||||
IN: gadgets
|
||||
|
||||
: init-gl ( dim -- )
|
||||
0.0 0.0 0.0 0.0 glClearColor
|
||||
{ 1.0 0.0 0.0 0.0 } gl-color
|
||||
GL_COLOR_BUFFER_BIT glClear
|
||||
GL_PROJECTION glMatrixMode
|
||||
glLoadIdentity
|
||||
GL_MODELVIEW glMatrixMode
|
||||
glLoadIdentity
|
||||
{ 0 0 0 } over <rect> clip set
|
||||
dup first2 0 0 2swap glViewport
|
||||
0 swap first2 0 gluOrtho2D
|
||||
GL_SMOOTH glShadeModel
|
||||
GL_BLEND glEnable
|
||||
GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA glBlendFunc
|
||||
GL_SCISSOR_TEST glEnable
|
||||
GL_MODELVIEW glMatrixMode ;
|
||||
|
||||
GENERIC: draw-gadget* ( gadget -- )
|
||||
|
||||
M: gadget draw-gadget* ( gadget -- ) drop ;
|
||||
|
|
Loading…
Reference in New Issue