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.draw; 26 27 public import desmath.linear.vector; 28 public import desil.rect; 29 30 import desil; 31 32 interface DiViewport 33 { 34 @property irect rect() const; 35 @property vec2 offset() const; 36 @property vec2 scale() const; 37 38 /++ перевод координат локальные +/ 39 final vec2 mapToLocal(T)( in T crd ) const 40 if( isCompVector!(2,float,T) ) 41 { 42 vec2 r = crd - rect.pos - offset; 43 r.x /= scale.x; 44 r.y /= scale.y; 45 return r; 46 } 47 48 final vec2 mapToParent(T)( in T crd ) const 49 if( isCompVector!(2,float,T) ) 50 { 51 vec2 r = crd; 52 r.x *= scale.x; 53 r.y *= scale.y; 54 return r + offset + rect.pos; 55 } 56 } 57 58 interface DiDrawStack 59 { 60 irect push( in DiViewport ); 61 void pull(); 62 } 63 64 interface DiDrawable { void draw(); } 65 interface DiAnimate { void idle( float dt ); } 66 67 interface DiShapeDrawable : DiDrawable 68 { void reshape( in irect ); } 69 70 interface DiNovemShapeDrawable : DiShapeDrawable 71 { 72 @property 73 { 74 DiShapeDrawable[9] elems(); 75 ivec2 corner() const; 76 } 77 78 final void draw() 79 { foreach( e; elems ) if( e !is null ) e.draw(); } 80 81 final void reshape( in irect r ) 82 { 83 irect rect = r; 84 85 if( rect.w < corner.x * 2 ) rect.w = corner.x * 2; 86 if( rect.h < corner.y * 2 ) rect.h = corner.y * 2; 87 88 if( elems[0] !is null ) elems[0].reshape( irect( rect.pos, corner ) ); 89 if( elems[1] !is null ) elems[1].reshape( irect( rect.pos.x + corner.x, rect.pos.y, rect.w - corner.x*2, corner.y ) ); 90 if( elems[2] !is null ) elems[2].reshape( irect( rect.pos.x + rect.w - corner.x, rect.pos.y, corner ) ); 91 92 if( elems[3] !is null ) elems[3].reshape( irect( rect.pos.x, rect.pos.y + corner.y, corner.x, rect.h - corner.y*2 ) ); 93 if( elems[4] !is null ) elems[4].reshape( irect( rect.pos + corner, rect.size - corner*2 ) ); 94 if( elems[5] !is null ) elems[5].reshape( irect( rect.pos.x + rect.w - corner.x, rect.pos.y + corner.y, corner.x, rect.h - corner.y*2 ) ); 95 96 if( elems[6] !is null ) elems[6].reshape( irect( rect.pos.x, rect.pos.y + rect.h - corner.y, corner ) ); 97 if( elems[7] !is null ) elems[7].reshape( irect( rect.pos.x + corner.x, rect.pos.y + rect.h - corner.y, rect.w - corner.x*2, corner.y ) ); 98 if( elems[8] !is null ) elems[8].reshape( irect( rect.pos.x + rect.w - corner.x, rect.pos.y + rect.h - corner.y, corner ) ); 99 } 100 } 101 102 interface DiAnimateShape : DiShapeDrawable, DiAnimate 103 { 104 /* workaround Issue 11796 */ 105 void idle( float ); 106 void draw(); 107 void reshape( in irect ); 108 } 109 110 interface DiDrawRect : DiShapeDrawable 111 { 112 enum UseTexture 113 { 114 NONE = cast(int)0, 115 ALPHA = 1, 116 FULL = 2 117 } 118 119 @property 120 { 121 ref UseTexture useTexture(); 122 ref const(UseTexture) useTexture() const; 123 124 irect rect() const; 125 final void rect( in irect r ) { reshape( r ); } 126 127 col4 color() const; 128 void color( in col4 ); 129 } 130 131 /* workaround Issue 11796 */ 132 void draw(); 133 void reshape( in irect ); 134 135 void image( in Image ); 136 void image( in ImageReadAccess ); 137 } 138 139 //TODO: path, line, circle, etc 140 141 interface DiDrawFactory 142 { 143 @property DiDrawRect rect(); 144 //TODO: path, line, circle, etc 145 }