/
BitmapResource.ec
190 lines (169 loc) · 5.76 KB
/
BitmapResource.ec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
namespace gfx;
import "Window"
static Array<FileFilter> filters
{ [
{
$"Image Files (*.jpg, *.jpeg, *.bmp, *.pcx, *.png, *.gif)",
"jpg, jpeg, bmp, pcx, png, gif"
}
] };
static Array<FileType> types
{ [
{ $"Image", "jpg" }
] };
static FileDialog fileDialog { autoCreate = false, filters = filters.array, sizeFilters = filters.count * sizeof(FileFilter), types = types.array, sizeTypes = types.count * sizeof(FileType), text = $"Select Image" };
public class BitmapResource : Resource
{
char * fileName;
Bitmap bitmap;
bool grayed, mono, transparent;
bool alphaBlend;
bool mipMaps;
int count;
bool keepData;
BitmapResource()
{
transparent = true;
}
~BitmapResource()
{
if(bitmap != null)
{
//if(!grayed)
// Logf("Freeing %s (%d)\n", fileName, bitmap.picture);
// bitmap.Free(bitmap);
delete bitmap;
}
if(fileName)
delete fileName;
}
void Load(BitmapResource copy, DisplaySystem displaySystem)
{
delete fileName;
fileName = CopyString(copy.fileName);
grayed = copy.grayed;
mono = copy.mono;
transparent = copy.transparent;
alphaBlend = copy.alphaBlend;
mipMaps = copy.mipMaps;
keepData = copy.keepData;
if(fileName)
{
DisplaySystem ds = displaySystem;
bitmap = Bitmap { alphaBlend = (alphaBlend && displaySystem.pixelFormat != pixelFormat8), keepData = keepData };
#if defined(__WIN32__)
// if(bitmap.alphaBlend) ds = null;
#endif
if(
(mipMaps && !bitmap.LoadMipMaps(fileName, null, ds)) ||
(!mipMaps && mono && !bitmap.LoadMonochrome(fileName, null, ds)) ||
(!mipMaps && !mono && grayed && !bitmap.LoadGrayed(fileName, null, ds)) ||
(!mipMaps && !mono && !grayed && transparent && !bitmap.LoadT(fileName, null, ds)) ||
(!mipMaps && !mono && !grayed && !transparent && !bitmap.Load(fileName, null, ds)))
delete bitmap;
if(bitmap && bitmap.alphaBlend)
{
bitmap.Convert(null, pixelFormat888, null);
bitmap.displaySystem = displaySystem;
}
}
}
void Reference(BitmapResource reference)
{
bitmap = reference.bitmap;
count++;
}
void Dereference()
{
count--;
if(!count)
bitmap = null;
}
Window OnEdit(DataBox window, DataBox master, int x, int y, int w, int h, void * userData)
{
Window editData = class::OnEdit(window, master, x + 24, y, w - 48, h, userData);
Button browse
{
window, master = editData, inactive = true, text = "...", hotKey = f2,
position = { Max(x + 24, x + w - 24), y }, size = { 24, h };
bool NotifyClicked(Button button, int x, int y, Modifiers mods)
{
DataBox master = (DataBox)this.master;
fileDialog.master = rootWindow;
if(fileDialog.Modal() == ok)
{
const char * filePath = fileDialog.filePath;
BitmapResource resource { fileName = filePath };
master.SetData(resource, false);
// TOCHECK: Why do we need to Refresh here?
master.Refresh();
// TODO: This is a Button?
// contents = filePath;
}
return true;
}
};
editData.anchor.right = 24;
browse.Create();
return editData;
}
void OnDisplay(Surface surface, int x, int y, int width, void * fieldData, Alignment alignment, DataDisplayFlags displayFlags)
{
char * string = this ? fileName : null;
Bitmap bitmap = this ? this.bitmap : null;
// TODO: This isn't an ideal way of obtaining the clipped height, will fail on hidden areas
int yOffset = (1+surface.box.bottom - surface.box.top - 17)/2;
/* GCC-4.4 Bug!!
if(!string) string = "(none)";
surface.WriteTextDots(alignment, x + 24, y + 1, width - 24, string, strlen(string));
*/
if(string)
surface.WriteTextDots(alignment, x + 24, y + 1, width - 24, string, strlen(string));
else
surface.WriteTextDots(alignment, x + 24, y + 1, width - 24, "(none)", 6);
y += yOffset-1;
surface.SetBackground(white);
surface.Area(x - 4, y, x + 20, y + 15);
if(bitmap)
surface.Filter(bitmap, x, y + 2, 0,0, 18, 12, bitmap.width, bitmap.height);
surface.SetForeground(black);
surface.Rectangle(x-1, y + 1, x + 18, y + 14);
}
public:
property const char * fileName
{
set
{
delete fileName;
fileName = CopyString(value);
if(value && SearchString(value, 0, ".png", false, true))
alphaBlend = true;
}
get { return this ? fileName : null; }
};
property bool grayed { set { grayed = value; } get { return this ? grayed : false; } };
property bool monochrome
{
set
{
mono = value;
}
get { return this ? mono : false; }
};
property bool transparent { set { transparent = value; } get { return this ? transparent : false; } isset { return (this && !transparent) ? true : false; } };
property bool alphaBlend
{
set { alphaBlend = value; }
get { return this ? alphaBlend : false; }
isset { return alphaBlend && (!fileName || !SearchString(fileName, 0, ".png", false, true)); }
};
property bool mipMaps
{
set { mipMaps = value; }
get { return this ? mipMaps : false; }
isset { return mipMaps; }
};
property bool keepData { set { keepData = value; } get { return this ? keepData : false; } };
property Bitmap bitmap { get { return this ? bitmap : null; } set { bitmap = value; if(bitmap) incref bitmap; } };
property Window window { set { if(value) value.AddResource(this); } };
};