-
Notifications
You must be signed in to change notification settings - Fork 399
/
Copy pathuiwidgetimage.cpp
196 lines (177 loc) · 9.32 KB
/
uiwidgetimage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "uiwidget.h"
#include <framework/graphics/painter.h>
#include <framework/graphics/texture.h>
#include <framework/graphics/texturemanager.h>
#include <framework/graphics/graphics.h>
void UIWidget::initImage()
{
m_imageCoordsBuffer.enableHardwareCaching();
}
void UIWidget::parseImageStyle(const OTMLNodePtr& styleNode)
{
for(const OTMLNodePtr& node : styleNode->children()) {
if(node->tag() == "image-source")
setImageSource(stdext::resolve_path(node->value(), node->source()));
else if(node->tag() == "image-offset-x")
setImageOffsetX(node->value<int>());
else if(node->tag() == "image-offset-y")
setImageOffsetY(node->value<int>());
else if(node->tag() == "image-offset")
setImageOffset(node->value<Point>());
else if(node->tag() == "image-width")
setImageWidth(node->value<int>());
else if(node->tag() == "image-height")
setImageHeight(node->value<int>());
else if(node->tag() == "image-size")
setImageSize(node->value<Size>());
else if(node->tag() == "image-rect")
setImageRect(node->value<Rect>());
else if(node->tag() == "image-clip")
setImageClip(node->value<Rect>());
else if(node->tag() == "image-fixed-ratio")
setImageFixedRatio(node->value<bool>());
else if(node->tag() == "image-repeated")
setImageRepeated(node->value<bool>());
else if(node->tag() == "image-smooth")
setImageSmooth(node->value<bool>());
else if(node->tag() == "image-color")
setImageColor(node->value<Color>());
else if(node->tag() == "image-border-top")
setImageBorderTop(node->value<int>());
else if(node->tag() == "image-border-right")
setImageBorderRight(node->value<int>());
else if(node->tag() == "image-border-bottom")
setImageBorderBottom(node->value<int>());
else if(node->tag() == "image-border-left")
setImageBorderLeft(node->value<int>());
else if(node->tag() == "image-border")
setImageBorder(node->value<int>());
else if(node->tag() == "image-auto-resize")
setImageAutoResize(node->value<bool>());
}
}
void UIWidget::drawImage(const Rect& screenCoords)
{
if(!m_imageTexture || !screenCoords.isValid())
return;
// cache vertex buffers
if(m_imageCachedScreenCoords != screenCoords || m_imageMustRecache) {
m_imageCoordsBuffer.clear();
m_imageCachedScreenCoords = screenCoords;
m_imageMustRecache = false;
Rect drawRect = screenCoords;
drawRect.translate(m_imageRect.topLeft());
if(m_imageRect.isValid())
drawRect.resize(m_imageRect.size());
Rect clipRect = m_imageClipRect.isValid() ? m_imageClipRect : Rect(0, 0, m_imageTexture->getSize());
if(!m_imageBordered) {
if(m_imageFixedRatio) {
Size textureSize = m_imageTexture->getSize();
Size textureClipSize = drawRect.size();
textureClipSize.scale(textureSize, Fw::KeepAspectRatio);
Point texCoordsOffset;
if(textureSize.height() > textureClipSize.height())
texCoordsOffset.y = (textureSize.height() - textureClipSize.height())/2;
else if(textureSize.width() > textureClipSize.width())
texCoordsOffset.x = (textureSize.width() - textureClipSize.width())/2;
Rect textureClipRect(texCoordsOffset, textureClipSize);
m_imageCoordsBuffer.addRect(drawRect, textureClipRect);
} else {
if(m_imageRepeated)
m_imageCoordsBuffer.addRepeatedRects(drawRect, clipRect);
else
m_imageCoordsBuffer.addRect(drawRect, clipRect);
}
} else {
int top = m_imageBorder.top;
int bottom = m_imageBorder.bottom;
int left = m_imageBorder.left;
int right = m_imageBorder.right;
// calculates border coords
const Rect clip = clipRect;
Rect leftBorder(clip.left(), clip.top() + top, left, clip.height() - top - bottom);
Rect rightBorder(clip.right() - right + 1, clip.top() + top, right, clip.height() - top - bottom);
Rect topBorder(clip.left() + left, clip.top(), clip.width() - right - left, top);
Rect bottomBorder(clip.left() + left, clip.bottom() - bottom + 1, clip.width() - right - left, bottom);
Rect topLeftCorner(clip.left(), clip.top(), left, top);
Rect topRightCorner(clip.right() - right + 1, clip.top(), right, top);
Rect bottomLeftCorner(clip.left(), clip.bottom() - bottom + 1, left, bottom);
Rect bottomRightCorner(clip.right() - right + 1, clip.bottom() - bottom + 1, right, bottom);
Rect center(clip.left() + left, clip.top() + top, clip.width() - right - left, clip.height() - top - bottom);
Size bordersSize(leftBorder.width() + rightBorder.width(), topBorder.height() + bottomBorder.height());
Size centerSize = drawRect.size() - bordersSize;
Rect rectCoords;
// first the center
if(centerSize.area() > 0) {
rectCoords = Rect(drawRect.left() + leftBorder.width(), drawRect.top() + topBorder.height(), centerSize);
m_imageCoordsBuffer.addRepeatedRects(rectCoords, center);
}
// top left corner
rectCoords = Rect(drawRect.topLeft(), topLeftCorner.size());
m_imageCoordsBuffer.addRepeatedRects(rectCoords, topLeftCorner);
// top
rectCoords = Rect(drawRect.left() + topLeftCorner.width(), drawRect.topLeft().y, centerSize.width(), topBorder.height());
m_imageCoordsBuffer.addRepeatedRects(rectCoords, topBorder);
// top right corner
rectCoords = Rect(drawRect.left() + topLeftCorner.width() + centerSize.width(), drawRect.top(), topRightCorner.size());
m_imageCoordsBuffer.addRepeatedRects(rectCoords, topRightCorner);
// left
rectCoords = Rect(drawRect.left(), drawRect.top() + topLeftCorner.height(), leftBorder.width(), centerSize.height());
m_imageCoordsBuffer.addRepeatedRects(rectCoords, leftBorder);
// right
rectCoords = Rect(drawRect.left() + leftBorder.width() + centerSize.width(), drawRect.top() + topRightCorner.height(), rightBorder.width(), centerSize.height());
m_imageCoordsBuffer.addRepeatedRects(rectCoords, rightBorder);
// bottom left corner
rectCoords = Rect(drawRect.left(), drawRect.top() + topLeftCorner.height() + centerSize.height(), bottomLeftCorner.size());
m_imageCoordsBuffer.addRepeatedRects(rectCoords, bottomLeftCorner);
// bottom
rectCoords = Rect(drawRect.left() + bottomLeftCorner.width(), drawRect.top() + topBorder.height() + centerSize.height(), centerSize.width(), bottomBorder.height());
m_imageCoordsBuffer.addRepeatedRects(rectCoords, bottomBorder);
// bottom right corner
rectCoords = Rect(drawRect.left() + bottomLeftCorner.width() + centerSize.width(), drawRect.top() + topRightCorner.height() + centerSize.height(), bottomRightCorner.size());
m_imageCoordsBuffer.addRepeatedRects(rectCoords, bottomRightCorner);
}
}
// smooth is now enabled by default for all textures
//m_imageTexture->setSmooth(m_imageSmooth);
g_painter->setColor(m_imageColor);
g_painter->drawTextureCoords(m_imageCoordsBuffer, m_imageTexture);
}
void UIWidget::setImageSource(const std::string& source)
{
if(source.empty())
m_imageTexture = nullptr;
else
m_imageTexture = g_textures.getTexture(source);
if(m_imageTexture && (!m_rect.isValid() || m_imageAutoResize)) {
Size size = getSize();
Size imageSize = m_imageTexture->getSize();
if(size.width() <= 0 || m_imageAutoResize)
size.setWidth(imageSize.width());
if(size.height() <= 0 || m_imageAutoResize)
size.setHeight(imageSize.height());
setSize(size);
}
m_imageMustRecache = true;
}