blob: 3985d8de0764445c5aaa85c40281c2f74d7d48df [file] [log] [blame]
/*
* 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 <chrono>
// A sample WYSIWYG text editor built using the GlyphDecorator API.
class SkottieTextEditor final : public skottie::GlyphDecorator {
public:
explicit SkottieTextEditor(std::unique_ptr<skottie::TextPropertyHandle>&&);
~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.
};
std::tuple<size_t, size_t> 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);
const std::unique_ptr<skottie::TextPropertyHandle> fTextProp;
const SkPath fCursorPath;
const SkRect fCursorBounds;
std::vector<GlyphData> fGlyphData;
std::tuple<size_t, size_t> fSelection = {0,0};
size_t fCursorIndex = 0;
bool fEnabled = false;
bool fMouseDown = false;
std::chrono::time_point<std::chrono::steady_clock> fTimeBase;
};
#endif // SkottieTextEditor_DEFINED