-
Notifications
You must be signed in to change notification settings - Fork 4
/
image_example.zig
212 lines (196 loc) · 8.62 KB
/
image_example.zig
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
///
/// Creates a button, a label, a toggle and a radio using an image. Uses an image for the cursor as well.
/// From original example in C
/// https://tecgraf.puc-rio.br/iup/examples/C/image.c
const std = @import("std");
const iup = @import("iup.zig");
const fmt = std.fmt;
const MainLoop = iup.MainLoop;
const Dialog = iup.Dialog;
const Button = iup.Button;
const MessageDlg = iup.MessageDlg;
const Multiline = iup.Multiline;
const Label = iup.Label;
const Text = iup.Text;
const VBox = iup.VBox;
const HBox = iup.HBox;
const Menu = iup.Menu;
const SubMenu = iup.SubMenu;
const Separator = iup.Separator;
const Fill = iup.Fill;
const Item = iup.Item;
const FileDlg = iup.FileDlg;
const Toggle = iup.Toggle;
const Tabs = iup.Tabs;
const Frame = iup.Frame;
const Radio = iup.Radio;
const ScreenSize = iup.ScreenSize;
const Image = iup.Image;
const ImageRgb = iup.ImageRgb;
const ImageRgba = iup.ImageRgba;
var allocator: std.mem.Allocator = undefined;
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
allocator = gpa.allocator();
try MainLoop.open();
defer MainLoop.close();
var dlg = try create_dialog();
defer dlg.deinit();
try dlg.showXY(.Center, .Center);
try MainLoop.beginLoop();
}
fn create_dialog() !*Dialog {
var img_x = try images.getX();
var img_cursor = try images.getCursor();
var str_size = try fmt.allocPrintZ(allocator, "\"X\" image width = {}; \"X\" image height = {}", .{ img_x.getWidth(), img_x.getHeight() });
defer allocator.free(str_size);
return try (Dialog.init()
.setTitle("IUPforZig - Images")
.setSize(.Third, .Quarter)
.setCursor(img_cursor)
.setChildren(
.{
VBox.init()
.setChildren(
.{
VBox.init()
.setMargin(2, 2)
.setChildren(
.{
HBox.init()
.setGap(5)
.setChildren(
.{
Frame.init()
.setSize(60, 60)
.setTitle("button")
.setChildren(
.{
Button.init()
.setImage(img_x),
},
),
Frame.init()
.setSize(60, 60)
.setTitle("label")
.setChildren(
.{
Label.init()
.setImage(img_x),
},
),
Frame.init()
.setSize(60, 60)
.setTitle("toggle")
.setChildren(
.{
Toggle.init()
.setImage(img_x),
},
),
Frame.init()
.setSize(60, 60)
.setTitle("radio")
.setChildren(
.{
Radio.init()
.setChildren(
.{
VBox.init()
.setChildren(
.{
Toggle.init()
.setImage(img_x),
Toggle.init()
.setImage(img_x),
},
),
},
),
},
),
},
),
Fill.init(),
HBox.init()
.setChildren(
.{
Fill.init(),
Label.init().setTitle(str_size),
Fill.init(),
},
),
},
),
},
),
},
)
.unwrap());
}
const images = struct {
const pixelmap_x = [_]u8{ // zig fmt: off
1,2,3,3,3,3,3,3,3,2,1,
2,1,2,3,3,3,3,3,2,1,2,
3,2,1,2,3,3,3,2,1,2,3,
3,3,2,1,2,3,2,1,2,3,3,
3,3,3,2,1,2,1,2,3,3,3,
3,3,3,3,2,1,2,3,3,3,3,
3,3,3,2,1,2,1,2,3,3,3,
3,3,2,1,2,3,2,1,2,3,3,
3,2,1,2,3,3,3,2,1,2,3,
2,1,2,3,3,3,3,3,2,1,2,
1,2,3,3,3,3,3,3,3,2,1
}; // zig fmt: on
const pixelmap_cursor = [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0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
}; // zig fmt: on
pub fn getX() !*Image {
return try (Image.init(11, 11, pixelmap_x[0..])
.setHandle("img_x")
.setColors(1, .{ .r = 0, .g = 1, .b = 0 })
.setColors(2, .{ .r = 255, .g = 0, .b = 0 })
.setColors(3, .{ .r = 255, .g = 255, .b = 0 })
.unwrap());
}
pub fn getCursor() !*Image {
return try (Image.init(32, 32, pixelmap_cursor[0..])
.setHandle("img_cursor ")
.setHotspot(21, 10)
.setColors(1, .{ .r = 255, .g = 0, .b = 0 })
.setColors(2, .{ .r = 128, .g = 0, .b = 0 })
.unwrap());
}
};