Skip to content

Commit

Permalink
feat: speed up animations, dont render stuff if invisible, black back…
Browse files Browse the repository at this point in the history
…ground
  • Loading branch information
f0e committed Nov 23, 2024
1 parent a1f0c62 commit 0238994
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
10 changes: 6 additions & 4 deletions src/gui/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,12 @@ void gui::redraw_window(os::Window* window) {

gfx::Rect drop_zone = rc;

static float fill_shade = 10.f;
fill_shade = std::lerp(fill_shade, windowData.dragging ? 30.f : 10.f, 25.f * delta_time);
paint.color(gfx::rgba(255, 255, 255, fill_shade));
s->drawRect(drop_zone, paint);
static float fill_shade = 0.f;
fill_shade = std::lerp(fill_shade, windowData.dragging ? 30.f : 0.f, 25.f * delta_time);
if ((int)fill_shade > 0) {
paint.color(gfx::rgba(255, 255, 255, fill_shade));
s->drawRect(drop_zone, paint);
}

// paint.style(os::Paint::Style::Stroke);
// const int stroke_shade = 100;
Expand Down
30 changes: 23 additions & 7 deletions src/gui/ui/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,62 @@ const int gap = 10;

void ui::render_bar(os::Surface* surface, const Element* element, float anim) {
auto& bar_data = std::get<BarElementData>(element->data);
render::rect_filled(surface, element->rect, gfx::seta(bar_data.background_color, anim * 255));

int alpha = anim * 255;
if (alpha == 0)
return;

render::rect_filled(surface, element->rect, gfx::seta(bar_data.background_color, alpha));

if (bar_data.percent_fill > 0) {
gfx::Rect fill_rect = element->rect;
fill_rect.w = static_cast<int>(element->rect.w * bar_data.percent_fill);
render::rect_filled(surface, fill_rect, gfx::seta(bar_data.fill_color, anim * 255));
render::rect_filled(surface, fill_rect, gfx::seta(bar_data.fill_color, alpha));
}
}

void ui::render_text(os::Surface* surface, const Element* element, float anim) {
auto& text_data = std::get<TextElementData>(element->data);

int alpha = anim * 255;
if (alpha == 0)
return;

gfx::Point text_pos = element->rect.origin();
text_pos.y += text_data.font.getSize();
render::text(surface, text_pos, gfx::seta(text_data.color, anim * 255), text_data.text, text_data.font, text_data.align);

render::text(surface, text_pos, gfx::seta(text_data.color, alpha), text_data.text, text_data.font, text_data.align);
}

void ui::render_image(os::Surface* surface, const Element* element, float anim) {
auto& image_data = std::get<ImageElementData>(element->data);

int alpha = anim * 255;
int stroke_alpha = anim * 125;
if (alpha == 0 && stroke_alpha == 0)
return;

gfx::Rect image_rect = element->rect;
image_rect.shrink(3);

os::Paint paint;
paint.color(gfx::rgba(255, 255, 255, anim * 255));
paint.color(gfx::rgba(255, 255, 255, alpha));
surface->drawSurface(image_data.image_surface.get(), image_data.image_surface->bounds(), image_rect, os::Sampling(), &paint);

os::Paint stroke_paint;
stroke_paint.style(os::Paint::Style::Stroke);
stroke_paint.strokeWidth(1);

image_rect.enlarge(1);
stroke_paint.color(gfx::rgba(155, 155, 155, 125 * anim));
stroke_paint.color(gfx::rgba(155, 155, 155, stroke_alpha));
surface->drawRect(image_rect, stroke_paint);

image_rect.enlarge(1);
stroke_paint.color(gfx::rgba(80, 80, 80, 125 * anim));
stroke_paint.color(gfx::rgba(80, 80, 80, stroke_alpha));
surface->drawRect(image_rect, stroke_paint);

image_rect.enlarge(1);
stroke_paint.color(gfx::rgba(155, 155, 155, 125 * anim));
stroke_paint.color(gfx::rgba(155, 155, 155, stroke_alpha));
surface->drawRect(image_rect, stroke_paint);
}

Expand Down
2 changes: 1 addition & 1 deletion src/gui/ui/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ namespace ui {

struct AnimatedElement {
std::shared_ptr<Element> element;
AnimationState animation = AnimationState(15.f);
AnimationState animation = AnimationState(25.f);
};

struct Container {
Expand Down

0 comments on commit 0238994

Please sign in to comment.