use crate::cot_base::{CursorOnTarget, Sendable};
use std::fmt::{Debug, Formatter, Result as FmtResult};
pub struct Circle {
pub cot: CursorOnTarget,
callsign: String,
color: String,
stroke_color: String,
stroke_weight: f64,
fill_color: String,
remarks: String,
rings: i32,
spacing: f64,
}
impl Debug for Circle {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
f.debug_struct("Circle")
.field("cot", &self.cot)
.field("callsign", &self.callsign)
.field("color", &self.color)
.field("stroke_color", &self.stroke_color)
.field("stroke_weight", &self.stroke_weight)
.field("fill_color", &self.fill_color)
.field("remarks", &self.remarks)
.field("rings", &self.rings)
.field("spacing", &self.spacing)
.finish()
}
}
impl Circle {
pub fn new(
cot: CursorOnTarget,
callsign: String,
color: String,
stroke_color: String,
stroke_weight: f64,
fill_color: String,
remarks: String,
rings: i32,
spacing: f64,
) -> Self {
let mut new_circle = Circle {
cot,
callsign,
color,
stroke_color,
stroke_weight,
fill_color,
remarks,
rings,
spacing,
};
new_circle.cot.type_string = Some("u-r-b-c-c".to_string());
new_circle.update_xml();
new_circle
}
pub fn get_xml(&mut self) -> String {
self.cot.get_xml()
}
pub fn get_xml_bytes(&mut self) -> Vec<u8> {
self.cot.get_xml_bytes()
}
pub fn get_field(&self, field: &str) -> String {
match field {
"callsign" => self.callsign.to_string(),
"color" => self.color.to_string(),
"stroke_color" => self.stroke_color.to_string(),
"stroke_weight" => self.stroke_weight.to_string(),
"fill_color" => self.fill_color.to_string(),
"remarks" => self.remarks.to_string(),
"rings" => self.rings.to_string(),
"spacing" => self.spacing.to_string(),
_ => "".to_string(),
}
}
}
impl Sendable for Circle {
fn update_xml(&mut self) {
let mut elipses = "".to_string();
for i in 1..self.rings + 1 {
let minor = i as f64 * self.spacing;
let major = i as f64 * self.spacing;
elipses.push_str(&format!(
r#" <ellipse minor="{minor}" angle="360" major="{major}" />"#,
minor = minor,
major = major,
));
if i != self.rings {
elipses.push_str("\n");
}
}
let details = format!(
r#" <contact callsign="{callsign}" />
<rangeUnits value="1" />
<shape>
{eclipses}
<link relation="p-c" uid="{uid2}" type="b-x-KmlStyle">
<Style>
<LineStyle>
<color>00fff000</color>
<width>3</width>
<alpha>0.5</alpha>
</LineStyle>
<PolyStyle>
<color>00ff0000</color>
</PolyStyle>
</Style>
</link>
</shape>
<color argb="-16711681" />
<strokeColor value="-16711681" />
<strokeWeight value="3.0" />
<fillColor value="16776960" />
<archive />
<remarks />"#,
callsign = self.callsign.to_string(),
eclipses = elipses,
uid2 = "527456a2-d5f0-4df9-af66-c959a176c0ac.Style",
);
self.cot.set_xml_detail(details);
}
fn modify_field(&mut self, field: &str, value: &str) {
match field {
"callsign" => self.callsign = value.to_string(),
"color" => self.color = value.to_string(),
"stroke_color" => self.stroke_color = value.to_string(),
"stroke_weight" => self.stroke_weight = value.parse().unwrap(),
"fill_color" => self.fill_color = value.to_string(),
"remarks" => self.remarks = value.to_string(),
"rings" => self.rings = value.parse().unwrap(),
"spacing" => self.spacing = value.parse().unwrap(),
_ => (),
}
self.update_xml();
}
fn get_cot(&self)-> CursorOnTarget {
self.cot.clone()
}
}