Skip to content

Commit 3db6667

Browse files
committed
Just a bit further cleanup by making background images.
1 parent 7afd16f commit 3db6667

File tree

11 files changed

+208
-140
lines changed

11 files changed

+208
-140
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ add_executable (
196196
${CMAKE_CURRENT_SOURCE_DIR}/src/AnimationPanel.h
197197
${CMAKE_CURRENT_SOURCE_DIR}/src/AnimationView.cpp
198198
${CMAKE_CURRENT_SOURCE_DIR}/src/AnimationView.h
199-
${CMAKE_CURRENT_SOURCE_DIR}/src/BackgroundImage.cpp
200-
${CMAKE_CURRENT_SOURCE_DIR}/src/BackgroundImage.h
199+
${CMAKE_CURRENT_SOURCE_DIR}/src/BackgroundImages.cpp
200+
${CMAKE_CURRENT_SOURCE_DIR}/src/BackgroundImages.h
201201
${CMAKE_CURRENT_SOURCE_DIR}/src/CCOmniviewCanvas.cpp
202202
${CMAKE_CURRENT_SOURCE_DIR}/src/CCOmniviewCanvas.h
203203
${CMAKE_CURRENT_SOURCE_DIR}/src/CalChartApp.cpp

src/BackgroundImage.h

Lines changed: 0 additions & 73 deletions
This file was deleted.
Lines changed: 119 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* BackgroundImage.cpp
2+
* BackgroundImages.cpp
33
* Maintains the background image data
44
*/
55

@@ -20,9 +20,57 @@
2020
along with this program. If not, see <http://www.gnu.org/licenses/>.
2121
*/
2222

23-
#include "BackgroundImage.h"
23+
#include "BackgroundImages.h"
24+
#include "cc_image.h"
2425
#include <algorithm>
2526

27+
class BackgroundImage {
28+
public:
29+
BackgroundImage(wxImage const& image, int x, int y, int scaled_width, int scaled_height);
30+
31+
bool MouseClickIsHit(wxMouseEvent const& event, wxDC const& dc) const;
32+
void OnMouseLeftDown(wxMouseEvent const& event, wxDC const& dc);
33+
// returns left, top, width, height
34+
std::array<int, 4> OnMouseLeftUp(wxMouseEvent const& event, wxDC const& dc);
35+
36+
void OnMouseMove(wxMouseEvent const& event, wxDC const& dc);
37+
void OnPaint(wxDC& dc, bool drawPicAdjustDots, bool selected) const;
38+
39+
private:
40+
static constexpr auto kCircleSize = 6;
41+
42+
wxImage mImage;
43+
wxBitmap mBitmap;
44+
wxPoint mBitmapPoint;
45+
46+
// what type of background adjustments could we do
47+
enum class BackgroundAdjustType {
48+
kUpperLeft = 0,
49+
kUpper,
50+
kUpperRight,
51+
kLeft,
52+
kMove,
53+
kRight,
54+
kLowerLeft,
55+
kLower,
56+
kLowerRight,
57+
kLast,
58+
};
59+
BackgroundAdjustType mBackgroundAdjustType;
60+
61+
struct CalculateScaleAndMove {
62+
public:
63+
CalculateScaleAndMove(wxPoint startClick, wxRect rect, BackgroundAdjustType adjustType);
64+
65+
wxRect operator()(wxCoord x, wxCoord y, wxRect wxRect);
66+
wxPoint mStartClick;
67+
wxRect mRect;
68+
float mAspectRatio;
69+
BackgroundAdjustType mAdjustType;
70+
};
71+
std::unique_ptr<CalculateScaleAndMove> mScaleAndMove;
72+
};
73+
2674
BackgroundImage::BackgroundImage(const wxImage& image, int x, int y, int scaled_width, int scaled_height)
2775
: mImage(image)
2876
, mBitmapPoint(x, y)
@@ -253,3 +301,72 @@ wxRect BackgroundImage::CalculateScaleAndMove::operator()(wxCoord x, wxCoord y,
253301
return rect;
254302
}
255303
}
304+
305+
BackgroundImages::BackgroundImages() = default;
306+
BackgroundImages::~BackgroundImages() = default;
307+
308+
void BackgroundImages::SetBackgroundImages(std::vector<CalChart::ImageData> const& images)
309+
{
310+
mBackgroundImages.clear();
311+
mWhichBackgroundIndex = -1;
312+
for (auto&& image : images) {
313+
// ugh... not sure if there's a better way to pass data to image.
314+
auto d = static_cast<unsigned char*>(malloc(sizeof(unsigned char) * image.image_width * image.image_height * 3));
315+
std::copy(image.data.begin(), image.data.end(), d);
316+
auto a = static_cast<unsigned char*>(nullptr);
317+
if (image.alpha.size()) {
318+
a = static_cast<unsigned char*>(malloc(sizeof(unsigned char) * image.image_width * image.image_height));
319+
std::copy(image.alpha.begin(), image.alpha.end(), a);
320+
wxImage img(image.image_width, image.image_height, d, a);
321+
mBackgroundImages.emplace_back(img, image.left, image.top, image.scaled_width, image.scaled_height);
322+
} else {
323+
wxImage img(image.image_width, image.image_height, d);
324+
mBackgroundImages.emplace_back(img, image.left, image.top, image.scaled_width, image.scaled_height);
325+
}
326+
}
327+
}
328+
329+
void BackgroundImages::OnPaint(wxDC& dc) const
330+
{
331+
for (auto i = 0; i < static_cast<int>(mBackgroundImages.size()); ++i) {
332+
mBackgroundImages[i].OnPaint(dc, mAdjustBackgroundMode, mWhichBackgroundIndex == i);
333+
}
334+
}
335+
336+
void BackgroundImages::OnMouseLeftDown(wxMouseEvent const& event, wxDC const& dc)
337+
{
338+
if (!mAdjustBackgroundMode) {
339+
return;
340+
}
341+
mWhichBackgroundIndex = -1;
342+
for (auto i = 0; i < static_cast<int>(mBackgroundImages.size()); ++i) {
343+
if (mBackgroundImages[i].MouseClickIsHit(event, dc)) {
344+
mWhichBackgroundIndex = i;
345+
}
346+
}
347+
if (mWhichBackgroundIndex != -1) {
348+
mBackgroundImages[mWhichBackgroundIndex].OnMouseLeftDown(event, dc);
349+
}
350+
}
351+
352+
std::optional<std::tuple<int, std::array<int, 4>>> BackgroundImages::OnMouseLeftUp(wxMouseEvent const& event, wxDC const& dc)
353+
{
354+
if (!mAdjustBackgroundMode) {
355+
return {};
356+
}
357+
if (mWhichBackgroundIndex >= 0 && mWhichBackgroundIndex < static_cast<int>(mBackgroundImages.size())) {
358+
return { { mWhichBackgroundIndex, mBackgroundImages[mWhichBackgroundIndex].OnMouseLeftUp(event, dc) } };
359+
}
360+
return {};
361+
}
362+
363+
void BackgroundImages::OnMouseMove(wxMouseEvent const& event, wxDC const& dc)
364+
{
365+
if (!mAdjustBackgroundMode) {
366+
return;
367+
}
368+
if (mWhichBackgroundIndex >= 0 && mWhichBackgroundIndex < static_cast<int>(mBackgroundImages.size())) {
369+
mBackgroundImages[mWhichBackgroundIndex].OnMouseMove(event, dc);
370+
}
371+
}
372+

src/BackgroundImages.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#pragma once
2+
/*
3+
* BackgroundImage.h
4+
* Maintains the background image data
5+
*/
6+
/*
7+
Copyright (C) 1995-2011 Garrick Brian Meeker, Richard Michael Powell
8+
9+
This program is free software: you can redistribute it and/or modify
10+
it under the terms of the GNU General Public License as published by
11+
the Free Software Foundation, either version 3 of the License, or
12+
(at your option) any later version.
13+
14+
This program is distributed in the hope that it will be useful,
15+
but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
GNU General Public License for more details.
18+
19+
You should have received a copy of the GNU General Public License
20+
along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*/
22+
23+
#include <array>
24+
#include <memory>
25+
#include <wx/dc.h>
26+
#include <wx/event.h>
27+
28+
namespace CalChart {
29+
class ImageData;
30+
}
31+
32+
class BackgroundImage;
33+
34+
class BackgroundImages {
35+
public:
36+
BackgroundImages();
37+
~BackgroundImages();
38+
39+
void SetBackgroundImages(std::vector<CalChart::ImageData> const& images);
40+
41+
auto GetAdjustBackgroundMode() const { return mAdjustBackgroundMode; }
42+
void SetAdjustBackgroundMode(bool adjustBackgroundMode) { mAdjustBackgroundMode = adjustBackgroundMode; }
43+
44+
std::optional<std::size_t> GetCurrentIndex() const { return mWhichBackgroundIndex == -1 ? std::optional<std::size_t>{} : mWhichBackgroundIndex; }
45+
46+
void OnMouseLeftDown(wxMouseEvent const& event, wxDC const& dc);
47+
std::optional<std::tuple<int, std::array<int, 4>>> OnMouseLeftUp(wxMouseEvent const& event, wxDC const& dc);
48+
void OnMouseMove(wxMouseEvent const& event, wxDC const& dc);
49+
50+
void OnPaint(wxDC& dc) const;
51+
52+
private:
53+
std::vector<BackgroundImage> mBackgroundImages;
54+
bool mAdjustBackgroundMode{};
55+
int mWhichBackgroundIndex = -1;
56+
57+
};

src/CalChartDoc.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,12 @@ void CalChartDoc::SetDrawBackground(bool drawBackground)
367367
UpdateAllViews();
368368
}
369369

370+
void CalChartDoc::SetCurrentMove(CalChart::MoveMode move)
371+
{
372+
mCurrentMove = move;
373+
UpdateAllViews();
374+
}
375+
370376
const ShowMode& CalChartDoc::GetShowMode() const { return mShow->GetShowMode(); }
371377

372378
int CalChartDoc::PrintToPS(std::ostream& buffer, bool overview,

src/CalChartDoc.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ class CalChartDoc : public wxDocument {
175175
void SetDrawPaths(bool drawPaths);
176176
auto GetDrawBackground() const { return mDrawBackground; }
177177
void SetDrawBackground(bool drawBackground);
178+
auto GetCurrentMove() const { return mCurrentMove; }
179+
void SetCurrentMove(CalChart::MoveMode move);
178180

179181
CalChart::ShowMode const& GetShowMode() const;
180182
// nullptr if there is no animation
@@ -254,6 +256,7 @@ class CalChartDoc : public wxDocument {
254256
std::unique_ptr<CalChart::Show> mShow;
255257
std::unique_ptr<CalChart::Animation> mAnimation;
256258
CalChart::Select mSelect = CalChart::Select::Box;
259+
CalChart::MoveMode mCurrentMove = CalChart::MoveMode::Normal;
257260
int mCurrentReferencePoint{};
258261
bool mDrawPaths{};
259262
bool mDrawBackground{};

0 commit comments

Comments
 (0)