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.
This commit is contained in:
vladislavbelov 2021-03-28 21:55:13 +00:00
parent 0e7fafebe1
commit 969112b9c8
39 changed files with 340 additions and 412 deletions

View file

@ -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);
}

View file

@ -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.

View file

@ -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,

View file

@ -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;
}

View file

@ -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

View file

@ -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();

View file

@ -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; }

View file

@ -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();

View file

@ -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

View file

@ -141,7 +141,7 @@ bool CGUI::ParseString<CSize2D>(const CGUI* UNUSED(pGUI), const CStrW& Value, CS
}
template <>
bool CGUI::ParseString<CPos>(const CGUI* UNUSED(pGUI), const CStrW& Value, CPos& Output)
bool CGUI::ParseString<CVector2D>(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<CPos>(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;
}

View file

@ -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<CPos>("_mousepos", pos, true);
usedobj->SetSetting<CVector2D>("_mousepos", pos, true);
}
else
{
@ -216,7 +216,7 @@ static i32 GetTooltipDelay(const CStr& style, CGUI& pGUI)
return tooltipobj->GetSetting<i32>("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();

View file

@ -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;
};

View file

@ -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();

View file

@ -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;
//@}
//--------------------------------------------------------

View file

@ -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));

View file

@ -23,6 +23,7 @@
#include "gui/SGUIMessage.h"
#include "gui/ObjectBases/IGUIObject.h"
#include "gui/SettingTypes/CGUIString.h"
#include "maths/Vector2D.h"
#include <math.h>
@ -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<EVAlign>("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()");

View file

@ -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 <vector>
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:
/**

View file

@ -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();

View file

@ -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));
}
}

View file

@ -77,7 +77,7 @@ protected:
CVector2D m_LeftBottom, m_RightTop;
std::vector<CPos> m_TextPositions;
std::vector<CVector2D> m_TextPositions;
bool m_EqualX, m_EqualY;

View file

@ -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<int>(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);
}

View file

@ -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 <string>
@ -96,7 +97,7 @@ protected:
/**
* Placement of text.
*/
CPos m_TextPos;
CVector2D m_TextPos;
// Is the dropdown opened?
bool m_Open;

View file

@ -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<SRow>::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;
}

View file

@ -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;

View file

@ -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();

View file

@ -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);

View file

@ -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;
}

View file

@ -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

View file

@ -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);
}

View file

@ -75,7 +75,7 @@ protected:
/**
* Placement of text. Ignored when scrollbars are active.
*/
CPos m_TextPos;
CVector2D m_TextPos;
// Settings
float m_BufferZone;

View file

@ -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:

View file

@ -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;
};

View file

@ -158,62 +158,6 @@ template<> void ScriptInterface::ToJSVal<CGUIColor>(const ScriptRequest& rq, JS:
*/
template<> bool ScriptInterface::FromJSVal<CGUIColor>(const ScriptRequest& rq, JS::HandleValue v, CGUIColor& out) = delete;
template<> void ScriptInterface::ToJSVal<CSize2D>(const ScriptRequest& rq, JS::MutableHandleValue ret, const CSize2D& val)
{
CreateObject(rq, ret, "width", val.Width, "height", val.Height);
}
template<> bool ScriptInterface::FromJSVal<CSize2D>(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<CPos>(const ScriptRequest& rq, JS::MutableHandleValue ret, const CPos& val)
{
CreateObject(rq, ret, "x", val.x, "y", val.y);
}
template<> bool ScriptInterface::FromJSVal<CPos>(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<CRect>(const ScriptRequest& rq, JS::MutableHandleValue ret, const CRect& val)
{
CreateObject(
@ -357,4 +301,60 @@ template<> bool ScriptInterface::FromJSVal<CGUISpriteInstance>(const ScriptReque
return true;
}
template<> void ScriptInterface::ToJSVal<CSize2D>(const ScriptRequest& rq, JS::MutableHandleValue ret, const CSize2D& val)
{
CreateObject(rq, ret, "width", val.Width, "height", val.Height);
}
template<> bool ScriptInterface::FromJSVal<CSize2D>(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<CVector2D>(const ScriptRequest& rq, JS::MutableHandleValue ret, const CVector2D& val)
{
CreateObject(rq, ret, "x", val.X, "y", val.Y);
}
template<> bool ScriptInterface::FromJSVal<CVector2D>(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

View file

@ -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<CPos>(nullptr, CStrW(L"0.0 10.0"), test));
TS_ASSERT_EQUALS(CPos(0.0, 10.0), test);
TS_ASSERT(CGUI::ParseString<CVector2D>(nullptr, CStrW(L"0.0 10.0"), test));
TS_ASSERT_EQUALS(CVector2D(0.0, 10.0), test);
TS_ASSERT(!CGUI::ParseString<CPos>(nullptr, CStrW(L"0"), test));
TS_ASSERT(!CGUI::ParseString<CPos>(nullptr, CStrW(L"0 10 20"), test));
TS_ASSERT(!CGUI::ParseString<CPos>(nullptr, CStrW(L"0,0 10,0"), test));
TS_ASSERT(!CGUI::ParseString<CVector2D>(nullptr, CStrW(L"0"), test));
TS_ASSERT(!CGUI::ParseString<CVector2D>(nullptr, CStrW(L"0 10 20"), test));
TS_ASSERT(!CGUI::ParseString<CVector2D>(nullptr, CStrW(L"0,0 10,0"), test));
}
};

58
source/maths/Vector2D.cpp Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#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;
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
/*
* 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 <math.h>
///////////////////////////////////////////////////////////////////////////////
// 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

View file

@ -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;
}

View file

@ -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

View file

@ -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<std::string>)
class IComponent;
template<> void ScriptInterface::ToJSVal<std::vector<IComponent*> >(const ScriptRequest& rq, JS::MutableHandleValue ret, const std::vector<IComponent*>& val)
template<> void ScriptInterface::ToJSVal<std::vector<IComponent*>>(const ScriptRequest& rq, JS::MutableHandleValue ret, const std::vector<IComponent*>& val)
{
ToJSVal_vector(rq, ret, val);
}
template<> bool ScriptInterface::FromJSVal<std::vector<Entity> >(const ScriptRequest& rq, JS::HandleValue v, std::vector<Entity>& out)
template<> bool ScriptInterface::FromJSVal<std::vector<Entity>>(const ScriptRequest& rq, JS::HandleValue v, std::vector<Entity>& out)
{
return FromJSVal_vector(rq, v, out);
}
template<> void ScriptInterface::ToJSVal<CVector2D>(const ScriptRequest& rq, JS::MutableHandleValue ret, const CVector2D& val)
{
std::vector<float> vec = {val.X, val.Y};
ToJSVal_vector(rq, ret, vec);
}
template<> bool ScriptInterface::FromJSVal<CVector2D>(const ScriptRequest& rq, JS::HandleValue v, CVector2D& out)
{
std::vector<float> 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