Parcourir la source

App focus lose and gain support on Mac, fixed bug with modifier keys

Ivan Safrin il y a 13 ans
Parent
commit
2342359635

+ 1 - 0
Core/Contents/Include/PolyCocoaCore.h

@@ -63,6 +63,7 @@ namespace Polycode {
 		char mouseButton;
 		char mouseButton;
 		
 		
 		static const int INPUT_EVENT = 0;
 		static const int INPUT_EVENT = 0;
+		static const int FOCUS_EVENT = 1;		
 	};
 	};
 	
 	
 	
 	

+ 7 - 0
Core/Contents/Include/PolyCore.h

@@ -307,6 +307,8 @@ namespace Polycode {
 		void *getUserPointer() { return userPointer; }
 		void *getUserPointer() { return userPointer; }
 		
 		
 		static const int EVENT_CORE_RESIZE = 0;		
 		static const int EVENT_CORE_RESIZE = 0;		
+		static const int EVENT_LOST_FOCUS = 1;
+		static const int EVENT_GAINED_FOCUS = 2;
 		
 		
 		/**
 		/**
 		* Returns the default working path of the application.
 		* Returns the default working path of the application.
@@ -322,8 +324,13 @@ namespace Polycode {
 		CoreMutex *eventMutex;
 		CoreMutex *eventMutex;
 		
 		
 		void removeThread(Threaded *thread);
 		void removeThread(Threaded *thread);
+		
 				
 				
 	protected:	
 	protected:	
+	
+		void loseFocus();
+		void gainFocus();
+		
 		
 		
 		String userHomeDirectory;
 		String userHomeDirectory;
 		String defaultWorkingDirectory;
 		String defaultWorkingDirectory;

+ 2 - 0
Core/Contents/Include/PolyCoreInput.h

@@ -149,6 +149,8 @@ namespace Polycode {
 		*/
 		*/
 		bool simulateTouchWithMouse;
 		bool simulateTouchWithMouse;
 		
 		
+		void clearInput();
+		
 	protected:
 	protected:
 		
 		
 		std::vector<JoystickInfo> joysticks;
 		std::vector<JoystickInfo> joysticks;

+ 43 - 6
Core/Contents/PolycodeView/Mac OS X/PolycodeView.mm

@@ -56,16 +56,51 @@
 -(void) viewDidMoveToWindow
 -(void) viewDidMoveToWindow
 {
 {
 	[super viewDidMoveToWindow];
 	[super viewDidMoveToWindow];
+	
+	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidResignMain:) name:NSWindowDidResignKeyNotification object:[self window]];
+	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidResignMain:) name:NSWindowDidResignMainNotification object:[self window]];
+
+
+	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidBecomeMain:) name:NSWindowDidBecomeKeyNotification object:[self window]];
+	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidBecomeMain:) name:NSWindowDidBecomeMainNotification object:[self window]];
+
+	
 	viewReady = YES;
 	viewReady = YES;
 }
 }
 
 
+- (void)windowDidResignMain:(NSNotification *)notification
+{	
+	if(core == NULL)
+		return;
+	
+	memset(modifierMap, 0, 512);
+	
+	core->lockMutex(core->eventMutex);	
+	CocoaEvent newEvent;
+	newEvent.eventGroup = CocoaEvent::FOCUS_EVENT;
+	newEvent.eventCode = Core::EVENT_LOST_FOCUS;	
+	core->cocoaEvents.push_back(newEvent);	
+	core->unlockMutex(core->eventMutex);		
+}
 
 
-- (void) initKeymap {	 
+- (void)windowDidBecomeMain:(NSNotification *)notification
+{	
+	if(core == NULL)
+		return;
 	
 	
-	for(int i=0; i < 512; i++) {
-		modifierMap[i] = 0;	
-	}
+	core->lockMutex(core->eventMutex);	
+	CocoaEvent newEvent;
+	newEvent.eventGroup = CocoaEvent::FOCUS_EVENT;
+	newEvent.eventCode = Core::EVENT_GAINED_FOCUS;	
+	core->cocoaEvents.push_back(newEvent);	
+	core->unlockMutex(core->eventMutex);		
+}
+
+
+- (void) initKeymap {	 
 	
 	
+	memset(modifierMap, 0, 512);
+		
 	keymap[0x00] = KEY_a;
 	keymap[0x00] = KEY_a;
 	keymap[0x01] = KEY_s;
 	keymap[0x01] = KEY_s;
 	keymap[0x02] = KEY_d;
 	keymap[0x02] = KEY_d;
@@ -294,7 +329,6 @@
 //	core->setVideoMode(oldSize.width, oldSize.height, core->isFullscreen(), core->getAALevel());	
 //	core->setVideoMode(oldSize.width, oldSize.height, core->isFullscreen(), core->getAALevel());	
 }
 }
 
 
-
 // INPUT
 // INPUT
 
 
 - (void) otherMouseDown:(NSEvent *) event
 - (void) otherMouseDown:(NSEvent *) event
@@ -439,6 +473,9 @@
 	
 	
 //	NSLog(@"KEY: %x\n", [theEvent keyCode]);
 //	NSLog(@"KEY: %x\n", [theEvent keyCode]);
 	
 	
+	if([theEvent keyCode] == 0) {
+		return;
+	}
 	core->lockMutex(core->eventMutex);	
 	core->lockMutex(core->eventMutex);	
 	CocoaEvent newEvent;	
 	CocoaEvent newEvent;	
 	newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
 	newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
@@ -446,7 +483,7 @@
 	
 	
 	if(modifierMap[[theEvent keyCode]] == 0) {
 	if(modifierMap[[theEvent keyCode]] == 0) {
 		modifierMap[[theEvent keyCode]] = 1;
 		modifierMap[[theEvent keyCode]] = 1;
-		newEvent.eventCode = InputEvent::EVENT_KEYDOWN;		
+		newEvent.eventCode = InputEvent::EVENT_KEYDOWN;	
 	} else {
 	} else {
 		modifierMap[[theEvent keyCode]] = 0;
 		modifierMap[[theEvent keyCode]] = 0;
 		newEvent.eventCode = InputEvent::EVENT_KEYUP;				
 		newEvent.eventCode = InputEvent::EVENT_KEYUP;				

+ 10 - 0
Core/Contents/Source/PolyCocoaCore.mm

@@ -397,6 +397,16 @@ void CocoaCore::checkEvents() {
 						break;						
 						break;						
 				}
 				}
 				break;
 				break;
+				case CocoaEvent::FOCUS_EVENT:
+					switch(event.eventCode) {
+						case Core::EVENT_LOST_FOCUS:
+							loseFocus();						
+						break;
+						case Core::EVENT_GAINED_FOCUS:
+							gainFocus();
+						break;						
+					}
+				break;
 		}
 		}
 	}
 	}
 	cocoaEvents.clear();	
 	cocoaEvents.clear();	

+ 9 - 0
Core/Contents/Source/PolyCore.cpp

@@ -143,6 +143,15 @@ namespace Polycode {
 		return eventMutex;
 		return eventMutex;
 	}
 	}
 	
 	
+	void Core::loseFocus() {
+		input->clearInput();
+		dispatchEvent(new Event(), EVENT_LOST_FOCUS);
+	}
+	
+	void Core::gainFocus() {
+		dispatchEvent(new Event(), EVENT_GAINED_FOCUS);
+	}
+	
 	void Core::removeThread(Threaded *thread) {
 	void Core::removeThread(Threaded *thread) {
 		if(threadedEventMutex){ 
 		if(threadedEventMutex){ 
 			lockMutex(threadedEventMutex);
 			lockMutex(threadedEventMutex);

+ 6 - 3
Core/Contents/Source/PolyCoreInput.cpp

@@ -36,15 +36,18 @@ namespace Polycode {
 	}
 	}
 	
 	
 	CoreInput::CoreInput() : EventDispatcher() {
 	CoreInput::CoreInput() : EventDispatcher() {
+		clearInput();
+		simulateTouchWithMouse = false;
+	}
+	
+	void CoreInput::clearInput() {
 		mouseButtons[0] = false;
 		mouseButtons[0] = false;
 		mouseButtons[1] = false;
 		mouseButtons[1] = false;
 		mouseButtons[2] = false;
 		mouseButtons[2] = false;
 		
 		
 		for(int i=0; i < 512; i++) {
 		for(int i=0; i < 512; i++) {
 			keyboardState[i] = 0;
 			keyboardState[i] = 0;
-		}
-		
-		simulateTouchWithMouse = false;
+		}			
 	}
 	}
 	
 	
 	CoreInput::~CoreInput() {
 	CoreInput::~CoreInput() {

+ 22 - 16
IDE/Build/Mac OS X/English.lproj/MainMenu.xib

@@ -2,13 +2,13 @@
 <archive type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="7.10">
 <archive type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="7.10">
 	<data>
 	<data>
 		<int key="IBDocument.SystemTarget">1060</int>
 		<int key="IBDocument.SystemTarget">1060</int>
-		<string key="IBDocument.SystemVersion">11C74</string>
-		<string key="IBDocument.InterfaceBuilderVersion">1938</string>
-		<string key="IBDocument.AppKitVersion">1138.23</string>
-		<string key="IBDocument.HIToolboxVersion">567.00</string>
+		<string key="IBDocument.SystemVersion">11E53</string>
+		<string key="IBDocument.InterfaceBuilderVersion">2182</string>
+		<string key="IBDocument.AppKitVersion">1138.47</string>
+		<string key="IBDocument.HIToolboxVersion">569.00</string>
 		<object class="NSMutableDictionary" key="IBDocument.PluginVersions">
 		<object class="NSMutableDictionary" key="IBDocument.PluginVersions">
 			<string key="NS.key.0">com.apple.InterfaceBuilder.CocoaPlugin</string>
 			<string key="NS.key.0">com.apple.InterfaceBuilder.CocoaPlugin</string>
-			<string key="NS.object.0">1938</string>
+			<string key="NS.object.0">2182</string>
 		</object>
 		</object>
 		<object class="NSArray" key="IBDocument.IntegratedClassDependencies">
 		<object class="NSArray" key="IBDocument.IntegratedClassDependencies">
 			<bool key="EncodedWithXMLCoder">YES</bool>
 			<bool key="EncodedWithXMLCoder">YES</bool>
@@ -665,8 +665,9 @@
 				<string key="NSWindowClass">NSWindow</string>
 				<string key="NSWindowClass">NSWindow</string>
 				<nil key="NSViewClass"/>
 				<nil key="NSViewClass"/>
 				<nil key="NSUserInterfaceItemIdentifier"/>
 				<nil key="NSUserInterfaceItemIdentifier"/>
+				<string key="NSWindowContentMinSize">{400, 200}</string>
 				<object class="NSView" key="NSWindowView" id="439893737">
 				<object class="NSView" key="NSWindowView" id="439893737">
-					<nil key="NSNextResponder"/>
+					<reference key="NSNextResponder"/>
 					<int key="NSvFlags">256</int>
 					<int key="NSvFlags">256</int>
 					<object class="NSMutableArray" key="NSSubviews">
 					<object class="NSMutableArray" key="NSSubviews">
 						<bool key="EncodedWithXMLCoder">YES</bool>
 						<bool key="EncodedWithXMLCoder">YES</bool>
@@ -676,6 +677,8 @@
 							<object class="NSPSMatrix" key="NSDrawMatrix"/>
 							<object class="NSPSMatrix" key="NSDrawMatrix"/>
 							<string key="NSFrameSize">{800, 600}</string>
 							<string key="NSFrameSize">{800, 600}</string>
 							<reference key="NSSuperview" ref="439893737"/>
 							<reference key="NSSuperview" ref="439893737"/>
+							<reference key="NSWindow"/>
+							<reference key="NSNextKeyView"/>
 							<object class="NSOpenGLPixelFormat" key="NSPixelFormat">
 							<object class="NSOpenGLPixelFormat" key="NSPixelFormat">
 								<object class="NSMutableData" key="NSPixelAttributes">
 								<object class="NSMutableData" key="NSPixelAttributes">
 									<bytes key="NS.bytes">AAAAYAAAAAA</bytes>
 									<bytes key="NS.bytes">AAAAYAAAAAA</bytes>
@@ -684,9 +687,12 @@
 						</object>
 						</object>
 					</object>
 					</object>
 					<string key="NSFrameSize">{800, 600}</string>
 					<string key="NSFrameSize">{800, 600}</string>
+					<reference key="NSSuperview"/>
+					<reference key="NSWindow"/>
 					<reference key="NSNextKeyView" ref="633009941"/>
 					<reference key="NSNextKeyView" ref="633009941"/>
 				</object>
 				</object>
 				<string key="NSScreenRect">{{0, 0}, {1680, 1028}}</string>
 				<string key="NSScreenRect">{{0, 0}, {1680, 1028}}</string>
+				<string key="NSMinSize">{400, 222}</string>
 				<string key="NSMaxSize">{10000000000000, 10000000000000}</string>
 				<string key="NSMaxSize">{10000000000000, 10000000000000}</string>
 				<bool key="NSWindowIsRestorable">YES</bool>
 				<bool key="NSWindowIsRestorable">YES</bool>
 			</object>
 			</object>
@@ -1780,7 +1786,7 @@
 					<string>83.IBPluginDependency</string>
 					<string>83.IBPluginDependency</string>
 					<string>92.IBPluginDependency</string>
 					<string>92.IBPluginDependency</string>
 				</object>
 				</object>
-				<object class="NSMutableArray" key="dict.values">
+				<object class="NSArray" key="dict.values">
 					<bool key="EncodedWithXMLCoder">YES</bool>
 					<bool key="EncodedWithXMLCoder">YES</bool>
 					<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
 					<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
 					<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
 					<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
@@ -1901,7 +1907,7 @@
 							<string>saveDocumentAs:</string>
 							<string>saveDocumentAs:</string>
 							<string>saveDocumentTo:</string>
 							<string>saveDocumentTo:</string>
 						</object>
 						</object>
-						<object class="NSMutableArray" key="dict.values">
+						<object class="NSArray" key="dict.values">
 							<bool key="EncodedWithXMLCoder">YES</bool>
 							<bool key="EncodedWithXMLCoder">YES</bool>
 							<string>id</string>
 							<string>id</string>
 							<string>id</string>
 							<string>id</string>
@@ -1922,7 +1928,7 @@
 							<string>saveDocumentAs:</string>
 							<string>saveDocumentAs:</string>
 							<string>saveDocumentTo:</string>
 							<string>saveDocumentTo:</string>
 						</object>
 						</object>
-						<object class="NSMutableArray" key="dict.values">
+						<object class="NSArray" key="dict.values">
 							<bool key="EncodedWithXMLCoder">YES</bool>
 							<bool key="EncodedWithXMLCoder">YES</bool>
 							<object class="IBActionInfo">
 							<object class="IBActionInfo">
 								<string key="name">printDocument:</string>
 								<string key="name">printDocument:</string>
@@ -1974,7 +1980,7 @@
 							<string>runProject:</string>
 							<string>runProject:</string>
 							<string>saveFile:</string>
 							<string>saveFile:</string>
 						</object>
 						</object>
-						<object class="NSMutableArray" key="dict.values">
+						<object class="NSArray" key="dict.values">
 							<bool key="EncodedWithXMLCoder">YES</bool>
 							<bool key="EncodedWithXMLCoder">YES</bool>
 							<string>id</string>
 							<string>id</string>
 							<string>id</string>
 							<string>id</string>
@@ -2005,7 +2011,7 @@
 							<string>runProject:</string>
 							<string>runProject:</string>
 							<string>saveFile:</string>
 							<string>saveFile:</string>
 						</object>
 						</object>
-						<object class="NSMutableArray" key="dict.values">
+						<object class="NSArray" key="dict.values">
 							<bool key="EncodedWithXMLCoder">YES</bool>
 							<bool key="EncodedWithXMLCoder">YES</bool>
 							<object class="IBActionInfo">
 							<object class="IBActionInfo">
 								<string key="name">browseExamples:</string>
 								<string key="name">browseExamples:</string>
@@ -2061,7 +2067,7 @@
 							<string>projectMenu</string>
 							<string>projectMenu</string>
 							<string>window</string>
 							<string>window</string>
 						</object>
 						</object>
-						<object class="NSMutableArray" key="dict.values">
+						<object class="NSArray" key="dict.values">
 							<bool key="EncodedWithXMLCoder">YES</bool>
 							<bool key="EncodedWithXMLCoder">YES</bool>
 							<string>PolycodeView</string>
 							<string>PolycodeView</string>
 							<string>NSMenu</string>
 							<string>NSMenu</string>
@@ -2076,7 +2082,7 @@
 							<string>projectMenu</string>
 							<string>projectMenu</string>
 							<string>window</string>
 							<string>window</string>
 						</object>
 						</object>
-						<object class="NSMutableArray" key="dict.values">
+						<object class="NSArray" key="dict.values">
 							<bool key="EncodedWithXMLCoder">YES</bool>
 							<bool key="EncodedWithXMLCoder">YES</bool>
 							<object class="IBToOneOutletInfo">
 							<object class="IBToOneOutletInfo">
 								<string key="name">polycodeView</string>
 								<string key="name">polycodeView</string>
@@ -2126,10 +2132,10 @@
 				<string>NSMenuCheckmark</string>
 				<string>NSMenuCheckmark</string>
 				<string>NSMenuMixedState</string>
 				<string>NSMenuMixedState</string>
 			</object>
 			</object>
-			<object class="NSMutableArray" key="dict.values">
+			<object class="NSArray" key="dict.values">
 				<bool key="EncodedWithXMLCoder">YES</bool>
 				<bool key="EncodedWithXMLCoder">YES</bool>
-				<string>{9, 8}</string>
-				<string>{7, 2}</string>
+				<string>{11, 11}</string>
+				<string>{10, 3}</string>
 			</object>
 			</object>
 		</object>
 		</object>
 	</data>
 	</data>

+ 2 - 0
Modules/Contents/UI/Source/PolyUITextInput.cpp

@@ -90,6 +90,7 @@ UITextInput::UITextInput(bool multiLine, Number width, Number height) : ScreenEn
 	inputRect->addEventListener(this, InputEvent::EVENT_MOUSEOVER);
 	inputRect->addEventListener(this, InputEvent::EVENT_MOUSEOVER);
 	inputRect->addEventListener(this, InputEvent::EVENT_MOUSEOUT);
 	inputRect->addEventListener(this, InputEvent::EVENT_MOUSEOUT);
 	inputRect->processInputEvents = true;
 	inputRect->processInputEvents = true;
+	inputRect->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
 	
 	
 	selectorRectTop = new ScreenShape(ScreenShape::SHAPE_RECT, 1,1);
 	selectorRectTop = new ScreenShape(ScreenShape::SHAPE_RECT, 1,1);
 	selectorRectTop->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
 	selectorRectTop->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
@@ -288,6 +289,7 @@ void UITextInput::Resize(int x, int y) {
 	inputRect->resizeBox(x, y);
 	inputRect->resizeBox(x, y);
 	this->width = x;
 	this->width = x;
 	this->height = y;	
 	this->height = y;	
+	matrixDirty = true;	
 	setHitbox(x,y);
 	setHitbox(x,y);
 }
 }