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
// Base crate for Orasis plugins.
// Copyright (C) 2019 Daniil Fomichev
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

use std::sync::mpsc::{Receiver, Sender};

pub struct EventData {
    /// Plugin intended to receive the event. Use `None` to trigger processing by plugin manager.
    pub target_id: Option<String>,
    /// A short string describing type of event.
    pub meta: String,
    /// Optional `String` receiver.
    pub message: Option<Receiver<String>>,
    /// Optional `u64` receiver.
    pub data: Option<Receiver<u64>>
}

impl EventData {
    pub fn sys(meta: String,
               message: Option<Receiver<String>>,
               data: Option<Receiver<u64>>) -> Self {
        EventData {
            target_id: None,
            meta,
            message,
            data
        }
    }
    pub fn new(target_id: String,
               meta: String,
               message: Option<Receiver<String>>,
               data: Option<Receiver<u64>>) -> Self {
        EventData {
            target_id: Some(target_id),
            meta,
            message,
            data
        }
    }
}

pub enum Event {
    /// Input event as key presses, mouse clicks, etc.
    Input(termion::event::Event),
    /// Special event for plugin communication.
    Special(EventData),
    /// SIGWYNCH.
    Resize((u16, u16)),
    /// Some time have passed.
    Tick
}

/// Here plugins do their changes.
pub struct VirtualCanvas<'a> {
    pub width: u64,
    pub height: u64,
    /// ARGB data; access pixels with `[y * width + x]`.
    pub data: &'a mut [u32],

    pub o_width: u64,
    pub o_height: u64,
    /// Overlay as visible on the screen; access pixels with `[y * o_width + x]`.
    pub overlay: &'a mut [char]
}

pub trait ImageEditorPlugin {
    /// Store emitter to be able to send events later.
    fn setup(&mut self, emitter: Sender<&Event>);
    
    /// Plugin-unique identifier. May be used in `EventData.target_id`.
    fn id(&self) -> String;
    
    /// Plugin is starting to be used. Called by user, not other plugins.
    fn activate(&mut self, canvas: &VirtualCanvas);
    
    /// Plugin may draw some additional overlay there.
    fn draw(&self, canvas: &mut VirtualCanvas);

    /// Return value: whether this plugin consumes the event.
    fn act(&mut self, ev: &Event, canvas: &mut VirtualCanvas) -> bool;
    
    /// Plugin is deactivated. It may be activated again later. Called by user, not other plugins.
    fn deactivate(&mut self);
}