2012-05-24 13:57:23 +00:00
# include "alchemywindow.hpp"
2015-01-10 01:50:43 +00:00
# include <MyGUI_Gui.h>
2012-05-25 13:24:33 +00:00
# include "../mwbase/environment.hpp"
2012-07-03 10:30:50 +00:00
# include "../mwbase/world.hpp"
2012-08-12 16:11:09 +00:00
# include "../mwbase/windowmanager.hpp"
2012-07-03 10:30:50 +00:00
2014-09-19 07:10:46 +00:00
# include "../mwmechanics/magiceffects.hpp"
2015-01-10 00:00:52 +00:00
# include "../mwmechanics/alchemy.hpp"
2015-08-21 09:12:39 +00:00
# include "../mwmechanics/actorutil.hpp"
2014-09-19 07:10:46 +00:00
2013-05-11 16:38:27 +00:00
# include "../mwworld/class.hpp"
2014-12-19 10:26:54 +00:00
# include "../mwworld/esmstore.hpp"
# include <components/esm/records.hpp>
2013-05-11 16:38:27 +00:00
# include "inventoryitemmodel.hpp"
# include "sortfilteritemmodel.hpp"
# include "itemview.hpp"
2014-06-05 20:13:18 +00:00
# include "itemwidget.hpp"
2012-05-25 13:24:33 +00:00
2012-05-24 13:57:23 +00:00
namespace MWGui
{
2013-04-10 18:46:21 +00:00
AlchemyWindow : : AlchemyWindow ( )
: WindowBase ( " openmw_alchemy_window.layout " )
2013-05-15 15:54:18 +00:00
, mSortModel ( NULL )
2015-01-10 00:00:52 +00:00
, mAlchemy ( new MWMechanics : : Alchemy ( ) )
2015-05-01 00:24:27 +00:00
, mApparatus ( 4 )
, mIngredients ( 4 )
2012-05-24 13:57:23 +00:00
{
2012-05-25 10:58:28 +00:00
getWidget ( mCreateButton , " CreateButton " ) ;
getWidget ( mCancelButton , " CancelButton " ) ;
2012-10-02 08:29:47 +00:00
getWidget ( mIngredients [ 0 ] , " Ingredient1 " ) ;
getWidget ( mIngredients [ 1 ] , " Ingredient2 " ) ;
getWidget ( mIngredients [ 2 ] , " Ingredient3 " ) ;
getWidget ( mIngredients [ 3 ] , " Ingredient4 " ) ;
2012-09-30 17:05:45 +00:00
getWidget ( mApparatus [ 0 ] , " Apparatus1 " ) ;
getWidget ( mApparatus [ 1 ] , " Apparatus2 " ) ;
getWidget ( mApparatus [ 2 ] , " Apparatus3 " ) ;
getWidget ( mApparatus [ 3 ] , " Apparatus4 " ) ;
2012-05-25 14:20:57 +00:00
getWidget ( mEffectsBox , " CreatedEffects " ) ;
2012-05-25 16:00:13 +00:00
getWidget ( mNameEdit , " NameEdit " ) ;
2013-05-11 16:38:27 +00:00
getWidget ( mItemView , " ItemView " ) ;
2013-05-15 15:54:18 +00:00
2013-05-11 16:38:27 +00:00
mItemView - > eventItemClicked + = MyGUI : : newDelegate ( this , & AlchemyWindow : : onSelectedItem ) ;
2012-05-25 10:58:28 +00:00
2012-10-02 08:29:47 +00:00
mIngredients [ 0 ] - > eventMouseButtonClick + = MyGUI : : newDelegate ( this , & AlchemyWindow : : onIngredientSelected ) ;
mIngredients [ 1 ] - > eventMouseButtonClick + = MyGUI : : newDelegate ( this , & AlchemyWindow : : onIngredientSelected ) ;
mIngredients [ 2 ] - > eventMouseButtonClick + = MyGUI : : newDelegate ( this , & AlchemyWindow : : onIngredientSelected ) ;
mIngredients [ 3 ] - > eventMouseButtonClick + = MyGUI : : newDelegate ( this , & AlchemyWindow : : onIngredientSelected ) ;
2012-05-25 10:58:28 +00:00
mCreateButton - > eventMouseButtonClick + = MyGUI : : newDelegate ( this , & AlchemyWindow : : onCreateButtonClicked ) ;
mCancelButton - > eventMouseButtonClick + = MyGUI : : newDelegate ( this , & AlchemyWindow : : onCancelButtonClicked ) ;
2012-05-24 13:57:23 +00:00
center ( ) ;
}
2012-05-25 10:58:28 +00:00
void AlchemyWindow : : onCancelButtonClicked ( MyGUI : : Widget * _sender )
{
2014-05-27 03:13:37 +00:00
exit ( ) ;
2012-05-25 10:58:28 +00:00
}
void AlchemyWindow : : onCreateButtonClicked ( MyGUI : : Widget * _sender )
{
2015-01-10 00:00:52 +00:00
MWMechanics : : Alchemy : : Result result = mAlchemy - > create ( mNameEdit - > getCaption ( ) ) ;
2017-07-10 11:48:00 +00:00
MWBase : : WindowManager * winMgr = MWBase : : Environment : : get ( ) . getWindowManager ( ) ;
2012-10-18 20:21:39 +00:00
2016-02-29 12:25:09 +00:00
switch ( result )
2012-05-25 16:00:13 +00:00
{
2016-02-29 12:25:09 +00:00
case MWMechanics : : Alchemy : : Result_NoName :
2017-07-10 11:48:00 +00:00
winMgr - > messageBox ( " #{sNotifyMessage37} " ) ;
2016-02-29 12:25:09 +00:00
break ;
case MWMechanics : : Alchemy : : Result_NoMortarAndPestle :
2017-07-10 11:48:00 +00:00
winMgr - > messageBox ( " #{sNotifyMessage45} " ) ;
2016-02-29 12:25:09 +00:00
break ;
case MWMechanics : : Alchemy : : Result_LessThanTwoIngredients :
2017-07-10 11:48:00 +00:00
winMgr - > messageBox ( " #{sNotifyMessage6a} " ) ;
2016-02-29 12:25:09 +00:00
break ;
case MWMechanics : : Alchemy : : Result_Success :
2017-07-10 11:48:00 +00:00
winMgr - > messageBox ( " #{sPotionSuccess} " ) ;
winMgr - > playSound ( " potion success " ) ;
2017-07-26 02:36:12 +00:00
/*
Start of tes3mp addition
Include a messagebox notifying players that player - made potions are not synced yet
*/
MWBase : : Environment : : get ( ) . getWindowManager ( ) - > messageBox ( " Player-made potions are not synchronized in multiplayer yet and they will not show up for other players. " ) ;
/*
End of tes3mp addition
*/
2016-02-29 12:25:09 +00:00
break ;
case MWMechanics : : Alchemy : : Result_NoEffects :
case MWMechanics : : Alchemy : : Result_RandomFailure :
2017-07-10 11:48:00 +00:00
winMgr - > messageBox ( " #{sNotifyMessage8} " ) ;
winMgr - > playSound ( " potion fail " ) ;
2016-02-29 12:25:09 +00:00
break ;
2012-05-25 16:00:13 +00:00
}
2016-02-29 12:25:09 +00:00
// remove ingredient slots that have been fully used up
2012-10-02 08:29:47 +00:00
for ( int i = 0 ; i < 4 ; + + i )
if ( mIngredients [ i ] - > isUserString ( " ToolTipType " ) )
{
MWWorld : : Ptr ingred = * mIngredients [ i ] - > getUserData < MWWorld : : Ptr > ( ) ;
if ( ingred . getRefData ( ) . getCount ( ) = = 0 )
removeIngredient ( mIngredients [ i ] ) ;
2015-08-18 01:32:23 +00:00
}
2012-10-02 08:29:47 +00:00
2012-05-25 16:00:13 +00:00
update ( ) ;
2012-05-25 10:58:28 +00:00
}
2012-05-25 13:24:33 +00:00
void AlchemyWindow : : open ( )
{
2015-08-21 09:12:39 +00:00
mAlchemy - > setAlchemist ( MWMechanics : : getPlayer ( ) ) ;
2013-11-14 13:41:10 +00:00
2015-08-21 09:12:39 +00:00
InventoryItemModel * model = new InventoryItemModel ( MWMechanics : : getPlayer ( ) ) ;
2013-05-15 15:54:18 +00:00
mSortModel = new SortFilterItemModel ( model ) ;
mSortModel - > setFilter ( SortFilterItemModel : : Filter_OnlyIngredients ) ;
mItemView - > setModel ( mSortModel ) ;
2015-06-04 20:09:40 +00:00
mItemView - > resetScrollBars ( ) ;
2013-05-15 15:54:18 +00:00
2012-11-05 19:47:28 +00:00
mNameEdit - > setCaption ( " " ) ;
2012-09-30 17:05:45 +00:00
int index = 0 ;
2015-01-10 00:00:52 +00:00
for ( MWMechanics : : Alchemy : : TToolsIterator iter ( mAlchemy - > beginTools ( ) ) ;
iter ! = mAlchemy - > endTools ( ) & & index < static_cast < int > ( mApparatus . size ( ) ) ; + + iter , + + index )
2012-05-25 13:24:33 +00:00
{
2014-10-20 15:27:25 +00:00
mApparatus . at ( index ) - > setItem ( * iter ) ;
mApparatus . at ( index ) - > clearUserStrings ( ) ;
2012-09-30 17:05:45 +00:00
if ( ! iter - > isEmpty ( ) )
{
2012-10-02 08:20:49 +00:00
mApparatus . at ( index ) - > setUserString ( " ToolTipType " , " ItemPtr " ) ;
2017-03-13 01:47:52 +00:00
mApparatus . at ( index ) - > setUserData ( MWWorld : : Ptr ( * iter ) ) ;
2012-09-30 17:05:45 +00:00
}
2012-05-25 13:24:33 +00:00
}
2012-10-19 18:48:15 +00:00
update ( ) ;
2012-05-25 13:24:33 +00:00
}
2014-05-27 03:13:37 +00:00
void AlchemyWindow : : exit ( ) {
2015-01-10 00:00:52 +00:00
mAlchemy - > clear ( ) ;
2014-05-27 03:13:37 +00:00
MWBase : : Environment : : get ( ) . getWindowManager ( ) - > removeGuiMode ( GM_Alchemy ) ;
MWBase : : Environment : : get ( ) . getWindowManager ( ) - > removeGuiMode ( GM_Inventory ) ;
}
2012-05-25 13:24:33 +00:00
void AlchemyWindow : : onIngredientSelected ( MyGUI : : Widget * _sender )
{
2012-05-25 16:00:13 +00:00
removeIngredient ( _sender ) ;
2012-05-25 13:24:33 +00:00
update ( ) ;
}
2013-05-11 16:38:27 +00:00
void AlchemyWindow : : onSelectedItem ( int index )
2012-05-25 13:24:33 +00:00
{
2013-05-11 16:38:27 +00:00
MWWorld : : Ptr item = mSortModel - > getItem ( index ) . mBase ;
2015-01-10 00:00:52 +00:00
int res = mAlchemy - > addIngredient ( item ) ;
2012-10-18 20:21:39 +00:00
2012-10-19 18:44:53 +00:00
if ( res ! = - 1 )
2012-05-25 13:24:33 +00:00
{
update ( ) ;
2012-05-25 14:20:57 +00:00
2014-05-22 18:37:22 +00:00
std : : string sound = item . getClass ( ) . getUpSoundId ( item ) ;
2017-07-10 11:48:00 +00:00
MWBase : : Environment : : get ( ) . getWindowManager ( ) - > playSound ( sound ) ;
2012-05-25 13:24:33 +00:00
}
}
void AlchemyWindow : : update ( )
{
2015-01-10 00:00:52 +00:00
std : : string suggestedName = mAlchemy - > suggestPotionName ( ) ;
2014-10-20 15:28:22 +00:00
if ( suggestedName ! = mSuggestedPotionName )
mNameEdit - > setCaptionWithReplacing ( suggestedName ) ;
mSuggestedPotionName = suggestedName ;
2013-05-11 16:38:27 +00:00
mSortModel - > clearDragItems ( ) ;
2015-01-10 00:00:52 +00:00
MWMechanics : : Alchemy : : TIngredientsIterator it = mAlchemy - > beginIngredients ( ) ;
2012-05-25 13:24:33 +00:00
for ( int i = 0 ; i < 4 ; + + i )
{
2014-06-05 20:13:18 +00:00
ItemWidget * ingredient = mIngredients [ i ] ;
2012-05-25 13:24:33 +00:00
2012-10-19 18:48:15 +00:00
MWWorld : : Ptr item ;
2015-01-10 00:00:52 +00:00
if ( it ! = mAlchemy - > endIngredients ( ) )
2012-10-19 18:48:15 +00:00
{
item = * it ;
+ + it ;
}
2012-05-25 13:24:33 +00:00
2013-05-11 16:38:27 +00:00
if ( ! item . isEmpty ( ) )
mSortModel - > addDragItem ( item , item . getRefData ( ) . getCount ( ) ) ;
2012-05-25 13:24:33 +00:00
if ( ingredient - > getChildCount ( ) )
MyGUI : : Gui : : getInstance ( ) . destroyWidget ( ingredient - > getChildAt ( 0 ) ) ;
2012-10-19 18:44:53 +00:00
ingredient - > clearUserStrings ( ) ;
2014-06-05 20:13:18 +00:00
ingredient - > setItem ( item ) ;
2012-10-19 18:44:53 +00:00
if ( item . isEmpty ( ) )
continue ;
ingredient - > setUserString ( " ToolTipType " , " ItemPtr " ) ;
2017-03-13 01:47:52 +00:00
ingredient - > setUserData ( MWWorld : : Ptr ( item ) ) ;
2012-10-19 18:44:53 +00:00
2017-03-13 01:47:52 +00:00
ingredient - > setCount ( item . getRefData ( ) . getCount ( ) ) ;
2012-05-25 13:24:33 +00:00
}
2012-05-25 14:20:57 +00:00
2013-05-11 16:38:27 +00:00
mItemView - > update ( ) ;
2012-10-19 18:44:53 +00:00
2015-01-10 00:00:52 +00:00
std : : set < MWMechanics : : EffectKey > effectIds = mAlchemy - > listEffects ( ) ;
2014-09-19 07:10:46 +00:00
Widgets : : SpellEffectList list ;
2016-02-02 21:49:48 +00:00
unsigned int effectIndex = 0 ;
2016-10-30 15:23:51 +00:00
for ( std : : set < MWMechanics : : EffectKey > : : iterator it2 = effectIds . begin ( ) ; it2 ! = effectIds . end ( ) ; + + it2 )
2012-05-25 14:20:57 +00:00
{
2014-09-19 07:10:46 +00:00
Widgets : : SpellEffectParams params ;
2016-10-30 15:23:51 +00:00
params . mEffectID = it2 - > mId ;
const ESM : : MagicEffect * magicEffect = MWBase : : Environment : : get ( ) . getWorld ( ) - > getStore ( ) . get < ESM : : MagicEffect > ( ) . find ( it2 - > mId ) ;
2014-09-19 07:10:46 +00:00
if ( magicEffect - > mData . mFlags & ESM : : MagicEffect : : TargetSkill )
2016-10-30 15:23:51 +00:00
params . mSkill = it2 - > mArg ;
2014-09-19 07:10:46 +00:00
else if ( magicEffect - > mData . mFlags & ESM : : MagicEffect : : TargetAttribute )
2016-10-30 15:23:51 +00:00
params . mAttribute = it2 - > mArg ;
2014-09-19 07:10:46 +00:00
params . mIsConstant = true ;
params . mNoTarget = true ;
2016-02-02 21:49:48 +00:00
params . mKnown = mAlchemy - > knownEffect ( effectIndex , MWBase : : Environment : : get ( ) . getWorld ( ) - > getPlayerPtr ( ) ) ;
2014-09-19 07:10:46 +00:00
list . push_back ( params ) ;
2016-02-02 21:49:48 +00:00
+ + effectIndex ;
2012-05-25 14:20:57 +00:00
}
while ( mEffectsBox - > getChildCount ( ) )
MyGUI : : Gui : : getInstance ( ) . destroyWidget ( mEffectsBox - > getChildAt ( 0 ) ) ;
MyGUI : : IntCoord coord ( 0 , 0 , mEffectsBox - > getWidth ( ) , 24 ) ;
Widgets : : MWEffectListPtr effectsWidget = mEffectsBox - > createWidget < Widgets : : MWEffectList >
2012-09-10 07:10:50 +00:00
( " MW_StatName " , coord , MyGUI : : Align : : Left | MyGUI : : Align : : Top ) ;
2012-10-18 20:21:39 +00:00
2014-09-19 07:10:46 +00:00
effectsWidget - > setEffectList ( list ) ;
2012-05-25 14:20:57 +00:00
2013-03-03 12:11:02 +00:00
std : : vector < MyGUI : : Widget * > effectItems ;
2012-05-25 14:20:57 +00:00
effectsWidget - > createEffectWidgets ( effectItems , mEffectsBox , coord , false , 0 ) ;
effectsWidget - > setCoord ( coord ) ;
2012-05-25 13:24:33 +00:00
}
2012-05-25 16:00:13 +00:00
void AlchemyWindow : : removeIngredient ( MyGUI : : Widget * ingredient )
{
2012-10-18 20:21:39 +00:00
for ( int i = 0 ; i < 4 ; + + i )
if ( mIngredients [ i ] = = ingredient )
2015-01-10 00:00:52 +00:00
mAlchemy - > removeIngredient ( i ) ;
2012-10-19 18:44:53 +00:00
update ( ) ;
2012-05-25 16:00:13 +00:00
}
2012-05-24 13:57:23 +00:00
}