-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnapshot.hpp
46 lines (34 loc) · 1.46 KB
/
Snapshot.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#pragma once
#include "JmlCommandline.hpp"
namespace mc {
inline auto runSnapshotScript(JmlCommandline const& cli) -> juce::Result
{
auto state = sol::state{};
state.open_libraries(sol::lib::base, sol::lib::package, sol::lib::string);
lua::bindings::allJuceModules(state);
auto const scriptFile = juce::File{cli.scriptPath};
scriptFile.getParentDirectory().setAsCurrentWorkingDirectory();
auto script = state.load_file(scriptFile.getFullPathName().toStdString());
if (not script.valid()) {
sol::error const error = script;
return juce::Result::fail(error.what());
}
auto factory = script.get<sol::protected_function>();
auto result = factory();
if (not result.valid()) {
sol::error const error = result;
return juce::Result::fail(error.what());
}
sol::object const obj = result;
auto* component = obj.as<juce::Component*>();
if (component == nullptr) { return juce::Result::fail("Failed to get juce::Component* from lua result"); }
component->resized();
auto snapshot = component->createComponentSnapshot(component->getBounds());
auto snapShotFile = juce::File::getCurrentWorkingDirectory().getChildFile("snapshot.png");
if (snapShotFile.existsAsFile()) { snapShotFile.deleteFile(); }
auto out = snapShotFile.createOutputStream();
auto jpg = juce::PNGImageFormat{};
jpg.writeImageToStream(snapshot, *out);
return juce::Result::ok();
}
} // namespace mc