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
// 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.

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<std::sync::mpsc::Receiver<String>>,
    /// Optional `u64` receiver.
    pub data: Option<std::sync::mpsc::Receiver<u64>>
}

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_widrh: 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 : Send {
    /// Store emitter to be able to send events later.
    fn setup(&mut self, emitter: std::sync::mpsc::Sender<&Event>);
    
    /// Plugin-unique identifier. May be used in `EventData.target_id`.
    fn id(&self) -> String;
    
    /// Plugin is starting to be used.
    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.
    fn deactivate(&mut self);
}