Skip to content

Commit 22b2877

Browse files
committed
Implement Current Comparison Component
Resolves #31
1 parent 3b8b7fc commit 22b2877

File tree

7 files changed

+97
-0
lines changed

7 files changed

+97
-0
lines changed

capi/bind_gen/src/emscripten.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,11 @@ export interface DeltaComponentStateJson {
298298
color: Color;
299299
}
300300
301+
export interface CurrentComparisonComponentStateJson {
302+
text: string;
303+
comparison: string;
304+
}
305+
301306
export interface RunEditorStateJson {
302307
icon_change?: string,
303308
game: string,

capi/bind_gen/src/node.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,11 @@ export interface DeltaComponentStateJson {
312312
color: Color;
313313
}
314314
315+
export interface CurrentComparisonComponentStateJson {
316+
text: string;
317+
comparison: string;
318+
}
319+
315320
export interface RunEditorStateJson {
316321
icon_change?: string,
317322
game: string,
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use livesplit_core::component::current_comparison::Component as CurrentComparisonComponent;
2+
use livesplit_core::Timer;
3+
use super::{Json, alloc, own_drop, acc, output_vec, acc_mut};
4+
use current_comparison_component_state::OwnedCurrentComparisonComponentState;
5+
6+
pub type OwnedCurrentComparisonComponent = *mut CurrentComparisonComponent;
7+
8+
#[no_mangle]
9+
pub unsafe extern "C" fn CurrentComparisonComponent_new() -> OwnedCurrentComparisonComponent {
10+
alloc(CurrentComparisonComponent::new())
11+
}
12+
13+
#[no_mangle]
14+
pub unsafe extern "C" fn CurrentComparisonComponent_drop(this: OwnedCurrentComparisonComponent) {
15+
own_drop(this);
16+
}
17+
18+
#[no_mangle]
19+
pub unsafe extern "C" fn CurrentComparisonComponent_state_as_json(this: *mut CurrentComparisonComponent, timer: *const Timer)-> Json{
20+
output_vec(|o| { acc_mut(this).state(acc(timer)).write_json(o).unwrap(); })
21+
}
22+
23+
#[no_mangle]
24+
pub unsafe extern "C" fn CurrentComparisonComponent_state
25+
(this: *mut CurrentComparisonComponent,
26+
timer: *const Timer)
27+
-> OwnedCurrentComparisonComponentState {
28+
alloc(acc_mut(this).state(acc(timer)))
29+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use livesplit_core::component::current_comparison::State as CurrentComparisonComponentState;
2+
use super::{own_drop, acc, output_str};
3+
use libc::c_char;
4+
5+
pub type OwnedCurrentComparisonComponentState = *mut CurrentComparisonComponentState;
6+
7+
#[no_mangle]
8+
pub unsafe extern "C" fn CurrentComparisonComponentState_drop(this: OwnedCurrentComparisonComponentState){
9+
own_drop(this);
10+
}
11+
12+
#[no_mangle]
13+
pub unsafe extern "C" fn CurrentComparisonComponentState_text(this: *const CurrentComparisonComponentState)
14+
-> *const c_char{
15+
output_str(&acc(this).text)
16+
}
17+
18+
#[no_mangle]
19+
pub unsafe extern "C" fn CurrentComparisonComponentState_comparison(this: *const CurrentComparisonComponentState)
20+
-> *const c_char{
21+
output_str(&acc(this).comparison)
22+
}

capi/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ pub mod current_pace_component;
4444
pub mod current_pace_component_state;
4545
pub mod delta_component;
4646
pub mod delta_component_state;
47+
pub mod current_comparison_component;
48+
pub mod current_comparison_component_state;
4749
pub mod run_editor;
4850
pub mod shared_timer;
4951
pub mod timer_read_lock;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use Timer;
2+
use serde_json::{to_writer, Result};
3+
use std::io::Write;
4+
5+
#[derive(Default)]
6+
pub struct Component;
7+
8+
#[derive(Serialize, Deserialize)]
9+
pub struct State {
10+
pub text: String,
11+
pub comparison: String,
12+
}
13+
14+
impl State {
15+
pub fn write_json<W>(&self, mut writer: W) -> Result<()>
16+
where W: Write
17+
{
18+
to_writer(&mut writer, self)
19+
}
20+
}
21+
22+
impl Component {
23+
pub fn new() -> Self {
24+
Default::default()
25+
}
26+
27+
pub fn state(&self, timer: &Timer) -> State {
28+
State {
29+
text: String::from("Comparing Against"),
30+
comparison: timer.current_comparison().to_string(),
31+
}
32+
}
33+
}

src/component/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod current_comparison;
12
pub mod current_pace;
23
pub mod delta;
34
pub mod graph;

0 commit comments

Comments
 (0)