Pioneer
Loading...
Searching...
No Matches
PiGui.h
Go to the documentation of this file.
1// Copyright © 2008-2024 Pioneer Developers. See AUTHORS.txt for details
2// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3
4#pragma once
5
6#include "FileSystem.h"
7#include "RefCounted.h"
8#include "imgui/imgui.h"
9
10#include "utils.h"
11
12#include <map>
13#include <unordered_set>
14
15union SDL_Event;
16
17namespace Graphics {
18 class Texture;
19 class Renderer;
20} // namespace Graphics
21
22class GuiApplication;
23
24namespace PiGui {
25
26 class RasterizeSVGTask;
27
29 RasterizeSVGResult(uint8_t *data, int width, int height) :
30 data(data),
31 width(width),
33 {
34 }
35
36 std::unique_ptr<uint8_t[]> data;
37 int width;
38 int height;
39 };
40
41 class PiFace {
42 public:
43 using UsedRange = std::pair<uint16_t, uint16_t>;
44
45 PiFace(const std::string &ttfname, float sizefactor) :
46 m_ttfname(ttfname),
47 m_sizefactor(sizefactor) {}
48
49 PiFace(const std::string &svgname, uint16_t startGlyph, int columns, int rows) :
50 m_svgname(svgname),
51 m_svgrows(rows),
52 m_svgcolumns(columns),
53 m_loadrange({ startGlyph, startGlyph + (rows * columns) })
54 {
55 }
56
57 const std::string &ttfname() const { return m_ttfname; }
58 float sizefactor() const { return m_sizefactor; }
59
60 const std::string &svgname() const { return m_svgname; }
61 bool isSvgFont() const { return !m_svgname.empty(); }
62
63 int svgRows() const { return m_svgrows; }
64 int svgCols() const { return m_svgcolumns; }
65
66 // Add this fontface at the specified size to the global font atlas
67 ImFont *addTTFFaceToAtlas(int pixelSize, ImFontConfig *config, ImVector<ImWchar> *ranges);
68
69 // Add this SVG fontface at the specified size to the global font atlas
70 ImFont *addSVGFaceToAtlas(int pixelSize, ImFontConfig *config, ImVector<ImWchar> *ranges, RasterizeSVGResult *svgData, ImVector<int> *outGlyphRects);
71 // Copy the pixel data for this fontface into the global font atlas
72 void finishSVGFaceData(ImFont *font, int pixelSize, RasterizeSVGResult *svgData, ImVector<int> *glyphRects);
73
74 private:
75 friend class Instance; // need access to some private data
76
77 std::string m_ttfname; // only the ttf name, it is automatically sought in data/fonts/
78 float m_sizefactor; // the requested pixelsize is multiplied by this factor
79
80 std::string m_svgname; // path to svg file for font-face
81 int m_svgrows; // number of character rows
82 int m_svgcolumns; // number of character columns
83 UsedRange m_loadrange; // start and end of font glyphs to load
84 };
85
87 public:
88 PiFontDefinition(const std::string &name) :
89 name(name) {}
90 PiFontDefinition(const std::string &name, const std::vector<PiFace> &faces) :
91 name(name),
92 faces(faces) {}
94 name("unknown") {}
95
96 std::string name;
97 std::vector<PiFace> faces;
98 bool loadDefaultRange = true;
99 };
100
101 class PiFont {
102 public:
103 using UsedRange = std::pair<uint16_t, uint16_t>;
104
106 ImFont *font;
108 std::unique_ptr<ImVector<int>> glyphRects;
110 };
111
112 PiFont(PiFontDefinition &fontDef, int pixelSize) :
113 m_fontDef(fontDef),
114 m_pixelsize(pixelSize)
115 {
116 }
117
118 const std::string &name() const { return m_fontDef.name; }
119 std::vector<PiFace> &faces() const { return m_fontDef.faces; }
120 const std::vector<UsedRange> &used_ranges() const { return m_used_ranges; }
121 int pixelsize() const { return m_pixelsize; }
122 const PiFontDefinition &definition() const { return m_fontDef; }
123
124 std::vector<CustomGlyphData> &custom_glyphs() { return m_custom_glyphs; }
125
126 void describe(bool withFaces = false) const;
127
128 bool addGlyph(unsigned short glyph);
129
130 private:
131 PiFontDefinition &m_fontDef;
132 int m_pixelsize;
133 std::vector<UsedRange> m_used_ranges;
134 std::vector<CustomGlyphData> m_custom_glyphs;
135 };
136
137 class InstanceRenderer;
138
139 /* Class to wrap ImGui. */
140 class Instance : public RefCounted {
141 public:
143
144 void Init(Graphics::Renderer *renderer);
145 void Uninit();
146
147 InstanceRenderer *GetRenderer() { return m_instanceRenderer.get(); }
148
149 // Call at the start of every frame. Calls ImGui::NewFrame() internally.
150 void NewFrame();
151
152 // Call at the end of a frame that you're not going to render the results of
153 void EndFrame();
154
155 // Calls ImGui::EndFrame() internally and does book-keeping before rendering.
156 void Render();
157
158 // Sets the ImGui Style object to use the predefined development tooling style
159 void SetDebugStyle();
160
161 // Sets the ImGui Style object to use the game UI style object as modified by Lua
162 void SetNormalStyle();
163
164 ImFont *AddFont(const std::string &name, int size);
165 ImFont *GetFont(const std::string &name, int size);
166
167 void AddGlyph(ImFont *font, unsigned short glyph);
168
169 bool ProcessEvent(SDL_Event *event);
170
171 private:
172 GuiApplication *m_app;
173 Graphics::Renderer *m_renderer;
174 std::unique_ptr<InstanceRenderer> m_instanceRenderer;
175
176 // Stores the pointer to the ini file name given to ImGui,
177 // so we can delete[] the memory again when uninitializing.
178 char *m_ioIniFilename;
179
180 std::map<std::pair<std::string, int>, ImFont *> m_fonts;
181 std::map<ImFont *, std::pair<std::string, int>> m_im_fonts;
182 std::map<std::pair<std::string, int>, PiFont> m_pi_fonts;
183 bool m_should_bake_fonts;
184
185 std::map<std::string, PiFontDefinition> m_font_definitions;
186
187 std::vector<ImVector<ImWchar> *> m_glyphRanges;
188 std::vector<RasterizeSVGTask *> m_svgFontTasks;
189 std::map<std::string, std::vector<RasterizeSVGResult>> m_svgFontRasterData;
190
191 ImGuiStyle m_debugStyle;
192 bool m_debugStyleActive;
193
194 void AddFontDefinition(const PiFontDefinition &font)
195 {
196 m_font_definitions[font.name] = font;
197 }
198
199 void LoadFontDefinitionFromFile(const std::string &filePath);
200
201 void BakeFonts();
202 void BakeFont(PiFont &font);
203 void ClearFonts();
204
205 RasterizeSVGResult *RequestSVGFaceData(PiFace *face, int pixelsize);
206 };
207
208 int RadialPopupSelectMenu(const ImVec2 center, const char *popup_id, int mouse_button, const std::vector<ImTextureID> &tex_ids, const std::vector<std::pair<ImVec2, ImVec2>> &uvs, const std::vector<ImU32> &colors, const std::vector<const char *> &tooltips, unsigned int size, unsigned int padding);
209 bool CircularSlider(const ImVec2 &center, float *v, float v_min, float v_max);
210
211 bool LowThrustButton(const char *label, const ImVec2 &size_arg, int thrust_level, const ImVec4 &bg_col, int frame_padding, ImColor gauge_fg, ImColor gauge_bg);
212 bool ButtonImageSized(ImTextureID user_texture_id, const ImVec2 &size, const ImVec2 &imgSize, const ImVec2 &uv0, const ImVec2 &uv1, int frame_padding, const ImVec4 &bg_col, const ImVec4 &tint_col);
213
214 void ThrustIndicator(const std::string &id_string, const ImVec2 &size, const ImVec4 &thrust, const ImVec4 &velocity, const ImVec4 &bg_col, int frame_padding, ImColor vel_fg, ImColor vel_bg, ImColor thrust_fg, ImColor thrust_bg);
215
216 // we need to know if the change was made by direct input or the change was
217 // made by mouse movement, to successfully serve values such as YYYY-MM-DD
218 // when changing with the mouse, we get some internal delta value and add
219 // it to previous internal value
220 // when we receive direct input from the keyboard, we get a completely
221 // different (face value), an integer of the form YYYY-MM-DD so we just
222 // convert it to internal value.
223 // Also see: data/pigui/modules/new-game-window/location.lua
225 DragChangeMode IncrementDrag(const char *label, double &v, float v_speed, const double v_min, const double v_max, const char *format, bool draw_progress_bar);
226
227 inline bool WantCaptureMouse()
228 {
229 return ImGui::GetIO().WantCaptureMouse;
230 }
231
233 {
234 return ImGui::GetIO().WantCaptureKeyboard;
235 }
236
237 std::vector<Graphics::Texture *> &GetSVGTextures();
238 ImTextureID RenderSVG(Graphics::Renderer *renderer, std::string svgFilename, int width, int height);
239
240} //namespace PiGui
Definition: Renderer.h:45
Definition: GuiApplication.h:27
Definition: PiGuiRenderer.h:13
Definition: PiGui.h:140
InstanceRenderer * GetRenderer()
Definition: PiGui.h:147
bool ProcessEvent(SDL_Event *event)
Definition: PiGui.cpp:424
ImFont * GetFont(const std::string &name, int size)
Definition: PiGui.cpp:315
ImFont * AddFont(const std::string &name, int size)
Definition: PiGui.cpp:353
void AddGlyph(ImFont *font, unsigned short glyph)
Definition: PiGui.cpp:328
void EndFrame()
Definition: PiGui.cpp:486
void Uninit()
Definition: PiGui.cpp:677
void Render()
Definition: PiGui.cpp:495
void SetNormalStyle()
Definition: PiGui.cpp:307
void Init(Graphics::Renderer *renderer)
Definition: PiGui.cpp:386
void NewFrame()
Definition: PiGui.cpp:431
void SetDebugStyle()
Definition: PiGui.cpp:299
Definition: PiGui.h:41
ImFont * addTTFFaceToAtlas(int pixelSize, ImFontConfig *config, ImVector< ImWchar > *ranges)
Definition: PiGui.cpp:702
std::pair< uint16_t, uint16_t > UsedRange
Definition: PiGui.h:43
float sizefactor() const
Definition: PiGui.h:58
const std::string & svgname() const
Definition: PiGui.h:60
int svgRows() const
Definition: PiGui.h:63
int svgCols() const
Definition: PiGui.h:64
ImFont * addSVGFaceToAtlas(int pixelSize, ImFontConfig *config, ImVector< ImWchar > *ranges, RasterizeSVGResult *svgData, ImVector< int > *outGlyphRects)
Definition: PiGui.cpp:712
PiFace(const std::string &svgname, uint16_t startGlyph, int columns, int rows)
Definition: PiGui.h:49
PiFace(const std::string &ttfname, float sizefactor)
Definition: PiGui.h:45
bool isSvgFont() const
Definition: PiGui.h:61
const std::string & ttfname() const
Definition: PiGui.h:57
void finishSVGFaceData(ImFont *font, int pixelSize, RasterizeSVGResult *svgData, ImVector< int > *glyphRects)
Definition: PiGui.cpp:745
Definition: PiGui.h:101
int pixelsize() const
Definition: PiGui.h:121
std::pair< uint16_t, uint16_t > UsedRange
Definition: PiGui.h:103
void describe(bool withFaces=false) const
Definition: PiGui.cpp:800
std::vector< CustomGlyphData > & custom_glyphs()
Definition: PiGui.h:124
const PiFontDefinition & definition() const
Definition: PiGui.h:122
std::vector< PiFace > & faces() const
Definition: PiGui.h:119
const std::string & name() const
Definition: PiGui.h:118
PiFont(PiFontDefinition &fontDef, int pixelSize)
Definition: PiGui.h:112
bool addGlyph(unsigned short glyph)
Definition: PiGui.cpp:784
const std::vector< UsedRange > & used_ranges() const
Definition: PiGui.h:120
Definition: RefCounted.h:11
Definition: Background.h:14
Definition: GuiApplication.h:18
DragChangeMode IncrementDrag(const char *label, double &v, float v_speed, const double v_min, const double v_max, const char *format, bool draw_progress_bar)
Definition: Widgets.cpp:320
bool WantCaptureMouse()
Definition: PiGui.h:227
DragChangeMode
Definition: PiGui.h:224
bool LowThrustButton(const char *label, const ImVec2 &size_arg, int thrust_level, const ImVec4 &bg_col, int frame_padding, ImColor gauge_fg, ImColor gauge_bg)
Definition: Widgets.cpp:222
std::vector< Graphics::Texture * > & GetSVGTextures()
Definition: PiGui.cpp:36
ImTextureID RenderSVG(Graphics::Renderer *renderer, std::string svgFilename, int width, int height)
Definition: PiGui.cpp:168
int RadialPopupSelectMenu(const ImVec2 center, const char *popup_id, int mouse_button, const std::vector< ImTextureID > &tex_ids, const std::vector< std::pair< ImVec2, ImVec2 > > &uvs, const std::vector< ImU32 > &colors, const std::vector< const char * > &tooltips, unsigned int size, unsigned int padding)
Definition: Widgets.cpp:11
bool CircularSlider(const ImVec2 &center, float *v, float v_min, float v_max)
Definition: Widgets.cpp:121
bool WantCaptureKeyboard()
Definition: PiGui.h:232
bool ButtonImageSized(ImTextureID user_texture_id, const ImVec2 &size, const ImVec2 &imgSize, const ImVec2 &uv0, const ImVec2 &uv1, int frame_padding, const ImVec4 &bg_col, const ImVec4 &tint_col)
Definition: Widgets.cpp:280
void ThrustIndicator(const std::string &id_string, const ImVec2 &size, const ImVec4 &thrust, const ImVec4 &velocity, const ImVec4 &bg_col, int frame_padding, ImColor vel_fg, ImColor vel_bg, ImColor thrust_fg, ImColor thrust_bg)
Definition: Widgets.cpp:159
Definition: PiGui.h:86
std::string name
Definition: PiGui.h:96
PiFontDefinition(const std::string &name, const std::vector< PiFace > &faces)
Definition: PiGui.h:90
bool loadDefaultRange
Definition: PiGui.h:98
PiFontDefinition(const std::string &name)
Definition: PiGui.h:88
PiFontDefinition()
Definition: PiGui.h:93
std::vector< PiFace > faces
Definition: PiGui.h:97
Definition: PiGui.h:105
RasterizeSVGResult * svgData
Definition: PiGui.h:109
std::unique_ptr< ImVector< int > > glyphRects
Definition: PiGui.h:108
ImFont * font
Definition: PiGui.h:106
PiFace * face
Definition: PiGui.h:107
Definition: PiGui.h:28
RasterizeSVGResult(uint8_t *data, int width, int height)
Definition: PiGui.h:29
int width
Definition: PiGui.h:37
std::unique_ptr< uint8_t[]> data
Definition: PiGui.h:36
int height
Definition: PiGui.h:38