Files
lightgui/widgets/components/GtkVideo/GtkVideo.h

619 lines
9.8 KiB
C++

#pragma once
#include "core/xframe_core.h"
#include <cstdio>
//region GtkVideo
// A video surface: dark picture area, a big play glyph in the
// middle while paused, and a progress line along the bottom.
// Clicking toggles play/pause; step() advances playback.
class GtkVideo
{
private:
Display *display_;
int screen_;
Window window_;
GC gc_;
SStyle_Manager *styles_;
int x1_, y1_, x2_, y2_;
std::string name_;
std::string text_;
std::string style_name_; // "" = manager's active style
bool visible_ = true;
bool enabled_ = true;
bool focused_ = false;
std::string filename_;
bool playing_ = false;
double position_ = 0.25; // 0..1 through the stream
double duration_sec_ = 195.0;
bool autoplay_ = false;
// Getter/setter pass: colours formerly hardcoded in draw()/helpers.
Srgb picture_area_color_{16, 16, 20};
Srgb fill_color_{28, 34, 48};
Srgb text_color_{200, 200, 205};
Srgb big_play_color_{220, 220, 220};
Srgb pause_bars_color_{235, 235, 235};
Srgb progress_line_color_{70, 70, 76};
Srgb record_color_{230, 60, 60};
Srgb border_color_{120, 120, 130};
public:
std::function<void()> on_play;
std::function<void()> on_pause;
GtkVideo(Display* display, int screen, Window window, GC gc, SStyle_Manager* styles, int x, int y, int width, int height)
{
display_ = display;
screen_ = screen;
window_ = window;
gc_ = gc;
styles_ = styles;
x1_ = x;
y1_ = y;
x2_ = x + width;
y2_ = y + height;
name_ = "GtkVideo";
text_ = "video.mp4";
filename_ = "video.mp4";
}
~GtkVideo() {}
void set_bounds(int x, int y, int width, int height)
{
x1_ = x;
y1_ = y;
x2_ = x + width;
y2_ = y + height;
}
int width()
{
return x2_ - x1_;
}
int height()
{
return y2_ - y1_;
}
bool contains(int x, int y)
{
return visible_ && x >= x1_ && x <= x2_ && y >= y1_ && y <= y2_;
}
void set_visible(bool v)
{
visible_ = v;
}
bool get_visible()
{
return visible_;
}
void set_enabled(bool e)
{
enabled_ = e;
}
bool get_enabled()
{
return enabled_;
}
void set_style_name(std::string s)
{
style_name_ = s;
}
void set_text(std::string t)
{
text_ = t;
}
std::string get_text()
{
return text_;
}
SStyle style()
{
return styles_->resolve(style_name_);
}
//region GtkVideo API
void set_filename(std::string f)
{
filename_ = f;
text_ = f;
position_ = 0.0;
playing_ = autoplay_;
}
std::string get_filename()
{
return filename_;
}
void play()
{
if (!playing_)
{
playing_ = true;
if (on_play)
{
on_play();
}
}
}
void pause()
{
if (playing_)
{
playing_ = false;
if (on_pause)
{
on_pause();
}
}
}
bool get_playing()
{
return playing_;
}
void set_autoplay(bool a)
{
autoplay_ = a;
}
bool get_autoplay()
{
return autoplay_;
}
void set_position(double p)
{
position_ = std::max(0.0, std::min(1.0, p));
}
double get_position()
{
return position_;
}
void set_duration(double sec)
{
duration_sec_ = std::max(1.0, sec);
}
double get_duration()
{
return duration_sec_;
}
double get_time_sec()
{
return position_ * duration_sec_;
}
// Advance playback; call from a timer. Returns false at the end.
bool step(double dt_sec)
{
if (!playing_)
{
return true;
}
position_ += dt_sec / duration_sec_;
if (position_ >= 1.0)
{
position_ = 1.0;
playing_ = false;
return false;
}
return true;
}
//endregion
//region GtkVideo colours (getter/setter pass: formerly hardcoded in draw()/helpers)
void set_picture_area_color(Srgb c) { picture_area_color_ = c; }
Srgb get_picture_area_color() { return picture_area_color_; }
void set_picture_area_color_rgb(int r, int g, int b) { picture_area_color_ = Srgb{r, g, b}; }
void set_fill_color(Srgb c) { fill_color_ = c; }
Srgb get_fill_color() { return fill_color_; }
void set_fill_color_rgb(int r, int g, int b) { fill_color_ = Srgb{r, g, b}; }
void set_text_color(Srgb c) { text_color_ = c; }
Srgb get_text_color() { return text_color_; }
void set_text_color_rgb(int r, int g, int b) { text_color_ = Srgb{r, g, b}; }
void set_big_play_color(Srgb c) { big_play_color_ = c; }
Srgb get_big_play_color() { return big_play_color_; }
void set_big_play_color_rgb(int r, int g, int b) { big_play_color_ = Srgb{r, g, b}; }
void set_pause_bars_color(Srgb c) { pause_bars_color_ = c; }
Srgb get_pause_bars_color() { return pause_bars_color_; }
void set_pause_bars_color_rgb(int r, int g, int b) { pause_bars_color_ = Srgb{r, g, b}; }
void set_progress_line_color(Srgb c) { progress_line_color_ = c; }
Srgb get_progress_line_color() { return progress_line_color_; }
void set_progress_line_color_rgb(int r, int g, int b) { progress_line_color_ = Srgb{r, g, b}; }
void set_record_color(Srgb c) { record_color_ = c; }
Srgb get_record_color() { return record_color_; }
void set_record_color_rgb(int r, int g, int b) { record_color_ = Srgb{r, g, b}; }
void set_border_color(Srgb c) { border_color_ = c; }
Srgb get_border_color() { return border_color_; }
void set_border_color_rgb(int r, int g, int b) { border_color_ = Srgb{r, g, b}; }
//endregion
void draw()
{
if (!visible_)
{
return;
}
SStyle st = style();
// Picture area: near black with a faint scene gradient.
XSetForeground(display_, gc_, styles_->get_color_rgb(picture_area_color_.r, picture_area_color_.g, picture_area_color_.b));
XFillRectangle(display_, window_, gc_, x1_, y1_, width(), height());
XSetForeground(display_, gc_, styles_->get_color_rgb(fill_color_.r, fill_color_.g, fill_color_.b));
XFillRectangle(display_, window_, gc_, x1_, y1_, width(), height() / 2);
// Title, top left.
SStyle cap = st;
cap.text_color_ = styles_->get_color_rgb(text_color_.r, text_color_.g, text_color_.b);
xf::draw_text_at(display_, window_, gc_, cap, x1_ + 6, y1_ + 16, text_);
int cx = (x1_ + x2_) / 2;
int cy = (y1_ + y2_) / 2;
if (!playing_)
{
// Big play triangle in a ring.
XSetForeground(display_, gc_, styles_->get_color_rgb(big_play_color_.r, big_play_color_.g, big_play_color_.b));
XDrawArc(display_, window_, gc_, cx - 26, cy - 26, 52, 52, 0, 360 * 64);
xf::draw_triangle_right(display_, window_, gc_, styles_->get_color_rgb(pause_bars_color_.r, pause_bars_color_.g, pause_bars_color_.b), cx - 8, cy, 16);
}
else
{
// Pause bars.
XSetForeground(display_, gc_, styles_->get_color_rgb(pause_bars_color_.r, pause_bars_color_.g, pause_bars_color_.b));
XFillRectangle(display_, window_, gc_, cx - 12, cy - 14, 8, 28);
XFillRectangle(display_, window_, gc_, cx + 4, cy - 14, 8, 28);
}
// Progress line along the bottom.
int bar_y = y2_ - 6;
XSetForeground(display_, gc_, styles_->get_color_rgb(progress_line_color_.r, progress_line_color_.g, progress_line_color_.b));
XFillRectangle(display_, window_, gc_, x1_ + 4, bar_y, width() - 8, 3);
XSetForeground(display_, gc_, styles_->get_color_rgb(record_color_.r, record_color_.g, record_color_.b));
XFillRectangle(display_, window_, gc_, x1_ + 4, bar_y, (int)((width() - 8) * position_), 3);
if (focused_)
{
XSetForeground(display_, gc_, styles_->get_color_rgb(border_color_.r, border_color_.g, border_color_.b));
XDrawRectangle(display_, window_, gc_, x1_, y1_, width() - 1, height() - 1);
}
XFlush(display_);
}
bool on_button_press(int x, int y, unsigned int button)
{
if (!contains(x, y) || !enabled_ || button != 1)
{
return false;
}
focused_ = true;
if (playing_)
{
pause();
}
else
{
if (position_ >= 1.0)
{
position_ = 0.0; // replay from the start
}
play();
}
return true;
}
bool on_button_release(int x, int y, unsigned int button)
{
(void)x;
(void)y;
(void)button;
return false;
}
bool on_motion(int x, int y)
{
(void)x;
(void)y;
return false;
}
bool on_key(KeySym key, const char* str)
{
(void)str;
if (!focused_ || !enabled_)
{
return false;
}
if (key == XK_space)
{
if (playing_)
{
pause();
}
else
{
play();
}
return true;
}
if (key == XK_Right)
{
set_position(position_ + 0.05);
return true;
}
if (key == XK_Left)
{
set_position(position_ - 0.05);
return true;
}
return false;
}
};
//endregion