ppsspp/Common/UI/Context.cpp

286 lines
9.0 KiB
C++
Raw Normal View History

2017-02-26 14:40:53 +00:00
#include "ppsspp_config.h"
#include <algorithm>
2020-10-04 08:10:55 +00:00
#include "Common/System/Display.h"
#include "Common/System/System.h"
#include "Common/UI/UI.h"
#include "Common/UI/View.h"
#include "Common/UI/Context.h"
#include "Common/Render/DrawBuffer.h"
2020-10-04 22:05:28 +00:00
#include "Common/Render/Text/draw_text.h"
#include "Common/Log.h"
2020-08-18 08:07:51 +00:00
#include "UI/TextureUtil.h"
UIContext::UIContext() {
fontStyle_ = new UI::FontStyle();
bounds_ = Bounds(0, 0, dp_xres, dp_yres);
}
UIContext::~UIContext() {
sampler_->Release();
delete fontStyle_;
delete textDrawer_;
}
void UIContext::Init(Draw::DrawContext *thin3d, Draw::Pipeline *uipipe, Draw::Pipeline *uipipenotex, DrawBuffer *uidrawbuffer, DrawBuffer *uidrawbufferTop) {
2016-12-25 17:18:19 +00:00
using namespace Draw;
draw_ = thin3d;
sampler_ = draw_->CreateSamplerState({ TextureFilter::LINEAR, TextureFilter::LINEAR, TextureFilter::LINEAR });
ui_pipeline_ = uipipe;
ui_pipeline_notex_ = uipipenotex;
uidrawbuffer_ = uidrawbuffer;
uidrawbufferTop_ = uidrawbufferTop;
textDrawer_ = TextDrawer::Create(thin3d); // May return nullptr if no implementation is available for this platform.
}
void UIContext::BeginFrame() {
if (!uitexture_) {
uitexture_ = CreateTextureFromFile(draw_, "ui_atlas.zim", ImageFileType::ZIM, false);
_dbg_assert_msg_(uitexture_, "Failed to load ui_atlas.zim.\n\nPlace it in the directory \"assets\" under your PPSSPP directory.");
}
2018-12-18 22:56:36 +00:00
uidrawbufferTop_->SetCurZ(0.0f);
uidrawbuffer_->SetCurZ(0.0f);
ActivateTopScissor();
}
void UIContext::Begin() {
BeginPipeline(ui_pipeline_, sampler_);
}
void UIContext::BeginNoTex() {
draw_->BindSamplerStates(0, 1, &sampler_);
UIBegin(ui_pipeline_notex_);
}
void UIContext::BeginPipeline(Draw::Pipeline *pipeline, Draw::SamplerState *samplerState) {
_assert_(pipeline != nullptr);
draw_->BindSamplerStates(0, 1, &samplerState);
RebindTexture();
UIBegin(pipeline);
}
void UIContext::RebindTexture() const {
if (uitexture_)
draw_->BindTexture(0, uitexture_->GetTexture());
}
void UIContext::Flush() {
if (uidrawbuffer_) {
2013-03-30 18:23:02 +00:00
uidrawbuffer_->Flush();
}
if (uidrawbufferTop_) {
2013-03-30 18:23:02 +00:00
uidrawbufferTop_->Flush();
}
}
void UIContext::SetCurZ(float curZ) {
ui_draw2d.SetCurZ(curZ);
ui_draw2d_front.SetCurZ(curZ);
}
// TODO: Support transformed bounds using stencil instead.
2013-05-27 22:32:00 +00:00
void UIContext::PushScissor(const Bounds &bounds) {
Flush();
Bounds clipped = TransformBounds(bounds);
if (scissorStack_.size())
clipped.Clip(scissorStack_.back());
2017-12-10 22:20:32 +00:00
else
clipped.Clip(bounds_);
scissorStack_.push_back(clipped);
2013-05-27 22:32:00 +00:00
ActivateTopScissor();
}
void UIContext::PopScissor() {
Flush();
scissorStack_.pop_back();
ActivateTopScissor();
}
Bounds UIContext::GetScissorBounds() {
if (!scissorStack_.empty())
return scissorStack_.back();
else
return bounds_;
}
Bounds UIContext::GetLayoutBounds() const {
Bounds bounds = GetBounds();
float left = System_GetPropertyFloat(SYSPROP_DISPLAY_SAFE_INSET_LEFT);
float right = System_GetPropertyFloat(SYSPROP_DISPLAY_SAFE_INSET_RIGHT);
float top = System_GetPropertyFloat(SYSPROP_DISPLAY_SAFE_INSET_TOP);
float bottom = System_GetPropertyFloat(SYSPROP_DISPLAY_SAFE_INSET_BOTTOM);
// ILOG("Insets: %f %f %f %f", left, right, top, bottom);
// Adjust left edge to compensate for cutouts (notches) if any.
bounds.x += left;
bounds.w -= (left + right);
bounds.y += top;
bounds.h -= (top + bottom);
return bounds;
}
2013-05-27 22:32:00 +00:00
void UIContext::ActivateTopScissor() {
Bounds bounds;
2013-05-27 22:32:00 +00:00
if (scissorStack_.size()) {
2017-08-07 11:18:19 +00:00
float scale_x = pixel_in_dps_x;
float scale_y = pixel_in_dps_y;
bounds = scissorStack_.back();
2017-08-07 11:18:19 +00:00
int x = floorf(scale_x * bounds.x);
int y = floorf(scale_y * bounds.y);
int w = std::max(0.0f, ceilf(scale_x * bounds.w));
int h = std::max(0.0f, ceilf(scale_y * bounds.h));
draw_->SetScissorRect(x, y, w, h);
} else {
// Avoid rounding errors
draw_->SetScissorRect(0, 0, pixel_xres, pixel_yres);
2013-05-27 22:32:00 +00:00
}
}
void UIContext::SetFontScale(float scaleX, float scaleY) {
fontScaleX_ = scaleX;
fontScaleY_ = scaleY;
}
void UIContext::SetFontStyle(const UI::FontStyle &fontStyle) {
*fontStyle_ = fontStyle;
if (textDrawer_) {
textDrawer_->SetFontScale(fontScaleX_, fontScaleY_);
2016-03-17 18:42:59 +00:00
textDrawer_->SetFont(fontStyle.fontName.c_str(), fontStyle.sizePts, fontStyle.flags);
}
}
2016-08-07 23:49:50 +00:00
void UIContext::MeasureText(const UI::FontStyle &style, float scaleX, float scaleY, const char *str, float *x, float *y, int align) const {
MeasureTextCount(style, scaleX, scaleY, str, (int)strlen(str), x, y, align);
}
2016-08-07 23:49:50 +00:00
void UIContext::MeasureTextCount(const UI::FontStyle &style, float scaleX, float scaleY, const char *str, int count, float *x, float *y, int align) const {
if (!textDrawer_ || (align & FLAG_DYNAMIC_ASCII)) {
float sizeFactor = (float)style.sizePts / 24.0f;
2016-08-07 23:49:50 +00:00
Draw()->SetFontScale(scaleX * sizeFactor, scaleY * sizeFactor);
Draw()->MeasureTextCount(style.atlasFont, str, count, x, y);
} else {
textDrawer_->SetFont(style.fontName.c_str(), style.sizePts, style.flags);
2016-08-07 23:49:50 +00:00
textDrawer_->SetFontScale(scaleX, scaleY);
textDrawer_->MeasureString(str, count, x, y);
textDrawer_->SetFont(fontStyle_->fontName.c_str(), fontStyle_->sizePts, fontStyle_->flags);
}
}
2016-08-07 23:49:50 +00:00
void UIContext::MeasureTextRect(const UI::FontStyle &style, float scaleX, float scaleY, const char *str, int count, const Bounds &bounds, float *x, float *y, int align) const {
if (!textDrawer_ || (align & FLAG_DYNAMIC_ASCII)) {
float sizeFactor = (float)style.sizePts / 24.0f;
2016-08-07 23:49:50 +00:00
Draw()->SetFontScale(scaleX * sizeFactor, scaleY * sizeFactor);
Draw()->MeasureTextRect(style.atlasFont, str, count, bounds, x, y, align);
} else {
textDrawer_->SetFont(style.fontName.c_str(), style.sizePts, style.flags);
2016-08-07 23:49:50 +00:00
textDrawer_->SetFontScale(scaleX, scaleY);
textDrawer_->MeasureStringRect(str, count, bounds, x, y, align);
textDrawer_->SetFont(fontStyle_->fontName.c_str(), fontStyle_->sizePts, fontStyle_->flags);
}
}
void UIContext::DrawText(const char *str, float x, float y, uint32_t color, int align) {
if (!textDrawer_ || (align & FLAG_DYNAMIC_ASCII)) {
float sizeFactor = (float)fontStyle_->sizePts / 24.0f;
Draw()->SetFontScale(fontScaleX_ * sizeFactor, fontScaleY_ * sizeFactor);
Draw()->DrawText(fontStyle_->atlasFont, str, x, y, color, align);
} else {
textDrawer_->SetFontScale(fontScaleX_, fontScaleY_);
textDrawer_->DrawString(*Draw(), str, x, y, color, align);
RebindTexture();
}
}
void UIContext::DrawTextShadow(const char *str, float x, float y, uint32_t color, int align) {
uint32_t alpha = (color >> 1) & 0xFF000000;
DrawText(str, x + 2, y + 2, alpha, align);
DrawText(str, x, y, color, align);
}
void UIContext::DrawTextRect(const char *str, const Bounds &bounds, uint32_t color, int align) {
if (!textDrawer_ || (align & FLAG_DYNAMIC_ASCII)) {
float sizeFactor = (float)fontStyle_->sizePts / 24.0f;
Draw()->SetFontScale(fontScaleX_ * sizeFactor, fontScaleY_ * sizeFactor);
Draw()->DrawTextRect(fontStyle_->atlasFont, str, bounds.x, bounds.y, bounds.w, bounds.h, color, align);
} else {
textDrawer_->SetFontScale(fontScaleX_, fontScaleY_);
Bounds rounded = bounds;
rounded.x = floorf(rounded.x);
rounded.y = floorf(rounded.y);
textDrawer_->DrawStringRect(*Draw(), str, rounded, color, align);
RebindTexture();
}
}
void UIContext::FillRect(const UI::Drawable &drawable, const Bounds &bounds) {
2013-11-26 12:56:56 +00:00
// Only draw if alpha is non-zero.
if ((drawable.color & 0xFF000000) == 0)
return;
switch (drawable.type) {
case UI::DRAW_SOLID_COLOR:
uidrawbuffer_->DrawImageCenterTexel(theme->whiteImage, bounds.x, bounds.y, bounds.x2(), bounds.y2(), drawable.color);
break;
case UI::DRAW_4GRID:
uidrawbuffer_->DrawImage4Grid(drawable.image, bounds.x, bounds.y, bounds.x2(), bounds.y2(), drawable.color);
break;
case UI::DRAW_STRETCH_IMAGE:
uidrawbuffer_->DrawImageStretch(drawable.image, bounds.x, bounds.y, bounds.x2(), bounds.y2(), drawable.color);
break;
2013-10-19 21:29:05 +00:00
case UI::DRAW_NOTHING:
break;
}
2013-10-19 21:29:05 +00:00
}
2021-01-17 12:44:57 +00:00
void UIContext::DrawImageVGradient(ImageID image, uint32_t color1, uint32_t color2, const Bounds &bounds) {
uidrawbuffer_->DrawImageStretchVGradient(image, bounds.x, bounds.y, bounds.x2(), bounds.y2(), color1, color2);
}
void UIContext::PushTransform(const UITransform &transform) {
Flush();
using namespace Lin;
Matrix4x4 m = Draw()->GetDrawMatrix();
const Vec3 &t = transform.translate;
Vec3 scaledTranslate = Vec3(
t.x * m.xx + t.y * m.xy + t.z * m.xz + m.xw,
t.x * m.yx + t.y * m.yy + t.z * m.yz + m.yw,
t.x * m.zx + t.y * m.zy + t.z * m.zz + m.zw);
m.translateAndScale(scaledTranslate, transform.scale);
Draw()->PushDrawMatrix(m);
Draw()->PushAlpha(transform.alpha);
transformStack_.push_back(transform);
}
void UIContext::PopTransform() {
Flush();
transformStack_.pop_back();
Draw()->PopDrawMatrix();
Draw()->PopAlpha();
}
Bounds UIContext::TransformBounds(const Bounds &bounds) {
if (!transformStack_.empty()) {
const UITransform t = transformStack_.back();
Bounds translated = bounds.Offset(t.translate.x, t.translate.y);
// Scale around the center as the origin.
float scaledX = (translated.x - dp_xres * 0.5f) * t.scale.x + dp_xres * 0.5f;
float scaledY = (translated.y - dp_yres * 0.5f) * t.scale.y + dp_yres * 0.5f;
return Bounds(scaledX, scaledY, translated.w * t.scale.x, translated.h * t.scale.y);
}
return bounds;
}