Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Public Member Functions | List of all members
PongSlide Class Referencefinal
Inheritance diagram for PongSlide:
Slide SkRefCnt SkRefCntBase

Public Member Functions

 PongSlide ()
 
void load (SkScalar w, SkScalar h) override
 
bool onChar (SkUnichar uni) override
 
void resize (SkScalar w, SkScalar h) override
 
void draw (SkCanvas *canvas) override
 
bool animate (double nanos) override
 
- Public Member Functions inherited from Slide
virtual SkISize getDimensions () const
 
virtual void gpuTeardown ()
 
virtual void unload ()
 
virtual bool onMouse (SkScalar x, SkScalar y, skui::InputState state, skui::ModifierKey modifiers)
 
virtual bool onGetControls (SkMetaData *)
 
virtual void onSetControls (const SkMetaData &)
 
const SkStringgetName ()
 
- Public Member Functions inherited from SkRefCntBase
 SkRefCntBase ()
 
virtual ~SkRefCntBase ()
 
bool unique () const
 
void ref () const
 
void unref () const
 

Additional Inherited Members

- Protected Attributes inherited from Slide
SkString fName
 

Detailed Description

Definition at line 80 of file SVGPongSlide.cpp.

Constructor & Destructor Documentation

◆ PongSlide()

PongSlide::PongSlide ( )
inline

Definition at line 82 of file SVGPongSlide.cpp.

82{ fName = "SGPong"; }
SkString fName
Definition Slide.h:54

Member Function Documentation

◆ animate()

bool PongSlide::animate ( double  nanos)
inlineoverridevirtual

Reimplemented from Slide.

Definition at line 197 of file SVGPongSlide.cpp.

197 {
198 // onAnimate may fire before the first draw.
199 if (fScene) {
200 SkScalar dt = (TimeUtils::NanosToMSec(nanos) - fLastTick) * fTimeScale;
201 fLastTick = TimeUtils::NanosToMSec(nanos);
202
203 fPaddle0.posTick(dt);
204 fPaddle1.posTick(dt);
205 fBall.posTick(dt);
206
207 this->enforceConstraints();
208
209 fPaddle0.updateDom();
210 fPaddle1.updateDom();
211 fBall.updateDom();
212 }
213 return true;
214 }
float SkScalar
Definition extension.cpp:12
static SkMSec NanosToMSec(double nanos)
Definition TimeUtils.h:16

◆ draw()

void PongSlide::draw ( SkCanvas canvas)
inlineoverridevirtual

Implements Slide.

Definition at line 178 of file SVGPongSlide.cpp.

178 {
180 fScene->render(canvas);
181
182 if (fShowInval) {
183 SkPaint fill, stroke;
184 fill.setAntiAlias(true);
185 fill.setColor(0x40ff0000);
186 stroke.setAntiAlias(true);
187 stroke.setColor(0xffff0000);
189
190 for (const auto& r : ic) {
191 canvas->drawRect(r, fill);
192 canvas->drawRect(r, stroke);
193 }
194 }
195 }
void drawRect(const SkRect &rect, const SkPaint &paint)
void setStyle(Style style)
Definition SkPaint.cpp:105
void setColor(SkColor color)
Definition SkPaint.cpp:119
void setAntiAlias(bool aa)
Definition SkPaint.h:170
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194

◆ load()

void PongSlide::load ( SkScalar  w,
SkScalar  h 
)
inlineoverridevirtual

Reimplemented from Slide.

Definition at line 84 of file SVGPongSlide.cpp.

84 {
85 const SkRect fieldBounds = kBounds.makeOutset(kBallSize / 2, kBallSize / 2);
86 const SkRRect ball = SkRRect::MakeOval(SkRect::MakeWH(kBallSize, kBallSize));
87 const SkRRect paddle = SkRRect::MakeRectXY(SkRect::MakeWH(kPaddleSize.width(),
88 kPaddleSize.height()),
89 kPaddleSize.width() / 2,
90 kPaddleSize.width() / 2);
91 fBall.initialize(ball,
92 SkPoint::Make(kBounds.centerX(), kBounds.centerY()),
93 SkVector::Make(fRand.nextRangeScalar(kBallSpeedMin, kBallSpeedMax),
94 fRand.nextRangeScalar(kBallSpeedMin, kBallSpeedMax)));
95 fPaddle0.initialize(paddle,
96 SkPoint::Make(fieldBounds.left() - kPaddleSize.width() / 2,
97 fieldBounds.centerY()),
98 SkVector::Make(0, 0));
99 fPaddle1.initialize(paddle,
100 SkPoint::Make(fieldBounds.right() + kPaddleSize.width() / 2,
101 fieldBounds.centerY()),
102 SkVector::Make(0, 0));
103
104 // Background decoration.
105 SkPathBuilder bgPath;
106 bgPath.moveTo(kBounds.left() , fieldBounds.top())
107 .lineTo(kBounds.right(), fieldBounds.top())
108 .moveTo(kBounds.left() , fieldBounds.bottom())
109 .lineTo(kBounds.right(), fieldBounds.bottom());
110 // TODO: stroke-dash support would come in handy right about now.
111 for (uint32_t i = 0; i < kBackgroundDashCount; ++i) {
112 bgPath.moveTo(kBounds.centerX(),
113 kBounds.top() + (i + 0.25f) * kBounds.height() / kBackgroundDashCount)
114 .lineTo(kBounds.centerX(),
115 kBounds.top() + (i + 0.75f) * kBounds.height() / kBackgroundDashCount);
116 }
117
118 auto bg_path = sksg::Path::Make(bgPath.detach());
119 auto bg_paint = sksg::Color::Make(SK_ColorBLACK);
120 bg_paint->setStyle(SkPaint::kStroke_Style);
121 bg_paint->setStrokeWidth(kBackgroundStroke);
122
123 auto ball_paint = sksg::Color::Make(SK_ColorGREEN),
124 paddle0_paint = sksg::Color::Make(SK_ColorBLUE),
125 paddle1_paint = sksg::Color::Make(SK_ColorRED),
126 shadow_paint = sksg::Color::Make(SK_ColorBLACK);
127 ball_paint->setAntiAlias(true);
128 paddle0_paint->setAntiAlias(true);
129 paddle1_paint->setAntiAlias(true);
130 shadow_paint->setAntiAlias(true);
131 shadow_paint->setOpacity(kShadowOpacity);
132
133 // Build the scene graph.
134 auto group = sksg::Group::Make();
135 group->addChild(sksg::Draw::Make(std::move(bg_path), std::move(bg_paint)));
136 group->addChild(sksg::Draw::Make(fPaddle0.shadowNode, shadow_paint));
137 group->addChild(sksg::Draw::Make(fPaddle1.shadowNode, shadow_paint));
138 group->addChild(sksg::Draw::Make(fBall.shadowNode, shadow_paint));
139 group->addChild(sksg::Draw::Make(fPaddle0.objectNode, paddle0_paint));
140 group->addChild(sksg::Draw::Make(fPaddle1.objectNode, paddle1_paint));
141 group->addChild(sksg::Draw::Make(fBall.objectNode, ball_paint));
142
143 // Handle everything in a normalized 1x1 space.
144 fContentMatrix = sksg::Matrix<SkMatrix>::Make(
146 SkRect::MakeWH(w, h)));
147 auto root = sksg::TransformEffect::Make(std::move(group), fContentMatrix);
148 fScene = sksg::Scene::Make(std::move(root));
149
150 // Off we go.
151 this->updatePaddleStrategy();
152 }
constexpr SkColor SK_ColorBLUE
Definition SkColor.h:135
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorBLACK
Definition SkColor.h:103
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
static SkMatrix RectToRect(const SkRect &src, const SkRect &dst, ScaleToFit mode=kFill_ScaleToFit)
Definition SkMatrix.h:157
SkPathBuilder & lineTo(SkPoint pt)
SkPathBuilder & moveTo(SkPoint pt)
static SkRRect MakeOval(const SkRect &oval)
Definition SkRRect.h:162
static SkRRect MakeRectXY(const SkRect &rect, SkScalar xRad, SkScalar yRad)
Definition SkRRect.h:180
SkScalar nextRangeScalar(SkScalar min, SkScalar max)
Definition SkRandom.h:106
static sk_sp< Color > Make(SkColor c)
Definition SkSGPaint.cpp:44
static sk_sp< Draw > Make(sk_sp< GeometryNode > geo, sk_sp< PaintNode > paint)
Definition SkSGDraw.h:35
static sk_sp< Group > Make()
Definition SkSGGroup.h:31
static sk_sp< Matrix > Make(const T &m)
static sk_sp< Path > Make()
Definition SkSGPath.h:31
static std::unique_ptr< Scene > Make(sk_sp< RenderNode > root)
Definition SkSGScene.cpp:16
static sk_sp< TransformEffect > Make(sk_sp< RenderNode > child, sk_sp< Transform > transform)
SkScalar w
SkScalar h
static constexpr SkPoint Make(float x, float y)
constexpr float left() const
Definition SkRect.h:734
constexpr float top() const
Definition SkRect.h:741
constexpr float right() const
Definition SkRect.h:748
constexpr float centerY() const
Definition SkRect.h:785
constexpr float width() const
Definition SkRect.h:762
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609
constexpr float bottom() const
Definition SkRect.h:755

◆ onChar()

bool PongSlide::onChar ( SkUnichar  uni)
inlineoverridevirtual

Reimplemented from Slide.

Definition at line 154 of file SVGPongSlide.cpp.

154 {
155 switch (uni) {
156 case '[':
157 fTimeScale = SkTPin(fTimeScale - 0.1f, kTimeScaleMin, kTimeScaleMax);
158 return true;
159 case ']':
160 fTimeScale = SkTPin(fTimeScale + 0.1f, kTimeScaleMin, kTimeScaleMax);
161 return true;
162 case 'I':
163 fShowInval = !fShowInval;
164 return true;
165 default:
166 break;
167 }
168 return false;
169 }
static constexpr const T & SkTPin(const T &x, const T &lo, const T &hi)
Definition SkTPin.h:19

◆ resize()

void PongSlide::resize ( SkScalar  w,
SkScalar  h 
)
inlineoverridevirtual

Reimplemented from Slide.

Definition at line 171 of file SVGPongSlide.cpp.

171 {
172 if (fContentMatrix) {
173 fContentMatrix->setMatrix(SkMatrix::RectToRect(SkRect::MakeWH(1, 1),
174 SkRect::MakeWH(w, h)));
175 }
176 }

The documentation for this class was generated from the following file: