rewrite of C4GuiWindow to use C4GUI stuff wherever possible

Controls
David Dormagen 2014-10-05 16:46:14 +02:00
parent 952f06b173
commit 7319f7b3cc
8 changed files with 454 additions and 357 deletions

View File

@ -1486,10 +1486,12 @@ void C4Game::Default()
DebugHost.Clear();
DebugWait = false;
delete GuiWindowRoot;
// pGui is empty atm. Readd script window to GUI.
// the window will be deleted by pGui->Clear
const float standardVerticalBorder = 100.0f;
const float standardHorizontalBorder = 100.0f;
GuiWindowRoot = new C4GuiWindow(standardVerticalBorder, standardHorizontalBorder);
pGUI->AddElement(GuiWindowRoot);
}
void C4Game::Evaluate()

View File

@ -297,7 +297,7 @@ void C4Viewport::Draw(C4TargetFacet &cgo0, bool fDrawOverlay)
// ingame menus
C4ST_STARTNEW(GuiWindowStat, "C4Viewport::DrawOverlay: Menus")
pDraw->SetZoom(0, 0, 1.0);
::Game.GuiWindowRoot->Draw(gui_cgo, Player);
::Game.GuiWindowRoot->DrawAll(gui_cgo, Player);
C4ST_STOP(GuiWindowStat)
DrawOverlay(gui_cgo, GameZoom);

View File

@ -837,6 +837,7 @@ namespace C4GUI
float fY = float(iPxY) / fZoom;
// forward to mouse
Mouse.Input(iButton, fX, fY, dwKeyParam);
// dragging
if (Mouse.pDragElement)
{

View File

@ -383,8 +383,7 @@ namespace C4GUI
bool fDragging; // if set, mouse is down on component and dragging enabled
ContextHandler *pContextHandler; // context handler to be called upon context menu request
public:
bool fVisible; // if false, component (and subcomponents) are not drawn
bool fVisible; // if false, component (and subcomponents) are not drawn
protected:
C4Rect rcBounds; // element bounds
@ -753,13 +752,66 @@ namespace C4GUI
virtual Element *GetFirstContained() { return pFirst; }
virtual Element *GetLastContained() { return pLast; }
virtual Element *GetFirstNestedElement(bool fBackwards);
class Iterator
{
private:
Element *current;
public:
Iterator(Element *element = nullptr) : current(element) { }
Element * operator*() const { return current; }
Element * operator->() const { return current; }
void operator++() { current = current->GetNext(); };
void operator++(int) { operator++(); }
bool operator==(const Iterator & other) const
{
return (current == other.current);
}
bool operator!=(const Iterator & other) const
{
return !(*this == other);
}
};
class ReverseIterator
{
private:
Element *current;
public:
ReverseIterator(Element *element = nullptr) : current(element) { }
Element * operator*() const { return current; }
Element * operator->() const { return current; }
void operator++() { current = current->GetPrev(); };
void operator++(int) { operator++(); }
bool operator==(const ReverseIterator & other) const
{
return (current == other.current);
}
bool operator!=(const ReverseIterator & other) const
{
return !(*this == other);
}
};
// provide C++-style iterator interface
Iterator begin() { return Iterator(pFirst); }
Iterator end() { return Iterator(nullptr); }
ReverseIterator rbegin() { return ReverseIterator(pLast); }
ReverseIterator rend() { return ReverseIterator(nullptr); }
Element *GetFirst() { return pFirst; }
Element *GetLast() { return pLast; }
virtual Container *GetContainer() { return this; } // returns parent for elements; this for containers
Element *GetElementByIndex(int32_t i); // get indexed child element
int32_t GetElementCount();
virtual void SetVisibility(bool fToValue);
virtual void SetVisibility(bool fToValue);
virtual bool IsFocused(Control *pCtrl) { return pParent ? pParent->IsFocused(pCtrl) : false; }
virtual bool IsSelectedChild(Element *pChild) { return pParent ? pParent->IsSelectedChild(pChild) : true; } // whether the child element is selected - only false for list-box-containers which can have unselected children
virtual bool IsParentOf(Element *pEl); // whether this is the parent container (directly or recursively) of the passed element
@ -822,11 +874,6 @@ namespace C4GUI
void Update(); // update scroll bar according to window
void OnPosChanged(); // update window according to scroll bar, and/or do callbacks
// mouse handling
virtual void MouseInput(CMouse &rMouse, int32_t iButton, int32_t iX, int32_t iY, DWORD dwKeyParam); // input: mouse movement or buttons
virtual void DoDragging(CMouse &rMouse, int32_t iX, int32_t iY, DWORD dwKeyParam); // dragging: allow dragging of thumb
virtual void MouseLeave(CMouse &rMouse); // mouse leaves with button down: reset down state of buttons
virtual void DrawElement(C4TargetFacet &cgo); // draw scroll bar
// suppress scrolling pin for very narrow menus
@ -851,6 +898,11 @@ namespace C4GUI
ScrollBar(C4Rect &rcBounds, bool fHorizontal, BaseParCallbackHandler<int32_t> *pCB, int32_t iCBMaxRange=256); // ctor for callback
~ScrollBar(); // dtor
// mouse handling
virtual void MouseInput(CMouse &rMouse, int32_t iButton, int32_t iX, int32_t iY, DWORD dwKeyParam); // input: mouse movement or buttons
virtual void DoDragging(CMouse &rMouse, int32_t iX, int32_t iY, DWORD dwKeyParam); // dragging: allow dragging of thumb
virtual void MouseLeave(CMouse &rMouse); // mouse leaves with button down: reset down state of buttons
// change style
void SetDecoration(ScrollBarFacets *pToGfx, bool fAutoHide)
{ pCustomGfx = pToGfx; this->fAutoHide=fAutoHide; }
@ -2440,6 +2492,7 @@ namespace C4GUI
{
public:
int32_t x,y; // cursor position
int32_t forPlayer; // player that the mouse movement originated from
bool LDown, MDown, RDown; // mouse button states
int32_t LDownX, LDownY; // position where left button was pressed last
DWORD dwKeys; // shift, ctrl, etc.

View File

@ -502,7 +502,8 @@ namespace C4GUI
rtBounds.x += rtBounds.Wdt; rtBounds.Wdt = C4GUI_ScrollBarWdt;
pScrollBar = new ScrollBar(rtBounds, this);
// add self and scroll bar to window
pParentWindow->AddElement(this);
if (pParentWindow != this)
pParentWindow->AddElement(this);
pParentWindow->AddElement(pScrollBar);
}

File diff suppressed because it is too large Load Diff

View File

@ -189,9 +189,10 @@ class C4GuiWindowScrollBar
C4GuiWindow *parent;
};
class C4GuiWindow
class C4GuiWindow : public C4GUI::ScrollWindow
{
friend class C4GuiWindowAction;
friend class C4GuiWindowScrollBar;
private:
// the menu ID is always unique, however the sub-menu IDs do NOT have to be unique
@ -200,39 +201,18 @@ class C4GuiWindow
// this is not only a window inside a menu but a top-level-window?
// this does not mean the ::WindowMenuRoot but rather a player-created submenu
bool isMainWindow;
bool mainWindowNeedsLayoutUpdate;
void RequestLayoutUpdate();
std::list<C4GuiWindow*> children;
C4GuiWindow *parent;
bool wasRemoved; // to notify the window that it should not inform its parent on Close() a second time
bool closeActionWasExecuted; // to prevent a window from calling the close-callback twice even if f.e. closed in the close-callback..
bool visible;
C4Object *target;
const C4Object *GetTarget() { return target; }
C4GuiWindowScrollBar *scrollBar;
// this remembers whether the window currently has mouse focus and whether it has been mouse-down-ed
// all windows with mouse focus set are remembered by their parents and notified when the mouse left
enum MouseState // values of this enum will be bit-wise combined
{
None = 0,
Focus = 1,
MouseDown = 2
};
int32_t currentMouseState; // this needs to be saved in savegames!!!
// OnMouseOut() called by this window, unsets the mouse focus
// must notify children, too!
void OnMouseOut(int32_t player);
void OnMouseIn(int32_t player); // called by this window, sets the mouse focus
bool HasMouseFocus() { return currentMouseState & MouseState::Focus; }
// properties are stored extra to make "tags" possible
C4GuiWindowProperty props[C4GuiWindowPropertyName::_lastProp];
void Init();
// withMultipleFlag is there to draw only the non-multiple or the multiple windows
// withMultipleFlag == -1: all windows are drawn (standard)
// withMultipleFlag == 0: only one non-Multiple window is drawn
// withMultipleFlag == 1: only Multiple windows are drawn
// returns whether at least one child was drawn
bool DrawChildren(C4TargetFacet &cgo, int32_t player, float parentLeft, float parentTop, float parentRight, float parentBottom, int32_t withMultipleFlag = -1);
// ID is set by parent, parent gives unique IDs to children
void SetID(int32_t to) { id = to; }
// to be used to generate the quick-access children map for main menus
@ -252,15 +232,13 @@ class C4GuiWindow
int32_t GenerateMenuID() { return ++id; }
int32_t GenerateActionID() { return ++id; }
void UpdateLayout();
void UpdateLayoutGrid();
void UpdateLayoutVertical();
// children height should be set when enabling a scroll bar so that, with style FitChildren, the size can simply be changed
void EnableScrollBar(bool enable = true, float childrenHeight = 0.0f);
public:
// used by mouse input, this is in screen coordinates
struct _lastDrawPosition
/*struct _lastDrawPosition
{
float left, right;
float top, bottom;
@ -269,10 +247,8 @@ class C4GuiWindow
int32_t dirty; // indicates wish to update topMostChild and bottomMostChild asap
bool needLayoutUpdate;
_lastDrawPosition() : left(0.0f), right(0.0f), top(0.0f), bottom(0.0f), topMostChild(0.0f), bottomMostChild(0.0f), dirty(2), needLayoutUpdate(false){}
} lastDrawPosition;
} lastDrawPosition;*/
bool IsVisible() { return visible; }
void SetVisible(bool f) { visible = f; }
void SetTag(C4String *tag);
C4GuiWindow();
@ -306,16 +282,53 @@ class C4GuiWindow
void Close();
void ClearPointers(C4Object *pObj);
// this updates the window's layout and also propagates to all children
bool UpdateLayout(C4TargetFacet &cgo);
bool UpdateLayout(C4TargetFacet &cgo, float parentWidth, float parentHeight);
bool UpdateChildLayout(C4TargetFacet &cgo, float parentWidth, float parentHeight);
// special layouts that are set by styles
void UpdateLayoutGrid();
void UpdateLayoutVertical();
// Draw without parameters can be used for the root
bool DrawAll(C4TargetFacet &cgo, int32_t player);
bool Draw(C4TargetFacet &cgo, int32_t player);
bool Draw(C4TargetFacet &cgo, int32_t player, float parentLeft, float parentTop, float parentRight, float parentBottom);
bool GetClippingRect(float &left, float &top, float &right, float &bottom);
bool GetClippingRect(int32_t &left, int32_t &top, int32_t &right, int32_t &bottom);
// withMultipleFlag is there to draw only the non-multiple or the multiple windows
// withMultipleFlag == -1: all windows are drawn (standard)
// withMultipleFlag == 0: only one non-Multiple window is drawn
// withMultipleFlag == 1: only Multiple windows are drawn
// returns whether at least one child was drawn
bool DrawChildren(C4TargetFacet &cgo, int32_t player, int32_t withMultipleFlag = -1);
// used for commands that have been synchronized and are coming from the command queue
// attention: calls to this need to be synchronized!
bool ExecuteCommand(int32_t actionID, int32_t player, int32_t subwindowID, int32_t actionType, C4Object *target);
virtual bool MouseInput(int32_t player, int32_t button, int32_t mouseX, int32_t mouseY, DWORD dwKeyParam);
// virtual bool MouseInput(int32_t player, int32_t button, int32_t mouseX, int32_t mouseY, DWORD dwKeyParam);
// this is called only on the root menu
virtual void MouseInput(C4GUI::CMouse &rMouse, int32_t iButton, int32_t iX, int32_t iY, DWORD dwKeyParam);
// this is then called on the child windows, note the return value
virtual bool ProcessMouseInput(C4GUI::CMouse &rMouse, int32_t iButton, int32_t iX, int32_t iY, DWORD dwKeyParam);
// called when mouse cursor enters element region
virtual void MouseEnter(C4GUI::CMouse &rMouse);
// called when mouse cursor leaves element region
virtual void MouseLeave(C4GUI::CMouse &rMouse);
// this remembers whether the window currently has mouse focus and whether it has been mouse-down-ed
// all windows with mouse focus set are remembered by their parents and notified when the mouse left
enum MouseState // values of this enum will be bit-wise combined
{
None = 0,
Focus = 1,
MouseDown = 2
};
int32_t currentMouseState; // this needs to be saved in savegames!!!
// OnMouseOut() called by this window, unsets the mouse focus
// must notify children, too!
void OnMouseOut(int32_t player);
void OnMouseIn(int32_t player); // called by this window, sets the mouse focus
bool HasMouseFocus() { return currentMouseState & MouseState::Focus; }
private:
// TODO: actually scale with font size (needs font to be able to scale first..)
float Em2Pix(float em) { return 12.0f * em; }

View File

@ -55,9 +55,12 @@ public:
void Enlarge(int32_t iBy)
{ Enlarge(iBy, iBy); }
int32_t GetMiddleX() { return x+Wdt/2; }
int32_t GetMiddleY() { return y+Hgt/2; }
int32_t GetBottom() { return y+Hgt; }
int32_t GetMiddleX() const { return x+Wdt/2; }
int32_t GetMiddleY() const { return y + Hgt / 2; }
int32_t GetBottom() const { return y + Hgt; }
int32_t GetTop() const { return y; }
int32_t GetLeft() const { return x; }
int32_t GetRight() const { return x + Wdt; }
C4Rect(int32_t tx, int32_t ty, int32_t twdt, int32_t thgt) // ctor
{ x=tx; y=ty; Wdt=twdt; Hgt=thgt; }