| 1 | // Aseprite | 
|---|---|
| 2 | // Copyright (C) 2019-2020 Igara Studio S.A. | 
| 3 | // Copyright (C) 2001-2015 David Capello | 
| 4 | // | 
| 5 | // This program is distributed under the terms of | 
| 6 | // the End-User License Agreement for Aseprite. | 
| 7 | |
| 8 | #ifdef HAVE_CONFIG_H | 
| 9 | #include "config.h" | 
| 10 | #endif | 
| 11 | |
| 12 | #include "app/tools/stroke.h" | 
| 13 | |
| 14 | namespace app { | 
| 15 | namespace tools { | 
| 16 | |
| 17 | void Stroke::reset() | 
| 18 | { | 
| 19 | m_pts.clear(); | 
| 20 | } | 
| 21 | |
| 22 | void Stroke::reset(int n, const Pt& pt) | 
| 23 | { | 
| 24 | m_pts.resize(n, pt); | 
| 25 | } | 
| 26 | |
| 27 | void Stroke::addPoint(const Pt& pt) | 
| 28 | { | 
| 29 | m_pts.push_back(pt); | 
| 30 | } | 
| 31 | |
| 32 | void Stroke::offset(const gfx::Point& delta) | 
| 33 | { | 
| 34 | for (auto& p : m_pts) { | 
| 35 | p.x += delta.x; | 
| 36 | p.y += delta.y; | 
| 37 | } | 
| 38 | } | 
| 39 | |
| 40 | void Stroke::erase(int index) | 
| 41 | { | 
| 42 | ASSERT(0 <= index && index < m_pts.size()); | 
| 43 | if (0 <= index && index < m_pts.size()) | 
| 44 | m_pts.erase(m_pts.begin()+index); | 
| 45 | } | 
| 46 | |
| 47 | gfx::Rect Stroke::bounds() const | 
| 48 | { | 
| 49 | if (m_pts.empty()) | 
| 50 | return gfx::Rect(); | 
| 51 | |
| 52 | gfx::Point | 
| 53 | minpt(m_pts[0].x, m_pts[0].y), | 
| 54 | maxpt(m_pts[0].x, m_pts[0].y); | 
| 55 | |
| 56 | for (std::size_t c=1; c<m_pts.size(); ++c) { | 
| 57 | int x = m_pts[c].x; | 
| 58 | int y = m_pts[c].y; | 
| 59 | if (minpt.x > x) minpt.x = x; | 
| 60 | if (minpt.y > y) minpt.y = y; | 
| 61 | if (maxpt.x < x) maxpt.x = x; | 
| 62 | if (maxpt.y < y) maxpt.y = y; | 
| 63 | } | 
| 64 | |
| 65 | return gfx::Rect(minpt.x, minpt.y, | 
| 66 | maxpt.x - minpt.x + 1, | 
| 67 | maxpt.y - minpt.y + 1); | 
| 68 | } | 
| 69 | |
| 70 | std::vector<int> Stroke::toXYInts() const | 
| 71 | { | 
| 72 | std::vector<int> output; | 
| 73 | if (!empty()) { | 
| 74 | output.reserve(2*size()); | 
| 75 | for (auto pt : m_pts) { | 
| 76 | output.push_back(pt.x); | 
| 77 | output.push_back(pt.y); | 
| 78 | } | 
| 79 | } | 
| 80 | return output; | 
| 81 | } | 
| 82 | |
| 83 | } // namespace tools | 
| 84 | } // namespace app | 
| 85 | 
