| 1 |
#include <iostream> |
| 2 |
#include <sstream> |
| 3 |
|
| 4 |
#include "oodle.h" |
| 5 |
// For compilers that support precompilation, includes "wx.h". |
| 6 |
#include "wx/wxprec.h" |
| 7 |
|
| 8 |
#ifdef __BORLANDC__ |
| 9 |
#pragma hdrstop |
| 10 |
#endif |
| 11 |
|
| 12 |
#ifndef WX_PRECOMP |
| 13 |
#include "wx/wx.h" |
| 14 |
#endif |
| 15 |
|
| 16 |
using namespace std; |
| 17 |
|
| 18 |
CSimpleGame g(150, 150, 32, false); |
| 19 |
CSprite *sprite = NULL; |
| 20 |
|
| 21 |
CColor colors[] = { CColor(), CColor(255,255,255), CColor(255), CColor(0,255), CColor(0,0,255), CColor(255,255) }; |
| 22 |
|
| 23 |
class MyApp : public wxApp |
| 24 |
{ |
| 25 |
public: |
| 26 |
virtual bool OnInit(); |
| 27 |
|
| 28 |
}; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class MyFrame : public wxFrame |
| 33 |
{ |
| 34 |
public: |
| 35 |
|
| 36 |
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); |
| 37 |
virtual ~MyFrame(); |
| 38 |
|
| 39 |
void DoBegin(); |
| 40 |
void DoPause(); |
| 41 |
void DoStop(); |
| 42 |
|
| 43 |
void OnIdle(wxTimerEvent& WXUNUSED(event)); |
| 44 |
void OnQuit(wxCommandEvent& event); |
| 45 |
void OnOpen(wxCommandEvent& event); |
| 46 |
void OnScroll(wxScrollEvent& event); |
| 47 |
void OnPlay(wxCommandEvent& event); |
| 48 |
void OnBegin(wxCommandEvent& event); |
| 49 |
void OnEnd(wxCommandEvent& event); |
| 50 |
void OnPause(wxCommandEvent& event); |
| 51 |
void OnStop(wxCommandEvent& event); |
| 52 |
void OnSpeed(wxCommandEvent& event); |
| 53 |
void OnLoop(wxCommandEvent& event); |
| 54 |
void OnColor(wxCommandEvent& event); |
| 55 |
|
| 56 |
DECLARE_EVENT_TABLE() |
| 57 |
|
| 58 |
enum |
| 59 |
{ |
| 60 |
ID_Quit = 1, |
| 61 |
ID_Open, |
| 62 |
ID_Controls_Begin, |
| 63 |
ID_Controls_End, |
| 64 |
ID_Controls_Play, |
| 65 |
ID_Controls_Pause, |
| 66 |
ID_Controls_Stop, |
| 67 |
ID_Speed_Text, |
| 68 |
ID_Loop, |
| 69 |
ID_Color, |
| 70 |
ID_TIMER |
| 71 |
}; |
| 72 |
|
| 73 |
wxScrollBar *scroll; |
| 74 |
wxTimer* m_timer; |
| 75 |
wxChoice* loop, *color; |
| 76 |
wxTextCtrl* speedctl; |
| 77 |
}; |
| 78 |
|
| 79 |
BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
| 80 |
EVT_MENU(ID_Quit, MyFrame::OnQuit) |
| 81 |
EVT_MENU(ID_Open, MyFrame::OnOpen) |
| 82 |
EVT_SCROLL(MyFrame::OnScroll) |
| 83 |
EVT_BUTTON(ID_Controls_Play, MyFrame::OnPlay) |
| 84 |
EVT_TIMER(ID_TIMER, MyFrame::OnIdle) |
| 85 |
EVT_BUTTON(ID_Controls_Begin, MyFrame::OnBegin) |
| 86 |
EVT_BUTTON(ID_Controls_Pause, MyFrame::OnPause) |
| 87 |
EVT_BUTTON(ID_Controls_Stop, MyFrame::OnStop) |
| 88 |
EVT_BUTTON(ID_Controls_End, MyFrame::OnEnd) |
| 89 |
EVT_TEXT(ID_Speed_Text, MyFrame::OnSpeed) |
| 90 |
EVT_CHOICE(ID_Color, MyFrame::OnColor) |
| 91 |
EVT_CHOICE(ID_Loop, MyFrame::OnLoop) |
| 92 |
END_EVENT_TABLE() |
| 93 |
|
| 94 |
|
| 95 |
IMPLEMENT_APP(MyApp) |
| 96 |
|
| 97 |
bool MyApp::OnInit() |
| 98 |
{ |
| 99 |
//g.addSprite(new CSprite(CVector(0,0), CVector(0,0), "block1.bmp", "static", CRect(0,0,0,0), false) ); |
| 100 |
MyFrame *frame = new MyFrame( "Animation Viewer", wxDefaultPosition, wxSize(400,145) ); |
| 101 |
frame->Show(TRUE); |
| 102 |
SetTopWindow(frame); |
| 103 |
|
| 104 |
return TRUE; |
| 105 |
} |
| 106 |
MyFrame::~MyFrame() |
| 107 |
{ |
| 108 |
delete m_timer; |
| 109 |
// delete scroll; |
| 110 |
} |
| 111 |
|
| 112 |
void MyFrame::OnLoop(wxCommandEvent& event) |
| 113 |
{ |
| 114 |
if (sprite) |
| 115 |
if (event.GetSelection() == 1) |
| 116 |
{ |
| 117 |
sprite->getCurrentAnimation()->setLoop(false); |
| 118 |
} |
| 119 |
else |
| 120 |
{ |
| 121 |
sprite->getCurrentAnimation()->setLoop(true); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
void MyFrame::OnColor(wxCommandEvent& event) |
| 126 |
{ |
| 127 |
g.setBGColor( colors[event.GetSelection()] ); |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
void MyFrame::OnSpeed(wxCommandEvent& event) |
| 133 |
{ |
| 134 |
if (sprite) |
| 135 |
{ |
| 136 |
long s = 0; |
| 137 |
speedctl->GetValue().ToLong(&s); |
| 138 |
sprite->getCurrentAnimation()->setSpeed( (int)s ); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
void MyFrame::OnScroll(wxScrollEvent& event) |
| 143 |
{ |
| 144 |
ostringstream s; |
| 145 |
s << "Frame " << event.GetPosition()+1 << "/" << scroll->GetRange(); |
| 146 |
SetStatusText(s.str().c_str()); |
| 147 |
if (sprite) |
| 148 |
sprite->getCurrentAnimation()->setFrame(event.GetPosition()); |
| 149 |
} |
| 150 |
void MyFrame::OnIdle(wxTimerEvent& e) |
| 151 |
{ |
| 152 |
g.update(); |
| 153 |
if(sprite) |
| 154 |
{ |
| 155 |
scroll->SetThumbPosition(sprite->getCurrentAnimation()->getFrameIndex()); |
| 156 |
ostringstream s; |
| 157 |
s << "Frame " << sprite->getCurrentAnimation()->getFrameIndex()+1 << "/" << scroll->GetRange(); |
| 158 |
SetStatusText(s.str().c_str()); |
| 159 |
} |
| 160 |
} |
| 161 |
void MyFrame::OnPlay(wxCommandEvent& event) |
| 162 |
{ |
| 163 |
bool t = m_timer->Start(2); |
| 164 |
if (sprite) sprite->getCurrentAnimation()->start(); |
| 165 |
} |
| 166 |
|
| 167 |
void MyFrame::OnEnd(wxCommandEvent& event) |
| 168 |
{ |
| 169 |
sprite->getCurrentAnimation()->setFrame(sprite->getCurrentAnimation()->getFrames()-1); |
| 170 |
DoPause(); |
| 171 |
|
| 172 |
//g.update(); |
| 173 |
//m_timer->Stop(); |
| 174 |
} |
| 175 |
|
| 176 |
void MyFrame::OnBegin(wxCommandEvent& event) |
| 177 |
{ |
| 178 |
DoBegin(); |
| 179 |
} |
| 180 |
|
| 181 |
void MyFrame::DoBegin() |
| 182 |
{ |
| 183 |
sprite->getCurrentAnimation()->setFrame(0); |
| 184 |
DoPause(); |
| 185 |
//g.update(); |
| 186 |
//m_timer->Stop(); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
void MyFrame::OnPause(wxCommandEvent& event) |
| 191 |
{ |
| 192 |
DoPause(); |
| 193 |
} |
| 194 |
|
| 195 |
void MyFrame::DoPause() |
| 196 |
{ |
| 197 |
sprite->getCurrentAnimation()->pause(); |
| 198 |
} |
| 199 |
|
| 200 |
void MyFrame::OnStop(wxCommandEvent& event) |
| 201 |
{ |
| 202 |
DoStop(); |
| 203 |
} |
| 204 |
|
| 205 |
void MyFrame::DoStop() |
| 206 |
{ |
| 207 |
DoBegin(); |
| 208 |
DoPause(); |
| 209 |
} |
| 210 |
|
| 211 |
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) |
| 212 |
: wxFrame((wxFrame *)NULL, -1, title, pos, size) |
| 213 |
{ |
| 214 |
wxMenu *menuFile = new wxMenu; |
| 215 |
m_timer = new wxTimer(this, ID_TIMER); |
| 216 |
|
| 217 |
menuFile->Append( ID_Open, "&Open animation..." ); |
| 218 |
menuFile->AppendSeparator(); |
| 219 |
menuFile->Append( ID_Quit, "E&xit" ); |
| 220 |
|
| 221 |
wxMenuBar *menuBar = new wxMenuBar; |
| 222 |
menuBar->Append( menuFile, "&File" ); |
| 223 |
|
| 224 |
SetMenuBar( menuBar ); |
| 225 |
SetStatusBarPane( -1 ); |
| 226 |
CreateStatusBar(); |
| 227 |
|
| 228 |
ostringstream s; |
| 229 |
s << "Frame " << 0 << "/" << 0; |
| 230 |
SetStatusText(s.str().c_str()); |
| 231 |
|
| 232 |
wxPanel *panel1 = new wxPanel(this, -1); |
| 233 |
/*wxPanel *panel2 = new wxPanel(panel1, -1); |
| 234 |
wxPanel *panel3 = new wxPanel(panel1, -1);*/ |
| 235 |
scroll = new wxScrollBar(panel1, -1, wxPoint(0,0), wxSize(-1,-1), wxSB_HORIZONTAL); |
| 236 |
|
| 237 |
wxBoxSizer *sizer1 = new wxBoxSizer(wxVERTICAL); |
| 238 |
wxBoxSizer *sizer2 = new wxBoxSizer(wxVERTICAL); |
| 239 |
wxBoxSizer *sizer3 = new wxBoxSizer(wxHORIZONTAL); |
| 240 |
wxBoxSizer *sizer4 = new wxBoxSizer(wxHORIZONTAL); |
| 241 |
|
| 242 |
sizer2->Add( scroll, 1, wxEXPAND , 4 ); |
| 243 |
sizer3->Add( new wxButton(panel1, ID_Controls_Begin, "<", wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT ), 0, wxEXPAND | wxALIGN_CENTER, 0 ); |
| 244 |
sizer3->Add( new wxButton(panel1, ID_Controls_Pause, "Pause", wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT ), 0, wxEXPAND | wxALIGN_CENTER, 0 ); |
| 245 |
sizer3->Add( new wxButton(panel1, ID_Controls_Play, "Play", wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT ), 0, wxEXPAND | wxALIGN_CENTER, 0 ); |
| 246 |
sizer3->Add( new wxButton(panel1, ID_Controls_Stop, "Stop", wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT ), 0, wxEXPAND | wxALIGN_CENTER, 0 ); |
| 247 |
sizer3->Add( new wxButton(panel1, ID_Controls_End, ">", wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT ), 0, wxEXPAND | wxALIGN_CENTER, 0 ); |
| 248 |
|
| 249 |
sizer4->Add( new wxStaticText(panel1, -1, "Speed: ", wxDefaultPosition),0, wxALIGN_CENTER_VERTICAL | wxLEFT, 5 ); |
| 250 |
speedctl = new wxTextCtrl(panel1, ID_Speed_Text, "30", wxDefaultPosition, wxSize(35, -1)); |
| 251 |
sizer4->Add(speedctl , 0, wxALIGN_CENTER, 0); |
| 252 |
sizer4->Add( new wxStaticText(panel1, -1, "Loop: ", wxDefaultPosition),0, wxALIGN_CENTER_VERTICAL | wxLEFT, 5 ); |
| 253 |
wxString strings[2] = {"Yes", "No"}; |
| 254 |
loop = new wxChoice(panel1, ID_Loop, wxDefaultPosition, wxDefaultSize, 2, strings); |
| 255 |
loop->SetSelection(0); |
| 256 |
sizer4->Add(loop , 0, wxALIGN_CENTER, 0); |
| 257 |
|
| 258 |
sizer4->Add( new wxStaticText(panel1, -1, "Color: ", wxDefaultPosition),0, wxALIGN_CENTER_VERTICAL | wxLEFT, 5 ); |
| 259 |
wxString strings2[6] = {"Black", "White", "Red", "Green", "Blue", "Yellow"}; |
| 260 |
color = new wxChoice(panel1, ID_Color, wxDefaultPosition, wxDefaultSize, 6, strings2); |
| 261 |
color->SetSelection(0); |
| 262 |
sizer4->Add(color , 0, wxALIGN_CENTER, 0); |
| 263 |
|
| 264 |
sizer1->Add( sizer3, 0, wxALIGN_CENTER, 0); |
| 265 |
sizer1->Add( sizer4, 0, wxALIGN_CENTER, 0); |
| 266 |
sizer1->Add( sizer2, 1,wxEXPAND | wxALIGN_CENTER, 0); |
| 267 |
|
| 268 |
panel1->SetSizer( sizer1 ); |
| 269 |
|
| 270 |
/*panel1->SetSizer( sizer1 ); |
| 271 |
panel1->SetAutoLayout( TRUE ); |
| 272 |
sizer1->Fit( panel1 );*/ |
| 273 |
|
| 274 |
/*panel2->SetSizer( sizer2 ); |
| 275 |
panel2->SetAutoLayout( TRUE ); |
| 276 |
sizer2->Fit( panel2 );*/ |
| 277 |
|
| 278 |
/*panel3->SetSizer( sizer3 ); |
| 279 |
panel3->SetAutoLayout( TRUE ); |
| 280 |
sizer3->Fit( panel3 );*/ |
| 281 |
|
| 282 |
scroll->SetScrollbar(0, 1, 1, 1, TRUE); |
| 283 |
|
| 284 |
bool t = m_timer->Start(2); |
| 285 |
|
| 286 |
} |
| 287 |
|
| 288 |
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
| 289 |
{ |
| 290 |
Close(TRUE); |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event)) |
| 295 |
{ |
| 296 |
try |
| 297 |
{ |
| 298 |
wxFileDialog f(this, "Choose animation file...", "", "", "Image files|*.bmp;*.png", wxOPEN); |
| 299 |
if (f.ShowModal() == wxID_OK) |
| 300 |
{ |
| 301 |
wxTextEntryDialog e1(this, "Tile width:", "", "0", wxOK); |
| 302 |
wxTextEntryDialog e2(this, "Tile height:", "", "0", wxOK); |
| 303 |
wxTextEntryDialog e3(this, "Padding:", "", "0", wxOK); |
| 304 |
wxTextEntryDialog e4(this, "Speed:", "", "0", wxOK); |
| 305 |
|
| 306 |
e1.ShowModal(); e2.ShowModal(); e3.ShowModal(); e4.ShowModal(); |
| 307 |
long width = 0; |
| 308 |
e1.GetValue().ToLong(&width); |
| 309 |
long height = 0; |
| 310 |
e2.GetValue().ToLong(&height); |
| 311 |
long padding = 0; |
| 312 |
e3.GetValue().ToLong(&padding); |
| 313 |
long speed = 0; |
| 314 |
e4.GetValue().ToLong(&speed); |
| 315 |
|
| 316 |
speedctl->Clear(); |
| 317 |
*speedctl << speed; |
| 318 |
|
| 319 |
wxString s = f.GetDirectory() + "/" + f.GetFilename(); |
| 320 |
g.removeSprite(sprite); |
| 321 |
delete sprite; |
| 322 |
if (width != 0 && height != 0) |
| 323 |
{ |
| 324 |
sprite = new CSprite(ZERO_VECTOR, ZERO_VECTOR, s.c_str(), "static", CRect(0,0,0,0), width, height, padding, speed, true, true); |
| 325 |
g.addSprite(sprite); |
| 326 |
} |
| 327 |
else |
| 328 |
{ |
| 329 |
sprite = new CSprite(ZERO_VECTOR, ZERO_VECTOR, s.c_str(), "static", CRect(0,0,0,0), true,true); |
| 330 |
g.addSprite(sprite); |
| 331 |
} |
| 332 |
|
| 333 |
g.resize(sprite->getWidth(), sprite->getHeight(), 32, false); |
| 334 |
|
| 335 |
SetStatusText("Loaded " + s); |
| 336 |
scroll->SetScrollbar(0, 1, sprite->getCurrentAnimation()->getFrames(), 1, TRUE); |
| 337 |
DoStop(); |
| 338 |
} |
| 339 |
} |
| 340 |
catch(ex_Generic e) |
| 341 |
{ |
| 342 |
wxMessageBox(e.what()); |
| 343 |
} |
| 344 |
} |
| 345 |
|