Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #456: move animation to use draw command #457

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
257 changes: 158 additions & 99 deletions src/AnimationView.cpp

Large diffs are not rendered by default.

51 changes: 31 additions & 20 deletions src/AnimationView.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
*/

#include "CalChartAngles.h"
#include "DCSaveRestore.h"
#include "CalChartDrawCommand.h"
#include "CalChartMeasure.h"
#include <array>
#include <map>
#include <memory>
Expand All @@ -45,58 +46,63 @@ class AnimationView : public wxView {
public:
AnimationView(CalChartView* view, CalChartConfiguration const& config, wxWindow* frame);
~AnimationView() override;
AnimationView(AnimationView const&) = delete;
auto operator=(AnimationView const&) = delete;
AnimationView(AnimationView&&) = delete;
auto operator=(AnimationView&&) = delete;

void OnDraw(wxDC* dc) override;
void OnDraw(wxDC& dc, CalChartConfiguration const& config);
void OnDrawDots(wxDC& dc, CalChartConfiguration const& config);
void OnDrawSprites(wxDC& dc, CalChartConfiguration const& config);
void OnUpdate(wxView* sender, wxObject* hint = (wxObject*)nullptr) override;
void OnUpdate(wxView* sender, wxObject* hint = nullptr) override;

void PrevBeat();
void NextBeat();
void GotoTotalBeat(unsigned i);
bool AtEndOfShow() const;
[[nodiscard]] auto AtEndOfShow() const -> bool;

void RefreshAnimationSheet();

// info
int GetTotalNumberBeats() const;
int GetTotalCurrentBeat() const;
[[nodiscard]] auto GetTotalNumberBeats() const -> int;
[[nodiscard]] auto GetTotalCurrentBeat() const -> int;

std::pair<wxPoint, wxPoint> GetShowSizeAndOffset() const;
std::pair<wxPoint, wxPoint> GetMarcherSizeAndOffset() const;
[[nodiscard]] auto GetShowSizeAndOffset() const -> std::pair<wxPoint, wxPoint>;
[[nodiscard]] auto GetMarcherSizeAndOffset() const -> std::pair<wxPoint, wxPoint>;

CalChart::ShowMode const& GetShowMode() const;
[[nodiscard]] auto GetShowMode() const -> CalChart::ShowMode const&;

struct MarcherInfo {
CalChart::Radian direction{};
float x{};
float y{};
};

MarcherInfo GetMarcherInfo(int which) const;
[[nodiscard]] auto GetMarcherInfo(int which) const -> MarcherInfo;

std::multimap<double, MarcherInfo> GetMarchersByDistance(float fromX, float fromY) const;
[[nodiscard]] auto GetMarchersByDistance(float fromX, float fromY) const -> std::multimap<double, MarcherInfo>;

public:
void UnselectAll();
void SelectMarchersInBox(wxPoint const& mouseStart, wxPoint const& mouseEnd, bool altDown);

void ToggleTimer();
bool OnBeat() const;
[[nodiscard]] auto OnBeat() const -> bool;

void SetDrawCollisionWarning(bool b) { mDrawCollisionWarning = b; }
auto GetDrawCollisionWarning() const { return mDrawCollisionWarning; }
[[nodiscard]] auto GetDrawCollisionWarning() const { return mDrawCollisionWarning; }

void SetPlayCollisionWarning(bool b) { mPlayCollisionWarning = b; }
auto GetPlayCollisionWarning() const { return mPlayCollisionWarning; }
[[nodiscard]] auto GetPlayCollisionWarning() const { return mPlayCollisionWarning; }

private:
void Generate();
void RefreshFrame();

AnimationPanel const* GetAnimationFrame() const;
AnimationPanel* GetAnimationFrame();
void RegenerateImages() const;
[[nodiscard]] auto GenerateDraw(CalChartConfiguration const& config) const -> std::vector<CalChart::Draw::DrawCommand>;
[[nodiscard]] auto GenerateDrawDots(CalChartConfiguration const& config) const -> std::vector<CalChart::Draw::DrawCommand>;
[[nodiscard]] auto GenerateDrawSprites(CalChartConfiguration const& config) const -> std::vector<CalChart::Draw::DrawCommand>;

[[nodiscard]] auto GetAnimationFrame() const -> AnimationPanel const*;
[[nodiscard]] auto GetAnimationFrame() -> AnimationPanel*;

// Yes, this view has a view...
CalChartView* mView{};
Expand All @@ -113,5 +119,10 @@ class AnimationView : public wxView {
Size
};

std::array<wxImage, 8 * ImageBeat::Size> mSpriteImages;
static constexpr auto kAngles = 8;
// these need to be mutable because effectively they are a cache and may need to be regenerated.
mutable double mScaleSize = 0;
mutable std::array<std::shared_ptr<CalChart::ImageData>, kAngles * ImageBeat::Size> mSpriteCalChartImages;

CalChart::MeasureDuration mMeasure;
};
19 changes: 4 additions & 15 deletions src/BackgroundImages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

/*
Copyright (C) 1995-2011 Garrick Brian Meeker, Richard Michael Powell
Copyright (C) 1995-2024 Garrick Brian Meeker, Richard Michael Powell

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -21,6 +21,7 @@
*/

#include "BackgroundImages.h"
#include "CalChartDrawPrimativesHelper.h"
#include "CalChartImage.h"
#include "CalChartTypes.h"
#include <algorithm>
Expand Down Expand Up @@ -314,24 +315,12 @@ wxRect BackgroundImage::CalculateScaleAndMove::operator()(wxCoord x, wxCoord y,
BackgroundImages::BackgroundImages() = default;
BackgroundImages::~BackgroundImages() = default;

void BackgroundImages::SetBackgroundImages(std::vector<CalChart::ImageData> const& images)
void BackgroundImages::SetBackgroundImages(std::vector<CalChart::ImageInfo> const& images)
{
mBackgroundImages.clear();
mWhichBackgroundIndex = -1;
for (auto&& image : images) {
// ugh... not sure if there's a better way to pass data to image.
auto d = static_cast<unsigned char*>(malloc(sizeof(unsigned char) * image.image_width * image.image_height * 3));
std::copy(image.data.begin(), image.data.end(), d);
auto a = static_cast<unsigned char*>(nullptr);
if (image.alpha.size()) {
a = static_cast<unsigned char*>(malloc(sizeof(unsigned char) * image.image_width * image.image_height));
std::copy(image.alpha.begin(), image.alpha.end(), a);
wxImage img(image.image_width, image.image_height, d, a);
mBackgroundImages.emplace_back(img, image.left, image.top, image.scaled_width, image.scaled_height);
} else {
wxImage img(image.image_width, image.image_height, d);
mBackgroundImages.emplace_back(img, image.left, image.top, image.scaled_width, image.scaled_height);
}
mBackgroundImages.emplace_back(wxCalChart::ConvertTowxImage(image.data), image.left, image.top, image.scaled_width, image.scaled_height);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/BackgroundImages.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Maintains the background image data
*/
/*
Copyright (C) 1995-2011 Garrick Brian Meeker, Richard Michael Powell
Copyright (C) 1995-2024 Garrick Brian Meeker, Richard Michael Powell

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -27,7 +27,7 @@
#include <wx/event.h>

namespace CalChart {
struct ImageData;
struct ImageInfo;
}

class BackgroundImage;
Expand All @@ -37,7 +37,7 @@ class BackgroundImages {
BackgroundImages();
~BackgroundImages();

void SetBackgroundImages(std::vector<CalChart::ImageData> const& images);
void SetBackgroundImages(std::vector<CalChart::ImageInfo> const& images);

auto GetAdjustBackgroundMode() const { return mAdjustBackgroundMode; }
void SetAdjustBackgroundMode(bool adjustBackgroundMode) { mAdjustBackgroundMode = adjustBackgroundMode; }
Expand Down
4 changes: 2 additions & 2 deletions src/CalChartDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,10 +615,10 @@ std::unique_ptr<wxCommand> CalChartDoc::Create_ToggleLabelVisibilityCommand()
return std::make_unique<CalChartDocCommand>(*this, wxT("Setting Label Visibility"), cmds);
}

std::unique_ptr<wxCommand> CalChartDoc::Create_AddNewBackgroundImageCommand(int left, int top, int image_width, int image_height, std::vector<unsigned char> const& data, std::vector<unsigned char> const& alpha)
std::unique_ptr<wxCommand> CalChartDoc::Create_AddNewBackgroundImageCommand(ImageInfo const& image)
{
auto cmds = Create_SetSheetPair();
cmds.emplace_back(Inject_CalChartDocArg(mShow->Create_AddNewBackgroundImageCommand(ImageData{ left, top, image_width, image_height, image_width, image_height, data, alpha })));
cmds.emplace_back(Inject_CalChartDocArg(mShow->Create_AddNewBackgroundImageCommand(image)));
return std::make_unique<CalChartDocCommand>(*this, wxT("Adding Background Image"), cmds);
}

Expand Down
4 changes: 2 additions & 2 deletions src/CalChartDoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

/*
Copyright (C) 1995-2011 Garrick Brian Meeker, Richard Michael Powell
Copyright (C) 1995-2024 Garrick Brian Meeker, Richard Michael Powell

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -224,7 +224,7 @@ class CalChartDoc : public wxDocument {
std::unique_ptr<wxCommand> Create_ToggleLabelFlipCommand();
std::unique_ptr<wxCommand> Create_SetLabelVisibleCommand(bool isVisible);
std::unique_ptr<wxCommand> Create_ToggleLabelVisibilityCommand();
std::unique_ptr<wxCommand> Create_AddNewBackgroundImageCommand(int left, int top, int image_width, int image_height, std::vector<unsigned char> const& data, std::vector<unsigned char> const& alpha);
std::unique_ptr<wxCommand> Create_AddNewBackgroundImageCommand(CalChart::ImageInfo const& image);
std::unique_ptr<wxCommand> Create_RemoveBackgroundImageCommand(int which);
std::unique_ptr<wxCommand> Create_MoveBackgroundImageCommand(int which, int left, int top, int scaled_width, int scaled_height);
std::unique_ptr<wxCommand> Create_SetTransitionCommand(const std::vector<CalChart::Coord>& finalPositions, const std::map<CalChart::SYMBOL_TYPE, std::string>& continuities, const std::vector<CalChart::SYMBOL_TYPE>& marcherDotTypes);
Expand Down
35 changes: 35 additions & 0 deletions src/CalChartDrawPrimativesHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/

#include "CalChartDrawPrimatives.h"
#include "CalChartImage.h"
#include "CalChartSizes.h"
#include <wx/brush.h>
#include <wx/colour.h>
Expand Down Expand Up @@ -206,4 +207,38 @@ inline auto setFont(wxDC& dc, CalChart::Font font)
dc.SetFont(wxFont(size, family, style, weight));
}

inline auto ConvertToImageData(wxImage const& image) -> CalChart::ImageData
{
auto width = image.GetWidth();
auto height = image.GetHeight();
auto data = std::vector<unsigned char>(width * height * 3);
auto* d = image.GetData();
std::copy(d, d + width * height * 3, data.data());
auto alpha = std::vector<unsigned char>{};
auto* a = image.GetAlpha();
if (a) {
alpha.resize(width * height);
std::copy(a, a + width * height, alpha.data());
}

return { width, height, data, alpha };
}

inline auto ConvertToImageInfo(wxImage const& image, int x = 0, int y = 0) -> CalChart::ImageInfo
{
auto width = image.GetWidth();
auto height = image.GetHeight();
return CalChart::ImageInfo{ x, y, width, height, ConvertToImageData(image) };
}

inline auto ConvertTowxImage(CalChart::ImageData const& image) -> wxImage
{
auto data = image.data;
if (image.alpha.size()) {
auto alpha = image.alpha;
return { image.image_width, image.image_height, data.data(), alpha.data(), true };
}
return { image.image_width, image.image_height, data.data(), true };
}

}
8 changes: 8 additions & 0 deletions src/CalChartDrawing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,14 @@ namespace details {
auto where = wxCalChart::to_wxPoint(c.c1) + layoutPoint;
DrawText(dc, c.text, fDIP(where), c.anchor, c.withBackground);
},
[&dc, layoutPoint = surface.origin](CalChart::Draw::Image const& c) {
auto where = wxCalChart::to_wxPoint(c.mStart) + layoutPoint;
auto image = wxCalChart::ConvertTowxImage(*c.mImage);
if (c.mGreyscale) {
image = image.ConvertToGreyscale();
}
dc.DrawBitmap(image, fDIP(where));
},
[]([[maybe_unused]] CalChart::Draw::Ignore const& c) {
},
[]([[maybe_unused]] CalChart::Draw::Tab const& c) {
Expand Down
6 changes: 6 additions & 0 deletions src/CalChartDrawingGetMinSize.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ namespace details {
return wxSize(width, metrics.height + text.linePad);
}

inline auto GetMinSize([[maybe_unused]] Context context, CalChart::Draw::Image const& image) -> StackSize
{
// TODO: should this have scale in it?
return wxSize(image.mImage->image_width, image.mImage->image_width);
}

inline auto GetMinSize(Context context, CalChart::Draw::DrawManipulators const& cmd) -> StackSize;

inline auto GetMinSize(Context context, CalChart::Draw::DrawStack const& cmd) -> StackSize;
Expand Down
18 changes: 2 additions & 16 deletions src/CalChartView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "CalChartDoc.h"
#include "CalChartDocCommand.h"
#include "CalChartDrawCommand.h"
#include "CalChartDrawPrimativesHelper.h"
#include "CalChartDrawing.h"
#include "CalChartFrame.h"
#include "CalChartShapes.h"
Expand Down Expand Up @@ -608,22 +609,7 @@ bool CalChartView::AddBackgroundImage(const wxImage& image)
if (!image.IsOk()) {
return false;
}
auto x = 100;
auto y = 100;

auto width = image.GetWidth();
auto height = image.GetHeight();
std::vector<unsigned char> data(width * height * 3);
auto d = image.GetData();
std::copy(d, d + width * height * 3, data.data());
std::vector<unsigned char> alpha;
auto a = image.GetAlpha();
if (a) {
alpha.resize(width * height);
std::copy(a, a + width * height, alpha.data());
}

auto cmd = mShow->Create_AddNewBackgroundImageCommand(x, y, width, height, data, alpha);
auto cmd = mShow->Create_AddNewBackgroundImageCommand(wxCalChart::ConvertToImageInfo(image, 100, 100));
GetDocument()->GetCommandProcessor()->Submit(cmd.release());
return true;
}
Expand Down
27 changes: 27 additions & 0 deletions src/core/CalChartDrawCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
#include "CalChartConstants.h"
#include "CalChartCoord.h"
#include "CalChartDrawPrimatives.h"
#include "CalChartImage.h"
#include <algorithm>
#include <compare>
#include <functional>
#include <memory>
#include <numeric>
#include <optional>
#include <ranges>
Expand Down Expand Up @@ -74,6 +76,7 @@ namespace Draw {
struct Circle;
struct Rectangle;
struct Text;
struct Image;
struct Tab;
using DrawItems = std::variant<
Ignore,
Expand All @@ -83,6 +86,7 @@ namespace Draw {
Circle,
Rectangle,
Text,
Image,
Tab>;

// meta manipulators
Expand Down Expand Up @@ -327,6 +331,29 @@ namespace Draw {
inline auto operator-(Text const& lhs, Coord rhs) { return Text{ lhs.c1 - rhs, lhs.text, lhs.anchor, lhs.withBackground, lhs.linePad }; }
inline auto operator-(Coord lhs, Text const& rhs) { return Text{ lhs - rhs.c1, rhs.text, rhs.anchor, rhs.withBackground, rhs.linePad }; }

struct Image {
Coord mStart{};
std::shared_ptr<ImageData> mImage{};
bool mGreyscale{};

Image(Coord::units startx, Coord::units starty, std::shared_ptr<ImageData> image, bool greyscale = false)
: Image{ { startx, starty }, std::move(image), greyscale }
{
}

Image(Coord start, std::shared_ptr<ImageData> image, bool greyscale = false)
: mStart{ start }
, mImage{ std::move(image) }
, mGreyscale{ greyscale }
{
}
friend auto operator==(Image const&, Image const&) -> bool = default;
};
inline auto operator+(Image lhs, Coord rhs) { return Image{ lhs.mStart + rhs, lhs.mImage, lhs.mGreyscale }; }
inline auto operator+(Coord lhs, Image rhs) { return Image{ lhs + rhs.mStart, rhs.mImage, rhs.mGreyscale }; }
inline auto operator-(Image lhs, Coord rhs) { return Image{ lhs.mStart - rhs, lhs.mImage, lhs.mGreyscale }; }
inline auto operator-(Coord lhs, Image rhs) { return Image{ lhs - rhs.mStart, rhs.mImage, rhs.mGreyscale }; }

struct OverrideFont {
Font font;
std::vector<DrawCommand> commands{};
Expand Down
Loading
Loading