bettola/client/src/ui/taskbar.cpp

99 lines
3.4 KiB
C++

#include "taskbar.h"
#include <memory>
#include <chrono>
#include <ctime>
#include <iomanip>
#include <sstream>
#include "gfx/shape_renderer.h"
#include "gfx/txt_renderer.h"
#include "gfx/types.h"
#include "ui/ui_window.h"
Taskbar::Taskbar(int screen_width, int screen_height) {
_width = screen_width;
_height = 32;
_y_pos = 0; /* Taskbar at bottom because boring? */
_start_button_width = 60;
}
Taskbar::~Taskbar(void) {}
void Taskbar::render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer,
const std::vector<std::unique_ptr<UIWindow>>& windows,
UIWindow* focused_window) {
const Color taskbar_color = { 0.1f, 0.12f, 0.14f };
const Color button_color = { 0.2f, 0.22f, 0.24f };
const Color button_focused_color = { 0.3f, 0.32f, 0.34f };
const Color button_text_color = { 0.9f, 0.9f, 0.9f };
shape_renderer->draw_rect(0, _y_pos, _width, _height, taskbar_color);
/* Draw start button. */
shape_renderer->draw_rect(5, _y_pos + 5, _start_button_width - 10,
_height - 10, button_color);
txt_renderer->render_text("[B]", 20, _y_pos + 11, 1.0f,
button_text_color);
/* Draw app buttons. */
int button_width = 150;
int padding = 5;
int x_offset = _start_button_width + padding;
for(const auto& window : windows) {
bool is_focused = (window.get() == focused_window);
shape_renderer->draw_rect(x_offset, _y_pos + padding, button_width,
_height - (padding * 2),
is_focused ? button_focused_color : button_color);
/* TODO: Truncate text when too long. */
txt_renderer->render_text(window->get_title().c_str(), x_offset + 10,
_y_pos + 11, 1.0f, button_text_color);
x_offset += button_width + padding;
}
/* Draw clock. */
auto now = std::chrono::system_clock::now();
auto in_time_t = std::chrono::system_clock::to_time_t(now);
std::stringstream ss;
ss << std::put_time(std::localtime(&in_time_t), "%H:%M");
std::string time_str = ss.str();
txt_renderer->render_text(time_str.c_str(), _width-50, _y_pos+11, 1.0f,
button_text_color);
}
UIWindow* Taskbar::handle_event(SDL_Event* event, int screen_height,
const std::vector<std::unique_ptr<UIWindow>>& windows) {
if(event->type == SDL_EVENT_MOUSE_BUTTON_UP) {
int mouse_x = event->button.x;
int mouse_y = screen_height - event->button.y; /* Convert to UI coords. */
int button_width = 150;
int padding = 5;
int x_offset = _start_button_width + padding;
for(const auto& window : windows) {
if(mouse_x >= x_offset && mouse_x <= x_offset + button_width &&
mouse_y >= _y_pos && mouse_y <= _y_pos + _height) {
return window.get(); /* Return clicked window. */
}
x_offset += button_width + padding;
}
}
return nullptr; /* No window button was clicked. */
}
bool Taskbar::is_start_button_clicked(SDL_Event* event, int screen_height) {
if(event->type == SDL_EVENT_MOUSE_BUTTON_UP) {
int mouse_x = event->button.x;
int mouse_y = screen_height - event->button.y;
return (mouse_x >= 0 && mouse_x <= _start_button_width && mouse_y >= _y_pos &&
mouse_y <= _y_pos + _height);
}
return false;
}
int Taskbar::get_height(void) const {
return _height;
}