void UIWidget::draw(const Rect& visibleRect, Fw::DrawPane drawPane) { Rect oldClipRect; if(m_clipping) { oldClipRect = g_painter->getClipRect(); g_painter->setClipRect(visibleRect); } if(m_rotation != 0.0f) { g_painter->pushTransformMatrix(); g_painter->rotate(m_rect.center(), m_rotation * (Fw::pi / 180.0)); } drawSelf(drawPane); if(m_children.size() > 0) { if(m_clipping) g_painter->setClipRect(visibleRect.intersection(getPaddingRect())); drawChildren(visibleRect, drawPane); } if(m_rotation != 0.0f) g_painter->popTransformMatrix(); if(m_clipping) { g_painter->setClipRect(oldClipRect); } }
bool Input::setViewPort(const TextRenderInfo &info, SDL_Rect &rcText, SDL_Rect &rcViewPort) { if (_text.empty()) return false; memcpy(&rcViewPort, &rcText, sizeof(SDL_Rect)); rcViewPort.x = 0; rcViewPort.y = 0; SDL_Rect rcPading; getPaddingRect(rcPading); if (rcText.w <= rcPading.w) return true; rcViewPort.w = rcText.w; rcViewPort.h = rcText.h; rcViewPort.x = rcText.w - rcPading.w; rcViewPort.w = rcPading.w; rcText.w = rcPading.w; return true; }
void UIMinimap::drawSelf(Fw::DrawPane drawPane) { UIWidget::drawSelf(drawPane); if((drawPane & Fw::ForegroundPane) == 0) return; g_minimap.draw(getPaddingRect(), getCameraPosition(), m_scale, m_color); }
std::string UITextEdit::getDisplayedText() { std::string text; if(m_textHidden) text = std::string(m_text.length(), '*'); else text = m_text; if(m_textWrap && m_rect.isValid()) text = m_font->wrapText(text, getPaddingRect().width() - m_textOffset.x); return text; }
void UIProgressRect::drawSelf(Fw::DrawPane drawPane) { if((drawPane & Fw::ForegroundPane) == 0) return; g_painter->setColor(m_backgroundColor); // todo: check +1 to right/bottom // todo: add smooth Rect drawRect = getPaddingRect(); // 0% - 12.5% (12.5) // triangle from top center, to top right (var x) if(m_percent < 12.5) { Point var = Point(std::max<int>(m_percent - 0.0, 0.0) * (drawRect.right() - drawRect.horizontalCenter()) / 12.5, 0); g_painter->drawFilledTriangle(drawRect.center(), drawRect.topRight() + Point(1,0), drawRect.topCenter() + var); } // 12.5% - 37.5% (25) // triangle from top right to bottom right (var y) if(m_percent < 37.5) { Point var = Point(0, std::max<int>(m_percent - 12.5, 0.0) * (drawRect.bottom() - drawRect.top()) / 25.0); g_painter->drawFilledTriangle(drawRect.center(), drawRect.bottomRight() + Point(1,1), drawRect.topRight() + var + Point(1,0)); } // 37.5% - 62.5% (25) // triangle from bottom right to bottom left (var x) if(m_percent < 62.5) { Point var = Point(std::max<int>(m_percent - 37.5, 0.0) * (drawRect.right() - drawRect.left()) / 25.0, 0); g_painter->drawFilledTriangle(drawRect.center(), drawRect.bottomLeft() + Point(0,1), drawRect.bottomRight() - var + Point(1,1)); } // 62.5% - 87.5% (25) // triangle from bottom left to top left if(m_percent < 87.5) { Point var = Point(0, std::max<int>(m_percent - 62.5, 0.0) * (drawRect.bottom() - drawRect.top()) / 25.0); g_painter->drawFilledTriangle(drawRect.center(), drawRect.topLeft(), drawRect.bottomLeft() - var + Point(0,1)); } // 87.5% - 100% (12.5) // triangle from top left to top center if(m_percent < 100) { Point var = Point(std::max<int>(m_percent - 87.5, 0.0) * (drawRect.horizontalCenter() - drawRect.left()) / 12.5, 0); g_painter->drawFilledTriangle(drawRect.center(), drawRect.topCenter(), drawRect.topLeft() + var); } drawImage(m_rect); drawBorder(m_rect); drawIcon(m_rect); drawText(m_rect); }
void UIItem::drawSelf(Fw::DrawPane drawPane) { if((drawPane & Fw::ForegroundPane) == 0) return; // draw style components in order if(m_backgroundColor.aF() > Fw::MIN_ALPHA) { Rect backgroundDestRect = m_rect; backgroundDestRect.expand(-m_borderWidth.top, -m_borderWidth.right, -m_borderWidth.bottom, -m_borderWidth.left); drawBackground(m_rect); } drawImage(m_rect); if(m_itemVisible && m_item) { Rect drawRect = getPaddingRect(); Point dest = drawRect.bottomRight() + Point(1,1); int exactSize = std::max<int>(32, m_item->getExactSize()); if(exactSize == 0) return; float scaleFactor = std::min<float>(drawRect.width() / (float)exactSize, drawRect.height() / (float)exactSize); dest += (m_item->getDisplacement() - Point(32,32)) * scaleFactor; g_painter->setColor(m_color); m_item->draw(dest, scaleFactor, true); if(m_font && (m_item->isStackable() || m_item->isChargeable()) && m_item->getCountOrSubType() > 1) { std::string count = stdext::to_string(m_item->getCountOrSubType()); g_painter->setColor(Color(231, 231, 231)); m_font->drawText(count, Rect(m_rect.topLeft(), m_rect.bottomRight() - Point(3, 0)), Fw::AlignBottomRight); } if(m_showId) m_font->drawText(stdext::to_string(m_item->getServerId()), m_rect, Fw::AlignBottomRight); } drawBorder(m_rect); drawIcon(m_rect); drawText(m_rect); }
bool Input::draw(SDL_Renderer * renderer) { //static clock_t clock = std::clock(); Label::draw(renderer); SDL_Rect rcText = { 0, 0, 0, 0 }; if (!_text.empty()) { memcpy(&rcText, &getTextRect(), sizeof(rcText)); } SDL_Rect rcPading; getPaddingRect(rcPading); SDL_Rect dstText = { rcPading.x, rcPading.y, rcText.w, rcText.h }; if (_bGrating) { int x = rcPading.x + dstText.w + 1; // +1 for grating SDL_SetRenderDrawColor(renderer, 0xcc, 0xcc, 0xcc, 0xff); SDL_RenderDrawLine(renderer, x, rcPading.y, x, rcPading.y + rcPading.h); } return true; }
Rect UIWidget::getChildrenRect() { Rect childrenRect; for(const UIWidgetPtr& child : m_children) { if(!child->isExplicitlyVisible() || !child->getRect().isValid()) continue; Rect marginRect = child->getMarginRect(); if(!childrenRect.isValid()) childrenRect = marginRect; else childrenRect = childrenRect.united(marginRect); } Rect myClippingRect = getPaddingRect(); if(!childrenRect.isValid()) childrenRect = myClippingRect; else { if(childrenRect.width() < myClippingRect.width()) childrenRect.setWidth(myClippingRect.width()); if(childrenRect.height() < myClippingRect.height()) childrenRect.setHeight(myClippingRect.height()); } return childrenRect; }
Point UIMinimap::getTilePoint(const Position& pos) { return g_minimap.getTilePoint(pos, getPaddingRect(), getCameraPosition(), m_scale); }
Position UIMinimap::getTilePosition(const Point& mousePos) { return g_minimap.getTilePosition(mousePos, getPaddingRect(), getCameraPosition(), m_scale); }
void UITextEdit::wrapText() { setText(m_font->wrapText(m_text, getPaddingRect().width() - m_textOffset.x)); }
void UITextEdit::update(bool focusCursor) { if(!m_updatesEnabled) return; std::string text = getDisplayedText(); m_drawText = text; int textLength = text.length(); // prevent glitches if(m_rect.isEmpty()) return; // map glyphs positions Size textBoxSize; const std::vector<Point>& glyphsPositions = m_font->calculateGlyphsPositions(text, m_textAlign, &textBoxSize); const Rect *glyphsTextureCoords = m_font->getGlyphsTextureCoords(); const Size *glyphsSize = m_font->getGlyphsSize(); int glyph; // update rect size if(!m_rect.isValid() || m_textHorizontalAutoResize || m_textVerticalAutoResize) { textBoxSize += Size(m_padding.left + m_padding.right, m_padding.top + m_padding.bottom) + m_textOffset.toSize(); Size size = getSize(); if(size.width() <= 0 || (m_textHorizontalAutoResize && !m_textWrap)) size.setWidth(textBoxSize.width()); if(size.height() <= 0 || m_textVerticalAutoResize) size.setHeight(textBoxSize.height()); setSize(size); } // resize just on demand if(textLength > (int)m_glyphsCoords.size()) { m_glyphsCoords.resize(textLength); m_glyphsTexCoords.resize(textLength); } Point oldTextAreaOffset = m_textVirtualOffset; if(textBoxSize.width() <= getPaddingRect().width()) m_textVirtualOffset.x = 0; if(textBoxSize.height() <= getPaddingRect().height()) m_textVirtualOffset.y = 0; // readjust start view area based on cursor position m_cursorInRange = false; if(focusCursor && m_autoScroll) { if(m_cursorPos > 0 && textLength > 0) { assert(m_cursorPos <= textLength); Rect virtualRect(m_textVirtualOffset, m_rect.size() - Size(m_padding.left+m_padding.right, 0)); // previous rendered virtual rect int pos = m_cursorPos - 1; // element before cursor glyph = (uchar)text[pos]; // glyph of the element before cursor Rect glyphRect(glyphsPositions[pos], glyphsSize[glyph]); // if the cursor is not on the previous rendered virtual rect we need to update it if(!virtualRect.contains(glyphRect.topLeft()) || !virtualRect.contains(glyphRect.bottomRight())) { // calculate where is the first glyph visible Point startGlyphPos; startGlyphPos.y = std::max(glyphRect.bottom() - virtualRect.height(), 0); startGlyphPos.x = std::max(glyphRect.right() - virtualRect.width(), 0); // find that glyph for(pos = 0; pos < textLength; ++pos) { glyph = (uchar)text[pos]; glyphRect = Rect(glyphsPositions[pos], glyphsSize[glyph]); glyphRect.setTop(std::max(glyphRect.top() - m_font->getYOffset() - m_font->getGlyphSpacing().height(), 0)); glyphRect.setLeft(std::max(glyphRect.left() - m_font->getGlyphSpacing().width(), 0)); // first glyph entirely visible found if(glyphRect.topLeft() >= startGlyphPos) { m_textVirtualOffset.x = glyphsPositions[pos].x; m_textVirtualOffset.y = glyphsPositions[pos].y - m_font->getYOffset(); break; } } } } else { m_textVirtualOffset = Point(0,0); } m_cursorInRange = true; } else { if(m_cursorPos > 0 && textLength > 0) { Rect virtualRect(m_textVirtualOffset, m_rect.size() - Size(2*m_padding.left+m_padding.right, 0) ); // previous rendered virtual rect int pos = m_cursorPos - 1; // element before cursor glyph = (uchar)text[pos]; // glyph of the element before cursor Rect glyphRect(glyphsPositions[pos], glyphsSize[glyph]); if(virtualRect.contains(glyphRect.topLeft()) && virtualRect.contains(glyphRect.bottomRight())) m_cursorInRange = true; } else { m_cursorInRange = true; } } bool fireAreaUpdate = false; if(oldTextAreaOffset != m_textVirtualOffset) fireAreaUpdate = true; Rect textScreenCoords = m_rect; textScreenCoords.expandLeft(-m_padding.left); textScreenCoords.expandRight(-m_padding.right); textScreenCoords.expandBottom(-m_padding.bottom); textScreenCoords.expandTop(-m_padding.top); m_drawArea = textScreenCoords; if(textScreenCoords.size() != m_textVirtualSize) { m_textVirtualSize = textScreenCoords.size(); fireAreaUpdate = true; } Size totalSize = textBoxSize; if(totalSize.width() < m_textVirtualSize.width()) totalSize.setWidth(m_textVirtualSize.height()); if(totalSize.height() < m_textVirtualSize.height()) totalSize.setHeight(m_textVirtualSize.height()); if(m_textTotalSize != totalSize) { m_textTotalSize = totalSize; fireAreaUpdate = true; } if(m_textAlign & Fw::AlignBottom) { m_drawArea.translate(0, textScreenCoords.height() - textBoxSize.height()); } else if(m_textAlign & Fw::AlignVerticalCenter) { m_drawArea.translate(0, (textScreenCoords.height() - textBoxSize.height()) / 2); } else { // AlignTop } if(m_textAlign & Fw::AlignRight) { m_drawArea.translate(textScreenCoords.width() - textBoxSize.width(), 0); } else if(m_textAlign & Fw::AlignHorizontalCenter) { m_drawArea.translate((textScreenCoords.width() - textBoxSize.width()) / 2, 0); } else { // AlignLeft } for(int i = 0; i < textLength; ++i) { glyph = (uchar)text[i]; m_glyphsCoords[i].clear(); // skip invalid glyphs if(glyph < 32 && glyph != (uchar)'\n') continue; // calculate initial glyph rect and texture coords Rect glyphScreenCoords(glyphsPositions[i], glyphsSize[glyph]); Rect glyphTextureCoords = glyphsTextureCoords[glyph]; // first translate to align position if(m_textAlign & Fw::AlignBottom) { glyphScreenCoords.translate(0, textScreenCoords.height() - textBoxSize.height()); } else if(m_textAlign & Fw::AlignVerticalCenter) { glyphScreenCoords.translate(0, (textScreenCoords.height() - textBoxSize.height()) / 2); } else { // AlignTop // nothing to do } if(m_textAlign & Fw::AlignRight) { glyphScreenCoords.translate(textScreenCoords.width() - textBoxSize.width(), 0); } else if(m_textAlign & Fw::AlignHorizontalCenter) { glyphScreenCoords.translate((textScreenCoords.width() - textBoxSize.width()) / 2, 0); } else { // AlignLeft // nothing to do } // only render glyphs that are after startRenderPosition if(glyphScreenCoords.bottom() < m_textVirtualOffset.y || glyphScreenCoords.right() < m_textVirtualOffset.x) continue; // bound glyph topLeft to startRenderPosition if(glyphScreenCoords.top() < m_textVirtualOffset.y) { glyphTextureCoords.setTop(glyphTextureCoords.top() + (m_textVirtualOffset.y - glyphScreenCoords.top())); glyphScreenCoords.setTop(m_textVirtualOffset.y); } if(glyphScreenCoords.left() < m_textVirtualOffset.x) { glyphTextureCoords.setLeft(glyphTextureCoords.left() + (m_textVirtualOffset.x - glyphScreenCoords.left())); glyphScreenCoords.setLeft(m_textVirtualOffset.x); } // subtract startInternalPos glyphScreenCoords.translate(-m_textVirtualOffset); // translate rect to screen coords glyphScreenCoords.translate(textScreenCoords.topLeft()); // only render if glyph rect is visible on screenCoords if(!textScreenCoords.intersects(glyphScreenCoords)) continue; // bound glyph bottomRight to screenCoords bottomRight if(glyphScreenCoords.bottom() > textScreenCoords.bottom()) { glyphTextureCoords.setBottom(glyphTextureCoords.bottom() + (textScreenCoords.bottom() - glyphScreenCoords.bottom())); glyphScreenCoords.setBottom(textScreenCoords.bottom()); } if(glyphScreenCoords.right() > textScreenCoords.right()) { glyphTextureCoords.setRight(glyphTextureCoords.right() + (textScreenCoords.right() - glyphScreenCoords.right())); glyphScreenCoords.setRight(textScreenCoords.right()); } // render glyph m_glyphsCoords[i] = glyphScreenCoords; m_glyphsTexCoords[i] = glyphTextureCoords; } if(fireAreaUpdate) onTextAreaUpdate(m_textVirtualOffset, m_textVirtualSize, m_textTotalSize); g_app.repaint(); }