1 /*
2 Copyright (c) 2013 Andrey Penechko
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 
6 Permission is hereby granted, free of charge, to any person or organization
7 obtaining a copy of the software and accompanying documentation covered by
8 this license the "Software" to use, reproduce, display, distribute,
9 execute, and transmit the Software, and to prepare derivative works of the
10 Software, and to permit third-parties to whom the Software is furnished to
11 do so, all subject to the following:
12 
13 The copyright notices in the Software and this entire statement, including
14 the above license grant, this restriction and the following disclaimer,
15 must be included in all copies of the Software, in whole or in part, and
16 all derivative works of the Software, unless such copies or derivative
17 works are solely in the form of machine-executable object code generated by
18 a source language processor.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE.
27 */
28 
29 module anchovy.gui.guiskin;
30 
31 import anchovy.gui;
32 import anchovy.graphics.interfaces.irenderer;
33 import anchovy.graphics.texture;
34 import anchovy.core.types;
35 
36 class GuiSkin
37 {
38 	string skinFilename;
39 	//May be replaced by atlas
40 	Texture texture;
41 	string textureFilename;
42 	string name;
43 
44 	Font[string] fonts;
45 	FontInfo[] fontInfos;
46 	GuiStyle[string] styles;
47 
48 	void addStyle(string styleName, GuiStyle style)
49 	{
50 		styles[styleName] = style;
51 	}
52 
53 	void loadResources(IGuiRenderer guiRenderer)
54 	{
55 		texture = guiRenderer.renderer.createTexture(textureFilename);
56 		foreach(ref FontInfo info; fontInfos)
57 		{
58 			uint font = guiRenderer.fontManager.createFont(info.filename, info.size);
59 			fonts[info.name] = guiRenderer.fontManager.getFont(font);
60 			fonts[info.name].verticalOffset = info.verticalOffset;
61 		}
62 	}
63 
64 	GuiStyle opIndex(string styleName)
65 	{
66 		return styles.get(styleName, null);
67 	}
68 
69 	override string toString() const
70 	{
71 		string result;
72 		result ~= "GuiSkin(Name:'" ~ name~"', textureFilename: '"~textureFilename~"'\n";
73 
74 		foreach(string styleName, ref style; styles)
75 		{
76 			result ~= styleName~"("~ style.toString ~ "\n";
77 		}
78 
79 		result ~=")\n";
80 		return result;
81 	}
82 }
83 
84 struct FontInfo
85 {
86 	string name;
87 	string filename;
88 	uint size;
89 	int verticalOffset;
90 }
91 
92 class GuiStyle
93 {
94 	GuiStyleState[string] states;
95 	string fontName; // "normal" by default
96 
97 	GuiStyleState opIndex(string stateName) const
98 	{
99 		const(GuiStyleState)* state = stateName in states;
100 		if (state is null) return states["normal"];
101 		return *state;
102 	}
103 
104 	override string toString() const
105 	{
106 		return "fixedBord:"~to!string(states["normal"].fixedBorders.toString) ~
107 			" contPadd:"~to!string(states["normal"].contentPadding.toString) ~
108 				" rect:"~to!string(states["normal"].atlasRect) ~
109 				"minSize: "~to!string(states["normal"].minSize);
110 	}
111 }
112 
113 struct GuiStyleState
114 {
115 	///Defines position and sizes of controls texture in skin teture
116 	Rect atlasRect;
117 
118 	/// Minimal size of the widget. If not explicitly specified equal to atlasRect size.
119 	/// It is highly recommended to set it to size equal or greater than atlasRect size to prevent glitches.
120 	ivec2 minSize;
121 
122 	/// Maximal size of the widget. By default equal to [0,0]. If maxSize is zero maxSize is not limited.
123 	ivec2 maxSize;
124 	
125 	/// Defines offset of content rect from widget borders.
126 	RectOffset contentPadding;
127 
128 	/// Defines outline of skin rect. Useful for drawing highlighting.
129 	/// If skin has drawn outline, with this parameter set
130 	/// guiRenderer will draw that sides outside of rect.
131 	RectOffset outline;
132 	
133 	/// Defines non-stretchable borders of texture.
134 	/// Corner parts will stay non-stretched.
135 	/// Left/right sides will be stretched vertically.
136 	/// Top/bottom sides will be stretched horizontally.
137 	/// Middle part will be stretched 
138 	RectOffset fixedBorders;
139 	
140 	Color textColor;
141 	Color backgroundColor;
142 }