Просмотр исходного кода

Added YES and NO events to UIEvent, added Yes/No/Cancel popup to IDE, IDE will now ask if you want to save file before closing if there are unsaved changes

Ivan Safrin 12 лет назад
Родитель
Сommit
8214319cdc

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

@@ -187,6 +187,7 @@ public:
 	
 	TextInputPopup *textInputPopup;
 	YesNoPopup *yesNoPopup;
+	YesNoCancelPopup *yesNoCancelPopup;
 	
 	ScreenEntity *welcomeEntity;	
 	PolycodeProjectBrowser *projectBrowser;

+ 18 - 0
IDE/Contents/Include/ToolWindows.h

@@ -64,4 +64,22 @@ class YesNoPopup : public UIWindow {
 		ScreenEntity *buttonAnchor;
 		UIButton *cancelButton;
 		UIButton *okButton;	
+};
+
+class YesNoCancelPopup : public UIWindow {
+	public:
+		YesNoCancelPopup();
+		~YesNoCancelPopup();
+		
+		void setCaption(String caption);
+		void handleEvent(Event *event);
+		
+		String action;
+	
+		ScreenLabel *captionLabel;
+	
+		ScreenEntity *buttonAnchor;
+		UIButton *cancelButton;
+		UIButton *noButton;		
+		UIButton *okButton;
 };

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

@@ -601,6 +601,10 @@ PolycodeFrame::PolycodeFrame() : ScreenEntity() {
 	yesNoPopup = new YesNoPopup();
 	yesNoPopup->visible = false;
 	
+	yesNoCancelPopup = new YesNoCancelPopup();
+	yesNoCancelPopup->visible = false;
+	
+	
 	aboutWindow = new UIWindow("", 800, 440);
 	aboutWindow->closeOnEscape = true;
 	ScreenImage *aboutImage = new ScreenImage("Images/about.png");

+ 40 - 6
IDE/Contents/Source/PolycodeIDEApp.cpp

@@ -87,8 +87,13 @@ PolycodeIDEApp::PolycodeIDEApp(PolycodeView *view) : EventDispatcher() {
 	frame->console->backtraceWindow->addEventListener(this, BackTraceEvent::EVENT_BACKTRACE_SELECTED);
 
 	frame->textInputPopup->addEventListener(this, UIEvent::OK_EVENT);	
+
 	frame->yesNoPopup->addEventListener(this, UIEvent::OK_EVENT);
 	frame->yesNoPopup->addEventListener(this, UIEvent::CANCEL_EVENT);
+
+	frame->yesNoCancelPopup->addEventListener(this, UIEvent::YES_EVENT);
+	frame->yesNoCancelPopup->addEventListener(this, UIEvent::NO_EVENT);
+
 	
 	frame->newProjectWindow->addEventListener(this, UIEvent::OK_EVENT);
 	frame->exportProjectWindow->addEventListener(this, UIEvent::OK_EVENT);
@@ -227,16 +232,19 @@ void PolycodeIDEApp::removeEditor(PolycodeEditor *editor) {
 	if(!editor)
 		return;
 		
-//	if(editor->hasChanges()) {
-//		printf("HAS CHANGES!\n");
-//	} else {	
+	if(editor->hasChanges()) {
+		OSFileEntry entry(editor->getFilePath(), OSFileEntry::TYPE_FILE);	
+		frame->yesNoCancelPopup->setCaption("The file \""+entry.name+"\" has unsaved changes. Save before closing?");
+		frame->yesNoCancelPopup->action = "closeFile";
+		frame->showModal(frame->yesNoCancelPopup);		
+	} else {	
 		frame->removeEditor(editor);
 		editorManager->destroyEditor(editor);
 		if(editorManager->openEditors.size() > 0) {
 			editorManager->setCurrentEditor(editorManager->openEditors[0]);
 			frame->showEditor(editorManager->openEditors[0]);
 		}
-//	}
+	}
 }
 
 void PolycodeIDEApp::closeFile() {
@@ -579,8 +587,34 @@ void PolycodeIDEApp::handleEvent(Event *event) {
 		}
 	}
 
-	if(event->getDispatcher() == frame->yesNoPopup) {
-
+	if(event->getDispatcher() == frame->yesNoCancelPopup) {
+		if(frame->yesNoCancelPopup->action == "closeFile") {
+			switch(event->getEventCode()) {
+				case UIEvent::YES_EVENT:
+				{
+					PolycodeEditor *editor = editorManager->getCurrentEditor();
+					if(editor) {
+						editor->saveFile();
+						closeFile();
+					}
+					frame->hideModal();					
+				}
+				break;
+				case UIEvent::NO_EVENT:
+				{
+					PolycodeEditor *editor = editorManager->getCurrentEditor();
+					if(editor) {
+						editor->setHasChanges(false);
+						closeFile();
+					}
+					frame->hideModal();
+				}
+				break;
+				case UIEvent::CANCEL_EVENT:
+				break;
+			}
+		}			
+	} else if(event->getDispatcher() == frame->yesNoPopup) {
 		if(event->getEventType() == "UIEvent" && event->getEventCode() == UIEvent::CANCEL_EVENT) {
 			if(frame->yesNoPopup->action == "saveAndRun") {
 				runNextFrame = true;			

+ 62 - 0
IDE/Contents/Source/ToolWindows.cpp

@@ -125,4 +125,66 @@ void YesNoPopup::handleEvent(Event *event) {
 
 YesNoPopup::~YesNoPopup() {
 	
+}
+
+YesNoCancelPopup::YesNoCancelPopup() : UIWindow(L"", 300, 80) {
+	
+	captionLabel = new ScreenLabel("This is a caption", 12);	
+	addChild(captionLabel);
+	captionLabel->setPosition(padding, 35);
+		
+	buttonAnchor = new ScreenEntity();
+	buttonAnchor->processInputEvents = true;
+	addChild(buttonAnchor);
+	
+	noButton = new UIButton(L"No", 100);
+	noButton->addEventListener(this, UIEvent::CLICK_EVENT);
+	buttonAnchor->addChild(noButton);
+	noButton->setPosition(0, 60);		
+	
+	okButton = new UIButton(L"Yes", 100);
+	okButton->addEventListener(this, UIEvent::CLICK_EVENT);
+	buttonAnchor->addChild(okButton);
+	okButton->setPosition(120, 60);
+	
+	cancelButton = new UIButton(L"Cancel", 100);
+	cancelButton->addEventListener(this, UIEvent::CLICK_EVENT);
+	buttonAnchor->addChild(cancelButton);
+	cancelButton->setPosition(240, 60);		
+
+	closeOnEscape = true;
+
+}
+
+void YesNoCancelPopup::setCaption(String caption) {
+	captionLabel->setText(caption);
+	setWindowSize(captionLabel->getWidth() + 50, 80);
+	captionLabel->setPosition(padding + (captionLabel->getWidth() + 50 - captionLabel->getWidth()) / 2.0, 35);
+	buttonAnchor->setPosition(padding + ((captionLabel->getWidth() + 50 - 360) / 2.0), 0);
+}
+
+void YesNoCancelPopup::handleEvent(Event *event) {
+	if(event->getEventType() == "UIEvent") {
+		if(event->getEventCode() == UIEvent::CLICK_EVENT) {
+			if(event->getDispatcher() == okButton) {
+				dispatchEvent(new UIEvent(), UIEvent::YES_EVENT);						
+			}
+			
+			if(event->getDispatcher() == noButton) {
+				dispatchEvent(new UIEvent(), UIEvent::NO_EVENT);
+			}									
+			
+			if(event->getDispatcher() == cancelButton) {
+				dispatchEvent(new UIEvent(), UIEvent::CLOSE_EVENT);
+				dispatchEvent(new UIEvent(), UIEvent::CANCEL_EVENT);				
+			}									
+			
+		}
+	}
+	UIWindow::handleEvent(event);	
+}
+
+
+YesNoCancelPopup::~YesNoCancelPopup() {
+	
 }

+ 2 - 1
Modules/Contents/UI/Include/PolyUIEvent.h

@@ -37,7 +37,8 @@ namespace Polycode {
 			static const int OK_EVENT = EVENTBASE_UIEVENT+2;
 			static const int CANCEL_EVENT = EVENTBASE_UIEVENT+3;
 			static const int CHANGE_EVENT = EVENTBASE_UIEVENT+4;
-						
+			static const int YES_EVENT = EVENTBASE_UIEVENT+5;
+			static const int NO_EVENT = EVENTBASE_UIEVENT+6;									
 		protected: