bgfx/3rdparty/ocornut-imgui/imgui_wm.h

309 lines
8.4 KiB
C
Raw Normal View History

2015-09-30 19:22:51 -04:00
/*
* Copyright 2011-2015 Branimir Karadzic. All rights reserved.
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
/*
* Based on ImWindow code from:
* https://github.com/thennequin/ImWindow
*
* MIT license:
* https://github.com/thennequin/ImWindow/blob/master/LICENSE
*/
#ifndef IMGUI_WM_H_HEADER_GUARD
#define IMGUI_WM_H_HEADER_GUARD
#include "imgui.h"
typedef unsigned int ImU32;
#ifndef ImwList
2015-09-30 23:02:59 -04:00
# include <list>
# define ImwList std::list
2015-09-30 19:22:51 -04:00
#endif // ImList
#ifndef ImwMap
2015-09-30 23:02:59 -04:00
# include <unordered_map>
# define ImwMap std::unordered_map
2015-09-30 19:22:51 -04:00
#endif // ImMap
2015-10-01 01:36:56 -04:00
namespace ImGuiWM
2015-09-30 19:22:51 -04:00
{
enum EDockOrientation
{
E_DOCK_ORIENTATION_CENTER,
E_DOCK_ORIENTATION_TOP,
E_DOCK_ORIENTATION_LEFT,
E_DOCK_ORIENTATION_RIGHT,
E_DOCK_ORIENTATION_BOTTOM,
};
2015-10-01 01:36:56 -04:00
class Id
2015-09-30 19:22:51 -04:00
{
2015-09-30 23:02:59 -04:00
public:
2015-10-01 01:36:56 -04:00
Id();
2015-09-30 19:22:51 -04:00
ImU32 GetId() const;
const char* GetStr() const;
2015-09-30 23:02:59 -04:00
2015-09-30 19:22:51 -04:00
private:
2015-09-30 23:02:59 -04:00
ImU32 m_iId;
char m_pId[11];
2015-09-30 19:22:51 -04:00
static int s_iNextId;
};
2015-10-01 01:36:56 -04:00
class Window
2015-09-30 19:22:51 -04:00
{
2015-10-01 01:36:56 -04:00
friend class WindowManager;
friend class Container;
2015-09-30 23:02:59 -04:00
2015-09-30 19:22:51 -04:00
public:
2015-09-30 23:02:59 -04:00
virtual void OnGui() = 0;
virtual void OnMenu() {};
const char* GetId() const { return m_oId.GetStr(); }
2015-09-30 19:22:51 -04:00
2015-09-30 23:02:59 -04:00
void Destroy();
2015-09-30 19:22:51 -04:00
2015-09-30 23:02:59 -04:00
void SetTitle(const char* pTitle);
const char* GetTitle() const;
2015-09-30 19:22:51 -04:00
2015-09-30 23:02:59 -04:00
const ImVec2& GetLastPosition() const;
const ImVec2& GetLastSize() const;
2015-09-30 19:22:51 -04:00
protected:
2015-10-01 01:36:56 -04:00
Window();
virtual ~Window();
2015-09-30 19:22:51 -04:00
2015-09-30 23:02:59 -04:00
char* m_pTitle;
2015-10-01 01:36:56 -04:00
Id m_oId;
2015-09-30 19:22:51 -04:00
2015-09-30 23:02:59 -04:00
ImVec2 m_oLastPosition;
ImVec2 m_oLastSize;
2015-09-30 19:22:51 -04:00
};
2015-10-01 01:36:56 -04:00
typedef ImwList<Window*> WindowList;
2015-09-30 19:22:51 -04:00
2015-10-01 01:36:56 -04:00
class PlatformWindow;
2015-09-30 19:22:51 -04:00
2015-10-01 01:36:56 -04:00
class Container
2015-09-30 19:22:51 -04:00
{
2015-10-01 01:36:56 -04:00
friend class PlatformWindow;
2015-09-30 19:22:51 -04:00
public:
2015-10-01 01:36:56 -04:00
void Dock(Window* pWindow,EDockOrientation eOrientation = E_DOCK_ORIENTATION_CENTER);
bool UnDock(Window* pWindow);
2015-09-30 19:22:51 -04:00
2015-09-30 23:02:59 -04:00
bool IsEmpty();
bool IsSplit();
bool HasWindowTabbed();
2015-10-01 01:36:56 -04:00
Container* HasWindow(const Window* pWindow);
PlatformWindow* GetPlatformWindowParent() const;
Container* GetBestDocking(const ImVec2 oCursorPosInContainer,EDockOrientation& oOutOrientation,ImVec2& oOutAreaPos,ImVec2& oOutAreaSize);
2015-09-30 19:22:51 -04:00
protected:
2015-10-01 01:36:56 -04:00
Container(Container* pParent);
Container(PlatformWindow* pParent);
~Container();
2015-09-30 19:22:51 -04:00
2015-09-30 23:02:59 -04:00
void CreateSplits();
void Paint();
2015-09-30 19:22:51 -04:00
2015-10-01 01:36:56 -04:00
Container* m_pParent;
PlatformWindow* m_pParentWindow;
WindowList m_lWindows;
Container* m_pSplits[2];
2015-09-30 19:22:51 -04:00
2015-09-30 23:02:59 -04:00
float m_fSplitRatio;
bool m_bVerticalSplit;
int m_iActiveWindow;
2015-09-30 19:22:51 -04:00
2015-09-30 23:02:59 -04:00
bool m_bIsDrag;
2015-09-30 19:22:51 -04:00
2015-09-30 23:02:59 -04:00
ImVec2 m_oLastPosition;
ImVec2 m_oLastSize;
2015-09-30 19:22:51 -04:00
};
2015-10-01 01:36:56 -04:00
class PlatformWindow
2015-09-30 19:22:51 -04:00
{
2015-10-01 01:36:56 -04:00
friend class WindowManager;
2015-09-30 23:02:59 -04:00
2015-09-30 19:22:51 -04:00
public:
2015-10-01 01:36:56 -04:00
PlatformWindow(bool bMainWindow,bool bIsDragWindow);
virtual ~PlatformWindow();
2015-09-30 23:02:59 -04:00
2015-10-01 01:36:56 -04:00
virtual bool Init(PlatformWindow* pParent) = 0;
2015-09-30 19:22:51 -04:00
2015-09-30 23:02:59 -04:00
virtual const ImVec2& GetPosition() const = 0;
virtual const ImVec2& GetSize() const = 0;
2015-09-30 19:22:51 -04:00
2015-09-30 23:02:59 -04:00
virtual void Show() = 0;
virtual void Hide() = 0;
virtual void SetSize(const ImVec2& size) = 0;
virtual void SetPosition(const ImVec2& pos) = 0;
virtual void SetTitle(const char* pTitle) = 0;
2015-09-30 19:22:51 -04:00
2015-09-30 23:02:59 -04:00
bool IsMain();
2015-09-30 19:22:51 -04:00
2015-10-01 01:36:56 -04:00
void Dock(Window* pWindow);
bool UnDock(Window* pWindow);
2015-09-30 19:22:51 -04:00
2015-10-01 01:36:56 -04:00
Container* GetContainer();
Container* HasWindow(Window* pWindow);
2015-09-30 23:02:59 -04:00
bool IsStateSet();
2015-09-30 19:22:51 -04:00
protected:
2015-09-30 23:02:59 -04:00
void SetState();
void RestoreState();
void OnLoseFocus();
virtual void PreUpdate() = 0;
virtual void Paint();
virtual void Destroy() = 0;
virtual void StartDrag() = 0;
virtual void StopDrag() = 0;
virtual bool IsDraging() = 0;
void PaintContainer();
void OnClose();
2015-10-01 01:36:56 -04:00
Id m_oId;
2015-09-30 23:02:59 -04:00
bool m_bMain;
bool m_bIsDragWindow;
2015-10-01 01:36:56 -04:00
Container* m_pContainer;
2015-09-30 23:02:59 -04:00
void* m_pState;
void* m_pPreviousState;
2015-09-30 19:22:51 -04:00
};
2015-10-01 01:36:56 -04:00
typedef ImwList<PlatformWindow*> PlatformWindowList;
2015-09-30 19:22:51 -04:00
2015-10-01 01:36:56 -04:00
class WindowManager
2015-09-30 19:22:51 -04:00
{
2015-10-01 01:36:56 -04:00
friend class Window;
friend class PlatformWindow;
friend class Container;
2015-09-30 19:22:51 -04:00
struct PlatformWindowAction
{
2015-10-01 01:36:56 -04:00
PlatformWindow* m_pPlatformWindow;
2015-09-30 23:02:59 -04:00
unsigned int m_iFlags;
ImVec2 m_oPosition;
ImVec2 m_oSize;
2015-09-30 19:22:51 -04:00
};
struct DockAction
{
2015-10-01 01:36:56 -04:00
Window* m_pWindow;
2015-09-30 23:02:59 -04:00
2015-09-30 19:22:51 -04:00
// Is Dock or Float
2015-09-30 23:02:59 -04:00
bool m_bFloat;
2015-09-30 19:22:51 -04:00
//For Docking
2015-10-01 01:36:56 -04:00
Window* m_pWith;
2015-09-30 23:02:59 -04:00
EDockOrientation m_eOrientation;
2015-10-01 01:36:56 -04:00
PlatformWindow* m_pToPlatformWindow;
Container* m_pToContainer;
2015-09-30 23:02:59 -04:00
2015-09-30 19:22:51 -04:00
//For Floating
2015-09-30 23:02:59 -04:00
ImVec2 m_oPosition;
ImVec2 m_oSize;
2015-09-30 19:22:51 -04:00
};
struct DrawWindowAreaAction
{
2015-10-01 01:36:56 -04:00
DrawWindowAreaAction(PlatformWindow* pWindow,const ImVec2& oRectPos,const ImVec2& oRectSize,const ImColor& oColor);
PlatformWindow* m_pWindow;
2015-09-30 23:02:59 -04:00
ImVec2 m_oRectPos;
ImVec2 m_oRectSize;
ImColor m_oColor;
2015-09-30 19:22:51 -04:00
};
public:
struct Config
{
Config();
2015-09-30 23:02:59 -04:00
float m_fDragMarginRatio;
float m_fDragMarginSizeRatio;
ImColor m_oHightlightAreaColor;
2015-09-30 19:22:51 -04:00
};
2015-10-01 01:36:56 -04:00
WindowManager();
virtual ~WindowManager();
2015-09-30 19:22:51 -04:00
2015-09-30 23:02:59 -04:00
bool Init();
bool Run();
void Exit();
2015-09-30 19:22:51 -04:00
2015-10-01 01:36:56 -04:00
PlatformWindow* GetMainPlatformWindow();
2015-09-30 23:02:59 -04:00
Config& GetConfig();
2015-09-30 19:22:51 -04:00
2015-09-30 23:02:59 -04:00
void SetMainTitle(const char* pTitle);
2015-09-30 19:22:51 -04:00
2015-10-01 01:36:56 -04:00
void Dock(Window* pWindow, EDockOrientation eOrientation = E_DOCK_ORIENTATION_CENTER, PlatformWindow* pToPlatformWindow = NULL);
void DockTo(Window* pWindow, EDockOrientation eOrientation = E_DOCK_ORIENTATION_CENTER, Container* pContainer = NULL);
void DockWith(Window* pWindow, Window* pWithWindow,EDockOrientation eOrientation = E_DOCK_ORIENTATION_CENTER);
void Float(Window* pWindow, const ImVec2& oPosition = ImVec2(-1,-1), const ImVec2& oSize = ImVec2(-1,-1));
2015-09-30 19:22:51 -04:00
2015-10-01 01:36:56 -04:00
const WindowList& GetWindowList() const;
PlatformWindow* GetCurrentPlatformWindow();
PlatformWindow* GetWindowParent(Window* pWindow);
2015-09-30 19:22:51 -04:00
2015-10-03 01:22:11 -04:00
void Log(const char* pFormat, ...);
2015-09-30 23:02:59 -04:00
virtual void LogFormatted(const char* pStr) = 0;;
2015-10-01 01:36:56 -04:00
static WindowManager* GetInstance();
2015-09-30 19:22:51 -04:00
protected:
2015-10-01 01:36:56 -04:00
virtual PlatformWindow* CreatePlatformWindow(bool bMain,PlatformWindow* pParent,bool bDragWindow) = 0;
2015-09-30 23:02:59 -04:00
virtual void InternalRun() = 0;
2015-10-01 01:36:56 -04:00
void AddWindow(Window* pWindow);
void RemoveWindow(Window* pWindow);
void DestroyWindow(Window* pWindow);
2015-09-30 23:02:59 -04:00
2015-10-01 01:36:56 -04:00
void InternalDock(Window* pWindow,EDockOrientation eOrientation,PlatformWindow* pToPlatformWindow);
void InternalDockTo(Window* pWindow,EDockOrientation eOrientation,Container* pToContainer);
void InternalDockWith(Window* pWindow,Window* pWithWindow,EDockOrientation eOrientation);
void InternalFloat(Window* pWindow,ImVec2 oPosition,ImVec2 oSize);
void InternalUnDock(Window* pWindow);
void InternalDrag(Window* pWindow);
2015-09-30 23:02:59 -04:00
2015-10-01 01:36:56 -04:00
void OnClosePlatformWindow(PlatformWindow* pWindow);
2015-09-30 23:02:59 -04:00
2015-10-01 01:36:56 -04:00
void DrawWindowArea(PlatformWindow* pWindow,const ImVec2& oPos,const ImVec2& oSize,const ImColor& oColor);
2015-09-30 23:02:59 -04:00
void PreUpdate();
void Update();
void UpdatePlatformwWindowActions();
void UpdateDockActions();
void UpdateOrphans();
2015-10-01 01:36:56 -04:00
void Paint(PlatformWindow* pWindow);
2015-09-30 23:02:59 -04:00
2015-10-01 01:36:56 -04:00
void StartDragWindow(Window* pWindow);
2015-09-30 23:02:59 -04:00
void StopDragWindow();
void UpdateDragWindow();
2015-10-03 01:22:11 -04:00
Container* GetBestDocking(PlatformWindow* pPlatformWindow,const ImVec2 oCursorPos,EDockOrientation& oOutOrientation,ImVec2& oOutAreaPos,ImVec2& oOutAreaSize);
2015-09-30 23:02:59 -04:00
Config m_oConfig;
2015-10-01 01:36:56 -04:00
PlatformWindow* m_pMainPlatformWindow;
PlatformWindowList m_lPlatformWindows;
PlatformWindow* m_pDragPlatformWindow;
WindowList m_lWindows;
WindowList m_lOrphanWindows;
WindowList m_lToDestroyWindows;
PlatformWindowList m_lToDestroyPlatformWindows;
2015-09-30 23:02:59 -04:00
ImwList<PlatformWindowAction*> m_lPlatformWindowActions;
ImwList<DockAction*> m_lDockActions;
ImwList<DrawWindowAreaAction> m_lDrawWindowAreas;
2015-10-01 01:36:56 -04:00
PlatformWindow* m_pCurrentPlatformWindow;
Window* m_pDraggedWindow;
2015-09-30 23:02:59 -04:00
ImVec2 m_oDragPreviewOffset;
2015-10-01 01:36:56 -04:00
static WindowManager* s_pInstance;
2015-09-30 19:22:51 -04:00
};
}
#endif // IMGUI_WM_H_HEADER_GUARD