1 /***************************************************************************
2 containergui.cpp - The container contents window
3 -------------------
4 begin : Sat May 3 2003
5 copyright : (C) 2003 by Gabor Torok
6 email : cctorok@yahoo.com
7 ***************************************************************************/
8
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18 #include "common/constants.h"
19 #include "containerview.h"
20 #include "containergui.h"
21 #include "render/renderlib.h"
22 #include "rpg/rpglib.h"
23 #include "sound.h"
24 #include "item.h"
25 #include "creature.h"
26 #include "shapepalette.h"
27 #include "pcui.h"
28
29 using namespace std;
30
31 // ###### MS Visual C++ specific ######
32 #if defined(_MSC_VER) && defined(_DEBUG)
33 # define new DEBUG_NEW
34 # undef THIS_FILE
35 static char THIS_FILE[] = __FILE__;
36 #endif
37
38 #define OFFSET_X 1
39 #define OFFSET_Y 0
40
ContainerGui(Scourge * scourge,int x,int y)41 ContainerGui::ContainerGui( Scourge *scourge, int x, int y ) {
42 this->scourge = scourge;
43
44 win = new Window( scourge->getSDLHandler(), x, y, 105, 40, "", scourge->getShapePalette()->getGuiTexture(), true, Window::BASIC_WINDOW, scourge->getShapePalette()->getGuiTexture2() );
45 openButton = new Button( 10, 5, 90, 25, scourge->getShapePalette()->getHighlightTexture(), Constants::getMessage( Constants::OPEN_CONTAINER_LABEL ) );
46 win->addWidget( ( Widget* )openButton );
47 infoButton = new Button( 10, 30, 90, 50, scourge->getShapePalette()->getHighlightTexture(), _( "Info" ) );
48 infoButton->setEnabled( false );
49 win->addWidget( ( Widget* )infoButton );
50 getAllButton = new Button( 10, 55, 90, 75, scourge->getShapePalette()->getHighlightTexture(), _( "Get All" ) );
51 win->addWidget( ( Widget* )getAllButton );
52 closeButton = new Button( 10, win->getHeight() - 55, 90, win->getHeight() - 35, scourge->getShapePalette()->getHighlightTexture(), _( "Close" ) );
53 win->addWidget( ( Widget* )closeButton );
54
55 view = new ContainerView( scourge, NULL, win, 95, 5 );
56 win->addWidget( (Canvas*)view );
57
58 win->registerEventHandler( this );
59 win->setRawEventHandler( this );
60 win->setVisible( true );
61 }
62
~ContainerGui()63 ContainerGui::~ContainerGui() {
64 delete win;
65 }
66
setContainer(Item * container)67 void ContainerGui::setContainer( Item *container ) {
68 this->container = container;
69 win->setTitle( container->getItemName() );
70 win->resize( 105 + container->getRpgItem()->getContainerWidth() * GRID_SIZE,
71 40 + container->getRpgItem()->getContainerHeight() * GRID_SIZE );
72 closeButton->move( 10, win->getHeight() - 55 );
73 view->setItem( container );
74 }
75
handleEvent(SDL_Event * event)76 bool ContainerGui::handleEvent( SDL_Event *event ) {
77 view->handleEvent( event );
78 return false;
79 }
80
handleEvent(Widget * widget,SDL_Event * event)81 bool ContainerGui::handleEvent( Widget *widget, SDL_Event *event ) {
82 if ( widget == win->closeButton || widget == closeButton ) {
83 win->setVisible( false );
84 // scourge->closeContainerGui( this );
85 } else if ( widget == infoButton ) {
86 if( view->getSelectedItem() ) {
87 view->showInfo( view->getSelectedItem() );
88 }
89 } else if ( widget == openButton ) {
90 if ( view->getSelectedItem() && view->getSelectedItem()->getRpgItem()->getType() == RpgItem::CONTAINER ) {
91 scourge->openContainerGui( view->getSelectedItem() );
92 }
93 } else if ( widget == getAllButton ) {
94 while ( view->getContainer()->getContainedItemCount() > 0 ) {
95 Item *item = view->getContainer()->getContainedItem( 0 );
96 // try to add it
97 //if( scourge->getParty()->getPlayer()->addToBackpack( item ) ) {
98 scourge->getPcUi()->setCreature( scourge->getParty()->getPlayer() );
99 if ( scourge->getPcUi()->addToBackpack( item ) ) {
100 if( item == view->getSelectedItem() ) {
101 view->setSelectedItem( NULL );
102 }
103 view->getContainer()->removeContainedItem( item );
104 } else {
105 scourge->showMessageDialog( _( "There is not enough room in your backpack for everything." ) );
106 break;
107 }
108 }
109 scourge->getPcUi()->refresh();
110 view->refresh();
111 } else {
112 bool b = view->handleEvent( widget, event );
113 infoButton->setEnabled( view->getSelectedItem() != NULL );
114 return b;
115 }
116 return false;
117 }
118
refresh()119 void ContainerGui::refresh() {
120 view->refresh();
121 }
122
getSelectedItem()123 Item *ContainerGui::getSelectedItem() {
124 return view->getSelectedItem();
125 }
126