summaryrefslogtreecommitdiffstats
path: root/svgbob/src/focus_char.rs
blob: 7fe56aff17e1b472a26baf6a766beacae78c675d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
use block::Block;
use fragments::Fragment;
use loc::Loc;
use grid::Grid;
use properties::{
    Properties,
    Signal::{self,Weak,Medium,Strong},
    Can::{self,ConnectTo,Is,IsStrongAll},
    Characteristic,
};
use point_block::PointBlock;
use point::Point;
use loc_block::LocBlock;
use element::Element;
use fragments::Fragment::Text;
use element::{line,dashed_line,arrow_line,start_arrow_line,arc,open_circle,solid_circle,text};
use location::Location;
use settings::Settings;

#[derive(Debug, Clone)]
pub struct FocusChar<'g> {
    loc: Loc,
    ch: char,
    grid: &'g Grid,
}

impl<'g> FocusChar<'g> {
    pub fn new(loc: &Loc, grid: &'g Grid) -> Self {
        let s: Option<&String> = grid.get(loc);
        // if there is a text in this location, take the first char as the focus char
        let ch = match s {
            Some(s) => s.chars().nth(0).unwrap_or('\0'),
            None => '\0',
        };

        Self {
            loc: loc.clone(),
            ch: ch,
            grid: grid,
        }
    }

    /// get the text of self char, including complex block
    /// concatenated with multiple strings in utf8 encoding
    fn text(&self) -> String {
        match self.grid.get(&self.loc) {
            Some(s) => s.to_owned(),
            None => "".to_string(),
        }
    }

    /// get the focus char at this location
    fn get(&self, loc: &Loc) -> Self {
        FocusChar::new(loc, self.grid)
    }

    /// if the character matches given argument
    pub fn is(&self, ch: char) -> bool {
        self.ch.is(ch)
    }

    /// if character is any character in the string
    pub fn any(&self, s: &str) -> bool {
        self.ch.any(s)
    }

    fn used_as_text(&self) -> bool {
        if self.is_text_surrounded() {
            // not if it can strongly connect to 4 directions
            if self.can_strongly_connect(&Block::O) || self.can_strongly_connect(&Block::K)
                || self.can_strongly_connect(&Block::C)
                || self.can_strongly_connect(&Block::W)
                || self.can_strongly_connect(&Block::U)
                || self.can_strongly_connect(&Block::Y)
            {
                false
            } else {
                true
            }
        } else {
            false
        }
    }

    fn is_text_surrounded(&self) -> bool {
        self.left().ch.is_alphanumeric() || self.right().ch.is_alphanumeric()
    }

    pub fn is_null(&self) -> bool {
        self.is('\0')
    }

    pub fn is_blank(&self) -> bool {
        self.is_null() || self.is(' ')
    }

    ///////////////////////////////////
    //
    //  can strongly or mediumly connect
    //
    ///////////////////////////////////

    fn can_pass_medium_connect(&self, block: &Block) -> bool {
        self.can_strongly_connect(block) || self.can_medium_connect(block)
    }

    fn can_pass_weakly_connect(&self, block: &Block) -> bool {
        self.can_strongly_connect(block) || self.can_medium_connect(block)
            || self.can_weakly_connect(block)
    }

    pub fn can_strongly_connect(&self, block: &Block) -> bool {
        self.ch.can_connect(&Strong, block)
    }

    fn can_medium_connect(&self, block: &Block) -> bool {
        self.ch.can_connect(&Medium, block)
    }

    fn can_weakly_connect(&self, block: &Block) -> bool {
        self.ch.can_connect(&Weak, block)
    }

    fn point(&self, pb: &PointBlock) -> Point {
        self.loc_block().to_point(pb)
    }

    fn loc_block(&self) -> LocBlock {
        LocBlock {
            loc: self.loc.to_owned(),
            settings: self.get_settings(),
        }
    }

    fn to_element(&self, frag: Fragment) -> Element {
        let unit_x = self.loc_block().unit_x();
        match frag {
            Fragment::Line(p1, p2) => line(&self.point(&p1), &self.point(&p2)),
            Fragment::DashedLine(p1, p2) => dashed_line(&self.point(&p1), &self.point(&p2)),
            Fragment::ArrowLine(p1, p2) => arrow_line(&self.point(&p1), &self.point(&p2)),

            Fragment::StartArrowLine(p1, p2) => start_arrow_line(&self.point(&p1), &self.point(&p2)),

            Fragment::Arc(p1, p2, m) => arc(&self.point(&p1), &self.point(&p2), m as f32 * unit_x),

            Fragment::OpenCircle(c, m) => open_circle(&self.point(&c), m as f32 * unit_x),

            Fragment::SolidCircle(c, m) => solid_circle(&self.point(&c), m as f32 * unit_x),
            Fragment::Text(s) => text(&self.loc, &s),
        }
    }

    /// TODO: optimize this by getting accumulating the location
    /// and convert it into loc in 1 call
    /// then get the focus char at this location;
    fn from_location(&self, location: &Location) -> FocusChar<'g> {
        let loc = self.loc.from_location(location);
        self.get(&loc)
    }

    fn can_block_pass_connect(&self, block: &Block, signal: &Signal) -> bool {
        match *signal {
            Strong => self.can_strongly_connect(block),
            Medium => self.can_pass_medium_connect(block),
            Weak => self.can_pass_weakly_connect(block),
        }
    }

    pub fn get_elements(&self) -> (Vec<Element>, Vec<Loc>) {
        let (fragments, consumed_location) = self.get_fragments();
        let mut elements: Vec<Element> = fragments
            .into_iter()
            .map(|frag| self.to_element(frag))
            .collect();
        let consumed_loc: Vec<Loc> = consumed_location
            .into_iter()
            .map(|location| self.loc.from_location(&location))
            .collect();
        elements.sort();
        elements.dedup();
        (elements, consumed_loc)
    }

    fn is_satisfied(&self, can: &Can) -> bool {
        match *can {
            ConnectTo(ref cond_block, ref signal) => {
                self.can_block_pass_connect(&cond_block, signal)
            }
            Is(char) => self.is(char),
            IsStrongAll(ref blocks) => blocks.iter().all(|b| self.is_strong_block(&b)),
        }
    }

    /// check to see if this specified block for this focused
    /// char is intensified to be strong
    fn is_intensified(&self, arg_block: &Block) -> bool {
        let character: Option<Characteristic> = self.ch.get_characteristic();
        if let Some(character) = character {
            character.intensify.iter().any(|&(ref block, ref cond)| {
                let fc = self.from_location(&cond.loc);
                block == arg_block && fc.is_satisfied(&cond.can)
            })
        } else {
            false
        }
    }

    fn can_be_strong_block(&self, block: &Block) -> bool {
        if self.is_strong_block(block)