Otclient  14/8/2020
uiprogressrect.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 "uiprogressrect.h"
24 #include <framework/otml/otml.h>
27 
29 {
30  m_percent = 0;
31 }
32 
34 {
35  if((drawPane & Fw::ForegroundPane) == 0)
36  return;
37 
39 
40  // todo: check +1 to right/bottom
41  // todo: add smooth
42  Rect drawRect = getPaddingRect();
43 
44  // 0% - 12.5% (12.5)
45  // triangle from top center, to top right (var x)
46  if(m_percent < 12.5) {
47  Point var = Point(std::max<int>(m_percent - 0.0, 0.0) * (drawRect.right() - drawRect.horizontalCenter()) / 12.5, 0);
48  g_painter->drawFilledTriangle(drawRect.center(), drawRect.topRight() + Point(1,0), drawRect.topCenter() + var);
49  }
50 
51  // 12.5% - 37.5% (25)
52  // triangle from top right to bottom right (var y)
53  if(m_percent < 37.5) {
54  Point var = Point(0, std::max<int>(m_percent - 12.5, 0.0) * (drawRect.bottom() - drawRect.top()) / 25.0);
55  g_painter->drawFilledTriangle(drawRect.center(), drawRect.bottomRight() + Point(1,1), drawRect.topRight() + var + Point(1,0));
56  }
57 
58  // 37.5% - 62.5% (25)
59  // triangle from bottom right to bottom left (var x)
60  if(m_percent < 62.5) {
61  Point var = Point(std::max<int>(m_percent - 37.5, 0.0) * (drawRect.right() - drawRect.left()) / 25.0, 0);
62  g_painter->drawFilledTriangle(drawRect.center(), drawRect.bottomLeft() + Point(0,1), drawRect.bottomRight() - var + Point(1,1));
63  }
64 
65  // 62.5% - 87.5% (25)
66  // triangle from bottom left to top left
67  if(m_percent < 87.5) {
68  Point var = Point(0, std::max<int>(m_percent - 62.5, 0.0) * (drawRect.bottom() - drawRect.top()) / 25.0);
69  g_painter->drawFilledTriangle(drawRect.center(), drawRect.topLeft(), drawRect.bottomLeft() - var + Point(0,1));
70  }
71 
72  // 87.5% - 100% (12.5)
73  // triangle from top left to top center
74  if(m_percent < 100) {
75  Point var = Point(std::max<int>(m_percent - 87.5, 0.0) * (drawRect.horizontalCenter() - drawRect.left()) / 12.5, 0);
76  g_painter->drawFilledTriangle(drawRect.center(), drawRect.topCenter(), drawRect.topLeft() + var);
77  }
78 
83 }
84 
85 void UIProgressRect::setPercent(float percent)
86 {
87  m_percent = stdext::clamp<float>((double)percent, 0.0, 100.0);
88 }
89 
90 void UIProgressRect::onStyleApply(const std::string& styleName, const OTMLNodePtr& styleNode)
91 {
92  UIWidget::onStyleApply(styleName, styleNode);
93 
94  for(const OTMLNodePtr& node : styleNode->children()) {
95  if(node->tag() == "percent")
96  setPercent(node->value<float>());
97  }
98 }
Painter::setColor
virtual void setColor(const Color &color)
Definition: painter.h:77
UIWidget::getPaddingRect
Rect getPaddingRect()
Definition: uiwidget.cpp:1054
TRect::bottomLeft
TPoint< T > bottomLeft() const
Definition: rect.h:63
graphics.h
otml.h
TRect< int >
UIWidget::onStyleApply
virtual void onStyleApply(const std::string &styleName, const OTMLNodePtr &styleNode)
Definition: uiwidget.cpp:1461
TRect::left
T left() const
Definition: rect.h:52
OTMLNode::children
OTMLNodeList children()
Definition: otmlnode.cpp:170
TRect::bottom
T bottom() const
Definition: rect.h:55
Point
TPoint< int > Point
Definition: point.h:86
TRect::topRight
TPoint< T > topRight() const
Definition: rect.h:62
UIWidget::m_rect
Rect m_rect
Definition: uiwidget.h:62
UIWidget::drawBorder
void drawBorder(const Rect &screenCoords)
Definition: uiwidgetbasestyle.cpp:348
UIProgressRect::setPercent
void setPercent(float percent)
Definition: uiprogressrect.cpp:85
UIProgressRect::m_percent
float m_percent
Definition: uiprogressrect.h:42
TRect::topLeft
TPoint< T > topLeft() const
Definition: rect.h:60
TRect::horizontalCenter
T horizontalCenter() const
Definition: rect.h:56
UIProgressRect::UIProgressRect
UIProgressRect()
Definition: uiprogressrect.cpp:28
UIWidget::drawIcon
void drawIcon(const Rect &screenCoords)
Definition: uiwidgetbasestyle.cpp:380
fontmanager.h
UIWidget::drawImage
void drawImage(const Rect &screenCoords)
Definition: uiwidgetimage.cpp:78
TRect::right
T right() const
Definition: rect.h:54
UIWidget::m_backgroundColor
Color m_backgroundColor
Definition: uiwidget.h:282
Fw::DrawPane
DrawPane
Definition: const.h:285
stdext::shared_object_ptr< OTMLNode >
uiprogressrect.h
Fw::ForegroundPane
@ ForegroundPane
Definition: const.h:286
g_painter
Painter * g_painter
Definition: painter.cpp:28
UIProgressRect::drawSelf
void drawSelf(Fw::DrawPane drawPane)
Definition: uiprogressrect.cpp:33
TRect::top
T top() const
Definition: rect.h:53
Painter::drawFilledTriangle
virtual void drawFilledTriangle(const Point &a, const Point &b, const Point &c)=0
TRect::bottomRight
TPoint< T > bottomRight() const
Definition: rect.h:61
TPoint< int >
TRect::center
TPoint< T > center() const
Definition: rect.h:68
UIWidget::drawText
void drawText(const Rect &screenCoords)
Definition: uiwidgettext.cpp:83
TRect::topCenter
TPoint< T > topCenter() const
Definition: rect.h:64
UIProgressRect::onStyleApply
void onStyleApply(const std::string &styleName, const OTMLNodePtr &styleNode)
Definition: uiprogressrect.cpp:90