uiitem.cpp

main 155 lines · 4.5 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1/*
2 * Copyright (c) 2010-2017 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 "spritemanager.h"
25#include "game.h"
26#include <framework/otml/otml.h>
27#include <framework/graphics/graphics.h>
28#include <framework/graphics/fontmanager.h>
29
30UIItem::UIItem()
31{
32 m_draggable = true;
33}
34
35void UIItem::drawSelf(Fw::DrawPane drawPane)
36{
37 if(drawPane != Fw::ForegroundPane)
38 return;
39 // draw style components in order
40 if(m_backgroundColor.aF() > Fw::MIN_ALPHA) {
41 Rect backgroundDestRect = m_rect;
42 backgroundDestRect.expand(-m_borderWidth.top, -m_borderWidth.right, -m_borderWidth.bottom, -m_borderWidth.left);
43 drawBackground(m_rect);
44 }
45
46 drawImage(m_rect);
47
48 if(m_itemVisible && m_item) {
49 Rect drawRect = getPaddingRect();
50
51 int exactSize = std::max<int>(g_sprites.spriteSize(), m_item->getExactSize());
52 if(exactSize == 0)
53 return;
54
55 m_item->setColor(m_color);
56 m_item->draw(drawRect);
57
58 if(m_font && m_showCount && (m_item->isStackable() || m_item->isChargeable()) && m_item->getCountOrSubType() > 1) {
59 g_drawQueue->addText(m_font, m_countText, Rect(m_rect.topLeft(), m_rect.bottomRight() - Point(3, 0)), Fw::AlignBottomRight, Color(231, 231, 231));
60 }
61
62 if (m_showId) {
63 g_drawQueue->addText(m_font, std::to_string(m_item->getServerId()), m_rect, Fw::AlignBottomRight, Color(231, 231, 231));
64 }
65 }
66
67 drawBorder(m_rect);
68 drawIcon(m_rect);
69 drawText(m_rect);
70}
71
72void UIItem::setItemId(int id)
73{
74 if (!m_item && id != 0)
75 m_item = Item::create(id);
76 else {
77 // remove item
78 if (id == 0)
79 m_item = nullptr;
80 else
81 m_item->setId(id);
82 }
83
84 if (m_item)
85 m_item->setShader(m_shader);
86
87 callLuaField("onItemChange");
88}
89
90void UIItem::setItemCount(int count)
91{
92 if (m_item) {
93 m_item->setCount(count);
94 callLuaField("onItemChange");
95 cacheCountText();
96 }
97}
98void UIItem::setItemSubType(int subType)
99{
100 if (m_item) {
101 m_item->setSubType(subType);
102 callLuaField("onItemChange");
103 }
104}
105
106void UIItem::setItem(const ItemPtr& item)
107{
108 m_item = item;
109 if (m_item) {
110 m_item->setShader(m_shader);
111 cacheCountText();
112 callLuaField("onItemChange");
113 }
114}
115
116void UIItem::setItemShader(const std::string& str)
117{
118 m_shader = str;
119
120 if (m_item) {
121 m_item->setShader(m_shader);
122 callLuaField("onItemChange");
123 }
124}
125
126void UIItem::onStyleApply(const std::string& styleName, const OTMLNodePtr& styleNode)
127{
128 UIWidget::onStyleApply(styleName, styleNode);
129
130 for(const OTMLNodePtr& node : styleNode->children()) {
131 if(node->tag() == "item-id")
132 setItemId(node->value<int>());
133 else if(node->tag() == "item-count")
134 setItemCount(node->value<int>());
135 else if(node->tag() == "item-visible")
136 setItemVisible(node->value<bool>());
137 else if(node->tag() == "virtual")
138 setVirtual(node->value<bool>());
139 else if(node->tag() == "show-id")
140 m_showId = node->value<bool>();
141 else if(node->tag() == "shader")
142 setItemShader(node->value());
143 }
144}
145
146void UIItem::cacheCountText()
147{
148 int count = m_item->getCountOrSubType();
149 if (!g_game.getFeature(Otc::GameCountU16) || count < 1000) {
150 m_countText = std::to_string(count);
151 return;
152 }
153
154 m_countText = stdext::format("%.0fk", count / 1000.0);
155}
Top