Pioneer
PiGui.h
Go to the documentation of this file.
1 // Copyright © 2008-2022 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  std::map<std::pair<std::string, int>, ImFont *> m_fonts;
124  std::map<ImFont *, std::pair<std::string, int>> m_im_fonts;
125  std::map<std::pair<std::string, int>, PiFont> m_pi_fonts;
126  bool m_should_bake_fonts;
127 
128  std::map<std::string, PiFont> m_font_definitions;
129 
130  ImGuiStyle m_debugStyle;
131  bool m_debugStyleActive;
132 
133  void BakeFonts();
134  void BakeFont(PiFont &font);
135  void AddFontDefinition(const PiFont &font) { m_font_definitions[font.name()] = font; }
136  void ClearFonts();
137  };
138 
139  int RadialPopupSelectMenu(const ImVec2 &center, std::string popup_id, int mouse_button, std::vector<ImTextureID> tex_ids, std::vector<std::pair<ImVec2, ImVec2>> uvs, unsigned int size, std::vector<std::string> tooltips);
140  bool CircularSlider(const ImVec2 &center, float *v, float v_min, float v_max);
141 
142  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);
143  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);
144 
145  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);
146 
147  void IncrementDrag(const std::string &label, int &v, const int v_min, const int v_max, const std::string &format);
148 
149  inline bool WantCaptureMouse()
150  {
151  return ImGui::GetIO().WantCaptureMouse;
152  }
153 
154  inline bool WantCaptureKeyboard()
155  {
156  return ImGui::GetIO().WantCaptureKeyboard;
157  }
158 
159  std::vector<Graphics::Texture *> &GetSVGTextures();
160  ImTextureID RenderSVG(Graphics::Renderer *renderer, std::string svgFilename, int width, int height);
161 
162 } //namespace PiGui
Definition: Renderer.h:44
Definition: PiGuiRenderer.h:13
Definition: PiGui.h:86
bool ProcessEvent(SDL_Event *event)
Definition: PiGui.cpp:337
Instance()
Definition: PiGui.cpp:161
ImFont * GetFont(const std::string &name, int size)
Definition: PiGui.cpp:225
void RefreshFontsTexture()
Definition: PiGui.cpp:295
ImFont * AddFont(const std::string &name, int size)
Definition: PiGui.cpp:268
void AddGlyph(ImFont *font, unsigned short glyph)
Definition: PiGui.cpp:238
void EndFrame()
Definition: PiGui.cpp:384
void Uninit()
Definition: PiGui.cpp:499
void Render()
Definition: PiGui.cpp:393
void SetNormalStyle()
Definition: PiGui.cpp:217
void Init(Graphics::Renderer *renderer)
Definition: PiGui.cpp:303
InstanceRenderer * GetRenderer()
Definition: PiGui.h:93
void NewFrame()
Definition: PiGui.cpp:344
void SetDebugStyle()
Definition: PiGui.cpp:209
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:523
void addGlyph(unsigned short glyph)
Definition: PiGui.cpp:529
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:544
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
int RadialPopupSelectMenu(const ImVec2 &center, std::string popup_id, int mouse_button, std::vector< ImTextureID > tex_ids, std::vector< std::pair< ImVec2, ImVec2 >> uvs, unsigned int size, std::vector< std::string > tooltips)
Definition: Widgets.cpp:11
bool WantCaptureMouse()
Definition: PiGui.h:149
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:207
std::vector< Graphics::Texture * > & GetSVGTextures()
Definition: PiGui.cpp:29
void IncrementDrag(const std::string &label, int &v, const int v_min, const int v_max, const std::string &format)
Definition: Widgets.cpp:305
ImTextureID RenderSVG(Graphics::Renderer *renderer, std::string svgFilename, int width, int height)
Definition: PiGui.cpp:54
bool CircularSlider(const ImVec2 &center, float *v, float v_min, float v_max)
Definition: Widgets.cpp:106
bool WantCaptureKeyboard()
Definition: PiGui.h:154
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:265
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:144
void Output(const char *message, Args... args)
Definition: utils.h:41