Skip to content

Commit

Permalink
refactor: split out elements into separate files, remove unused has_c…
Browse files Browse the repository at this point in the history
…ontent_changed function
  • Loading branch information
f0e committed Nov 24, 2024
1 parent 335510b commit 48fe7b0
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 278 deletions.
51 changes: 51 additions & 0 deletions src/gui/ui/elements/bar.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "../ui.h"
#include "../render.h"
#include "../utils.h"

void ui::render_bar(os::Surface* surface, const Element* element, float anim) {
const int text_gap = 7;

auto& bar_data = std::get<BarElementData>(element->data);

gfx::Color adjusted_background_color = utils::adjust_color(bar_data.background_color, anim);
gfx::Color adjusted_fill_color = utils::adjust_color(bar_data.fill_color, anim);

gfx::Size text_size(0, 0);

if (bar_data.bar_text && bar_data.text_color && bar_data.font) {
gfx::Color adjusted_text_color = utils::adjust_color(*bar_data.text_color, anim);

gfx::Point text_pos = element->rect.origin();

text_size = render::get_text_size(*bar_data.bar_text, **bar_data.font);

text_pos.x = element->rect.x2();
text_pos.y += (*bar_data.font)->getSize() / 2;

render::text(surface, text_pos, adjusted_text_color, *bar_data.bar_text, **bar_data.font, os::TextAlign::Right);
}

gfx::Rect bar_rect = element->rect;
bar_rect.w -= text_size.w + text_gap;

render::rounded_rect_filled(surface, bar_rect, adjusted_background_color, 1000.f);

if (bar_data.percent_fill > 0) {
gfx::Rect fill_rect = bar_rect;
fill_rect.w = static_cast<int>(bar_rect.w * bar_data.percent_fill);
render::rounded_rect_filled(surface, fill_rect, adjusted_fill_color, 1000.f);
}
}

std::shared_ptr<ui::Element> ui::add_bar(const std::string& id, Container& container, float percent_fill, gfx::Color background_color, gfx::Color fill_color, int bar_width, std::optional<std::string> bar_text, std::optional<gfx::Color> text_color, std::optional<const SkFont*> font) {
auto element = std::make_shared<Element>(Element{
ElementType::BAR,
gfx::Rect(container.current_position, gfx::Size(bar_width, 6)),
BarElementData{ percent_fill, background_color, fill_color, bar_text, text_color, font },
render_bar,
});

add_element(container, id, element, container.line_height);

return element;
}
57 changes: 57 additions & 0 deletions src/gui/ui/elements/button.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "../ui.h"
#include "../render.h"
#include "../utils.h"
#include "../keys.h"
#include "../../gui.h"

void ui::render_button(os::Surface* surface, const Element* element, float anim) {
const float button_rounding = 7.8f;

auto& button_data = std::get<ButtonElementData>(element->data);

bool hovered = element->rect.contains(keys::mouse_pos);
if (hovered) {
gui::set_cursor(os::NativeCursor::Link);

if (button_data.on_press) {
if (keys::is_mouse_down()) {
(*button_data.on_press)();
keys::on_mouse_press_handled(os::Event::MouseButton::LeftButton);
}
}
}

gfx::Color adjusted_color = utils::adjust_color(gfx::rgba(255, 255, 255, hovered ? 75 : 20), anim); // todo: lerp hover shade
gfx::Color adjusted_text_color = utils::adjust_color(gfx::rgba(255, 255, 255, 255), anim);

gfx::Point text_pos = element->rect.center();

text_pos.y += button_data.font.getSize() / 2 - 1;
gfx::Rect cur_rect = element->rect;

// border
cur_rect.shrink(1);
render::rounded_rect_stroke(surface, cur_rect, utils::adjust_color(gfx::rgba(100, 100, 100, 255), anim), button_rounding);

// fill
render::rounded_rect_filled(surface, cur_rect, adjusted_color, button_rounding);

render::text(surface, text_pos, adjusted_text_color, button_data.text, button_data.font, os::TextAlign::Center);
}

std::shared_ptr<ui::Element> ui::add_button(const std::string& id, Container& container, const std::string& text, const SkFont& font, std::optional<std::function<void()>> on_press) {
const gfx::Size button_padding(40, 20);

gfx::Size text_size = render::get_text_size(text, font);

auto element = std::make_shared<Element>(Element{
ElementType::BUTTON,
gfx::Rect(container.current_position, text_size + button_padding),
ButtonElementData{ text, font, on_press },
render_button,
});

add_element(container, id, element, container.line_height);

return element;
}
101 changes: 101 additions & 0 deletions src/gui/ui/elements/image.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include "../ui.h"

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;

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

os::Paint paint;
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, stroke_alpha));
surface->drawRect(image_rect, stroke_paint);

image_rect.enlarge(1);
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, stroke_alpha));
surface->drawRect(image_rect, stroke_paint);
}

std::optional<std::shared_ptr<ui::Element>> ui::add_image(const std::string& id, Container& container, std::string image_path, gfx::Size max_size, std::string image_id) {
os::SurfaceRef image_surface;
os::SurfaceRef last_image_surface;

// get existing image
if (container.elements.contains(id)) {
std::shared_ptr<Element> cached_element = container.elements[id].element;
auto& image_data = std::get<ImageElementData>(cached_element->data);
if (image_data.image_id == image_id) { // edge cases this might not work, it's using current_frame, maybe image gets written after ffmpeg reports progress? idk. good enough for now
image_surface = image_data.image_surface;
}
else {
last_image_surface = image_data.image_surface;
}
}

// load image if new
if (!image_surface) {
image_surface = os::instance()->loadRgbaSurface(image_path.c_str());

if (!image_surface) {
printf("%s failed to load image (id: %s)\n", id.c_str(), image_id.c_str());
if (last_image_surface) {
// use last image as fallback todo: this is a bit hacky make it better
image_surface = last_image_surface;
}
else {
return {};
}
}

printf("%s loaded image (id: %s)\n", id.c_str(), image_id.c_str());
}

gfx::Rect image_rect(container.current_position, max_size);

float aspect_ratio = image_surface->width() / static_cast<float>(image_surface->height());

float target_width = image_rect.h * aspect_ratio;
float target_height = image_rect.w / aspect_ratio;

if (target_width <= image_rect.w) {
image_rect.w = static_cast<int>(target_width);
}
else {
image_rect.h = static_cast<int>(target_height);
}

if (image_rect.h > max_size.h) {
image_rect.h = max_size.h;
image_rect.w = static_cast<int>(max_size.h * aspect_ratio);
}

if (image_rect.w > max_size.w) {
image_rect.w = max_size.w;
image_rect.h = static_cast<int>(max_size.w / aspect_ratio);
}

auto element = std::make_shared<Element>(Element{
ElementType::IMAGE,
image_rect,
ImageElementData{ image_path, image_surface, image_id },
render_image,
});

add_element(container, id, element, container.line_height);

return element;
}
41 changes: 41 additions & 0 deletions src/gui/ui/elements/text.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "../ui.h"
#include "../render.h"
#include "../utils.h"

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

gfx::Color adjusted_color = utils::adjust_color(text_data.color, anim);

gfx::Point text_pos = element->rect.origin();
text_pos.y += text_data.font.getSize() - 1;

render::text(surface, text_pos, adjusted_color, text_data.text, text_data.font, text_data.align);
}

std::shared_ptr<ui::Element> ui::add_text(const std::string& id, Container& container, const std::string& text, gfx::Color color, const SkFont& font, os::TextAlign align, int margin_bottom) {
auto element = std::make_shared<Element>(Element{
ElementType::TEXT,
gfx::Rect(container.current_position, gfx::Size(0, font.getSize())),
TextElementData{ text, color, font, align },
render_text,
});

add_element(container, id, element, margin_bottom);

return element;
}

std::shared_ptr<ui::Element> ui::add_text_fixed(const std::string& id, Container& container, gfx::Point position, const std::string& text, gfx::Color color, const SkFont& font, os::TextAlign align, int margin_bottom) {
auto element = std::make_shared<Element>(Element{
ElementType::TEXT,
gfx::Rect(position, gfx::Size(0, font.getSize())),
TextElementData{ text, color, font, align },
render_text,
true,
});

add_element_fixed(container, id, element);

return element;
}
Loading

0 comments on commit 48fe7b0

Please sign in to comment.