Add GUI Support
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_SDL2)
|
||||
#include <array>
|
||||
#include <SDL2/SDL.h>
|
||||
@ -11,17 +12,20 @@ class FontCache
|
||||
{
|
||||
std::array<SDL_Texture*,96> font_chrs;
|
||||
int mw,mh,ps;
|
||||
void Load(SDL_Renderer* renderer,TTF_Font* font,const SDL_Color& color);
|
||||
void Load(SDL_Renderer* renderer,TTF_Font* font);
|
||||
public:
|
||||
FontCache(SDL_Renderer* renderer,TTF_Font* font,const SDL_Color& color);
|
||||
FontCache(SDL_Renderer* renderer,std::string font,int sz,const SDL_Color& color);
|
||||
FontCache(SDL_Renderer* renderer,const uint8_t* mem,size_t cnt,int sz,const SDL_Color& color);
|
||||
FontCache(SDL_Renderer* renderer,const std::vector<uint8_t>& v,int sz,const SDL_Color& color);
|
||||
|
||||
FontCache(SDL_Renderer* renderer,TTF_Font* font);
|
||||
FontCache(SDL_Renderer* renderer,std::string font,int sz);
|
||||
FontCache(SDL_Renderer* renderer,const uint8_t* mem,size_t cnt,int sz);
|
||||
FontCache(SDL_Renderer* renderer,const std::vector<uint8_t>& v,int sz);
|
||||
FontCache(SDL_Renderer* renderer,int sz);
|
||||
SDL_Texture* operator[](char c);
|
||||
SDL_Texture* GetCharOfColor(char c, const SDL_Color& color);
|
||||
int MaxWidth();
|
||||
int MaxHeight();
|
||||
int PointSize();
|
||||
void CalculateSize(std::string text, int& x,int& y);
|
||||
void Render(SDL_Renderer* renderer,int x,int y, std::string text, const SDL_Color& color);
|
||||
~FontCache();
|
||||
};
|
||||
}
|
||||
|
||||
244
include/TessesFramework/SDL2/GUI.hpp
Normal file
244
include/TessesFramework/SDL2/GUI.hpp
Normal file
@ -0,0 +1,244 @@
|
||||
#pragma once
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_SDL2)
|
||||
#include <SDL2/SDL.h>
|
||||
#include "FontCache.hpp"
|
||||
#include "../Filesystem/VFSFix.hpp"
|
||||
#include "../Filesystem/VFS.hpp"
|
||||
#include "../Common.hpp"
|
||||
#include "../Serialization/Json.hpp"
|
||||
|
||||
namespace Tesses::Framework::SDL2
|
||||
{
|
||||
class GUIPalette {
|
||||
public:
|
||||
GUIPalette();
|
||||
GUIPalette(bool isDarkMode, SDL_Color accent,int fontSize=24);
|
||||
GUIPalette(SDL_Color accent, SDL_Color background, SDL_Color border_color, SDL_Color border_hover, SDL_Color border_active, SDL_Color border_hover_active, int fontSize=24);
|
||||
SDL_Color accent; //color is used for font when not over accent background
|
||||
SDL_Color background;
|
||||
SDL_Color border_color; //color is used for font when over accent background
|
||||
SDL_Color border_hover;
|
||||
SDL_Color border_active;
|
||||
SDL_Color border_hover_active;
|
||||
int fontSize;
|
||||
|
||||
SDL_Color& GetBorderColor(bool isHovering, bool isActive, bool isMouseDown);
|
||||
};
|
||||
|
||||
class GUIEventArgs
|
||||
{
|
||||
public:
|
||||
virtual std::string Type();
|
||||
virtual ~GUIEventArgs();
|
||||
};
|
||||
class View;
|
||||
class GUIMouseButtonEventArgs : public GUIEventArgs
|
||||
{
|
||||
public:
|
||||
Uint32 which;
|
||||
int x;
|
||||
int y;
|
||||
Uint8 button;
|
||||
std::string Type();
|
||||
};
|
||||
class GUIJsonViewNotFoundEventArgs : public GUIEventArgs
|
||||
{
|
||||
public:
|
||||
std::string Type();
|
||||
View* destView;
|
||||
Tesses::Framework::Serialization::Json::JObject jsonObject;
|
||||
std::string typeString;
|
||||
};
|
||||
class GUISDLEventEventArgs : public GUIEventArgs
|
||||
{
|
||||
public:
|
||||
std::string Type();
|
||||
SDL_Event event;
|
||||
};
|
||||
constexpr uint64_t VIEWFLAG_HOVER_STATE=(uint64_t)1 << 0;
|
||||
constexpr uint64_t VIEWFLAG_MOUSEDOWN_STATE =(uint64_t)1<<1;
|
||||
constexpr uint64_t VIEWFLAG_ISACTIVE=(uint64_t)1<<2;
|
||||
constexpr uint64_t VIEWFLAG_TABSTOP=(uint64_t)1<<3;
|
||||
constexpr uint64_t VIEWFLAG_INTERCEPT_TAB=(uint64_t)1<<4;
|
||||
constexpr uint64_t VIEWFLAG_CHECKED=(uint64_t)1<<5;
|
||||
class GUIPopup;
|
||||
class GUIWindow;
|
||||
class ContainerView;
|
||||
class View {
|
||||
protected:
|
||||
std::string text;
|
||||
std::string id;
|
||||
uint64_t flags;
|
||||
protected:
|
||||
|
||||
View();
|
||||
View(std::string text);
|
||||
virtual void OnDraw(SDL_Renderer* renderer, SDL_Rect& r);
|
||||
virtual void OnDragDropFile(Tesses::Framework::Filesystem::VFSPath filePath,SDL_Rect myRect, SDL_Point dropLoc);
|
||||
virtual void OnDragDropText(std::string text,SDL_Rect myRect, SDL_Point dropLoc);
|
||||
virtual void OnEnter(GUIEventArgs& evt);
|
||||
virtual void OnLeave(GUIEventArgs& evt);
|
||||
virtual void OnMouseDown(GUIMouseButtonEventArgs& evt);
|
||||
virtual void OnMouseUp(GUIMouseButtonEventArgs& evt);
|
||||
virtual void OnClick(GUIEventArgs& evt);
|
||||
virtual bool OnEvent(SDL_Event& event, SDL_Rect& myBounds, SDL_Rect& visibleBounds);
|
||||
virtual void OnSetParent(View* v);
|
||||
|
||||
void CallOnDraw(View* view, SDL_Renderer* renderer, SDL_Rect& myRect)
|
||||
{
|
||||
view->OnDraw(renderer,myRect);
|
||||
}
|
||||
bool CallOnEvent(View* view,SDL_Event& event, SDL_Rect& myBounds, SDL_Rect& visibleBounds)
|
||||
{
|
||||
return view->OnEvent(event,myBounds,visibleBounds);
|
||||
}
|
||||
|
||||
|
||||
ContainerView* parent;
|
||||
|
||||
public:
|
||||
bool GetViewFlag(uint64_t flag)
|
||||
{
|
||||
return (this->flags & flag) != 0;
|
||||
}
|
||||
void SetViewFlag(uint64_t flag, bool value)
|
||||
{
|
||||
if(value)
|
||||
this->flags |= flag;
|
||||
else
|
||||
this->flags &= ~flag;
|
||||
}
|
||||
|
||||
EventList<View*,GUIMouseButtonEventArgs&> MouseUp;
|
||||
EventList<View*,GUIMouseButtonEventArgs&> MouseDown;
|
||||
EventList<View*,GUIEventArgs&> Click;
|
||||
EventList<View*,GUIEventArgs&> Enter;
|
||||
EventList<View*,GUIEventArgs&> Leave;
|
||||
EventList<View*,GUISDLEventEventArgs&> SDLEvent;
|
||||
virtual ~View();
|
||||
friend class GUIWindow;
|
||||
virtual GUIWindow* GetWindow();
|
||||
virtual std::string GetText();
|
||||
virtual void SetText(std::string text);
|
||||
virtual void SetId(std::string id);
|
||||
virtual std::string GetId();
|
||||
virtual View* FindViewById(std::string id);
|
||||
friend class ContainerView;
|
||||
};
|
||||
class ContainerView : public View {
|
||||
public:
|
||||
virtual size_t ViewCount()=0;
|
||||
virtual View* GetViewAt(size_t index)=0;
|
||||
virtual View* FindViewById(std::string id);
|
||||
protected:
|
||||
ContainerView();
|
||||
ContainerView(std::string text);
|
||||
|
||||
|
||||
|
||||
|
||||
void AssignChildParentToThis(View* view)
|
||||
{
|
||||
if(view != nullptr)
|
||||
{
|
||||
view->parent = this;
|
||||
view->OnSetParent(this);
|
||||
|
||||
}
|
||||
}
|
||||
};
|
||||
enum class TabNextResult {
|
||||
KeepGoing,
|
||||
TabNext,
|
||||
Done
|
||||
};
|
||||
class GUIPopup : public ContainerView {
|
||||
View* child;
|
||||
bool ownsChild;
|
||||
protected:
|
||||
void OnDraw(SDL_Renderer* renderer, SDL_Rect& myRect);
|
||||
bool OnEvent(SDL_Event& event, SDL_Rect& myBounds, SDL_Rect& visibleBounds);
|
||||
public:
|
||||
|
||||
size_t ViewCount();
|
||||
View* GetViewAt(size_t index);
|
||||
GUIPopup();
|
||||
GUIPopup(SDL_Rect bounds);
|
||||
GUIPopup(int x, int y,int w, int h);
|
||||
SDL_Rect bounds;
|
||||
|
||||
void SetView(View* view, bool owns=true);
|
||||
~GUIPopup();
|
||||
|
||||
friend class GUIWindow;
|
||||
};
|
||||
class GUIWindow : public ContainerView
|
||||
{
|
||||
std::vector<GUIPopup*> popups;
|
||||
View* child;
|
||||
bool ownsChild;
|
||||
SDL_Window* window;
|
||||
SDL_Renderer* renderer;
|
||||
void Event(SDL_Event& event);
|
||||
void Draw();
|
||||
void DeactivateAll(View* view);
|
||||
void TabNext(View* view,TabNextResult& nr);
|
||||
|
||||
protected:
|
||||
void OnDraw(SDL_Renderer* renderer, SDL_Rect& myRect);
|
||||
bool OnEvent(SDL_Event& event, SDL_Rect& myBounds, SDL_Rect& visibleBounds);
|
||||
|
||||
public:
|
||||
EventList<View*,GUIJsonViewNotFoundEventArgs&> JsonViewNotFound;
|
||||
size_t ViewCount();
|
||||
View* GetViewAt(size_t index);
|
||||
FontCache* normal_font;
|
||||
FontCache* monospaced_font;
|
||||
GUIPalette palette;
|
||||
GUIWindow(std::string title, int w, int h, Uint32 flags, const GUIPalette& palette);
|
||||
void SetPalette(const GUIPalette& palette);
|
||||
void SetView(View* view,bool owns=true);
|
||||
|
||||
void ShowPopup(GUIPopup* popup);
|
||||
void ShowPopup(GUIPopup& popup);
|
||||
|
||||
void MakeActive(View* view);
|
||||
void TabNext();
|
||||
GUIWindow* GetWindow();
|
||||
|
||||
~GUIWindow();
|
||||
|
||||
friend class GUI;
|
||||
friend class GUIPopup;
|
||||
|
||||
void SetText(std::string text);
|
||||
|
||||
void SetView(Tesses::Framework::Serialization::Json::JToken json);
|
||||
|
||||
View* CreateViewFromJson(Tesses::Framework::Serialization::Json::JObject json);
|
||||
};
|
||||
|
||||
|
||||
class GUI {
|
||||
std::vector<GUIWindow*> windows;
|
||||
public:
|
||||
void Update();
|
||||
friend class GUIWindow;
|
||||
};
|
||||
extern GUI gui;
|
||||
|
||||
class Clipper {
|
||||
SDL_Rect theRect;
|
||||
SDL_Renderer* renderer;
|
||||
bool isClipped;
|
||||
public:
|
||||
Clipper(SDL_Renderer* renderer, SDL_Rect& myRect);
|
||||
bool Clip(SDL_Rect rect);
|
||||
~Clipper();
|
||||
static void ClipRect(SDL_Rect& child, SDL_Rect& parent);
|
||||
};
|
||||
|
||||
|
||||
|
||||
}
|
||||
#endif
|
||||
12
include/TessesFramework/SDL2/ParseColor.hpp
Normal file
12
include/TessesFramework/SDL2/ParseColor.hpp
Normal file
@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_SDL2)
|
||||
#include <SDL2/SDL.h>
|
||||
#include <string>
|
||||
|
||||
namespace Tesses::Framework::SDL2
|
||||
{
|
||||
bool TryParseSDLColor(std::string str, SDL_Color& col);
|
||||
}
|
||||
|
||||
#endif
|
||||
9
include/TessesFramework/SDL2/Stream.hpp
Normal file
9
include/TessesFramework/SDL2/Stream.hpp
Normal file
@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_SDL2)
|
||||
#include "../Streams/Stream.hpp"
|
||||
#include <SDL2/SDL_rwops.h>
|
||||
namespace Tesses::Framework::SDL2
|
||||
{
|
||||
SDL_RWops* RwopsFromStream(Tesses::Framework::Streams::Stream* strm, bool owns=true);
|
||||
}
|
||||
#endif
|
||||
33
include/TessesFramework/SDL2/Views/AbsoluteView.hpp
Normal file
33
include/TessesFramework/SDL2/Views/AbsoluteView.hpp
Normal file
@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_SDL2)
|
||||
#include "../GUI.hpp"
|
||||
|
||||
namespace Tesses::Framework::SDL2::Views
|
||||
{
|
||||
class AbsoluteView : public ContainerView {
|
||||
std::vector<std::pair<std::pair<View*,bool>,SDL_Rect>> views;
|
||||
protected:
|
||||
void OnDraw(SDL_Renderer* renderer, SDL_Rect& myRect);
|
||||
bool OnEvent(SDL_Event& event, SDL_Rect& myBounds, SDL_Rect& visibleBounds);
|
||||
public:
|
||||
size_t ViewCount();
|
||||
View* GetViewAt(size_t index);
|
||||
AbsoluteView();
|
||||
void Add(SDL_Rect rect, View* view, bool owns=true);
|
||||
void Set(View* view, SDL_Rect rect);
|
||||
void Remove(View* view);
|
||||
~AbsoluteView();
|
||||
|
||||
};
|
||||
|
||||
/* class AbsoluteContainer : public View {
|
||||
std::vector<std::pair<std::pair<View*,bool>,SDL_Rect>> views;
|
||||
public:
|
||||
void Add(SDL_Rect rect, View* view, bool owns=true);
|
||||
void Set(View* view, SDL_Rect rect);
|
||||
void Remove(View* view);
|
||||
|
||||
~AbsoluteContainer();
|
||||
};*/
|
||||
}
|
||||
#endif
|
||||
18
include/TessesFramework/SDL2/Views/ButtonView.hpp
Normal file
18
include/TessesFramework/SDL2/Views/ButtonView.hpp
Normal file
@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_SDL2)
|
||||
#include "../GUI.hpp"
|
||||
|
||||
namespace Tesses::Framework::SDL2::Views
|
||||
{
|
||||
class ButtonView : public View {
|
||||
protected:
|
||||
virtual void OnDraw(SDL_Renderer* renderer, SDL_Rect& r);
|
||||
virtual bool OnEvent(SDL_Event& event, SDL_Rect& myBounds, SDL_Rect& visibleBounds);
|
||||
|
||||
public:
|
||||
ButtonView();
|
||||
ButtonView(std::string text);
|
||||
|
||||
};
|
||||
}
|
||||
#endif
|
||||
25
include/TessesFramework/SDL2/Views/CheckView.hpp
Normal file
25
include/TessesFramework/SDL2/Views/CheckView.hpp
Normal file
@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_SDL2)
|
||||
#include "../GUI.hpp"
|
||||
|
||||
namespace Tesses::Framework::SDL2::Views
|
||||
{
|
||||
class CheckView : public View {
|
||||
protected:
|
||||
virtual void OnCheckChanged(GUIEventArgs& event);
|
||||
virtual void OnDraw(SDL_Renderer* renderer, SDL_Rect& r);
|
||||
virtual bool OnEvent(SDL_Event& event, SDL_Rect& myBounds, SDL_Rect& visibleBounds);
|
||||
|
||||
public:
|
||||
CheckView(bool checked, std::string label);
|
||||
CheckView();
|
||||
|
||||
virtual bool GetChecked();
|
||||
virtual void SetChecked(bool value);
|
||||
|
||||
|
||||
|
||||
EventList<View*,GUIEventArgs&> CheckChanged;
|
||||
};
|
||||
}
|
||||
#endif
|
||||
15
include/TessesFramework/SDL2/Views/LabelView.hpp
Normal file
15
include/TessesFramework/SDL2/Views/LabelView.hpp
Normal file
@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_SDL2)
|
||||
#include "../GUI.hpp"
|
||||
|
||||
namespace Tesses::Framework::SDL2::Views
|
||||
{
|
||||
class LabelView : public View {
|
||||
protected:
|
||||
virtual void OnDraw(SDL_Renderer* renderer, SDL_Rect& r);
|
||||
public:
|
||||
LabelView();
|
||||
LabelView(std::string text);
|
||||
};
|
||||
}
|
||||
#endif
|
||||
17
include/TessesFramework/SDL2/Views/ProgressView.hpp
Normal file
17
include/TessesFramework/SDL2/Views/ProgressView.hpp
Normal file
@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_SDL2)
|
||||
#include "../GUI.hpp"
|
||||
|
||||
namespace Tesses::Framework::SDL2::Views
|
||||
{
|
||||
class ProgressView : public View
|
||||
{
|
||||
protected:
|
||||
virtual void OnDraw(SDL_Renderer* renderer, SDL_Rect& r);
|
||||
public:
|
||||
ProgressView();
|
||||
ProgressView(double value);
|
||||
double value;
|
||||
};
|
||||
};
|
||||
#endif
|
||||
20
include/TessesFramework/SDL2/Views/TextListView.hpp
Normal file
20
include/TessesFramework/SDL2/Views/TextListView.hpp
Normal file
@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_SDL2)
|
||||
#include "../GUI.hpp"
|
||||
|
||||
namespace Tesses::Framework::SDL2::Views
|
||||
{
|
||||
class TextListView : public View {
|
||||
|
||||
protected:
|
||||
virtual void OnDraw(SDL_Renderer* renderer, SDL_Rect& r);
|
||||
virtual bool OnEvent(SDL_Event& event, SDL_Rect& myBounds, SDL_Rect& visibleBounds);
|
||||
public:
|
||||
TextListView();
|
||||
size_t firstIndex;
|
||||
int selected;
|
||||
std::vector<std::string> items;
|
||||
|
||||
};
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user