Line data Source code
1 : /** 2 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev> 3 : 4 : Permission is hereby granted, free of charge, to any person obtaining a copy 5 : of this software and associated documentation files (the "Software"), to deal 6 : in the Software without restriction, including without limitation the rights 7 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 : copies of the Software, and to permit persons to whom the Software is 9 : furnished to do so, subject to the following conditions: 10 : 11 : The above copyright notice and this permission notice shall be included in 12 : all copies or substantial portions of the Software. 13 : 14 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 : THE SOFTWARE. 21 : **/ 22 : 23 : #include "MaterialDataScrollHandlerGrid.h" 24 : 25 : namespace STAPPLER_VERSIONIZED stappler::xenolith::material2d { 26 : 27 0 : bool DataScrollHandlerGrid::init(DataScroll *s) { 28 0 : if (!Handler::init(s)) { 29 0 : return false; 30 : } 31 : 32 0 : _padding = s->getPadding(); 33 : 34 0 : return true; 35 : } 36 : 37 20 : bool DataScrollHandlerGrid::init(DataScroll *s, const Padding &p) { 38 20 : if (!Handler::init(s)) { 39 0 : return false; 40 : } 41 : 42 20 : _padding = p; 43 : 44 20 : return true; 45 : } 46 : 47 20 : DataScroll::ItemMap DataScrollHandlerGrid::run(Request t, DataMap &&data) { 48 20 : DataScroll::ItemMap ret; 49 20 : auto size = _size; 50 20 : size.width -= (_padding.left + _padding.right); 51 : 52 20 : uint32_t cols = uint32_t(floorf(size.width / _cellMinWidth)); 53 20 : if (cols == 0) { 54 0 : cols = 1; 55 : } 56 : 57 20 : auto cellWidth = (_autoPaddings?(std::min(_cellMinWidth, size.width / cols)):(size.width / cols)); 58 20 : auto cellHeight = (_fixedHeight?_cellHeight:cellWidth / _cellAspectRatio); 59 : 60 20 : _currentCellSize = Size2(cellWidth, cellHeight); 61 20 : _currentCols = uint32_t(cols); 62 20 : _widthPadding = (_size.width - _currentCellSize.width * _currentCols) / 2.0f; 63 : 64 420 : for (auto &it : data) { 65 400 : auto item = onItem(std::move(it.second), it.first); 66 400 : ret.insert(std::make_pair(it.first, item)); 67 400 : } 68 40 : return ret; 69 0 : } 70 : 71 20 : void DataScrollHandlerGrid::setCellMinWidth(float v) { 72 20 : _cellMinWidth = v; 73 20 : } 74 : 75 20 : void DataScrollHandlerGrid::setCellAspectRatio(float v) { 76 20 : _cellAspectRatio = v; 77 20 : _fixedHeight = false; 78 20 : } 79 20 : void DataScrollHandlerGrid::setCellHeight(float v) { 80 20 : _cellHeight = v; 81 20 : _fixedHeight = true; 82 20 : } 83 : 84 20 : void DataScrollHandlerGrid::setAutoPaddings(bool value) { 85 20 : _autoPaddings = value; 86 20 : } 87 : 88 20 : bool DataScrollHandlerGrid::isAutoPaddings() const { 89 20 : return _autoPaddings; 90 : } 91 : 92 400 : Rc<DataScroll::Item> DataScrollHandlerGrid::onItem(Value &&data, DataSource::Id id) { 93 400 : DataSource::Id::Type row = id.get() / _currentCols; 94 400 : DataSource::Id::Type col = id.get() % _currentCols; 95 : 96 400 : Vec2 pos(col * _currentCellSize.width + _widthPadding, row * _currentCellSize.height); 97 : 98 800 : return Rc<DataScroll::Item>::create(std::move(data), pos, _currentCellSize); 99 : } 100 : 101 : }