1 /+
2 The MIT License (MIT)
3 
4     Copyright (c) <2013> <Oleg Butko (deviator), Anton Akzhigitov (Akzwar)>
5 
6     Permission is hereby granted, free of charge, to any person obtaining a copy
7     of this software and associated documentation files (the "Software"), to deal
8     in the Software without restriction, including without limitation the rights
9     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10     copies of the Software, and to permit persons to whom the Software is
11     furnished to do so, subject to the following conditions:
12 
13     The above copyright notice and this permission notice shall be included in
14     all copies or substantial portions of the Software.
15 
16     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22     THE SOFTWARE.
23 +/
24 
25 module desgui.core.context;
26 
27 public import desgui.core.except;
28 public import desgui.core.draw;
29 public import desgui.core.textrender;
30 
31 interface DiContext
32 {
33     @property DiDrawStack drawStack();
34     @property DiGlyphRender baseGlyphRender();
35     @property DiDrawFactory draw();
36 }
37 
38 version(unittest)
39 {
40     import desil;
41     package
42     {
43         class TestDrawStack : DiDrawStack
44         {
45         private:
46             const(DiViewport)[] cur;
47             irect[] work;
48 
49         public:
50 
51             irect push( in DiViewport w )
52             {
53                 cur ~= w;
54                 if( work.length == 0 )
55                     work ~= w.rect;
56 
57                 work ~= work[$-1].overlapLocal( w.rect );
58 
59                 return work[$-1];
60             }
61 
62             void pull()
63             {
64                 cur = cur[0 .. $-1];
65                 work = work[0 .. $-1];
66             }
67         }
68 
69         class TestDrawFactory : DiDrawFactory
70         {
71             @property DiDrawRect rect() { return new TestDrawRect; }
72         }
73 
74         class TestSubstrate : DiSubstrate
75         {
76             void draw() {}
77             void idle( float dt ) {}
78             void reshape( in irect r ) {}
79         }
80 
81         class TestStyle : DiStyle
82         {
83             DiSubstrate getSubstrate( string name )
84             { return new TestSubstrate(); }
85         }
86 
87         class TestContext : DiContext
88         {
89             DiDrawStack ds;
90             DiGlyphRender gr;
91             DiDrawFactory df;
92 
93             this() 
94             { 
95                 ds = new TestDrawStack; 
96                 gr = new TestGlyphRender;
97                 df = new TestDrawFactory;
98             }
99 
100             @property DiDrawStack drawStack() { return ds; }
101             @property DiGlyphRender baseGlyphRender() { return gr; }
102             @property DiDrawFactory draw() { return df; }
103         }
104 
105         static Image screen;
106         void clearScreen(){ screen = Image( imsize_t(100, 20), ImageType( ImCompType.UBYTE, 1 ) ); }
107         static this(){ clearScreen(); }
108 
109         class TestDrawRect: DiDrawRect
110         {
111         private: 
112             Image tex;
113             UseTexture ut;
114             irect rr;
115             col4 clr;
116         public:
117             this( irect r=irect(0,0,1,1) )
118             {
119                 rr = r;
120                 tex = Image( imsize_t(r.size), ImageType( ImCompType.UBYTE, 1 ) );
121             }
122 
123             @property
124             {
125                 ref UseTexture useTexture() { return ut; }
126                 ref const(UseTexture) useTexture() const { return ut; }
127 
128                 irect rect() const{ return rr; }
129 
130                 col4 color() const { return clr; }
131                 void color( in col4 c ){ clr = c; }
132             }
133 
134             void draw(){ screen.paste( rr.pos, tex ); }
135 
136             void reshape( in irect r ){ rr = r; }
137 
138             void image( in Image im ){ tex = Image(im); }
139             void image( in ImageReadAccess im ){ tex = Image(im.selfImage()); }
140         }
141     }
142 }