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 #ifndef _GUISCREEN_H 5 #define _GUISCREEN_H 6 7 #include "FontCache.h" 8 #include "Gui.h" 9 #include "graphics/Graphics.h" 10 #include "graphics/RenderState.h" 11 #include "text/TextureFont.h" 12 #include <list> 13 #include <stack> 14 15 namespace Graphics { 16 class Renderer; 17 } 18 19 namespace Gui { 20 class Screen { 21 public: 22 static void Init(Graphics::Renderer *renderer, int real_width, int real_height, int ui_width, int ui_height); 23 static void Uninit(); 24 static void Draw(); 25 static void AddBaseWidget(Widget *w, int x, int y); 26 static void RemoveBaseWidget(Widget *w); 27 static void OnMouseMotion(SDL_MouseMotionEvent *e); 28 static void OnClick(SDL_MouseButtonEvent *e); 29 static void OnKeyDown(const SDL_Keysym *sym); 30 static void OnKeyUp(const SDL_Keysym *sym); 31 static void OnTextInput(const SDL_TextInputEvent *e); 32 static void EnterOrtho(); 33 static void LeaveOrtho(); GetWidth()34 static int GetWidth() { return width; } GetHeight()35 static int GetHeight() { return height; } 36 // gluProject but fixes UI/screen size mismatch 37 static bool Project(const vector3d &in, vector3d &out); 38 friend void Widget::SetShortcut(SDL_Keycode key, SDL_Keymod mod); 39 friend Widget::~Widget(); 40 static bool IsBaseWidget(const Widget *); GetCoords2Pixels(float scale[2])41 static void GetCoords2Pixels(float scale[2]) 42 { 43 scale[0] = fontScale[0]; 44 scale[1] = fontScale[1]; 45 } GetCoords2Pixels()46 static const float *GetCoords2Pixels() { return fontScale; } 47 static void SetFocused(Widget *w, bool enableKeyRepeat = false); 48 static void ClearFocus(); IsFocused(Widget * w)49 static bool IsFocused(Widget *w) 50 { 51 return w == focusedWidget; 52 } 53 PushFont(RefCountedPtr<Text::TextureFont> font)54 static void PushFont(RefCountedPtr<Text::TextureFont> font) { s_fontStack.push(font); } PushFont(std::string name)55 static void PushFont(std::string name) { PushFont(s_fontCache.GetTextureFont(name)); } PopFont()56 static void PopFont() { s_fontStack.pop(); }; GetFont()57 static RefCountedPtr<Text::TextureFont> GetFont() { return s_fontStack.size() ? s_fontStack.top() : s_defaultFont; } GetDefaultFont()58 static RefCountedPtr<Text::TextureFont> GetDefaultFont() { return s_defaultFont; } 59 60 static float GetFontHeight(Text::TextureFont *font = 0); 61 static float GetFontDescender(Text::TextureFont *font = 0); 62 63 static void RenderStringBuffer(RefCountedPtr<Graphics::VertexBuffer> vb, const std::string &s, float xoff, float yoff, const Color &color = Color::WHITE, Text::TextureFont *font = 0); 64 static void RenderMarkupBuffer(RefCountedPtr<Graphics::VertexBuffer> vb, const std::string &s, const Color &color = Color::WHITE, Text::TextureFont *font = 0); 65 static void MeasureString(const std::string &s, float &w, float &h, Text::TextureFont *font = 0); 66 static int PickCharacterInString(const std::string &s, float x, float y, Text::TextureFont *font = 0); 67 static void MeasureCharacterPos(const std::string &s, int charIndex, float &x, float &y, Text::TextureFont *font = 0); 68 GetRenderer()69 static Graphics::Renderer *GetRenderer() { return s_renderer; } 70 71 static Graphics::RenderState *alphaBlendState; 72 static Graphics::Material *flatColorMaterial; 73 74 private: 75 static void AddShortcutWidget(Widget *w); 76 static void RemoveShortcutWidget(Widget *w); 77 static void SDLEventCoordToScreenCoord(int sdlev_x, int sdlev_y, float *x, float *y); 78 79 static bool initted; 80 static int width, height; 81 static int realWidth, realHeight; 82 static float invRealWidth, invRealHeight; 83 static std::list<Widget *> kbshortcut_widgets; 84 static std::list<Widget *> mouseHoveredWidgets; 85 static float fontScale[2]; 86 static Gui::Fixed *baseContainer; 87 static Gui::Widget *focusedWidget; 88 static void OnDeleteFocusedWidget(); 89 static matrix4x4f modelMatrix; 90 static matrix4x4f projMatrix; 91 static Graphics::Viewport viewport; 92 93 static FontCache s_fontCache; 94 static std::stack<RefCountedPtr<Text::TextureFont>> s_fontStack; 95 static RefCountedPtr<Text::TextureFont> s_defaultFont; 96 97 static Graphics::Renderer *s_renderer; 98 }; 99 } // namespace Gui 100 101 #endif /* _GUISCREEN_H */ 102