Particles: Remove play-position thing, simplify mouse tracking

Change-Id: I36ce78ee1de8096d9aa164444e8af31b3831a3ab
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/258217
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
This commit is contained in:
Brian Osman 2019-12-05 14:38:14 -05:00 committed by Skia Commit-Bot
parent b0047b57b7
commit ffbfd1bc78
2 changed files with 17 additions and 64 deletions

View File

@ -21,14 +21,6 @@
using namespace sk_app;
namespace {
static SkScalar kDragSize = 8.0f;
static SkTArray<SkPoint*> gDragPoints;
int gDragIndex = -1;
}
///////////////////////////////////////////////////////////////////////////////
static int InputTextCallback(ImGuiInputTextCallbackData* data) {
@ -178,7 +170,6 @@ ParticlesSlide::ParticlesSlide() {
// Register types for serialization
SkParticleEffect::RegisterParticleTypes();
fName = "Particles";
fPlayPosition.set(200.0f, 200.0f);
fResourceProvider = skresources::FileResourceProvider::Make(GetResourcePath());
}
@ -207,9 +198,6 @@ void ParticlesSlide::load(SkScalar winWidth, SkScalar winHeight) {
void ParticlesSlide::draw(SkCanvas* canvas) {
canvas->clear(SK_ColorGRAY);
gDragPoints.reset();
gDragPoints.push_back(&fPlayPosition);
// Window to show all loaded effects, and allow playing them
if (ImGui::Begin("Library", nullptr, ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
static bool looped = true;
@ -257,7 +245,7 @@ void ParticlesSlide::draw(SkCanvas* canvas) {
sk_sp<SkParticleEffect> effect(new SkParticleEffect(fLoaded[i].fParams,
fRandom));
effect->start(fAnimationTime, looped);
fRunning.push_back({ fPlayPosition, fLoaded[i].fName, effect, false });
fRunning.push_back({ fLoaded[i].fName, effect, false });
fRandom.nextU();
}
ImGui::SameLine();
@ -278,6 +266,10 @@ void ParticlesSlide::draw(SkCanvas* canvas) {
}
ImGui::End();
// Most effects are centered around the origin, so we shift the canvas...
constexpr SkVector kTranslation = { 250.0f, 250.0f };
const SkPoint mousePos = fMousePos - kTranslation;
// Another window to show all the running effects
if (ImGui::Begin("Running")) {
for (int i = 0; i < fRunning.count(); ++i) {
@ -288,14 +280,12 @@ void ParticlesSlide::draw(SkCanvas* canvas) {
ImGui::SameLine();
bool remove = ImGui::Button("X") || !effect->isAlive();
ImGui::SameLine();
ImGui::Text("%4g, %4g %5d %s", fRunning[i].fPosition.fX, fRunning[i].fPosition.fY,
effect->getCount(), fRunning[i].fName.c_str());
ImGui::Text("%5d %s", effect->getCount(), fRunning[i].fName.c_str());
if (fRunning[i].fTrackMouse) {
effect->setPosition({ ImGui::GetMousePos().x, ImGui::GetMousePos().y });
fRunning[i].fPosition.set(0, 0);
effect->setPosition(mousePos);
}
auto uniformsGui = [](const SkSL::ByteCode* code, float* data, SkPoint spawnPos) {
auto uniformsGui = [mousePos](const SkSL::ByteCode* code, float* data) {
if (!code || !data) {
return;
}
@ -312,9 +302,8 @@ void ParticlesSlide::draw(SkCanvas* canvas) {
if (uni.fName == "mouse_pos" &&
uni.fType == SkSL::TypeCategory::kFloat &&
uni.fRows == 2 && uni.fColumns == 1) {
ImVec2 mousePos = ImGui::GetMousePos();
vals[0] = mousePos.x - spawnPos.fX;
vals[1] = mousePos.y - spawnPos.fY;
vals[0] = mousePos.fX;
vals[1] = mousePos.fY;
continue;
}
@ -349,8 +338,8 @@ void ParticlesSlide::draw(SkCanvas* canvas) {
}
}
};
uniformsGui(effect->effectCode(), effect->effectUniforms(), fRunning[i].fPosition);
uniformsGui(effect->particleCode(), effect->particleUniforms(), fRunning[i].fPosition);
uniformsGui(effect->effectCode(), effect->effectUniforms());
uniformsGui(effect->particleCode(), effect->particleUniforms());
if (remove) {
fRunning.removeShuffle(i);
}
@ -359,26 +348,12 @@ void ParticlesSlide::draw(SkCanvas* canvas) {
}
ImGui::End();
SkPaint dragPaint;
dragPaint.setColor(SK_ColorLTGRAY);
dragPaint.setAntiAlias(true);
SkPaint dragHighlight;
dragHighlight.setStyle(SkPaint::kStroke_Style);
dragHighlight.setColor(SK_ColorGREEN);
dragHighlight.setStrokeWidth(2);
dragHighlight.setAntiAlias(true);
for (int i = 0; i < gDragPoints.count(); ++i) {
canvas->drawCircle(*gDragPoints[i], kDragSize, dragPaint);
if (gDragIndex == i) {
canvas->drawCircle(*gDragPoints[i], kDragSize, dragHighlight);
}
}
canvas->save();
canvas->translate(kTranslation.fX, kTranslation.fY);
for (const auto& effect : fRunning) {
canvas->save();
canvas->translate(effect.fPosition.fX, effect.fPosition.fY);
effect.fEffect->draw(canvas);
canvas->restore();
}
canvas->restore();
}
bool ParticlesSlide::animate(double nanos) {
@ -391,27 +366,6 @@ bool ParticlesSlide::animate(double nanos) {
}
bool ParticlesSlide::onMouse(SkScalar x, SkScalar y, skui::InputState state, skui::ModifierKey modifiers) {
if (gDragIndex == -1) {
if (state == skui::InputState::kDown) {
float bestDistance = kDragSize;
SkPoint mousePt = { x, y };
for (int i = 0; i < gDragPoints.count(); ++i) {
float distance = SkPoint::Distance(*gDragPoints[i], mousePt);
if (distance < bestDistance) {
gDragIndex = i;
bestDistance = distance;
}
}
return gDragIndex != -1;
}
} else {
// Currently dragging
SkASSERT(gDragIndex < gDragPoints.count());
gDragPoints[gDragIndex]->set(x, y);
if (state == skui::InputState::kUp) {
gDragIndex = -1;
}
return true;
}
fMousePos.set(x, y);
return false;
}

View File

@ -39,7 +39,7 @@ private:
SkRandom fRandom;
bool fAnimated = false;
double fAnimationTime = 0;
SkPoint fPlayPosition;
SkPoint fMousePos = { 0, 0 };
struct LoadedEffect {
SkString fName;
@ -48,7 +48,6 @@ private:
SkTArray<LoadedEffect> fLoaded;
struct RunningEffect {
SkPoint fPosition;
SkString fName;
sk_sp<SkParticleEffect> fEffect;
bool fTrackMouse;