2016-06-16 16:52:35 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SampleSlide.h"
|
|
|
|
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkCommonFlags.h"
|
|
|
|
#include "SkOSFile.h"
|
|
|
|
#include "SkStream.h"
|
|
|
|
|
2017-11-02 13:46:49 +00:00
|
|
|
using namespace sk_app;
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
SampleSlide::SampleSlide(const SampleFactory factory)
|
|
|
|
: fSampleFactory(factory)
|
2017-11-02 13:46:49 +00:00
|
|
|
, fClick(nullptr) {
|
2018-08-08 15:36:17 +00:00
|
|
|
sk_sp<Sample> sample(factory());
|
|
|
|
Sample::RequestTitle(sample.get(), &fName);
|
2016-06-16 16:52:35 +00:00
|
|
|
}
|
|
|
|
|
2017-11-02 13:46:49 +00:00
|
|
|
SampleSlide::~SampleSlide() { delete fClick; }
|
2016-06-16 16:52:35 +00:00
|
|
|
|
2018-09-04 22:35:20 +00:00
|
|
|
SkISize SampleSlide::getDimensions() const {
|
|
|
|
return SkISize::Make(SkScalarCeilToInt(fSample->width()), SkScalarCeilToInt(fSample->height()));
|
|
|
|
}
|
|
|
|
|
2016-06-16 16:52:35 +00:00
|
|
|
void SampleSlide::draw(SkCanvas* canvas) {
|
2018-08-08 15:36:17 +00:00
|
|
|
SkASSERT(fSample);
|
|
|
|
fSample->draw(canvas);
|
2016-06-16 16:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SampleSlide::load(SkScalar winWidth, SkScalar winHeight) {
|
2018-08-08 15:36:17 +00:00
|
|
|
fSample.reset(fSampleFactory());
|
|
|
|
fSample->setSize(winWidth, winHeight);
|
2016-06-16 16:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SampleSlide::unload() {
|
2018-08-08 15:36:17 +00:00
|
|
|
fSample.reset();
|
2016-06-16 16:52:35 +00:00
|
|
|
}
|
|
|
|
|
2017-02-14 20:16:46 +00:00
|
|
|
bool SampleSlide::onChar(SkUnichar c) {
|
2018-08-08 15:36:17 +00:00
|
|
|
if (!fSample) {
|
2017-02-24 20:22:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-08-08 15:36:17 +00:00
|
|
|
Sample::Event evt(Sample::kCharEvtName);
|
2017-02-14 20:16:46 +00:00
|
|
|
evt.setFast32(c);
|
2018-08-08 15:36:17 +00:00
|
|
|
return fSample->doQuery(&evt);
|
2017-02-14 20:16:46 +00:00
|
|
|
}
|
|
|
|
|
2017-11-02 13:46:49 +00:00
|
|
|
bool SampleSlide::onMouse(SkScalar x, SkScalar y, Window::InputState state,
|
|
|
|
uint32_t modifiers) {
|
2018-08-08 15:36:17 +00:00
|
|
|
// map to Sample::Click modifiers
|
2017-11-02 13:46:49 +00:00
|
|
|
unsigned modifierKeys = 0;
|
2018-08-08 15:36:17 +00:00
|
|
|
modifierKeys |= (modifiers & Window::kShift_ModifierKey) ? Sample::Click::kShift_ModifierKey : 0;
|
|
|
|
modifierKeys |= (modifiers & Window::kControl_ModifierKey) ? Sample::Click::kControl_ModifierKey : 0;
|
|
|
|
modifierKeys |= (modifiers & Window::kOption_ModifierKey) ? Sample::Click::kOption_ModifierKey : 0;
|
|
|
|
modifierKeys |= (modifiers & Window::kCommand_ModifierKey) ? Sample::Click::kCommand_ModifierKey : 0;
|
2017-11-02 13:46:49 +00:00
|
|
|
|
|
|
|
bool handled = false;
|
|
|
|
switch (state) {
|
|
|
|
case Window::kDown_InputState: {
|
|
|
|
delete fClick;
|
2018-08-08 15:36:17 +00:00
|
|
|
fClick = fSample->findClickHandler(SkIntToScalar(x), SkIntToScalar(y), modifierKeys);
|
2017-11-02 13:46:49 +00:00
|
|
|
if (fClick) {
|
2018-08-08 15:36:17 +00:00
|
|
|
Sample::DoClickDown(fClick, x, y, modifierKeys);
|
2017-11-02 13:46:49 +00:00
|
|
|
handled = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Window::kMove_InputState: {
|
|
|
|
if (fClick) {
|
2018-08-08 15:36:17 +00:00
|
|
|
Sample::DoClickMoved(fClick, x, y, modifierKeys);
|
2017-11-02 13:46:49 +00:00
|
|
|
handled = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Window::kUp_InputState: {
|
|
|
|
if (fClick) {
|
2018-08-08 15:36:17 +00:00
|
|
|
Sample::DoClickUp(fClick, x, y, modifierKeys);
|
2017-11-02 13:46:49 +00:00
|
|
|
delete fClick;
|
|
|
|
fClick = nullptr;
|
|
|
|
handled = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return handled;
|
|
|
|
}
|