diff --git a/Aman/AmanController.cpp b/Aman/AmanController.cpp index 1761fd1..8e88d3b 100644 --- a/Aman/AmanController.cpp +++ b/Aman/AmanController.cpp @@ -2,46 +2,22 @@ #include "AmanController.h" #include "AmanPlugIn.h" -#include "AmanTimeline.h" -#include "AmanTimelineView.h" #include "AmanWindow.h" -#include "TitleBar.h" - -#define THREE_HOURS 10800 -#define FIVE_MINUTES 300 AmanController::AmanController(AmanPlugIn* plugin) { this->amanModel = plugin; this->amanWindow = NULL; - - this->titleBar = std::make_shared(); - - this->titleBar->on("COLLAPSE_CLICKED", [&]() { - if (this->amanWindow->isExpanded()) { - this->amanWindow->collapse(); - } else { - this->amanWindow->expand(); - } - }); - - this->titleBar->on("MOUSE_PRESSED", [&]() { this->moveWindow = true; }); - - this->titleBar->on("RESIZE_PRESSED", [&]() { - if (this->amanWindow->isExpanded()) { - this->doResize = true; - } - }); } AmanController::~AmanController() {} -void AmanController::openWindow() { +void AmanController::modelLoaded() { if (this->amanWindow == nullptr) { - this->amanWindow = std::make_shared(this, titleBar, amanModel->getAvailableIds()); + this->amanWindow = std::make_shared(this, amanModel->getAvailableIds()); } } -void AmanController::dataUpdated() { +void AmanController::modelUpdated() { if (this->amanWindow != nullptr) { auto timelines = this->amanModel->getTimelines(activeTimelines); this->amanWindow->update(timelines); @@ -59,41 +35,5 @@ void AmanController::toggleTimeline(const std::string& id) { } else { activeTimelines.push_back(id); } - dataUpdated(); + modelUpdated(); } - -void AmanController::mousePressed(CPoint cursorPosition) { titleBar->mousePressed(cursorPosition); } - -void AmanController::mouseReleased(CPoint cursorPosition) { - this->moveWindow = false; - this->doResize = false; -} - -void AmanController::mouseMoved(CPoint cursorPosition) { - if (this->doResize) { - CPoint diff = cursorPosition - this->previousMousePosition; - this->amanWindow->resizeWindowBy(diff); - } else if (this->moveWindow && this->amanWindow != NULL) { - CPoint diff = cursorPosition - this->previousMousePosition; - this->amanWindow->moveWindowBy(diff); - } - - this->previousMousePosition = cursorPosition; -} - -void AmanController::mouseWheelSrolled(CPoint cursorPosition, short delta) { - auto allTimelines = this->amanModel->getTimelines(activeTimelines); - auto timelinePointedAt = this->amanWindow->getTimelineAt(allTimelines, cursorPosition); - if (timelinePointedAt) { - auto currentRange = timelinePointedAt->getRange(); - auto newRange = currentRange - delta; - auto limitReached = newRange < FIVE_MINUTES || newRange > THREE_HOURS; - - if (!limitReached) { - timelinePointedAt->setRange(newRange); - dataUpdated(); - } - } -} - -void AmanController::windowClosed() { this->amanWindow = NULL; } diff --git a/Aman/AmanController.h b/Aman/AmanController.h index 93cbd9b..f8f0e62 100644 --- a/Aman/AmanController.h +++ b/Aman/AmanController.h @@ -4,34 +4,21 @@ #include #include -class TitleBar; class AmanWindow; class AmanPlugIn; -class AmanTimeline; class AmanController { public: AmanController(AmanPlugIn* model); - void openWindow(); - void windowClosed(); - void mousePressed(CPoint cursorPosition); - void mouseReleased(CPoint cursorPosition); - void mouseMoved(CPoint cursorPosition); - void mouseWheelSrolled(CPoint cursorPosition, short delta); - void dataUpdated(); + void modelLoaded(); + void modelUpdated(); void toggleTimeline(const std::string& id); ~AmanController(); private: - bool moveWindow = false; - bool doResize = false; - CPoint mouseDownPosition; - CPoint previousMousePosition; - AmanPlugIn* amanModel; std::shared_ptr amanWindow; - std::shared_ptr titleBar; std::vector activeTimelines; }; diff --git a/Aman/AmanPlugIn.cpp b/Aman/AmanPlugIn.cpp index ef4c8db..59bbfea 100644 --- a/Aman/AmanPlugIn.cpp +++ b/Aman/AmanPlugIn.cpp @@ -39,8 +39,8 @@ AmanPlugIn::AmanPlugIn() : CPlugIn(COMPATIBILITY_CODE, "Arrival Manager", "1.4.0 loadTimelines("aman_profiles.json"); amanController = std::make_shared(this); - amanController->openWindow(); - amanController->dataUpdated(); + amanController->modelLoaded(); + amanController->modelUpdated(); } AmanPlugIn::~AmanPlugIn() { @@ -132,7 +132,7 @@ std::vector AmanPlugIn::getInboundsForFix(const std::string& fixNa void AmanPlugIn::OnTimer(int Counter) { // Runs every second - amanController->dataUpdated(); + amanController->modelUpdated(); } bool AmanPlugIn::OnCompileCommand(const char* sCommandLine) { @@ -145,7 +145,7 @@ bool AmanPlugIn::OnCompileCommand(const char* sCommandLine) { std::string command = args.at(1); if (command == "show") { - amanController->openWindow(); + amanController->modelLoaded(); cmdHandled = true; } else if (command == "clear") { timelines.clear(); @@ -168,7 +168,7 @@ bool AmanPlugIn::OnCompileCommand(const char* sCommandLine) { } } if (timelinesChanged) { - amanController->dataUpdated(); + amanController->modelUpdated(); saveToSettings(); } diff --git a/Aman/AmanWindow.cpp b/Aman/AmanWindow.cpp index fa4a644..54f1054 100644 --- a/Aman/AmanWindow.cpp +++ b/Aman/AmanWindow.cpp @@ -9,24 +9,42 @@ #include -AmanWindow::AmanWindow(AmanController* controller, std::shared_ptr titleBar, std::set ids) : Window("AmanWindow", "AMAN") { +#define THREE_HOURS 10800 +#define FIVE_MINUTES 300 + +AmanWindow::AmanWindow(AmanController* controller, std::set availProfiles) : Window("AmanWindow", "AMAN") { this->controller = controller; - this->titleBar = titleBar; + this->titleBar = std::make_shared(); this->menuBar = std::make_shared(); - std::vector options(ids.begin(), ids.end()); - static auto onSelection = [controller](const std::string& timelineId) { controller->toggleTimeline(timelineId); }; - this->profilesMenu = std::make_shared("Load", options, onSelection); + std::vector profileOptions(availProfiles.begin(), availProfiles.end()); + this->profilesMenu = std::make_shared("Load", profileOptions, onSelection); this->menuBar->addPopupMenu(profilesMenu); + + this->titleBar->on("COLLAPSE_CLICKED", [&]() { + if (this->isExpanded()) { + this->collapse(); + } else { + this->expand(); + } + }); + + this->titleBar->on("MOUSE_PRESSED", [&]() { this->moveWindow = true; }); + + this->titleBar->on("RESIZE_PRESSED", [&]() { + if (this->isExpanded()) { + this->doResize = true; + } + }); } AmanWindow::~AmanWindow() {} -void AmanWindow::update(std::shared_ptr>> timelines) { +void AmanWindow::update(timelineCollection timelines) { renderTimelinesMutex.lock(); // Wait for current render to complete timelinesToRender = timelines; renderTimelinesMutex.unlock(); @@ -37,16 +55,17 @@ void AmanWindow::update(std::shared_ptr timelineIds; + timelineView = clientRect; + timelineView.top += 35; + // This code runs on the window's thread, so we must make sure // the main thread is not currently writing to the shared AmanTimeline-vector renderTimelinesMutex.lock(); - timelineView = clientRect; - timelineView.top += 35; for (auto& timeline : *timelinesToRender) { - lastTimelineArea = AmanTimelineView::render(timeline, timelineView, hdc, lastTimelineArea.right); + previousTimelineArea = AmanTimelineView::render(timeline, timelineView, hdc, previousTimelineArea.right); timelineIds.push_back(timeline->getIdentifier()); } renderTimelinesMutex.unlock(); @@ -59,7 +78,7 @@ void AmanWindow::drawContent(HDC hdc, CRect clientRect) { this->menuBar->render(hdc, titleBarRect); } -std::shared_ptr AmanWindow::getTimelineAt(std::shared_ptr>> timelines, CPoint cursorPosition) { +std::shared_ptr AmanWindow::getTimelineAt(timelineCollection timelines, CPoint cursorPosition) { int nextOffsetX = 0; for (auto& timeline : *timelines) { CRect area = AmanTimelineView::getArea(timeline, timelineView, nextOffsetX); @@ -93,31 +112,52 @@ bool AmanWindow::isExpanded() { } void AmanWindow::mousePressed(CPoint cursorPosClient) { - controller->mousePressed(cursorPosClient); if (menuBar->onMouseClick(cursorPosClient)) { requestRepaint(); } + titleBar->mousePressed(cursorPosClient); + this->mouseDownPosition = cursorPosClient; } void AmanWindow::mouseReleased(CPoint cursorPosClient) { - controller->mouseReleased(cursorPosClient); + this->moveWindow = false; + this->doResize = false; } void AmanWindow::mouseMoved(CPoint cursorPosClient) { + ClientToScreen(hwnd, &cursorPosClient); + + CPoint diff = cursorPosClient - prevMousePosition; + + if (this->doResize) { + this->resizeWindowBy(diff); + } else if (this->moveWindow) { + this->moveWindowBy(diff); + } + if (menuBar->onMouseMove(cursorPosClient)) { requestRepaint(); } - ClientToScreen(hwnd, &cursorPosClient); - controller->mouseMoved(cursorPosClient); + + prevMousePosition = cursorPosClient; } void AmanWindow::mouseWheelSrolled(CPoint cursorPosClient, short delta) { - controller->mouseWheelSrolled(cursorPosClient, delta); + auto timelinePointedAt = getTimelineAt(timelinesToRender, cursorPosClient); + if (timelinePointedAt) { + auto currentRange = timelinePointedAt->getRange(); + auto newRange = currentRange - delta; + auto limitReached = newRange < FIVE_MINUTES || newRange > THREE_HOURS; + + if (!limitReached) { + timelinePointedAt->setRange(newRange); + requestRepaint(); + } + } if (menuBar->onMouseScroll(delta)) { requestRepaint(); } } void AmanWindow::windowClosed() { - controller->windowClosed(); } \ No newline at end of file diff --git a/Aman/AmanWindow.h b/Aman/AmanWindow.h index 57f8453..dd6e0eb 100644 --- a/Aman/AmanWindow.h +++ b/Aman/AmanWindow.h @@ -14,18 +14,15 @@ class TitleBar; class PopupMenu; class MenuBar; +typedef std::shared_ptr>> timelineCollection; + class AmanWindow : public Window { public: - AmanWindow(AmanController* controller, std::shared_ptr titleBar, std::set ids); + AmanWindow(AmanController* controller, std::set availProfiles); ~AmanWindow(); - void update(std::shared_ptr>> timelines); - void collapse(); - void expand(); - bool isExpanded(); - - std::shared_ptr getTimelineAt(std::shared_ptr>> all, CPoint cursorPosition); + void update(timelineCollection timelines); private: AmanController* controller; @@ -33,12 +30,23 @@ class AmanWindow : public Window { std::shared_ptr menuBar; std::shared_ptr profilesMenu; - std::shared_ptr>> timelinesToRender; + timelineCollection timelinesToRender; std::mutex renderTimelinesMutex; int originalHeight; CRect timelineView; + CRect originalSize; + CPoint mouseDownPosition; + CPoint prevMousePosition; + bool moveWindow = false; + bool doResize = false; + + void collapse(); + void expand(); + bool isExpanded(); + std::shared_ptr getTimelineAt(timelineCollection all, CPoint cursorPosition); + void mousePressed(CPoint cursorPosClient) override; void mouseReleased(CPoint cursorPosClient) override; void mouseMoved(CPoint cursorPosClient) override; diff --git a/Aman/Window.cpp b/Aman/Window.cpp index 8a2b952..6ffd825 100644 --- a/Aman/Window.cpp +++ b/Aman/Window.cpp @@ -10,17 +10,13 @@ Window::Window(const std::string& className, const std::string& windowName) { show(SW_SHOWNORMAL); // Thread responsible for updating the window - exit = false; CreateThread(0, NULL, lookForMessages, this, NULL, &threadId); } Window::~Window() { - exit = true; - //PostQuitMessage(0); SendMessage(hwnd, WM_CLOSE, 0, 0); WaitForSingleObject(&threadId, INFINITE); int i = 32; - //TerminateThread(&threadId, 0); } // Window thread procedure @@ -38,7 +34,7 @@ bool Window::processNextMessage() { if (::GetMessage(&msg, hwnd, 0, 0)) { ::TranslateMessage(&msg); ::DispatchMessage(&msg); - } else if(exit) { + } else { return false; }