فهرست منبع

Proper player closing procedure on Mac, IDE will disconnect existing clients before launch, proper centering on line highlight

Ivan Safrin 13 سال پیش
والد
کامیت
a5c2354aed

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

@@ -119,6 +119,7 @@ namespace Polycode {
 		String executeExternalCommand(String command);
 		
 		void launchApplicationWithFile(String application, String file);
+		void openFileWithApplication(String file, String application);
 		
 		void setCursor(int cursorType);
 		void warpCursor(int x, int y);

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

@@ -252,6 +252,14 @@ void CocoaCore::setVideoMode(int xRes, int yRes, bool fullScreen, bool vSync, in
 	*/
 }
 
+void CocoaCore::openFileWithApplication(String file, String application) {
+	NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
+	NSString *filePath = [NSString stringWithCString:file.c_str()];
+	NSString *appString = [NSString stringWithCString:application.c_str()];
+		
+	[workspace openFile: filePath withApplication: appString andDeactivate: YES];
+}
+
 void CocoaCore::launchApplicationWithFile(String application, String file) {
 	NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
 	NSURL *url = [NSURL fileURLWithPath: [NSString stringWithCString:application.c_str()]];

+ 2 - 1
Core/Contents/Source/PolyServer.cpp

@@ -104,6 +104,8 @@ void Server::handlePeerConnection(PeerConnection *connection) {
 }
 
 void Server::DisconnectClient(ServerClient *client) {
+	sendReliableDataToClient(client, NULL, 0, PACKET_TYPE_DISONNECT);
+
 	for(unsigned int i=0;i<clients.size();i++) {
 		if(clients[i] == client) {			
 			clients.erase(clients.begin()+i);
@@ -133,7 +135,6 @@ void Server::handlePacket(Packet *packet, PeerConnection *connection) {
 		break;
 		case PACKET_TYPE_DISONNECT:
 		{
-			sendReliableDataToClient(client, NULL, 0, PACKET_TYPE_DISONNECT);
 			DisconnectClient(client);
 		}
 		break;		

+ 2 - 0
IDE/Contents/Include/PolycodeFrame.h

@@ -71,6 +71,8 @@ public:
 	PolycodeProjectBrowser *projectBrowser;
 		
 	UIImageButton *playButton;
+	UIImageButton *stopButton;
+		
 	UIHSizer *mainSizer;
 	
 	PolycodeConsole *console;

+ 2 - 0
IDE/Contents/Include/PolycodeIDEApp.h

@@ -57,6 +57,8 @@ public:
 	
 	void openFile(OSFileEntry file);
 	
+	void stopProject();
+	
 	// menu commands
 	void renameFile();
 	void removeFile();

+ 2 - 0
IDE/Contents/Include/PolycodeRemoteDebugger.h

@@ -55,6 +55,8 @@ class PolycodeRemoteDebugger : EventHandler {
 		void handleEvent(Event *event);
 			
 		bool isConnected();
+		
+		void Disconnect();
 			
 		static const int EVENT_DEBUG_ERROR = 32;
 		static const int EVENT_DEBUG_PRINT = 33;

BIN
IDE/Contents/Resources/Images/stop_button.png


+ 5 - 0
IDE/Contents/Source/PolycodeFrame.cpp

@@ -93,6 +93,11 @@ PolycodeFrame::PolycodeFrame() : ScreenEntity() {
 	playButton = new UIImageButton("play_button.png");
 	addChild(playButton);
 	playButton->setPosition(10,8);
+
+	stopButton = new UIImageButton("stop_button.png");
+	addChild(stopButton);
+	stopButton->setPosition(10,8);
+
 	
 	resizer = new ScreenImage("corner_resize.png");	
 	addChild(resizer);

+ 34 - 1
IDE/Contents/Source/PolycodeIDEApp.cpp

@@ -61,7 +61,8 @@ PolycodeIDEApp::PolycodeIDEApp(PolycodeView *view) : EventDispatcher() {
 	frame->exampleBrowserWindow->addEventListener(this, UIEvent::OK_EVENT);
 	
 	frame->playButton->addEventListener(this, UIEvent::CLICK_EVENT);
-	
+	frame->stopButton->addEventListener(this, UIEvent::CLICK_EVENT);
+		
 	screen->addChild(frame);
 	
 	projectManager = new PolycodeProjectManager();
@@ -165,7 +166,17 @@ void PolycodeIDEApp::browseExamples() {
 
 }
 
+void PolycodeIDEApp::stopProject() {
+	printf("Disconnecting clients...\n");
+	if(debugger->isConnected()) {
+		debugger->Disconnect();
+	}
+}
+
 void PolycodeIDEApp::runProject() {
+	printf("Running project...\n");
+	stopProject();
+
 	if(projectManager->getActiveProject()) {
 		String outPath = PolycodeToolLauncher::generateTempPath() + ".polyapp";
 		PolycodeToolLauncher::buildProject(projectManager->getActiveProject(), outPath);
@@ -296,6 +307,13 @@ void PolycodeIDEApp::handleEvent(Event *event) {
 			runProject();
 		}
 	}
+
+	if(event->getDispatcher() == frame->stopButton) {	
+		if(event->getEventType() == "UIEvent" && event->getEventCode() == UIEvent::CLICK_EVENT) {
+			stopProject();
+		}
+	}
+
 	
 	if(event->getDispatcher() == frame->textInputPopup) {
 		if(event->getEventType() == "UIEvent" && event->getEventCode() == UIEvent::OK_EVENT) {
@@ -385,6 +403,21 @@ PolycodeIDEApp::~PolycodeIDEApp() {
 
 bool PolycodeIDEApp::Update() {
 
+	if(debugger->isConnected()) {
+			frame->stopButton->visible = true;
+			frame->stopButton->enabled = true;			
+			
+			frame->playButton->visible = false;
+			frame->playButton->enabled = false;			
+			
+	} else {
+			frame->stopButton->visible = false;
+			frame->stopButton->enabled = false;			
+			
+			frame->playButton->visible = true;
+			frame->playButton->enabled = true;				
+	}
+
 	if(projectManager->getProjectCount() == 1) {
 		projectManager->setActiveProject(projectManager->getProjectByIndex(0));
 	}

+ 7 - 0
IDE/Contents/Source/PolycodeRemoteDebugger.cpp

@@ -45,6 +45,13 @@ void PolycodeRemoteDebugger::injectCode(String code) {
 	server->sendReliableDataToAllClients((char*)code.c_str(), code.length()+1, EVENT_INJECT_CODE);
 }
 
+void PolycodeRemoteDebugger::Disconnect() {
+	for(int i=0; i < debuggerClients.size(); i++) {
+		server->DisconnectClient(debuggerClients[i]->client);
+	}
+	debuggerClients.clear();
+}
+
 void PolycodeRemoteDebugger::handleEvent(Event *event) {
 
 	for(int i=0; i < debuggerClients.size(); i++) {

+ 1 - 0
IDE/Contents/Source/PolycodeTextEditor.cpp

@@ -231,6 +231,7 @@ bool PolycodeTextEditor::openFile(OSFileEntry filePath) {
 void PolycodeTextEditor::highlightLine(unsigned int lineNumber) {
 	int lineSize = textInput->getLineText(lineNumber-1).length();
 	textInput->setSelection(lineNumber-1, lineNumber-1, 0, lineSize);
+	textInput->showLine(lineNumber, false);
 }
 
 void PolycodeTextEditor::saveFile() {

+ 2 - 1
IDE/Contents/Source/PolycodeToolLauncher.cpp

@@ -83,7 +83,8 @@ void PolycodeToolLauncher::runPolyapp(String polyappPath) {
 	String polycodeBasePath = CoreServices::getInstance()->getCore()->getDefaultWorkingDirectory();
 	String command = polycodeBasePath+"/Standalone/Player/PolycodePlayer.app";
 	
-	cocoaCore->launchApplicationWithFile(command, polyappPath);
+//	cocoaCore->launchApplicationWithFile(command, polyappPath);
+	cocoaCore->openFileWithApplication(polyappPath, command);
 #else
 	PolycodeRunner *runner = new PolycodeRunner(polyappPath);
 	CoreServices::getInstance()->getCore()->createThread(runner);

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

@@ -799,6 +799,11 @@ void UITextInput::Paste() {
 }
 
 void UITextInput::showLine(unsigned int lineNumber, bool top) {
+	if(top) {
+		scrollContainer->setScrollValue(0.0, ((((lineNumber) * ((lineHeight+lineSpacing)))) + padding)/scrollContainer->getContentSize().y);
+	} else {
+		scrollContainer->setScrollValue(0.0, (((((lineNumber) * ((lineHeight+lineSpacing)))) + padding-(scrollContainer->getHeight()/2.0))/scrollContainer->getContentSize().y));	
+	}
 }
 
 void UITextInput::onKeyDown(PolyKEY key, wchar_t charCode) {

+ 3 - 2
Player/Contents/Include/PolycodePlayer.h

@@ -94,6 +94,7 @@ public:
 	static const int EVENT_PRINT = 1;
 	static const int EVENT_RESIZE = 2;
 	static const int EVENT_REMOVE = 3;
+	static const int EVENT_CLOSE = 4;
 };
 
 class PolycodePlayer : public EventDispatcher {
@@ -130,6 +131,8 @@ public:
 	Core *core;	
 	
 	String fullPath;
+	
+	bool useDebugger;	
 		
 protected:
 
@@ -139,8 +142,6 @@ protected:
 	
 	lua_State *L;		
 	
-	bool useDebugger;
-	
 	
 	bool doCodeInject;
 	String injectCodeString;

+ 9 - 0
Player/Contents/Platform/Darwin/MyDocument.h

@@ -49,6 +49,9 @@ public:
 			case PolycodeDebugEvent::EVENT_PRINT:
 				[playerDocument printToConsole: [NSString stringWithCString:debugEvent->errorString.c_str()]];				
 				break;
+			case PolycodeDebugEvent::EVENT_CLOSE:
+				[playerDocument needToClosePlayer];
+			break;				
 			case PolycodeDebugEvent::EVENT_RESIZE:
 //				printf("RERERERERESIZE\n");
 //				[playerDocument resizeDocument: debugEvent->xRes withYRes: debugEvent->yRes];
@@ -69,12 +72,18 @@ public:
 	NSWindow *consoleWindow;
 	NSTextView *consoleTextView;
 	bool showingConsole;
+	bool playerRunning;
+	bool needsToClose;
+	
 }
 
 - (void) printToConsole: (NSString*) message;
 - (void) handleDebugError: (NSString*) error onLine:(int) lineNumber;
 - (IBAction) showConsoleWindow: (id) sender;
 
+- (void) destroyPlayer;
+- (void) needToClosePlayer;
+
 @property (assign) IBOutlet PolycodeView *mainView;
 @property (assign) IBOutlet NSWindow *consoleWindow;
 @property (assign) IBOutlet NSTextView *consoleTextView;

+ 30 - 9
Player/Contents/Platform/Darwin/MyDocument.mm

@@ -32,7 +32,9 @@ THE SOFTWARE.
 {
     self = [super init];
     if (self) {
+			playerRunning = true;
 			showingConsole = NO;
+			needsToClose = false;
     }
     return self;
 }
@@ -49,9 +51,11 @@ THE SOFTWARE.
 	player =  new CocoaPolycodePlayer(mainView, [docFileName cStringUsingEncoding:NSASCIIStringEncoding], false, true);
 	playerProxy = new PolycodeProxy();
 	playerProxy->playerDocument = self;
+	
 	player->addEventListener(playerProxy, PolycodeDebugEvent::EVENT_RESIZE);
 	player->addEventListener(playerProxy, PolycodeDebugEvent::EVENT_PRINT);		
 	player->addEventListener(playerProxy, PolycodeDebugEvent::EVENT_ERROR);			
+	player->addEventListener(playerProxy, PolycodeDebugEvent::EVENT_CLOSE);				
 	player->windowData = self;
 	
 	player->runPlayer();
@@ -61,6 +65,23 @@ THE SOFTWARE.
 	[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSEventTrackingRunLoopMode]; // ensure timer fires during resize		
 }
 
+- (void) destroyPlayer {
+	if(playerRunning) {
+		printf("DESTROYING PLAYER");
+		playerRunning = false;
+		[timer invalidate];
+		[timer release];	
+		delete player;
+		delete playerProxy;	
+		player = NULL;
+		playerProxy = NULL;		
+	}	
+}	
+
+- (void) needToClosePlayer {
+	needsToClose = true;
+}
+
 - (IBAction) showConsoleWindow: (id) sender 
 {
 	if(!showingConsole) {
@@ -90,8 +111,10 @@ NSMutableAttributedString *str = [[NSMutableAttributedString alloc ]initWithStri
 [textStorage endEditing];
 [str release];
 
-showingConsole = NO;
-[self showConsoleWindow:self];
+	if(!player->useDebugger) {
+		showingConsole = NO;
+		[self showConsoleWindow:self];
+	}
 
 }
 
@@ -114,8 +137,10 @@ NSMutableAttributedString *str = [[NSMutableAttributedString alloc ]initWithStri
 - (void)animationTimer:(NSTimer *)timer
 {
 
-	if(!player->Update()) {
-		[self close];
+	if(player) {
+		if(!player->Update() || needsToClose) {
+			[self close];
+		}
 	}
 }
 
@@ -123,11 +148,7 @@ NSMutableAttributedString *str = [[NSMutableAttributedString alloc ]initWithStri
 
 - (void)close
 {
-	[timer invalidate];
-	[timer release];	
-	delete player;
-	delete playerProxy;
-	
+	[self destroyPlayer];
 	[super close];
 
 }

+ 20 - 2
Player/Contents/Source/PolycodePlayer.cpp

@@ -26,9 +26,20 @@ THE SOFTWARE.
 PolycodeRemoteDebuggerClient::PolycodeRemoteDebuggerClient() : EventDispatcher() {
 	client = new Client(6445, 1);
 	client->Connect("127.0.0.1", 4630);	
+	
+	client->addEventListener(this, ClientEvent::EVENT_SERVER_DISCONNECTED);
 }
 
 void PolycodeRemoteDebuggerClient::handleEvent(Event *event) {
+
+	if(event->getDispatcher() == client) {
+		switch(event->getEventCode()) {
+			case ClientEvent::EVENT_SERVER_DISCONNECTED:
+				dispatchEvent(new Event(), Event::COMPLETE_EVENT);
+			break;
+		}
+	} else {
+
 	PolycodeDebugEvent *debugEvent = (PolycodeDebugEvent*) event;
 	switch(event->getEventCode()) {
 		case PolycodeDebugEvent::EVENT_PRINT:
@@ -68,6 +79,7 @@ void PolycodeRemoteDebuggerClient::handleEvent(Event *event) {
 			}
 		break;		
 	}
+	}
 }
 
 PolycodeRemoteDebuggerClient::~PolycodeRemoteDebuggerClient() {
@@ -605,10 +617,10 @@ void PolycodePlayer::loadFile(const char *fileName) {
 	
 	if(useDebugger) {
 	
-		// clear the other listeners
-		this->removeAllHandlers();
 			
 		remoteDebuggerClient = new PolycodeRemoteDebuggerClient();
+		remoteDebuggerClient->addEventListener(this, Event::COMPLETE_EVENT);
+		
 		this->addEventListener(remoteDebuggerClient, PolycodeDebugEvent::EVENT_PRINT);
 		this->addEventListener(remoteDebuggerClient, PolycodeDebugEvent::EVENT_ERROR);		
 		remoteDebuggerClient->client->addEventListener(this, ClientEvent::EVENT_CLIENT_READY);
@@ -642,6 +654,12 @@ void PolycodePlayer::handleEvent(Event *event) {
 		runFile(fullPath);
 		debuggerTimer->Pause(true);
 	}
+	
+	if(event->getDispatcher() == remoteDebuggerClient) {
+		if(event->getEventCode() == Event::COMPLETE_EVENT) {
+			dispatchEvent(new PolycodeDebugEvent(), PolycodeDebugEvent::EVENT_CLOSE);
+		}
+	}
 
 	if(event->getDispatcher() == remoteDebuggerClient->client) {
 		ClientEvent *clientEvent = (ClientEvent*) event;