Skip to content

Commit

Permalink
Move most of the view logic from AmanController to AmanWindow
Browse files Browse the repository at this point in the history
  • Loading branch information
EvenAR committed Dec 16, 2020
1 parent 6709236 commit 257fb4b
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 114 deletions.
68 changes: 4 additions & 64 deletions Aman/AmanController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<TitleBar>();

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<AmanWindow>(this, titleBar, amanModel->getAvailableIds());
this->amanWindow = std::make_shared<AmanWindow>(this, amanModel->getAvailableIds());
}
}

void AmanController::dataUpdated() {
void AmanController::modelUpdated() {
if (this->amanWindow != nullptr) {
auto timelines = this->amanModel->getTimelines(activeTimelines);
this->amanWindow->update(timelines);
Expand All @@ -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; }
17 changes: 2 additions & 15 deletions Aman/AmanController.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,21 @@
#include <string>
#include <memory>

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> amanWindow;
std::shared_ptr<TitleBar> titleBar;

std::vector<std::string> activeTimelines;
};
10 changes: 5 additions & 5 deletions Aman/AmanPlugIn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ AmanPlugIn::AmanPlugIn() : CPlugIn(COMPATIBILITY_CODE, "Arrival Manager", "1.4.0
loadTimelines("aman_profiles.json");

amanController = std::make_shared<AmanController>(this);
amanController->openWindow();
amanController->dataUpdated();
amanController->modelLoaded();
amanController->modelUpdated();
}

AmanPlugIn::~AmanPlugIn() {
Expand Down Expand Up @@ -132,7 +132,7 @@ std::vector<AmanAircraft> AmanPlugIn::getInboundsForFix(const std::string& fixNa

void AmanPlugIn::OnTimer(int Counter) {
// Runs every second
amanController->dataUpdated();
amanController->modelUpdated();
}

bool AmanPlugIn::OnCompileCommand(const char* sCommandLine) {
Expand All @@ -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();
Expand All @@ -168,7 +168,7 @@ bool AmanPlugIn::OnCompileCommand(const char* sCommandLine) {
}
}
if (timelinesChanged) {
amanController->dataUpdated();
amanController->modelUpdated();
saveToSettings();
}

Expand Down
74 changes: 57 additions & 17 deletions Aman/AmanWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,42 @@

#include <algorithm>

AmanWindow::AmanWindow(AmanController* controller, std::shared_ptr<TitleBar> titleBar, std::set<std::string> ids) : Window("AmanWindow", "AMAN") {
#define THREE_HOURS 10800
#define FIVE_MINUTES 300

AmanWindow::AmanWindow(AmanController* controller, std::set<std::string> availProfiles) : Window("AmanWindow", "AMAN") {
this->controller = controller;
this->titleBar = titleBar;
this->titleBar = std::make_shared<TitleBar>();
this->menuBar = std::make_shared<MenuBar>();

std::vector<std::string> options(ids.begin(), ids.end());

static auto onSelection = [controller](const std::string& timelineId) {
controller->toggleTimeline(timelineId);
};

this->profilesMenu = std::make_shared<PopupMenu>("Load", options, onSelection);
std::vector<std::string> profileOptions(availProfiles.begin(), availProfiles.end());
this->profilesMenu = std::make_shared<PopupMenu>("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<std::vector<std::shared_ptr<AmanTimeline>>> timelines) {
void AmanWindow::update(timelineCollection timelines) {
renderTimelinesMutex.lock(); // Wait for current render to complete
timelinesToRender = timelines;
renderTimelinesMutex.unlock();
Expand All @@ -37,16 +55,17 @@ void AmanWindow::update(std::shared_ptr<std::vector<std::shared_ptr<AmanTimeline
void AmanWindow::drawContent(HDC hdc, CRect clientRect) {
FillRect(hdc, &clientRect, AMAN_BRUSH_MAIN_BACKGROUND);

CRect lastTimelineArea;
CRect previousTimelineArea;
std::vector<std::string> 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();
Expand All @@ -59,7 +78,7 @@ void AmanWindow::drawContent(HDC hdc, CRect clientRect) {
this->menuBar->render(hdc, titleBarRect);
}

std::shared_ptr<AmanTimeline> AmanWindow::getTimelineAt(std::shared_ptr<std::vector<std::shared_ptr<AmanTimeline>>> timelines, CPoint cursorPosition) {
std::shared_ptr<AmanTimeline> AmanWindow::getTimelineAt(timelineCollection timelines, CPoint cursorPosition) {
int nextOffsetX = 0;
for (auto& timeline : *timelines) {
CRect area = AmanTimelineView::getArea(timeline, timelineView, nextOffsetX);
Expand Down Expand Up @@ -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();
}
24 changes: 16 additions & 8 deletions Aman/AmanWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,39 @@ class TitleBar;
class PopupMenu;
class MenuBar;

typedef std::shared_ptr<std::vector<std::shared_ptr<AmanTimeline>>> timelineCollection;

class AmanWindow : public Window {

public:
AmanWindow(AmanController* controller, std::shared_ptr<TitleBar> titleBar, std::set<std::string> ids);
AmanWindow(AmanController* controller, std::set<std::string> availProfiles);
~AmanWindow();

void update(std::shared_ptr<std::vector<std::shared_ptr<AmanTimeline>>> timelines);
void collapse();
void expand();
bool isExpanded();

std::shared_ptr<AmanTimeline> getTimelineAt(std::shared_ptr<std::vector<std::shared_ptr<AmanTimeline>>> all, CPoint cursorPosition);
void update(timelineCollection timelines);

private:
AmanController* controller;
std::shared_ptr<TitleBar> titleBar;
std::shared_ptr<MenuBar> menuBar;
std::shared_ptr<PopupMenu> profilesMenu;

std::shared_ptr<std::vector<std::shared_ptr<AmanTimeline>>> 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<AmanTimeline> getTimelineAt(timelineCollection all, CPoint cursorPosition);

void mousePressed(CPoint cursorPosClient) override;
void mouseReleased(CPoint cursorPosClient) override;
void mouseMoved(CPoint cursorPosClient) override;
Expand Down
6 changes: 1 addition & 5 deletions Aman/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -38,7 +34,7 @@ bool Window::processNextMessage() {
if (::GetMessage(&msg, hwnd, 0, 0)) {
::TranslateMessage(&msg);
::DispatchMessage(&msg);
} else if(exit) {
} else {
return false;
}

Expand Down

0 comments on commit 257fb4b

Please sign in to comment.