11using System ;
2+ using System . Collections . Generic ;
3+ using PaintPower . Tools ;
4+ using PaintPower . Tools . Graphics ;
25
36namespace PaintPower . Display . DisplayIntegration ;
47
8+ // DIPlay or Diplay. You know, like Dip-lay, like "Display" with out the 's'. (Inspired by a typo while learning CSS)
59public class DIPlay
610{
7- public DIPlay ( ) { }
11+ public GfxPane gfxPane ;
12+ public static Point stageSize = new ( PaintPower_Engine . App . _project ? . Metadata ? . StageWidth , PaintPower_Engine . App . _project ? . Metadata ? . StageHeight ) ;
13+
14+ public List < DIItem > items = new ( ) ;
15+
16+ public DIPlay ( )
17+ {
18+ setStageSize ( ) ;
19+ gfxPane = new ( stageSize . x , stageSize . y ) ;
20+ }
21+
22+ public void setStageSize ( )
23+ {
24+ stageSize = new ( PaintPower_Engine . App . _project ? . Metadata ? . StageWidth , PaintPower_Engine . App . _project ? . Metadata ? . StageHeight ) ;
25+ }
26+
27+ public void Start ( )
28+ {
29+ var timer = new System . Timers . Timer ( 1000.0 / 60.0 ) ;
30+ timer . Elapsed += async ( _ , __ ) => Tick ( ) ;
31+ timer . Start ( ) ;
32+ }
33+
34+ public async void Tick ( )
35+ {
36+ var pane = gfxPane ;
37+ int t = 0 ;
38+
39+ await PaintPower_Engine . App . vm . Tick ( ) ;
40+
41+ List < DrawCommand > batch = new ( ) ;
42+
43+ foreach ( DIItem item in items )
44+ {
45+ if ( ! item . IsVisible )
46+ continue ;
47+
48+ var g = item . DrawAs ( ) ;
49+
50+ if ( g is GraphicAnimation anim )
51+ {
52+ int frame = ResolveAnimationFrame ( anim , t * 16 ) ;
53+ Graphic frameGraphic = anim . Frames [ frame ] ;
54+
55+ if ( IsCulled ( frameGraphic , ( float ) item . x , ( float ) item . y , item . ScaleX , item . ScaleY ) )
56+ continue ;
57+
58+ batch . Add ( new DrawCommand (
59+ frameGraphic ,
60+ ( float ) item . x ,
61+ ( float ) item . y ,
62+ item . Rotation ,
63+ item . ScaleX ,
64+ item . ScaleY ,
65+ item . Z
66+ ) ) ;
67+
68+ }
69+ else if ( g is Graphic img )
70+ {
71+ if ( IsCulled ( img , ( float ) item . x , ( float ) item . y , item . ScaleX , item . ScaleY ) )
72+ continue ;
73+
74+ batch . Add ( new DrawCommand (
75+ img ,
76+ ( float ) item . x ,
77+ ( float ) item . y ,
78+ item . Rotation ,
79+ item . ScaleX ,
80+ item . ScaleY ,
81+ item . Z
82+ ) ) ;
83+ }
84+ }
85+
86+ // Sort for Z-Layering
87+ batch . Sort ( ( a , b ) => a . Z . CompareTo ( b . Z ) ) ;
88+
89+ pane . Renderer . Clear ( 0xFF202020 ) ;
90+ pane . Renderer . DrawBatch ( batch ) ;
91+
92+ Avalonia . Threading . Dispatcher . UIThread . Post ( pane . Present ) ;
93+
94+ t ++ ;
95+ }
96+
97+ private bool IsCulled ( Graphic g , float x , float y , float scaleX , float scaleY )
98+ {
99+ float halfW = g . Width * scaleX / 2f ;
100+ float halfH = g . Height * scaleY / 2f ;
101+
102+ float left = x - halfW ;
103+ float right = x + halfW ;
104+ float top = y - halfH ;
105+ float bottom = y + halfH ;
106+
107+ return right < 0 ||
108+ left > stageSize . x ||
109+ bottom < 0 ||
110+ top > stageSize . y ;
111+ }
112+
113+ private int ResolveAnimationFrame ( GraphicAnimation anim , int timeMs )
114+ {
115+ int total = 0 ;
116+
117+ for ( int i = 0 ; i < anim . FrameDelays . Count ; i ++ )
118+ {
119+ total += anim . FrameDelays [ i ] ;
120+ if ( timeMs % total < anim . FrameDelays [ i ] )
121+ return i ;
122+ }
123+
124+ return 0 ;
125+ }
8126}
0 commit comments