1 module rpui.widgets.canvas.widget;
2 
3 import rpui.primitives;
4 import rpui.events;
5 import rpui.widget;
6 
7 interface CanvasRenderer {
8     void onCreate(Widget widget);
9 
10     void onDestroy();
11 
12     void onRender();
13 
14     void onProgress(in ProgressEvent event);
15 }
16 
17 final class Canvas : Widget {
18     enum Background {
19         transparent,  /// Render without color.
20         light,
21         dark,
22         action  /// Color for actions like OK, Cancel etc.
23     }
24 
25     @field Background background = Background.light;  /// Background color of panel.
26 
27     private CanvasRenderer canvasRenderer_;
28     private bool isInit = false;
29 
30     @property CanvasRenderer canvasRenderer() { return canvasRenderer_; }
31     @property void canvasRenderer(CanvasRenderer val) {
32         canvasRenderer_ = val;
33 
34         if (canvasRenderer_ && isInit) {
35             canvasRenderer_.onCreate(this);
36         }
37     }
38 
39     ~this() {
40         if (canvasRenderer_) {
41             canvasRenderer_.onDestroy();
42         }
43     }
44 
45     override void onRender() {
46         if (canvasRenderer_) {
47             view.pushScissor(Rect(absolutePosition, size));
48             canvasRenderer_.onRender();
49             view.popScissor();
50         }
51     }
52 
53     override void onProgress(in ProgressEvent event) {
54         super.onProgress(event);
55 
56         if (canvasRenderer_) {
57             canvasRenderer_.onProgress(event);
58         }
59     }
60 
61     override void onCreate() {
62         super.onCreate();
63 
64         if (canvasRenderer_) {
65             canvasRenderer_.onCreate(this);
66         }
67 
68         isInit = true;
69     }
70 }