Skip to content

Commit

Permalink
Support custom fade color/background
Browse files Browse the repository at this point in the history
  • Loading branch information
XorTroll committed Jun 17, 2024
1 parent ffef991 commit 3509023
Show file tree
Hide file tree
Showing 36 changed files with 832 additions and 574 deletions.
7 changes: 7 additions & 0 deletions Plutonium/include/pu/ui/ui_Application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ namespace pu::ui {
u8 fade_alpha_increment_steps;
SigmoidIncrementer<i32> fade_alpha_incr;
i32 fade_alpha;
sdl2::Texture fade_bg_tex;
Color fade_bg_clr;
Layout::Ref lyt;
Overlay::Ref ovl;
u64 ovl_timeout_ms;
Expand All @@ -48,6 +50,7 @@ namespace pu::ui {
public:
Application(render::Renderer::Ref renderer);
PU_SMART_CTOR(Application)
~Application();

inline void LoadLayout(Layout::Ref lyt) {
this->lyt = lyt;
Expand Down Expand Up @@ -103,8 +106,12 @@ namespace pu::ui {

bool CallForRender();
bool CallForRenderWithRenderOver(RenderOverFunction render_over_fn);

void FadeIn();
void FadeOut();
void SetFadeBackgroundImage(const std::string &path);
void SetFadeBackgroundColor(const Color clr);
void ResetFadeBackgroundImage();

inline bool IsFadedIn() {
return this->fade_alpha > 0;
Expand Down
5 changes: 3 additions & 2 deletions Plutonium/include/pu/ui/ui_Layout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace pu::ui {
std::vector<RenderCallback> render_cbs;

public:
Layout() : Container(0, 0, render::ScreenWidth, render::ScreenHeight), has_image(false), over_bg_color(DefaultBackgroundColor), sim_touch_pos(), over_bg_tex(), on_ipt(), render_cbs() {}
Layout() : Container(0, 0, render::ScreenWidth, render::ScreenHeight), has_image(false), over_bg_color(DefaultBackgroundColor), sim_touch_pos(), over_bg_tex(nullptr), on_ipt(), render_cbs() {}
PU_SMART_CTOR(Layout)
~Layout();

Expand All @@ -58,7 +58,7 @@ namespace pu::ui {
}

inline bool HasBackgroundImage() {
return this->has_image;
return this->over_bg_tex != nullptr;
}

inline sdl2::Texture GetBackgroundImageTexture() {
Expand All @@ -70,6 +70,7 @@ namespace pu::ui {
}

void SetBackgroundImage(const std::string &path);
void ResetBackgroundImage();
void SetBackgroundColor(const Color clr);

inline void SimulateTouchPosition(const TouchPoint sim_touch_pos) {
Expand Down
28 changes: 27 additions & 1 deletion Plutonium/source/pu/ui/ui_Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ namespace pu::ui {
this->fade_alpha = 0xFF;
this->fade_alpha_increment_steps = DefaultFadeAlphaIncrementSteps;
this->fade_alpha_incr = {};
this->fade_bg_tex = nullptr;
this->fade_bg_clr = { 0, 0, 0, 0xFF };
padConfigureInput(1, HidNpadStyleSet_NpadStandard);
padInitializeDefault(&this->input_pad);
}

Application::~Application() {
this->ResetFadeBackgroundImage();
}

void Application::Prepare() {
if(!this->loaded) {
this->OnLoad();
Expand Down Expand Up @@ -133,6 +139,20 @@ namespace pu::ui {
this->CallForRender();
}

void Application::SetFadeBackgroundImage(const std::string &path) {
this->ResetFadeBackgroundImage();
this->fade_bg_tex = render::LoadImage(path);
}

void Application::SetFadeBackgroundColor(const Color clr) {
this->ResetFadeBackgroundImage();
this->fade_bg_clr = clr;
}

void Application::ResetFadeBackgroundImage() {
render::DeleteTexture(this->fade_bg_tex);
}

void Application::OnRender() {
padUpdate(&this->input_pad);
const auto keys_down = this->GetButtonsDown();
Expand Down Expand Up @@ -204,7 +224,13 @@ namespace pu::ui {
}
}

this->renderer->RenderRectangleFill({ 0, 0, 0, static_cast<u8>(0xFF - this->fade_alpha) }, 0, 0, render::ScreenWidth, render::ScreenHeight);
const auto over_alpha = static_cast<u8>(0xFF - this->fade_alpha);
if(this->fade_bg_tex != nullptr) {
this->renderer->RenderTexture(this->fade_bg_tex, 0, 0, render::TextureRenderOptions::WithCustomAlpha(over_alpha));
}
else {
this->renderer->RenderRectangleFill(this->fade_bg_clr.WithAlpha(over_alpha), 0, 0, render::ScreenWidth, render::ScreenHeight);
}
}

void Application::Close() {
Expand Down
12 changes: 7 additions & 5 deletions Plutonium/source/pu/ui/ui_Layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@
namespace pu::ui {

Layout::~Layout() {
render::DeleteTexture(this->over_bg_tex);
this->ResetBackgroundImage();
}

void Layout::SetBackgroundImage(const std::string &path) {
render::DeleteTexture(this->over_bg_tex);
this->has_image = true;
this->ResetBackgroundImage();
this->over_bg_tex = render::LoadImage(path);
}

void Layout::SetBackgroundColor(const Color clr) {
render::DeleteTexture(this->over_bg_tex);
this->has_image = false;
this->ResetBackgroundImage();
this->over_bg_color = clr;
}

void Layout::ResetBackgroundImage() {
render::DeleteTexture(this->over_bg_tex);
}

TouchPoint Layout::ConsumeSimulatedTouchPosition() {
auto touch_pos_copy = this->sim_touch_pos;
this->sim_touch_pos = {};
Expand Down
Loading

0 comments on commit 3509023

Please sign in to comment.