1 // =================================
 2 // Copyright (c) 2024 Seppo Laakko
 3 // Distributed under the MIT license
 4 // =================================
 5 
 6 using System;
 7 using System.Collections;
 8 
 9 namespace System.Windows
10 {
11     public class ImageList
12     {
13         public ImageList()
14         {
15         }
16         [nodiscard]
17         public Result<bool> AddImage(const string& imageName)
18         {
19             auto bitmapResult = Bitmap.FromResource(imageName);
20             if (bitmapResult.Error())
21             {
22                 return Result<bool>(ErrorId(bitmapResult.GetErrorId()));
23             }
24             UniquePtr<Bitmap> bm(new Bitmap(Rvalue(bitmapResult.Value())));
25             if (bm->Error())
26             {
27                 return Result<bool>(ErrorId(bm->GetErrorId()));
28             }
29             AddImage(imageNamebm.Release());
30             return Result<bool>(true);
31         }
32         [nodiscard]
33         public Result<bool> AddDisabledImage(const string& imageName)
34         {
35             int imageIndex = GetImageIndex(imageName);
36             if (imageIndex != -1)
37             {
38                 Bitmap* bitmap = images[imageIndex].Get();
39                 auto grayBitmapResult = bitmap->ToGrayBitmap();
40                 if (grayBitmapResult.Error())
41                 {
42                     return Result<bool>(ErrorId(grayBitmapResult.GetErrorId()));
43                 }
44                 UniquePtr<Bitmap> disabledImage = new Bitmap(Rvalue(grayBitmapResult.Value()));
45                 if (disabledImage->Error())
46                 {
47                     return Result<bool>(ErrorId(disabledImage->GetErrorId()));
48                 }
49                 AddImage(imageName + ".disabled"disabledImage.Release());
50             }
51             else
52             {
53                 int errorId = AllocateError("image \'" + imageName + "\' not found");
54                 return Result<bool>(ErrorId(errorId));
55             }
56             return Result<bool>(true);
57         }
58         public void AddImage(const string& imageNameBitmap* bitmap)
59         {
60             int imageIndex = cast<int>(images.Count());
61             images.Add(UniquePtr<Bitmap>(bitmap));
62             imageIndexMap[imageName] = imageIndex;
63         }
64         public int GetImageIndex(const string& imageName)
65         {
66             auto it = imageIndexMap.CFind(imageName);
67             if (it != imageIndexMap.CEnd())
68             {
69                 return it->second;
70             }
71             else
72             {
73                 return -1;
74             }
75         }
76         public int GetDisabledImageIndex(const string& imageName)
77         {
78             return GetImageIndex(imageName + ".disabled");
79         }
80         public Bitmap* GetImage(int imageIndex) const
81         {
82             if (imageIndex >= 0 && imageIndex < images.Count())
83             {
84                 return images[imageIndex].Get();
85             }
86             else
87             {
88                 return null;
89             }
90         }
91         private Map<stringint> imageIndexMap;
92         private List<UniquePtr<Bitmap>> images;
93     }
94 }