Pioneer
PiGui.h
Go to the documentation of this file.
1 // Copyright © 2008-2023 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 <unordered_set>
13 
14 namespace Graphics {
15  class Texture;
16  class Renderer;
17 } // namespace Graphics
18 
19 namespace PiGui {
20 
21  class PiFace {
22  public:
23  using UsedRange = std::pair<uint16_t, uint16_t>;
24  PiFace(const std::string &ttfname, float sizefactor) :
25  m_ttfname(ttfname),
26  m_sizefactor(sizefactor) {}
27 
28  const std::string &ttfname() const { return m_ttfname; }
29 
30  float sizefactor() const { return m_sizefactor; }
31 
32  //std::unordered_map<unsigned short, unsigned short> &invalid_glyphs() const { return m_invalid_glyphs; }
33  const std::vector<UsedRange> &used_ranges() const { return m_used_ranges; }
34 
35  bool isValidGlyph(unsigned short glyph) const;
36  void addGlyph(unsigned short glyph);
37  void sortUsedRanges() const;
38 
39  private:
40  friend class Instance; // need access to some private data
41 
42  std::string m_ttfname; // only the ttf name, it is automatically sought in data/fonts/
43  float m_sizefactor; // the requested pixelsize is multiplied by this factor
44 
45  std::unordered_set<unsigned short> m_invalid_glyphs;
46  mutable std::vector<UsedRange> m_used_ranges;
47 
48  ImVector<ImWchar> m_imgui_ranges;
49  };
50 
51  class PiFont {
52  public:
53  PiFont(const std::string &name) :
54  m_name(name) {}
55  PiFont(const std::string &name, const std::vector<PiFace> &faces) :
56  m_name(name),
57  m_faces(faces) {}
58  PiFont() :
59  m_name("unknown") {}
60 
61  const std::vector<PiFace> &faces() const { return m_faces; }
62  std::vector<PiFace> &faces() { return m_faces; }
63 
64  const std::string &name() const { return m_name; }
65 
66  int pixelsize() const { return m_pixelsize; }
67  void setPixelsize(int pixelsize) { m_pixelsize = pixelsize; }
68 
69  void describe() const
70  {
71  Output("font %s:\n", name().c_str());
72  for (const PiFace &face : faces()) {
73  Output("- %s %f\n", face.ttfname().c_str(), face.sizefactor());
74  }
75  }
76 
77  private:
78  std::string m_name;
79  std::vector<PiFace> m_faces;
80  int m_pixelsize;
81  };
82 
83  class InstanceRenderer;
84 
85  /* Class to wrap ImGui. */
86  class Instance : public RefCounted {
87  public:
88  Instance();
89 
90  void Init(Graphics::Renderer *renderer);
91  void Uninit();
92 
93  InstanceRenderer *GetRenderer() { return m_instanceRenderer.get(); }
94 
95  // Call at the start of every frame. Calls ImGui::NewFrame() internally.
96  void NewFrame();
97 
98  // Call at the end of a frame that you're not going to render the results of
99  void EndFrame();
100 
101  // Calls ImGui::EndFrame() internally and does book-keeping before rendering.
102  void Render();
103 
104  // Sets the ImGui Style object to use the predefined development tooling style
105  void SetDebugStyle();
106 
107  // Sets the ImGui Style object to use the game UI style object as modified by Lua
108  void SetNormalStyle();
109 
110  ImFont *AddFont(const std::string &name, int size);
111  ImFont *GetFont(const std::string &name, int size);
112 
113  void AddGlyph(ImFont *font, unsigned short glyph);
114 
115  bool ProcessEvent(SDL_Event *event);
116 
117  void RefreshFontsTexture();
118 
119  private:
120  Graphics::Renderer *m_renderer;
121  std::unique_ptr<InstanceRenderer> m_instanceRenderer;
122 
123  // Stores the pointer to the ini file name given to ImGui,
124  // so we can delete[] the memory again when uninitializing.
125  char *m_ioIniFilename;
126 
127  std::map<std::pair<std::string, int>, ImFont *> m_fonts;
128  std::map<ImFont *, std::pair<std::string, int>> m_im_fonts;
129  std::map<std::pair<std::string, int>, PiFont> m_pi_fonts;
130  bool m_should_bake_fonts;
131 
132  std::map<std::string, PiFont> m_font_definitions;
133 
134  ImGuiStyle m_debugStyle;
135  bool m_debugStyleActive;
136 
137  void BakeFonts();
138  void BakeFont(PiFont &font);
139  void AddFontDefinition(const PiFont &font) { m_font_definitions[font.name()] = font; }
140  void ClearFonts();
141  };
142 
143  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);
144  bool CircularSlider(const ImVec2 &center, float *v, float v_min, float v_max);
145 
146  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);
147  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);
148 
149  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);
150 
151  void IncrementDrag(const std::string &label, int &v, const int v_min, const int v_max, const std::string &format);
152 
153  inline bool WantCaptureMouse()
154  {
155  return ImGui::GetIO().WantCaptureMouse;
156  }
157 
158  inline bool WantCaptureKeyboard()
159  {
160  return ImGui::GetIO().WantCaptureKeyboard;
161  }
162 
163  std::vector<Graphics::Texture *> &GetSVGTextures();
164  ImTextureID RenderSVG(Graphics::Renderer *renderer, std::string svgFilename, int width, int height);
165 
166 } //namespace PiGui
Definition: Renderer.h:44
Definition: PiGuiRenderer.h:13
Definition: PiGui.h:86
bool ProcessEvent(SDL_Event *event)
Definition: PiGui.cpp:373
Instance()
Definition: PiGui.cpp:197
ImFont * GetFont(const std::string &name, int size)
Definition: PiGui.cpp:261
void RefreshFontsTexture()
Definition: PiGui.cpp:331
ImFont * AddFont(const std::string &name, int size)
Definition: PiGui.cpp:304
void AddGlyph(ImFont *font, unsigned short glyph)
Definition: PiGui.cpp:274
void EndFrame()
Definition: PiGui.cpp:420
void Uninit()
Definition: PiGui.cpp:535
void Render()
Definition: PiGui.cpp:429
void SetNormalStyle()
Definition: PiGui.cpp:253
void Init(Graphics::Renderer *renderer)
Definition: PiGui.cpp:339
InstanceRenderer * GetRenderer()
Definition: PiGui.h:93
void NewFrame()
Definition: PiGui.cpp:380
void SetDebugStyle()
Definition: PiGui.cpp:245
Definition: PiGui.h:21
std::pair< uint16_t, uint16_t > UsedRange
Definition: PiGui.h:23
float sizefactor() const
Definition: PiGui.h:30
bool isValidGlyph(unsigned short glyph) const
Definition: PiGui.cpp:560
void addGlyph(unsigned short glyph)
Definition: PiGui.cpp:566
PiFace(const std::string &ttfname, float sizefactor)
Definition: PiGui.h:24
const std::vector< UsedRange > & used_ranges() const
Definition: PiGui.h:33
void sortUsedRanges() const
Definition: PiGui.cpp:581
const std::string & ttfname() const
Definition: PiGui.h:28
Definition: PiGui.h:51
PiFont()
Definition: PiGui.h:58
const std::string & name() const
Definition: PiGui.h:64
void describe() const
Definition: PiGui.h:69
PiFont(const std::string &name)
Definition: PiGui.h:53
int pixelsize() const
Definition: PiGui.h:66
std::vector< PiFace > & faces()
Definition: PiGui.h:62
PiFont(const std::string &name, const std::vector< PiFace > &faces)
Definition: PiGui.h:55
void setPixelsize(int pixelsize)
Definition: PiGui.h:67
const std::vector< PiFace > & faces() const
Definition: PiGui.h:61
Definition: RefCounted.h:11
Definition: Background.h:14
Definition: LuaBody.cpp:29
bool WantCaptureMouse()
Definition: PiGui.h:153
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:223
std::vector< Graphics::Texture * > & GetSVGTextures()
Definition: PiGui.cpp:33
void IncrementDrag(const std::string &label, int &v, const int v_min, const int v_max, const std::string &format)
Definition: Widgets.cpp:321
ImTextureID RenderSVG(Graphics::Renderer *renderer, std::string svgFilename, int width, int height)
Definition: PiGui.cpp:137
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:12
bool CircularSlider(const ImVec2 &center, float *v, float v_min, float v_max)
Definition: Widgets.cpp:122
bool WantCaptureKeyboard()
Definition: PiGui.h:158
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:281
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:160
void Output(const char *message, Args... args)
Definition: utils.h:41