Skip to content

Layout parser Custom Variables from LiveSplit layouts #874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 50 additions & 40 deletions src/layout/parser/splits.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use super::{
accuracy, comparison_override, end_tag, parse_bool, parse_children, text, text_parsed,
timing_method_override, Error, GradientBuilder, GradientKind, ListGradientKind, Result,
Error, GradientBuilder, GradientKind, ListGradientKind, Result, accuracy, comparison_override,
end_tag, parse_bool, parse_children, text, text_parsed, timing_method_override,
};
use crate::{
component::splits::{
self, ColumnKind, ColumnSettings, ColumnStartWith, ColumnUpdateTrigger, ColumnUpdateWith,
TimeColumn,
TimeColumn, VariableColumn,
},
platform::prelude::*,
util::xml::{helper::text_as_escaped_string_err, Reader},
util::xml::{Reader, helper::text_as_escaped_string_err},
};

pub use crate::component::splits::Component;
Expand Down Expand Up @@ -49,6 +49,7 @@ pub fn settings(reader: &mut Reader<'_>, component: &mut Component) -> Result<()
parse_children(reader, |reader, _, _| {
let mut column_name = String::new();
let mut column = TimeColumn::default();
let mut custom_variable = false;
parse_children(reader, |reader, tag, _| match tag.name() {
"Name" => text(reader, |v| column_name = v.into_owned()),
"Comparison" => {
Expand All @@ -58,41 +59,44 @@ pub fn settings(reader: &mut Reader<'_>, component: &mut Component) -> Result<()
timing_method_override(reader, |v| column.timing_method = v)
}
"Type" => text_as_escaped_string_err(reader, |v| {
(
column.start_with,
column.update_with,
column.update_trigger,
) = match v {
"Delta" => (
ColumnStartWith::Empty,
ColumnUpdateWith::Delta,
ColumnUpdateTrigger::Contextual,
),
"SplitTime" => (
ColumnStartWith::ComparisonTime,
ColumnUpdateWith::SplitTime,
ColumnUpdateTrigger::OnEndingSegment,
),
"DeltaorSplitTime" => (
ColumnStartWith::ComparisonTime,
ColumnUpdateWith::DeltaWithFallback,
ColumnUpdateTrigger::Contextual,
),
"SegmentDelta" => (
ColumnStartWith::Empty,
ColumnUpdateWith::SegmentDelta,
ColumnUpdateTrigger::Contextual,
),
"SegmentTime" => (
ColumnStartWith::ComparisonSegmentTime,
ColumnUpdateWith::SegmentTime,
ColumnUpdateTrigger::OnEndingSegment,
),
"SegmentDeltaorSegmentTime" => (
ColumnStartWith::ComparisonSegmentTime,
ColumnUpdateWith::SegmentDeltaWithFallback,
ColumnUpdateTrigger::Contextual,
),
match v {
"Delta" => {
column.start_with = ColumnStartWith::Empty;
column.update_with = ColumnUpdateWith::Delta;
column.update_trigger = ColumnUpdateTrigger::Contextual;
}
"SplitTime" => {
column.start_with = ColumnStartWith::ComparisonTime;
column.update_with = ColumnUpdateWith::SplitTime;
column.update_trigger =
ColumnUpdateTrigger::OnEndingSegment;
}
"DeltaorSplitTime" => {
column.start_with = ColumnStartWith::ComparisonTime;
column.update_with =
ColumnUpdateWith::DeltaWithFallback;
column.update_trigger = ColumnUpdateTrigger::Contextual;
}
"SegmentDelta" => {
column.start_with = ColumnStartWith::Empty;
column.update_with = ColumnUpdateWith::SegmentDelta;
column.update_trigger = ColumnUpdateTrigger::Contextual;
}
"SegmentTime" => {
column.start_with =
ColumnStartWith::ComparisonSegmentTime;
column.update_with = ColumnUpdateWith::SegmentTime;
column.update_trigger =
ColumnUpdateTrigger::OnEndingSegment;
}
"SegmentDeltaorSegmentTime" => {
column.start_with =
ColumnStartWith::ComparisonSegmentTime;
column.update_with =
ColumnUpdateWith::SegmentDeltaWithFallback;
column.update_trigger = ColumnUpdateTrigger::Contextual;
}
"CustomVariable" => custom_variable = true,
_ => return Err(Error::ParseColumnType),
};

Expand All @@ -103,8 +107,14 @@ pub fn settings(reader: &mut Reader<'_>, component: &mut Component) -> Result<()
settings.columns.insert(
0,
splits::ColumnSettings {
kind: if custom_variable {
ColumnKind::Variable(VariableColumn {
variable_name: column_name.clone(),
})
} else {
ColumnKind::Time(column)
},
name: column_name,
kind: ColumnKind::Time(column),
},
);
Ok(())
Expand Down
20 changes: 15 additions & 5 deletions src/layout/parser/text.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{color, end_tag, parse_bool, parse_children, text, GradientBuilder, Result};
use super::{GradientBuilder, Result, color, end_tag, parse_bool, parse_children, text};
use crate::{component::text::Text, platform::prelude::*, util::xml::Reader};

pub use crate::component::text::Component;
Expand All @@ -8,6 +8,14 @@ pub fn settings(reader: &mut Reader<'_>, component: &mut Component) -> Result<()
let mut background_builder = GradientBuilder::new();
let (mut override_label, mut override_value) = (false, false);
let (mut left_center, mut right) = (String::new(), String::new());
// - Normally, when `custom_variable` is false,
// `Text2`/`right` is interpreted as text to be displayed as-is,
// through `Text::Split` or `Text::Center`.
// - But when `custom_variable` is true,
// `Text2`/`right` is interpreted as the custom variable name,
// and the value of the custom variable is displayed instead,
// through `Text::Variable`.
let mut custom_variable = false;

parse_children(reader, |reader, tag, _| {
if !background_builder.parse_background(reader, tag.name())? {
Expand All @@ -19,6 +27,7 @@ pub fn settings(reader: &mut Reader<'_>, component: &mut Component) -> Result<()
"Text1" => text(reader, |v| left_center = v.into_owned()),
"Text2" => text(reader, |v| right = v.into_owned()),
"Display2Rows" => parse_bool(reader, |b| settings.display_two_rows = b),
"CustomVariable" => parse_bool(reader, |b| custom_variable = b),
_ => {
// FIXME:
// Font1
Expand All @@ -39,10 +48,11 @@ pub fn settings(reader: &mut Reader<'_>, component: &mut Component) -> Result<()
if !override_value {
settings.right_color = None;
}
settings.text = match (left_center.is_empty(), right.is_empty()) {
(false, false) => Text::Split(left_center, right),
(false, true) => Text::Center(left_center),
_ => Text::Center(right),
settings.text = match (custom_variable, left_center.is_empty(), right.is_empty()) {
(true, lc_empty, _) => Text::Variable(right, !lc_empty),
(false, false, false) => Text::Split(left_center, right),
(false, false, true) => Text::Center(left_center),
(false, true, _) => Text::Center(right),
};
settings.background = background_builder.build();

Expand Down
Loading
Loading