LCOV - code coverage report
Current view: top level - xenolith/renderer/material2d/base - MaterialMenuSource.cc (source / functions) Hit Total Coverage
Test: coverage.info Lines: 91 199 45.7 %
Date: 2024-05-12 00:16:13 Functions: 29 55 52.7 %

          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 "MaterialMenuSource.h"
      24             : #include "XLNode.h"
      25             : 
      26             : namespace STAPPLER_VERSIONIZED stappler::xenolith::material2d {
      27             : 
      28         126 : bool MenuSourceItem::init() {
      29         126 :         return true;
      30             : }
      31             : 
      32           0 : Rc<MenuSourceItem> MenuSourceItem::copy() const {
      33           0 :         auto ret = Rc<MenuSourceItem>::create();
      34           0 :         ret->setCustomData(_customData);
      35           0 :         return ret;
      36           0 : }
      37             : 
      38           0 : void MenuSourceItem::setCustomData(const Value &val) {
      39           0 :         _customData = val;
      40           0 :         setDirty();
      41           0 : }
      42             : 
      43           0 : void MenuSourceItem::setCustomData(Value &&val) {
      44           0 :         _customData = std::move(val);
      45           0 :         setDirty();
      46           0 : }
      47             : 
      48           0 : const Value &MenuSourceItem::getCustomData() const {
      49           0 :         return _customData;
      50             : }
      51             : 
      52           0 : MenuSourceItem * MenuSourceItem::setAttachCallback(const AttachCallback &cb) {
      53           0 :         _attachCallback = cb;
      54           0 :         return this;
      55             : }
      56             : 
      57           0 : MenuSourceItem * MenuSourceItem::setDetachCallback(const AttachCallback &cb) {
      58           0 :         _detachCallback = cb;
      59           0 :         return this;
      60             : }
      61             : 
      62         210 : MenuSourceItem::Type MenuSourceItem::getType() const {
      63         210 :         return _type;
      64             : }
      65             : 
      66         166 : void MenuSourceItem::handleNodeAttached(Node *n) {
      67         166 :         if (_attachCallback) {
      68           0 :                 _attachCallback(this, n);
      69             :         }
      70         166 : }
      71             : 
      72           0 : void MenuSourceItem::handleNodeDetached(Node *n) {
      73           0 :         if (_detachCallback) {
      74           0 :                 _detachCallback(this, n);
      75             :         }
      76           0 : }
      77             : 
      78          74 : void MenuSourceItem::setDirty() {
      79          74 :         Subscription::setDirty();
      80          74 : }
      81             : 
      82         232 : MenuSourceButton::~MenuSourceButton() { }
      83             : 
      84         106 : bool MenuSourceButton::init(StringView str, IconName name, Callback &&cb) {
      85         106 :         if (!init()) {
      86           0 :                 return false;
      87             :         }
      88             : 
      89         106 :         _name = str.str<Interface>();
      90         106 :         _nameIcon = name;
      91         106 :         _callback = move(cb);
      92         106 :         return true;
      93             : }
      94             : 
      95          10 : bool MenuSourceButton::init(StringView str, IconName name, Rc<MenuSource> &&menu) {
      96          10 :         if (!init()) {
      97           0 :                 return false;
      98             :         }
      99             : 
     100          10 :         _name = str.str<Interface>();
     101          10 :         _nameIcon = name;
     102          10 :         _nextMenu = move(menu);
     103          10 :         return true;
     104             : }
     105             : 
     106         116 : bool MenuSourceButton::init() {
     107         116 :         if (!MenuSourceItem::init()) {
     108           0 :                 return false;
     109             :         }
     110             : 
     111         116 :         _type = Type::Button;
     112         116 :         return true;
     113             : }
     114             : 
     115           0 : Rc<MenuSourceItem> MenuSourceButton::copy() const {
     116           0 :         auto ret = Rc<MenuSourceButton>::create();
     117           0 :         ret->setName(_name);
     118           0 :         ret->setNameIcon(_nameIcon);
     119           0 :         ret->setValue(_name);
     120           0 :         ret->setValueIcon(_nameIcon);
     121           0 :         ret->setSelected(_selected);
     122           0 :         ret->setNextMenu(_nextMenu);
     123           0 :         ret->setCallback(Callback(_callback));
     124           0 :         ret->setCustomData(_customData);
     125           0 :         return ret;
     126           0 : }
     127             : 
     128           0 : void MenuSourceButton::setName(StringView val) {
     129           0 :         if (_name != val) {
     130           0 :                 _name = val.str<Interface>();
     131           0 :                 setDirty();
     132             :         }
     133           0 : }
     134         350 : const String & MenuSourceButton::getName() const {
     135         350 :         return _name;
     136             : }
     137             : 
     138          30 : void MenuSourceButton::setValue(StringView val) {
     139          30 :         if (_value != val) {
     140          30 :                 _value = val.str<Interface>();
     141          30 :                 setDirty();
     142             :         }
     143          30 : }
     144         344 : const String & MenuSourceButton::getValue() const {
     145         344 :         return _value;
     146             : }
     147             : 
     148          10 : void MenuSourceButton::setNameIcon(IconName icon) {
     149          10 :         if (_nameIcon != icon) {
     150          10 :                 _nameIcon = icon;
     151          10 :                 setDirty();
     152             :         }
     153          10 : }
     154         464 : IconName MenuSourceButton::getNameIcon() const {
     155         464 :         return _nameIcon;
     156             : }
     157             : 
     158          20 : void MenuSourceButton::setValueIcon(IconName icon) {
     159          20 :         if (_valueIcon != icon) {
     160          20 :                 _valueIcon = icon;
     161          20 :                 setDirty();
     162             :         }
     163          20 : }
     164         344 : IconName MenuSourceButton::getValueIcon() const {
     165         344 :         return _valueIcon;
     166             : }
     167             : 
     168           0 : void MenuSourceButton::setCallback(Callback &&cb) {
     169           0 :         _callback = move(cb);
     170           0 :         setDirty();
     171           0 : }
     172         425 : const MenuSourceButton::Callback & MenuSourceButton::getCallback() const {
     173         425 :         return _callback;
     174             : }
     175             : 
     176           0 : void MenuSourceButton::setNextMenu(MenuSource *menu) {
     177           0 :         if (menu != _nextMenu) {
     178           0 :                 _nextMenu = menu;
     179           0 :                 setDirty();
     180             :         }
     181           0 : }
     182         738 : MenuSource * MenuSourceButton::getNextMenu() const {
     183         738 :         return _nextMenu ? _nextMenu.get() : nullptr;
     184             : }
     185             : 
     186          26 : void MenuSourceButton::setSelected(bool value) {
     187          26 :         if (_selected != value) {
     188          14 :                 _selected = value;
     189          14 :                 setDirty();
     190             :         }
     191          26 : }
     192         352 : bool MenuSourceButton::isSelected() const {
     193         352 :         return _selected;
     194             : }
     195             : 
     196           0 : bool MenuSourceCustom::init() {
     197           0 :         if (!MenuSourceItem::init()) {
     198           0 :                 return false;
     199             :         }
     200             : 
     201           0 :         _type = Type::Custom;
     202           0 :         return true;
     203             : }
     204             : 
     205           0 : bool MenuSourceCustom::init(float h, const FactoryFunction &func, float minWidth) {
     206           0 :         return init([h] (Node *, float w) { return h; }, func);
     207             : }
     208             : 
     209           0 : bool MenuSourceCustom::init(const HeightFunction &h, const FactoryFunction &func, float minWidth) {
     210           0 :         if (!init()) {
     211           0 :                 return false;
     212             :         }
     213             : 
     214           0 :         _minWidth = minWidth;
     215           0 :         _heightFunction = h;
     216           0 :         _function = func;
     217           0 :         return true;
     218             : }
     219             : 
     220           0 : Rc<MenuSourceItem> MenuSourceCustom::copy() const {
     221           0 :         auto ret = Rc<MenuSourceCustom>::create(_heightFunction, _function);
     222           0 :         ret->setCustomData(_customData);
     223           0 :         return ret;
     224           0 : }
     225             : 
     226           0 : float MenuSourceCustom::getMinWidth() const {
     227           0 :         return _minWidth;
     228             : }
     229             : 
     230           0 : float MenuSourceCustom::getHeight(Node *n, float w) const {
     231           0 :         return _heightFunction(n, w);
     232             : }
     233             : 
     234           0 : const MenuSourceCustom::HeightFunction & MenuSourceCustom::getHeightFunction() const {
     235           0 :         return _heightFunction;
     236             : }
     237             : 
     238           0 : const MenuSourceCustom::FactoryFunction & MenuSourceCustom::getFactoryFunction() const {
     239           0 :         return _function;
     240             : }
     241             : 
     242         124 : MenuSource::~MenuSource() { }
     243             : 
     244           0 : void MenuSource::setHintCount(size_t h) {
     245           0 :         _hintCount = h;
     246           0 : }
     247             : 
     248           0 : size_t MenuSource::getHintCount() const {
     249           0 :         return _hintCount;
     250             : }
     251             : 
     252           0 : Rc<MenuSource> MenuSource::copy() const {
     253           0 :         auto ret = Rc<MenuSource>::create();
     254           0 :         for (auto &it : _items) {
     255           0 :                 ret->addItem(it->copy());
     256             :         }
     257           0 :         ret->setHintCount(_hintCount);
     258           0 :         return ret;
     259           0 : }
     260             : 
     261         126 : void MenuSource::addItem(MenuSourceItem *item) {
     262         126 :         if (item) {
     263         126 :                 _items.emplace_back(item);
     264         126 :                 setDirty();
     265             :         }
     266         126 : }
     267             : 
     268          60 : MenuSourceButton *MenuSource::addButton(StringView str, Callback &&cb) {
     269          60 :         auto item = Rc<MenuSourceButton>::create(str, IconName::None, move(cb));
     270          60 :         addItem(item);
     271         120 :         return item;
     272          60 : }
     273             : 
     274          46 : MenuSourceButton *MenuSource::addButton(StringView str, IconName name, Callback &&cb) {
     275          46 :         auto item = Rc<MenuSourceButton>::create(str, name, move(cb));
     276          46 :         addItem(item);
     277          92 :         return item;
     278          46 : }
     279             : 
     280          10 : MenuSourceButton *MenuSource::addButton(StringView str, IconName name, Rc<MenuSource> &&source) {
     281          10 :         auto item = Rc<MenuSourceButton>::create(str, name, move(source));
     282          10 :         addItem(item);
     283          20 :         return item;
     284          10 : }
     285             : 
     286           0 : MenuSourceCustom *MenuSource::addCustom(float h, const MenuSourceCustom::FactoryFunction &func, float w) {
     287           0 :         auto item = Rc<MenuSourceCustom>::create(h, func, w);
     288           0 :         addItem(item);
     289           0 :         return item;
     290           0 : }
     291             : 
     292           0 : MenuSourceCustom *MenuSource::addCustom(const HeightFunction &h, const FactoryFunction &func, float w) {
     293           0 :         auto item = Rc<MenuSourceCustom>::create(h, func, w);
     294           0 :         addItem(item);
     295           0 :         return item;
     296           0 : }
     297             : 
     298          10 : MenuSourceItem *MenuSource::addSeparator() {
     299          10 :         auto item = Rc<MenuSourceItem>::create();
     300          10 :         addItem(item);
     301          20 :         return item;
     302          10 : }
     303             : 
     304           0 : void MenuSource::clear() {
     305           0 :         _items.clear();
     306           0 :         setDirty();
     307           0 : }
     308             : 
     309          30 : uint32_t MenuSource::count() {
     310          30 :         return (uint32_t)_items.size();
     311             : }
     312             : 
     313          52 : const Vector<Rc<MenuSourceItem>> &MenuSource::getItems() const {
     314          52 :         return _items;
     315             : }
     316             : 
     317             : }

Generated by: LCOV version 1.14