Compare commits
6 Commits
81bced2945
...
cd68386df2
Author | SHA1 | Date |
---|---|---|
Savanni D'Gerinel | cd68386df2 | |
Savanni D'Gerinel | 4c86ef5444 | |
Savanni D'Gerinel | 85b80d78ff | |
Savanni D'Gerinel | 15b56e0c81 | |
savanni | 1016ba756c | |
savanni | 52ca039f45 |
|
@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License along with Lum
|
||||||
//! Where the sled.rs library uses `Result<Result<A, Error>, FatalError>`, these are a little hard to
|
//! Where the sled.rs library uses `Result<Result<A, Error>, FatalError>`, these are a little hard to
|
||||||
//! work with. This library works out a set of utility functions that allow us to work with the
|
//! work with. This library works out a set of utility functions that allow us to work with the
|
||||||
//! nested errors in the same way as a regular Result.
|
//! nested errors in the same way as a regular Result.
|
||||||
use std::error::Error;
|
use std::{error::Error, fmt};
|
||||||
|
|
||||||
/// Implement this trait for the application's fatal errors.
|
/// Implement this trait for the application's fatal errors.
|
||||||
///
|
///
|
||||||
|
@ -110,6 +110,37 @@ impl<A, FE, E> From<Result<A, E>> for Flow<A, FE, E> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<A, FE, E> fmt::Debug for Flow<A, FE, E>
|
||||||
|
where
|
||||||
|
A: fmt::Debug,
|
||||||
|
FE: fmt::Debug,
|
||||||
|
E: fmt::Debug,
|
||||||
|
{
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
Flow::Ok(val) => f.write_fmt(format_args!("Flow::Ok {:?}", val)),
|
||||||
|
Flow::Err(err) => f.write_fmt(format_args!("Flow::Err {:?}", err)),
|
||||||
|
Flow::Fatal(err) => f.write_fmt(format_args!("Flow::Fatal {:?}", err)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A, FE, E> PartialEq for Flow<A, FE, E>
|
||||||
|
where
|
||||||
|
A: PartialEq,
|
||||||
|
FE: PartialEq,
|
||||||
|
E: PartialEq,
|
||||||
|
{
|
||||||
|
fn eq(&self, rhs: &Self) -> bool {
|
||||||
|
match (self, rhs) {
|
||||||
|
(Flow::Ok(val), Flow::Ok(rhs)) => val == rhs,
|
||||||
|
(Flow::Err(_), Flow::Err(_)) => true,
|
||||||
|
(Flow::Fatal(_), Flow::Fatal(_)) => true,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Convenience function to create an ok value.
|
/// Convenience function to create an ok value.
|
||||||
pub fn ok<A, FE: FatalError, E: Error>(val: A) -> Flow<A, FE, E> {
|
pub fn ok<A, FE: FatalError, E: Error>(val: A) -> Flow<A, FE, E> {
|
||||||
Flow::Ok(val)
|
Flow::Ok(val)
|
||||||
|
@ -177,43 +208,25 @@ mod test {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq for Flow<i32, FatalError, Error> {
|
|
||||||
fn eq(&self, rhs: &Self) -> bool {
|
|
||||||
match (self, rhs) {
|
|
||||||
(Flow::Ok(val), Flow::Ok(rhs)) => val == rhs,
|
|
||||||
(Flow::Err(_), Flow::Err(_)) => true,
|
|
||||||
(Flow::Fatal(_), Flow::Fatal(_)) => true,
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Debug for Flow<i32, FatalError, Error> {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
match self {
|
|
||||||
Flow::Ok(val) => f.write_fmt(format_args!("Flow::Ok {}", val)),
|
|
||||||
Flow::Err(err) => f.write_fmt(format_args!("Flow::Err {:?}", err)),
|
|
||||||
Flow::Fatal(err) => f.write_fmt(format_args!("Flow::Fatal {:?}", err)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_can_map_things() {
|
fn it_can_map_things() {
|
||||||
let success = ok(15);
|
let success: Flow<i32, FatalError, Error> = ok(15);
|
||||||
assert_eq!(ok(16), success.map(|v| v + 1));
|
assert_eq!(ok(16), success.map(|v| v + 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_can_chain_success() {
|
fn it_can_chain_success() {
|
||||||
let success = ok(15);
|
let success: Flow<i32, FatalError, Error> = ok(15);
|
||||||
assert_eq!(ok(16), success.and_then(|v| ok(v + 1)));
|
assert_eq!(ok(16), success.and_then(|v| ok(v + 1)));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_can_handle_an_error() {
|
fn it_can_handle_an_error() {
|
||||||
let failure = error(Error::Error);
|
let failure: Flow<i32, FatalError, Error> = error(Error::Error);
|
||||||
assert_eq!(ok(16), failure.or_else(|_| ok(16)));
|
assert_eq!(
|
||||||
|
ok::<i32, FatalError, Error>(16),
|
||||||
|
failure.or_else(|_| ok(16))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -1,99 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<interface>
|
|
||||||
<template class="HexGridWindow" parent="GtkApplicationWindow">
|
|
||||||
<property name="can-focus">False</property>
|
|
||||||
<child>
|
|
||||||
<object class="GtkBox">
|
|
||||||
<property name="visible">True</property>
|
|
||||||
<property name="can-focus">False</property>
|
|
||||||
<property name="spacing">8</property>
|
|
||||||
<child>
|
|
||||||
<object class="GtkDrawingArea" id="drawing_area">
|
|
||||||
<property name="width-request">1024</property>
|
|
||||||
<property name="height-request">768</property>
|
|
||||||
<property name="visible">True</property>
|
|
||||||
<property name="can-focus">False</property>
|
|
||||||
<property name="margin-start">8</property>
|
|
||||||
<property name="margin-end">8</property>
|
|
||||||
<property name="margin-top">8</property>
|
|
||||||
<property name="margin-bottom">8</property>
|
|
||||||
<property name="hexpand">True</property>
|
|
||||||
</object>
|
|
||||||
</child>
|
|
||||||
<child>
|
|
||||||
<object class="GtkBox">
|
|
||||||
<property name="width-request">100</property>
|
|
||||||
<property name="visible">True</property>
|
|
||||||
<property name="can-focus">False</property>
|
|
||||||
<property name="margin-start">4</property>
|
|
||||||
<property name="margin-end">4</property>
|
|
||||||
<property name="margin-top">4</property>
|
|
||||||
<property name="margin-bottom">4</property>
|
|
||||||
<property name="orientation">vertical</property>
|
|
||||||
<property name="spacing">8</property>
|
|
||||||
<child>
|
|
||||||
<object class="GtkBox">
|
|
||||||
<property name="visible">True</property>
|
|
||||||
<property name="can-focus">False</property>
|
|
||||||
<property name="hexpand">True</property>
|
|
||||||
<property name="spacing">8</property>
|
|
||||||
<property name="homogeneous">True</property>
|
|
||||||
<child>
|
|
||||||
<object class="GtkLabel">
|
|
||||||
<property name="visible">True</property>
|
|
||||||
<property name="can-focus">False</property>
|
|
||||||
<property name="label" translatable="yes">Canvas Address</property>
|
|
||||||
</object>
|
|
||||||
</child>
|
|
||||||
<child>
|
|
||||||
<object class="GtkLabel" id="canvas_address">
|
|
||||||
<property name="visible">True</property>
|
|
||||||
<property name="can-focus">False</property>
|
|
||||||
<property name="margin-start">4</property>
|
|
||||||
<property name="margin-end">4</property>
|
|
||||||
<property name="margin-top">4</property>
|
|
||||||
<property name="margin-bottom">4</property>
|
|
||||||
<property name="label" translatable="yes">-----</property>
|
|
||||||
</object>
|
|
||||||
</child>
|
|
||||||
</object>
|
|
||||||
</child>
|
|
||||||
<child>
|
|
||||||
<object class="GtkBox">
|
|
||||||
<property name="visible">True</property>
|
|
||||||
<property name="can-focus">False</property>
|
|
||||||
<property name="hexpand">True</property>
|
|
||||||
<property name="spacing">8</property>
|
|
||||||
<property name="homogeneous">True</property>
|
|
||||||
<child>
|
|
||||||
<object class="GtkLabel">
|
|
||||||
<property name="visible">True</property>
|
|
||||||
<property name="can-focus">False</property>
|
|
||||||
<property name="label" translatable="yes">Hexagon Address</property>
|
|
||||||
</object>
|
|
||||||
</child>
|
|
||||||
<child>
|
|
||||||
<object class="GtkLabel" id="hex_address">
|
|
||||||
<property name="visible">True</property>
|
|
||||||
<property name="can-focus">False</property>
|
|
||||||
<property name="margin-start">4</property>
|
|
||||||
<property name="margin-end">4</property>
|
|
||||||
<property name="margin-top">4</property>
|
|
||||||
<property name="margin-bottom">4</property>
|
|
||||||
<property name="label" translatable="yes">-----</property>
|
|
||||||
</object>
|
|
||||||
</child>
|
|
||||||
</object>
|
|
||||||
</child>
|
|
||||||
<child>
|
|
||||||
<placeholder/>
|
|
||||||
</child>
|
|
||||||
<child>
|
|
||||||
<placeholder/>
|
|
||||||
</child>
|
|
||||||
</object>
|
|
||||||
</child>
|
|
||||||
</object>
|
|
||||||
</child>
|
|
||||||
</template>
|
|
||||||
</interface>
|
|
|
@ -1,7 +1,6 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<gresources>
|
<gresources>
|
||||||
<gresource prefix="/com/luminescent-dreams/hex-grid/">
|
<gresource prefix="/com/luminescent-dreams/hex-grid/">
|
||||||
<file>main.glade</file>
|
|
||||||
<file>terrain.ppm</file>
|
<file>terrain.ppm</file>
|
||||||
<file>map.txt</file>
|
<file>map.txt</file>
|
||||||
</gresource>
|
</gresource>
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
use glib::Object;
|
||||||
|
use gtk::{prelude::*, subclass::prelude::*};
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct LabeledFieldPrivate {
|
||||||
|
value: gtk::Label,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[glib::object_subclass]
|
||||||
|
impl ObjectSubclass for LabeledFieldPrivate {
|
||||||
|
const NAME: &'static str = "LabeledField";
|
||||||
|
type Type = LabeledField;
|
||||||
|
type ParentType = gtk::Box;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ObjectImpl for LabeledFieldPrivate {}
|
||||||
|
impl WidgetImpl for LabeledFieldPrivate {}
|
||||||
|
impl BoxImpl for LabeledFieldPrivate {}
|
||||||
|
|
||||||
|
glib::wrapper! {
|
||||||
|
pub struct LabeledField(ObjectSubclass<LabeledFieldPrivate>) @extends gtk::Box, gtk::Widget;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LabeledField {
|
||||||
|
pub fn new(label: &str, default_value: &str) -> Self {
|
||||||
|
let field: Self = Object::builder().build();
|
||||||
|
field.set_hexpand(true);
|
||||||
|
field.set_spacing(8);
|
||||||
|
field.set_homogeneous(true);
|
||||||
|
field.append(>k::Label::new(Some(label)));
|
||||||
|
field.append(&field.imp().value);
|
||||||
|
field.imp().value.set_label(default_value);
|
||||||
|
field
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_value(&self, value: &str) {
|
||||||
|
self.imp().value.set_label(value);
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,92 +10,24 @@ Luminescent Dreams Tools is distributed in the hope that it will be useful, but
|
||||||
You should have received a copy of the GNU General Public License along with Lumeto. If not, see <https://www.gnu.org/licenses/>.
|
You should have received a copy of the GNU General Public License along with Lumeto. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use cairo::{Context, Path};
|
use cairo::Context;
|
||||||
use coordinates::{hex_map::parse_data, AxialAddr};
|
use coordinates::{hex_map::parse_data, AxialAddr};
|
||||||
use gio::resources_lookup_data;
|
use gio::resources_lookup_data;
|
||||||
use glib::{subclass::InitializingObject, Object};
|
use glib::{subclass::InitializingObject, Object};
|
||||||
use gtk::{
|
use gtk::{gio, prelude::*, subclass::prelude::*, Application, DrawingArea};
|
||||||
gdk_pixbuf::Pixbuf, gio, prelude::*, subclass::prelude::*, Application, CompositeTemplate,
|
use image::io::Reader as ImageReader;
|
||||||
DrawingArea, Label,
|
|
||||||
};
|
|
||||||
use image::{io::Reader as ImageReader, DynamicImage};
|
|
||||||
use std::{cell::RefCell, io::Cursor, rc::Rc};
|
use std::{cell::RefCell, io::Cursor, rc::Rc};
|
||||||
|
|
||||||
|
mod labeled_field;
|
||||||
|
mod palette_entry;
|
||||||
|
mod tile;
|
||||||
|
mod utilities;
|
||||||
|
|
||||||
const APP_ID: &'static str = "com.luminescent-dreams.hex-grid";
|
const APP_ID: &'static str = "com.luminescent-dreams.hex-grid";
|
||||||
const HEX_RADIUS: f64 = 50.;
|
const HEX_RADIUS: f64 = 50.;
|
||||||
const MAP_RADIUS: usize = 3;
|
const MAP_RADIUS: usize = 3;
|
||||||
const DRAWING_ORIGIN: (f64, f64) = (1024. / 2., 768. / 2.);
|
const DRAWING_ORIGIN: (f64, f64) = (1024. / 2., 768. / 2.);
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
enum Terrain {
|
|
||||||
Empty,
|
|
||||||
Mountain,
|
|
||||||
Grasslands,
|
|
||||||
ShallowWater,
|
|
||||||
DeepWater,
|
|
||||||
Badlands,
|
|
||||||
Desert,
|
|
||||||
Swamp,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for Terrain {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self::Empty
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<&str> for Terrain {
|
|
||||||
fn from(s: &str) -> Self {
|
|
||||||
match s {
|
|
||||||
"m" => Self::Mountain,
|
|
||||||
"g" => Self::Grasslands,
|
|
||||||
"sw" => Self::ShallowWater,
|
|
||||||
"dw" => Self::DeepWater,
|
|
||||||
"b" => Self::Badlands,
|
|
||||||
"d" => Self::Desert,
|
|
||||||
"s" => Self::Swamp,
|
|
||||||
_ => Self::Empty,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<String> for Terrain {
|
|
||||||
fn from(s: String) -> Self {
|
|
||||||
Self::from(s.as_ref())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Tile {
|
|
||||||
terrain: Terrain,
|
|
||||||
image: Pixbuf,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Tile {
|
|
||||||
fn new(source: &DynamicImage, terrain: Terrain) -> Tile {
|
|
||||||
let image = match terrain {
|
|
||||||
Terrain::DeepWater => pixbuf_from_image_tile(source.clone().crop(0, 0, 100, 88)),
|
|
||||||
Terrain::ShallowWater => pixbuf_from_image_tile(source.clone().crop(100, 0, 100, 88)),
|
|
||||||
Terrain::Grasslands => pixbuf_from_image_tile(source.clone().crop(200, 0, 100, 88)),
|
|
||||||
Terrain::Desert => pixbuf_from_image_tile(source.clone().crop(300, 0, 100, 88)),
|
|
||||||
Terrain::Mountain => pixbuf_from_image_tile(source.clone().crop(0, 88, 100, 88)),
|
|
||||||
Terrain::Badlands => pixbuf_from_image_tile(source.clone().crop(100, 88, 100, 88)),
|
|
||||||
Terrain::Swamp => pixbuf_from_image_tile(source.clone().crop(0, 176, 100, 88)),
|
|
||||||
Terrain::Empty => pixbuf_from_image_tile(source.clone().crop(300, 176, 100, 88)),
|
|
||||||
};
|
|
||||||
|
|
||||||
Tile { terrain, image }
|
|
||||||
}
|
|
||||||
|
|
||||||
fn render_on_context(&self, context: &Context, translate_x: f64, translate_y: f64) {
|
|
||||||
context.save().unwrap();
|
|
||||||
context.append_path(&hexagon_path(context, translate_x, translate_y, 100., 88.));
|
|
||||||
context.clip();
|
|
||||||
context.set_source_pixbuf(&self.image, translate_x, translate_y);
|
|
||||||
context.paint().expect("paint should succeed");
|
|
||||||
context.restore().unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
gio::resources_register_include!("com.luminescent-dreams.hex-grid.gresource")
|
gio::resources_register_include!("com.luminescent-dreams.hex-grid.gresource")
|
||||||
.expect("Failed to register resources");
|
.expect("Failed to register resources");
|
||||||
|
@ -110,15 +42,13 @@ fn main() {
|
||||||
app.run();
|
app.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(CompositeTemplate, Default)]
|
|
||||||
#[template(resource = "/com/luminescent-dreams/hex-grid/main.glade")]
|
|
||||||
pub struct HexGridWindowPrivate {
|
pub struct HexGridWindowPrivate {
|
||||||
#[template_child]
|
layout: gtk::Box,
|
||||||
pub drawing_area: TemplateChild<DrawingArea>,
|
palette: gtk::Box,
|
||||||
#[template_child]
|
|
||||||
pub hex_address: TemplateChild<Label>,
|
drawing_area: DrawingArea,
|
||||||
#[template_child]
|
hex_address: labeled_field::LabeledField,
|
||||||
pub canvas_address: TemplateChild<Label>,
|
canvas_address: labeled_field::LabeledField,
|
||||||
|
|
||||||
current_coordinate: Rc<RefCell<Option<AxialAddr>>>,
|
current_coordinate: Rc<RefCell<Option<AxialAddr>>>,
|
||||||
}
|
}
|
||||||
|
@ -129,17 +59,71 @@ impl ObjectSubclass for HexGridWindowPrivate {
|
||||||
type Type = HexGridWindow;
|
type Type = HexGridWindow;
|
||||||
type ParentType = gtk::ApplicationWindow;
|
type ParentType = gtk::ApplicationWindow;
|
||||||
|
|
||||||
fn class_init(c: &mut Self::Class) {
|
fn new() -> Self {
|
||||||
c.bind_template();
|
println!("hexGridWindowPrivate::new()");
|
||||||
}
|
let current_coordinate = Rc::new(RefCell::new(None));
|
||||||
|
|
||||||
fn instance_init(obj: &InitializingObject<Self>) {
|
let drawing_area = DrawingArea::builder()
|
||||||
obj.init_template();
|
.width_request(1024)
|
||||||
|
.height_request(768)
|
||||||
|
.margin_start(8)
|
||||||
|
.margin_end(8)
|
||||||
|
.margin_top(8)
|
||||||
|
.margin_bottom(8)
|
||||||
|
.hexpand(true)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let layout = gtk::Box::builder()
|
||||||
|
.homogeneous(false)
|
||||||
|
.spacing(8)
|
||||||
|
.can_focus(false)
|
||||||
|
.visible(true)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let sidebar = gtk::Box::builder()
|
||||||
|
.orientation(gtk::Orientation::Vertical)
|
||||||
|
.width_request(100)
|
||||||
|
.visible(true)
|
||||||
|
.can_focus(false)
|
||||||
|
.margin_start(4)
|
||||||
|
.margin_end(4)
|
||||||
|
.margin_top(4)
|
||||||
|
.margin_bottom(4)
|
||||||
|
.spacing(8)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let canvas_address = labeled_field::LabeledField::new("Canvas Address", "-----");
|
||||||
|
let hex_address = labeled_field::LabeledField::new("Hex Address", "-----");
|
||||||
|
|
||||||
|
let palette = gtk::Box::builder()
|
||||||
|
.spacing(8)
|
||||||
|
.orientation(gtk::Orientation::Vertical)
|
||||||
|
.hexpand(true)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
sidebar.append(&canvas_address);
|
||||||
|
sidebar.append(&hex_address);
|
||||||
|
sidebar.append(&palette);
|
||||||
|
|
||||||
|
layout.append(&drawing_area);
|
||||||
|
layout.append(&sidebar);
|
||||||
|
|
||||||
|
layout.show();
|
||||||
|
|
||||||
|
Self {
|
||||||
|
drawing_area,
|
||||||
|
hex_address,
|
||||||
|
canvas_address,
|
||||||
|
current_coordinate,
|
||||||
|
layout,
|
||||||
|
palette,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ObjectImpl for HexGridWindowPrivate {
|
impl ObjectImpl for HexGridWindowPrivate {
|
||||||
fn constructed(&self) {
|
fn constructed(&self) {
|
||||||
|
println!("HexGridWindowPrivate::constructed()");
|
||||||
self.parent_constructed();
|
self.parent_constructed();
|
||||||
|
|
||||||
let map_text_resource = resources_lookup_data(
|
let map_text_resource = resources_lookup_data(
|
||||||
|
@ -149,7 +133,8 @@ impl ObjectImpl for HexGridWindowPrivate {
|
||||||
.expect("map should be in the bundle")
|
.expect("map should be in the bundle")
|
||||||
.to_vec();
|
.to_vec();
|
||||||
|
|
||||||
let hex_map = parse_data::<Terrain>(String::from_utf8(map_text_resource).unwrap().lines());
|
let hex_map =
|
||||||
|
parse_data::<tile::Terrain>(String::from_utf8(map_text_resource).unwrap().lines());
|
||||||
|
|
||||||
let terrain_data = resources_lookup_data(
|
let terrain_data = resources_lookup_data(
|
||||||
"/com/luminescent-dreams/hex-grid/terrain.ppm",
|
"/com/luminescent-dreams/hex-grid/terrain.ppm",
|
||||||
|
@ -161,13 +146,28 @@ impl ObjectImpl for HexGridWindowPrivate {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let image = reader.decode().unwrap();
|
let image = reader.decode().unwrap();
|
||||||
let deep_water = Tile::new(&image, Terrain::DeepWater);
|
let badlands = tile::Tile::new(&image, tile::Terrain::Badlands);
|
||||||
let shallow_water = Tile::new(&image, Terrain::ShallowWater);
|
let deep_water = tile::Tile::new(&image, tile::Terrain::DeepWater);
|
||||||
let grasslands = Tile::new(&image, Terrain::Grasslands);
|
let desert = tile::Tile::new(&image, tile::Terrain::Desert);
|
||||||
let desert = Tile::new(&image, Terrain::Desert);
|
let grasslands = tile::Tile::new(&image, tile::Terrain::Grasslands);
|
||||||
let mountain = Tile::new(&image, Terrain::Mountain);
|
let mountain = tile::Tile::new(&image, tile::Terrain::Mountain);
|
||||||
let badlands = Tile::new(&image, Terrain::Badlands);
|
let shallow_water = tile::Tile::new(&image, tile::Terrain::ShallowWater);
|
||||||
let swamp = Tile::new(&image, Terrain::Swamp);
|
let swamp = tile::Tile::new(&image, tile::Terrain::Swamp);
|
||||||
|
|
||||||
|
self.palette
|
||||||
|
.append(&palette_entry::PaletteEntry::new(&badlands));
|
||||||
|
self.palette
|
||||||
|
.append(&palette_entry::PaletteEntry::new(&deep_water));
|
||||||
|
self.palette
|
||||||
|
.append(&palette_entry::PaletteEntry::new(&desert));
|
||||||
|
self.palette
|
||||||
|
.append(&palette_entry::PaletteEntry::new(&grasslands));
|
||||||
|
self.palette
|
||||||
|
.append(&palette_entry::PaletteEntry::new(&mountain));
|
||||||
|
self.palette
|
||||||
|
.append(&palette_entry::PaletteEntry::new(&shallow_water));
|
||||||
|
self.palette
|
||||||
|
.append(&palette_entry::PaletteEntry::new(&swamp));
|
||||||
|
|
||||||
let motion_controller = gtk::EventControllerMotion::new();
|
let motion_controller = gtk::EventControllerMotion::new();
|
||||||
{
|
{
|
||||||
|
@ -182,13 +182,13 @@ impl ObjectImpl for HexGridWindowPrivate {
|
||||||
|
|
||||||
let (q, r) = axial_round(q, r);
|
let (q, r) = axial_round(q, r);
|
||||||
let coordinate = AxialAddr::new(q, r);
|
let coordinate = AxialAddr::new(q, r);
|
||||||
canvas_address.set_label(&format!("{:.0} {:.0}", x, y));
|
canvas_address.set_value(&format!("{:.0} {:.0}", x, y));
|
||||||
|
|
||||||
if coordinate.distance(&AxialAddr::origin()) > MAP_RADIUS {
|
if coordinate.distance(&AxialAddr::origin()) > MAP_RADIUS {
|
||||||
hex_address.set_label(&format!("-----"));
|
hex_address.set_value(&format!("-----"));
|
||||||
*c.borrow_mut() = None;
|
*c.borrow_mut() = None;
|
||||||
} else {
|
} else {
|
||||||
hex_address.set_label(&format!("{:.0} {:.0}", coordinate.q(), coordinate.r()));
|
hex_address.set_value(&format!("{:.0} {:.0}", coordinate.q(), coordinate.r()));
|
||||||
*c.borrow_mut() = Some(coordinate);
|
*c.borrow_mut() = Some(coordinate);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -201,16 +201,6 @@ impl ObjectImpl for HexGridWindowPrivate {
|
||||||
|
|
||||||
context.set_line_width(2.);
|
context.set_line_width(2.);
|
||||||
|
|
||||||
/*
|
|
||||||
deep_water.render_on_context(&context, 0., 0.);
|
|
||||||
shallow_water.render_on_context(&context, 150., 0.);
|
|
||||||
grasslands.render_on_context(&context, 300., 0.);
|
|
||||||
desert.render_on_context(&context, 450., 0.);
|
|
||||||
mountain.render_on_context(&context, 0., 100.);
|
|
||||||
badlands.render_on_context(&context, 150., 100.);
|
|
||||||
swamp.render_on_context(&context, 0., 200.);
|
|
||||||
*/
|
|
||||||
|
|
||||||
for coordinate in vec![AxialAddr::origin()]
|
for coordinate in vec![AxialAddr::origin()]
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.chain(AxialAddr::origin().addresses(MAP_RADIUS))
|
.chain(AxialAddr::origin().addresses(MAP_RADIUS))
|
||||||
|
@ -225,17 +215,16 @@ impl ObjectImpl for HexGridWindowPrivate {
|
||||||
let translate_y = center_y - (3. as f64).sqrt() * HEX_RADIUS / 2.;
|
let translate_y = center_y - (3. as f64).sqrt() * HEX_RADIUS / 2.;
|
||||||
|
|
||||||
let tile = match hex_map.get(&coordinate).unwrap() {
|
let tile = match hex_map.get(&coordinate).unwrap() {
|
||||||
Terrain::Mountain => &mountain,
|
tile::Terrain::Mountain => &mountain,
|
||||||
Terrain::Grasslands => &grasslands,
|
tile::Terrain::Grasslands => &grasslands,
|
||||||
Terrain::ShallowWater => &shallow_water,
|
tile::Terrain::ShallowWater => &shallow_water,
|
||||||
Terrain::DeepWater => &deep_water,
|
tile::Terrain::DeepWater => &deep_water,
|
||||||
Terrain::Badlands => &badlands,
|
tile::Terrain::Badlands => &badlands,
|
||||||
Terrain::Desert => &desert,
|
tile::Terrain::Desert => &desert,
|
||||||
Terrain::Swamp => &swamp,
|
tile::Terrain::Swamp => &swamp,
|
||||||
_ => panic!("unhandled terrain type"),
|
_ => panic!("unhandled terrain type"),
|
||||||
};
|
};
|
||||||
tile.render_on_context(context, translate_x, translate_y);
|
tile.render_on_context(context, translate_x, translate_y);
|
||||||
// draw_hexagon(context, center_x, center_y, HEX_RADIUS);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -254,14 +243,16 @@ glib::wrapper! {
|
||||||
|
|
||||||
impl HexGridWindow {
|
impl HexGridWindow {
|
||||||
pub fn new(app: &Application) -> Self {
|
pub fn new(app: &Application) -> Self {
|
||||||
Object::builder().property("application", app).build()
|
let window: Self = Object::builder().property("application", app).build();
|
||||||
|
window.set_child(Some(&window.imp().layout));
|
||||||
|
window
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw_hexagon(context: &Context, center_x: f64, center_y: f64, radius: f64) {
|
fn draw_hexagon(context: &Context, center_x: f64, center_y: f64, radius: f64) {
|
||||||
let ul_x = center_x - radius;
|
let ul_x = center_x - radius;
|
||||||
let ul_y = center_y - (3. as f64).sqrt() * radius / 2.;
|
let ul_y = center_y - (3. as f64).sqrt() * radius / 2.;
|
||||||
let points: Vec<(f64, f64)> = hexagon(radius * 2., (3. as f64).sqrt() * radius);
|
let points: Vec<(f64, f64)> = utilities::hexagon(radius * 2., (3. as f64).sqrt() * radius);
|
||||||
context.new_path();
|
context.new_path();
|
||||||
context.move_to(ul_x + points[0].0, ul_y + points[0].1);
|
context.move_to(ul_x + points[0].0, ul_y + points[0].1);
|
||||||
context.line_to(ul_x + points[1].0, ul_y + points[1].1);
|
context.line_to(ul_x + points[1].0, ul_y + points[1].1);
|
||||||
|
@ -272,51 +263,6 @@ fn draw_hexagon(context: &Context, center_x: f64, center_y: f64, radius: f64) {
|
||||||
context.close_path();
|
context.close_path();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn hexagon_path(
|
|
||||||
context: &Context,
|
|
||||||
translate_x: f64,
|
|
||||||
translate_y: f64,
|
|
||||||
width: f64,
|
|
||||||
height: f64,
|
|
||||||
) -> Path {
|
|
||||||
context.new_path();
|
|
||||||
let points = hexagon(width, height);
|
|
||||||
context.move_to(translate_x + points[0].0, translate_y + points[0].1);
|
|
||||||
context.line_to(translate_x + points[1].0, translate_y + points[1].1);
|
|
||||||
context.line_to(translate_x + points[2].0, translate_y + points[2].1);
|
|
||||||
context.line_to(translate_x + points[3].0, translate_y + points[3].1);
|
|
||||||
context.line_to(translate_x + points[4].0, translate_y + points[4].1);
|
|
||||||
context.line_to(translate_x + points[5].0, translate_y + points[5].1);
|
|
||||||
context.copy_path().expect("to successfully copy a path")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn hexagon(width: f64, height: f64) -> Vec<(f64, f64)> {
|
|
||||||
let center_x = width / 2.;
|
|
||||||
let center_y = height / 2.;
|
|
||||||
let radius = width / 2.;
|
|
||||||
|
|
||||||
vec![
|
|
||||||
(center_x + radius, center_y),
|
|
||||||
(
|
|
||||||
center_x + radius / 2.,
|
|
||||||
center_y + (3. as f64).sqrt() * radius / 2.,
|
|
||||||
),
|
|
||||||
(
|
|
||||||
center_x - radius / 2.,
|
|
||||||
center_y + (3. as f64).sqrt() * radius / 2.,
|
|
||||||
),
|
|
||||||
(center_x - radius, center_y),
|
|
||||||
(
|
|
||||||
center_x - radius / 2.,
|
|
||||||
center_y - (3. as f64).sqrt() * radius / 2.,
|
|
||||||
),
|
|
||||||
(
|
|
||||||
center_x + radius / 2.,
|
|
||||||
center_y - (3. as f64).sqrt() * radius / 2.,
|
|
||||||
),
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
fn axial_round(q_f64: f64, r_f64: f64) -> (i32, i32) {
|
fn axial_round(q_f64: f64, r_f64: f64) -> (i32, i32) {
|
||||||
let s_f64 = -q_f64 - r_f64;
|
let s_f64 = -q_f64 - r_f64;
|
||||||
let mut q = q_f64.round();
|
let mut q = q_f64.round();
|
||||||
|
@ -334,15 +280,3 @@ fn axial_round(q_f64: f64, r_f64: f64) -> (i32, i32) {
|
||||||
}
|
}
|
||||||
unsafe { (q.to_int_unchecked::<i32>(), r.to_int_unchecked::<i32>()) }
|
unsafe { (q.to_int_unchecked::<i32>(), r.to_int_unchecked::<i32>()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pixbuf_from_image_tile(image: image::DynamicImage) -> Pixbuf {
|
|
||||||
Pixbuf::from_bytes(
|
|
||||||
&glib::Bytes::from(image.as_bytes()),
|
|
||||||
gtk::gdk_pixbuf::Colorspace::Rgb,
|
|
||||||
false,
|
|
||||||
8,
|
|
||||||
image.width() as i32,
|
|
||||||
image.height() as i32,
|
|
||||||
image.to_rgb8().sample_layout().height_stride as i32,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
|
@ -0,0 +1,52 @@
|
||||||
|
use crate::tile;
|
||||||
|
use glib::Object;
|
||||||
|
use gtk::{prelude::*, subclass::prelude::*};
|
||||||
|
use std::{cell::RefCell, rc::Rc};
|
||||||
|
|
||||||
|
pub struct PaletteEntryPrivate {
|
||||||
|
terrain: Rc<RefCell<tile::Terrain>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[glib::object_subclass]
|
||||||
|
impl ObjectSubclass for PaletteEntryPrivate {
|
||||||
|
const NAME: &'static str = "PaletteEntry";
|
||||||
|
type Type = PaletteEntry;
|
||||||
|
type ParentType = gtk::ListBoxRow;
|
||||||
|
|
||||||
|
fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
terrain: Rc::new(RefCell::new(tile::Terrain::Empty)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ObjectImpl for PaletteEntryPrivate {}
|
||||||
|
impl WidgetImpl for PaletteEntryPrivate {}
|
||||||
|
impl ListBoxRowImpl for PaletteEntryPrivate {
|
||||||
|
fn activate(&self) {
|
||||||
|
println!("row activated: {:?}", self.terrain);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
glib::wrapper! {
|
||||||
|
pub struct PaletteEntry(ObjectSubclass<PaletteEntryPrivate>) @extends gtk::ListBoxRow, gtk::Widget;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PaletteEntry {
|
||||||
|
pub fn new(tile: &tile::Tile) -> Self {
|
||||||
|
let row: Self = Object::builder().build();
|
||||||
|
*row.imp().terrain.borrow_mut() = tile.terrain.clone();
|
||||||
|
let layout = gtk::Box::builder().spacing(8).build();
|
||||||
|
|
||||||
|
let image = gtk::Image::from_pixbuf(Some(&tile.image));
|
||||||
|
image.set_width_request(100);
|
||||||
|
image.set_height_request(88);
|
||||||
|
|
||||||
|
layout.append(&image);
|
||||||
|
layout.append(>k::Label::new(Some(&String::from(&tile.terrain))));
|
||||||
|
|
||||||
|
row.set_child(Some(&layout));
|
||||||
|
|
||||||
|
row
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,108 @@
|
||||||
|
use crate::utilities;
|
||||||
|
use cairo::Context;
|
||||||
|
use gtk::{gdk_pixbuf::Pixbuf, prelude::*};
|
||||||
|
use image::DynamicImage;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub enum Terrain {
|
||||||
|
Badlands,
|
||||||
|
DeepWater,
|
||||||
|
Desert,
|
||||||
|
Empty,
|
||||||
|
Grasslands,
|
||||||
|
Mountain,
|
||||||
|
ShallowWater,
|
||||||
|
Swamp,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Terrain {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::Empty
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&str> for Terrain {
|
||||||
|
fn from(s: &str) -> Self {
|
||||||
|
match s {
|
||||||
|
"m" => Self::Mountain,
|
||||||
|
"g" => Self::Grasslands,
|
||||||
|
"sw" => Self::ShallowWater,
|
||||||
|
"dw" => Self::DeepWater,
|
||||||
|
"b" => Self::Badlands,
|
||||||
|
"d" => Self::Desert,
|
||||||
|
"s" => Self::Swamp,
|
||||||
|
_ => Self::Empty,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<String> for Terrain {
|
||||||
|
fn from(s: String) -> Self {
|
||||||
|
Self::from(s.as_ref())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&Terrain> for String {
|
||||||
|
fn from(t: &Terrain) -> Self {
|
||||||
|
match t {
|
||||||
|
Terrain::Badlands => "Badlands",
|
||||||
|
Terrain::DeepWater => "Deep Water",
|
||||||
|
Terrain::Desert => "Desert",
|
||||||
|
Terrain::Empty => "Empty",
|
||||||
|
Terrain::Grasslands => "Grasslands",
|
||||||
|
Terrain::Mountain => "Mountain",
|
||||||
|
Terrain::ShallowWater => "Shallow Water",
|
||||||
|
Terrain::Swamp => "Swamp",
|
||||||
|
}
|
||||||
|
.to_owned()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Tile {
|
||||||
|
pub terrain: Terrain,
|
||||||
|
pub image: Pixbuf,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Tile {
|
||||||
|
pub fn new(source: &DynamicImage, terrain: Terrain) -> Tile {
|
||||||
|
let image = match terrain {
|
||||||
|
Terrain::DeepWater => pixbuf_from_image_tile(source.clone().crop(0, 0, 100, 88)),
|
||||||
|
Terrain::ShallowWater => pixbuf_from_image_tile(source.clone().crop(100, 0, 100, 88)),
|
||||||
|
Terrain::Grasslands => pixbuf_from_image_tile(source.clone().crop(200, 0, 100, 88)),
|
||||||
|
Terrain::Desert => pixbuf_from_image_tile(source.clone().crop(300, 0, 100, 88)),
|
||||||
|
Terrain::Mountain => pixbuf_from_image_tile(source.clone().crop(0, 88, 100, 88)),
|
||||||
|
Terrain::Badlands => pixbuf_from_image_tile(source.clone().crop(100, 88, 100, 88)),
|
||||||
|
Terrain::Swamp => pixbuf_from_image_tile(source.clone().crop(0, 176, 100, 88)),
|
||||||
|
Terrain::Empty => pixbuf_from_image_tile(source.clone().crop(300, 176, 100, 88)),
|
||||||
|
};
|
||||||
|
|
||||||
|
Tile { terrain, image }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn render_on_context(&self, context: &Context, translate_x: f64, translate_y: f64) {
|
||||||
|
context.save().unwrap();
|
||||||
|
context.append_path(&utilities::hexagon_path(
|
||||||
|
context,
|
||||||
|
translate_x,
|
||||||
|
translate_y,
|
||||||
|
100.,
|
||||||
|
88.,
|
||||||
|
));
|
||||||
|
context.clip();
|
||||||
|
context.set_source_pixbuf(&self.image, translate_x, translate_y);
|
||||||
|
context.paint().expect("paint should succeed");
|
||||||
|
context.restore().unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pixbuf_from_image_tile(image: image::DynamicImage) -> Pixbuf {
|
||||||
|
Pixbuf::from_bytes(
|
||||||
|
&glib::Bytes::from(image.as_bytes()),
|
||||||
|
gtk::gdk_pixbuf::Colorspace::Rgb,
|
||||||
|
false,
|
||||||
|
8,
|
||||||
|
image.width() as i32,
|
||||||
|
image.height() as i32,
|
||||||
|
image.to_rgb8().sample_layout().height_stride as i32,
|
||||||
|
)
|
||||||
|
}
|
|
@ -0,0 +1,46 @@
|
||||||
|
use cairo::{Context, Path};
|
||||||
|
|
||||||
|
pub fn hexagon_path(
|
||||||
|
context: &Context,
|
||||||
|
translate_x: f64,
|
||||||
|
translate_y: f64,
|
||||||
|
width: f64,
|
||||||
|
height: f64,
|
||||||
|
) -> Path {
|
||||||
|
context.new_path();
|
||||||
|
let points = hexagon(width, height);
|
||||||
|
context.move_to(translate_x + points[0].0, translate_y + points[0].1);
|
||||||
|
context.line_to(translate_x + points[1].0, translate_y + points[1].1);
|
||||||
|
context.line_to(translate_x + points[2].0, translate_y + points[2].1);
|
||||||
|
context.line_to(translate_x + points[3].0, translate_y + points[3].1);
|
||||||
|
context.line_to(translate_x + points[4].0, translate_y + points[4].1);
|
||||||
|
context.line_to(translate_x + points[5].0, translate_y + points[5].1);
|
||||||
|
context.copy_path().expect("to successfully copy a path")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn hexagon(width: f64, height: f64) -> Vec<(f64, f64)> {
|
||||||
|
let center_x = width / 2.;
|
||||||
|
let center_y = height / 2.;
|
||||||
|
let radius = width / 2.;
|
||||||
|
|
||||||
|
vec![
|
||||||
|
(center_x + radius, center_y),
|
||||||
|
(
|
||||||
|
center_x + radius / 2.,
|
||||||
|
center_y + (3. as f64).sqrt() * radius / 2.,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
center_x - radius / 2.,
|
||||||
|
center_y + (3. as f64).sqrt() * radius / 2.,
|
||||||
|
),
|
||||||
|
(center_x - radius, center_y),
|
||||||
|
(
|
||||||
|
center_x - radius / 2.,
|
||||||
|
center_y - (3. as f64).sqrt() * radius / 2.,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
center_x + radius / 2.,
|
||||||
|
center_y - (3. as f64).sqrt() * radius / 2.,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
|
||||||
|
server-dev:
|
||||||
|
cd server && cargo watch -x run
|
||||||
|
|
||||||
|
server-test:
|
||||||
|
cd server && cargo watch -x test
|
||||||
|
|
||||||
|
client-dev:
|
||||||
|
cd client && npm run watch
|
|
@ -11,6 +11,7 @@
|
||||||
<div class="controls"><button class="play-pause">Pause</button></div>
|
<div class="controls"><button class="play-pause">Pause</button></div>
|
||||||
|
|
||||||
<div class="track-list">
|
<div class="track-list">
|
||||||
|
<!--
|
||||||
<div class="track-list__grouping">
|
<div class="track-list__grouping">
|
||||||
<ul class="bulletless-list">
|
<ul class="bulletless-list">
|
||||||
<li> By Artist </li>
|
<li> By Artist </li>
|
||||||
|
@ -19,61 +20,15 @@
|
||||||
<li> Dance Music </li>
|
<li> Dance Music </li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
-->
|
||||||
|
|
||||||
<div class="track-list__tracks">
|
<div>
|
||||||
<table>
|
<ul class="track-list__tracks bulletless-list">
|
||||||
<thead>
|
</ul>
|
||||||
<tr>
|
|
||||||
<th> Track # </th>
|
|
||||||
<th> Title </th>
|
|
||||||
<th> Artist </th>
|
|
||||||
<th> Album </th>
|
|
||||||
<th> Length </th>
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
|
|
||||||
<tbody>
|
|
||||||
<tr class="track-list__track-row">
|
|
||||||
<td> 1 </td>
|
|
||||||
<td> Underground </td>
|
|
||||||
<td> Lindsey Stirling </td>
|
|
||||||
<td> Artemis </td>
|
|
||||||
<td> 4:24 </td>
|
|
||||||
</tr>
|
|
||||||
<tr class="track-list__track-row">
|
|
||||||
<td> 2 </td>
|
|
||||||
<td> Artemis </td>
|
|
||||||
<td> Lindsey Stirling </td>
|
|
||||||
<td> Artemis </td>
|
|
||||||
<td> 3:54 </td>
|
|
||||||
</tr>
|
|
||||||
<tr class="track-list__track-row">
|
|
||||||
<td> 3 </td>
|
|
||||||
<td> Til the Light Goes Out </td>
|
|
||||||
<td> Lindsey Stirling </td>
|
|
||||||
<td> Artemis </td>
|
|
||||||
<td> 4:46 </td>
|
|
||||||
</tr>
|
|
||||||
<tr class="track-list__track-row">
|
|
||||||
<td> 4 </td>
|
|
||||||
<td> Between Twilight </td>
|
|
||||||
<td> Lindsey Stirling </td>
|
|
||||||
<td> Artemis </td>
|
|
||||||
<td> 4:20 </td>
|
|
||||||
</tr>
|
|
||||||
<tr class="track-list__track-row">
|
|
||||||
<td> 5 </td>
|
|
||||||
<td> Foreverglow </td>
|
|
||||||
<td> Lindsey Stirling </td>
|
|
||||||
<td> Artemis </td>
|
|
||||||
<td> 3:58 </td>
|
|
||||||
</tr>
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script src="./dist/main.js" type="module"></script>
|
<script src="./bundle.js" type="module"></script>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -4,9 +4,9 @@
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "main.js",
|
"main": "main.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "browserify src/main.ts -p [ tsify ] > dist/bundle.js",
|
"build": "webpack",
|
||||||
"test": "echo \"Error: no test specified\" && exit 1",
|
"test": "echo \"Error: no test specified\" && exit 1",
|
||||||
"watch": "exa index.html styles.css src/* | entr -s 'npm run build'"
|
"watch": "webpack --watch"
|
||||||
},
|
},
|
||||||
"author": "Savanni D'Gerinel <savanni@luminescent-dreams.com>",
|
"author": "Savanni D'Gerinel <savanni@luminescent-dreams.com>",
|
||||||
"license": "GPL-3.0-or-later",
|
"license": "GPL-3.0-or-later",
|
||||||
|
@ -16,11 +16,12 @@
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/lodash": "^4.14.191",
|
"@types/lodash": "^4.14.191",
|
||||||
"babelify": "^10.0.0",
|
"copy-webpack-plugin": "^11.0.0",
|
||||||
"browserify": "^17.0.0",
|
"css-loader": "^6.7.3",
|
||||||
"live-server": "^1.2.2",
|
"style-loader": "^3.3.1",
|
||||||
"tsify": "^5.0.4",
|
"ts-loader": "^9.4.2",
|
||||||
"typescript": "^4.9.4",
|
"typescript": "^4.9.5",
|
||||||
"watchify": "^4.0.0"
|
"webpack": "^5.75.0",
|
||||||
|
"webpack-cli": "^5.0.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,133 @@
|
||||||
|
import { TrackInfo } from "../client";
|
||||||
|
|
||||||
|
export class TrackName extends HTMLElement {
|
||||||
|
container: HTMLElement;
|
||||||
|
|
||||||
|
static get observedAttributes() {
|
||||||
|
return ["name"];
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
this.container = document.createElement("div");
|
||||||
|
}
|
||||||
|
|
||||||
|
get name(): string | null {
|
||||||
|
return this.getAttribute("name");
|
||||||
|
}
|
||||||
|
|
||||||
|
set name(name: string | null) {
|
||||||
|
while (this.container.lastChild) {
|
||||||
|
this.container.removeChild(this.container.lastChild);
|
||||||
|
}
|
||||||
|
if (name) {
|
||||||
|
this.setAttribute("name", name);
|
||||||
|
this.container.appendChild(document.createTextNode(name));
|
||||||
|
} else {
|
||||||
|
this.removeAttribute("name");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
connectedCallback() {
|
||||||
|
this.appendChild(this.container);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class TrackCard extends HTMLElement {
|
||||||
|
static get observedAttributes() {
|
||||||
|
return ["id", "trackNumber", "name", "album", "artist"];
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
attributeChangeCallback(
|
||||||
|
attrName: string,
|
||||||
|
oldValue: string,
|
||||||
|
newValue: string
|
||||||
|
): void {
|
||||||
|
if (newValue !== oldValue) {
|
||||||
|
this.updateContent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
get name(): string | null {
|
||||||
|
return this.getAttribute("name");
|
||||||
|
}
|
||||||
|
|
||||||
|
set name(name: string | null) {
|
||||||
|
if (name) {
|
||||||
|
this.setAttribute("name", name);
|
||||||
|
} else {
|
||||||
|
this.removeAttribute("open");
|
||||||
|
}
|
||||||
|
this.updateContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
get artist(): string | null {
|
||||||
|
return this.getAttribute("artist");
|
||||||
|
}
|
||||||
|
|
||||||
|
set artist(artist: string | null) {
|
||||||
|
if (artist) {
|
||||||
|
this.setAttribute("artist", artist);
|
||||||
|
} else {
|
||||||
|
this.removeAttribute("open");
|
||||||
|
}
|
||||||
|
this.updateContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
get album(): string | null {
|
||||||
|
return this.getAttribute("album");
|
||||||
|
}
|
||||||
|
|
||||||
|
set album(album: string | null) {
|
||||||
|
if (album) {
|
||||||
|
this.setAttribute("album", album);
|
||||||
|
} else {
|
||||||
|
this.removeAttribute("open");
|
||||||
|
}
|
||||||
|
this.updateContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
get length(): string | null {
|
||||||
|
return this.getAttribute("length");
|
||||||
|
}
|
||||||
|
|
||||||
|
set length(length: string | null) {
|
||||||
|
if (length) {
|
||||||
|
this.setAttribute("length", length);
|
||||||
|
} else {
|
||||||
|
this.removeAttribute("open");
|
||||||
|
}
|
||||||
|
this.updateContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
connectedCallback() {
|
||||||
|
this.updateContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
updateContent() {
|
||||||
|
const container = document.createElement("div");
|
||||||
|
container.classList.add("track-card");
|
||||||
|
this.innerHTML = "";
|
||||||
|
|
||||||
|
this.appendChild(container);
|
||||||
|
|
||||||
|
while (container.lastChild) {
|
||||||
|
container.removeChild(container.lastChild);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this["name"]) {
|
||||||
|
const trackName = document.createElement("track-name");
|
||||||
|
trackName.name = this["name"];
|
||||||
|
container.appendChild(trackName);
|
||||||
|
}
|
||||||
|
this["length"] && container.appendChild(document.createTextNode("1:23"));
|
||||||
|
this["album"] &&
|
||||||
|
container.appendChild(document.createTextNode("Shatter Me"));
|
||||||
|
this["artist"] &&
|
||||||
|
container.appendChild(document.createTextNode("Lindsey Stirling"));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
export interface TrackInfo {
|
||||||
|
id: string;
|
||||||
|
track_number?: number;
|
||||||
|
name?: string;
|
||||||
|
album?: string;
|
||||||
|
artist?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getTracks = (): Promise<TrackInfo[]> =>
|
||||||
|
fetch("/api/v1/tracks").then((r) => r.json());
|
|
@ -1,4 +1,16 @@
|
||||||
import * as _ from "lodash";
|
import * as _ from "lodash";
|
||||||
|
import { TrackInfo, getTracks } from "./client";
|
||||||
|
import { TrackName, TrackCard } from "./blocks/track";
|
||||||
|
|
||||||
|
window.customElements.define("track-name", TrackName);
|
||||||
|
window.customElements.define("track-card", TrackCard);
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
interface HTMLElementTagNameMap {
|
||||||
|
"track-name": TrackName;
|
||||||
|
"track-card": TrackCard;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const replaceTitle = () => {
|
const replaceTitle = () => {
|
||||||
const title = document.querySelector(".js-title");
|
const title = document.querySelector(".js-title");
|
||||||
|
@ -7,21 +19,27 @@ const replaceTitle = () => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
const updateTrackList = (tracks: TrackInfo[]) => {
|
||||||
const checkWeatherService = () => {
|
const track_list = document.querySelector(".track-list__tracks");
|
||||||
fetch("https://api.weather.gov/")
|
if (track_list) {
|
||||||
.then((r) => r.json())
|
let track_formats = _.map(tracks, (info) => {
|
||||||
.then((js) => {
|
let card: TrackCard = document.createElement("track-card");
|
||||||
const weather = document.querySelector('.js-weather');
|
card.name = info.name || null;
|
||||||
weather.innerHTML = js.status;
|
return card;
|
||||||
});
|
});
|
||||||
|
_.map(track_formats, (trackCard) => {
|
||||||
|
let listItem = document.createElement("li");
|
||||||
|
listItem.classList.add("track-list__row");
|
||||||
|
listItem.appendChild(trackCard);
|
||||||
|
track_list.appendChild(listItem);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
console.log("track_list does not exist");
|
||||||
}
|
}
|
||||||
*/
|
};
|
||||||
|
|
||||||
const run = () => {
|
const run = () => {
|
||||||
replaceTitle();
|
getTracks().then((tracks) => updateTrackList(tracks));
|
||||||
console.log(_.map([4, 8], (x) => x * x));
|
|
||||||
// checkWeatherService();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
run();
|
run();
|
||||||
|
|
|
@ -25,15 +25,37 @@ body {
|
||||||
list-style: none;
|
list-style: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
.track-list__track-row {
|
.track-list__row {
|
||||||
background-color: rgb(10, 10, 10);
|
margin-top: 32px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.track-list__track-row:nth-child(even) {
|
/*
|
||||||
|
.track-list__row:nth-child(even) {
|
||||||
background-color: rgb(255, 255, 255);
|
background-color: rgb(255, 255, 255);
|
||||||
}
|
}
|
||||||
|
|
||||||
.track-list__track-row:nth-child(odd) {
|
.track-list__row:nth-child(odd) {
|
||||||
background-color: rgb(200, 200, 200);
|
background-color: rgb(200, 200, 200);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
.track-card {
|
||||||
|
border: 1px solid black;
|
||||||
|
border-radius: 5px;
|
||||||
|
padding: 8px;
|
||||||
|
width: 300px;
|
||||||
|
height: 100px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.track-card__name {
|
||||||
|
}
|
||||||
|
|
||||||
|
.track-card__length {
|
||||||
|
}
|
||||||
|
|
||||||
|
.track-card__album {
|
||||||
|
}
|
||||||
|
|
||||||
|
.track-card__artist {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,9 @@
|
||||||
"rootDir": "src",
|
"rootDir": "src",
|
||||||
"outDir": "./dist",
|
"outDir": "./dist",
|
||||||
"lib": ["es2016", "DOM"],
|
"lib": ["es2016", "DOM"],
|
||||||
"sourceMap": true
|
"sourceMap": true,
|
||||||
}
|
"strict": true,
|
||||||
|
"noImplicitAny": true
|
||||||
|
},
|
||||||
|
"include": ["src"]
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
const path = require('path');
|
||||||
|
const CopyPlugin = require('copy-webpack-plugin');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
mode: 'development',
|
||||||
|
entry: './src/main.ts',
|
||||||
|
module: {
|
||||||
|
rules: [
|
||||||
|
{
|
||||||
|
test: /\.tsx?$/,
|
||||||
|
use: 'ts-loader',
|
||||||
|
exclude: /node_modules/,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
test: /\.html$/i,
|
||||||
|
type: 'asset/resource',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
test: /\.css$/i,
|
||||||
|
use: ['style-loader', 'css-loader'],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
resolve: {
|
||||||
|
extensions: ['.tsx', '.ts', '.js'],
|
||||||
|
},
|
||||||
|
plugins: [
|
||||||
|
new CopyPlugin({
|
||||||
|
patterns: [
|
||||||
|
{ from: "index.html", to: "index.html" },
|
||||||
|
{ from: "styles.css", to: "styles.css" },
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
output: {
|
||||||
|
filename: 'bundle.js',
|
||||||
|
path: path.resolve(__dirname, 'dist'),
|
||||||
|
},
|
||||||
|
};
|
|
@ -583,6 +583,7 @@ version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"dbus",
|
"dbus",
|
||||||
"flow",
|
"flow",
|
||||||
|
"mime_guess",
|
||||||
"mpris",
|
"mpris",
|
||||||
"rusqlite",
|
"rusqlite",
|
||||||
"serde",
|
"serde",
|
||||||
|
|
|
@ -8,6 +8,7 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
dbus = { version = "0.9.7" }
|
dbus = { version = "0.9.7" }
|
||||||
flow = { path = "../../flow" }
|
flow = { path = "../../flow" }
|
||||||
|
mime_guess = "2.0.4"
|
||||||
mpris = { version = "2.0" }
|
mpris = { version = "2.0" }
|
||||||
rusqlite = { version = "0.28" }
|
rusqlite = { version = "0.28" }
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
|
|
@ -10,15 +10,8 @@ Luminescent Dreams Tools is distributed in the hope that it will be useful, but
|
||||||
You should have received a copy of the GNU General Public License along with Lumeto. If not, see <https://www.gnu.org/licenses/>.
|
You should have received a copy of the GNU General Public License along with Lumeto. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use dbus::ffidisp::Connection;
|
|
||||||
use mpris::{FindingError, PlaybackStatus, Player, PlayerFinder, ProgressTick};
|
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use std::{
|
use std::time::Duration;
|
||||||
path::PathBuf,
|
|
||||||
sync::mpsc::{channel, Receiver, Sender, TryRecvError},
|
|
||||||
thread,
|
|
||||||
time::Duration,
|
|
||||||
};
|
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
pub enum Message {
|
pub enum Message {
|
||||||
|
@ -27,10 +20,10 @@ pub enum Message {
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum Event {
|
pub enum Event {
|
||||||
Paused(Track, Duration),
|
Paused(TrackId, Duration),
|
||||||
Playing(Track, Duration),
|
Playing(TrackId, Duration),
|
||||||
Stopped,
|
Stopped,
|
||||||
Position(Track, Duration),
|
Position(TrackId, Duration),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -39,17 +32,6 @@ pub struct DeviceInformation {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn list_devices(conn: Connection) -> Result<Vec<DeviceInformation>, FindingError> {
|
|
||||||
Ok(PlayerFinder::for_connection(conn)
|
|
||||||
.find_all()?
|
|
||||||
.into_iter()
|
|
||||||
.map(|player| DeviceInformation {
|
|
||||||
address: player.unique_name().to_owned(),
|
|
||||||
name: player.identity().to_owned(),
|
|
||||||
})
|
|
||||||
.collect())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Error)]
|
#[derive(Debug, Error)]
|
||||||
pub enum AudioError {
|
pub enum AudioError {
|
||||||
#[error("DBus device was not found")]
|
#[error("DBus device was not found")]
|
||||||
|
@ -113,17 +95,8 @@ impl AsRef<String> for TrackId {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
pub struct TrackInfo {
|
|
||||||
pub track_number: Option<i32>,
|
|
||||||
pub name: Option<String>,
|
|
||||||
pub album: Option<String>,
|
|
||||||
pub artist: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Serialize)]
|
#[derive(Clone, Debug, PartialEq, Serialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
pub struct TrackInfo {
|
||||||
pub struct Track {
|
|
||||||
pub id: TrackId,
|
pub id: TrackId,
|
||||||
pub track_number: Option<i32>,
|
pub track_number: Option<i32>,
|
||||||
pub name: Option<String>,
|
pub name: Option<String>,
|
||||||
|
@ -131,38 +104,20 @@ pub struct Track {
|
||||||
pub artist: Option<String>,
|
pub artist: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
impl From<&mpris::Metadata> for Track {
|
|
||||||
fn from(data: &mpris::Metadata) -> Self {
|
|
||||||
Self {
|
|
||||||
id: data.track_id().unwrap(),
|
|
||||||
track_number: data.track_number(),
|
|
||||||
name: data.title().map(|s| s.to_owned()),
|
|
||||||
album: data.album_name().map(|s| s.to_owned()),
|
|
||||||
artist: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<mpris::Metadata> for Track {
|
|
||||||
fn from(data: mpris::Metadata) -> Self {
|
|
||||||
Self::from(&data)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize)]
|
#[derive(Clone, Debug, Serialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub enum State {
|
pub enum State {
|
||||||
Playing(Track),
|
Playing(TrackInfo),
|
||||||
Paused(Track),
|
Paused(TrackInfo),
|
||||||
Stopped,
|
Stopped,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
pub struct CurrentlyPlaying {
|
pub struct CurrentlyPlaying {
|
||||||
track: Track,
|
track: TrackInfo,
|
||||||
position: Duration,
|
position: Duration,
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize)]
|
#[derive(Clone, Debug, Serialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
|
@ -180,6 +135,7 @@ pub trait AudioPlayer {
|
||||||
fn play_pause(&self) -> Result<State, AudioError>;
|
fn play_pause(&self) -> Result<State, AudioError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
pub struct GStreamerPlayer {
|
pub struct GStreamerPlayer {
|
||||||
url: url::Url,
|
url: url::Url,
|
||||||
}
|
}
|
||||||
|
@ -201,6 +157,7 @@ impl AudioPlayer for GStreamerPlayer {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
pub struct MprisDevice {
|
pub struct MprisDevice {
|
||||||
|
|
|
@ -1,72 +1,92 @@
|
||||||
use flow::Flow;
|
use flow::Flow;
|
||||||
use std::{io::stdin, path::PathBuf, sync::Arc, thread, time::Duration};
|
use std::{
|
||||||
// use warp::Filter;
|
net::{IpAddr, Ipv4Addr, SocketAddr},
|
||||||
|
path::PathBuf,
|
||||||
|
sync::Arc,
|
||||||
|
};
|
||||||
|
use warp::Filter;
|
||||||
|
|
||||||
use music_player::{core::Core, database::MemoryIndex};
|
use music_player::{
|
||||||
|
audio::TrackInfo,
|
||||||
|
core::Core,
|
||||||
|
database::{MemoryIndex, MusicIndex},
|
||||||
|
music_scanner::FileScanner,
|
||||||
|
};
|
||||||
|
|
||||||
/*
|
fn tracks(index: &Arc<impl MusicIndex>) -> Vec<TrackInfo> {
|
||||||
fn tracks() -> Vec<Track> {
|
match index.list_tracks() {
|
||||||
vec![
|
Flow::Ok(tracks) => tracks,
|
||||||
Track {
|
Flow::Err(err) => panic!("error: {}", err),
|
||||||
track_number: Some(1),
|
Flow::Fatal(err) => panic!("fatal: {}", err),
|
||||||
name: Some("Underground".to_owned()),
|
|
||||||
album: Some("Artemis".to_owned()),
|
|
||||||
artist: Some("Lindsey Stirling".to_owned()),
|
|
||||||
path: PathBuf::from("/mnt/music/Lindsey Stirling/Artemis/01 - Underground.ogg"),
|
|
||||||
},
|
|
||||||
Track {
|
|
||||||
track_number: Some(2),
|
|
||||||
name: Some("Artemis".to_owned()),
|
|
||||||
album: Some("Artemis".to_owned()),
|
|
||||||
artist: Some("Lindsey Stirling".to_owned()),
|
|
||||||
path: PathBuf::from("/mnt/music/Lindsey Stirling/Artemis/02 - Artemis.ogg"),
|
|
||||||
},
|
|
||||||
Track {
|
|
||||||
track_number: Some(3),
|
|
||||||
name: Some("Til the Light Goes Out".to_owned()),
|
|
||||||
album: Some("Artemis".to_owned()),
|
|
||||||
artist: Some("Lindsey Stirling".to_owned()),
|
|
||||||
path: PathBuf::from(
|
|
||||||
"/mnt/music/Lindsey Stirling/Artemis/03 - Til the Light Goes Out.ogg",
|
|
||||||
),
|
|
||||||
},
|
|
||||||
Track {
|
|
||||||
track_number: Some(4),
|
|
||||||
name: Some("Between Twilight".to_owned()),
|
|
||||||
album: Some("Artemis".to_owned()),
|
|
||||||
artist: Some("Lindsey Stirling".to_owned()),
|
|
||||||
path: PathBuf::from("/mnt/music/Lindsey Stirling/Artemis/04 - Between Twilight.ogg"),
|
|
||||||
},
|
|
||||||
Track {
|
|
||||||
track_number: Some(5),
|
|
||||||
name: Some("Foreverglow".to_owned()),
|
|
||||||
album: Some("Artemis".to_owned()),
|
|
||||||
artist: Some("Lindsey Stirling".to_owned()),
|
|
||||||
path: PathBuf::from("/mnt/music/Lindsey Stirling/Artemis/05 - Foreverglow.ogg"),
|
|
||||||
},
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Static(PathBuf);
|
||||||
|
|
||||||
|
impl Static {
|
||||||
|
fn read(self, root: PathBuf) -> String {
|
||||||
|
/*
|
||||||
|
let mut path = root;
|
||||||
|
match self {
|
||||||
|
Bundle::Index => path.push(PathBuf::from("index.html")),
|
||||||
|
Bundle::App => path.push(PathBuf::from("bundle.js")),
|
||||||
|
Bundle::Styles => path.push(PathBuf::from("styles.css")),
|
||||||
|
};
|
||||||
|
std::fs::read_to_string(path).expect("to find the file")
|
||||||
*/
|
*/
|
||||||
|
let mut path = root;
|
||||||
|
path.push(self.0);
|
||||||
|
println!("path: {:?}", path);
|
||||||
|
std::fs::read_to_string(path).expect("to find the file")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
pub async fn main() {
|
pub async fn main() {
|
||||||
match Core::new(Arc::new(MemoryIndex::new())) {
|
let dev = std::env::var("DEV")
|
||||||
Flow::Ok(core) => {
|
.ok()
|
||||||
let mut buf = String::new();
|
.and_then(|v| v.parse::<bool>().ok())
|
||||||
let _ = stdin().read_line(&mut buf).unwrap();
|
.unwrap_or(false);
|
||||||
core.exit();
|
let bundle_root = std::env::var("BUNDLE_ROOT")
|
||||||
}
|
.map(|b| PathBuf::from(b))
|
||||||
Flow::Err(err) => println!("non-fatal error: {:?}", err),
|
.unwrap();
|
||||||
Flow::Fatal(err) => println!("fatal error: {:?}", err),
|
let music_root = std::env::var("MUSIC_ROOT")
|
||||||
}
|
.map(|b| PathBuf::from(b))
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
/*
|
let index = Arc::new(MemoryIndex::new());
|
||||||
let connection = Connection::new_session().expect("to connect to dbus");
|
let scanner = FileScanner::new(vec![music_root.clone()]);
|
||||||
|
let _core = match Core::new(index.clone(), scanner) {
|
||||||
|
Flow::Ok(core) => core,
|
||||||
|
Flow::Err(error) => panic!("error: {}", error),
|
||||||
|
Flow::Fatal(error) => panic!("fatal: {}", error),
|
||||||
|
};
|
||||||
|
|
||||||
for player in list_players(connection) {
|
println!("config: {:?} {:?} {:?}", dev, bundle_root, music_root);
|
||||||
println!("player found: {}", player.identity());
|
|
||||||
|
let root = warp::path!().and(warp::get()).map({
|
||||||
|
let bundle_root = bundle_root.clone();
|
||||||
|
move || {
|
||||||
|
warp::http::Response::builder()
|
||||||
|
.header("content-type", "text/html")
|
||||||
|
.body(Static(PathBuf::from("index.html")).read(bundle_root.clone()))
|
||||||
}
|
}
|
||||||
*/
|
});
|
||||||
|
let assets = warp::path!(String).and(warp::get()).map({
|
||||||
|
let bundle_root = bundle_root.clone();
|
||||||
|
move |filename: String| {
|
||||||
|
let mime_type = mime_guess::from_path(filename.clone())
|
||||||
|
.first()
|
||||||
|
.map(|m| m.essence_str().to_owned())
|
||||||
|
.unwrap_or("text/plain".to_owned());
|
||||||
|
println!("mime_type: {:?}", mime_type);
|
||||||
|
// let mut path = PathBuf::from("assets");
|
||||||
|
// path.push(filename);
|
||||||
|
warp::http::Response::builder()
|
||||||
|
.header("content-type", mime_type)
|
||||||
|
.body(Static(PathBuf::from(filename)).read(bundle_root.clone()))
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
/*
|
/*
|
||||||
let devices = warp::path!("api" / "v1" / "devices")
|
let devices = warp::path!("api" / "v1" / "devices")
|
||||||
|
@ -75,10 +95,14 @@ pub async fn main() {
|
||||||
let conn = Connection::new_session().expect("to connect to dbus");
|
let conn = Connection::new_session().expect("to connect to dbus");
|
||||||
warp::reply::json(&list_devices(conn))
|
warp::reply::json(&list_devices(conn))
|
||||||
});
|
});
|
||||||
|
*/
|
||||||
|
|
||||||
let track_list = warp::path!("api" / "v1" / "tracks")
|
let track_list = warp::path!("api" / "v1" / "tracks").and(warp::get()).map({
|
||||||
.and(warp::get())
|
let index = index.clone();
|
||||||
.map(|| warp::reply::json(&tracks()));
|
move || warp::reply::json(&tracks(&index))
|
||||||
|
});
|
||||||
|
|
||||||
|
/*
|
||||||
let tracks_for_artist = warp::path!("api" / "v1" / "artist" / String)
|
let tracks_for_artist = warp::path!("api" / "v1" / "artist" / String)
|
||||||
.and(warp::get())
|
.and(warp::get())
|
||||||
.map(|_artist: String| warp::reply::json(&tracks()));
|
.map(|_artist: String| warp::reply::json(&tracks()));
|
||||||
|
@ -103,12 +127,10 @@ pub async fn main() {
|
||||||
.or(tracks_for_artist)
|
.or(tracks_for_artist)
|
||||||
.or(queue)
|
.or(queue)
|
||||||
.or(playing_status);
|
.or(playing_status);
|
||||||
|
*/
|
||||||
|
let routes = root.or(assets).or(track_list);
|
||||||
let server = warp::serve(routes);
|
let server = warp::serve(routes);
|
||||||
server
|
server
|
||||||
.run(SocketAddr::new(
|
.run(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 8002))
|
||||||
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
|
|
||||||
8002,
|
|
||||||
))
|
|
||||||
.await;
|
.await;
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
database::{Database, MemoryIndex, MusicIndex},
|
audio::TrackInfo, database::MusicIndex, music_scanner::MusicScanner, Error, FatalError,
|
||||||
Error, FatalError,
|
|
||||||
};
|
};
|
||||||
use flow::{error, fatal, ok, return_error, return_fatal, Flow};
|
use flow::{ok, Flow};
|
||||||
use std::{
|
use std::{
|
||||||
path::{Path, PathBuf},
|
|
||||||
sync::{
|
sync::{
|
||||||
mpsc::{channel, Receiver, RecvTimeoutError, Sender},
|
mpsc::{channel, Receiver, RecvTimeoutError, Sender},
|
||||||
Arc,
|
Arc,
|
||||||
|
@ -14,18 +12,8 @@ use std::{
|
||||||
time::{Duration, Instant},
|
time::{Duration, Instant},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Error)]
|
fn scan_frequency() -> Duration {
|
||||||
pub enum ScannerError {
|
Duration::from_secs(60)
|
||||||
#[error("Cannot scan {0}")]
|
|
||||||
CannotScan(PathBuf),
|
|
||||||
#[error("IO error {0}")]
|
|
||||||
IO(std::io::Error),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<std::io::Error> for ScannerError {
|
|
||||||
fn from(err: std::io::Error) -> Self {
|
|
||||||
Self::IO(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum ControlMsg {
|
pub enum ControlMsg {
|
||||||
|
@ -33,7 +21,8 @@ pub enum ControlMsg {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum TrackMsg {
|
pub enum TrackMsg {
|
||||||
DbUpdate,
|
UpdateInProgress,
|
||||||
|
UpdateComplete,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum PlaybackMsg {
|
pub enum PlaybackMsg {
|
||||||
|
@ -45,129 +34,68 @@ pub enum PlaybackMsg {
|
||||||
|
|
||||||
pub struct Core {
|
pub struct Core {
|
||||||
db: Arc<dyn MusicIndex>,
|
db: Arc<dyn MusicIndex>,
|
||||||
track_handle: JoinHandle<()>,
|
_track_handle: JoinHandle<()>,
|
||||||
track_rx: Receiver<TrackMsg>,
|
_track_rx: Receiver<TrackMsg>,
|
||||||
playback_handle: JoinHandle<()>,
|
_playback_handle: JoinHandle<()>,
|
||||||
playback_rx: Receiver<PlaybackMsg>,
|
_playback_rx: Receiver<PlaybackMsg>,
|
||||||
control_tx: Sender<ControlMsg>,
|
control_tx: Sender<ControlMsg>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn scan_frequency() -> Duration {
|
impl Core {
|
||||||
Duration::from_secs(60)
|
pub fn new(
|
||||||
}
|
|
||||||
|
|
||||||
pub struct FileScanner {
|
|
||||||
db: Arc<dyn MusicIndex>,
|
db: Arc<dyn MusicIndex>,
|
||||||
control_rx: Receiver<ControlMsg>,
|
scanner: impl MusicScanner + 'static,
|
||||||
tracker_tx: Sender<TrackMsg>,
|
) -> Flow<Core, FatalError, Error> {
|
||||||
next_scan: Instant,
|
let (control_tx, control_rx) = channel::<ControlMsg>();
|
||||||
music_directories: Vec<PathBuf>,
|
let db = db;
|
||||||
}
|
|
||||||
|
|
||||||
impl FileScanner {
|
let (_track_handle, _track_rx) = {
|
||||||
fn new(
|
let (track_tx, track_rx) = channel();
|
||||||
db: Arc<dyn MusicIndex>,
|
let db = db.clone();
|
||||||
roots: Vec<PathBuf>,
|
let track_handle = thread::spawn(move || {
|
||||||
control_rx: Receiver<ControlMsg>,
|
let mut next_scan = Instant::now();
|
||||||
tracker_tx: Sender<TrackMsg>,
|
|
||||||
) -> Self {
|
|
||||||
Self {
|
|
||||||
db,
|
|
||||||
control_rx,
|
|
||||||
tracker_tx,
|
|
||||||
next_scan: Instant::now(),
|
|
||||||
music_directories: roots,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn scan(&mut self) {
|
|
||||||
loop {
|
loop {
|
||||||
match self.control_rx.recv_timeout(Duration::from_millis(100)) {
|
if Instant::now() >= next_scan {
|
||||||
|
let _ = track_tx.send(TrackMsg::UpdateInProgress);
|
||||||
|
for track in scanner.scan() {
|
||||||
|
match track {
|
||||||
|
Ok(track) => db.add_track(track),
|
||||||
|
Err(_) => ok(()),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
let _ = track_tx.send(TrackMsg::UpdateComplete);
|
||||||
|
next_scan = Instant::now() + scan_frequency();
|
||||||
|
}
|
||||||
|
match control_rx.recv_timeout(Duration::from_millis(1000)) {
|
||||||
Ok(ControlMsg::Exit) => return,
|
Ok(ControlMsg::Exit) => return,
|
||||||
Err(RecvTimeoutError::Timeout) => (),
|
Err(RecvTimeoutError::Timeout) => (),
|
||||||
Err(RecvTimeoutError::Disconnected) => return,
|
Err(RecvTimeoutError::Disconnected) => return,
|
||||||
}
|
}
|
||||||
if Instant::now() >= self.next_scan {
|
|
||||||
for root in self.music_directories.iter() {
|
|
||||||
self.scan_dir(vec![root.clone()]);
|
|
||||||
}
|
}
|
||||||
self.next_scan = Instant::now() + scan_frequency();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn scan_dir(&self, mut paths: Vec<PathBuf>) -> Flow<(), FatalError, ScannerError> {
|
|
||||||
while let Some(dir) = paths.pop() {
|
|
||||||
println!("scanning {:?}", dir);
|
|
||||||
return_error!(self.scan_dir_(&mut paths, dir));
|
|
||||||
}
|
|
||||||
ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn scan_dir_(
|
|
||||||
&self,
|
|
||||||
paths: &mut Vec<PathBuf>,
|
|
||||||
dir: PathBuf,
|
|
||||||
) -> Flow<(), FatalError, ScannerError> {
|
|
||||||
let dir_iter = return_error!(Flow::from(dir.read_dir().map_err(ScannerError::from)));
|
|
||||||
for entry in dir_iter {
|
|
||||||
match entry {
|
|
||||||
Ok(entry) if entry.path().is_dir() => paths.push(entry.path()),
|
|
||||||
Ok(entry) => {
|
|
||||||
let _ = return_fatal!(self.scan_file(entry.path()).or_else(|err| {
|
|
||||||
println!("scan_file failed: {:?}", err);
|
|
||||||
ok::<(), FatalError, ScannerError>(())
|
|
||||||
}));
|
|
||||||
()
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
println!("scan_dir could not read path: ({:?})", err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn scan_file(&self, path: PathBuf) -> Flow<(), FatalError, ScannerError> {
|
|
||||||
ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Core {
|
|
||||||
pub fn new(db: Arc<dyn MusicIndex>) -> Flow<Core, FatalError, Error> {
|
|
||||||
let (control_tx, control_rx) = channel::<ControlMsg>();
|
|
||||||
|
|
||||||
let (track_handle, track_rx) = {
|
|
||||||
let (track_tx, track_rx) = channel();
|
|
||||||
let db = db.clone();
|
|
||||||
let track_handle = thread::spawn(move || {
|
|
||||||
FileScanner::new(
|
|
||||||
db,
|
|
||||||
vec![PathBuf::from("/home/savanni/Music/")],
|
|
||||||
control_rx,
|
|
||||||
track_tx,
|
|
||||||
)
|
|
||||||
.scan();
|
|
||||||
});
|
});
|
||||||
(track_handle, track_rx)
|
(track_handle, track_rx)
|
||||||
};
|
};
|
||||||
|
|
||||||
let (playback_handle, playback_rx) = {
|
let (_playback_handle, _playback_rx) = {
|
||||||
let (playback_tx, playback_rx) = channel();
|
let (_playback_tx, playback_rx) = channel();
|
||||||
let playback_handle = thread::spawn(move || {});
|
let playback_handle = thread::spawn(move || {});
|
||||||
(playback_handle, playback_rx)
|
(playback_handle, playback_rx)
|
||||||
};
|
};
|
||||||
|
|
||||||
ok(Core {
|
ok(Core {
|
||||||
db,
|
db,
|
||||||
track_handle,
|
_track_handle,
|
||||||
track_rx,
|
_track_rx,
|
||||||
playback_handle,
|
_playback_handle,
|
||||||
playback_rx,
|
_playback_rx,
|
||||||
control_tx,
|
control_tx,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn list_tracks<'a>(&'a self) -> Flow<Vec<TrackInfo>, FatalError, Error> {
|
||||||
|
self.db.list_tracks().map_err(Error::DatabaseError)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn exit(&self) {
|
pub fn exit(&self) {
|
||||||
let _ = self.control_tx.send(ControlMsg::Exit);
|
let _ = self.control_tx.send(ControlMsg::Exit);
|
||||||
/*
|
/*
|
||||||
|
@ -178,4 +106,49 @@ impl Core {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {}
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
use crate::{audio::TrackId, database::MemoryIndex, music_scanner::factories::MockScanner};
|
||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
|
fn with_example_index<F>(f: F)
|
||||||
|
where
|
||||||
|
F: Fn(Core),
|
||||||
|
{
|
||||||
|
let index = MemoryIndex::new();
|
||||||
|
let scanner = MockScanner::new();
|
||||||
|
match Core::new(Arc::new(index), scanner) {
|
||||||
|
Flow::Ok(core) => {
|
||||||
|
thread::sleep(Duration::from_millis(10));
|
||||||
|
f(core)
|
||||||
|
}
|
||||||
|
Flow::Err(error) => panic!("{:?}", error),
|
||||||
|
Flow::Fatal(error) => panic!("{:?}", error),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn it_lists_tracks() {
|
||||||
|
with_example_index(|core| match core.list_tracks() {
|
||||||
|
Flow::Ok(tracks) => {
|
||||||
|
let track_ids = tracks
|
||||||
|
.iter()
|
||||||
|
.map(|t| t.id.clone())
|
||||||
|
.collect::<HashSet<TrackId>>();
|
||||||
|
assert_eq!(track_ids.len(), 5);
|
||||||
|
assert_eq!(
|
||||||
|
track_ids,
|
||||||
|
HashSet::from([
|
||||||
|
TrackId::from("/home/savanni/Track 1.mp3".to_owned()),
|
||||||
|
TrackId::from("/home/savanni/Track 2.mp3".to_owned()),
|
||||||
|
TrackId::from("/home/savanni/Track 3.mp3".to_owned()),
|
||||||
|
TrackId::from("/home/savanni/Track 4.mp3".to_owned()),
|
||||||
|
TrackId::from("/home/savanni/Track 5.mp3".to_owned()),
|
||||||
|
])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Flow::Fatal(err) => panic!("fatal error: {:?}", err),
|
||||||
|
Flow::Err(err) => panic!("error: {:?}", err),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
audio::{Track, TrackId, TrackInfo},
|
audio::{TrackId, TrackInfo},
|
||||||
FatalError,
|
FatalError,
|
||||||
};
|
};
|
||||||
use flow::{error, ok, Flow};
|
use flow::{error, ok, Flow};
|
||||||
|
@ -11,7 +11,7 @@ use std::{
|
||||||
};
|
};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
#[derive(Debug, Error)]
|
#[derive(Debug, Error, PartialEq)]
|
||||||
pub enum DatabaseError {
|
pub enum DatabaseError {
|
||||||
#[error("database is unreadable")]
|
#[error("database is unreadable")]
|
||||||
DatabaseUnreadable,
|
DatabaseUnreadable,
|
||||||
|
@ -20,13 +20,14 @@ pub enum DatabaseError {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait MusicIndex: Sync + Send {
|
pub trait MusicIndex: Sync + Send {
|
||||||
fn add_track(&mut self, track: &TrackInfo) -> Flow<Track, FatalError, DatabaseError>;
|
fn add_track(&self, track: TrackInfo) -> Flow<(), FatalError, DatabaseError>;
|
||||||
fn remove_track(&mut self, id: &TrackId) -> Flow<(), FatalError, DatabaseError>;
|
fn remove_track(&self, id: &TrackId) -> Flow<(), FatalError, DatabaseError>;
|
||||||
fn get_track_info(&self, id: &TrackId) -> Flow<Option<Track>, FatalError, DatabaseError>;
|
fn get_track_info(&self, id: &TrackId) -> Flow<Option<TrackInfo>, FatalError, DatabaseError>;
|
||||||
|
fn list_tracks<'a>(&'a self) -> Flow<Vec<TrackInfo>, FatalError, DatabaseError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct MemoryIndex {
|
pub struct MemoryIndex {
|
||||||
tracks: RwLock<HashMap<TrackId, Track>>,
|
tracks: RwLock<HashMap<TrackId, TrackInfo>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MemoryIndex {
|
impl MemoryIndex {
|
||||||
|
@ -38,21 +39,13 @@ impl MemoryIndex {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MusicIndex for MemoryIndex {
|
impl MusicIndex for MemoryIndex {
|
||||||
fn add_track(&mut self, info: &TrackInfo) -> Flow<Track, FatalError, DatabaseError> {
|
fn add_track(&self, info: TrackInfo) -> Flow<(), FatalError, DatabaseError> {
|
||||||
let id = TrackId::default();
|
|
||||||
let track = Track {
|
|
||||||
id: id.clone(),
|
|
||||||
track_number: info.track_number,
|
|
||||||
name: info.name.clone(),
|
|
||||||
album: info.album.clone(),
|
|
||||||
artist: info.artist.clone(),
|
|
||||||
};
|
|
||||||
let mut tracks = self.tracks.write().unwrap();
|
let mut tracks = self.tracks.write().unwrap();
|
||||||
tracks.insert(id, track.clone());
|
tracks.insert(info.id.clone(), info);
|
||||||
ok(track)
|
ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_track(&mut self, id: &TrackId) -> Flow<(), FatalError, DatabaseError> {
|
fn remove_track(&self, id: &TrackId) -> Flow<(), FatalError, DatabaseError> {
|
||||||
let mut tracks = self.tracks.write().unwrap();
|
let mut tracks = self.tracks.write().unwrap();
|
||||||
tracks.remove(&id);
|
tracks.remove(&id);
|
||||||
ok(())
|
ok(())
|
||||||
|
@ -61,13 +54,23 @@ impl MusicIndex for MemoryIndex {
|
||||||
fn get_track_info<'a>(
|
fn get_track_info<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
id: &TrackId,
|
id: &TrackId,
|
||||||
) -> Flow<Option<Track>, FatalError, DatabaseError> {
|
) -> Flow<Option<TrackInfo>, FatalError, DatabaseError> {
|
||||||
let track = {
|
let track = {
|
||||||
let tracks = self.tracks.read().unwrap();
|
let tracks = self.tracks.read().unwrap();
|
||||||
tracks.get(&id).cloned()
|
tracks.get(&id).cloned()
|
||||||
};
|
};
|
||||||
ok(track)
|
ok(track)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn list_tracks<'a>(&'a self) -> Flow<Vec<TrackInfo>, FatalError, DatabaseError> {
|
||||||
|
ok(self
|
||||||
|
.tracks
|
||||||
|
.read()
|
||||||
|
.unwrap()
|
||||||
|
.values()
|
||||||
|
.cloned()
|
||||||
|
.collect::<Vec<TrackInfo>>())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ManagedConnection<'a> {
|
pub struct ManagedConnection<'a> {
|
||||||
|
@ -104,3 +107,35 @@ impl Database {
|
||||||
pool.push(conn);
|
pool.push(conn);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
fn with_memory_index<F>(f: F)
|
||||||
|
where
|
||||||
|
F: Fn(&dyn MusicIndex),
|
||||||
|
{
|
||||||
|
let index = MemoryIndex::new();
|
||||||
|
f(&index)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn it_saves_and_loads_data() {
|
||||||
|
with_memory_index(|index| {
|
||||||
|
let info = TrackInfo {
|
||||||
|
id: TrackId::from("track_1".to_owned()),
|
||||||
|
track_number: None,
|
||||||
|
name: None,
|
||||||
|
album: None,
|
||||||
|
artist: None,
|
||||||
|
};
|
||||||
|
index.add_track(info.clone());
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
Flow::Ok(Some(info)),
|
||||||
|
index.get_track_info(&TrackId::from("track_1".to_owned()))
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
pub mod audio;
|
pub mod audio;
|
||||||
pub mod core;
|
pub mod core;
|
||||||
pub mod database;
|
pub mod database;
|
||||||
|
pub mod music_scanner;
|
||||||
use database::DatabaseError;
|
use database::DatabaseError;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
#[derive(Debug, Error)]
|
#[derive(Debug, Error, PartialEq)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
#[error("Database error: {0}")]
|
#[error("Database error: {0}")]
|
||||||
DatabaseError(DatabaseError),
|
DatabaseError(DatabaseError),
|
||||||
|
@ -16,7 +17,7 @@ impl From<DatabaseError> for Error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Error)]
|
#[derive(Debug, Error, PartialEq)]
|
||||||
pub enum FatalError {
|
pub enum FatalError {
|
||||||
#[error("Unexpected error")]
|
#[error("Unexpected error")]
|
||||||
UnexpectedError,
|
UnexpectedError,
|
||||||
|
|
|
@ -0,0 +1,182 @@
|
||||||
|
use crate::audio::{TrackId, TrackInfo};
|
||||||
|
use std::{
|
||||||
|
fs::{DirEntry, ReadDir},
|
||||||
|
path::PathBuf,
|
||||||
|
};
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
#[derive(Debug, Error)]
|
||||||
|
pub enum ScannerError {
|
||||||
|
#[error("Cannot scan {0}")]
|
||||||
|
CannotScan(PathBuf),
|
||||||
|
#[error("Not found {0}")]
|
||||||
|
NotFound(PathBuf),
|
||||||
|
#[error("IO error {0}")]
|
||||||
|
IO(std::io::Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<std::io::Error> for ScannerError {
|
||||||
|
fn from(err: std::io::Error) -> Self {
|
||||||
|
Self::IO(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait MusicScanner: Send {
|
||||||
|
fn scan<'a>(&'a self) -> Box<dyn Iterator<Item = Result<TrackInfo, ScannerError>> + 'a>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct FileScanner {
|
||||||
|
roots: Vec<PathBuf>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FileScanner {
|
||||||
|
pub fn new(roots: Vec<PathBuf>) -> Self {
|
||||||
|
Self { roots }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct FileIterator {
|
||||||
|
dirs: Vec<PathBuf>,
|
||||||
|
file_iter: Option<ReadDir>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FileIterator {
|
||||||
|
fn scan_file(&self, path: PathBuf) -> Result<TrackInfo, ScannerError> {
|
||||||
|
Ok(TrackInfo {
|
||||||
|
id: TrackId::from(path.to_str().unwrap().to_owned()),
|
||||||
|
album: None,
|
||||||
|
artist: None,
|
||||||
|
name: path
|
||||||
|
.file_stem()
|
||||||
|
.and_then(|s| s.to_str())
|
||||||
|
.map(|s| s.to_owned()),
|
||||||
|
track_number: None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum EntryInfo {
|
||||||
|
Dir(PathBuf),
|
||||||
|
File(PathBuf),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Iterator for FileIterator {
|
||||||
|
type Item = Result<TrackInfo, ScannerError>;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
fn process_entry(entry: DirEntry) -> Result<EntryInfo, ScannerError> {
|
||||||
|
entry
|
||||||
|
.metadata()
|
||||||
|
.map_err(ScannerError::from)
|
||||||
|
.map(|metadata| {
|
||||||
|
if metadata.is_dir() {
|
||||||
|
EntryInfo::Dir(entry.path())
|
||||||
|
} else {
|
||||||
|
EntryInfo::File(entry.path())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
let next_file = match &mut self.file_iter {
|
||||||
|
Some(iter) => iter.next(),
|
||||||
|
None => None,
|
||||||
|
};
|
||||||
|
|
||||||
|
match next_file {
|
||||||
|
Some(Ok(entry)) => match process_entry(entry) {
|
||||||
|
Ok(EntryInfo::Dir(path)) => {
|
||||||
|
self.dirs.push(path);
|
||||||
|
self.next()
|
||||||
|
}
|
||||||
|
Ok(EntryInfo::File(path)) => Some(self.scan_file(path)),
|
||||||
|
Err(err) => Some(Err(err)),
|
||||||
|
},
|
||||||
|
Some(Err(err)) => Some(Err(ScannerError::from(err))),
|
||||||
|
None => match self.dirs.pop() {
|
||||||
|
Some(dir) => match dir.read_dir() {
|
||||||
|
Ok(entry) => {
|
||||||
|
self.file_iter = Some(entry);
|
||||||
|
self.next()
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
if err.kind() == std::io::ErrorKind::NotFound {
|
||||||
|
Some(Err(ScannerError::NotFound(dir)))
|
||||||
|
} else {
|
||||||
|
Some(Err(ScannerError::from(err)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None => None,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MusicScanner for FileScanner {
|
||||||
|
fn scan<'a>(&'a self) -> Box<dyn Iterator<Item = Result<TrackInfo, ScannerError>> + 'a> {
|
||||||
|
Box::new(FileIterator {
|
||||||
|
dirs: self.roots.clone(),
|
||||||
|
file_iter: None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod factories {
|
||||||
|
use super::*;
|
||||||
|
use crate::audio::TrackId;
|
||||||
|
|
||||||
|
pub struct MockScanner {
|
||||||
|
data: Vec<TrackInfo>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MockScanner {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
data: vec![
|
||||||
|
TrackInfo {
|
||||||
|
id: TrackId::from("/home/savanni/Track 1.mp3".to_owned()),
|
||||||
|
track_number: Some(1),
|
||||||
|
name: Some("Track 1".to_owned()),
|
||||||
|
album: Some("Savanni's Demo".to_owned()),
|
||||||
|
artist: Some("Savanni".to_owned()),
|
||||||
|
},
|
||||||
|
TrackInfo {
|
||||||
|
id: TrackId::from("/home/savanni/Track 2.mp3".to_owned()),
|
||||||
|
track_number: Some(2),
|
||||||
|
name: Some("Track 2".to_owned()),
|
||||||
|
album: Some("Savanni's Demo".to_owned()),
|
||||||
|
artist: Some("Savanni".to_owned()),
|
||||||
|
},
|
||||||
|
TrackInfo {
|
||||||
|
id: TrackId::from("/home/savanni/Track 3.mp3".to_owned()),
|
||||||
|
track_number: Some(3),
|
||||||
|
name: Some("Track 3".to_owned()),
|
||||||
|
album: Some("Savanni's Demo".to_owned()),
|
||||||
|
artist: Some("Savanni".to_owned()),
|
||||||
|
},
|
||||||
|
TrackInfo {
|
||||||
|
id: TrackId::from("/home/savanni/Track 4.mp3".to_owned()),
|
||||||
|
track_number: Some(4),
|
||||||
|
name: Some("Track 4".to_owned()),
|
||||||
|
album: Some("Savanni's Demo".to_owned()),
|
||||||
|
artist: Some("Savanni".to_owned()),
|
||||||
|
},
|
||||||
|
TrackInfo {
|
||||||
|
id: TrackId::from("/home/savanni/Track 5.mp3".to_owned()),
|
||||||
|
track_number: Some(5),
|
||||||
|
name: Some("Track 5".to_owned()),
|
||||||
|
album: Some("Savanni's Demo".to_owned()),
|
||||||
|
artist: Some("Savanni".to_owned()),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MusicScanner for MockScanner {
|
||||||
|
fn scan<'a>(&'a self) -> Box<dyn Iterator<Item = Result<TrackInfo, ScannerError>> + 'a> {
|
||||||
|
Box::new(self.data.iter().map(|t| Ok(t.clone())))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue