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 "MaterialDataScrollHandlerSlice.h" 24 : 25 : namespace STAPPLER_VERSIONIZED stappler::xenolith::material2d { 26 : 27 0 : bool DataScrollHandlerSlice::init(DataScroll *s, DataCallback &&cb) { 28 0 : if (!Handler::init(s)) { 29 0 : return false; 30 : } 31 : 32 0 : auto &items = s->getItems(); 33 0 : if (!items.empty()) { 34 0 : _originFront = items.begin()->second->getPosition(); 35 0 : _originBack = items.rbegin()->second->getPosition(); 36 0 : _originBack.y += items.rbegin()->second->getContentSize().height; 37 : } 38 : 39 0 : _dataCallback = move(cb); 40 : 41 0 : return true; 42 : } 43 : 44 0 : void DataScrollHandlerSlice::setDataCallback(DataCallback &&cb) { 45 0 : _dataCallback = move(cb); 46 0 : } 47 : 48 0 : DataScroll::ItemMap DataScrollHandlerSlice::run(Request t, DataMap &&data) { 49 0 : DataScroll::ItemMap ret; 50 : 51 0 : auto origin = getOrigin(t); 52 : 53 0 : if (t == DataScroll::Request::Front) { 54 0 : for (auto it = data.rbegin(); it != data.rend(); it ++) { 55 0 : auto item = onItem(std::move(it->second), origin); 56 : 57 0 : item->setPosition( 58 0 : (_layout == DataScroll::Layout::Vertical) 59 0 : ?Vec2(origin.x, origin.y - item->getContentSize().height) 60 0 : :Vec2(origin.x - item->getContentSize().height, origin.y)); 61 : 62 0 : if (_layout == DataScroll::Layout::Vertical) { 63 0 : origin.y -= item->getContentSize().height; 64 : } else { 65 0 : origin.x -= item->getContentSize().width; 66 : } 67 : 68 0 : ret.insert(std::make_pair(it->first, item)); 69 0 : } 70 : } else { 71 0 : for (auto &it : data) { 72 0 : auto item = onItem(std::move(it.second), origin); 73 : 74 0 : if (_layout == DataScroll::Layout::Vertical) { 75 0 : origin.y += item->getContentSize().height; 76 : } else { 77 0 : origin.x += item->getContentSize().width; 78 : } 79 : 80 0 : ret.insert(std::make_pair(it.first, item)); 81 0 : } 82 : } 83 0 : return ret; 84 0 : } 85 : 86 0 : Vec2 DataScrollHandlerSlice::getOrigin(Request t) const { 87 0 : Vec2 origin = Vec2(0, 0); 88 : 89 0 : switch (t) { 90 0 : case DataScroll::Request::Reset: 91 0 : origin = Vec2(0, 0); 92 0 : break; 93 0 : case DataScroll::Request::Update: 94 : case DataScroll::Request::Front: 95 0 : origin = _originFront; 96 0 : break; 97 0 : case DataScroll::Request::Back: 98 0 : origin = _originBack; 99 0 : break; 100 : } 101 : 102 0 : return origin; 103 : } 104 : 105 0 : Rc<DataScroll::Item> DataScrollHandlerSlice::onItem(Value &&d, const Vec2 &o) { 106 0 : if (_dataCallback) { 107 0 : return _dataCallback(this, std::move(d), o); 108 : } 109 0 : return nullptr; 110 : } 111 : 112 : }