| 1 | // LAF Gfx Library |
| 2 | // Copyright (C) 2019 Igara Studio S.A. |
| 3 | // Copyright (C) 2001-2015 David Capello |
| 4 | // |
| 5 | // This file is released under the terms of the MIT license. |
| 6 | // Read LICENSE.txt for more information. |
| 7 | |
| 8 | #ifndef GFX_TEXTURE_SIZE_H_INCLUDED |
| 9 | #define GFX_TEXTURE_SIZE_H_INCLUDED |
| 10 | #pragma once |
| 11 | |
| 12 | #include "base/task.h" |
| 13 | #include "gfx/fwd.h" |
| 14 | #include "gfx/rect.h" |
| 15 | #include <vector> |
| 16 | |
| 17 | namespace gfx { |
| 18 | |
| 19 | // TODO add support for rotations |
| 20 | class PackingRects { |
| 21 | public: |
| 22 | PackingRects(int borderPadding = 0, int shapePadding = 0) : |
| 23 | m_borderPadding(borderPadding), |
| 24 | m_shapePadding(shapePadding) { |
| 25 | } |
| 26 | |
| 27 | typedef std::vector<Rect> Rects; |
| 28 | typedef Rects::const_iterator const_iterator; |
| 29 | |
| 30 | // Iterate over all given rectangles (in the same order they where |
| 31 | // given in addSize() calls). |
| 32 | const_iterator begin() const { return m_rects.begin(); } |
| 33 | const_iterator end() const { return m_rects.end(); } |
| 34 | |
| 35 | std::size_t size() const { return m_rects.size(); } |
| 36 | const Rect& operator[](int i) const { return m_rects[i]; } |
| 37 | |
| 38 | // Adds a new rectangle. |
| 39 | void add(const Size& sz); |
| 40 | void add(const Rect& rc); |
| 41 | |
| 42 | // Returns the best size for the texture. |
| 43 | Size bestFit(base::task_token& token, |
| 44 | const int fixedWidth = 0, |
| 45 | const int fixedHeight = 0); |
| 46 | |
| 47 | // Rearrange all given rectangles to best fit a texture size. |
| 48 | // Returns true if all rectangles were correctly arranged or false |
| 49 | // if there is not enough space. |
| 50 | bool pack(const Size& size, |
| 51 | base::task_token& token); |
| 52 | |
| 53 | // Returns the bounds of the packed area. |
| 54 | const Rect& bounds() const { return m_bounds; } |
| 55 | |
| 56 | private: |
| 57 | int m_borderPadding; |
| 58 | int m_shapePadding; |
| 59 | |
| 60 | Rect m_bounds; |
| 61 | Rects m_rects; |
| 62 | }; |
| 63 | |
| 64 | } // namespace gfx |
| 65 | |
| 66 | #endif |
| 67 | |