2021-01-17 07:44:14 -08:00
/* Copyright (C) 2021 Wildfire Games.
2009-04-18 10:00:33 -07:00
* 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/>.
*/
2004-06-03 11:38:14 -07:00
# include "precompiled.h"
2015-08-21 10:08:41 -07:00
2019-09-30 01:19:56 -07:00
# include "IGUIObject.h"
2019-09-22 16:28:25 -07:00
2019-09-18 13:51:45 -07:00
# include "gui/CGUI.h"
2019-08-29 02:07:29 -07:00
# include "gui/CGUISetting.h"
2020-11-21 09:49:06 -08:00
# include "gui/Scripting/JSInterface_GUIProxy.h"
2020-05-10 07:01:00 -07:00
# include "js/Conversions.h"
2009-09-27 08:04:46 -07:00
# include "ps/CLogger.h"
2019-09-22 16:28:25 -07:00
# include "ps/GameSetup/Config.h"
2016-06-21 03:33:11 -07:00
# include "ps/Profile.h"
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
# include "scriptinterface/ScriptContext.h"
2020-11-30 01:03:20 -08:00
# include "scriptinterface/ScriptExtraHeaders.h"
2015-08-21 10:08:41 -07:00
# include "scriptinterface/ScriptInterface.h"
2019-08-22 15:34:12 -07:00
# include "soundmanager/ISoundManager.h"
2009-09-27 08:04:46 -07:00
2021-01-17 07:44:14 -08:00
# include <unordered_map>
2020-01-15 08:00:37 -08:00
const CStr IGUIObject : : EventNameMouseEnter = " MouseEnter " ;
const CStr IGUIObject : : EventNameMouseMove = " MouseMove " ;
const CStr IGUIObject : : EventNameMouseLeave = " MouseLeave " ;
2019-08-21 03:12:33 -07:00
IGUIObject : : IGUIObject ( CGUI & pGUI )
Change GUI Object Setting values to be members of the IGUIObject inheriting class, rename AddSetting to RegisterSetting.
Improves performance for Draw calls by 3-5% according to a shady
benchmark.
Improves memory layout, since the values are not on the heap anymore but
in the using class.
Reduces complexity of the implementation and increases type safety.
Allows specifying default values at setting value construction time,
refs D2242.
Inspired by Vladislav introducing members that cached GetSetting values
in c016a74309/D325, refs #4039, ee38f0db37/D763, refs 4225,
a1c4c23ce4/D474, D406, which were formerly proposed to be removed in
D2241.
Differential Revision: https://code.wildfiregames.com/D2313
Tested on: clang 8.0.1, gcc 9.1.0, Jenkins vs2015
Comments By: Vladislav
This was SVN commit r23005.
2019-09-27 05:49:59 -07:00
: m_pGUI ( pGUI ) ,
m_pParent ( ) ,
m_MouseHovering ( ) ,
m_LastClickTime ( ) ,
m_Enabled ( ) ,
m_Hidden ( ) ,
m_Size ( ) ,
m_Style ( ) ,
m_Hotkey ( ) ,
m_Z ( ) ,
m_Absolute ( ) ,
m_Ghost ( ) ,
m_AspectRatio ( ) ,
m_Tooltip ( ) ,
m_TooltipStyle ( )
{
RegisterSetting ( " enabled " , m_Enabled ) ;
RegisterSetting ( " hidden " , m_Hidden ) ;
RegisterSetting ( " size " , m_Size ) ;
RegisterSetting ( " style " , m_Style ) ;
RegisterSetting ( " hotkey " , m_Hotkey ) ;
RegisterSetting ( " z " , m_Z ) ;
RegisterSetting ( " absolute " , m_Absolute ) ;
RegisterSetting ( " ghost " , m_Ghost ) ;
RegisterSetting ( " aspectratio " , m_AspectRatio ) ;
RegisterSetting ( " tooltip " , m_Tooltip ) ;
RegisterSetting ( " tooltip_style " , m_TooltipStyle ) ;
2004-05-28 21:06:50 -07:00
// Setup important defaults
Change GUI Object Setting values to be members of the IGUIObject inheriting class, rename AddSetting to RegisterSetting.
Improves performance for Draw calls by 3-5% according to a shady
benchmark.
Improves memory layout, since the values are not on the heap anymore but
in the using class.
Reduces complexity of the implementation and increases type safety.
Allows specifying default values at setting value construction time,
refs D2242.
Inspired by Vladislav introducing members that cached GetSetting values
in c016a74309/D325, refs #4039, ee38f0db37/D763, refs 4225,
a1c4c23ce4/D474, D406, which were formerly proposed to be removed in
D2241.
Differential Revision: https://code.wildfiregames.com/D2313
Tested on: clang 8.0.1, gcc 9.1.0, Jenkins vs2015
Comments By: Vladislav
This was SVN commit r23005.
2019-09-27 05:49:59 -07:00
// TODO: Should be in the default style?
Move static GUI<>::SetSetting operating on IGUIObject to a member IGUIObject::SetSetting.
Remove PSERROR codes from SetSetting (let std::map throw an out_of_range
if a caller wants to Set a setting that doesn't exist without having
checked with SettingExists, equal to GetSetting from 92b6cdfeab).
That also simplifies std::function SetSettingWrap construct from
0a7d0ecdde to void IGUIObject::SettingChanged.
Don't trigger debug_warn or exceptions in GUITooltip::ShowTooltip if the
XML author specified wrong tooltip input, and dodge another
dynamic_cast.
Rename existing IGUIObject::SetSetting to
IGUIObject::SetSettingFromString and comment that it is purposed for
parsing XML files.
Remove SetSetting default value, so that authors are made aware
explicitly of the need to decide the function broadcasting a message,
refs d87057b1c0, 719f2d7967, ...
Change const bool& SkipMessage to const bool SendMessage, so that a
positive value relates to a positive action.
Clean AddSettings whitespace and integer types.
Differential Revision: https://code.wildfiregames.com/D2231
Tested on: gcc 9.1.0, clang 8.0.1, Jenkins
Comments By: Philip on IRC on 2010-07-24 on GUIUtil being ugly, in case
that one counts
This was SVN commit r22796.
2019-08-28 04:21:11 -07:00
SetSetting < bool > ( " hidden " , false , true ) ;
SetSetting < bool > ( " ghost " , false , true ) ;
SetSetting < bool > ( " enabled " , true , true ) ;
SetSetting < bool > ( " absolute " , true , true ) ;
2003-11-23 18:18:41 -08:00
}
IGUIObject : : ~ IGUIObject ( )
{
2020-12-31 06:27:02 -08:00
for ( const std : : pair < const CStr , IGUISetting * > & p : m_Settings )
2019-08-03 19:20:08 -07:00
delete p . second ;
2015-01-24 06:46:52 -08:00
2019-08-01 13:20:24 -07:00
if ( ! m_ScriptHandlers . empty ( ) )
2020-11-18 06:39:04 -08:00
JS_RemoveExtraGCRootsTracer ( m_pGUI . GetScriptInterface ( ) - > GetGeneralJSContext ( ) , Trace , this ) ;
2003-11-23 18:18:41 -08:00
2019-10-11 09:20:50 -07:00
// m_Children is deleted along all other GUI Objects in the CGUI destructor
2003-11-23 18:18:41 -08:00
}
2019-10-11 09:20:50 -07:00
void IGUIObject : : AddChild ( IGUIObject & pChild )
2003-11-23 18:18:41 -08:00
{
2019-10-11 09:20:50 -07:00
pChild . SetParent ( this ) ;
m_Children . push_back ( & pChild ) ;
2003-11-23 18:18:41 -08:00
}
2019-08-03 19:20:08 -07:00
template < typename T >
Change GUI Object Setting values to be members of the IGUIObject inheriting class, rename AddSetting to RegisterSetting.
Improves performance for Draw calls by 3-5% according to a shady
benchmark.
Improves memory layout, since the values are not on the heap anymore but
in the using class.
Reduces complexity of the implementation and increases type safety.
Allows specifying default values at setting value construction time,
refs D2242.
Inspired by Vladislav introducing members that cached GetSetting values
in c016a74309/D325, refs #4039, ee38f0db37/D763, refs 4225,
a1c4c23ce4/D474, D406, which were formerly proposed to be removed in
D2241.
Differential Revision: https://code.wildfiregames.com/D2313
Tested on: clang 8.0.1, gcc 9.1.0, Jenkins vs2015
Comments By: Vladislav
This was SVN commit r23005.
2019-09-27 05:49:59 -07:00
void IGUIObject : : RegisterSetting ( const CStr & Name , T & Value )
2003-11-23 18:18:41 -08:00
{
2019-08-03 19:20:08 -07:00
if ( SettingExists ( Name ) )
2019-09-22 15:49:20 -07:00
LOGERROR ( " The setting '%s' already exists on the object '%s'! " , Name . c_str ( ) , GetPresentableName ( ) . c_str ( ) ) ;
else
Change GUI Object Setting values to be members of the IGUIObject inheriting class, rename AddSetting to RegisterSetting.
Improves performance for Draw calls by 3-5% according to a shady
benchmark.
Improves memory layout, since the values are not on the heap anymore but
in the using class.
Reduces complexity of the implementation and increases type safety.
Allows specifying default values at setting value construction time,
refs D2242.
Inspired by Vladislav introducing members that cached GetSetting values
in c016a74309/D325, refs #4039, ee38f0db37/D763, refs 4225,
a1c4c23ce4/D474, D406, which were formerly proposed to be removed in
D2241.
Differential Revision: https://code.wildfiregames.com/D2313
Tested on: clang 8.0.1, gcc 9.1.0, Jenkins vs2015
Comments By: Vladislav
This was SVN commit r23005.
2019-09-27 05:49:59 -07:00
m_Settings . emplace ( Name , new CGUISetting < T > ( * this , Name , Value ) ) ;
2003-11-23 18:18:41 -08:00
}
2004-12-16 16:05:37 -08:00
2019-08-26 05:25:07 -07:00
bool IGUIObject : : SettingExists ( const CStr & Setting ) const
{
2019-09-22 16:28:25 -07:00
return m_Settings . find ( Setting ) ! = m_Settings . end ( ) ;
2019-08-26 05:25:07 -07:00
}
template < typename T >
T & IGUIObject : : GetSetting ( const CStr & Setting )
{
return static_cast < CGUISetting < T > * > ( m_Settings . at ( Setting ) ) - > m_pSetting ;
}
template < typename T >
const T & IGUIObject : : GetSetting ( const CStr & Setting ) const
{
return static_cast < CGUISetting < T > * > ( m_Settings . at ( Setting ) ) - > m_pSetting ;
}
Move static GUI<>::SetSetting operating on IGUIObject to a member IGUIObject::SetSetting.
Remove PSERROR codes from SetSetting (let std::map throw an out_of_range
if a caller wants to Set a setting that doesn't exist without having
checked with SettingExists, equal to GetSetting from 92b6cdfeab).
That also simplifies std::function SetSettingWrap construct from
0a7d0ecdde to void IGUIObject::SettingChanged.
Don't trigger debug_warn or exceptions in GUITooltip::ShowTooltip if the
XML author specified wrong tooltip input, and dodge another
dynamic_cast.
Rename existing IGUIObject::SetSetting to
IGUIObject::SetSettingFromString and comment that it is purposed for
parsing XML files.
Remove SetSetting default value, so that authors are made aware
explicitly of the need to decide the function broadcasting a message,
refs d87057b1c0, 719f2d7967, ...
Change const bool& SkipMessage to const bool SendMessage, so that a
positive value relates to a positive action.
Clean AddSettings whitespace and integer types.
Differential Revision: https://code.wildfiregames.com/D2231
Tested on: gcc 9.1.0, clang 8.0.1, Jenkins
Comments By: Philip on IRC on 2010-07-24 on GUIUtil being ugly, in case
that one counts
This was SVN commit r22796.
2019-08-28 04:21:11 -07:00
bool IGUIObject : : SetSettingFromString ( const CStr & Setting , const CStrW & Value , const bool SendMessage )
{
2019-09-16 09:13:25 -07:00
const std : : map < CStr , IGUISetting * > : : iterator it = m_Settings . find ( Setting ) ;
if ( it = = m_Settings . end ( ) )
{
LOGERROR ( " GUI object '%s' has no property called '%s', can't set parse and set value '%s' " , GetPresentableName ( ) . c_str ( ) , Setting . c_str ( ) , Value . ToUTF8 ( ) . c_str ( ) ) ;
return false ;
}
return it - > second - > FromString ( Value , SendMessage ) ;
Move static GUI<>::SetSetting operating on IGUIObject to a member IGUIObject::SetSetting.
Remove PSERROR codes from SetSetting (let std::map throw an out_of_range
if a caller wants to Set a setting that doesn't exist without having
checked with SettingExists, equal to GetSetting from 92b6cdfeab).
That also simplifies std::function SetSettingWrap construct from
0a7d0ecdde to void IGUIObject::SettingChanged.
Don't trigger debug_warn or exceptions in GUITooltip::ShowTooltip if the
XML author specified wrong tooltip input, and dodge another
dynamic_cast.
Rename existing IGUIObject::SetSetting to
IGUIObject::SetSettingFromString and comment that it is purposed for
parsing XML files.
Remove SetSetting default value, so that authors are made aware
explicitly of the need to decide the function broadcasting a message,
refs d87057b1c0, 719f2d7967, ...
Change const bool& SkipMessage to const bool SendMessage, so that a
positive value relates to a positive action.
Clean AddSettings whitespace and integer types.
Differential Revision: https://code.wildfiregames.com/D2231
Tested on: gcc 9.1.0, clang 8.0.1, Jenkins
Comments By: Philip on IRC on 2010-07-24 on GUIUtil being ugly, in case
that one counts
This was SVN commit r22796.
2019-08-28 04:21:11 -07:00
}
template < typename T >
void IGUIObject : : SetSetting ( const CStr & Setting , T & Value , const bool SendMessage )
{
2019-09-04 08:29:36 -07:00
PreSettingChange ( Setting ) ;
Change GUI Object Setting values to be members of the IGUIObject inheriting class, rename AddSetting to RegisterSetting.
Improves performance for Draw calls by 3-5% according to a shady
benchmark.
Improves memory layout, since the values are not on the heap anymore but
in the using class.
Reduces complexity of the implementation and increases type safety.
Allows specifying default values at setting value construction time,
refs D2242.
Inspired by Vladislav introducing members that cached GetSetting values
in c016a74309/D325, refs #4039, ee38f0db37/D763, refs 4225,
a1c4c23ce4/D474, D406, which were formerly proposed to be removed in
D2241.
Differential Revision: https://code.wildfiregames.com/D2313
Tested on: clang 8.0.1, gcc 9.1.0, Jenkins vs2015
Comments By: Vladislav
This was SVN commit r23005.
2019-09-27 05:49:59 -07:00
static_cast < CGUISetting < T > * > ( m_Settings . at ( Setting ) ) - > m_pSetting = std : : move ( Value ) ;
Move static GUI<>::SetSetting operating on IGUIObject to a member IGUIObject::SetSetting.
Remove PSERROR codes from SetSetting (let std::map throw an out_of_range
if a caller wants to Set a setting that doesn't exist without having
checked with SettingExists, equal to GetSetting from 92b6cdfeab).
That also simplifies std::function SetSettingWrap construct from
0a7d0ecdde to void IGUIObject::SettingChanged.
Don't trigger debug_warn or exceptions in GUITooltip::ShowTooltip if the
XML author specified wrong tooltip input, and dodge another
dynamic_cast.
Rename existing IGUIObject::SetSetting to
IGUIObject::SetSettingFromString and comment that it is purposed for
parsing XML files.
Remove SetSetting default value, so that authors are made aware
explicitly of the need to decide the function broadcasting a message,
refs d87057b1c0, 719f2d7967, ...
Change const bool& SkipMessage to const bool SendMessage, so that a
positive value relates to a positive action.
Clean AddSettings whitespace and integer types.
Differential Revision: https://code.wildfiregames.com/D2231
Tested on: gcc 9.1.0, clang 8.0.1, Jenkins
Comments By: Philip on IRC on 2010-07-24 on GUIUtil being ugly, in case
that one counts
This was SVN commit r22796.
2019-08-28 04:21:11 -07:00
SettingChanged ( Setting , SendMessage ) ;
}
template < typename T >
void IGUIObject : : SetSetting ( const CStr & Setting , const T & Value , const bool SendMessage )
{
2019-09-04 08:29:36 -07:00
PreSettingChange ( Setting ) ;
Change GUI Object Setting values to be members of the IGUIObject inheriting class, rename AddSetting to RegisterSetting.
Improves performance for Draw calls by 3-5% according to a shady
benchmark.
Improves memory layout, since the values are not on the heap anymore but
in the using class.
Reduces complexity of the implementation and increases type safety.
Allows specifying default values at setting value construction time,
refs D2242.
Inspired by Vladislav introducing members that cached GetSetting values
in c016a74309/D325, refs #4039, ee38f0db37/D763, refs 4225,
a1c4c23ce4/D474, D406, which were formerly proposed to be removed in
D2241.
Differential Revision: https://code.wildfiregames.com/D2313
Tested on: clang 8.0.1, gcc 9.1.0, Jenkins vs2015
Comments By: Vladislav
This was SVN commit r23005.
2019-09-27 05:49:59 -07:00
static_cast < CGUISetting < T > * > ( m_Settings . at ( Setting ) ) - > m_pSetting = Value ;
Move static GUI<>::SetSetting operating on IGUIObject to a member IGUIObject::SetSetting.
Remove PSERROR codes from SetSetting (let std::map throw an out_of_range
if a caller wants to Set a setting that doesn't exist without having
checked with SettingExists, equal to GetSetting from 92b6cdfeab).
That also simplifies std::function SetSettingWrap construct from
0a7d0ecdde to void IGUIObject::SettingChanged.
Don't trigger debug_warn or exceptions in GUITooltip::ShowTooltip if the
XML author specified wrong tooltip input, and dodge another
dynamic_cast.
Rename existing IGUIObject::SetSetting to
IGUIObject::SetSettingFromString and comment that it is purposed for
parsing XML files.
Remove SetSetting default value, so that authors are made aware
explicitly of the need to decide the function broadcasting a message,
refs d87057b1c0, 719f2d7967, ...
Change const bool& SkipMessage to const bool SendMessage, so that a
positive value relates to a positive action.
Clean AddSettings whitespace and integer types.
Differential Revision: https://code.wildfiregames.com/D2231
Tested on: gcc 9.1.0, clang 8.0.1, Jenkins
Comments By: Philip on IRC on 2010-07-24 on GUIUtil being ugly, in case
that one counts
This was SVN commit r22796.
2019-08-28 04:21:11 -07:00
SettingChanged ( Setting , SendMessage ) ;
}
2019-09-04 08:29:36 -07:00
void IGUIObject : : PreSettingChange ( const CStr & Setting )
{
if ( Setting = = " hotkey " )
m_pGUI . UnsetObjectHotkey ( this , GetSetting < CStr > ( Setting ) ) ;
}
Move static GUI<>::SetSetting operating on IGUIObject to a member IGUIObject::SetSetting.
Remove PSERROR codes from SetSetting (let std::map throw an out_of_range
if a caller wants to Set a setting that doesn't exist without having
checked with SettingExists, equal to GetSetting from 92b6cdfeab).
That also simplifies std::function SetSettingWrap construct from
0a7d0ecdde to void IGUIObject::SettingChanged.
Don't trigger debug_warn or exceptions in GUITooltip::ShowTooltip if the
XML author specified wrong tooltip input, and dodge another
dynamic_cast.
Rename existing IGUIObject::SetSetting to
IGUIObject::SetSettingFromString and comment that it is purposed for
parsing XML files.
Remove SetSetting default value, so that authors are made aware
explicitly of the need to decide the function broadcasting a message,
refs d87057b1c0, 719f2d7967, ...
Change const bool& SkipMessage to const bool SendMessage, so that a
positive value relates to a positive action.
Clean AddSettings whitespace and integer types.
Differential Revision: https://code.wildfiregames.com/D2231
Tested on: gcc 9.1.0, clang 8.0.1, Jenkins
Comments By: Philip on IRC on 2010-07-24 on GUIUtil being ugly, in case
that one counts
This was SVN commit r22796.
2019-08-28 04:21:11 -07:00
void IGUIObject : : SettingChanged ( const CStr & Setting , const bool SendMessage )
{
if ( Setting = = " size " )
{
// If setting was "size", we need to re-cache itself and all children
RecurseObject ( nullptr , & IGUIObject : : UpdateCachedSize ) ;
}
else if ( Setting = = " hidden " )
{
// Hiding an object requires us to reset it and all children
Change GUI Object Setting values to be members of the IGUIObject inheriting class, rename AddSetting to RegisterSetting.
Improves performance for Draw calls by 3-5% according to a shady
benchmark.
Improves memory layout, since the values are not on the heap anymore but
in the using class.
Reduces complexity of the implementation and increases type safety.
Allows specifying default values at setting value construction time,
refs D2242.
Inspired by Vladislav introducing members that cached GetSetting values
in c016a74309/D325, refs #4039, ee38f0db37/D763, refs 4225,
a1c4c23ce4/D474, D406, which were formerly proposed to be removed in
D2241.
Differential Revision: https://code.wildfiregames.com/D2313
Tested on: clang 8.0.1, gcc 9.1.0, Jenkins vs2015
Comments By: Vladislav
This was SVN commit r23005.
2019-09-27 05:49:59 -07:00
if ( m_Hidden )
Move static GUI<>::SetSetting operating on IGUIObject to a member IGUIObject::SetSetting.
Remove PSERROR codes from SetSetting (let std::map throw an out_of_range
if a caller wants to Set a setting that doesn't exist without having
checked with SettingExists, equal to GetSetting from 92b6cdfeab).
That also simplifies std::function SetSettingWrap construct from
0a7d0ecdde to void IGUIObject::SettingChanged.
Don't trigger debug_warn or exceptions in GUITooltip::ShowTooltip if the
XML author specified wrong tooltip input, and dodge another
dynamic_cast.
Rename existing IGUIObject::SetSetting to
IGUIObject::SetSettingFromString and comment that it is purposed for
parsing XML files.
Remove SetSetting default value, so that authors are made aware
explicitly of the need to decide the function broadcasting a message,
refs d87057b1c0, 719f2d7967, ...
Change const bool& SkipMessage to const bool SendMessage, so that a
positive value relates to a positive action.
Clean AddSettings whitespace and integer types.
Differential Revision: https://code.wildfiregames.com/D2231
Tested on: gcc 9.1.0, clang 8.0.1, Jenkins
Comments By: Philip on IRC on 2010-07-24 on GUIUtil being ugly, in case
that one counts
This was SVN commit r22796.
2019-08-28 04:21:11 -07:00
RecurseObject ( nullptr , & IGUIObject : : ResetStates ) ;
}
2019-09-04 08:29:36 -07:00
else if ( Setting = = " hotkey " )
m_pGUI . SetObjectHotkey ( this , GetSetting < CStr > ( Setting ) ) ;
2020-11-26 18:30:42 -08:00
else if ( Setting = = " style " )
m_pGUI . SetObjectStyle ( this , GetSetting < CStr > ( Setting ) ) ;
Move static GUI<>::SetSetting operating on IGUIObject to a member IGUIObject::SetSetting.
Remove PSERROR codes from SetSetting (let std::map throw an out_of_range
if a caller wants to Set a setting that doesn't exist without having
checked with SettingExists, equal to GetSetting from 92b6cdfeab).
That also simplifies std::function SetSettingWrap construct from
0a7d0ecdde to void IGUIObject::SettingChanged.
Don't trigger debug_warn or exceptions in GUITooltip::ShowTooltip if the
XML author specified wrong tooltip input, and dodge another
dynamic_cast.
Rename existing IGUIObject::SetSetting to
IGUIObject::SetSettingFromString and comment that it is purposed for
parsing XML files.
Remove SetSetting default value, so that authors are made aware
explicitly of the need to decide the function broadcasting a message,
refs d87057b1c0, 719f2d7967, ...
Change const bool& SkipMessage to const bool SendMessage, so that a
positive value relates to a positive action.
Clean AddSettings whitespace and integer types.
Differential Revision: https://code.wildfiregames.com/D2231
Tested on: gcc 9.1.0, clang 8.0.1, Jenkins
Comments By: Philip on IRC on 2010-07-24 on GUIUtil being ugly, in case
that one counts
This was SVN commit r22796.
2019-08-28 04:21:11 -07:00
if ( SendMessage )
{
SGUIMessage msg ( GUIM_SETTINGS_UPDATED , Setting ) ;
HandleMessage ( msg ) ;
}
}
2019-08-22 16:51:10 -07:00
bool IGUIObject : : IsMouseOver ( ) const
2003-11-23 18:18:41 -08:00
{
2019-08-21 03:12:33 -07:00
return m_CachedActualSize . PointInside ( m_pGUI . GetMousePos ( ) ) ;
2003-11-23 18:18:41 -08:00
}
2010-10-29 21:02:42 -07:00
bool IGUIObject : : MouseOverIcon ( )
{
return false ;
}
2015-08-21 10:08:41 -07:00
void IGUIObject : : UpdateMouseOver ( IGUIObject * const & pMouseOver )
2003-11-23 18:18:41 -08:00
{
if ( pMouseOver = = this )
{
if ( ! m_MouseHovering )
2020-01-15 08:00:37 -08:00
SendMouseEvent ( GUIM_MOUSE_ENTER , EventNameMouseEnter ) ;
2003-11-23 18:18:41 -08:00
m_MouseHovering = true ;
2020-01-15 08:00:37 -08:00
SendMouseEvent ( GUIM_MOUSE_OVER , EventNameMouseMove ) ;
2003-11-23 18:18:41 -08:00
}
2016-06-07 05:02:33 -07:00
else
2003-11-23 18:18:41 -08:00
{
if ( m_MouseHovering )
{
m_MouseHovering = false ;
2020-01-15 08:00:37 -08:00
SendMouseEvent ( GUIM_MOUSE_LEAVE , EventNameMouseLeave ) ;
2003-11-23 18:18:41 -08:00
}
}
}
2015-08-21 10:08:41 -07:00
void IGUIObject : : ChooseMouseOverAndClosest ( IGUIObject * & pObject )
2003-11-23 18:18:41 -08:00
{
2019-08-22 16:51:10 -07:00
if ( ! IsMouseOver ( ) )
2015-08-21 10:08:41 -07:00
return ;
// Check if we've got competition at all
2019-09-22 16:28:25 -07:00
if ( pObject = = nullptr )
2003-11-23 18:18:41 -08:00
{
2015-08-21 10:08:41 -07:00
pObject = this ;
return ;
}
2003-11-23 18:18:41 -08:00
2015-08-21 10:08:41 -07:00
// Or if it's closer
if ( GetBufferedZ ( ) > = pObject - > GetBufferedZ ( ) )
{
pObject = this ;
return ;
2003-11-23 18:18:41 -08:00
}
}
2015-08-21 10:08:41 -07:00
IGUIObject * IGUIObject : : GetParent ( ) const
2003-11-23 18:18:41 -08:00
{
// Important, we're not using GetParent() for these
// checks, that could screw it up
2019-09-22 16:28:25 -07:00
if ( m_pParent & & m_pParent - > m_pParent = = nullptr )
return nullptr ;
2003-11-23 18:18:41 -08:00
return m_pParent ;
}
2019-07-27 19:39:52 -07:00
void IGUIObject : : ResetStates ( )
{
// Notify the gui that we aren't hovered anymore
UpdateMouseOver ( nullptr ) ;
}
2003-11-24 09:13:37 -08:00
void IGUIObject : : UpdateCachedSize ( )
{
2003-11-24 18:47:12 -08:00
// If absolute="false" and the object has got a parent,
// use its cached size instead of the screen. Notice
// it must have just been cached for it to work.
Change GUI Object Setting values to be members of the IGUIObject inheriting class, rename AddSetting to RegisterSetting.
Improves performance for Draw calls by 3-5% according to a shady
benchmark.
Improves memory layout, since the values are not on the heap anymore but
in the using class.
Reduces complexity of the implementation and increases type safety.
Allows specifying default values at setting value construction time,
refs D2242.
Inspired by Vladislav introducing members that cached GetSetting values
in c016a74309/D325, refs #4039, ee38f0db37/D763, refs 4225,
a1c4c23ce4/D474, D406, which were formerly proposed to be removed in
D2241.
Differential Revision: https://code.wildfiregames.com/D2313
Tested on: clang 8.0.1, gcc 9.1.0, Jenkins vs2015
Comments By: Vladislav
This was SVN commit r23005.
2019-09-27 05:49:59 -07:00
if ( ! m_Absolute & & m_pParent & & ! IsRootObject ( ) )
2019-10-01 08:06:13 -07:00
m_CachedActualSize = m_Size . GetSize ( m_pParent - > m_CachedActualSize ) ;
2003-11-24 18:47:12 -08:00
else
2019-10-01 08:06:13 -07:00
m_CachedActualSize = m_Size . GetSize ( CRect ( 0.f , 0.f , g_xres / g_GuiScale , g_yres / g_GuiScale ) ) ;
2004-05-28 21:06:50 -07:00
2010-08-10 15:39:00 -07:00
// In a few cases, GUI objects have to resize to fill the screen
// but maintain a constant aspect ratio.
// Adjust the size to be the max possible, centered in the original size:
Change GUI Object Setting values to be members of the IGUIObject inheriting class, rename AddSetting to RegisterSetting.
Improves performance for Draw calls by 3-5% according to a shady
benchmark.
Improves memory layout, since the values are not on the heap anymore but
in the using class.
Reduces complexity of the implementation and increases type safety.
Allows specifying default values at setting value construction time,
refs D2242.
Inspired by Vladislav introducing members that cached GetSetting values
in c016a74309/D325, refs #4039, ee38f0db37/D763, refs 4225,
a1c4c23ce4/D474, D406, which were formerly proposed to be removed in
D2241.
Differential Revision: https://code.wildfiregames.com/D2313
Tested on: clang 8.0.1, gcc 9.1.0, Jenkins vs2015
Comments By: Vladislav
This was SVN commit r23005.
2019-09-27 05:49:59 -07:00
if ( m_AspectRatio )
2010-08-10 15:39:00 -07:00
{
Change GUI Object Setting values to be members of the IGUIObject inheriting class, rename AddSetting to RegisterSetting.
Improves performance for Draw calls by 3-5% according to a shady
benchmark.
Improves memory layout, since the values are not on the heap anymore but
in the using class.
Reduces complexity of the implementation and increases type safety.
Allows specifying default values at setting value construction time,
refs D2242.
Inspired by Vladislav introducing members that cached GetSetting values
in c016a74309/D325, refs #4039, ee38f0db37/D763, refs 4225,
a1c4c23ce4/D474, D406, which were formerly proposed to be removed in
D2241.
Differential Revision: https://code.wildfiregames.com/D2313
Tested on: clang 8.0.1, gcc 9.1.0, Jenkins vs2015
Comments By: Vladislav
This was SVN commit r23005.
2019-09-27 05:49:59 -07:00
if ( m_CachedActualSize . GetWidth ( ) > m_CachedActualSize . GetHeight ( ) * m_AspectRatio )
2010-08-10 15:39:00 -07:00
{
Change GUI Object Setting values to be members of the IGUIObject inheriting class, rename AddSetting to RegisterSetting.
Improves performance for Draw calls by 3-5% according to a shady
benchmark.
Improves memory layout, since the values are not on the heap anymore but
in the using class.
Reduces complexity of the implementation and increases type safety.
Allows specifying default values at setting value construction time,
refs D2242.
Inspired by Vladislav introducing members that cached GetSetting values
in c016a74309/D325, refs #4039, ee38f0db37/D763, refs 4225,
a1c4c23ce4/D474, D406, which were formerly proposed to be removed in
D2241.
Differential Revision: https://code.wildfiregames.com/D2313
Tested on: clang 8.0.1, gcc 9.1.0, Jenkins vs2015
Comments By: Vladislav
This was SVN commit r23005.
2019-09-27 05:49:59 -07:00
float delta = m_CachedActualSize . GetWidth ( ) - m_CachedActualSize . GetHeight ( ) * m_AspectRatio ;
2010-08-10 15:39:00 -07:00
m_CachedActualSize . left + = delta / 2.f ;
m_CachedActualSize . right - = delta / 2.f ;
}
else
{
Change GUI Object Setting values to be members of the IGUIObject inheriting class, rename AddSetting to RegisterSetting.
Improves performance for Draw calls by 3-5% according to a shady
benchmark.
Improves memory layout, since the values are not on the heap anymore but
in the using class.
Reduces complexity of the implementation and increases type safety.
Allows specifying default values at setting value construction time,
refs D2242.
Inspired by Vladislav introducing members that cached GetSetting values
in c016a74309/D325, refs #4039, ee38f0db37/D763, refs 4225,
a1c4c23ce4/D474, D406, which were formerly proposed to be removed in
D2241.
Differential Revision: https://code.wildfiregames.com/D2313
Tested on: clang 8.0.1, gcc 9.1.0, Jenkins vs2015
Comments By: Vladislav
This was SVN commit r23005.
2019-09-27 05:49:59 -07:00
float delta = m_CachedActualSize . GetHeight ( ) - m_CachedActualSize . GetWidth ( ) / m_AspectRatio ;
2010-08-10 15:39:00 -07:00
m_CachedActualSize . bottom - = delta / 2.f ;
m_CachedActualSize . top + = delta / 2.f ;
}
}
2003-11-24 09:13:37 -08:00
}
2020-11-26 18:30:42 -08:00
bool IGUIObject : : ApplyStyle ( const CStr & StyleName )
2003-12-26 22:26:03 -08:00
{
2019-08-27 09:03:24 -07:00
if ( ! m_pGUI . HasStyle ( StyleName ) )
2020-11-26 18:30:42 -08:00
{
LOGERROR ( " IGUIObject: Trying to use style '%s' that doesn't exist. " , StyleName . c_str ( ) ) ;
return false ;
}
2004-12-13 04:07:12 -08:00
2019-08-27 09:03:24 -07:00
// The default style may specify settings for any GUI object.
// Other styles are reported if they specify a Setting that does not exist,
// so that the XML author is informed and can correct the style.
2004-12-13 04:07:12 -08:00
2020-12-31 06:27:02 -08:00
for ( const std : : pair < const CStr , CStrW > & p : m_pGUI . GetStyle ( StyleName ) . m_SettingsDefaults )
2019-08-27 09:03:24 -07:00
{
if ( SettingExists ( p . first ) )
Move static GUI<>::SetSetting operating on IGUIObject to a member IGUIObject::SetSetting.
Remove PSERROR codes from SetSetting (let std::map throw an out_of_range
if a caller wants to Set a setting that doesn't exist without having
checked with SettingExists, equal to GetSetting from 92b6cdfeab).
That also simplifies std::function SetSettingWrap construct from
0a7d0ecdde to void IGUIObject::SettingChanged.
Don't trigger debug_warn or exceptions in GUITooltip::ShowTooltip if the
XML author specified wrong tooltip input, and dodge another
dynamic_cast.
Rename existing IGUIObject::SetSetting to
IGUIObject::SetSettingFromString and comment that it is purposed for
parsing XML files.
Remove SetSetting default value, so that authors are made aware
explicitly of the need to decide the function broadcasting a message,
refs d87057b1c0, 719f2d7967, ...
Change const bool& SkipMessage to const bool SendMessage, so that a
positive value relates to a positive action.
Clean AddSettings whitespace and integer types.
Differential Revision: https://code.wildfiregames.com/D2231
Tested on: gcc 9.1.0, clang 8.0.1, Jenkins
Comments By: Philip on IRC on 2010-07-24 on GUIUtil being ugly, in case
that one counts
This was SVN commit r22796.
2019-08-28 04:21:11 -07:00
SetSettingFromString ( p . first , p . second , true ) ;
2019-08-27 09:03:24 -07:00
else if ( StyleName ! = " default " )
LOGWARNING ( " GUI object has no setting \" %s \" , but the style \" %s \" defines it " , p . first , StyleName . c_str ( ) ) ;
2003-12-26 22:26:03 -08:00
}
2020-11-26 18:30:42 -08:00
return true ;
2003-12-26 22:26:03 -08:00
}
float IGUIObject : : GetBufferedZ ( ) const
{
Change GUI Object Setting values to be members of the IGUIObject inheriting class, rename AddSetting to RegisterSetting.
Improves performance for Draw calls by 3-5% according to a shady
benchmark.
Improves memory layout, since the values are not on the heap anymore but
in the using class.
Reduces complexity of the implementation and increases type safety.
Allows specifying default values at setting value construction time,
refs D2242.
Inspired by Vladislav introducing members that cached GetSetting values
in c016a74309/D325, refs #4039, ee38f0db37/D763, refs 4225,
a1c4c23ce4/D474, D406, which were formerly proposed to be removed in
D2241.
Differential Revision: https://code.wildfiregames.com/D2313
Tested on: clang 8.0.1, gcc 9.1.0, Jenkins vs2015
Comments By: Vladislav
This was SVN commit r23005.
2019-09-27 05:49:59 -07:00
if ( m_Absolute )
return m_Z ;
2004-05-28 21:06:50 -07:00
Change GUI Object Setting values to be members of the IGUIObject inheriting class, rename AddSetting to RegisterSetting.
Improves performance for Draw calls by 3-5% according to a shady
benchmark.
Improves memory layout, since the values are not on the heap anymore but
in the using class.
Reduces complexity of the implementation and increases type safety.
Allows specifying default values at setting value construction time,
refs D2242.
Inspired by Vladislav introducing members that cached GetSetting values
in c016a74309/D325, refs #4039, ee38f0db37/D763, refs 4225,
a1c4c23ce4/D474, D406, which were formerly proposed to be removed in
D2241.
Differential Revision: https://code.wildfiregames.com/D2313
Tested on: clang 8.0.1, gcc 9.1.0, Jenkins vs2015
Comments By: Vladislav
This was SVN commit r23005.
2019-09-27 05:49:59 -07:00
if ( GetParent ( ) )
return GetParent ( ) - > GetBufferedZ ( ) + m_Z ;
2019-08-23 07:43:10 -07:00
Change GUI Object Setting values to be members of the IGUIObject inheriting class, rename AddSetting to RegisterSetting.
Improves performance for Draw calls by 3-5% according to a shady
benchmark.
Improves memory layout, since the values are not on the heap anymore but
in the using class.
Reduces complexity of the implementation and increases type safety.
Allows specifying default values at setting value construction time,
refs D2242.
Inspired by Vladislav introducing members that cached GetSetting values
in c016a74309/D325, refs #4039, ee38f0db37/D763, refs 4225,
a1c4c23ce4/D474, D406, which were formerly proposed to be removed in
D2241.
Differential Revision: https://code.wildfiregames.com/D2313
Tested on: clang 8.0.1, gcc 9.1.0, Jenkins vs2015
Comments By: Vladislav
This was SVN commit r23005.
2019-09-27 05:49:59 -07:00
// In philosophy, a parentless object shouldn't be able to have a relative sizing,
// but we'll accept it so that absolute can be used as default without a complaint.
// Also, you could consider those objects children to the screen resolution.
return m_Z ;
2003-12-26 22:26:03 -08:00
}
2020-01-15 08:00:37 -08:00
void IGUIObject : : RegisterScriptHandler ( const CStr & eventName , const CStr & Code , CGUI & pGUI )
2004-07-08 08:23:47 -07:00
{
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
ScriptRequest rq ( pGUI . GetScriptInterface ( ) ) ;
2015-08-21 10:08:41 -07:00
2004-07-08 08:23:47 -07:00
const int paramCount = 1 ;
const char * paramNames [ paramCount ] = { " mouse " } ;
2004-07-11 11:18:54 -07:00
// Location to report errors from
2020-01-15 08:00:37 -08:00
CStr CodeName = GetName ( ) + " " + eventName ;
2004-07-11 11:18:54 -07:00
2004-12-21 10:41:58 -08:00
// Generate a unique name
2015-08-21 10:08:41 -07:00
static int x = 0 ;
2004-12-21 10:41:58 -08:00
char buf [ 64 ] ;
2020-01-15 08:00:37 -08:00
sprintf_s ( buf , ARRAY_SIZE ( buf ) , " __eventhandler%d (%s) " , x + + , eventName . c_str ( ) ) ;
2004-12-21 10:41:58 -08:00
2020-11-30 01:03:20 -08:00
// TODO: this is essentially the same code as ScriptInterface::LoadScript (with a tweak for the argument).
2020-11-13 05:18:22 -08:00
JS : : CompileOptions options ( rq . cx ) ;
2015-01-24 06:46:52 -08:00
options . setFileAndLine ( CodeName . c_str ( ) , 0 ) ;
2019-08-08 02:05:42 -07:00
options . setIsRunOnce ( false ) ;
2015-01-24 06:46:52 -08:00
2020-11-30 01:03:20 -08:00
JS : : SourceText < mozilla : : Utf8Unit > src ;
ENSURE ( src . init ( rq . cx , Code . c_str ( ) , Code . length ( ) , JS : : SourceOwnership : : Borrowed ) ) ;
JS : : RootedObjectVector emptyScopeChain ( rq . cx ) ;
JS : : RootedFunction func ( rq . cx , JS : : CompileFunction ( rq . cx , emptyScopeChain , options , buf , paramCount , paramNames , src ) ) ;
if ( func = = nullptr )
2016-09-02 09:34:02 -07:00
{
2020-01-15 08:00:37 -08:00
LOGERROR ( " RegisterScriptHandler: Failed to compile the script for %s " , eventName . c_str ( ) ) ;
2016-09-02 09:34:02 -07:00
return ;
}
2015-08-21 10:08:41 -07:00
2020-11-13 05:18:22 -08:00
JS : : RootedObject funcObj ( rq . cx , JS_GetFunctionObject ( func ) ) ;
2020-01-15 08:00:37 -08:00
SetScriptHandler ( eventName , funcObj ) ;
2005-03-30 14:33:10 -08:00
}
2004-12-21 10:41:58 -08:00
2020-01-15 08:00:37 -08:00
void IGUIObject : : SetScriptHandler ( const CStr & eventName , JS : : HandleObject Function )
2005-03-30 14:33:10 -08:00
{
2019-08-01 13:20:24 -07:00
if ( m_ScriptHandlers . empty ( ) )
2020-11-18 06:39:04 -08:00
JS_AddExtraGCRootsTracer ( m_pGUI . GetScriptInterface ( ) - > GetGeneralJSContext ( ) , Trace , this ) ;
2019-08-01 13:20:24 -07:00
2020-01-15 08:00:37 -08:00
m_ScriptHandlers [ eventName ] = JS : : Heap < JSObject * > ( Function ) ;
2020-11-14 13:54:17 -08:00
2021-01-17 07:44:14 -08:00
m_pGUI . m_EventObjects [ eventName ] . push_back ( this ) ;
2004-07-08 08:23:47 -07:00
}
2020-01-15 08:00:37 -08:00
void IGUIObject : : UnsetScriptHandler ( const CStr & eventName )
2019-10-28 04:35:04 -07:00
{
2020-01-15 08:00:37 -08:00
std : : map < CStr , JS : : Heap < JSObject * > > : : iterator it = m_ScriptHandlers . find ( eventName ) ;
2019-10-28 04:35:04 -07:00
if ( it = = m_ScriptHandlers . end ( ) )
return ;
m_ScriptHandlers . erase ( it ) ;
if ( m_ScriptHandlers . empty ( ) )
2020-11-18 06:39:04 -08:00
JS_RemoveExtraGCRootsTracer ( m_pGUI . GetScriptInterface ( ) - > GetGeneralJSContext ( ) , Trace , this ) ;
2021-01-17 07:44:14 -08:00
std : : unordered_map < CStr , std : : vector < IGUIObject * > > : : iterator it2 = m_pGUI . m_EventObjects . find ( eventName ) ;
if ( it2 = = m_pGUI . m_EventObjects . end ( ) )
return ;
std : : vector < IGUIObject * > & handlers = it2 - > second ;
handlers . erase ( std : : remove ( handlers . begin ( ) , handlers . end ( ) , this ) , handlers . end ( ) ) ;
if ( handlers . empty ( ) )
m_pGUI . m_EventObjects . erase ( it2 ) ;
2019-10-28 04:35:04 -07:00
}
2020-01-15 08:00:37 -08:00
InReaction IGUIObject : : SendEvent ( EGUIMessageType type , const CStr & eventName )
2011-04-28 13:42:11 -07:00
{
2011-11-09 05:09:01 -08:00
PROFILE2_EVENT ( " gui event " ) ;
2020-01-15 08:00:37 -08:00
PROFILE2_ATTR ( " type: %s " , eventName . c_str ( ) ) ;
2011-11-09 05:09:01 -08:00
PROFILE2_ATTR ( " object: %s " , m_Name . c_str ( ) ) ;
2011-04-28 13:42:11 -07:00
SGUIMessage msg ( type ) ;
HandleMessage ( msg ) ;
2020-01-15 08:00:37 -08:00
ScriptEvent ( eventName ) ;
2011-04-28 13:42:11 -07:00
2020-01-15 08:00:37 -08:00
return msg . skipped ? IN_PASS : IN_HANDLED ;
2011-04-28 13:42:11 -07:00
}
2020-01-15 08:00:37 -08:00
InReaction IGUIObject : : SendMouseEvent ( EGUIMessageType type , const CStr & eventName )
2004-07-08 08:23:47 -07:00
{
2020-01-15 08:00:37 -08:00
PROFILE2_EVENT ( " gui mouse event " ) ;
PROFILE2_ATTR ( " type: %s " , eventName . c_str ( ) ) ;
PROFILE2_ATTR ( " object: %s " , m_Name . c_str ( ) ) ;
SGUIMessage msg ( type ) ;
HandleMessage ( msg ) ;
2004-07-08 08:23:47 -07:00
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
ScriptRequest rq ( m_pGUI . GetScriptInterface ( ) ) ;
2014-01-04 02:14:53 -08:00
2004-07-08 08:23:47 -07:00
// Set up the 'mouse' parameter
2020-11-13 05:18:22 -08:00
JS : : RootedValue mouse ( rq . cx ) ;
2019-07-22 12:35:14 -07:00
2019-08-21 03:12:33 -07:00
const CPos & mousePos = m_pGUI . GetMousePos ( ) ;
2019-08-10 05:51:27 -07:00
2019-09-12 17:56:51 -07:00
ScriptInterface : : CreateObject (
2020-11-13 05:18:22 -08:00
rq ,
2019-07-22 12:35:14 -07:00
& mouse ,
2019-08-10 05:51:27 -07:00
" x " , mousePos . x ,
" y " , mousePos . y ,
2019-08-21 03:12:33 -07:00
" buttons " , m_pGUI . GetMouseButtons ( ) ) ;
2020-11-30 01:03:20 -08:00
JS : : RootedValueVector paramData ( rq . cx ) ;
2020-12-15 01:03:44 -08:00
ignore_result ( paramData . append ( mouse ) ) ;
2020-01-15 08:00:37 -08:00
ScriptEvent ( eventName , paramData ) ;
return msg . skipped ? IN_PASS : IN_HANDLED ;
}
void IGUIObject : : ScriptEvent ( const CStr & eventName )
2020-05-10 07:01:00 -07:00
{
ScriptEventWithReturn ( eventName ) ;
}
bool IGUIObject : : ScriptEventWithReturn ( const CStr & eventName )
2020-01-15 08:00:37 -08:00
{
if ( m_ScriptHandlers . find ( eventName ) = = m_ScriptHandlers . end ( ) )
2020-05-10 07:01:00 -07:00
return false ;
2020-01-15 08:00:37 -08:00
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
ScriptRequest rq ( m_pGUI . GetScriptInterface ( ) ) ;
2020-11-30 01:03:20 -08:00
JS : : RootedValueVector paramData ( rq . cx ) ;
2020-05-10 07:01:00 -07:00
return ScriptEventWithReturn ( eventName , paramData ) ;
2004-07-08 08:23:47 -07:00
}
2004-08-30 19:09:58 -07:00
2020-01-15 08:00:37 -08:00
void IGUIObject : : ScriptEvent ( const CStr & eventName , const JS : : HandleValueArray & paramData )
2020-05-10 07:01:00 -07:00
{
ScriptEventWithReturn ( eventName , paramData ) ;
}
bool IGUIObject : : ScriptEventWithReturn ( const CStr & eventName , const JS : : HandleValueArray & paramData )
2010-09-11 12:49:21 -07:00
{
2020-01-15 08:00:37 -08:00
std : : map < CStr , JS : : Heap < JSObject * > > : : iterator it = m_ScriptHandlers . find ( eventName ) ;
2010-09-11 12:49:21 -07:00
if ( it = = m_ScriptHandlers . end ( ) )
2020-05-10 07:01:00 -07:00
return false ;
2010-09-11 12:49:21 -07:00
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
ScriptRequest rq ( m_pGUI . GetScriptInterface ( ) ) ;
2020-11-13 05:18:22 -08:00
JS : : RootedObject obj ( rq . cx , GetJSObject ( ) ) ;
JS : : RootedValue handlerVal ( rq . cx , JS : : ObjectValue ( * it - > second ) ) ;
JS : : RootedValue result ( rq . cx ) ;
2019-07-19 14:15:04 -07:00
2020-11-13 05:18:22 -08:00
if ( ! JS_CallFunctionValue ( rq . cx , obj , handlerVal , paramData , & result ) )
2020-05-10 07:01:00 -07:00
{
2020-06-14 03:34:49 -07:00
LOGERROR ( " Errors executing script event \" %s \" " , eventName . c_str ( ) ) ;
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
ScriptException : : CatchPending ( rq ) ;
2020-05-10 07:01:00 -07:00
return false ;
}
return JS : : ToBoolean ( result ) ;
2010-09-11 12:49:21 -07:00
}
2019-08-02 05:18:30 -07:00
void IGUIObject : : CreateJSObject ( )
2007-06-08 15:56:01 -07:00
{
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
ScriptRequest rq ( m_pGUI . GetScriptInterface ( ) ) ;
2020-12-13 07:06:18 -08:00
using ProxyHandler = JSI_GUIProxy < std : : remove_pointer_t < decltype ( this ) > > ;
ProxyHandler : : CreateJSObject ( rq , this , GetGUI ( ) . GetProxyData ( & ProxyHandler : : Singleton ( ) ) , m_JSObject ) ;
2019-08-02 05:18:30 -07:00
}
JSObject * IGUIObject : : GetJSObject ( )
{
2007-06-08 15:56:01 -07:00
// Cache the object when somebody first asks for it, because otherwise
2019-08-02 05:18:30 -07:00
// we end up doing far too much object allocation.
2016-09-02 09:53:22 -07:00
if ( ! m_JSObject . initialized ( ) )
2019-08-02 05:18:30 -07:00
CreateJSObject ( ) ;
2015-01-24 06:46:52 -08:00
return m_JSObject . get ( ) ;
2020-11-21 09:49:06 -08:00
}
void IGUIObject : : toString ( ScriptInterface & scriptInterface , JS : : MutableHandleValue ret )
{
ScriptRequest rq ( scriptInterface ) ;
ScriptInterface : : ToJSVal ( rq , ret , " [GUIObject: " + GetName ( ) + " ] " ) ;
}
void IGUIObject : : focus ( ScriptInterface & UNUSED ( scriptInterface ) , JS : : MutableHandleValue ret )
{
GetGUI ( ) . SetFocusedObject ( this ) ;
ret . setUndefined ( ) ;
}
void IGUIObject : : blur ( ScriptInterface & UNUSED ( scriptInterface ) , JS : : MutableHandleValue ret )
{
GetGUI ( ) . SetFocusedObject ( nullptr ) ;
ret . setUndefined ( ) ;
}
void IGUIObject : : getComputedSize ( ScriptInterface & scriptInterface , JS : : MutableHandleValue ret )
{
UpdateCachedSize ( ) ;
ScriptRequest rq ( scriptInterface ) ;
ScriptInterface : : ToJSVal ( rq , ret , m_CachedActualSize ) ;
2007-06-08 15:56:01 -07:00
}
2019-09-30 07:08:14 -07:00
bool IGUIObject : : IsEnabled ( ) const
{
return m_Enabled ;
}
2019-08-22 16:51:10 -07:00
bool IGUIObject : : IsHidden ( ) const
2019-08-21 06:22:25 -07:00
{
Change GUI Object Setting values to be members of the IGUIObject inheriting class, rename AddSetting to RegisterSetting.
Improves performance for Draw calls by 3-5% according to a shady
benchmark.
Improves memory layout, since the values are not on the heap anymore but
in the using class.
Reduces complexity of the implementation and increases type safety.
Allows specifying default values at setting value construction time,
refs D2242.
Inspired by Vladislav introducing members that cached GetSetting values
in c016a74309/D325, refs #4039, ee38f0db37/D763, refs 4225,
a1c4c23ce4/D474, D406, which were formerly proposed to be removed in
D2241.
Differential Revision: https://code.wildfiregames.com/D2313
Tested on: clang 8.0.1, gcc 9.1.0, Jenkins vs2015
Comments By: Vladislav
This was SVN commit r23005.
2019-09-27 05:49:59 -07:00
return m_Hidden ;
2019-08-21 06:22:25 -07:00
}
2019-08-22 16:51:10 -07:00
bool IGUIObject : : IsHiddenOrGhost ( ) const
2019-08-21 06:22:25 -07:00
{
Change GUI Object Setting values to be members of the IGUIObject inheriting class, rename AddSetting to RegisterSetting.
Improves performance for Draw calls by 3-5% according to a shady
benchmark.
Improves memory layout, since the values are not on the heap anymore but
in the using class.
Reduces complexity of the implementation and increases type safety.
Allows specifying default values at setting value construction time,
refs D2242.
Inspired by Vladislav introducing members that cached GetSetting values
in c016a74309/D325, refs #4039, ee38f0db37/D763, refs 4225,
a1c4c23ce4/D474, D406, which were formerly proposed to be removed in
D2241.
Differential Revision: https://code.wildfiregames.com/D2313
Tested on: clang 8.0.1, gcc 9.1.0, Jenkins vs2015
Comments By: Vladislav
This was SVN commit r23005.
2019-09-27 05:49:59 -07:00
return m_Hidden | | m_Ghost ;
2019-08-21 06:22:25 -07:00
}
Change GUI Object Setting values to be members of the IGUIObject inheriting class, rename AddSetting to RegisterSetting.
Improves performance for Draw calls by 3-5% according to a shady
benchmark.
Improves memory layout, since the values are not on the heap anymore but
in the using class.
Reduces complexity of the implementation and increases type safety.
Allows specifying default values at setting value construction time,
refs D2242.
Inspired by Vladislav introducing members that cached GetSetting values
in c016a74309/D325, refs #4039, ee38f0db37/D763, refs 4225,
a1c4c23ce4/D474, D406, which were formerly proposed to be removed in
D2241.
Differential Revision: https://code.wildfiregames.com/D2313
Tested on: clang 8.0.1, gcc 9.1.0, Jenkins vs2015
Comments By: Vladislav
This was SVN commit r23005.
2019-09-27 05:49:59 -07:00
void IGUIObject : : PlaySound ( const CStrW & soundPath ) const
2019-08-22 15:34:12 -07:00
{
Change GUI Object Setting values to be members of the IGUIObject inheriting class, rename AddSetting to RegisterSetting.
Improves performance for Draw calls by 3-5% according to a shady
benchmark.
Improves memory layout, since the values are not on the heap anymore but
in the using class.
Reduces complexity of the implementation and increases type safety.
Allows specifying default values at setting value construction time,
refs D2242.
Inspired by Vladislav introducing members that cached GetSetting values
in c016a74309/D325, refs #4039, ee38f0db37/D763, refs 4225,
a1c4c23ce4/D474, D406, which were formerly proposed to be removed in
D2241.
Differential Revision: https://code.wildfiregames.com/D2313
Tested on: clang 8.0.1, gcc 9.1.0, Jenkins vs2015
Comments By: Vladislav
This was SVN commit r23005.
2019-09-27 05:49:59 -07:00
if ( g_SoundManager & & ! soundPath . empty ( ) )
2019-08-22 15:34:12 -07:00
g_SoundManager - > PlayAsUI ( soundPath . c_str ( ) , false ) ;
}
2004-08-30 19:09:58 -07:00
CStr IGUIObject : : GetPresentableName ( ) const
{
// __internal(), must be at least 13 letters to be able to be
// an internal name
2007-02-01 06:46:14 -08:00
if ( m_Name . length ( ) < = 12 )
2004-08-30 19:09:58 -07:00
return m_Name ;
2007-02-01 06:46:14 -08:00
if ( m_Name . substr ( 0 , 10 ) = = " __internal " )
2004-08-30 19:09:58 -07:00
return CStr ( " [unnamed object] " ) ;
else
return m_Name ;
2004-10-14 03:09:26 -07:00
}
void IGUIObject : : SetFocus ( )
{
2019-08-21 03:12:33 -07:00
m_pGUI . SetFocusedObject ( this ) ;
2004-10-14 03:09:26 -07:00
}
bool IGUIObject : : IsFocused ( ) const
{
2019-08-21 03:12:33 -07:00
return m_pGUI . GetFocusedObject ( ) = = this ;
2004-11-07 13:30:47 -08:00
}
2005-07-20 16:49:39 -07:00
2019-09-18 13:51:45 -07:00
bool IGUIObject : : IsBaseObject ( ) const
{
2020-12-09 06:39:14 -08:00
return this = = m_pGUI . GetBaseObject ( ) ;
2019-09-18 13:51:45 -07:00
}
2005-07-20 16:49:39 -07:00
bool IGUIObject : : IsRootObject ( ) const
{
2020-12-09 06:39:14 -08:00
return m_pParent = = m_pGUI . GetBaseObject ( ) ;
2005-07-20 16:49:39 -07:00
}
2009-09-27 08:04:46 -07:00
2015-08-21 10:08:41 -07:00
void IGUIObject : : TraceMember ( JSTracer * trc )
2015-01-24 06:46:52 -08:00
{
2019-08-01 13:20:24 -07:00
// Please ensure to adapt the Tracer enabling and disabling in accordance with the GC things traced!
2016-01-12 16:42:55 -08:00
for ( std : : pair < const CStr , JS : : Heap < JSObject * > > & handler : m_ScriptHandlers )
2020-11-12 00:04:24 -08:00
JS : : TraceEdge ( trc , & handler . second , " IGUIObject::m_ScriptHandlers " ) ;
2015-01-24 06:46:52 -08:00
}
2019-08-03 19:20:08 -07:00
// Instantiate templated functions:
Move static GUI<>::SetSetting operating on IGUIObject to a member IGUIObject::SetSetting.
Remove PSERROR codes from SetSetting (let std::map throw an out_of_range
if a caller wants to Set a setting that doesn't exist without having
checked with SettingExists, equal to GetSetting from 92b6cdfeab).
That also simplifies std::function SetSettingWrap construct from
0a7d0ecdde to void IGUIObject::SettingChanged.
Don't trigger debug_warn or exceptions in GUITooltip::ShowTooltip if the
XML author specified wrong tooltip input, and dodge another
dynamic_cast.
Rename existing IGUIObject::SetSetting to
IGUIObject::SetSettingFromString and comment that it is purposed for
parsing XML files.
Remove SetSetting default value, so that authors are made aware
explicitly of the need to decide the function broadcasting a message,
refs d87057b1c0, 719f2d7967, ...
Change const bool& SkipMessage to const bool SendMessage, so that a
positive value relates to a positive action.
Clean AddSettings whitespace and integer types.
Differential Revision: https://code.wildfiregames.com/D2231
Tested on: gcc 9.1.0, clang 8.0.1, Jenkins
Comments By: Philip on IRC on 2010-07-24 on GUIUtil being ugly, in case
that one counts
This was SVN commit r22796.
2019-08-28 04:21:11 -07:00
// These functions avoid copies by working with a reference and move semantics.
2019-08-26 05:25:07 -07:00
# define TYPE(T) \
Change GUI Object Setting values to be members of the IGUIObject inheriting class, rename AddSetting to RegisterSetting.
Improves performance for Draw calls by 3-5% according to a shady
benchmark.
Improves memory layout, since the values are not on the heap anymore but
in the using class.
Reduces complexity of the implementation and increases type safety.
Allows specifying default values at setting value construction time,
refs D2242.
Inspired by Vladislav introducing members that cached GetSetting values
in c016a74309/D325, refs #4039, ee38f0db37/D763, refs 4225,
a1c4c23ce4/D474, D406, which were formerly proposed to be removed in
D2241.
Differential Revision: https://code.wildfiregames.com/D2313
Tested on: clang 8.0.1, gcc 9.1.0, Jenkins vs2015
Comments By: Vladislav
This was SVN commit r23005.
2019-09-27 05:49:59 -07:00
template void IGUIObject : : RegisterSetting < T > ( const CStr & Name , T & Value ) ; \
2019-08-26 05:25:07 -07:00
template T & IGUIObject : : GetSetting < T > ( const CStr & Setting ) ; \
template const T & IGUIObject : : GetSetting < T > ( const CStr & Setting ) const ; \
Move static GUI<>::SetSetting operating on IGUIObject to a member IGUIObject::SetSetting.
Remove PSERROR codes from SetSetting (let std::map throw an out_of_range
if a caller wants to Set a setting that doesn't exist without having
checked with SettingExists, equal to GetSetting from 92b6cdfeab).
That also simplifies std::function SetSettingWrap construct from
0a7d0ecdde to void IGUIObject::SettingChanged.
Don't trigger debug_warn or exceptions in GUITooltip::ShowTooltip if the
XML author specified wrong tooltip input, and dodge another
dynamic_cast.
Rename existing IGUIObject::SetSetting to
IGUIObject::SetSettingFromString and comment that it is purposed for
parsing XML files.
Remove SetSetting default value, so that authors are made aware
explicitly of the need to decide the function broadcasting a message,
refs d87057b1c0, 719f2d7967, ...
Change const bool& SkipMessage to const bool SendMessage, so that a
positive value relates to a positive action.
Clean AddSettings whitespace and integer types.
Differential Revision: https://code.wildfiregames.com/D2231
Tested on: gcc 9.1.0, clang 8.0.1, Jenkins
Comments By: Philip on IRC on 2010-07-24 on GUIUtil being ugly, in case
that one counts
This was SVN commit r22796.
2019-08-28 04:21:11 -07:00
template void IGUIObject : : SetSetting < T > ( const CStr & Setting , T & Value , const bool SendMessage ) ; \
2019-08-26 05:25:07 -07:00
2019-10-02 02:44:00 -07:00
# include "gui/GUISettingTypes.h"
Move static GUI<>::SetSetting operating on IGUIObject to a member IGUIObject::SetSetting.
Remove PSERROR codes from SetSetting (let std::map throw an out_of_range
if a caller wants to Set a setting that doesn't exist without having
checked with SettingExists, equal to GetSetting from 92b6cdfeab).
That also simplifies std::function SetSettingWrap construct from
0a7d0ecdde to void IGUIObject::SettingChanged.
Don't trigger debug_warn or exceptions in GUITooltip::ShowTooltip if the
XML author specified wrong tooltip input, and dodge another
dynamic_cast.
Rename existing IGUIObject::SetSetting to
IGUIObject::SetSettingFromString and comment that it is purposed for
parsing XML files.
Remove SetSetting default value, so that authors are made aware
explicitly of the need to decide the function broadcasting a message,
refs d87057b1c0, 719f2d7967, ...
Change const bool& SkipMessage to const bool SendMessage, so that a
positive value relates to a positive action.
Clean AddSettings whitespace and integer types.
Differential Revision: https://code.wildfiregames.com/D2231
Tested on: gcc 9.1.0, clang 8.0.1, Jenkins
Comments By: Philip on IRC on 2010-07-24 on GUIUtil being ugly, in case
that one counts
This was SVN commit r22796.
2019-08-28 04:21:11 -07:00
# undef TYPE
2019-08-26 05:25:07 -07:00
Move static GUI<>::SetSetting operating on IGUIObject to a member IGUIObject::SetSetting.
Remove PSERROR codes from SetSetting (let std::map throw an out_of_range
if a caller wants to Set a setting that doesn't exist without having
checked with SettingExists, equal to GetSetting from 92b6cdfeab).
That also simplifies std::function SetSettingWrap construct from
0a7d0ecdde to void IGUIObject::SettingChanged.
Don't trigger debug_warn or exceptions in GUITooltip::ShowTooltip if the
XML author specified wrong tooltip input, and dodge another
dynamic_cast.
Rename existing IGUIObject::SetSetting to
IGUIObject::SetSettingFromString and comment that it is purposed for
parsing XML files.
Remove SetSetting default value, so that authors are made aware
explicitly of the need to decide the function broadcasting a message,
refs d87057b1c0, 719f2d7967, ...
Change const bool& SkipMessage to const bool SendMessage, so that a
positive value relates to a positive action.
Clean AddSettings whitespace and integer types.
Differential Revision: https://code.wildfiregames.com/D2231
Tested on: gcc 9.1.0, clang 8.0.1, Jenkins
Comments By: Philip on IRC on 2010-07-24 on GUIUtil being ugly, in case
that one counts
This was SVN commit r22796.
2019-08-28 04:21:11 -07:00
// Copying functions - discouraged except for primitives.
# define TYPE(T) \
template void IGUIObject : : SetSetting < T > ( const CStr & Setting , const T & Value , const bool SendMessage ) ; \
# define GUITYPE_IGNORE_NONCOPYABLE
2019-10-02 02:44:00 -07:00
# include "gui/GUISettingTypes.h"
Move static GUI<>::SetSetting operating on IGUIObject to a member IGUIObject::SetSetting.
Remove PSERROR codes from SetSetting (let std::map throw an out_of_range
if a caller wants to Set a setting that doesn't exist without having
checked with SettingExists, equal to GetSetting from 92b6cdfeab).
That also simplifies std::function SetSettingWrap construct from
0a7d0ecdde to void IGUIObject::SettingChanged.
Don't trigger debug_warn or exceptions in GUITooltip::ShowTooltip if the
XML author specified wrong tooltip input, and dodge another
dynamic_cast.
Rename existing IGUIObject::SetSetting to
IGUIObject::SetSettingFromString and comment that it is purposed for
parsing XML files.
Remove SetSetting default value, so that authors are made aware
explicitly of the need to decide the function broadcasting a message,
refs d87057b1c0, 719f2d7967, ...
Change const bool& SkipMessage to const bool SendMessage, so that a
positive value relates to a positive action.
Clean AddSettings whitespace and integer types.
Differential Revision: https://code.wildfiregames.com/D2231
Tested on: gcc 9.1.0, clang 8.0.1, Jenkins
Comments By: Philip on IRC on 2010-07-24 on GUIUtil being ugly, in case
that one counts
This was SVN commit r22796.
2019-08-28 04:21:11 -07:00
# undef GUITYPE_IGNORE_NONCOPYABLE
2019-08-03 19:20:08 -07:00
# undef TYPE