From d54d8587c9274deb0d7a9734d70d98b62992982e Mon Sep 17 00:00:00 2001 From: Erik Faye-Lund Date: Sat, 9 Feb 2008 23:50:45 +0000 Subject: [PATCH] more. stuff. --- synceditdata.h | 93 +++++++++++++++++++++++++++++++++++++++++++++++++------- synctracker2.cpp | 14 ++++----- trackview.cpp | 85 +++++++++++++++++++++++++-------------------------- trackview.h | 3 ++ 4 files changed, 134 insertions(+), 61 deletions(-) diff --git a/synceditdata.h b/synceditdata.h index 0b48ed5..c89b3c0 100644 --- a/synceditdata.h +++ b/synceditdata.h @@ -17,38 +17,87 @@ public: virtual void undo(SyncEditData *data) = 0; }; + class InsertCommand : public Command + { + public: + InsertCommand(size_t track, size_t row, const SyncTrack::KeyFrame &key) : track(track), row(row), key(key) {} + ~InsertCommand() {} + + virtual void exec(SyncEditData *data) + { + SyncTrack &track = data->getTrack(this->track); + assert(!track.isKeyFrame(row)); + track.setKeyFrame(row, key); + } + + virtual void undo(SyncEditData *data) + { + SyncTrack &track = data->getTrack(this->track); + assert(track.isKeyFrame(row)); + track.deleteKeyFrame(row); + } + + private: + size_t track, row; + SyncTrack::KeyFrame key; + }; + + class DeleteCommand : public Command + { + public: + DeleteCommand(size_t track, size_t row) : track(track), row(row) {} + ~DeleteCommand() {} + + virtual void exec(SyncEditData *data) + { + SyncTrack &track = data->getTrack(this->track); + assert(track.isKeyFrame(row)); + oldKey = *track.getKeyFrame(row); + track.deleteKeyFrame(row); + } + + virtual void undo(SyncEditData *data) + { + SyncTrack &track = data->getTrack(this->track); + assert(!track.isKeyFrame(row)); + track.setKeyFrame(row, oldKey); + } + + private: + size_t track, row; + SyncTrack::KeyFrame oldKey; + }; + + class EditCommand : public Command { public: - EditCommand(size_t track, size_t row, bool existing, float value) : track(track), row(row), newValExisting(existing), newVal(value) {} + EditCommand(size_t track, size_t row, const SyncTrack::KeyFrame &key) : track(track), row(row), key(key) {} ~EditCommand() {} virtual void exec(SyncEditData *data) { SyncTrack &track = data->getTrack(this->track); - // store old state - oldValExisting = track.isKeyFrame(row); - if (oldValExisting) oldVal = track.getKeyFrame(row)->value; + // store old key + assert(track.isKeyFrame(row)); + oldKey = *track.getKeyFrame(row); // update - if (!newValExisting) track.deleteKeyFrame(row); - else track.setKeyFrame(row, newVal); + track.setKeyFrame(row, key); } virtual void undo(SyncEditData *data) { SyncTrack &track = data->getTrack(this->track); - // un-update - if (!oldValExisting) track.deleteKeyFrame(row); - else track.setKeyFrame(row, oldVal); + assert(track.isKeyFrame(row)); + track.setKeyFrame(row, oldKey); } private: size_t track, row; - float newVal, oldVal; - bool newValExisting, oldValExisting; + SyncTrack::KeyFrame oldKey, key; }; class MultiCommand : public Command @@ -126,6 +175,28 @@ public: } } + void setKey(int track, int row, float val) + { + SyncTrack &t = getTrack(track); + SyncEditData::Command *cmd; + if (t.isKeyFrame(row)) + { + cmd = new EditCommand(track, row, val); + } + else + { + cmd = new InsertCommand(track, row, val); + } + exec(cmd); + } + + void deleteKey(int track, int row) + { + assert(getTrack(track).isKeyFrame(row)); + Command *cmd = new DeleteCommand(track, row); + exec(cmd); + } + private: std::stack undoStack; diff --git a/synctracker2.cpp b/synctracker2.cpp index 5d35bed..834b324 100644 --- a/synctracker2.cpp +++ b/synctracker2.cpp @@ -27,8 +27,8 @@ static LRESULT CALLBACK setRowsDialogProc(HWND hDlg, UINT message, WPARAM wParam assert(NULL != rows); /* create row-string */ - char temp[256]; - _snprintf(temp, 256, "%d", *rows); + TCHAR temp[256]; + _sntprintf_s(temp, 256, _T("%d"), *rows); /* set initial row count */ SetWindowText(GetDlgItem(hDlg, IDC_SETROWS_EDIT), temp); @@ -39,9 +39,9 @@ static LRESULT CALLBACK setRowsDialogProc(HWND hDlg, UINT message, WPARAM wParam if (LOWORD(wParam) == IDOK) { /* get value */ - char temp[256]; + TCHAR temp[256]; GetWindowText(GetDlgItem(hDlg, IDC_SETROWS_EDIT), temp, 256); - int result = atoi(temp); + int result = _tstoi(temp); /* update editor */ SendMessage(GetParent(hDlg), WM_SETROWS, 0, result); @@ -131,7 +131,7 @@ static LRESULT CALLBACK mainWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARA HINSTANCE hInstance = GetModuleHandle(NULL); int rows = trackView->getRows(); INT_PTR result = DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_SETROWS), hwnd, (DLGPROC)setRowsDialogProc, (LPARAM)&rows); - if (FAILED(result)) MessageBox(NULL, "unable to create dialog box", NULL, MB_OK); + if (FAILED(result)) MessageBox(NULL, _T("unable to create dialog box"), NULL, MB_OK); if (IDOK == result) { printf("result: %d\n", result); @@ -223,8 +223,8 @@ int _tmain(int argc, _TCHAR* argv[]) for (int i = 0; i < 2; ++i) { - char temp[256]; - _snprintf(temp, 256, "gen %02d", i); + TCHAR temp[256]; + _sntprintf_s(temp, 256, _T("gen %02d"), i); SyncTrack &temp2 = syncData.getTrack(temp); } diff --git a/trackview.cpp b/trackview.cpp index f7bf5d9..cbcac82 100644 --- a/trackview.cpp +++ b/trackview.cpp @@ -38,13 +38,14 @@ TrackView::TrackView() // selectBaseBrush = CreateSolidBrush(RGB(0xff, 0xdd, 0xff)); // selectDarkBrush = CreateSolidBrush(RGB(0xdd, 0xbb, 0xdd)); + selectBaseBrush = GetSysColorBrush(COLOR_HIGHLIGHT); selectDarkBrush = CreateSolidBrush(darken(GetSysColor(COLOR_HIGHLIGHT), 0.9f)); - editBrush = CreateSolidBrush(RGB(255, 255, 0)); - - clipboardFormat = RegisterClipboardFormat("syncdata"); - if (0 == clipboardFormat) printf("geh"); + editBrush = CreateSolidBrush(RGB(255, 255, 0)); // yellow + + clipboardFormat = RegisterClipboardFormat(_T("syncdata")); + assert(0 != clipboardFormat); } TrackView::~TrackView() @@ -97,13 +98,14 @@ void TrackView::paintTopMargin(HDC hdc, RECT rcTracks) fillRect = topLeftMargin; DrawEdge(hdc, &fillRect, BDR_RAISEDINNER | BDR_RAISEDOUTER, BF_ADJUST | BF_BOTTOM); FillRect(hdc, &fillRect, GetSysColorBrush(COLOR_3DFACE)); - ExcludeClipRect(hdc, topLeftMargin.left, topLeftMargin.top, topLeftMargin.right, topLeftMargin.bottom); int firstTrack = min(max(scrollPosX / trackWidth, 0), getTrackCount() - 1); int lastTrack = min(max(firstTrack + windowTracks + 1, 0), getTrackCount() - 1); SyncData *syncData = getSyncData(); if (NULL == syncData) return; + + SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT)); SyncData::TrackContainer::iterator trackIter = syncData->tracks.begin(); for (int track = 0; track <= lastTrack; ++track, ++trackIter) @@ -134,7 +136,6 @@ void TrackView::paintTopMargin(HDC hdc, RECT rcTracks) fillRect.left, 0, trackName.data(), int(trackName.length()) ); - ExcludeClipRect(hdc, topMargin.left, topMargin.top, topMargin.right, topMargin.bottom); } RECT topRightMargin; @@ -145,7 +146,9 @@ void TrackView::paintTopMargin(HDC hdc, RECT rcTracks) fillRect = topRightMargin; DrawEdge(hdc, &fillRect, BDR_RAISEDINNER | BDR_RAISEDOUTER, BF_ADJUST | BF_BOTTOM); FillRect(hdc, &fillRect, GetSysColorBrush(COLOR_3DFACE)); - ExcludeClipRect(hdc, topRightMargin.left, topRightMargin.top, topRightMargin.right, topRightMargin.bottom); + + // make sure that the top margin isn't overdrawn by the track-data + ExcludeClipRect(hdc, 0, 0, rcTracks.right, topMarginHeight); } void TrackView::paintTracks(HDC hdc, RECT rcTracks) @@ -162,9 +165,6 @@ void TrackView::paintTracks(HDC hdc, RECT rcTracks) lastRow = min(max(lastRow, 0), rows - 1); SetBkMode(hdc, TRANSPARENT); - -// SelectObject(hdc, editBrush); - paintTopMargin(hdc, rcTracks); for (int row = firstRow; row <= lastRow; ++row) @@ -183,19 +183,18 @@ void TrackView::paintTracks(HDC hdc, RECT rcTracks) FillRect(hdc, &leftMargin, fillBrush); DrawEdge(hdc, &leftMargin, BDR_RAISEDINNER | BDR_RAISEDOUTER, BF_RIGHT | BF_BOTTOM | BF_TOP); -// Rectangle( hdc, leftMargin.left, leftMargin.top, leftMargin.right, leftMargin.bottom + 1); - if ((row % 16) == 0) SetTextColor(hdc, RGB(0, 0, 0)); - else if ((row % 8) == 0) SetTextColor(hdc, RGB(32, 32, 32)); - else if ((row % 4) == 0) SetTextColor(hdc, RGB(64, 64, 64)); - else SetTextColor(hdc, RGB(128, 128, 128)); + if ((row % 8) == 0) SetTextColor(hdc, RGB(0, 0, 0)); + else if ((row % 4) == 0) SetTextColor(hdc, RGB(64, 64, 64)); + else SetTextColor(hdc, RGB(128, 128, 128)); + +/* if ((row % 4) == 0) SetTextColor(hdc, GetSysColor(COLOR_BTNTEXT)); + else SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT)); */ _sntprintf_s(temp, 256, _T("%0*Xh"), 5, row); TextOut(hdc, leftMargin.left, leftMargin.top, temp, int(_tcslen(temp)) ); - - ExcludeClipRect(hdc, leftMargin.left, leftMargin.top, leftMargin.right, leftMargin.bottom); } SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT)); @@ -330,7 +329,7 @@ void TrackView::copy() if (FAILED(OpenClipboard(getWin()))) { - MessageBox(NULL, "Failed to open clipboard", NULL, MB_OK); + MessageBox(NULL, _T("Failed to open clipboard"), NULL, MB_OK); return; } @@ -607,6 +606,29 @@ LRESULT TrackView::onHScroll(UINT sbCode, int newPos) return FALSE; } +void TrackView::onReturn() +{ + if (editString.size() > 0) + { + syncData->setKey(editTrack, editRow, float(_tstof(editString.c_str()))); + + editString.clear(); + invalidatePos(editTrack, editRow); + } + else MessageBeep(0); +} + +void TrackView::onDelete() +{ + SyncTrack &track = syncData->getTrack(editTrack); + if (track.isKeyFrame(editRow)) + { + syncData->deleteKey(editTrack, editRow); + invalidatePos(editTrack, editRow); + } + else MessageBeep(0); +} + LRESULT TrackView::onKeyDown(UINT keyCode, UINT flags) { bool refreshCaret = false; @@ -645,31 +667,8 @@ LRESULT TrackView::onKeyDown(UINT keyCode, UINT flags) switch (keyCode) { - case VK_RETURN: - if (editString.size() > 0) - { - SyncEditData::EditCommand *cmd = new SyncEditData::EditCommand( - editTrack, editRow, - true, float(_tstof(editString.c_str())) - ); - syncData->exec(cmd); - - editString.clear(); - invalidatePos(editTrack, editRow); - } - else MessageBeep(0); - break; - - case VK_DELETE: - { - SyncEditData::EditCommand *cmd = new SyncEditData::EditCommand( - editTrack, editRow, - false, 0.0f - ); - syncData->exec(cmd); - invalidatePos(editTrack, editRow); - } - break; + case VK_RETURN: onReturn(); break; + case VK_DELETE: onDelete(); break; case VK_BACK: if (editString.size() > 0) diff --git a/trackview.h b/trackview.h index 20fa54c..2fcbf10 100644 --- a/trackview.h +++ b/trackview.h @@ -38,6 +38,9 @@ private: LRESULT onKeyDown(UINT keyCode, UINT flags); LRESULT onChar(UINT keyCode, UINT flags); + void onReturn(); + void onDelete(); + void copy(); void cut(); void paste();