From 969112b9c8d6c65bd29ce3e96ed8a0628fde31fa Mon Sep 17 00:00:00 2001 From: vladislavbelov Date: Sun, 28 Mar 2021 21:55:13 +0000 Subject: [PATCH] Replaces CPos by more a consistent CVector2D, reduces geometry code duplication. Tested By: Freagarach, Langbart Differential Revision: https://code.wildfiregames.com/D3759 This was SVN commit r25152. --- .../data/mods/public/gui/summary/summary.js | 4 +- source/gui/CGUI.cpp | 6 +- source/gui/CGUI.h | 5 +- source/gui/CGUIScrollBarVertical.cpp | 24 +-- source/gui/CGUIScrollBarVertical.h | 8 +- source/gui/CGUIText.cpp | 6 +- source/gui/CGUIText.h | 5 +- source/gui/GUIRenderer.cpp | 2 +- source/gui/GUISettingTypes.h | 4 +- source/gui/GUIStringConversions.cpp | 12 +- source/gui/GUITooltip.cpp | 8 +- source/gui/GUITooltip.h | 10 +- source/gui/IGUIScrollBar.cpp | 8 +- source/gui/IGUIScrollBar.h | 11 +- source/gui/ObjectBases/IGUIObject.cpp | 6 +- source/gui/ObjectBases/IGUITextOwner.cpp | 13 +- source/gui/ObjectBases/IGUITextOwner.h | 10 +- source/gui/ObjectTypes/CButton.h | 3 +- source/gui/ObjectTypes/CChart.cpp | 8 +- source/gui/ObjectTypes/CChart.h | 2 +- source/gui/ObjectTypes/CDropDown.cpp | 22 +-- source/gui/ObjectTypes/CDropDown.h | 5 +- source/gui/ObjectTypes/CInput.cpp | 18 +- source/gui/ObjectTypes/CList.cpp | 14 +- source/gui/ObjectTypes/CMiniMap.cpp | 12 +- source/gui/ObjectTypes/COList.cpp | 24 +-- source/gui/ObjectTypes/CSlider.cpp | 2 +- source/gui/ObjectTypes/CSlider.h | 3 +- source/gui/ObjectTypes/CText.cpp | 2 +- source/gui/ObjectTypes/CText.h | 2 +- source/gui/ObjectTypes/CTooltip.cpp | 10 +- source/gui/ObjectTypes/CTooltip.h | 7 +- source/gui/Scripting/GuiScriptConversions.cpp | 112 ++++++------ source/gui/tests/test_ParseString.h | 14 +- source/maths/Vector2D.cpp | 58 +++++++ source/maths/Vector2D.h | 34 ++-- source/ps/Shapes.cpp | 160 ++++-------------- source/ps/Shapes.h | 70 ++------ source/scriptinterface/ScriptConversions.cpp | 28 +-- 39 files changed, 340 insertions(+), 412 deletions(-) create mode 100644 source/maths/Vector2D.cpp diff --git a/binaries/data/mods/public/gui/summary/summary.js b/binaries/data/mods/public/gui/summary/summary.js index dd239becb7..4412af63df 100644 --- a/binaries/data/mods/public/gui/summary/summary.js +++ b/binaries/data/mods/public/gui/summary/summary.js @@ -350,7 +350,7 @@ function updateChart(number, category, item, itemNumber, type) g_ScorePanelsData[category].counters, g_ScorePanelsData[category].headings); if (type) value = value[type]; - data.push([g_GameData.sim.playerStates[1].sequences.time[index], value]); + data.push({ "x": g_GameData.sim.playerStates[1].sequences.time[index], "y": value }); } series.push(data); } @@ -364,7 +364,7 @@ function updateChart(number, category, item, itemNumber, type) let value = g_ScorePanelsData[category].counters[itemNumber].fn(playerState, index, item); if (type) value = value[type]; - data.push([playerState.sequences.time[index], value]); + data.push({ "x": playerState.sequences.time[index], "y": value }); } series.push(data); } diff --git a/source/gui/CGUI.cpp b/source/gui/CGUI.cpp index cfa61c166a..77860c0b62 100644 --- a/source/gui/CGUI.cpp +++ b/source/gui/CGUI.cpp @@ -126,7 +126,7 @@ InReaction CGUI::HandleEvent(const SDL_Event_* ev) // Yes the mouse position is stored as float to avoid // constant conversions when operating in a // float-based environment. - m_MousePos = CPos((float)ev->ev.motion.x / g_GuiScale, (float)ev->ev.motion.y / g_GuiScale); + m_MousePos = CVector2D((float)ev->ev.motion.x / g_GuiScale, (float)ev->ev.motion.y / g_GuiScale); SGUIMessage msg(GUIM_MOUSE_MOTION); m_BaseObject->RecurseObject(&IGUIObject::IsHiddenOrGhost, &IGUIObject::HandleMessage, msg); @@ -148,10 +148,10 @@ InReaction CGUI::HandleEvent(const SDL_Event_* ev) } // Update m_MousePos (for delayed mouse button events) - CPos oldMousePos = m_MousePos; + CVector2D oldMousePos = m_MousePos; if (ev->ev.type == SDL_MOUSEBUTTONDOWN || ev->ev.type == SDL_MOUSEBUTTONUP) { - m_MousePos = CPos((float)ev->ev.button.x / g_GuiScale, (float)ev->ev.button.y / g_GuiScale); + m_MousePos = CVector2D((float)ev->ev.button.x / g_GuiScale, (float)ev->ev.button.y / g_GuiScale); } // Allow the focused object to pre-empt regular GUI events. diff --git a/source/gui/CGUI.h b/source/gui/CGUI.h index f7dbaa897c..fc2fcc95b8 100644 --- a/source/gui/CGUI.h +++ b/source/gui/CGUI.h @@ -29,6 +29,7 @@ #include "gui/SGUIStyle.h" #include "lib/input.h" #include "maths/Size2D.h" +#include "maths/Vector2D.h" #include "ps/Shapes.h" #include "ps/XML/Xeromyces.h" #include "scriptinterface/ScriptInterface.h" @@ -179,7 +180,7 @@ public: /** * Returns the current screen coordinates of the cursor. */ - const CPos& GetMousePos() const { return m_MousePos; }; + const CVector2D& GetMousePos() const { return m_MousePos; }; /** * Returns the currently pressed mouse buttons. @@ -555,7 +556,7 @@ private: * ChooseMouseOverAndClosest broadcast - * we'd need to pack this and pNearest in a struct */ - CPos m_MousePos; + CVector2D m_MousePos; /** * Indicates which buttons are pressed (bit 0 = LMB, diff --git a/source/gui/CGUIScrollBarVertical.cpp b/source/gui/CGUIScrollBarVertical.cpp index b9fc3b9749..0cf5874eda 100644 --- a/source/gui/CGUIScrollBarVertical.cpp +++ b/source/gui/CGUIScrollBarVertical.cpp @@ -31,7 +31,7 @@ CGUIScrollBarVertical::~CGUIScrollBarVertical() { } -void CGUIScrollBarVertical::SetPosFromMousePos(const CPos& mouse) +void CGUIScrollBarVertical::SetPosFromMousePos(const CVector2D& mouse) { if (!GetStyle()) return; @@ -44,7 +44,7 @@ void CGUIScrollBarVertical::SetPosFromMousePos(const CPos& mouse) if (GetStyle()->m_UseEdgeButtons) emptyBackground -= GetStyle()->m_Width * 2; - m_Pos = m_PosWhenPressed + GetMaxPos() * (mouse.y - m_BarPressedAtPos.y) / emptyBackground; + m_Pos = m_PosWhenPressed + GetMaxPos() * (mouse.Y - m_BarPressedAtPos.Y) / emptyBackground; } void CGUIScrollBarVertical::Draw() @@ -169,28 +169,28 @@ CRect CGUIScrollBarVertical::GetOuterRect() const return ret; } -bool CGUIScrollBarVertical::HoveringButtonMinus(const CPos& mouse) +bool CGUIScrollBarVertical::HoveringButtonMinus(const CVector2D& mouse) { if (!GetStyle()) return false; float StartX = m_RightAligned ? m_X-GetStyle()->m_Width : m_X; - return mouse.x >= StartX && - mouse.x <= StartX + GetStyle()->m_Width && - mouse.y >= m_Y && - mouse.y <= m_Y + GetStyle()->m_Width; + return mouse.X >= StartX && + mouse.X <= StartX + GetStyle()->m_Width && + mouse.Y >= m_Y && + mouse.Y <= m_Y + GetStyle()->m_Width; } -bool CGUIScrollBarVertical::HoveringButtonPlus(const CPos& mouse) +bool CGUIScrollBarVertical::HoveringButtonPlus(const CVector2D& mouse) { if (!GetStyle()) return false; float StartX = m_RightAligned ? m_X-GetStyle()->m_Width : m_X; - return mouse.x > StartX && - mouse.x < StartX + GetStyle()->m_Width && - mouse.y > m_Y + m_Length - GetStyle()->m_Width && - mouse.y < m_Y + m_Length; + return mouse.X > StartX && + mouse.X < StartX + GetStyle()->m_Width && + mouse.Y > m_Y + m_Length - GetStyle()->m_Width && + mouse.Y < m_Y + m_Length; } diff --git a/source/gui/CGUIScrollBarVertical.h b/source/gui/CGUIScrollBarVertical.h index aaa545b337..c1ab1c63e9 100644 --- a/source/gui/CGUIScrollBarVertical.h +++ b/source/gui/CGUIScrollBarVertical.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2019 Wildfire Games. +/* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -55,17 +55,17 @@ public: /** * Set m_Pos with g_mouse_x/y input, i.e. when dragging. */ - virtual void SetPosFromMousePos(const CPos& mouse); + virtual void SetPosFromMousePos(const CVector2D& mouse); /** * @see IGUIScrollBar#HoveringButtonMinus */ - virtual bool HoveringButtonMinus(const CPos& mouse); + virtual bool HoveringButtonMinus(const CVector2D& mouse); /** * @see IGUIScrollBar#HoveringButtonPlus */ - virtual bool HoveringButtonPlus(const CPos& mouse); + virtual bool HoveringButtonPlus(const CVector2D& mouse); /** * Set Right Aligned diff --git a/source/gui/CGUIText.cpp b/source/gui/CGUIText.cpp index 74128cb590..7c9c2326cc 100644 --- a/source/gui/CGUIText.cpp +++ b/source/gui/CGUIText.cpp @@ -368,7 +368,7 @@ bool CGUIText::AssembleCalls( for (STextCall& tc : Feedback2.m_TextCalls) { - tc.m_Pos = CPos(dx + x + x_pointer, y); + tc.m_Pos = CVector2D(dx + x + x_pointer, y); x_pointer += tc.m_Size.Width; @@ -426,7 +426,7 @@ bool CGUIText::AssembleCalls( return done; } -void CGUIText::Draw(CGUI& pGUI, const CGUIColor& DefaultColor, const CPos& pos, const float z, const CRect& clipping) const +void CGUIText::Draw(CGUI& pGUI, const CGUIColor& DefaultColor, const CVector2D& pos, const float z, const CRect& clipping) const { CShaderTechniquePtr tech = g_Renderer.GetShaderManager().LoadEffect(str_gui_text); @@ -455,7 +455,7 @@ void CGUIText::Draw(CGUI& pGUI, const CGUIColor& DefaultColor, const CPos& pos, textRenderer.Color(tc.m_UseCustomColor ? tc.m_Color : DefaultColor); textRenderer.Font(tc.m_Font); - textRenderer.Put(floorf(pos.x + tc.m_Pos.x), floorf(pos.y + tc.m_Pos.y), &tc.m_String); + textRenderer.Put(floorf(pos.X + tc.m_Pos.X), floorf(pos.Y + tc.m_Pos.Y), &tc.m_String); } textRenderer.Render(); diff --git a/source/gui/CGUIText.h b/source/gui/CGUIText.h index 79c315d425..ebe2f070b3 100644 --- a/source/gui/CGUIText.h +++ b/source/gui/CGUIText.h @@ -22,6 +22,7 @@ #include "gui/SettingTypes/CGUIColor.h" #include "gui/SettingTypes/EAlign.h" #include "maths/Size2D.h" +#include "maths/Vector2D.h" #include "ps/CStrIntern.h" #include "ps/Shapes.h" @@ -102,7 +103,7 @@ public: /** * Position */ - CPos m_Pos; + CVector2D m_Pos; /** * Size @@ -171,7 +172,7 @@ public: /** * Draw this CGUIText object */ - void Draw(CGUI& pGUI, const CGUIColor& DefaultColor, const CPos& pos, const float z, const CRect& clipping) const; + void Draw(CGUI& pGUI, const CGUIColor& DefaultColor, const CVector2D& pos, const float z, const CRect& clipping) const; const CSize2D& GetSize() const { return m_Size; } diff --git a/source/gui/GUIRenderer.cpp b/source/gui/GUIRenderer.cpp index 4069ce3d6d..22a274b0eb 100644 --- a/source/gui/GUIRenderer.cpp +++ b/source/gui/GUIRenderer.cpp @@ -295,7 +295,7 @@ CRect SDrawCall::ComputeTexCoords() const // UV coords at each vertex of the object). We know everything // except for TexCoords, so calculate it: - CPos translation (BlockTex.TopLeft()-BlockScreen.TopLeft()); + CVector2D translation(BlockTex.TopLeft()-BlockScreen.TopLeft()); float ScaleW = BlockTex.GetWidth()/BlockScreen.GetWidth(); float ScaleH = BlockTex.GetHeight()/BlockScreen.GetHeight(); diff --git a/source/gui/GUISettingTypes.h b/source/gui/GUISettingTypes.h index 400026eb7c..f7d2417491 100644 --- a/source/gui/GUISettingTypes.h +++ b/source/gui/GUISettingTypes.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2019 Wildfire Games. +/* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -33,7 +33,7 @@ TYPE(u32) TYPE(float) TYPE(EAlign) TYPE(EVAlign) -TYPE(CPos) +TYPE(CVector2D) #endif #ifndef GUITYPE_IGNORE_NONCOPYABLE diff --git a/source/gui/GUIStringConversions.cpp b/source/gui/GUIStringConversions.cpp index abd7d9d9aa..827fe1c6b7 100644 --- a/source/gui/GUIStringConversions.cpp +++ b/source/gui/GUIStringConversions.cpp @@ -141,7 +141,7 @@ bool CGUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CS } template <> -bool CGUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CPos& Output) +bool CGUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CVector2D& Output) { const unsigned int NUM_COORDS = 2; float coords[NUM_COORDS]; @@ -152,23 +152,23 @@ bool CGUI::ParseString(const CGUI* UNUSED(pGUI), const CStrW& Value, CPos& { if (stream.eof()) { - LOGWARNING("Too few CPos parameters (min %i). Your input: '%s'", NUM_COORDS, Value.ToUTF8().c_str()); + LOGWARNING("Too few CVector2D parameters (min %i). Your input: '%s'", NUM_COORDS, Value.ToUTF8().c_str()); return false; } stream >> coords[i]; if ((stream.rdstate() & std::wstringstream::failbit) != 0) { - LOGWARNING("Unable to parse CPos parameters. Your input: '%s'", Value.ToUTF8().c_str()); + LOGWARNING("Unable to parse CVector2D parameters. Your input: '%s'", Value.ToUTF8().c_str()); return false; } } - Output.x = coords[0]; - Output.y = coords[1]; + Output.X = coords[0]; + Output.Y = coords[1]; if (!stream.eof()) { - LOGWARNING("Too many CPos parameters (max %i). Your input: '%s'", NUM_COORDS, Value.ToUTF8().c_str()); + LOGWARNING("Too many CVector2D parameters (max %i). Your input: '%s'", NUM_COORDS, Value.ToUTF8().c_str()); return false; } diff --git a/source/gui/GUITooltip.cpp b/source/gui/GUITooltip.cpp index 6947f7f48e..59e1f156a5 100644 --- a/source/gui/GUITooltip.cpp +++ b/source/gui/GUITooltip.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2019 Wildfire Games. +/* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -110,7 +110,7 @@ bool GUITooltip::GetTooltip(IGUIObject* obj, CStr& style) return false; } -void GUITooltip::ShowTooltip(IGUIObject* obj, const CPos& pos, const CStr& style, CGUI& pGUI) +void GUITooltip::ShowTooltip(IGUIObject* obj, const CVector2D& pos, const CStr& style, CGUI& pGUI) { ENSURE(obj); @@ -134,7 +134,7 @@ void GUITooltip::ShowTooltip(IGUIObject* obj, const CPos& pos, const CStr& style if (usedobj->SettingExists("_mousepos")) { - usedobj->SetSetting("_mousepos", pos, true); + usedobj->SetSetting("_mousepos", pos, true); } else { @@ -216,7 +216,7 @@ static i32 GetTooltipDelay(const CStr& style, CGUI& pGUI) return tooltipobj->GetSetting("delay"); } -void GUITooltip::Update(IGUIObject* Nearest, const CPos& MousePos, CGUI& GUI) +void GUITooltip::Update(IGUIObject* Nearest, const CVector2D& MousePos, CGUI& GUI) { // Called once per frame, so efficiency isn't vital double now = timer_Time(); diff --git a/source/gui/GUITooltip.h b/source/gui/GUITooltip.h index a52c2861f1..ad7b4ea8b4 100644 --- a/source/gui/GUITooltip.h +++ b/source/gui/GUITooltip.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2019 Wildfire Games. +/* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -21,8 +21,8 @@ class IGUIObject; class CGUI; +#include "maths/Vector2D.h" #include "ps/CStr.h" -#include "ps/Shapes.h" class GUITooltip { @@ -30,11 +30,11 @@ public: NONCOPYABLE(GUITooltip); GUITooltip(); - void Update(IGUIObject* Nearest, const CPos& MousePos, CGUI& GUI); + void Update(IGUIObject* Nearest, const CVector2D& MousePos, CGUI& GUI); private: - void ShowTooltip(IGUIObject* obj, const CPos& pos, const CStr& style, CGUI& pGUI); + void ShowTooltip(IGUIObject* obj, const CVector2D& pos, const CStr& style, CGUI& pGUI); void HideTooltip(const CStr& style, CGUI& pGUI); bool GetTooltip(IGUIObject* obj, CStr& style); @@ -42,7 +42,7 @@ private: IGUIObject* m_PreviousObject; CStr m_PreviousTooltipName; - CPos m_PreviousMousePos; + CVector2D m_PreviousMousePos; double m_Time; bool m_IsIconTooltip; }; diff --git a/source/gui/IGUIScrollBar.cpp b/source/gui/IGUIScrollBar.cpp index b74ce1a5f7..30c97b288a 100644 --- a/source/gui/IGUIScrollBar.cpp +++ b/source/gui/IGUIScrollBar.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2020 Wildfire Games. +/* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -92,7 +92,7 @@ void IGUIScrollBar::HandleMessage(SGUIMessage& Message) { // TODO Gee: Optimizations needed! - const CPos& mouse = m_pGUI.GetMousePos(); + const CVector2D& mouse = m_pGUI.GetMousePos(); // If bar is being dragged if (m_BarPressed) @@ -119,7 +119,7 @@ void IGUIScrollBar::HandleMessage(SGUIMessage& Message) if (!m_pHostObject) break; - const CPos& mouse = m_pGUI.GetMousePos(); + const CVector2D& mouse = m_pGUI.GetMousePos(); // if bar is pressed if (GetBarRect().PointInside(mouse)) @@ -149,7 +149,7 @@ void IGUIScrollBar::HandleMessage(SGUIMessage& Message) { // Scroll plus or minus a lot, this might change, it doesn't // have to be fancy though. - if (mouse.y < GetBarRect().top) + if (mouse.Y < GetBarRect().top) ScrollMinusPlenty(); else ScrollPlusPlenty(); diff --git a/source/gui/IGUIScrollBar.h b/source/gui/IGUIScrollBar.h index 5a55e1dedb..023c516b5e 100644 --- a/source/gui/IGUIScrollBar.h +++ b/source/gui/IGUIScrollBar.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2019 Wildfire Games. +/* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -25,6 +25,7 @@ #define INCLUDED_IGUISCROLLBAR #include "gui/CGUISprite.h" +#include "maths/Vector2D.h" #include "ps/CStr.h" class CGUI; @@ -175,7 +176,7 @@ public: /** * Set m_Pos with g_mouse_x/y input, i.e. when draggin. */ - virtual void SetPosFromMousePos(const CPos& mouse) = 0; + virtual void SetPosFromMousePos(const CVector2D& mouse) = 0; /** * Hovering the scroll minus button @@ -183,7 +184,7 @@ public: * @param mouse current mouse position * @return True if mouse positions are hovering the button */ - virtual bool HoveringButtonMinus(const CPos& UNUSED(mouse)) { return false; } + virtual bool HoveringButtonMinus(const CVector2D& UNUSED(mouse)) { return false; } /** * Hovering the scroll plus button @@ -191,7 +192,7 @@ public: * @param mouse current mouse position * @return True if mouse positions are hovering the button */ - virtual bool HoveringButtonPlus(const CPos& UNUSED(mouse)) { return false; } + virtual bool HoveringButtonPlus(const CVector2D& UNUSED(mouse)) { return false; } /** * Get scroll-position @@ -393,7 +394,7 @@ protected: /** * Mouse position when bar was pressed */ - CPos m_BarPressedAtPos; + CVector2D m_BarPressedAtPos; //@} //-------------------------------------------------------- diff --git a/source/gui/ObjectBases/IGUIObject.cpp b/source/gui/ObjectBases/IGUIObject.cpp index 9585d5ca2e..cc7f2960c8 100644 --- a/source/gui/ObjectBases/IGUIObject.cpp +++ b/source/gui/ObjectBases/IGUIObject.cpp @@ -405,13 +405,13 @@ InReaction IGUIObject::SendMouseEvent(EGUIMessageType type, const CStr& eventNam // Set up the 'mouse' parameter JS::RootedValue mouse(rq.cx); - const CPos& mousePos = m_pGUI.GetMousePos(); + const CVector2D& mousePos = m_pGUI.GetMousePos(); ScriptInterface::CreateObject( rq, &mouse, - "x", mousePos.x, - "y", mousePos.y, + "x", mousePos.X, + "y", mousePos.Y, "buttons", m_pGUI.GetMouseButtons()); JS::RootedValueVector paramData(rq.cx); ignore_result(paramData.append(mouse)); diff --git a/source/gui/ObjectBases/IGUITextOwner.cpp b/source/gui/ObjectBases/IGUITextOwner.cpp index 7e94b71462..38f993ae86 100644 --- a/source/gui/ObjectBases/IGUITextOwner.cpp +++ b/source/gui/ObjectBases/IGUITextOwner.cpp @@ -23,6 +23,7 @@ #include "gui/SGUIMessage.h" #include "gui/ObjectBases/IGUIObject.h" #include "gui/SettingTypes/CGUIString.h" +#include "maths/Vector2D.h" #include @@ -89,7 +90,7 @@ void IGUITextOwner::UpdateText() } } -void IGUITextOwner::DrawText(size_t index, const CGUIColor& color, const CPos& pos, float z, const CRect& clipping) +void IGUITextOwner::DrawText(size_t index, const CGUIColor& color, const CVector2D& pos, float z, const CRect& clipping) { UpdateText(); @@ -98,23 +99,23 @@ void IGUITextOwner::DrawText(size_t index, const CGUIColor& color, const CPos& p m_GeneratedTexts.at(index).Draw(m_pObject.GetGUI(), color, pos, z, clipping); } -void IGUITextOwner::CalculateTextPosition(CRect& ObjSize, CPos& TextPos, CGUIText& Text) +void IGUITextOwner::CalculateTextPosition(CRect& ObjSize, CVector2D& TextPos, CGUIText& Text) { // The horizontal Alignment is now computed in GenerateText in order to not have to // loop through all of the TextCall objects again. - TextPos.x = ObjSize.left; + TextPos.X = ObjSize.left; switch (m_pObject.GetSetting("text_valign")) { case EVAlign::TOP: - TextPos.y = ObjSize.top; + TextPos.Y = ObjSize.top; break; case EVAlign::CENTER: // Round to integer pixel values, else the fonts look awful - TextPos.y = floorf(ObjSize.CenterPoint().y - Text.GetSize().Height / 2.f); + TextPos.Y = floorf(ObjSize.CenterPoint().Y - Text.GetSize().Height / 2.f); break; case EVAlign::BOTTOM: - TextPos.y = ObjSize.bottom - Text.GetSize().Height; + TextPos.Y = ObjSize.bottom - Text.GetSize().Height; break; default: debug_warn(L"Broken EVAlign in CButton::SetupText()"); diff --git a/source/gui/ObjectBases/IGUITextOwner.h b/source/gui/ObjectBases/IGUITextOwner.h index 2e32c236c4..aa1339ae91 100644 --- a/source/gui/ObjectBases/IGUITextOwner.h +++ b/source/gui/ObjectBases/IGUITextOwner.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2020 Wildfire Games. +/* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -34,13 +34,15 @@ GUI Object Base - Text Owner #include -class CStrW; struct CGUIColor; struct SGUIMessage; class CGUIText; class CGUIString; class IGUIObject; +class CStrW; +class CVector2D; + /** * Framework for handling Output text. */ @@ -82,7 +84,7 @@ public: * @param clipping Clipping rectangle, don't even add a parameter * to get no clipping. */ - virtual void DrawText(size_t index, const CGUIColor& color, const CPos& pos, float z, const CRect& clipping = CRect()); + virtual void DrawText(size_t index, const CGUIColor& color, const CVector2D& pos, float z, const CRect& clipping = CRect()); /** * Test if mouse position is over an icon @@ -113,7 +115,7 @@ protected: /** * Calculate the position for the text, based on the alignment. */ - void CalculateTextPosition(CRect& ObjSize, CPos& TextPos, CGUIText& Text); + void CalculateTextPosition(CRect& ObjSize, CVector2D& TextPos, CGUIText& Text); private: /** diff --git a/source/gui/ObjectTypes/CButton.h b/source/gui/ObjectTypes/CButton.h index 88db28e257..37de47593b 100644 --- a/source/gui/ObjectTypes/CButton.h +++ b/source/gui/ObjectTypes/CButton.h @@ -23,6 +23,7 @@ #include "gui/ObjectBases/IGUIObject.h" #include "gui/ObjectBases/IGUITextOwner.h" #include "gui/SettingTypes/CGUIString.h" +#include "maths/Vector2D.h" class CButton : public IGUIObject, public IGUITextOwner, public IGUIButtonBehavior { @@ -71,7 +72,7 @@ protected: /** * Placement of text. */ - CPos m_TextPos; + CVector2D m_TextPos; virtual void CreateJSObject(); diff --git a/source/gui/ObjectTypes/CChart.cpp b/source/gui/ObjectTypes/CChart.cpp index 40835854e8..0a043f05dc 100644 --- a/source/gui/ObjectTypes/CChart.cpp +++ b/source/gui/ObjectTypes/CChart.cpp @@ -183,8 +183,8 @@ void CChart::Draw() CRect CChart::GetChartRect() const { return CRect( - m_CachedActualSize.TopLeft() + CPos(m_AxisWidth, m_AxisWidth), - m_CachedActualSize.BottomRight() - CPos(m_AxisWidth, m_AxisWidth) + m_CachedActualSize.TopLeft() + CVector2D(m_AxisWidth, m_AxisWidth), + m_CachedActualSize.BottomRight() - CVector2D(m_AxisWidth, m_AxisWidth) ); } @@ -228,7 +228,7 @@ void CChart::SetupText() for (int i = 0; i < 3; ++i) { AddFormattedValue(m_FormatY, m_RightTop.Y - (m_RightTop.Y - m_LeftBottom.Y) / 3.f * i, m_Font, m_BufferZone); - m_TextPositions.emplace_back(GetChartRect().TopLeft() + CPos(0.f, height / 3.f * i)); + m_TextPositions.emplace_back(GetChartRect().TopLeft() + CVector2D(0.f, height / 3.f * i)); } // Add X-axis @@ -242,7 +242,7 @@ void CChart::SetupText() for (int i = 0; i < 3; ++i) { CSize2D text_size = AddFormattedValue(m_FormatX, m_RightTop.X - (m_RightTop.X - m_LeftBottom.X) / 3 * i, m_Font, m_BufferZone); - m_TextPositions.emplace_back(GetChartRect().BottomRight() - text_size - CPos(width / 3 * i, 0.f)); + m_TextPositions.emplace_back(GetChartRect().BottomRight() - text_size - CVector2D(width / 3 * i, 0.f)); } } diff --git a/source/gui/ObjectTypes/CChart.h b/source/gui/ObjectTypes/CChart.h index d4d542a828..3c787b8b51 100644 --- a/source/gui/ObjectTypes/CChart.h +++ b/source/gui/ObjectTypes/CChart.h @@ -77,7 +77,7 @@ protected: CVector2D m_LeftBottom, m_RightTop; - std::vector m_TextPositions; + std::vector m_TextPositions; bool m_EqualX, m_EqualY; diff --git a/source/gui/ObjectTypes/CDropDown.cpp b/source/gui/ObjectTypes/CDropDown.cpp index 2cb97f0763..e9d5f96a09 100644 --- a/source/gui/ObjectTypes/CDropDown.cpp +++ b/source/gui/ObjectTypes/CDropDown.cpp @@ -118,7 +118,7 @@ void CDropDown::HandleMessage(SGUIMessage& Message) if (!m_Open) break; - CPos mouse = m_pGUI.GetMousePos(); + CVector2D mouse = m_pGUI.GetMousePos(); if (!GetListRect().PointInside(mouse)) break; @@ -126,16 +126,16 @@ void CDropDown::HandleMessage(SGUIMessage& Message) const float scroll = m_ScrollBar ? GetScrollBar(0).GetPos() : 0.f; CRect rect = GetListRect(); - mouse.y += scroll; + mouse.Y += scroll; int set = -1; for (int i = 0; i < static_cast(m_List.m_Items.size()); ++i) { - if (mouse.y >= rect.top + m_ItemsYPositions[i] && - mouse.y < rect.top + m_ItemsYPositions[i+1] && + if (mouse.Y >= rect.top + m_ItemsYPositions[i] && + mouse.Y < rect.top + m_ItemsYPositions[i+1] && // mouse is not over scroll-bar (m_HideScrollBar || - mouse.x < GetScrollBar(0).GetOuterRect().left || - mouse.x > GetScrollBar(0).GetOuterRect().right)) + mouse.X < GetScrollBar(0).GetOuterRect().left || + mouse.X > GetScrollBar(0).GetOuterRect().right)) { set = i; } @@ -193,7 +193,7 @@ void CDropDown::HandleMessage(SGUIMessage& Message) } else { - const CPos& mouse = m_pGUI.GetMousePos(); + const CVector2D& mouse = m_pGUI.GetMousePos(); // If the regular area is pressed, then abort, and close. if (m_CachedActualSize.PointInside(mouse)) @@ -205,9 +205,9 @@ void CDropDown::HandleMessage(SGUIMessage& Message) } if (m_HideScrollBar || - mouse.x < GetScrollBar(0).GetOuterRect().left || - mouse.x > GetScrollBar(0).GetOuterRect().right || - mouse.y < GetListRect().top) + mouse.X < GetScrollBar(0).GetOuterRect().left || + mouse.X > GetScrollBar(0).GetOuterRect().right || + mouse.Y < GetListRect().top) { m_Open = false; GetScrollBar(0).SetZ(GetBufferedZ()); @@ -472,7 +472,7 @@ void CDropDown::Draw() CRect cliparea(m_CachedActualSize.left, m_CachedActualSize.top, m_CachedActualSize.right - m_ButtonWidth, m_CachedActualSize.bottom); - CPos pos(m_CachedActualSize.left, m_CachedActualSize.top); + CVector2D pos(m_CachedActualSize.left, m_CachedActualSize.top); DrawText(m_Selected, m_Enabled ? m_TextColorSelected : m_TextColorDisabled, pos, bz + 0.1f, cliparea); } diff --git a/source/gui/ObjectTypes/CDropDown.h b/source/gui/ObjectTypes/CDropDown.h index 32a5b15064..99aed4dda5 100644 --- a/source/gui/ObjectTypes/CDropDown.h +++ b/source/gui/ObjectTypes/CDropDown.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2020 Wildfire Games. +/* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -31,6 +31,7 @@ GUI Object - Drop Down (list) #include "gui/CGUISprite.h" #include "gui/ObjectBases/IGUIObject.h" #include "gui/ObjectTypes/CList.h" +#include "maths/Vector2D.h" #include @@ -96,7 +97,7 @@ protected: /** * Placement of text. */ - CPos m_TextPos; + CVector2D m_TextPos; // Is the dropdown opened? bool m_Open; diff --git a/source/gui/ObjectTypes/CInput.cpp b/source/gui/ObjectTypes/CInput.cpp index 0258c9920a..74f04c27a2 100644 --- a/source/gui/ObjectTypes/CInput.cpp +++ b/source/gui/ObjectTypes/CInput.cpp @@ -948,7 +948,7 @@ void CInput::HandleMessage(SGUIMessage& Message) m_MultiLine && GetScrollBar(0).GetStyle()) { - if (m_pGUI.GetMousePos().x > m_CachedActualSize.right - GetScrollBar(0).GetStyle()->m_Width) + if (m_pGUI.GetMousePos().X > m_CachedActualSize.right - GetScrollBar(0).GetStyle()->m_Width) break; } @@ -1126,8 +1126,8 @@ void CInput::HandleMessage(SGUIMessage& Message) SDL_Rect rect; rect.h = m_CachedActualSize.GetSize().Height; rect.w = m_CachedActualSize.GetSize().Width; - rect.x = m_CachedActualSize.TopLeft().x; - rect.y = m_CachedActualSize.TopLeft().y; + rect.x = m_CachedActualSize.TopLeft().X; + rect.y = m_CachedActualSize.TopLeft().Y; SDL_SetTextInputRect(&rect); SDL_StartTextInput(); break; @@ -1891,7 +1891,7 @@ int CInput::GetMouseHoveringTextPosition() const std::list::const_iterator current = m_CharacterPositions.begin(); - CPos mouse = m_pGUI.GetMousePos(); + CVector2D mouse = m_pGUI.GetMousePos(); if (m_MultiLine) { @@ -1906,10 +1906,10 @@ int CInput::GetMouseHoveringTextPosition() const // Change mouse position relative to text. mouse -= m_CachedActualSize.TopLeft(); - mouse.x -= m_BufferZone; - mouse.y += scroll - m_BufferZone; + mouse.X -= m_BufferZone; + mouse.Y += scroll - m_BufferZone; - int row = (int)((mouse.y) / spacing); + int row = (int)((mouse.Y) / spacing); if (row < 0) row = 0; @@ -1928,7 +1928,7 @@ int CInput::GetMouseHoveringTextPosition() const // current is already set to begin, // but we'll change the mouse.x to fit our horizontal scrolling mouse -= m_CachedActualSize.TopLeft(); - mouse.x -= m_BufferZone - m_HorizontalScroll; + mouse.X -= m_BufferZone - m_HorizontalScroll; // mouse.y is moot } @@ -1936,7 +1936,7 @@ int CInput::GetMouseHoveringTextPosition() const // Okay, now loop through the glyphs to find the appropriate X position float dummy; - retPosition += GetXTextPosition(current, mouse.x, dummy); + retPosition += GetXTextPosition(current, mouse.X, dummy); return retPosition; } diff --git a/source/gui/ObjectTypes/CList.cpp b/source/gui/ObjectTypes/CList.cpp index 46c24da8ef..12236e41bf 100644 --- a/source/gui/ObjectTypes/CList.cpp +++ b/source/gui/ObjectTypes/CList.cpp @@ -394,7 +394,7 @@ void CList::DrawList(const int& selected, const CGUISpriteInstance& sprite, cons cliparea.left = GetScrollBar(0).GetOuterRect().right; } - DrawText(i, textcolor, rect.TopLeft() - CPos(0.f, scroll - m_ItemsYPositions[i]), bz + 0.1f, cliparea); + DrawText(i, textcolor, rect.TopLeft() - CVector2D(0.f, scroll - m_ItemsYPositions[i]), bz + 0.1f, cliparea); } } } @@ -482,18 +482,18 @@ int CList::GetHoveredItem() const float scroll = m_ScrollBar ? GetScrollBar(0).GetPos() : 0.f; const CRect& rect = GetListRect(); - CPos mouse = m_pGUI.GetMousePos(); - mouse.y += scroll; + CVector2D mouse = m_pGUI.GetMousePos(); + mouse.Y += scroll; // Mouse is over scrollbar if (m_ScrollBar && GetScrollBar(0).IsVisible() && - mouse.x >= GetScrollBar(0).GetOuterRect().left && - mouse.x <= GetScrollBar(0).GetOuterRect().right) + mouse.X >= GetScrollBar(0).GetOuterRect().left && + mouse.X <= GetScrollBar(0).GetOuterRect().right) return -1; for (size_t i = 0; i < m_List.m_Items.size(); ++i) - if (mouse.y >= rect.top + m_ItemsYPositions[i] && - mouse.y < rect.top + m_ItemsYPositions[i + 1]) + if (mouse.Y >= rect.top + m_ItemsYPositions[i] && + mouse.Y < rect.top + m_ItemsYPositions[i + 1]) return i; return -1; diff --git a/source/gui/ObjectTypes/CMiniMap.cpp b/source/gui/ObjectTypes/CMiniMap.cpp index ec0540ea03..5682b10176 100644 --- a/source/gui/ObjectTypes/CMiniMap.cpp +++ b/source/gui/ObjectTypes/CMiniMap.cpp @@ -193,11 +193,11 @@ void CMiniMap::HandleMessage(SGUIMessage& Message) bool CMiniMap::IsMouseOver() const { // Get the mouse position. - const CPos& mousePos = m_pGUI.GetMousePos(); + const CVector2D& mousePos = m_pGUI.GetMousePos(); // Get the position of the center of the minimap. - CPos minimapCenter = CPos(m_CachedActualSize.left + m_CachedActualSize.GetWidth() / 2.0, m_CachedActualSize.bottom - m_CachedActualSize.GetHeight() / 2.0); + CVector2D minimapCenter = CVector2D(m_CachedActualSize.left + m_CachedActualSize.GetWidth() / 2.0, m_CachedActualSize.bottom - m_CachedActualSize.GetHeight() / 2.0); // Take the magnitude of the difference of the mouse position and minimap center. - double distFromCenter = sqrt(pow((mousePos.x - minimapCenter.x), 2) + pow((mousePos.y - minimapCenter.y), 2)); + double distFromCenter = sqrt(pow((mousePos.X - minimapCenter.X), 2) + pow((mousePos.Y - minimapCenter.Y), 2)); // If the distance is less then the radius of the minimap (half the width) the mouse is over the minimap. if (distFromCenter < m_CachedActualSize.GetWidth() / 2.0) return true; @@ -209,10 +209,10 @@ void CMiniMap::GetMouseWorldCoordinates(float& x, float& z) const { // Determine X and Z according to proportion of mouse position and minimap - const CPos& mousePos = m_pGUI.GetMousePos(); + const CVector2D& mousePos = m_pGUI.GetMousePos(); - float px = (mousePos.x - m_CachedActualSize.left) / m_CachedActualSize.GetWidth(); - float py = (m_CachedActualSize.bottom - mousePos.y) / m_CachedActualSize.GetHeight(); + float px = (mousePos.X - m_CachedActualSize.left) / m_CachedActualSize.GetWidth(); + float py = (m_CachedActualSize.bottom - mousePos.Y) / m_CachedActualSize.GetHeight(); float angle = GetAngle(); diff --git a/source/gui/ObjectTypes/COList.cpp b/source/gui/ObjectTypes/COList.cpp index 900ddbbe93..cf9a17ad89 100644 --- a/source/gui/ObjectTypes/COList.cpp +++ b/source/gui/ObjectTypes/COList.cpp @@ -27,7 +27,7 @@ #include "ps/CLogger.h" const float SORT_SPRITE_DIM = 16.0f; -const CPos COLUMN_SHIFT = CPos(0, 4); +const CVector2D COLUMN_SHIFT = CVector2D(0, 4); const CStr COList::EventNameSelectionColumnChange = "SelectionColumnChange"; @@ -76,7 +76,7 @@ void COList::SetupText() gui_string.SetValue(column.m_Heading); const CGUIText& text = AddText(gui_string, m_Font, width, m_BufferZone); - m_HeadingHeight = std::max(m_HeadingHeight, text.GetSize().Height + COLUMN_SHIFT.y); + m_HeadingHeight = std::max(m_HeadingHeight, text.GetSize().Height + COLUMN_SHIFT.Y); } // Generate texts @@ -139,7 +139,7 @@ void COList::HandleMessage(SGUIMessage& Message) if (!m_Sortable) return; - const CPos& mouse = m_pGUI.GetMousePos(); + const CVector2D& mouse = m_pGUI.GetMousePos(); if (!m_CachedActualSize.PointInside(mouse)) return; @@ -153,10 +153,10 @@ void COList::HandleMessage(SGUIMessage& Message) // Check if it's a decimal value, and if so, assume relative positioning. if (column.m_Width < 1 && column.m_Width > 0) width *= m_TotalAvailableColumnWidth; - CPos leftTopCorner = m_CachedActualSize.TopLeft() + CPos(xpos, 0); - if (mouse.x >= leftTopCorner.x && - mouse.x < leftTopCorner.x + width && - mouse.y < leftTopCorner.y + m_HeadingHeight) + CVector2D leftTopCorner = m_CachedActualSize.TopLeft() + CVector2D(xpos, 0); + if (mouse.X >= leftTopCorner.X && + mouse.X < leftTopCorner.X + width && + mouse.Y < leftTopCorner.Y + m_HeadingHeight) { if (column.m_Id != m_SelectedColumn) { @@ -361,7 +361,7 @@ void COList::DrawList(const int& selected, const CGUISpriteInstance& sprite, con if (column.m_Width < 1 && column.m_Width > 0) width *= m_TotalAvailableColumnWidth; - CPos leftTopCorner = m_CachedActualSize.TopLeft() + CPos(xpos, 0); + CVector2D leftTopCorner = m_CachedActualSize.TopLeft() + CVector2D(xpos, 0); // Draw sort arrows in colum header if (m_Sortable) @@ -380,7 +380,7 @@ void COList::DrawList(const int& selected, const CGUISpriteInstance& sprite, con else pSprite = &m_SpriteNotSorted; - m_pGUI.DrawSprite(*pSprite, bz + 0.1f, CRect(leftTopCorner + CPos(width - SORT_SPRITE_DIM, 0), leftTopCorner + CPos(width, SORT_SPRITE_DIM))); + m_pGUI.DrawSprite(*pSprite, bz + 0.1f, CRect(leftTopCorner + CVector2D(width - SORT_SPRITE_DIM, 0), leftTopCorner + CVector2D(width, SORT_SPRITE_DIM))); } // Draw column header text @@ -422,7 +422,7 @@ void COList::DrawList(const int& selected, const CGUISpriteInstance& sprite, con continue; // Determine text position and width - const CPos textPos = rect.TopLeft() + CPos(xpos, -scroll + m_ItemsYPositions[i]); + const CVector2D textPos = rect.TopLeft() + CVector2D(xpos, -scroll + m_ItemsYPositions[i]); float width = column.m_Width; // Check if it's a decimal value, and if so, assume relative positioning. @@ -431,8 +431,8 @@ void COList::DrawList(const int& selected, const CGUISpriteInstance& sprite, con // Clip text to the column (to prevent drawing text into the neighboring column) CRect cliparea2 = cliparea; - cliparea2.right = std::min(cliparea2.right, textPos.x + width); - cliparea2.bottom = std::min(cliparea2.bottom, textPos.y + rowHeight); + cliparea2.right = std::min(cliparea2.right, textPos.X + width); + cliparea2.bottom = std::min(cliparea2.bottom, textPos.Y + rowHeight); // Draw list item DrawText(objectsCount * (i +/*Heading*/1) + colIdx, column.m_TextColor, textPos, bz + 0.1f, cliparea2); diff --git a/source/gui/ObjectTypes/CSlider.cpp b/source/gui/ObjectTypes/CSlider.cpp index eabe85a138..98615845d2 100644 --- a/source/gui/ObjectTypes/CSlider.cpp +++ b/source/gui/ObjectTypes/CSlider.cpp @@ -98,7 +98,7 @@ void CSlider::HandleMessage(SGUIMessage& Message) if (m_Pressed) { m_Mouse = m_pGUI.GetMousePos(); - IncrementallyChangeValue((m_Mouse.x - GetButtonRect().CenterPoint().x) * GetSliderRatio()); + IncrementallyChangeValue((m_Mouse.X - GetButtonRect().CenterPoint().X) * GetSliderRatio()); } break; } diff --git a/source/gui/ObjectTypes/CSlider.h b/source/gui/ObjectTypes/CSlider.h index 87d226cfd1..9fcb216819 100644 --- a/source/gui/ObjectTypes/CSlider.h +++ b/source/gui/ObjectTypes/CSlider.h @@ -21,6 +21,7 @@ #include "gui/CGUISprite.h" #include "gui/ObjectBases/IGUIButtonBehavior.h" #include "gui/ObjectBases/IGUIObject.h" +#include "maths/Vector2D.h" class CSlider : public IGUIObject, public IGUIButtonBehavior { @@ -68,7 +69,7 @@ protected: float m_Value; private: - CPos m_Mouse; + CVector2D m_Mouse; }; #endif // INCLUDED_CSLIDER diff --git a/source/gui/ObjectTypes/CText.cpp b/source/gui/ObjectTypes/CText.cpp index 72000c1417..993cbdffa4 100644 --- a/source/gui/ObjectTypes/CText.cpp +++ b/source/gui/ObjectTypes/CText.cpp @@ -224,7 +224,7 @@ void CText::Draw() const CGUIColor& color = m_Enabled ? m_TextColor : m_TextColorDisabled; if (m_ScrollBar) - DrawText(0, color, m_CachedActualSize.TopLeft() - CPos(0.f, scroll), bz + 0.1f, cliparea); + DrawText(0, color, m_CachedActualSize.TopLeft() - CVector2D(0.f, scroll), bz + 0.1f, cliparea); else DrawText(0, color, m_TextPos, bz + 0.1f, cliparea); } diff --git a/source/gui/ObjectTypes/CText.h b/source/gui/ObjectTypes/CText.h index a4b5019a1a..be755a812e 100644 --- a/source/gui/ObjectTypes/CText.h +++ b/source/gui/ObjectTypes/CText.h @@ -75,7 +75,7 @@ protected: /** * Placement of text. Ignored when scrollbars are active. */ - CPos m_TextPos; + CVector2D m_TextPos; // Settings float m_BufferZone; diff --git a/source/gui/ObjectTypes/CTooltip.cpp b/source/gui/ObjectTypes/CTooltip.cpp index b4b3d41fe9..6def14fd36 100644 --- a/source/gui/ObjectTypes/CTooltip.cpp +++ b/source/gui/ObjectTypes/CTooltip.cpp @@ -85,27 +85,27 @@ void CTooltip::SetupText() // Position the tooltip relative to the mouse: - const CPos& mousepos = m_Independent ? m_pGUI.GetMousePos() : m_MousePos; + const CVector2D& mousepos = m_Independent ? m_pGUI.GetMousePos() : m_MousePos; float textwidth = m_GeneratedTexts[0].GetSize().Width; float textheight = m_GeneratedTexts[0].GetSize().Height; CGUISize size; - size.pixel.left = mousepos.x + m_Offset.x; + size.pixel.left = mousepos.X + m_Offset.X; size.pixel.right = size.pixel.left + textwidth; switch (m_Anchor) { case EVAlign::TOP: - size.pixel.top = mousepos.y + m_Offset.y; + size.pixel.top = mousepos.Y + m_Offset.Y; size.pixel.bottom = size.pixel.top + textheight; break; case EVAlign::BOTTOM: - size.pixel.bottom = mousepos.y + m_Offset.y; + size.pixel.bottom = mousepos.Y + m_Offset.Y; size.pixel.top = size.pixel.bottom - textheight; break; case EVAlign::CENTER: - size.pixel.top = mousepos.y + m_Offset.y - textheight/2.f; + size.pixel.top = mousepos.Y + m_Offset.Y - textheight/2.f; size.pixel.bottom = size.pixel.top + textwidth; break; default: diff --git a/source/gui/ObjectTypes/CTooltip.h b/source/gui/ObjectTypes/CTooltip.h index ab6149e3b3..508f306f58 100644 --- a/source/gui/ObjectTypes/CTooltip.h +++ b/source/gui/ObjectTypes/CTooltip.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2020 Wildfire Games. +/* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -22,6 +22,7 @@ #include "gui/ObjectBases/IGUIObject.h" #include "gui/ObjectBases/IGUITextOwner.h" #include "gui/SettingTypes/CGUIString.h" +#include "maths/Vector2D.h" /** * Dynamic tooltips. Similar to CText. @@ -57,11 +58,11 @@ protected: i32 m_Delay; CGUIColor m_TextColor; float m_MaxWidth; - CPos m_Offset; + CVector2D m_Offset; EVAlign m_Anchor; EAlign m_TextAlign; bool m_Independent; - CPos m_MousePos; + CVector2D m_MousePos; CStr m_UseObject; bool m_HideObject; }; diff --git a/source/gui/Scripting/GuiScriptConversions.cpp b/source/gui/Scripting/GuiScriptConversions.cpp index 15e8e3e3ae..5b7b3e7bf8 100644 --- a/source/gui/Scripting/GuiScriptConversions.cpp +++ b/source/gui/Scripting/GuiScriptConversions.cpp @@ -158,62 +158,6 @@ template<> void ScriptInterface::ToJSVal(const ScriptRequest& rq, JS: */ template<> bool ScriptInterface::FromJSVal(const ScriptRequest& rq, JS::HandleValue v, CGUIColor& out) = delete; -template<> void ScriptInterface::ToJSVal(const ScriptRequest& rq, JS::MutableHandleValue ret, const CSize2D& val) -{ - CreateObject(rq, ret, "width", val.Width, "height", val.Height); -} - -template<> bool ScriptInterface::FromJSVal(const ScriptRequest& rq, JS::HandleValue v, CSize2D& out) -{ - if (!v.isObject()) - { - LOGERROR("CSize2D value must be an object!"); - return false; - } - - if (!FromJSProperty(rq, v, "width", out.Width)) - { - LOGERROR("Failed to get CSize2D.Width property"); - return false; - } - - if (!FromJSProperty(rq, v, "height", out.Height)) - { - LOGERROR("Failed to get CSize2D.Height property"); - return false; - } - - return true; -} - -template<> void ScriptInterface::ToJSVal(const ScriptRequest& rq, JS::MutableHandleValue ret, const CPos& val) -{ - CreateObject(rq, ret, "x", val.x, "y", val.y); -} - -template<> bool ScriptInterface::FromJSVal(const ScriptRequest& rq, JS::HandleValue v, CPos& out) -{ - if (!v.isObject()) - { - LOGERROR("CPos value must be an object!"); - return false; - } - - if (!FromJSProperty(rq, v, "x", out.x)) - { - LOGERROR("Failed to get CPos.x property"); - return false; - } - - if (!FromJSProperty(rq, v, "y", out.y)) - { - LOGERROR("Failed to get CPos.y property"); - return false; - } - - return true; -} - template<> void ScriptInterface::ToJSVal(const ScriptRequest& rq, JS::MutableHandleValue ret, const CRect& val) { CreateObject( @@ -357,4 +301,60 @@ template<> bool ScriptInterface::FromJSVal(const ScriptReque return true; } +template<> void ScriptInterface::ToJSVal(const ScriptRequest& rq, JS::MutableHandleValue ret, const CSize2D& val) +{ + CreateObject(rq, ret, "width", val.Width, "height", val.Height); +} + +template<> bool ScriptInterface::FromJSVal(const ScriptRequest& rq, JS::HandleValue v, CSize2D& out) +{ + if (!v.isObject()) + { + LOGERROR("CSize2D value must be an object!"); + return false; + } + + if (!FromJSProperty(rq, v, "width", out.Width)) + { + LOGERROR("Failed to get CSize2D.Width property"); + return false; + } + + if (!FromJSProperty(rq, v, "height", out.Height)) + { + LOGERROR("Failed to get CSize2D.Height property"); + return false; + } + + return true; +} + +template<> void ScriptInterface::ToJSVal(const ScriptRequest& rq, JS::MutableHandleValue ret, const CVector2D& val) +{ + CreateObject(rq, ret, "x", val.X, "y", val.Y); +} + +template<> bool ScriptInterface::FromJSVal(const ScriptRequest& rq, JS::HandleValue v, CVector2D& out) +{ + if (!v.isObject()) + { + LOGERROR("CVector2D value must be an object!"); + return false; + } + + if (!FromJSProperty(rq, v, "x", out.X)) + { + LOGERROR("Failed to get CVector2D.X property"); + return false; + } + + if (!FromJSProperty(rq, v, "y", out.Y)) + { + LOGERROR("Failed to get CVector2D.Y property"); + return false; + } + + return true; +} + #undef SET diff --git a/source/gui/tests/test_ParseString.h b/source/gui/tests/test_ParseString.h index 0586f6cd7c..2cf37dd54d 100644 --- a/source/gui/tests/test_ParseString.h +++ b/source/gui/tests/test_ParseString.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2019 Wildfire Games. +/* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -92,13 +92,13 @@ public: void test_pos() { TestLogger nolog; - CPos test; + CVector2D test; - TS_ASSERT(CGUI::ParseString(nullptr, CStrW(L"0.0 10.0"), test)); - TS_ASSERT_EQUALS(CPos(0.0, 10.0), test); + TS_ASSERT(CGUI::ParseString(nullptr, CStrW(L"0.0 10.0"), test)); + TS_ASSERT_EQUALS(CVector2D(0.0, 10.0), test); - TS_ASSERT(!CGUI::ParseString(nullptr, CStrW(L"0"), test)); - TS_ASSERT(!CGUI::ParseString(nullptr, CStrW(L"0 10 20"), test)); - TS_ASSERT(!CGUI::ParseString(nullptr, CStrW(L"0,0 10,0"), test)); + TS_ASSERT(!CGUI::ParseString(nullptr, CStrW(L"0"), test)); + TS_ASSERT(!CGUI::ParseString(nullptr, CStrW(L"0 10 20"), test)); + TS_ASSERT(!CGUI::ParseString(nullptr, CStrW(L"0,0 10,0"), test)); } }; diff --git a/source/maths/Vector2D.cpp b/source/maths/Vector2D.cpp new file mode 100644 index 0000000000..34af3a9a51 --- /dev/null +++ b/source/maths/Vector2D.cpp @@ -0,0 +1,58 @@ +/* Copyright (C) 2021 Wildfire Games. + * This file is part of 0 A.D. + * + * 0 A.D. is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * 0 A.D. is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with 0 A.D. If not, see . + */ + +#include "precompiled.h" + +#include "Vector2D.h" + +#include "maths/Size2D.h" + +CVector2D::CVector2D(const CSize2D& size) : X(size.Width), Y(size.Height) +{ +} + +bool CVector2D::operator==(const CVector2D& v) const +{ + return X == v.X && Y == v.Y; +} + +bool CVector2D::operator!=(const CVector2D& v) const +{ + return !(*this == v); +} + +CVector2D CVector2D::operator+(const CSize2D& size) const +{ + return CVector2D(X + size.Width, Y + size.Height); +} + +CVector2D CVector2D::operator-(const CSize2D& size) const +{ + return CVector2D(X - size.Width, Y - size.Height); +} + +void CVector2D::operator+=(const CSize2D& size) +{ + X += size.Width; + Y += size.Height; +} + +void CVector2D::operator-=(const CSize2D& size) +{ + X -= size.Width; + Y -= size.Height; +} diff --git a/source/maths/Vector2D.h b/source/maths/Vector2D.h index d6b907052a..07b15bfe30 100644 --- a/source/maths/Vector2D.h +++ b/source/maths/Vector2D.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2011 Wildfire Games. +/* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -15,24 +15,23 @@ * along with 0 A.D. If not, see . */ -/* - * Provides an interface for a vector in R2 and allows vector and - * scalar operations on it - */ - -#ifndef INCLUDED_MATHS_VECTOR2D -#define INCLUDED_MATHS_VECTOR2D - +#ifndef INCLUDED_VECTOR2D +#define INCLUDED_VECTOR2D #include -/////////////////////////////////////////////////////////////////////////////// -// CVector2D: +class CSize2D; + +/* + * Provides an interface for a vector in R2 and allows vector and + * scalar operations on it. + */ class CVector2D { public: CVector2D() : X(0.0f), Y(0.0f) {} CVector2D(float x, float y) : X(x), Y(y) {} + CVector2D(const CSize2D& size); operator float*() { @@ -44,6 +43,9 @@ public: return &X; } + bool operator==(const CVector2D& v) const; + bool operator!=(const CVector2D& v) const; + CVector2D operator-() const { return CVector2D(-X, -Y); @@ -153,10 +155,14 @@ public: Y = newY; } + CVector2D operator+(const CSize2D& size) const; + CVector2D operator-(const CSize2D& size) const; + + void operator+=(const CSize2D& size); + void operator-=(const CSize2D& size); + public: float X, Y; }; -////////////////////////////////////////////////////////////////////////////////// - -#endif +#endif // INCLUDED_VECTOR2D diff --git a/source/ps/Shapes.cpp b/source/ps/Shapes.cpp index 627eb08e26..547e48bfd1 100644 --- a/source/ps/Shapes.cpp +++ b/source/ps/Shapes.cpp @@ -20,6 +20,7 @@ #include "Shapes.h" #include "maths/Size2D.h" +#include "maths/Vector2D.h" CRect::CRect() : left(0.f), top(0.f), right(0.f), bottom(0.f) @@ -31,8 +32,8 @@ CRect::CRect(const CRect& rect) : { } -CRect::CRect(const CPos &pos) : - left(pos.x), top(pos.y), right(pos.x), bottom(pos.y) +CRect::CRect(const CVector2D& pos) : + left(pos.X), top(pos.Y), right(pos.X), bottom(pos.Y) { } @@ -41,13 +42,13 @@ CRect::CRect(const CSize2D& size) : { } -CRect::CRect(const CPos& upperleft, const CPos& bottomright) : - left(upperleft.x), top(upperleft.y), right(bottomright.x), bottom(bottomright.y) +CRect::CRect(const CVector2D& upperleft, const CVector2D& bottomright) : + left(upperleft.X), top(upperleft.Y), right(bottomright.X), bottom(bottomright.Y) { } -CRect::CRect(const CPos& pos, const CSize2D& size) : - left(pos.x), top(pos.y), right(pos.x + size.Width), bottom(pos.y + size.Height) +CRect::CRect(const CVector2D& pos, const CSize2D& size) : + left(pos.X), top(pos.Y), right(pos.X + size.Width), bottom(pos.Y + size.Height) { } @@ -93,9 +94,9 @@ CRect CRect::operator+(const CRect& a) const return CRect(left + a.left, top + a.top, right + a.right, bottom + a.bottom); } -CRect CRect::operator+(const CPos& a) const +CRect CRect::operator+(const CVector2D& a) const { - return CRect(left + a.x, top + a.y, right + a.x, bottom + a.y); + return CRect(left + a.X, top + a.Y, right + a.X, bottom + a.Y); } CRect CRect::operator+(const CSize2D& a) const @@ -108,9 +109,9 @@ CRect CRect::operator-(const CRect& a) const return CRect(left - a.left, top - a.top, right - a.right, bottom - a.bottom); } -CRect CRect::operator-(const CPos& a) const +CRect CRect::operator-(const CVector2D& a) const { - return CRect(left - a.x, top - a.y, right - a.x, bottom - a.y); + return CRect(left - a.X, top - a.Y, right - a.X, bottom - a.Y); } CRect CRect::operator-(const CSize2D& a) const @@ -126,12 +127,12 @@ void CRect::operator+=(const CRect& a) bottom += a.bottom; } -void CRect::operator+=(const CPos& a) +void CRect::operator+=(const CVector2D& a) { - left += a.x; - top += a.y; - right += a.x; - bottom += a.y; + left += a.X; + top += a.Y; + right += a.X; + bottom += a.Y; } void CRect::operator+=(const CSize2D& a) @@ -150,12 +151,12 @@ void CRect::operator-=(const CRect& a) bottom -= a.bottom; } -void CRect::operator-=(const CPos& a) +void CRect::operator-=(const CVector2D& a) { - left -= a.x; - top -= a.y; - right -= a.x; - bottom -= a.y; + left -= a.X; + top -= a.Y; + right -= a.X; + bottom -= a.Y; } void CRect::operator-=(const CSize2D& a) @@ -181,129 +182,40 @@ CSize2D CRect::GetSize() const return CSize2D(right - left, bottom - top); } -CPos CRect::TopLeft() const +CVector2D CRect::TopLeft() const { - return CPos(left, top); + return CVector2D(left, top); } -CPos CRect::TopRight() const +CVector2D CRect::TopRight() const { - return CPos(right, top); + return CVector2D(right, top); } -CPos CRect::BottomLeft() const +CVector2D CRect::BottomLeft() const { - return CPos(left, bottom); + return CVector2D(left, bottom); } -CPos CRect::BottomRight() const +CVector2D CRect::BottomRight() const { - return CPos(right, bottom); + return CVector2D(right, bottom); } -CPos CRect::CenterPoint() const +CVector2D CRect::CenterPoint() const { - return CPos((left + right) / 2.f, (top + bottom) / 2.f); + return CVector2D((left + right) / 2.f, (top + bottom) / 2.f); } -bool CRect::PointInside(const CPos &point) const +bool CRect::PointInside(const CVector2D& point) const { - return (point.x >= left && - point.x <= right && - point.y >= top && - point.y <= bottom); + return (point.X >= left && + point.X <= right && + point.Y >= top && + point.Y <= bottom); } CRect CRect::Scale(float x, float y) const { return CRect(left * x, top * y, right * x, bottom * y); } - -/*************************************************************************/ - -CPos::CPos() : x(0.f), y(0.f) -{ -} - -CPos::CPos(const CPos& pos) : x(pos.x), y(pos.y) -{ -} - -CPos::CPos(const CSize2D& s) : x(s.Width), y(s.Height) -{ -} - -CPos::CPos(const float px, const float py) : x(px), y(py) -{ -} - -CPos& CPos::operator=(const CPos& a) -{ - x = a.x; - y = a.y; - return *this; -} - -bool CPos::operator==(const CPos &a) const -{ - return x == a.x && y == a.y; -} - -bool CPos::operator!=(const CPos& a) const -{ - return !(*this == a); -} - -CPos CPos::operator-() const -{ - return CPos(-x, -y); -} - -CPos CPos::operator+() const -{ - return *this; -} - -CPos CPos::operator+(const CPos& a) const -{ - return CPos(x + a.x, y + a.y); -} - -CPos CPos::operator+(const CSize2D& a) const -{ - return CPos(x + a.Width, y + a.Height); -} - -CPos CPos::operator-(const CPos& a) const -{ - return CPos(x - a.x, y - a.y); -} - -CPos CPos::operator-(const CSize2D& a) const -{ - return CPos(x - a.Width, y - a.Height); -} - -void CPos::operator+=(const CPos& a) -{ - x += a.x; - y += a.y; -} - -void CPos::operator+=(const CSize2D& a) -{ - x += a.Width; - y += a.Height; -} - -void CPos::operator-=(const CPos& a) -{ - x -= a.x; - y -= a.y; -} - -void CPos::operator-=(const CSize2D& a) -{ - x -= a.Width; - y -= a.Height; -} diff --git a/source/ps/Shapes.h b/source/ps/Shapes.h index 8ddbb8001e..54aac8148f 100644 --- a/source/ps/Shapes.h +++ b/source/ps/Shapes.h @@ -18,8 +18,8 @@ #ifndef INCLUDED_SHAPES #define INCLUDED_SHAPES -class CPos; class CSize2D; +class CVector2D; /** @@ -31,10 +31,10 @@ class CRect { public: CRect(); - CRect(const CPos &pos); - CRect(const CSize2D &size); - CRect(const CPos &upperleft, const CPos &bottomright); - CRect(const CPos &pos, const CSize2D &size); + CRect(const CVector2D& pos); + CRect(const CSize2D& size); + CRect(const CVector2D& upperleft, const CVector2D& bottomright); + CRect(const CVector2D& pos, const CSize2D& size); CRect(const float l, const float t, const float r, const float b); CRect(const CRect&); @@ -45,17 +45,17 @@ public: CRect operator+() const; CRect operator+(const CRect& a) const; - CRect operator+(const CPos& a) const; + CRect operator+(const CVector2D& a) const; CRect operator+(const CSize2D& a) const; CRect operator-(const CRect& a) const; - CRect operator-(const CPos& a) const; + CRect operator-(const CVector2D& a) const; CRect operator-(const CSize2D& a) const; void operator+=(const CRect& a); - void operator+=(const CPos& a); + void operator+=(const CVector2D& a); void operator+=(const CSize2D& a); void operator-=(const CRect& a); - void operator-=(const CPos& a); + void operator-=(const CVector2D& a); void operator-=(const CSize2D& a); /** @@ -76,39 +76,39 @@ public: /** * Get Position equivalent to top/left corner */ - CPos TopLeft() const; + CVector2D TopLeft() const; /** * Get Position equivalent to top/right corner */ - CPos TopRight() const; + CVector2D TopRight() const; /** * Get Position equivalent to bottom/left corner */ - CPos BottomLeft() const; + CVector2D BottomLeft() const; /** * Get Position equivalent to bottom/right corner */ - CPos BottomRight() const; + CVector2D BottomRight() const; /** * Get Position equivalent to the center of the rectangle */ - CPos CenterPoint() const; + CVector2D CenterPoint() const; /** * Evalutates if point is within the rectangle - * @param point CPos representing point + * @param point CVector2D representing point * @return true if inside. */ - bool PointInside(const CPos &point) const; + bool PointInside(const CVector2D &point) const; CRect Scale(float x, float y) const; /** - * Returning CPos representing each corner. + * Returning CVector2D representing each corner. */ public: @@ -118,40 +118,4 @@ public: float left, top, right, bottom; }; -/** - * Made to represent screen positions and delta values. - * @see CRect - * @see CSize2D - */ -class CPos -{ -public: - CPos(); - CPos(const CPos& pos); - CPos(const CSize2D &pos); - CPos(const float px, const float py); - - CPos& operator=(const CPos& a); - bool operator==(const CPos& a) const; - bool operator!=(const CPos& a) const; - CPos operator-() const; - CPos operator+() const; - - CPos operator+(const CPos& a) const; - CPos operator+(const CSize2D& a) const; - CPos operator-(const CPos& a) const; - CPos operator-(const CSize2D& a) const; - - void operator+=(const CPos& a); - void operator+=(const CSize2D& a); - void operator-=(const CPos& a); - void operator-=(const CSize2D& a); - -public: - /** - * Position - */ - float x, y; -}; - #endif // INCLUDED_SHAPES diff --git a/source/scriptinterface/ScriptConversions.cpp b/source/scriptinterface/ScriptConversions.cpp index 8a8e3bb099..3a2b3aed1b 100644 --- a/source/scriptinterface/ScriptConversions.cpp +++ b/source/scriptinterface/ScriptConversions.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2020 Wildfire Games. +/* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -307,37 +307,15 @@ JSVAL_VECTOR(std::vector) class IComponent; -template<> void ScriptInterface::ToJSVal >(const ScriptRequest& rq, JS::MutableHandleValue ret, const std::vector& val) +template<> void ScriptInterface::ToJSVal>(const ScriptRequest& rq, JS::MutableHandleValue ret, const std::vector& val) { ToJSVal_vector(rq, ret, val); } -template<> bool ScriptInterface::FromJSVal >(const ScriptRequest& rq, JS::HandleValue v, std::vector& out) +template<> bool ScriptInterface::FromJSVal>(const ScriptRequest& rq, JS::HandleValue v, std::vector& out) { return FromJSVal_vector(rq, v, out); } -template<> void ScriptInterface::ToJSVal(const ScriptRequest& rq, JS::MutableHandleValue ret, const CVector2D& val) -{ - std::vector vec = {val.X, val.Y}; - ToJSVal_vector(rq, ret, vec); -} - -template<> bool ScriptInterface::FromJSVal(const ScriptRequest& rq, JS::HandleValue v, CVector2D& out) -{ - std::vector vec; - - if (!FromJSVal_vector(rq, v, vec)) - return false; - - if (vec.size() != 2) - return false; - - out.X = vec[0]; - out.Y = vec[1]; - - return true; -} - #undef FAIL #undef WARN_IF_NOT