Otclient  14/8/2020
uiitem.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
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 "uiitem.h"
24 #include <framework/otml/otml.h>
27 
29 {
30  m_draggable = true;
31 }
32 
34 {
35  if((drawPane & Fw::ForegroundPane) == 0)
36  return;
37 
38  // draw style components in order
39  if(m_backgroundColor.aF() > Fw::MIN_ALPHA) {
40  Rect backgroundDestRect = m_rect;
43  }
44 
46 
47  if(m_itemVisible && m_item) {
48  Rect drawRect = getPaddingRect();
49  Point dest = drawRect.bottomRight() + Point(1,1);
50 
51  int exactSize = std::max<int>(32, m_item->getExactSize());
52  if(exactSize == 0)
53  return;
54 
55  float scaleFactor = std::min<float>(drawRect.width() / (float)exactSize, drawRect.height() / (float)exactSize);
56  dest += (m_item->getDisplacement() - Point(32,32)) * scaleFactor;
57 
59  m_item->draw(dest, scaleFactor, true);
60 
62  std::string count = stdext::to_string(m_item->getCountOrSubType());
63  g_painter->setColor(Color(231, 231, 231));
65  }
66 
67  if(m_showId)
69  }
70 
74 }
75 
76 void UIItem::setItemId(int id)
77 {
78  if(!m_item && id != 0)
79  m_item = Item::create(id);
80  else {
81  // remove item
82  if(id == 0)
83  m_item = nullptr;
84  else
85  m_item->setId(id);
86  }
87 }
88 
89 void UIItem::onStyleApply(const std::string& styleName, const OTMLNodePtr& styleNode)
90 {
91  UIWidget::onStyleApply(styleName, styleNode);
92 
93  for(const OTMLNodePtr& node : styleNode->children()) {
94  if(node->tag() == "item-id")
95  setItemId(node->value<int>());
96  else if(node->tag() == "item-count")
97  setItemCount(node->value<int>());
98  else if(node->tag() == "item-visible")
99  setItemVisible(node->value<bool>());
100  else if(node->tag() == "virtual")
101  setVirtual(node->value<bool>());
102  else if(node->tag() == "show-id")
103  m_showId = node->value<bool>();
104  }
105 }
Painter::setColor
virtual void setColor(const Color &color)
Definition: painter.h:77
EdgeGroup::right
T right
Definition: uiwidget.h:41
UIWidget::getPaddingRect
Rect getPaddingRect()
Definition: uiwidget.cpp:1054
graphics.h
Color
Definition: color.h:32
UIItem::m_itemVisible
stdext::boolean< true > m_itemVisible
Definition: uiitem.h:56
Thing::isChargeable
bool isChargeable()
Definition: thing.h:94
otml.h
TRect< int >
UIItem::UIItem
UIItem()
Definition: uiitem.cpp:28
UIWidget::onStyleApply
virtual void onStyleApply(const std::string &styleName, const OTMLNodePtr &styleNode)
Definition: uiwidget.cpp:1461
Fw::AlignBottomRight
@ AlignBottomRight
Definition: const.h:203
EdgeGroup::top
T top
Definition: uiwidget.h:40
UIWidget::m_borderWidth
EdgeGroup< int > m_borderWidth
Definition: uiwidget.h:290
OTMLNode::children
OTMLNodeList children()
Definition: otmlnode.cpp:170
Item::setId
void setId(uint32 id)
Definition: item.cpp:93
Point
TPoint< int > Point
Definition: point.h:86
UIWidget::m_rect
Rect m_rect
Definition: uiwidget.h:62
UIWidget::drawBorder
void drawBorder(const Rect &screenCoords)
Definition: uiwidgetbasestyle.cpp:348
uiitem.h
UIWidget::m_font
BitmapFontPtr m_font
Definition: uiwidget.h:489
TRect::topLeft
TPoint< T > topLeft() const
Definition: rect.h:60
UIItem::setItemVisible
void setItemVisible(bool visible)
Definition: uiitem.h:39
TRect::expand
void expand(T top, T right, T bottom, T left)
Definition: rect.h:95
Item::draw
void draw(const Point &dest, float scaleFactor, bool animate, LightView *lightView=nullptr)
Definition: item.cpp:70
Item::getServerId
uint16 getServerId()
Definition: item.h:99
UIItem::setVirtual
void setVirtual(bool virt)
Definition: uiitem.h:41
UIWidget::m_draggable
stdext::boolean< false > m_draggable
Definition: uiwidget.h:69
Color::aF
float aF() const
Definition: color.h:49
UIWidget::drawIcon
void drawIcon(const Rect &screenCoords)
Definition: uiwidgetbasestyle.cpp:380
Rect
TRect< int > Rect
Definition: rect.h:319
Item::create
static ItemPtr create(int id)
Definition: item.cpp:51
stdext::to_string
std::string to_string(const T &t)
Definition: string.h:35
BitmapFont::drawText
void drawText(const std::string &text, const Point &startPos)
Simple text render starting at startPos.
Definition: bitmapfont.cpp:80
fontmanager.h
UIWidget::drawImage
void drawImage(const Rect &screenCoords)
Definition: uiwidgetimage.cpp:78
UIWidget::m_backgroundColor
Color m_backgroundColor
Definition: uiwidget.h:282
Thing::getDisplacement
virtual Point getDisplacement()
Definition: thing.h:68
Fw::DrawPane
DrawPane
Definition: const.h:285
UIItem::drawSelf
void drawSelf(Fw::DrawPane drawPane)
Definition: uiitem.cpp:33
stdext::shared_object_ptr< OTMLNode >
Item::getExactSize
int getExactSize(int layer=0, int xPattern=0, int yPattern=0, int zPattern=0, int animationPhase=0)
Definition: item.cpp:403
Fw::ForegroundPane
@ ForegroundPane
Definition: const.h:286
g_painter
Painter * g_painter
Definition: painter.cpp:28
TRect::height
T height() const
Definition: rect.h:70
UIItem::m_item
ItemPtr m_item
Definition: uiitem.h:54
UIItem::setItemCount
void setItemCount(int count)
Definition: uiitem.h:37
UIItem::m_showId
stdext::boolean< false > m_showId
Definition: uiitem.h:57
TRect::bottomRight
TPoint< T > bottomRight() const
Definition: rect.h:61
TPoint< int >
EdgeGroup::bottom
T bottom
Definition: uiwidget.h:42
EdgeGroup::left
T left
Definition: uiwidget.h:43
TRect::width
T width() const
Definition: rect.h:69
UIWidget::drawText
void drawText(const Rect &screenCoords)
Definition: uiwidgettext.cpp:83
UIItem::setItemId
void setItemId(int id)
Definition: uiitem.cpp:76
UIItem::onStyleApply
void onStyleApply(const std::string &styleName, const OTMLNodePtr &styleNode)
Definition: uiitem.cpp:89
Thing::isStackable
bool isStackable()
Definition: thing.h:90
Item::getCountOrSubType
int getCountOrSubType()
Definition: item.h:94
UIWidget::drawBackground
void drawBackground(const Rect &screenCoords)
Definition: uiwidgetbasestyle.cpp:336
UIWidget::m_color
Color m_color
Definition: uiwidget.h:281