| 1 |  |  | /* | 
    
    | 2 |  |  |  *  The ManaPlus Client | 
    
    | 3 |  |  |  *  Copyright (C) 2013-2019  The ManaPlus Developers | 
    
    | 4 |  |  |  *  Copyright (C) 2019-2021  Andrei Karas | 
    
    | 5 |  |  |  * | 
    
    | 6 |  |  |  *  This file is part of The ManaPlus Client. | 
    
    | 7 |  |  |  * | 
    
    | 8 |  |  |  *  This program is free software; you can redistribute it and/or modify | 
    
    | 9 |  |  |  *  it under the terms of the GNU General Public License as published by | 
    
    | 10 |  |  |  *  the Free Software Foundation; either version 2 of the License, or | 
    
    | 11 |  |  |  *  any later version. | 
    
    | 12 |  |  |  * | 
    
    | 13 |  |  |  *  This program is distributed in the hope that it will be useful, | 
    
    | 14 |  |  |  *  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
    
    | 15 |  |  |  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
    
    | 16 |  |  |  *  GNU General Public License for more details. | 
    
    | 17 |  |  |  * | 
    
    | 18 |  |  |  *  You should have received a copy of the GNU General Public License | 
    
    | 19 |  |  |  *  along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
    
    | 20 |  |  |  */ | 
    
    | 21 |  |  |  | 
    
    | 22 |  |  | #include "gui/widgets/colorpage.h" | 
    
    | 23 |  |  |  | 
    
    | 24 |  |  | #include "gui/skin.h" | 
    
    | 25 |  |  |  | 
    
    | 26 |  |  | #include "gui/fonts/font.h" | 
    
    | 27 |  |  |  | 
    
    | 28 |  |  | #include "gui/models/colormodel.h" | 
    
    | 29 |  |  |  | 
    
    | 30 |  |  | #include "render/graphics.h" | 
    
    | 31 |  |  |  | 
    
    | 32 |  |  | #include "debug.h" | 
    
    | 33 |  |  |  | 
    
    | 34 |  | 1 | ColorPage::ColorPage(const Widget2 *const widget, | 
    
    | 35 |  |  |                      ListModel *const listModel, | 
    
    | 36 |  | 1 |                      const std::string &skin) : | 
    
    | 37 |  | 1 |     ListBox(widget, listModel, skin) | 
    
    | 38 |  |  | { | 
    
    | 39 | ✓✗✓✗ ✓✗✓✗
 ✗✗
 | 3 |     mItemPadding = mSkin != nullptr ? mSkin->getOption("itemPadding") : 1; | 
    
    | 40 | ✓✗ | 1 |     const Font *const font = getFont(); | 
    
    | 41 | ✓✗ | 2 |     mRowHeight = CAST_U32(font->getHeight() + | 
    
    | 42 |  | 1 |         2 * mItemPadding); | 
    
    | 43 | ✓✗ | 1 |     if (mListModel != nullptr) | 
    
    | 44 |  |  |     { | 
    
    | 45 |  | 3 |         setHeight(CAST_S32(getRowHeight()) * | 
    
    | 46 | ✓✗ | 1 |             mListModel->getNumberOfElements() | 
    
    | 47 | ✓✗ | 2 |             + 2 * mPadding + 20); | 
    
    | 48 |  |  |     } | 
    
    | 49 |  | 1 | } | 
    
    | 50 |  |  |  | 
    
    | 51 |  | 1 | ColorPage::~ColorPage() | 
    
    | 52 |  |  | { | 
    
    | 53 |  | 1 | } | 
    
    | 54 |  |  |  | 
    
    | 55 |  |  | void ColorPage::draw(Graphics *const graphics) | 
    
    | 56 |  |  | { | 
    
    | 57 |  |  |     BLOCK_START("ColorPage::draw") | 
    
    | 58 |  |  |  | 
    
    | 59 |  |  |     const ColorModel *const model = static_cast<const ColorModel*>( | 
    
    | 60 |  |  |         mListModel); | 
    
    | 61 |  |  |  | 
    
    | 62 |  |  |     mHighlightColor.a = CAST_U32(mAlpha * 255.0F); | 
    
    | 63 |  |  |     updateAlpha(); | 
    
    | 64 |  |  |     Font *const font = getFont(); | 
    
    | 65 |  |  |  | 
    
    | 66 |  |  |     const int rowHeight = CAST_S32(getRowHeight()); | 
    
    | 67 |  |  |     const int width = mDimension.width; | 
    
    | 68 |  |  |  | 
    
    | 69 |  |  |     if (mSelected >= 0) | 
    
    | 70 |  |  |     { | 
    
    | 71 |  |  |         graphics->setColor(mHighlightColor); | 
    
    | 72 |  |  |         graphics->fillRectangle(Rect(mPadding, | 
    
    | 73 |  |  |             rowHeight * mSelected + mPadding, | 
    
    | 74 |  |  |             mDimension.width - 2 * mPadding, rowHeight)); | 
    
    | 75 |  |  |  | 
    
    | 76 |  |  |         const ColorPair *const colors = model->getColorAt(mSelected); | 
    
    | 77 |  |  |         const std::string str = mListModel->getElementAt(mSelected); | 
    
    | 78 |  |  |         font->drawString(graphics, | 
    
    | 79 |  |  |             *colors->color1, | 
    
    | 80 |  |  |             *colors->color2, | 
    
    | 81 |  |  |             str, | 
    
    | 82 |  |  |             (width - font->getWidth(str)) / 2, | 
    
    | 83 |  |  |             mSelected * rowHeight + mPadding); | 
    
    | 84 |  |  |     } | 
    
    | 85 |  |  |  | 
    
    | 86 |  |  |     const int sz = mListModel->getNumberOfElements(); | 
    
    | 87 |  |  |     for (int i = 0, y = mPadding; i < sz; ++i, y += rowHeight) | 
    
    | 88 |  |  |     { | 
    
    | 89 |  |  |         if (i != mSelected) | 
    
    | 90 |  |  |         { | 
    
    | 91 |  |  |             const ColorPair *const colors = model->getColorAt(i); | 
    
    | 92 |  |  |             const std::string str = mListModel->getElementAt(i); | 
    
    | 93 |  |  |             font->drawString(graphics, | 
    
    | 94 |  |  |                 *colors->color1, | 
    
    | 95 |  |  |                 *colors->color2, | 
    
    | 96 |  |  |                 str, | 
    
    | 97 |  |  |                 (width - font->getWidth(str)) / 2, | 
    
    | 98 |  |  |                 y); | 
    
    | 99 |  |  |         } | 
    
    | 100 |  |  |     } | 
    
    | 101 |  |  |  | 
    
    | 102 |  |  |     BLOCK_END("ColorPage::draw") | 
    
    | 103 |  |  | } | 
    
    | 104 |  |  |  | 
    
    | 105 |  |  | void ColorPage::safeDraw(Graphics *const graphics) | 
    
    | 106 |  |  | { | 
    
    | 107 |  |  |     ColorPage::draw(graphics); | 
    
    | 108 |  |  | } | 
    
    | 109 |  |  |  | 
    
    | 110 |  |  | void ColorPage::resetAction() | 
    
    | 111 |  |  | { | 
    
    | 112 |  |  |     setSelected(-1); | 
    
    | 113 |  |  | } | 
    
    | 114 |  |  |  | 
    
    | 115 |  | 1 | void ColorPage::adjustSize() | 
    
    | 116 |  |  | { | 
    
    | 117 |  |  |     BLOCK_START("ColorPage::adjustSize") | 
    
    | 118 | ✓✗ | 1 |     if (mListModel != nullptr) | 
    
    | 119 |  |  |     { | 
    
    | 120 |  | 3 |         setHeight(CAST_S32(getRowHeight()) * | 
    
    | 121 |  | 2 |             mListModel->getNumberOfElements() + | 
    
    | 122 |  | 2 |             2 * mPadding + 20); | 
    
    | 123 |  |  |     } | 
    
    | 124 |  |  |     BLOCK_END("ColorPage::adjustSize") | 
    
    | 125 |  | 1 | } |