fix compile against newer giflib
[rofl0r-obeditor.git] / src / entity__wListAnims.cpp
blob54c7c7171cf7f7f5d8b53275afa849fabded63cc
1 /*
2 * wListAnims.cpp
4 * Created on: 6 nov. 2008
5 * Author: pat
6 */
8 #include <wx/wxprec.h>
9 #ifndef WX_PRECOMP
10 #include <wx/wx.h>
11 #endif
13 #include <list>
14 using namespace std;
16 #include "ob_editor.h"
17 #include "entity__wListAnims.h"
18 #include "entity__enums.h"
21 //****************************************************
22 //*********** new event
23 DEFINE_EVENT_TYPE(wxEVT_ANIM_LIST_CHANGE)
24 DEFINE_EVENT_TYPE(wxEVT_ANIM_SELECTED_CHANGE)
28 //****************************************************
29 //****************************************************
31 myListBox::myListBox(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, int n,
32 const wxString choices[], long style, const wxValidator& validator, const wxString& name)
33 :wxListBox(parent, id, pos, size, n, choices, style, validator, name)
37 void myListBox::OnCharPress(wxKeyEvent& event)
39 int kc = event.GetKeyCode();
40 wxChar ch = event.GetKeyCode();
41 wxString temp = ch;
42 temp = temp.Upper();
43 ch = temp[0];
45 wxCommandEvent _event;
46 if( kc == WXK_DOWN )
48 int new_ind = (GetSelection() + 1 < (int)GetCount() ) ? GetSelection() + 1 : GetCount() -1;
49 SetSelection(new_ind );
50 OnSelectionChange( _event );
51 return;
53 if( kc == WXK_UP )
55 int new_ind = (GetSelection() - 1 >= 0 ) ? GetSelection() - 1 : 0;
56 SetSelection( new_ind );
57 OnSelectionChange( _event );
58 return;
60 if( kc == WXK_PAGEUP )
62 int new_ind = (GetSelection() - 10 >= 0 ) ? GetSelection() - 10 : 0;
63 SetSelection( new_ind );
64 OnSelectionChange( _event );
65 return;
67 if( kc == WXK_PAGEDOWN )
69 int new_ind = (GetSelection() + 10 < (int)GetCount() ) ? GetSelection() + 10 : GetCount() -1;
70 SetSelection( new_ind );
71 OnSelectionChange( _event );
72 return;
75 for( size_t i = 0; i < GetCount();i++)
77 wxString temp = GetString(i).Upper();
78 wxChar t = temp[0];
80 if( t == ch )
82 SetSelection(i);
83 OnSelectionChange( _event );
84 return;
87 event.Skip();
91 void myListBox::OnSelectionChange( wxCommandEvent& event )
93 event.Skip();
96 void myListBox::EvtGetFocus(wxFocusEvent& event )
98 event.Skip();
102 //****************************************************
103 //*********** new event
105 // Event table
106 BEGIN_EVENT_TABLE(myListBox, wxListBox)
107 EVT_SET_FOCUS(myListBox::EvtGetFocus)
108 EVT_CHAR(myListBox::OnCharPress)
109 END_EVENT_TABLE()
113 //****************************************************
114 // LISTBOX ANIMS
115 //****************************************************
117 wListAnims::wListAnims(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, int n,
118 const wxString choices[], long style, const wxValidator& validator, const wxString& name)
119 :myListBox(parent, id, pos, size, n, choices, style, validator, name)
121 last_session_anim = wxString();
122 loading = false;
123 wxSize _t(200, 400 );
124 SetMinSize( _t );
125 arr_anims = NULL;
126 arr_anims_count = 0;
127 wList_frames = NULL;
130 //*************************************************************************
132 void wListAnims::AssociateToListFrame( wxWindow* _wList_frames)
134 wList_frames = _wList_frames;
138 //*************************************************************************
140 void wListAnims::ReloadLastSessionAnim()
142 // Load the last anim for this entity
143 last_session_anim = ConfigEdit_Read( wxT("last_anim_edited") );
146 //*************************************************************************
148 wListAnims::~wListAnims()
150 if( arr_anims != NULL )
151 delete[] arr_anims;
152 arr_anims = NULL;
153 arr_anims_count = 0;
157 //*************************************************************************
159 void wListAnims::RefreshSelection()
163 //*************************************************************************
164 /* Refresh the list of anims from the previously loaded arr_anims.
165 * Also restore the previous anim selected, if possible
166 * Finaly refresh the client zone.
168 void wListAnims::Refresh_List( bool b_keep_current_selection )
170 if( entity == NULL || loading )
171 return;
173 loading = true;
175 wxString prev_selected = GetStringSelection();
177 // First valid refresh
178 if( last_session_anim != wxString() )
180 prev_selected = last_session_anim;
181 last_session_anim = wxString();
183 else
184 prev_selected = last_anim_selected;
186 // Clear Previous content
187 if( GetCount() > 0 )
188 Clear();
190 // Sort the Anims by Names
191 list<int> l_order;
192 for( int i = 0; i < arr_anims_count; i ++)
194 wxString curr_anim_name = arr_anims[i]->GetToken(0);
196 list<int>::iterator it (l_order.begin())
197 , it_end( (l_order.end()) );
198 for( ; it != it_end; it++ )
200 wxString anim_name = arr_anims[*it]->GetToken(0);
202 if( P_StrCmp( curr_anim_name, anim_name ) < 0 )
203 break;
205 l_order.insert( it, i );
208 // Fill the listBox items
209 int i = 0;
210 while( l_order.empty() == false )
212 int curr_ind = l_order.front();
213 l_order.pop_front();
214 map_ind_numAnim[i] = curr_ind;
215 Append( arr_anims[curr_ind]->GetToken(0) );
216 i++;
219 loading = false;
221 // Try to restore the last edited anim
222 int _ind = 0;
223 if( arr_anims_count != 0 )
225 if( b_keep_current_selection )
227 if( prev_selected != wxString() )
229 for( int i = 0; i < arr_anims_count; i ++)
230 if( arr_anims[i]->GetToken(0) == prev_selected )
232 _ind = i;
233 break;
237 else
239 for( int i = 0; i < arr_anims_count; i ++)
241 if( arr_anims[i] == curr_anim )
243 _ind = i;
244 break;
248 curr_anim = arr_anims[_ind];
249 last_anim_selected = curr_anim->GetToken( 0 );
251 else
253 if( arr_anims_count == 0 )
255 curr_anim = NULL;
256 last_anim_selected = wxString();
258 else
260 curr_anim = arr_anims[0];
261 last_anim_selected = curr_anim->GetToken( 0 );
265 if( _ind < 0 || _ind >= arr_anims_count || curr_anim == NULL )
266 Select( wxNOT_FOUND );
267 else
268 pSetSelection(curr_anim->arr_token[0] );
270 ProcessSelectionChange();
271 Refresh();
274 //*************************************************************************
275 void
276 wListAnims::OnSelectionChange( wxCommandEvent& event )
278 if( loading )
279 return;
280 ProcessSelectionChange();
281 wxCommandEvent _evt(wxEVT_ANIM_SELECTED_CHANGE);
282 ProcessEvent( _evt);
285 //*************************************************************************
286 void
287 wListAnims::ProcessSelectionChange()
289 if( loading )
290 return;
292 int ind = GetSelection();
293 curr_anim = NULL;
294 last_anim_selected = wxString();
295 if( map_ind_numAnim[ind] >= 0 && map_ind_numAnim[ind] < (int) GetCount() )
297 curr_anim = arr_anims[map_ind_numAnim[ind]];
298 last_anim_selected = curr_anim->GetToken( 0 );
301 if( entityFrame->May_Register_ControlsState() == true )
303 // entityFrame->hist_anim_selected = GetStringSelection();
304 entityFrame->Register_ControlsState_inHistory(wxT("ProcessSelectionChange"));
308 //*************************************************************************
309 bool
310 wListAnims::pSetSelection( const wxString& s )
313 wxArrayString arr = GetStrings();
314 for( size_t i = 0; i < arr.GetCount(); i++ )
316 if( arr[i] == s )
318 SetSelection( i );
319 return true;
322 return false;
326 //*************************************************************************
327 void
328 wListAnims::EvtGetFocus(wxFocusEvent& event )
330 event.Skip();
334 //*************************************************************************
336 void wListAnims::EvtCharPress(wxKeyEvent& event)
338 int kc = event.GetKeyCode();
340 if( wList_frames != NULL )
342 // Resend left and right events to the list of anims
343 if( kc == WXK_LEFT || kc == WXK_RIGHT || kc == WXK_HOME || kc == WXK_END)
345 wList_frames->ProcessEvent( event );
346 return;
350 event.Skip();
354 //*************************************************************************
355 //*************************************************************************
356 //*************************************************************************
357 //*************************************************************************
358 //*************************************************************************
360 // Event table
361 BEGIN_EVENT_TABLE(wListAnims, myListBox)
362 EVT_LISTBOX(wxID_ANY,wListAnims::OnSelectionChange)
363 EVT_SET_FOCUS(wListAnims::EvtGetFocus)
364 EVT_CHAR(wListAnims::EvtCharPress)
365 END_EVENT_TABLE()
369 //*************************************************************************
370 //*************************************************************************
371 //*************************************************************************
372 //*************************************************************************
374 wListAnims_Editable::wListAnims_Editable(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, int n,
375 const wxString choices[], long style, const wxValidator& validator, const wxString& name)
376 :wListAnims(parent, id, pos, size, n, choices, style, validator, name)
378 popMenu = new wxMenu();
379 popMenu->Append( CMD_NEW, wxT("New Anim" ));
380 popMenu->Append( CMD_DEL, wxT("Delete Anim") );
381 popMenu->Append( CMD_COPY, wxT("Copy Anim" ));
382 popMenu->Append( CMD_RENAME, wxT("Rename Anim") );
386 //*************************************************************************
388 wListAnims_Editable::~wListAnims_Editable()
390 delete popMenu;
393 //*************************************************************************
395 void wListAnims_Editable::EvtRenameAnim(wxCommandEvent& event )
397 int indanim = GetSelection();
398 if( indanim < 0 || indanim > arr_anims_count )
399 return;
401 // Get the elt under the mouse
402 wxTextEntryDialog *_dialog = new wxTextEntryDialog(this, wxT("Rename Anim"), wxT("New name for the anim "), wxT("UNNAMED"),
403 wxOK | wxCANCEL | wxCENTRE );
404 int response = _dialog->ShowModal();
406 if( response == wxID_OK )
408 wxString animname = _dialog->GetValue().Trim().Trim(false);
409 if( animname == wxString())
410 wxMessageBox( wxT("Anim name is empty"), wxT("Error"), wxOK | wxICON_INFORMATION, this );
411 else
413 arr_anims[indanim]->SetToken(0 , animname );
414 SetString(indanim, animname);
416 // Mark entity as changed
417 entity->SetChanged();
423 //*************************************************************************
425 void wListAnims_Editable::EvtNewAnim( wxCommandEvent& event )
427 // int indanim = GetSelection();
429 wxTextEntryDialog _dialog(this, wxT("New anim"), wxT("Name for the new anim "), wxT("UNNAMED"),
430 wxOK | wxCANCEL | wxCENTRE );
431 int response = _dialog.ShowModal();
433 if( response == wxID_OK )
435 wxString animname = _dialog.GetValue().Trim().Trim(false);
436 if( animname == wxString())
437 wxMessageBox( wxT("Anim name is empty"), wxT("Error"), wxOK | wxICON_INFORMATION, this );
438 else
440 theHistoryManager.Set_State( false );
441 ob_anim* newanim = new ob_anim();
442 newanim->SetName(wxT("anim"));
443 newanim->SetToken(0 ,animname );
444 theHistoryManager.Set_State( true );
446 theHistoryManager.GroupStart( wxT("New Anim ") + animname );
447 entity->obj_container->Add_SubObj( newanim );
448 theHistoryManager.GroupEnd();
450 // Post the change to the parent
451 wxCommandEvent _evt( wxEVT_ANIM_LIST_CHANGE );
452 GetParent()->ProcessEvent( _evt );
454 // Refresh the list
455 Refresh_List();
457 // now the list should be updated => so select the newly created anim
458 Select( FindString( animname ));
459 wxCommandEvent event;
460 OnSelectionChange(event);
462 // Mark entity as changed
463 entity->SetChanged();
469 //*************************************************************************
471 void wListAnims_Editable::EvtCopyAnim(wxCommandEvent& event )
473 int indanim = GetSelection();
474 if( indanim < 0 || indanim > arr_anims_count )
475 return;
477 // Get the elt under the mouse
478 wxTextEntryDialog *_dialog = new wxTextEntryDialog(this, wxT("Anim copy"), wxT("Name for the copied anim "), wxT("UNNAMED"),
479 wxOK | wxCANCEL | wxCENTRE );
480 int response = _dialog->ShowModal();
482 if( response == wxID_OK )
484 wxString animname = _dialog->GetValue().Trim().Trim(false);
485 if( animname == wxString())
486 wxMessageBox( wxT("Anim name is empty"), wxT("Error"), wxOK | wxICON_INFORMATION, this );
487 else
489 theHistoryManager.Set_State( false );
490 ob_anim* newanim = (ob_anim*) arr_anims[indanim]->Clone();
491 newanim->SetToken(0 ,animname );
492 theHistoryManager.Set_State( true );
494 theHistoryManager.GroupStart( wxT("Copy Anim ") + arr_anims[indanim]->GetToken(0) );
495 entity->obj_container->Add_SubObj( newanim );
496 theHistoryManager.GroupEnd();
498 // Post the change to the parent
499 wxCommandEvent _evt( wxEVT_ANIM_LIST_CHANGE );
500 ProcessEvent( _evt );
502 // Refresh the list
503 Refresh_List();
505 // now the list should be updated => so select the newly created anim
506 Select( FindString( animname ));
507 wxCommandEvent event;
508 OnSelectionChange(event);
510 // Mark entity as changed
511 entity->SetChanged();
517 void wListAnims_Editable::EvtDeleteAnim(wxCommandEvent& event )
519 int indanim = GetSelection();
520 if( indanim < 0 || indanim >= arr_anims_count )
521 return;
523 // Make the guy to be sure
524 int res = wxMessageBox( wxT("Are really sure that you want to delete the anim <") +
525 arr_anims[indanim]->GetToken(0).Upper() + wxT("> ?"),
526 wxT("Hey !"), wxYES_NO | wxICON_INFORMATION, this );
528 if( res == wxYES )
530 theHistoryManager.GroupStart( wxT("Del Anim ") + arr_anims[indanim]->GetToken(0) );
531 arr_anims[indanim]->Rm();
532 theHistoryManager.GroupEnd();
534 ent_g_flag |= GM_ANIM_DELETION;
536 // Post the change to the parent
537 wxCommandEvent _evt( wxEVT_ANIM_LIST_CHANGE );
538 ProcessEvent( _evt );
540 // Refresh the list
541 Refresh_List();
543 // now the list should be updated => so select the newly created anim
544 if( indanim >= arr_anims_count )
545 indanim = arr_anims_count -1;
546 if( indanim == -1 )
547 indanim = wxNOT_FOUND;
549 Select( indanim );
550 wxCommandEvent event;
551 OnSelectionChange(event);
553 ent_g_flag &= ~GM_ANIM_DELETION;
554 // Mark entity as changed
555 entity->SetChanged();
560 //*************************************************************************
562 void wListAnims_Editable::EvtContextMenu( wxContextMenuEvent& event )
564 int indanim = HitTest( ScreenToClient(wxGetMousePosition() ));
565 if( indanim != GetSelection() && indanim >= 0 && indanim < arr_anims_count)
567 Select( indanim );
568 wxCommandEvent event;
569 OnSelectionChange(event);
572 PopupMenu( popMenu, ScreenToClient(wxGetMousePosition()) );
573 event.Skip();
576 //*************************************************************************
577 void
578 wListAnims_Editable::EvtCharPress(wxKeyEvent& event)
580 int kc = event.GetKeyCode();
582 if( wList_frames != NULL )
584 if( kc == WXK_DELETE)
586 wxCommandEvent new_event(wxEVT_COMMAND_MENU_SELECTED,CMD_DEL);
587 wList_frames->ProcessEvent( event );
588 // AddPendingEvent(new_event);
589 return;
593 wListAnims::EvtCharPress(event);
597 //*************************************************************************
598 //*************************************************************************
599 //*************************************************************************
600 // Event table
601 BEGIN_EVENT_TABLE(wListAnims_Editable, wListAnims)
602 EVT_MENU( CMD_NEW, wListAnims_Editable::EvtNewAnim)
603 EVT_MENU( CMD_RENAME, wListAnims_Editable::EvtRenameAnim)
604 EVT_MENU( CMD_COPY, wListAnims_Editable::EvtCopyAnim)
605 EVT_MENU( CMD_DEL, wListAnims_Editable::EvtDeleteAnim)
606 EVT_CONTEXT_MENU(wListAnims_Editable::EvtContextMenu)
607 EVT_CHAR(wListAnims_Editable::EvtCharPress)
608 END_EVENT_TABLE()