Skip to content

Commit f9deefa

Browse files
committed
extended FontAtlas to load fonts from files.
1 parent f3d1e5a commit f9deefa

8 files changed

+115
-29
lines changed

Font.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package imgui
2+
3+
// #include "imguiWrapperTypes.h"
4+
import "C"
5+
6+
// Font describes one loaded font in an atlas.
7+
type Font uintptr
8+
9+
// DefaultFont can be used to refer to the default font of the current font atlas without
10+
// having the actual font reference.
11+
const DefaultFont Font = 0
12+
13+
func (font Font) handle() C.IggFont {
14+
return C.IggFont(font)
15+
}

FontAtlas.go

+15
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@ func (atlas FontAtlas) handle() C.IggFontAtlas {
1818
return C.IggFontAtlas(atlas)
1919
}
2020

21+
// AddFontDefault adds the default font to the atlas. This is done by default if you do not call any
22+
// of the AddFont* methods before retrieving the texture data.
23+
func (atlas FontAtlas) AddFontDefault() Font {
24+
fontHandle := C.iggAddFontDefault(atlas.handle())
25+
return Font(fontHandle)
26+
}
27+
28+
// AddFontFromFileTTF attempts to load a font from given TTF file.
29+
func (atlas FontAtlas) AddFontFromFileTTF(filename string, sizePixels float32) Font {
30+
filenameArg, filenameFin := wrapString(filename)
31+
defer filenameFin()
32+
fontHandle := C.iggAddFontFromFileTTF(atlas.handle(), filenameArg, C.float(sizePixels))
33+
return Font(fontHandle)
34+
}
35+
2136
// TextureDataAlpha8 returns the image in 8-bit alpha values for the font atlas.
2237
// The returned image is valid as long as the font atlas is.
2338
func (atlas FontAtlas) TextureDataAlpha8() *Alpha8Image {

FontAtlasWrapper.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22
#include "FontAtlasWrapper.h"
33
#include "WrapperConverter.h"
44

5+
IggFont iggAddFontDefault(IggFontAtlas handle)
6+
{
7+
ImFontAtlas *fontAtlas = reinterpret_cast<ImFontAtlas *>(handle);
8+
ImFont *font = fontAtlas->AddFontDefault();
9+
return static_cast<IggFont>(font);
10+
}
11+
12+
IggFont iggAddFontFromFileTTF(IggFontAtlas handle, char const *filename, float sizePixels)
13+
{
14+
ImFontAtlas *fontAtlas = reinterpret_cast<ImFontAtlas *>(handle);
15+
ImFont *font = fontAtlas->AddFontFromFileTTF(filename, sizePixels);
16+
return static_cast<IggFont>(font);
17+
}
18+
519
void iggFontAtlasGetTexDataAsAlpha8(IggFontAtlas handle, unsigned char **pixels,
620
int *width, int *height, int *bytesPerPixel)
721
{

FontAtlasWrapper.h

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ extern "C"
77
{
88
#endif
99

10+
extern IggFont iggAddFontDefault(IggFontAtlas handle);
11+
extern IggFont iggAddFontFromFileTTF(IggFontAtlas handle, char const *filename, float sizePixels);
12+
1013
extern void iggFontAtlasGetTexDataAsAlpha8(IggFontAtlas handle, unsigned char **pixels,
1114
int *width, int *height, int *bytesPerPixel);
1215
extern void iggFontAtlasSetTextureID(IggFontAtlas handle, IggTextureID id);

imgui.go

+36-17
Original file line numberDiff line numberDiff line change
@@ -129,25 +129,14 @@ func SetNextWindowFocus() {
129129
C.iggSetNextWindowFocus()
130130
}
131131

132-
// PushStyleVarFloat pushes a float value on the stack to temporarily modify a style variable.
133-
func PushStyleVarFloat(id StyleVarID, value float32) {
134-
C.iggPushStyleVarFloat(C.int(id), C.float(value))
135-
}
136-
137-
// PushStyleVarVec2 pushes a Vec2 value on the stack to temporarily modify a style variable.
138-
func PushStyleVarVec2(id StyleVarID, value Vec2) {
139-
valueArg, _ := value.wrapped()
140-
C.iggPushStyleVarVec2(C.int(id), valueArg)
141-
}
142-
143-
// PopStyleVarV reverts the given amount of style variable changes.
144-
func PopStyleVarV(count int) {
145-
C.iggPopStyleVar(C.int(count))
132+
// PushFont adds the given font on the stack. Use DefaultFont to refer to the default font.
133+
func PushFont(font Font) {
134+
C.iggPushFont(font.handle())
146135
}
147136

148-
// PopStyleVar calls PopStyleVarV(1).
149-
func PopStyleVar() {
150-
PopStyleVarV(1)
137+
// PopFont removes the previously pushed font from the stack.
138+
func PopFont() {
139+
C.iggPopFont()
151140
}
152141

153142
// PushStyleColor pushes the current style color for given ID on a stack and sets the given one.
@@ -167,6 +156,27 @@ func PopStyleColor() {
167156
PopStyleColorV(1)
168157
}
169158

159+
// PushStyleVarFloat pushes a float value on the stack to temporarily modify a style variable.
160+
func PushStyleVarFloat(id StyleVarID, value float32) {
161+
C.iggPushStyleVarFloat(C.int(id), C.float(value))
162+
}
163+
164+
// PushStyleVarVec2 pushes a Vec2 value on the stack to temporarily modify a style variable.
165+
func PushStyleVarVec2(id StyleVarID, value Vec2) {
166+
valueArg, _ := value.wrapped()
167+
C.iggPushStyleVarVec2(C.int(id), valueArg)
168+
}
169+
170+
// PopStyleVarV reverts the given amount of style variable changes.
171+
func PopStyleVarV(count int) {
172+
C.iggPopStyleVar(C.int(count))
173+
}
174+
175+
// PopStyleVar calls PopStyleVarV(1).
176+
func PopStyleVar() {
177+
PopStyleVarV(1)
178+
}
179+
170180
// PushItemWidth sets width of items for the common item+label case, in pixels.
171181
// 0.0f = default to ~2/3 of windows width, >0.0f: width in pixels,
172182
// <0.0f align xx pixels to the right of window (so -1.0f always align width to the right side).
@@ -205,6 +215,15 @@ func Text(text string) {
205215
C.iggTextUnformatted(textArg)
206216
}
207217

218+
// LabelText adds text+label aligned the same way as value+label widgets.
219+
func LabelText(label, text string) {
220+
labelArg, labelFin := wrapString(label)
221+
defer labelFin()
222+
textArg, textFin := wrapString(text)
223+
defer textFin()
224+
C.iggLabelText(labelArg, textArg)
225+
}
226+
208227
// ButtonV returning true if it is pressed.
209228
func ButtonV(id string, size Vec2) bool {
210229
idArg, idFin := wrapString(id)

imguiWrapper.cpp

+26-10
Original file line numberDiff line numberDiff line change
@@ -110,20 +110,15 @@ void iggSetNextWindowFocus(void)
110110
ImGui::SetNextWindowFocus();
111111
}
112112

113-
void iggPushStyleVarFloat(int index, float value)
113+
void iggPushFont(IggFont handle)
114114
{
115-
ImGui::PushStyleVar(index, value);
115+
ImFont *font = reinterpret_cast<ImFont *>(handle);
116+
ImGui::PushFont(font);
116117
}
117118

118-
void iggPushStyleVarVec2(int index, IggVec2 const *value)
119+
void iggPopFont(void)
119120
{
120-
Vec2Wrapper valueArg(value);
121-
ImGui::PushStyleVar(index, *valueArg);
122-
}
123-
124-
void iggPopStyleVar(int count)
125-
{
126-
ImGui::PopStyleVar(count);
121+
ImGui::PopFont();
127122
}
128123

129124
void iggPushStyleColor(int index, IggVec4 const *col)
@@ -137,6 +132,22 @@ void iggPopStyleColor(int count)
137132
ImGui::PopStyleColor(count);
138133
}
139134

135+
void iggPushStyleVarFloat(int index, float value)
136+
{
137+
ImGui::PushStyleVar(index, value);
138+
}
139+
140+
void iggPushStyleVarVec2(int index, IggVec2 const *value)
141+
{
142+
Vec2Wrapper valueArg(value);
143+
ImGui::PushStyleVar(index, *valueArg);
144+
}
145+
146+
void iggPopStyleVar(int count)
147+
{
148+
ImGui::PopStyleVar(count);
149+
}
150+
140151
void iggPushItemWidth(float width)
141152
{
142153
ImGui::PushItemWidth(width);
@@ -162,6 +173,11 @@ void iggTextUnformatted(char const *text)
162173
ImGui::TextUnformatted(text);
163174
}
164175

176+
void iggLabelText(char const *label, char const *text)
177+
{
178+
ImGui::LabelText(label, "%s", text);
179+
}
180+
165181
IggBool iggButton(char const *label, IggVec2 const *size)
166182
{
167183
Vec2Wrapper sizeArg(size);

imguiWrapper.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,21 @@ extern void iggSetNextWindowPos(IggVec2 const *pos, int cond, IggVec2 const *piv
3232
extern void iggSetNextWindowSize(IggVec2 const *size, int cond);
3333
extern void iggSetNextWindowFocus(void);
3434

35+
extern void iggPushFont(IggFont handle);
36+
extern void iggPopFont(void);
37+
extern void iggPushStyleColor(int index, IggVec4 const *col);
38+
extern void iggPopStyleColor(int count);
3539
extern void iggPushStyleVarFloat(int index, float value);
3640
extern void iggPushStyleVarVec2(int index, IggVec2 const *value);
3741
extern void iggPopStyleVar(int count);
38-
extern void iggPushStyleColor(int index, IggVec4 const *col);
39-
extern void iggPopStyleColor(int count);
4042

4143
extern void iggPushItemWidth(float width);
4244
extern void iggPopItemWidth(void);
4345
extern void iggPushTextWrapPos(float wrapPosX);
4446
extern void iggPopTextWrapPos(void);
4547

4648
extern void iggTextUnformatted(char const *text);
49+
extern void iggLabelText(char const *label, char const *text);
4750

4851
extern IggBool iggButton(char const *label, IggVec2 const *size);
4952
extern IggBool iggCheckbox(char const *label, IggBool *selected);

imguiWrapperTypes.h

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ typedef void *IggDrawCmd;
1313
typedef void *IggDrawData;
1414
typedef void *IggDrawList;
1515
typedef void *IggFontAtlas;
16+
typedef void *IggFont;
1617
typedef void *IggGuiStyle;
1718
typedef void *IggIO;
1819

0 commit comments

Comments
 (0)