fast_ink_points.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef ASH_FAST_INK_FAST_INK_POINTS_H_
  5. #define ASH_FAST_INK_FAST_INK_POINTS_H_
  6. #include "ash/ash_export.h"
  7. #include "base/containers/circular_deque.h"
  8. #include "base/time/time.h"
  9. #include "third_party/skia/include/core/SkColor.h"
  10. #include "ui/gfx/geometry/rect.h"
  11. #include "ui/gfx/geometry/rect_f.h"
  12. namespace fast_ink {
  13. // FastInkPoints is a helper class used for displaying low-latency palette
  14. // tools. It contains a collection of points representing one or more
  15. // contiguous trajectory segments.
  16. class ASH_EXPORT FastInkPoints {
  17. public:
  18. static const SkColor kDefaultColor;
  19. // Struct to describe each point.
  20. struct FastInkPoint {
  21. gfx::PointF location;
  22. base::TimeTicks time;
  23. SkColor color = kDefaultColor;
  24. bool gap_after = false; // True when there is a gap after this point.
  25. };
  26. // Constructor with a parameter to choose the fade out time of the points in
  27. // the collection. Zero means no fadeout.
  28. explicit FastInkPoints(base::TimeDelta life_duration);
  29. FastInkPoints(const FastInkPoints&) = delete;
  30. FastInkPoints& operator=(const FastInkPoints&) = delete;
  31. ~FastInkPoints();
  32. // Adds a point.
  33. void AddPoint(const gfx::PointF& point, const base::TimeTicks& time);
  34. void AddPoint(const gfx::PointF& point,
  35. const base::TimeTicks& time,
  36. SkColor color);
  37. // Adds a gap after the most recent point. This is useful for multi-stroke
  38. // gesture handling (e.g. strokes going over the bezel).
  39. void AddGap();
  40. // Updates the collection latest time. Automatically clears points that are
  41. // too old.
  42. void MoveForwardToTime(const base::TimeTicks& latest_time);
  43. // Removes the last stroke by removing the points since the last gap. If there
  44. // are no gaps, deletes all points. Returns the bounding rectangle of the
  45. // deleted points.
  46. gfx::Rect UndoLastStroke();
  47. // Removes all points.
  48. void Clear();
  49. // Gets the bounding box of the points, int coordinates.
  50. gfx::Rect GetBoundingBox() const;
  51. // Gets the bounding box of the points, float coordinates.
  52. gfx::RectF GetBoundingBoxF() const;
  53. // Returns the oldest point in the collection.
  54. FastInkPoint GetOldest() const;
  55. // Returns the newest point in the collection.
  56. FastInkPoint GetNewest() const;
  57. // Returns the number of points in the collection.
  58. int GetNumberOfPoints() const;
  59. // Whether there are any points or not.
  60. bool IsEmpty() const;
  61. // Expose the collection so callers can work with the points.
  62. const base::circular_deque<FastInkPoint>& points() const;
  63. // Returns the fadeout factor for a point. This is a value between 0.0 and
  64. // 1.0, where 0.0 corresponds to a recently added point, and 1.0 to a point
  65. // that is about to expire. Do not call this method if |life_duration_| is 0.
  66. float GetFadeoutFactor(int index) const;
  67. // Fills the container with predicted points based on |real_points|.
  68. void Predict(const FastInkPoints& real_points,
  69. const base::TimeTicks& current_time,
  70. base::TimeDelta prediction_duration,
  71. const gfx::Size& screen_size);
  72. private:
  73. const base::TimeDelta life_duration_;
  74. base::circular_deque<FastInkPoint> points_;
  75. // The latest time of the collection of points. This gets updated when new
  76. // points are added or when MoveForwardToTime is called.
  77. base::TimeTicks collection_latest_time_;
  78. };
  79. } // namespace fast_ink
  80. #endif // ASH_FAST_INK_FAST_INK_POINTS_H_