1 module rpui.resources.images;
2 
3 import std.path;
4 import gapi.texture;
5 import rpui.paths;
6 
7 /**
8  * Get textures from files.
9  */
10 final class ImagesRes {
11     const Paths paths;
12     private Texture2D[string] textures;
13     private string uiTheme;
14 
15     this(in string uiTheme = "") {
16         this.paths = createPathes();
17         this.uiTheme = uiTheme;
18     }
19 
20     ~this() {
21         foreach (Texture2D texture; textures) {
22             deleteTexture2D(texture);
23         }
24     }
25 
26     /**
27      * Method loads the texture from `fileName` relative to $(I res/images).
28      */
29     Texture2D getTexture(in string fileName) {
30         const key = "res:" ~ fileName;
31 
32         if (key !in textures) {
33             const path = buildPath(paths.resources, "images", fileName);
34             textures[key] = createTexture2DFromFile(path);
35         }
36 
37         return textures[key];
38     }
39 
40     /**
41      * Method loads the texture from `fileName` for current UI theme
42      * i.e. relative to $(I res/ui/themes/images/{theme name}/images).
43      */
44     Texture2D getTextureForUiTheme(in string fileName) {
45         debug assert(uiTheme != "");
46         const key = "ui:" ~ fileName;
47 
48         if (key !in textures) {
49             const path = buildPath(paths.resources, "ui", "themes",
50                                    uiTheme, "images", fileName);
51 
52             textures[key] = createTexture2DFromFile(path);
53         }
54 
55         return textures[key];
56     }
57 
58     /**
59      * Method loads the texture from `fileName` which is absolute path.
60      */
61     Texture2D getTextureFromAbsolutePath(in string fileName) {
62         const key = "absolute:" ~ fileName;
63 
64         if (fileName !in textures) {
65             textures[key] = createTexture2DFromFile(fileName);
66         }
67 
68         return textures[key];
69     }
70 }