4 * Created on: 6 nov. 2008
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();
45 wxCommandEvent _event
;
48 int new_ind
= (GetSelection() + 1 < (int)GetCount() ) ? GetSelection() + 1 : GetCount() -1;
49 SetSelection(new_ind
);
50 OnSelectionChange( _event
);
55 int new_ind
= (GetSelection() - 1 >= 0 ) ? GetSelection() - 1 : 0;
56 SetSelection( new_ind
);
57 OnSelectionChange( _event
);
60 if( kc
== WXK_PAGEUP
)
62 int new_ind
= (GetSelection() - 10 >= 0 ) ? GetSelection() - 10 : 0;
63 SetSelection( new_ind
);
64 OnSelectionChange( _event
);
67 if( kc
== WXK_PAGEDOWN
)
69 int new_ind
= (GetSelection() + 10 < (int)GetCount() ) ? GetSelection() + 10 : GetCount() -1;
70 SetSelection( new_ind
);
71 OnSelectionChange( _event
);
75 for( size_t i
= 0; i
< GetCount();i
++)
77 wxString temp
= GetString(i
).Upper();
83 OnSelectionChange( _event
);
91 void myListBox::OnSelectionChange( wxCommandEvent
& event
)
96 void myListBox::EvtGetFocus(wxFocusEvent
& event
)
102 //****************************************************
103 //*********** new event
106 BEGIN_EVENT_TABLE(myListBox
, wxListBox
)
107 EVT_SET_FOCUS(myListBox::EvtGetFocus
)
108 EVT_CHAR(myListBox::OnCharPress
)
113 //****************************************************
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();
123 wxSize
_t(200, 400 );
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
)
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
)
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();
184 prev_selected
= last_anim_selected
;
186 // Clear Previous content
190 // Sort the Anims by Names
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 )
205 l_order
.insert( it
, i
);
208 // Fill the listBox items
210 while( l_order
.empty() == false )
212 int curr_ind
= l_order
.front();
214 map_ind_numAnim
[i
] = curr_ind
;
215 Append( arr_anims
[curr_ind
]->GetToken(0) );
221 // Try to restore the last edited anim
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
)
239 for( int i
= 0; i
< arr_anims_count
; i
++)
241 if( arr_anims
[i
] == curr_anim
)
248 curr_anim
= arr_anims
[_ind
];
249 last_anim_selected
= curr_anim
->GetToken( 0 );
253 if( arr_anims_count
== 0 )
256 last_anim_selected
= wxString();
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
);
268 pSetSelection(curr_anim
->arr_token
[0] );
270 ProcessSelectionChange();
274 //*************************************************************************
276 wListAnims::OnSelectionChange( wxCommandEvent
& event
)
280 ProcessSelectionChange();
281 wxCommandEvent
_evt(wxEVT_ANIM_SELECTED_CHANGE
);
285 //*************************************************************************
287 wListAnims::ProcessSelectionChange()
292 int ind
= GetSelection();
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 //*************************************************************************
310 wListAnims::pSetSelection( const wxString
& s
)
313 wxArrayString arr
= GetStrings();
314 for( size_t i
= 0; i
< arr
.GetCount(); i
++ )
326 //*************************************************************************
328 wListAnims::EvtGetFocus(wxFocusEvent
& event
)
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
);
354 //*************************************************************************
355 //*************************************************************************
356 //*************************************************************************
357 //*************************************************************************
358 //*************************************************************************
361 BEGIN_EVENT_TABLE(wListAnims
, myListBox
)
362 EVT_LISTBOX(wxID_ANY
,wListAnims::OnSelectionChange
)
363 EVT_SET_FOCUS(wListAnims::EvtGetFocus
)
364 EVT_CHAR(wListAnims::EvtCharPress
)
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()
393 //*************************************************************************
395 void wListAnims_Editable::EvtRenameAnim(wxCommandEvent
& event
)
397 int indanim
= GetSelection();
398 if( indanim
< 0 || indanim
> arr_anims_count
)
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 );
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 );
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
);
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
)
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 );
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
);
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
)
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 );
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
);
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;
547 indanim
= wxNOT_FOUND
;
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
)
568 wxCommandEvent event
;
569 OnSelectionChange(event
);
572 PopupMenu( popMenu
, ScreenToClient(wxGetMousePosition()) );
576 //*************************************************************************
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);
593 wListAnims::EvtCharPress(event
);
597 //*************************************************************************
598 //*************************************************************************
599 //*************************************************************************
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
)