ppsspp/Common/UI/Context.h

143 lines
4.1 KiB
C
Raw Permalink Normal View History

#pragma once
2020-08-18 08:07:51 +00:00
#include <memory>
2013-05-27 22:32:00 +00:00
#include <vector>
#include <cstdint>
2022-03-31 15:03:34 +00:00
#include <string>
2013-05-27 22:32:00 +00:00
#include "Common/Math/geom2d.h"
#include "Common/Math/lin/vec3.h"
#include "Common/Render/TextureAtlas.h"
2013-05-27 22:32:00 +00:00
// Everything you need to draw a UI collected into a single unit that can be passed around.
// Everything forward declared so this header is safe everywhere.
2016-12-25 17:18:19 +00:00
namespace Draw {
2016-12-25 20:01:57 +00:00
class DrawContext;
class Pipeline;
2016-12-25 19:54:37 +00:00
class DepthStencilState;
class Texture;
class BlendState;
2016-12-25 19:54:37 +00:00
class SamplerState;
class RasterState;
2016-12-25 17:18:19 +00:00
}
class DrawBuffer;
class TextDrawer;
2013-05-27 22:32:00 +00:00
namespace UI {
struct Drawable;
2020-08-18 08:07:51 +00:00
struct EventParams;
2013-05-27 22:32:00 +00:00
struct Theme;
struct FontStyle;
2020-08-18 08:07:51 +00:00
class Event;
class View;
2013-05-27 22:32:00 +00:00
}
class DrawBuffer;
struct UITransform {
// TODO: Or just use a matrix?
Lin::Vec3 translate;
Lin::Vec3 scale;
float alpha;
};
class UIContext {
public:
UIContext();
~UIContext();
void Init(Draw::DrawContext *thin3d, Draw::Pipeline *uipipe, Draw::Pipeline *uipipenotex, DrawBuffer *uidrawbuffer);
void BeginFrame();
void Begin();
2013-03-30 18:23:02 +00:00
void BeginNoTex();
void BeginPipeline(Draw::Pipeline *pipeline, Draw::SamplerState *samplerState);
void Flush();
2013-03-30 18:23:02 +00:00
void RebindTexture() const;
void BindFontTexture() const;
2023-12-14 10:10:36 +00:00
double FrameStartTime() const { return frameStartTime_; }
2013-05-27 22:32:00 +00:00
// TODO: Support transformed bounds using stencil
void PushScissor(const Bounds &bounds);
void PopScissor();
Bounds GetScissorBounds();
2013-05-27 22:32:00 +00:00
void ActivateTopScissor();
DrawBuffer *Draw() const { return uidrawbuffer_; }
// Utility methods
TextDrawer *Text() const { return textDrawer_; }
2022-12-27 23:18:35 +00:00
void SetTintSaturation(float tint, float sat);
// High level drawing functions. They generally assume the default texture to be bounds.
void SetFontStyle(const UI::FontStyle &style);
2013-11-26 13:36:22 +00:00
const UI::FontStyle &GetFontStyle() { return *fontStyle_; }
void SetFontScale(float scaleX, float scaleY);
2024-05-24 10:21:08 +00:00
void MeasureText(const UI::FontStyle &style, float scaleX, float scaleY, std::string_view str, float *x, float *y, int align = 0) const;
void MeasureTextRect(const UI::FontStyle &style, float scaleX, float scaleY, std::string_view str, const Bounds &bounds, float *x, float *y, int align = 0) const;
void DrawText(std::string_view str, float x, float y, uint32_t color, int align = 0);
void DrawTextShadow(std::string_view str, float x, float y, uint32_t color, int align = 0);
void DrawTextRect(std::string_view str, const Bounds &bounds, uint32_t color, int align = 0);
void DrawTextShadowRect(std::string_view str, const Bounds &bounds, uint32_t color, int align = 0);
2023-07-17 09:44:36 +00:00
// Will squeeze the text into the bounds if needed.
void DrawTextRectSqueeze(std::string_view str, const Bounds &bounds, uint32_t color, int align = 0);
2023-07-17 09:44:36 +00:00
2024-05-24 10:21:08 +00:00
float CalculateTextScale(std::string_view str, float availWidth, float availHeight) const;
2023-07-17 09:44:36 +00:00
void FillRect(const UI::Drawable &drawable, const Bounds &bounds);
void DrawRectDropShadow(const Bounds &bounds, float radius, float alpha, uint32_t color = 0);
2021-01-17 12:44:57 +00:00
void DrawImageVGradient(ImageID image, uint32_t color1, uint32_t color2, const Bounds &bounds);
// in dps, like dp_xres and dp_yres
void SetBounds(const Bounds &b) { bounds_ = b; }
const Bounds &GetBounds() const { return bounds_; }
Bounds GetLayoutBounds() const;
Draw::DrawContext *GetDrawContext() { return draw_; }
const UI::Theme &GetTheme() const {
return *theme;
}
void SetCurZ(float curZ);
void PushTransform(const UITransform &transform);
void PopTransform();
Bounds TransformBounds(const Bounds &bounds);
2022-03-31 15:03:34 +00:00
void setUIAtlas(const std::string &name);
// TODO: Move to private.
const UI::Theme *theme;
private:
Draw::DrawContext *draw_ = nullptr;
Bounds bounds_;
2023-12-14 10:10:36 +00:00
double frameStartTime_ = 0.0;
float fontScaleX_ = 1.0f;
float fontScaleY_ = 1.0f;
UI::FontStyle *fontStyle_ = nullptr;
TextDrawer *textDrawer_ = nullptr;
2014-08-17 10:17:59 +00:00
Draw::SamplerState *sampler_ = nullptr;
Draw::Pipeline *ui_pipeline_ = nullptr;
Draw::Pipeline *ui_pipeline_notex_ = nullptr;
Draw::Texture *uitexture_ = nullptr;
Draw::Texture *fontTexture_ = nullptr;
2014-08-17 10:17:59 +00:00
DrawBuffer *uidrawbuffer_ = nullptr;
2013-05-27 22:32:00 +00:00
std::vector<Bounds> scissorStack_;
std::vector<UITransform> transformStack_;
2022-03-31 15:03:34 +00:00
std::string lastUIAtlas_;
std::string UIAtlas_ = "ui_atlas.zim";
};