orasis_plugin/
lib.rs

1// Base crate for Orasis plugins.
2// Copyright (C) 2019 Daniil Fomichev
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9use std::sync::mpsc::{Receiver, Sender};
10
11pub struct EventData {
12    /// Plugin intended to receive the event. Use `None` to trigger processing by plugin manager.
13    pub target_id: Option<String>,
14    /// A short string describing type of event.
15    pub meta: String,
16    /// Optional `String` receiver.
17    pub message: Option<Receiver<String>>,
18    /// Optional `u64` receiver.
19    pub data: Option<Receiver<u64>>
20}
21
22impl EventData {
23    pub fn sys(meta: String,
24               message: Option<Receiver<String>>,
25               data: Option<Receiver<u64>>) -> Self {
26        EventData {
27            target_id: None,
28            meta,
29            message,
30            data
31        }
32    }
33    pub fn new(target_id: String,
34               meta: String,
35               message: Option<Receiver<String>>,
36               data: Option<Receiver<u64>>) -> Self {
37        EventData {
38            target_id: Some(target_id),
39            meta,
40            message,
41            data
42        }
43    }
44}
45
46pub enum Event {
47    /// Input event as key presses, mouse clicks, etc.
48    Input(termion::event::Event),
49    /// Special event for plugin communication.
50    Special(EventData),
51    /// SIGWYNCH.
52    Resize((u16, u16)),
53    /// Some time have passed.
54    Tick
55}
56
57/// Here plugins do their changes.
58pub struct VirtualCanvas<'a> {
59    pub width: u64,
60    pub height: u64,
61    /// ARGB data; access pixels with `[y * width + x]`.
62    pub data: &'a mut [u32],
63
64    pub o_width: u64,
65    pub o_height: u64,
66    /// Overlay as visible on the screen; access pixels with `[y * o_width + x]`.
67    pub overlay: &'a mut [char]
68}
69
70pub trait ImageEditorPlugin {
71    /// Store emitter to be able to send events later.
72    fn setup(&mut self, emitter: Sender<&Event>);
73    
74    /// Plugin-unique identifier. May be used in `EventData.target_id`.
75    fn id(&self) -> String;
76    
77    /// Plugin is starting to be used. Called by user, not other plugins.
78    fn activate(&mut self, canvas: &VirtualCanvas);
79    
80    /// Plugin may draw some additional overlay there.
81    fn draw(&self, canvas: &mut VirtualCanvas);
82
83    /// Return value: whether this plugin consumes the event.
84    fn act(&mut self, ev: &Event, canvas: &mut VirtualCanvas) -> bool;
85    
86    /// Plugin is deactivated. It may be activated again later. Called by user, not other plugins.
87    fn deactivate(&mut self);
88}