forked from gecube/opencaesar3
/
oc3_special_orders_window.cpp
255 lines (211 loc) · 8.25 KB
/
oc3_special_orders_window.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// This file is part of openCaesar3.
//
// openCaesar3 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 3 of the License, or
// (at your option) any later version.
//
// openCaesar3 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 openCaesar3. If not, see <http://www.gnu.org/licenses/>.
#include <cstdio>
#include "oc3_special_orders_window.hpp"
#include "oc3_gfx_engine.hpp"
#include "oc3_groupbox.hpp"
#include "oc3_gui_label.hpp"
#include "oc3_texturedbutton.hpp"
#include "oc3_gettext.hpp"
#include "oc3_picture_decorator.hpp"
#include "oc3_event.hpp"
#include "oc3_granary.hpp"
#include "oc3_building_warehouse.hpp"
#include "oc3_goodhelper.hpp"
#include "oc3_goodstore.hpp"
#include "oc3_goodorders.hpp"
template< class T >
class OrderGoodWidget : public Label
{
public:
OrderGoodWidget( Widget* parent, const Rect& rect, GoodType good, T storageBuilding )
: Label( parent, rect, "" )
{
_type = good;
_storageBuilding = storageBuilding;
setFont( Font::create( FONT_1_WHITE ) );
_btnChangeRule = new PushButton( this, Rect( 140, 0, 140 + 240, getHeight() ), "", -1, false, PushButton::blackBorderUp );
_btnChangeRule->setFont( Font::create( FONT_1_WHITE ) );
updateBtnText();
CONNECT( _btnChangeRule, onClicked(), this, OrderGoodWidget::changeGranaryRule );
}
virtual void _updateTexture( GfxEngine& painter )
{
Label::_updateTexture( painter );
Picture goodIcon = GoodHelper::getPicture( _type );
std::string goodName = GoodHelper::getName( _type );
if( getTextPicture() )
{
getTextPicture()->draw( goodIcon, 15, 0, false );
getTextPicture()->draw( goodIcon, 390, 0, false );
Font font = getFont();
font.draw( *getTextPicture(), goodName, 55, 0 );
}
}
void updateBtnText()
{
GoodOrders::Order rule = _storageBuilding->getGoodStore().getOrder( _type );
std::string ruleName[] = { _("##accept##"), _("##reject##"), _("##deliver##"), _("##none##") };
_btnChangeRule->setFont( Font::create( rule == GoodOrders::reject ? FONT_1_RED : FONT_1_WHITE ) );
_btnChangeRule->setText( ruleName[ rule ] );
}
void changeGranaryRule()
{
GoodOrders::Order rule = _storageBuilding->getGoodStore().getOrder( _type );
_storageBuilding->getGoodStore().setOrder( _type, GoodOrders::Order( (rule+1) % (GoodOrders::none)) );
updateBtnText();
}
private:
GoodType _type;
T _storageBuilding;
PushButton* _btnChangeRule;
};
class BaseSpecialOrdersWindow::Impl
{
public:
PictureRef bgPicture;
GroupBox* gbOrders;
Widget* gbOrdersInsideArea;
Label* lbTitle;
PushButton* btnExit;
PushButton* btnHelp;
PushButton* btnEmpty;
template< class T >
void addOrderWidget( const int index, const GoodType good, T storageBuiding )
{
Point offset( 0, 25 );
Size wdgSize( gbOrdersInsideArea->getWidth(), 25 );
new OrderGoodWidget<T>( gbOrdersInsideArea, Rect( offset * index, wdgSize), good, storageBuiding );
}
};
BaseSpecialOrdersWindow::BaseSpecialOrdersWindow( Widget* parent, const Point& pos )
: Widget( parent, -1, Rect( pos, Size( 510, 450 ) ) ), _d( new Impl )
{
// create the title
_d->lbTitle = new Label( this, Rect( 50, 10, getWidth()-50, 10 + 30 ), "", true );
_d->lbTitle->setFont( Font::create( FONT_3 ) );
_d->lbTitle->setTextAlignment( alignCenter, alignCenter );
_d->btnExit = new TexturedButton( this, Point( 472, getHeight() - 39 ), Size( 24 ), -1, ResourceMenu::exitInfBtnPicId );
_d->btnExit->setTooltipText( _("##infobox_tooltip_exit##") );
_d->btnHelp = new TexturedButton( this, Point( 14, getHeight() - 39 ), Size( 24 ), -1, ResourceMenu::helpInfBtnPicId );
_d->btnHelp->setTooltipText( _("##infobox_tooltip_help##") );
CONNECT( _d->btnExit, onClicked(), this, GranarySpecialOrdersWindow::deleteLater );
_d->bgPicture.reset( Picture::create( getSize() ) );
// draws the box and the inner black box
PictureDecorator::draw( *_d->bgPicture, Rect( Point( 0, 0 ), getSize() ), PictureDecorator::whiteFrame );
_d->gbOrders = new GroupBox( this, Rect( 17, 42, getWidth() - 17, getHeight() - 70), -1, GroupBox::blackFrame );
_d->gbOrdersInsideArea = new Widget( _d->gbOrders, -1, Rect( 5, 5, _d->gbOrders->getWidth() -5, _d->gbOrders->getHeight() -5 ) );
}
BaseSpecialOrdersWindow::~BaseSpecialOrdersWindow()
{
}
void BaseSpecialOrdersWindow::draw( GfxEngine& engine )
{
engine.drawPicture( *_d->bgPicture, getScreenLeft(), getScreenTop() );
Widget::draw( engine );
}
bool BaseSpecialOrdersWindow::isPointInside( const Point& point ) const
{
//resolve all screen for self using
return getParent()->getAbsoluteRect().isPointInside( point );
}
bool BaseSpecialOrdersWindow::onEvent( const NEvent& event)
{
switch( event.EventType )
{
case OC3_MOUSE_EVENT:
if( event.MouseEvent.Event == OC3_RMOUSE_LEFT_UP )
{
deleteLater();
return true;
}
else if( event.MouseEvent.Event == OC3_LMOUSE_LEFT_UP )
{
return true;
}
break;
default:
break;
}
return Widget::onEvent( event );
}
void BaseSpecialOrdersWindow::setTitle( const std::string& text )
{
_d->lbTitle->setText( text );
}
GranarySpecialOrdersWindow::GranarySpecialOrdersWindow( Widget* parent, const Point& pos, GranaryPtr granary )
: BaseSpecialOrdersWindow( parent, pos )
{
setTitle( _("##granary_orders##") );
int index=0;
_granary = granary;
for( int goodType=G_WHEAT; goodType <= G_VEGETABLE; goodType++ )
{
const GoodOrders::Order rule = granary->getGoodStore().getOrder( (GoodType)goodType );
if( rule != GoodOrders::none )
{
_d->addOrderWidget<GranaryPtr>( index, (GoodType)goodType, granary );
index++;
}
}
_btnToggleDevastation = new PushButton( this, Rect( 80, getHeight() - 45, getWidth() - 80, getHeight() - 25 ),
"", -1, false, PushButton::whiteBorderUp );
CONNECT( _btnToggleDevastation, onClicked(), this, GranarySpecialOrdersWindow::toggleDevastation );
_updateBtnDevastation();
}
void GranarySpecialOrdersWindow::toggleDevastation()
{
_granary->getGoodStore().setDevastation( !_granary->getGoodStore().isDevastation() );
_updateBtnDevastation();
}
void GranarySpecialOrdersWindow::_updateBtnDevastation()
{
_btnToggleDevastation->setText( _granary->getGoodStore().isDevastation()
? _("##stop_granary_devastation##")
: _("##devastate_granary##") );
}
WarehouseSpecialOrdersWindow::WarehouseSpecialOrdersWindow( Widget* parent, const Point& pos, WarehousePtr warehouse )
: BaseSpecialOrdersWindow( parent, pos )
{
setTitle( _("##warehouse_orders##") );
_warehouse = warehouse;
int index=0;
for( int goodType=G_WHEAT; goodType <= G_MARBLE; goodType++ )
{
const GoodOrders::Order rule = _warehouse->getGoodStore().getOrder( (GoodType)goodType );
if( rule != GoodOrders::none )
{
_d->addOrderWidget<WarehousePtr>( index, (GoodType)goodType, _warehouse );
index++;
}
}
_btnToggleDevastation = new PushButton( this, Rect( 80, getHeight() - 45, getWidth() - 80, getHeight() - 25 ),
"", -1, false, PushButton::whiteBorderUp );
_btnTradeCenter = new PushButton( this, Rect( 80, getHeight() - 70, getWidth() - 80, getHeight() - 50 ),
_("##Trace center##"), -1, false, PushButton::whiteBorderUp );
CONNECT( _btnToggleDevastation, onClicked(), this, WarehouseSpecialOrdersWindow::toggleDevastation );
_updateBtnDevastation();
}
void WarehouseSpecialOrdersWindow::toggleDevastation()
{
_warehouse->getGoodStore().setDevastation( !_warehouse->getGoodStore().isDevastation() );
_updateBtnDevastation();
}
void WarehouseSpecialOrdersWindow::_updateBtnDevastation()
{
_btnToggleDevastation->setText( _warehouse->getGoodStore().isDevastation()
? _("##stop_warehouse_devastation##")
: _("##devastate_warehouse##") );
}