2012-09-27 06:47:47 +00:00
# include "enchantingdialog.hpp"
2014-02-19 17:40:29 +00:00
# include <iomanip>
2015-01-10 02:56:06 +00:00
# include <MyGUI_Button.h>
# include <MyGUI_ScrollView.h>
2017-09-24 11:05:52 +00:00
# include <MyGUI_EditBox.h>
2015-01-10 02:56:06 +00:00
# include <components/widgets/list.hpp>
2017-06-01 06:00:15 +00:00
# include <components/settings/settings.hpp>
2013-03-16 18:00:14 +00:00
2014-01-22 12:04:36 +00:00
# include "../mwbase/mechanicsmanager.hpp"
2013-05-11 16:38:27 +00:00
# include "../mwworld/class.hpp"
2014-01-08 22:37:46 +00:00
# include "../mwworld/containerstore.hpp"
2014-12-19 10:26:54 +00:00
# include "../mwworld/esmstore.hpp"
2013-03-16 18:00:14 +00:00
2015-08-21 09:12:39 +00:00
# include "../mwmechanics/actorutil.hpp"
2013-03-16 18:00:14 +00:00
# include "itemselection.hpp"
2014-06-05 20:13:18 +00:00
# include "itemwidget.hpp"
2012-09-27 06:47:47 +00:00
2013-05-11 16:38:27 +00:00
# include "sortfilteritemmodel.hpp"
2012-09-27 06:47:47 +00:00
namespace MWGui
{
2013-04-10 18:46:21 +00:00
EnchantingDialog : : EnchantingDialog ( )
: WindowBase ( " openmw_enchanting_dialog.layout " )
2014-08-24 19:59:52 +00:00
, EffectEditorBase ( EffectEditorBase : : Enchanting )
2013-03-16 18:00:14 +00:00
, mItemSelectionDialog ( NULL )
2012-09-27 06:47:47 +00:00
{
2013-03-28 16:41:00 +00:00
getWidget ( mName , " NameEdit " ) ;
2012-10-03 13:06:54 +00:00
getWidget ( mCancelButton , " CancelButton " ) ;
2012-10-03 13:36:10 +00:00
getWidget ( mAvailableEffectsList , " AvailableEffects " ) ;
getWidget ( mUsedEffectsView , " UsedEffects " ) ;
2013-03-16 18:00:14 +00:00
getWidget ( mItemBox , " ItemBox " ) ;
getWidget ( mSoulBox , " SoulBox " ) ;
getWidget ( mEnchantmentPoints , " Enchantment " ) ;
getWidget ( mCastCost , " CastCost " ) ;
getWidget ( mCharge , " Charge " ) ;
2017-06-01 06:00:15 +00:00
getWidget ( mSuccessChance , " SuccessChance " ) ;
getWidget ( mChanceLayout , " ChanceLayout " ) ;
2013-03-16 18:00:14 +00:00
getWidget ( mTypeButton , " TypeButton " ) ;
getWidget ( mBuyButton , " BuyButton " ) ;
getWidget ( mPrice , " PriceLabel " ) ;
2013-04-03 16:02:30 +00:00
getWidget ( mPriceText , " PriceTextLabel " ) ;
2012-10-03 13:36:10 +00:00
setWidgets ( mAvailableEffectsList , mUsedEffectsView ) ;
2012-09-27 06:47:47 +00:00
2012-10-03 13:06:54 +00:00
mCancelButton - > eventMouseButtonClick + = MyGUI : : newDelegate ( this , & EnchantingDialog : : onCancelButtonClicked ) ;
2013-03-16 18:00:14 +00:00
mItemBox - > eventMouseButtonClick + = MyGUI : : newDelegate ( this , & EnchantingDialog : : onSelectItem ) ;
mSoulBox - > eventMouseButtonClick + = MyGUI : : newDelegate ( this , & EnchantingDialog : : onSelectSoul ) ;
2013-03-28 16:41:00 +00:00
mBuyButton - > eventMouseButtonClick + = MyGUI : : newDelegate ( this , & EnchantingDialog : : onBuyButtonClicked ) ;
mTypeButton - > eventMouseButtonClick + = MyGUI : : newDelegate ( this , & EnchantingDialog : : onTypeButtonClicked ) ;
2017-09-24 11:05:52 +00:00
mName - > eventEditSelectAccept + = MyGUI : : newDelegate ( this , & EnchantingDialog : : onAccept ) ;
2013-03-16 18:00:14 +00:00
}
EnchantingDialog : : ~ EnchantingDialog ( )
{
delete mItemSelectionDialog ;
2012-09-27 06:47:47 +00:00
}
2017-09-22 15:10:53 +00:00
void EnchantingDialog : : onOpen ( )
2012-09-27 06:47:47 +00:00
{
center ( ) ;
2017-09-24 11:05:52 +00:00
MWBase : : Environment : : get ( ) . getWindowManager ( ) - > setKeyFocusWidget ( mName ) ;
2014-06-05 20:13:18 +00:00
}
void EnchantingDialog : : setSoulGem ( const MWWorld : : Ptr & gem )
{
if ( gem . isEmpty ( ) )
{
mSoulBox - > setItem ( MWWorld : : Ptr ( ) ) ;
mSoulBox - > clearUserStrings ( ) ;
mEnchanting . setSoulGem ( MWWorld : : Ptr ( ) ) ;
}
else
{
mSoulBox - > setItem ( gem ) ;
mSoulBox - > setUserString ( " ToolTipType " , " ItemPtr " ) ;
2017-03-13 01:47:52 +00:00
mSoulBox - > setUserData ( MWWorld : : Ptr ( gem ) ) ;
2014-06-05 20:13:18 +00:00
mEnchanting . setSoulGem ( gem ) ;
}
}
void EnchantingDialog : : setItem ( const MWWorld : : Ptr & item )
{
if ( item . isEmpty ( ) )
{
mItemBox - > setItem ( MWWorld : : Ptr ( ) ) ;
mItemBox - > clearUserStrings ( ) ;
mEnchanting . setOldItem ( MWWorld : : Ptr ( ) ) ;
}
else
{
2014-08-24 14:41:17 +00:00
mName - > setCaption ( item . getClass ( ) . getName ( item ) ) ;
2014-06-05 20:13:18 +00:00
mItemBox - > setItem ( item ) ;
mItemBox - > setUserString ( " ToolTipType " , " ItemPtr " ) ;
2017-03-13 01:47:52 +00:00
mItemBox - > setUserData ( MWWorld : : Ptr ( item ) ) ;
2014-06-05 20:13:18 +00:00
mEnchanting . setOldItem ( item ) ;
}
2013-03-16 18:00:14 +00:00
}
void EnchantingDialog : : updateLabels ( )
{
2013-04-29 08:19:09 +00:00
std : : stringstream enchantCost ;
2013-05-27 18:23:04 +00:00
enchantCost < < std : : setprecision ( 1 ) < < std : : fixed < < mEnchanting . getEnchantPoints ( ) ;
2015-01-10 02:01:01 +00:00
mEnchantmentPoints - > setCaption ( enchantCost . str ( ) + " / " + MyGUI : : utility : : toString ( mEnchanting . getMaxEnchantValue ( ) ) ) ;
2013-03-28 16:41:00 +00:00
2015-01-10 02:01:01 +00:00
mCharge - > setCaption ( MyGUI : : utility : : toString ( mEnchanting . getGemCharge ( ) ) ) ;
2013-03-28 16:41:00 +00:00
2017-06-01 06:00:15 +00:00
int successChance = int ( mEnchanting . getEnchantChance ( ) ) ;
mSuccessChance - > setCaption ( MyGUI : : utility : : toString ( std : : max ( 0 , successChance ) ) ) ;
2013-05-27 18:16:57 +00:00
std : : stringstream castCost ;
2015-01-07 03:28:56 +00:00
castCost < < mEnchanting . getEffectiveCastCost ( ) ;
2014-12-10 17:05:11 +00:00
mCastCost - > setCaption ( castCost . str ( ) ) ;
2013-03-28 16:41:00 +00:00
2015-01-10 02:01:01 +00:00
mPrice - > setCaption ( MyGUI : : utility : : toString ( mEnchanting . getEnchantPrice ( ) ) ) ;
2013-04-02 18:46:48 +00:00
2013-05-27 12:42:08 +00:00
switch ( mEnchanting . getCastStyle ( ) )
2013-03-28 16:41:00 +00:00
{
2013-05-28 22:01:18 +00:00
case ESM : : Enchantment : : CastOnce :
2013-04-10 04:32:05 +00:00
mTypeButton - > setCaption ( MWBase : : Environment : : get ( ) . getWindowManager ( ) - > getGameSettingString ( " sItemCastOnce " , " Cast Once " ) ) ;
2015-01-05 04:33:51 +00:00
setConstantEffect ( false ) ;
2013-03-28 16:41:00 +00:00
break ;
2013-05-28 22:01:18 +00:00
case ESM : : Enchantment : : WhenStrikes :
2013-04-10 04:32:05 +00:00
mTypeButton - > setCaption ( MWBase : : Environment : : get ( ) . getWindowManager ( ) - > getGameSettingString ( " sItemCastWhenStrikes " , " When Strikes " ) ) ;
2015-01-05 04:33:51 +00:00
setConstantEffect ( false ) ;
2013-03-28 16:41:00 +00:00
break ;
2013-05-28 22:01:18 +00:00
case ESM : : Enchantment : : WhenUsed :
2013-04-10 04:32:05 +00:00
mTypeButton - > setCaption ( MWBase : : Environment : : get ( ) . getWindowManager ( ) - > getGameSettingString ( " sItemCastWhenUsed " , " When Used " ) ) ;
2015-01-05 04:33:51 +00:00
setConstantEffect ( false ) ;
2013-03-28 16:41:00 +00:00
break ;
2013-05-28 22:01:18 +00:00
case ESM : : Enchantment : : ConstantEffect :
2013-04-10 04:32:05 +00:00
mTypeButton - > setCaption ( MWBase : : Environment : : get ( ) . getWindowManager ( ) - > getGameSettingString ( " sItemCastConstant " , " Cast Constant " ) ) ;
2015-01-05 04:33:51 +00:00
setConstantEffect ( true ) ;
2013-03-28 16:41:00 +00:00
break ;
}
2012-09-27 06:47:47 +00:00
}
2017-09-22 19:26:41 +00:00
void EnchantingDialog : : setPtr ( const MWWorld : : Ptr & ptr )
2012-09-27 06:47:47 +00:00
{
2017-06-01 06:00:15 +00:00
mName - > setCaption ( " " ) ;
2017-09-22 19:26:41 +00:00
if ( ptr . getClass ( ) . isActor ( ) )
{
mEnchanting . setSelfEnchanting ( false ) ;
mEnchanting . setEnchanter ( ptr ) ;
mBuyButton - > setCaptionWithReplacing ( " #{sBuy} " ) ;
mChanceLayout - > setVisible ( false ) ;
mPtr = ptr ;
setSoulGem ( MWWorld : : Ptr ( ) ) ;
mPrice - > setVisible ( true ) ;
mPriceText - > setVisible ( true ) ;
}
else
{
mEnchanting . setSelfEnchanting ( true ) ;
mEnchanting . setEnchanter ( MWMechanics : : getPlayer ( ) ) ;
mBuyButton - > setCaptionWithReplacing ( " #{sCreate} " ) ;
bool enabled = Settings : : Manager : : getBool ( " show enchant chance " , " Game " ) ;
mChanceLayout - > setVisible ( enabled ) ;
mPtr = MWMechanics : : getPlayer ( ) ;
setSoulGem ( ptr ) ;
mPrice - > setVisible ( false ) ;
mPriceText - > setVisible ( false ) ;
}
2012-10-03 13:36:10 +00:00
2014-07-29 13:32:22 +00:00
setItem ( MWWorld : : Ptr ( ) ) ;
2012-10-03 13:36:10 +00:00
startEditing ( ) ;
2013-04-03 16:06:11 +00:00
updateLabels ( ) ;
2013-03-30 14:51:07 +00:00
}
2012-09-27 06:47:47 +00:00
void EnchantingDialog : : onReferenceUnavailable ( )
{
2013-04-10 04:32:05 +00:00
MWBase : : Environment : : get ( ) . getWindowManager ( ) - > removeGuiMode ( GM_Enchanting ) ;
2014-07-29 13:32:22 +00:00
resetReference ( ) ;
}
void EnchantingDialog : : resetReference ( )
{
ReferenceInterface : : resetReference ( ) ;
setItem ( MWWorld : : Ptr ( ) ) ;
setSoulGem ( MWWorld : : Ptr ( ) ) ;
mPtr = MWWorld : : Ptr ( ) ;
mEnchanting . setEnchanter ( MWWorld : : Ptr ( ) ) ;
2012-09-27 06:47:47 +00:00
}
2012-10-03 13:06:54 +00:00
void EnchantingDialog : : onCancelButtonClicked ( MyGUI : : Widget * sender )
{
2017-09-23 10:18:39 +00:00
MWBase : : Environment : : get ( ) . getWindowManager ( ) - > removeGuiMode ( GM_Enchanting ) ;
2012-10-03 13:06:54 +00:00
}
2013-03-16 18:00:14 +00:00
void EnchantingDialog : : onSelectItem ( MyGUI : : Widget * sender )
{
2014-06-05 20:13:18 +00:00
if ( mEnchanting . getOldItem ( ) . isEmpty ( ) )
{
delete mItemSelectionDialog ;
mItemSelectionDialog = new ItemSelectionDialog ( " #{sEnchantItems} " ) ;
mItemSelectionDialog - > eventItemSelected + = MyGUI : : newDelegate ( this , & EnchantingDialog : : onItemSelected ) ;
mItemSelectionDialog - > eventDialogCanceled + = MyGUI : : newDelegate ( this , & EnchantingDialog : : onItemCancel ) ;
mItemSelectionDialog - > setVisible ( true ) ;
2015-08-21 09:12:39 +00:00
mItemSelectionDialog - > openContainer ( MWMechanics : : getPlayer ( ) ) ;
2014-06-05 20:13:18 +00:00
mItemSelectionDialog - > setFilter ( SortFilterItemModel : : Filter_OnlyEnchantable ) ;
}
else
{
setItem ( MWWorld : : Ptr ( ) ) ;
2014-08-24 14:41:17 +00:00
updateLabels ( ) ;
2014-06-05 20:13:18 +00:00
}
2013-03-16 18:00:14 +00:00
}
void EnchantingDialog : : onItemSelected ( MWWorld : : Ptr item )
{
mItemSelectionDialog - > setVisible ( false ) ;
2014-06-05 20:13:18 +00:00
setItem ( item ) ;
2017-07-10 11:48:00 +00:00
MWBase : : Environment : : get ( ) . getWindowManager ( ) - > playSound ( item . getClass ( ) . getDownSoundId ( item ) ) ;
2013-05-27 12:42:08 +00:00
mEnchanting . nextCastStyle ( ) ;
2013-03-16 18:00:14 +00:00
updateLabels ( ) ;
}
void EnchantingDialog : : onItemCancel ( )
{
mItemSelectionDialog - > setVisible ( false ) ;
}
void EnchantingDialog : : onSoulSelected ( MWWorld : : Ptr item )
{
mItemSelectionDialog - > setVisible ( false ) ;
2013-03-28 16:41:00 +00:00
2014-07-29 13:32:22 +00:00
mEnchanting . setSoulGem ( item ) ;
2013-03-28 16:41:00 +00:00
if ( mEnchanting . getGemCharge ( ) = = 0 )
{
2013-04-10 04:32:05 +00:00
MWBase : : Environment : : get ( ) . getWindowManager ( ) - > messageBox ( " #{sNotifyMessage32} " ) ;
2013-03-28 16:41:00 +00:00
return ;
}
2013-03-16 18:00:14 +00:00
2014-06-05 20:13:18 +00:00
setSoulGem ( item ) ;
2017-07-10 11:48:00 +00:00
MWBase : : Environment : : get ( ) . getWindowManager ( ) - > playSound ( item . getClass ( ) . getDownSoundId ( item ) ) ;
2014-07-29 13:32:22 +00:00
updateLabels ( ) ;
2013-03-16 18:00:14 +00:00
}
void EnchantingDialog : : onSoulCancel ( )
{
mItemSelectionDialog - > setVisible ( false ) ;
}
void EnchantingDialog : : onSelectSoul ( MyGUI : : Widget * sender )
{
2014-06-05 20:13:18 +00:00
if ( mEnchanting . getGem ( ) . isEmpty ( ) )
{
delete mItemSelectionDialog ;
mItemSelectionDialog = new ItemSelectionDialog ( " #{sSoulGemsWithSouls} " ) ;
mItemSelectionDialog - > eventItemSelected + = MyGUI : : newDelegate ( this , & EnchantingDialog : : onSoulSelected ) ;
mItemSelectionDialog - > eventDialogCanceled + = MyGUI : : newDelegate ( this , & EnchantingDialog : : onSoulCancel ) ;
mItemSelectionDialog - > setVisible ( true ) ;
2015-08-21 09:12:39 +00:00
mItemSelectionDialog - > openContainer ( MWMechanics : : getPlayer ( ) ) ;
2014-06-05 20:13:18 +00:00
mItemSelectionDialog - > setFilter ( SortFilterItemModel : : Filter_OnlyChargedSoulstones ) ;
//MWBase::Environment::get().getWindowManager()->messageBox("#{sInventorySelectNoSoul}");
}
else
{
setSoulGem ( MWWorld : : Ptr ( ) ) ;
2017-05-20 14:30:11 +00:00
mEnchanting . nextCastStyle ( ) ;
2014-07-29 13:32:22 +00:00
updateLabels ( ) ;
2017-05-20 14:30:11 +00:00
updateEffectsView ( ) ;
2014-06-05 20:13:18 +00:00
}
2013-03-16 18:00:14 +00:00
}
2013-03-28 16:41:00 +00:00
void EnchantingDialog : : notifyEffectsChanged ( )
{
mEffectList . mList = mEffects ;
mEnchanting . setEffect ( mEffectList ) ;
updateLabels ( ) ;
}
void EnchantingDialog : : onTypeButtonClicked ( MyGUI : : Widget * sender )
{
2013-05-27 12:42:08 +00:00
mEnchanting . nextCastStyle ( ) ;
2013-03-28 16:41:00 +00:00
updateLabels ( ) ;
2015-01-05 04:33:51 +00:00
updateEffectsView ( ) ;
2013-03-28 16:41:00 +00:00
}
2017-09-24 11:05:52 +00:00
void EnchantingDialog : : onAccept ( MyGUI : : EditBox * sender )
{
onBuyButtonClicked ( sender ) ;
}
2013-03-28 16:41:00 +00:00
void EnchantingDialog : : onBuyButtonClicked ( MyGUI : : Widget * sender )
{
if ( mEffects . size ( ) < = 0 )
{
2014-01-10 21:27:31 +00:00
MWBase : : Environment : : get ( ) . getWindowManager ( ) - > messageBox ( " #{sEnchantmentMenu11} " ) ;
2013-03-28 16:41:00 +00:00
return ;
}
if ( mName - > getCaption ( ) . empty ( ) )
{
2013-04-10 04:32:05 +00:00
MWBase : : Environment : : get ( ) . getWindowManager ( ) - > messageBox ( " #{sNotifyMessage10} " ) ;
2013-03-28 16:41:00 +00:00
return ;
}
if ( mEnchanting . soulEmpty ( ) )
{
2013-04-10 04:32:05 +00:00
MWBase : : Environment : : get ( ) . getWindowManager ( ) - > messageBox ( " #{sNotifyMessage52} " ) ;
2013-03-28 16:41:00 +00:00
return ;
}
if ( mEnchanting . itemEmpty ( ) )
{
2013-04-10 04:32:05 +00:00
MWBase : : Environment : : get ( ) . getWindowManager ( ) - > messageBox ( " #{sNotifyMessage11} " ) ;
2013-03-30 11:54:20 +00:00
return ;
}
2013-05-27 18:23:04 +00:00
if ( mEnchanting . getEnchantPoints ( ) > mEnchanting . getMaxEnchantValue ( ) )
2013-03-30 11:54:20 +00:00
{
2013-04-10 04:32:05 +00:00
MWBase : : Environment : : get ( ) . getWindowManager ( ) - > messageBox ( " #{sNotifyMessage29} " ) ;
2013-03-28 16:41:00 +00:00
return ;
}
mEnchanting . setNewItemName ( mName - > getCaption ( ) ) ;
mEnchanting . setEffect ( mEffectList ) ;
2015-08-21 09:12:39 +00:00
MWWorld : : Ptr player = MWMechanics : : getPlayer ( ) ;
2014-01-08 22:37:46 +00:00
int playerGold = player . getClass ( ) . getContainerStore ( player ) . count ( MWWorld : : ContainerStore : : sGoldId ) ;
2014-07-25 22:57:49 +00:00
if ( mPtr ! = player & & mEnchanting . getEnchantPrice ( ) > playerGold )
2013-04-02 18:46:48 +00:00
{
2013-04-10 04:32:05 +00:00
MWBase : : Environment : : get ( ) . getWindowManager ( ) - > messageBox ( " #{sNotifyMessage18} " ) ;
2013-04-02 18:46:48 +00:00
return ;
}
2014-01-22 12:04:36 +00:00
// check if the player is attempting to use a soulstone or item that was stolen from this actor
if ( mPtr ! = player )
{
for ( int i = 0 ; i < 2 ; + + i )
{
MWWorld : : Ptr item = ( i = = 0 ) ? mEnchanting . getOldItem ( ) : mEnchanting . getGem ( ) ;
2015-02-04 20:18:43 +00:00
if ( MWBase : : Environment : : get ( ) . getMechanicsManager ( ) - > isItemStolenFrom ( item . getCellRef ( ) . getRefId ( ) ,
mPtr . getCellRef ( ) . getRefId ( ) ) )
2014-01-22 12:04:36 +00:00
{
std : : string msg = MWBase : : Environment : : get ( ) . getWorld ( ) - > getStore ( ) . get < ESM : : GameSetting > ( ) . find ( " sNotifyMessage49 " ) - > getString ( ) ;
if ( msg . find ( " %s " ) ! = std : : string : : npos )
msg . replace ( msg . find ( " %s " ) , 2 , item . getClass ( ) . getName ( item ) ) ;
MWBase : : Environment : : get ( ) . getWindowManager ( ) - > messageBox ( msg ) ;
2017-08-14 15:29:34 +00:00
MWBase : : Environment : : get ( ) . getMechanicsManager ( ) - > confiscateStolenItemToOwner ( player , item , mPtr , 1 ) ;
2014-01-22 12:04:36 +00:00
MWBase : : Environment : : get ( ) . getWindowManager ( ) - > removeGuiMode ( GM_Enchanting ) ;
2017-09-26 21:27:53 +00:00
MWBase : : Environment : : get ( ) . getWindowManager ( ) - > exitCurrentGuiMode ( ) ;
2014-01-22 12:04:36 +00:00
return ;
}
}
}
2013-03-30 18:08:42 +00:00
int result = mEnchanting . create ( ) ;
if ( result = = 1 )
2013-11-28 10:37:30 +00:00
{
2017-07-10 11:48:00 +00:00
MWBase : : Environment : : get ( ) . getWindowManager ( ) - > playSound ( " enchant success " ) ;
2013-04-10 04:32:05 +00:00
MWBase : : Environment : : get ( ) . getWindowManager ( ) - > messageBox ( " #{sEnchantmentMenu12} " ) ;
2017-07-26 02:36:12 +00:00
/*
Start of tes3mp addition
Include a messagebox notifying players that player - made enchantments are not synced yet
*/
MWBase : : Environment : : get ( ) . getWindowManager ( ) - > messageBox ( " Player-made enchantments are not synchronized in multiplayer yet and they will not show up for other players. " ) ;
/*
End of tes3mp addition
*/
2013-11-28 10:37:30 +00:00
}
2013-03-30 18:08:42 +00:00
else
2013-11-28 10:37:30 +00:00
{
2017-07-10 11:48:00 +00:00
MWBase : : Environment : : get ( ) . getWindowManager ( ) - > playSound ( " enchant fail " ) ;
2013-04-10 04:32:05 +00:00
MWBase : : Environment : : get ( ) . getWindowManager ( ) - > messageBox ( " #{sNotifyMessage34} " ) ;
2013-11-28 10:37:30 +00:00
}
2013-03-30 18:08:42 +00:00
2013-04-10 04:32:05 +00:00
MWBase : : Environment : : get ( ) . getWindowManager ( ) - > removeGuiMode ( GM_Enchanting ) ;
2013-03-28 16:41:00 +00:00
}
2012-09-27 06:47:47 +00:00
}