TODO: Optimization: Use text and quad containers for rendering
| 270 | |
| 271 | // TODO: Optimization: Use text and quad containers for rendering |
| 272 | void CTouchControls::CTouchButton::Render(std::optional<bool> Selected, std::optional<CUnitRect> Rect) const |
| 273 | { |
| 274 | dbg_assert(m_pBehavior != nullptr, "Touch button behavior is nullptr"); |
| 275 | CUIRect ScreenRect; |
| 276 | if(Rect.has_value()) |
| 277 | ScreenRect = m_pTouchControls->CalculateScreenFromUnitRect(*Rect, m_Shape); |
| 278 | else |
| 279 | ScreenRect = m_ScreenRect; |
| 280 | |
| 281 | ColorRGBA ButtonColor; |
| 282 | // "Selected" can decide which color to use, while not disturbing the original color check. |
| 283 | ButtonColor = m_pBehavior->IsActive() || Selected.value_or(false) ? m_pTouchControls->m_BackgroundColorActive : m_pTouchControls->m_BackgroundColorInactive; |
| 284 | if(!Selected.value_or(true)) |
| 285 | ButtonColor = m_pTouchControls->m_BackgroundColorInactive; |
| 286 | switch(m_Shape) |
| 287 | { |
| 288 | case EButtonShape::RECT: |
| 289 | { |
| 290 | ScreenRect.Draw(ButtonColor, m_pTouchControls->m_EditingActive ? IGraphics::CORNER_NONE : m_BackgroundCorners, 10.0f); |
| 291 | break; |
| 292 | } |
| 293 | case EButtonShape::CIRCLE: |
| 294 | { |
| 295 | const vec2 Center = ScreenRect.Center(); |
| 296 | const float Radius = minimum(ScreenRect.w, ScreenRect.h) / 2.0f; |
| 297 | m_pTouchControls->Graphics()->TextureClear(); |
| 298 | m_pTouchControls->Graphics()->QuadsBegin(); |
| 299 | m_pTouchControls->Graphics()->SetColor(ButtonColor); |
| 300 | m_pTouchControls->Graphics()->DrawCircle(Center.x, Center.y, Radius, maximum(round_truncate(Radius / 4.0f) & ~1, 32)); |
| 301 | m_pTouchControls->Graphics()->QuadsEnd(); |
| 302 | break; |
| 303 | } |
| 304 | default: |
| 305 | dbg_assert_failed("Unhandled shape"); |
| 306 | } |
| 307 | |
| 308 | const float FontSize = 22.0f; |
| 309 | CButtonLabel LabelData = m_pBehavior->GetLabel(); |
| 310 | CUIRect LabelRect; |
| 311 | ScreenRect.Margin(10.0f, &LabelRect); |
| 312 | SLabelProperties LabelProps; |
| 313 | LabelProps.m_MaxWidth = LabelRect.w; |
| 314 | if(LabelData.m_Type == CButtonLabel::EType::ICON) |
| 315 | { |
| 316 | m_pTouchControls->TextRender()->SetFontPreset(EFontPreset::ICON_FONT); |
| 317 | m_pTouchControls->TextRender()->SetRenderFlags(ETextRenderFlags::TEXT_RENDER_FLAG_ONLY_ADVANCE_WIDTH | ETextRenderFlags::TEXT_RENDER_FLAG_NO_X_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_Y_BEARING); |
| 318 | m_pTouchControls->Ui()->DoLabel(&LabelRect, LabelData.m_pLabel, FontSize, TEXTALIGN_MC, LabelProps); |
| 319 | m_pTouchControls->TextRender()->SetRenderFlags(0); |
| 320 | m_pTouchControls->TextRender()->SetFontPreset(EFontPreset::DEFAULT_FONT); |
| 321 | } |
| 322 | else |
| 323 | { |
| 324 | const char *pLabel = LabelData.m_Type == CButtonLabel::EType::LOCALIZED ? Localize(LabelData.m_pLabel) : LabelData.m_pLabel; |
| 325 | m_pTouchControls->Ui()->DoLabel(&LabelRect, pLabel, FontSize, TEXTALIGN_MC, LabelProps); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | void CTouchControls::CTouchButton::WriteToConfiguration(CJsonWriter *pWriter) |
no test coverage detected