feat: add ability to control zoom/angle on generated card image

This commit is contained in:
Brendan Hagan
2022-08-12 22:49:27 -04:00
parent a1fda8c6cd
commit 05855b812a
3 changed files with 63 additions and 17 deletions
+3 -3
View File
@@ -99,11 +99,11 @@ void export_images(const SetP& set, const vector<CardP>& cards,
void export_image(const SetP& set, const CardP& card, const String& filename); void export_image(const SetP& set, const CardP& card, const String& filename);
/// Generate a bitmap image of a card /// Generate a bitmap image of a card
Bitmap export_bitmap(const SetP& set, const CardP& card); Bitmap export_bitmap(const SetP& set, const CardP& card);
Bitmap export_bitmap(const SetP& set, const CardP& card, const double zoom, const Radians angle_radians);
/// Export a set to Magic Workstation format /// Export a set to Magic Workstation format
void export_mws(Window* parent, const SetP& set); void export_mws(Window* parent, const SetP& set);
/// Export a set to Apprentice /// Export a set to Apprentice
void export_apprentice(Window* parent, const SetP& set); void export_apprentice(Window* parent, const SetP& set);
+50 -13
View File
@@ -25,15 +25,35 @@ void export_image(const SetP& set, const CardP& card, const String& filename) {
} }
class UnzoomedDataViewer : public DataViewer { class UnzoomedDataViewer : public DataViewer {
public: public:
UnzoomedDataViewer();
UnzoomedDataViewer(double zoom, double angle);
virtual ~UnzoomedDataViewer() {}; virtual ~UnzoomedDataViewer() {};
Rotation getRotation() const override; Rotation getRotation() const override;
private: private:
double angle = 0.0; double zoom;
}; double angle;
bool declared_values;
};
UnzoomedDataViewer::UnzoomedDataViewer()
: zoom(1.0)
, angle(0.0)
, declared_values(false)
{}
UnzoomedDataViewer::UnzoomedDataViewer(const double zoom, const Radians angle = 0.0)
: zoom(zoom)
, angle(angle)
, declared_values(true)
{}
Rotation UnzoomedDataViewer::getRotation() const { Rotation UnzoomedDataViewer::getRotation() const {
if (!stylesheet) stylesheet = set->stylesheet; if (!stylesheet) stylesheet = set->stylesheet;
if (declared_values) {
return Rotation(angle, stylesheet->getCardRect(), zoom, 1.0, ROTATION_ATTACH_TOP_LEFT);
}
double export_zoom = settings.stylesheetSettingsFor(set->stylesheetFor(card)).export_zoom(); double export_zoom = settings.stylesheetSettingsFor(set->stylesheetFor(card)).export_zoom();
bool use_viewer_rotation = !settings.stylesheetSettingsFor(set->stylesheetFor(card)).card_normal_export(); bool use_viewer_rotation = !settings.stylesheetSettingsFor(set->stylesheetFor(card)).card_normal_export();
@@ -42,26 +62,43 @@ Rotation UnzoomedDataViewer::getRotation() const {
} else { } else {
return Rotation(angle, stylesheet->getCardRect(), export_zoom, 1.0, ROTATION_ATTACH_TOP_LEFT); return Rotation(angle, stylesheet->getCardRect(), export_zoom, 1.0, ROTATION_ATTACH_TOP_LEFT);
} }
} }
Bitmap export_bitmap(const SetP& set, const CardP& card) { Bitmap export_bitmap(const SetP& set, const CardP& card) {
if (!set) throw Error(_("no set")); if (!set) throw Error(_("no set"));
// create viewer
UnzoomedDataViewer viewer = UnzoomedDataViewer(); UnzoomedDataViewer viewer = UnzoomedDataViewer();
viewer.setSet(set); viewer.setSet(set);
viewer.setCard(card); viewer.setCard(card);
// size of cards // size of cards
RealSize size = viewer.getRotation().getExternalSize(); RealSize size = viewer.getRotation().getExternalSize();
// create bitmap & dc // create bitmap & dc
Bitmap bitmap((int) size.width, (int) size.height); Bitmap bitmap((int)size.width, (int)size.height);
if (!bitmap.Ok()) throw InternalError(_("Unable to create bitmap")); if (!bitmap.Ok()) throw InternalError(_("Unable to create bitmap"));
wxMemoryDC dc; wxMemoryDC dc;
dc.SelectObject(bitmap); dc.SelectObject(bitmap);
// draw // draw
viewer.draw(dc); viewer.draw(dc);
dc.SelectObject(wxNullBitmap); dc.SelectObject(wxNullBitmap);
return bitmap; return bitmap;
} }
Bitmap export_bitmap(const SetP& set, const CardP& card, const double zoom, const Radians angle = 0.0) {
if (!set) throw Error(_("no set"));
UnzoomedDataViewer viewer = UnzoomedDataViewer(zoom, angle);
viewer.setSet(set);
viewer.setCard(card);
// size of cards
RealSize size = viewer.getRotation().getExternalSize();
// create bitmap & dc
Bitmap bitmap((int)size.width, (int)size.height);
if (!bitmap.Ok()) throw InternalError(_("Unable to create bitmap"));
wxMemoryDC dc;
dc.SelectObject(bitmap);
// draw
viewer.draw(dc);
dc.SelectObject(wxNullBitmap);
return bitmap;
}
// ----------------------------------------------------------------------------- : Multiple card export // ----------------------------------------------------------------------------- : Multiple card export
+10 -1
View File
@@ -32,7 +32,16 @@ SCRIPT_FUNCTION(to_image) {
SCRIPT_FUNCTION(to_card_image) { SCRIPT_FUNCTION(to_card_image) {
SCRIPT_PARAM(Set*, set); SCRIPT_PARAM(Set*, set);
SCRIPT_PARAM(CardP, input); SCRIPT_PARAM(CardP, input);
return make_intrusive<ArbitraryImage>(export_bitmap(set, input).ConvertToImage()); SCRIPT_PARAM_DEFAULT(double, zoom, 100);
SCRIPT_PARAM_DEFAULT(Degrees, angle, 0);
SCRIPT_PARAM_DEFAULT(bool, use_user_settings, false);
if (use_user_settings) {
// Use the User's Preferences for Export Zoom and Angle settings.
return make_intrusive<ArbitraryImage>(export_bitmap(set, input).ConvertToImage());
} else {
// Use the provided (or defaulted) Zoom and Angle.
return make_intrusive<ArbitraryImage>(export_bitmap(set, input, (zoom / 100), deg_to_rad(angle)).ConvertToImage());
}
} }
// ----------------------------------------------------------------------------- : Image functions // ----------------------------------------------------------------------------- : Image functions