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
|
2019-04-23 17:05:21 +00:00
|
|
|
# include "include/gpu/GrContext.h"
|
2018-08-08 15:36:17 +00:00
|
|
|
#else
|
|
|
|
class GrContext;
|
|
|
|
#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
|
|
|
|
// Ensure the GrContext doesn't combine GrDrawOps across draw loops.
|
|
|
|
if (GrContext* context = canvas->getGrContext()) {
|
|
|
|
context->flush();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
canvas->restoreToCount(sc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-08-29 14:39:22 +00:00
|
|
|
bool Sample::mouse(SkPoint point, skui::InputState clickState, skui::ModifierKey modifierKeys) {
|
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;
|
|
|
|
this->onClick(fClick.get());
|
|
|
|
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;
|
|
|
|
return this->onClick(fClick.get());
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
bool result = this->onClick(fClick.get());
|
|
|
|
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;
|