1 /** 2 Copyright: Copyright (c) 2013-2014 Andrey Penechko. 3 License: a$(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0). 4 Authors: Andrey Penechko. 5 */ 6 7 module anchovy.gui.skin.guiskin; 8 9 import anchovy.gui; 10 import anchovy.graphics.interfaces.irenderer; 11 import anchovy.graphics.texture; 12 import anchovy.core.types; 13 14 class GuiSkin 15 { 16 string skinFilename; 17 //May be replaced by atlas 18 Texture texture; 19 string textureFilename; 20 string name; 21 22 Font[string] fonts; 23 FontInfo[] fontInfos; 24 GuiStyle[string] styles; 25 26 void addStyle(string styleName, GuiStyle style) 27 { 28 styles[styleName] = style; 29 } 30 31 void loadResources(IGuiRenderer guiRenderer) 32 { 33 texture = guiRenderer.renderer.createTexture(textureFilename); 34 foreach(ref FontInfo info; fontInfos) 35 { 36 uint font = guiRenderer.fontManager.createFont(info.filename, info.size); 37 fonts[info.name] = guiRenderer.fontManager.getFont(font); 38 fonts[info.name].verticalOffset = info.verticalOffset; 39 } 40 } 41 42 GuiStyle opIndex(string styleName) 43 { 44 return styles.get(styleName, null); 45 } 46 47 override string toString() const 48 { 49 string result; 50 result ~= "GuiSkin(Name:'" ~ name~"', textureFilename: '"~textureFilename~"'\n"; 51 52 foreach(string styleName, ref style; styles) 53 { 54 result ~= styleName~"("~ style.toString ~ "\n"; 55 } 56 57 result ~=")\n"; 58 return result; 59 } 60 } 61 62 struct FontInfo 63 { 64 string name; 65 string filename; 66 uint size; 67 int verticalOffset; 68 } 69 70 class GuiStyle 71 { 72 GuiStyleState[string] states; 73 string fontName; // "normal" by default 74 75 GuiStyleState opIndex(string stateName) const 76 { 77 const(GuiStyleState)* state = stateName in states; 78 if (state is null) return states["normal"]; 79 return *state; 80 } 81 82 override string toString() const 83 { 84 return "fixedBord:"~to!string(states["normal"].fixedBorders.toString) ~ 85 " contPadd:"~to!string(states["normal"].contentPadding.toString) ~ 86 " rect:"~to!string(states["normal"].atlasRect) ~ 87 "minSize: "~to!string(states["normal"].minSize); 88 } 89 } 90 91 struct GuiStyleState 92 { 93 ///Defines position and sizes of controls texture in skin teture 94 Rect atlasRect; 95 96 /// Minimal size of the widget. If not explicitly specified equal to atlasRect size. 97 /// It is highly recommended to set it to size equal or greater than atlasRect size to prevent glitches. 98 ivec2 minSize; 99 100 /// Maximal size of the widget. By default equal to [0,0]. If maxSize is zero maxSize is not limited. 101 ivec2 maxSize; 102 103 /// Defines offset of content rect from widget borders. 104 RectOffset contentPadding; 105 106 /// Defines outline of skin rect. Useful for drawing highlighting. 107 /// If skin has drawn outline, with this parameter set 108 /// guiRenderer will draw that sides outside of rect. 109 RectOffset outline; 110 111 /// Defines non-stretchable borders of texture. 112 /// Corner parts will stay non-stretched. 113 /// Left/right sides will be stretched vertically. 114 /// Top/bottom sides will be stretched horizontally. 115 /// Middle part will be stretched 116 RectOffset fixedBorders; 117 118 Color textColor; 119 Color backgroundColor; 120 }