Pioneer
PiGui.h
Go to the documentation of this file.
1 // Copyright © 2008-2021 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 to wrap ImGui. */
84  class Instance : public RefCounted {
85  public:
86  Instance();
87 
88  void Init(Graphics::Renderer *renderer);
89  void Uninit();
90 
91  // Call at the start of every frame. Calls ImGui::NewFrame() internally.
92  void NewFrame();
93 
94  // Call at the end of a frame that you're not going to render the results of
95  void EndFrame();
96 
97  // Calls ImGui::EndFrame() internally and does book-keeping before rendering.
98  void Render();
99 
100  // Sets the ImGui Style object to use the predefined development tooling style
101  void SetDebugStyle();
102 
103  // Sets the ImGui Style object to use the game UI style object as modified by Lua
104  void SetNormalStyle();
105 
106  ImFont *AddFont(const std::string &name, int size);
107  ImFont *GetFont(const std::string &name, int size);
108 
109  void AddGlyph(ImFont *font, unsigned short glyph);
110 
111  bool ProcessEvent(SDL_Event *event);
112 
113  void RefreshFontsTexture();
114 
115  private:
116  Graphics::Renderer *m_renderer;
117 
118  std::map<std::pair<std::string, int>, ImFont *> m_fonts;
119  std::map<ImFont *, std::pair<std::string, int>> m_im_fonts;
120  std::map<std::pair<std::string, int>, PiFont> m_pi_fonts;
121  bool m_should_bake_fonts;
122 
123  std::map<std::string, PiFont> m_font_definitions;
124 
125  ImGuiStyle m_debugStyle;
126  bool m_debugStyleActive;
127 
128  void BakeFonts();
129  void BakeFont(PiFont &font);
130  void AddFontDefinition(const PiFont &font) { m_font_definitions[font.name()] = font; }
131  void ClearFonts();
132  };
133 
134  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);
135  bool CircularSlider(const ImVec2 &center, float *v, float v_min, float v_max);
136 
137  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);
138  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);
139 
140  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);
141 
142  void IncrementDrag(const std::string &label, int &v, const int v_min, const int v_max, const std::string &format);
143 
144  inline bool WantCaptureMouse()
145  {
146  return ImGui::GetIO().WantCaptureMouse;
147  }
148 
149  inline bool WantCaptureKeyboard()
150  {
151  return ImGui::GetIO().WantCaptureKeyboard;
152  }
153 
154  std::vector<Graphics::Texture *> &GetSVGTextures();
155  ImTextureID RenderSVG(Graphics::Renderer *renderer, std::string svgFilename, int width, int height);
156 
157 } //namespace PiGui
void * ImTextureID
Definition: ModelSpinner.h:14
Definition: Renderer.h:39
Definition: PiGui.h:84
bool ProcessEvent(SDL_Event *event)
Definition: PiGui.cpp:343
Instance()
Definition: PiGui.cpp:163
ImFont * GetFont(const std::string &name, int size)
Definition: PiGui.cpp:227
void RefreshFontsTexture()
Definition: PiGui.cpp:297
ImFont * AddFont(const std::string &name, int size)
Definition: PiGui.cpp:270
void AddGlyph(ImFont *font, unsigned short glyph)
Definition: PiGui.cpp:240
void EndFrame()
Definition: PiGui.cpp:370
void Uninit()
Definition: PiGui.cpp:500
void Render()
Definition: PiGui.cpp:397
void SetNormalStyle()
Definition: PiGui.cpp:219
void Init(Graphics::Renderer *renderer)
Definition: PiGui.cpp:306
void NewFrame()
Definition: PiGui.cpp:350
void SetDebugStyle()
Definition: PiGui.cpp:211
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:524
void addGlyph(unsigned short glyph)
Definition: PiGui.cpp:530
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:545
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:13
Definition: LuaBody.cpp:28
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:144
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:31
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:56
bool CircularSlider(const ImVec2 &center, float *v, float v_min, float v_max)
Definition: Widgets.cpp:106
bool WantCaptureKeyboard()
Definition: PiGui.h:149
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