2018-08-08 15:36:17 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "samplecode/Sample.h"
|
2018-08-08 15:36:17 +00:00
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
2020-07-09 17:25:17 +00:00
|
|
|
# include "include/gpu/GrDirectContext.h"
|
2018-08-08 15:36:17 +00:00
|
|
|
#else
|
2020-10-20 14:11:53 +00:00
|
|
|
class GrDirectContext;
|
2018-08-08 15:36:17 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void Sample::setSize(SkScalar width, SkScalar height) {
|
2020-02-05 18:34:09 +00:00
|
|
|
width = std::max(0.0f, width);
|
|
|
|
height = std::max(0.0f, height);
|
2018-08-08 15:36:17 +00:00
|
|
|
|
|
|
|
if (fWidth != width || fHeight != height)
|
|
|
|
{
|
|
|
|
fWidth = width;
|
|
|
|
fHeight = height;
|
|
|
|
this->onSizeChange();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sample::draw(SkCanvas* canvas) {
|
|
|
|
if (fWidth && fHeight) {
|
|
|
|
SkRect r;
|
2019-08-24 23:39:13 +00:00
|
|
|
r.setLTRB(0, 0, fWidth, fHeight);
|
2018-08-08 15:36:17 +00:00
|
|
|
if (canvas->quickReject(r)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkAutoCanvasRestore as(canvas, true);
|
|
|
|
int sc = canvas->save();
|
|
|
|
|
|
|
|
if (!fHaveCalledOnceBeforeDraw) {
|
|
|
|
fHaveCalledOnceBeforeDraw = true;
|
|
|
|
this->onOnceBeforeDraw();
|
|
|
|
}
|
|
|
|
this->onDrawBackground(canvas);
|
|
|
|
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
this->onDrawContent(canvas);
|
|
|
|
#if SK_SUPPORT_GPU
|
2020-10-20 14:11:53 +00:00
|
|
|
// Ensure the context doesn't combine GrDrawOps across draw loops.
|
2020-07-09 17:25:17 +00:00
|
|
|
if (auto direct = GrAsDirectContext(canvas->recordingContext())) {
|
|
|
|
direct->flushAndSubmit();
|
2018-08-08 15:36:17 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
canvas->restoreToCount(sc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-08 21:09:06 +00:00
|
|
|
bool Sample::animate(double nanos) {
|
|
|
|
if (!fHaveCalledOnceBeforeDraw) {
|
|
|
|
fHaveCalledOnceBeforeDraw = true;
|
|
|
|
this->onOnceBeforeDraw();
|
|
|
|
}
|
|
|
|
return this->onAnimate(nanos);
|
|
|
|
}
|
2018-08-08 15:36:17 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-08-29 14:39:22 +00:00
|
|
|
bool Sample::mouse(SkPoint point, skui::InputState clickState, skui::ModifierKey modifierKeys) {
|
2020-04-22 15:09:30 +00:00
|
|
|
auto dispatch = [this](Click* c) {
|
|
|
|
return c->fHasFunc ? c->fFunc(c) : this->onClick(c);
|
|
|
|
};
|
|
|
|
|
2019-07-12 15:32:43 +00:00
|
|
|
switch (clickState) {
|
2019-08-29 14:39:22 +00:00
|
|
|
case skui::InputState::kDown:
|
2019-07-12 15:32:43 +00:00
|
|
|
fClick = nullptr;
|
|
|
|
fClick.reset(this->onFindClickHandler(point.x(), point.y(), modifierKeys));
|
|
|
|
if (!fClick) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
fClick->fPrev = fClick->fCurr = fClick->fOrig = point;
|
2019-08-29 14:39:22 +00:00
|
|
|
fClick->fState = skui::InputState::kDown;
|
2019-07-12 15:32:43 +00:00
|
|
|
fClick->fModifierKeys = modifierKeys;
|
2020-04-22 15:09:30 +00:00
|
|
|
dispatch(fClick.get());
|
2019-07-12 15:32:43 +00:00
|
|
|
return true;
|
2019-08-29 14:39:22 +00:00
|
|
|
case skui::InputState::kMove:
|
2019-07-12 15:32:43 +00:00
|
|
|
if (fClick) {
|
|
|
|
fClick->fPrev = fClick->fCurr;
|
|
|
|
fClick->fCurr = point;
|
2019-08-29 14:39:22 +00:00
|
|
|
fClick->fState = skui::InputState::kMove;
|
2019-07-12 15:32:43 +00:00
|
|
|
fClick->fModifierKeys = modifierKeys;
|
2020-04-22 15:09:30 +00:00
|
|
|
return dispatch(fClick.get());
|
2019-07-12 15:32:43 +00:00
|
|
|
}
|
|
|
|
return false;
|
2019-08-29 14:39:22 +00:00
|
|
|
case skui::InputState::kUp:
|
2019-07-12 15:32:43 +00:00
|
|
|
if (fClick) {
|
|
|
|
fClick->fPrev = fClick->fCurr;
|
|
|
|
fClick->fCurr = point;
|
2019-08-29 14:39:22 +00:00
|
|
|
fClick->fState = skui::InputState::kUp;
|
2019-07-12 15:32:43 +00:00
|
|
|
fClick->fModifierKeys = modifierKeys;
|
2020-04-22 15:09:30 +00:00
|
|
|
bool result = dispatch(fClick.get());
|
2019-07-12 15:32:43 +00:00
|
|
|
fClick = nullptr;
|
|
|
|
return result;
|
|
|
|
}
|
2019-07-16 13:43:35 +00:00
|
|
|
return false;
|
2019-09-09 20:53:39 +00:00
|
|
|
default:
|
|
|
|
// Ignore other cases
|
|
|
|
SkASSERT(false);
|
|
|
|
break;
|
2018-08-08 15:36:17 +00:00
|
|
|
}
|
2019-07-12 15:32:43 +00:00
|
|
|
SkASSERT(false);
|
|
|
|
return false;
|
2018-08-08 15:36:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void Sample::onSizeChange() {}
|
|
|
|
|
2019-08-29 14:39:22 +00:00
|
|
|
Sample::Click* Sample::onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) {
|
2018-08-08 15:36:17 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Sample::onClick(Click*) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sample::onDrawBackground(SkCanvas* canvas) {
|
|
|
|
canvas->drawColor(fBGColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
// need to explicitly declare this, or we get some weird infinite loop llist
|
|
|
|
template SampleRegistry* SampleRegistry::gHead;
|