/* * Copyright 2022 Google Inc. * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef SkottieTextEditor_DEFINED #define SkottieTextEditor_DEFINED #include "include/core/SkPath.h" #include "modules/skottie/include/SkottieProperty.h" #include "tools/skui/InputState.h" #include "tools/skui/ModifierKey.h" #include // A sample WYSIWYG text editor built using the GlyphDecorator API. class SkottieTextEditor final : public skottie::GlyphDecorator { public: explicit SkottieTextEditor(std::unique_ptr&&, std::vector>&&); ~SkottieTextEditor() override; void toggleEnabled(); void onDecorate(SkCanvas*, const GlyphInfo[], size_t) override; bool onMouseInput(SkScalar x, SkScalar y, skui::InputState state, skui::ModifierKey); bool onCharInput(SkUnichar c); private: struct GlyphData { SkRect fDevBounds; // Glyph bounds mapped to device space. size_t fCluster; // UTF8 cluster index. }; std::tuple currentSelection() const; size_t closestGlyph(const SkPoint& pt) const; void drawCursor(SkCanvas*, const GlyphInfo glyphs[], size_t size) const; void insertChar(SkUnichar c); void deleteChars(size_t offset, size_t count); bool deleteSelection(); void updateDeps(const SkString&); const std::unique_ptr fTextProp; const std::vector> fDependentProps; const SkPath fCursorPath; const SkRect fCursorBounds; std::vector fGlyphData; std::tuple fSelection = {0,0}; // Indices in the glyphs domain. size_t fCursorIndex = 0; // Index in the UTF8 domain. bool fEnabled = false; bool fMouseDown = false; std::chrono::time_point fTimeBase; }; #endif // SkottieTextEditor_DEFINED