Skip to content

Commit

Permalink
Added animated GIF export
Browse files Browse the repository at this point in the history
  • Loading branch information
TcT2k committed Mar 15, 2015
1 parent 12e0fe4 commit a833bb4
Show file tree
Hide file tree
Showing 6 changed files with 396 additions and 2 deletions.
1 change: 1 addition & 0 deletions App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class WADExplorerApp : public wxApp
{
wxImage::AddHandler(new wxPNGHandler());
wxImage::AddHandler(new wxJPEGHandler());
wxImage::AddHandler(new wxGIFHandler());

ExploreFrame* frame = new ExploreFrame(NULL);
frame->Show();
Expand Down
296 changes: 295 additions & 1 deletion HLMWADExplorer.fbp

Large diffs are not rendered by default.

33 changes: 32 additions & 1 deletion HLMWADFrames.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ BaseTexturePackPanel::BaseTexturePackPanel( wxWindow* parent, wxWindowID id, con
bSizer7->Add( m_staticText2, 0, wxALIGN_CENTER|wxALL, 5 );

m_colourPicker = new wxColourPickerCtrl( m_panel4, wxID_ANY, wxColour( 255, 255, 255 ), wxDefaultPosition, wxDefaultSize, wxCLRP_DEFAULT_STYLE );
bSizer7->Add( m_colourPicker, 0, wxALL, 5 );
bSizer7->Add( m_colourPicker, 0, wxALIGN_CENTER|wxALL, 5 );


bSizer6->Add( bSizer7, 0, wxEXPAND, 5 );
Expand All @@ -222,6 +222,35 @@ BaseTexturePackPanel::BaseTexturePackPanel( wxWindow* parent, wxWindowID id, con
bSizer11->Fit( m_framePanel );
bSizer6->Add( m_framePanel, 1, wxEXPAND | wxALL, 5 );

wxBoxSizer* bSizer111;
bSizer111 = new wxBoxSizer( wxHORIZONTAL );


bSizer111->Add( 0, 0, 1, wxEXPAND, 5 );

wxStaticBoxSizer* sbSizer1;
sbSizer1 = new wxStaticBoxSizer( new wxStaticBox( m_panel4, wxID_ANY, _("Animated GIF") ), wxVERTICAL );

m_staticText8 = new wxStaticText( m_panel4, wxID_ANY, _("Frame delay:"), wxDefaultPosition, wxDefaultSize, 0 );
m_staticText8->Wrap( -1 );
sbSizer1->Add( m_staticText8, 0, wxLEFT, 5 );

m_frameDelaySpinCtrl = new wxSpinCtrl( m_panel4, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, 10, 10000, 250 );
m_frameDelaySpinCtrl->SetMaxSize( wxSize( 70,-1 ) );

sbSizer1->Add( m_frameDelaySpinCtrl, 0, wxLEFT, 5 );

m_exportGIFButton = new wxButton( m_panel4, wxID_ANY, _("Export"), wxDefaultPosition, wxDefaultSize, 0 );
m_exportGIFButton->Enable( false );

sbSizer1->Add( m_exportGIFButton, 0, wxALL, 5 );


bSizer111->Add( sbSizer1, 0, wxALL|wxEXPAND, 5 );


bSizer6->Add( bSizer111, 0, wxEXPAND, 5 );

m_infoSizer = new wxBoxSizer( wxHORIZONTAL );

m_staticText3 = new wxStaticText( m_panel4, wxID_ANY, _("Zoom:"), wxDefaultPosition, wxDefaultSize, 0 );
Expand Down Expand Up @@ -269,6 +298,7 @@ BaseTexturePackPanel::BaseTexturePackPanel( wxWindow* parent, wxWindowID id, con
m_frameSpinCtrl->Connect( wxEVT_COMMAND_SPINCTRL_UPDATED, wxSpinEventHandler( BaseTexturePackPanel::OnFrameSpinCtrlChanged ), NULL, this );
m_frameSpinCtrl->Connect( wxEVT_COMMAND_TEXT_ENTER, wxCommandEventHandler( BaseTexturePackPanel::OnFrameSpinCtrlEnterPressed ), NULL, this );
m_colourPicker->Connect( wxEVT_COMMAND_COLOURPICKER_CHANGED, wxColourPickerEventHandler( BaseTexturePackPanel::OnColourPickerChanged ), NULL, this );
m_exportGIFButton->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( BaseTexturePackPanel::OnExportGIFClicked ), NULL, this );
m_zoomSpinCtrl->Connect( wxEVT_COMMAND_SPINCTRL_UPDATED, wxSpinEventHandler( BaseTexturePackPanel::OnZoomSpinCtrlChanged ), NULL, this );
}

Expand All @@ -279,6 +309,7 @@ BaseTexturePackPanel::~BaseTexturePackPanel()
m_frameSpinCtrl->Disconnect( wxEVT_COMMAND_SPINCTRL_UPDATED, wxSpinEventHandler( BaseTexturePackPanel::OnFrameSpinCtrlChanged ), NULL, this );
m_frameSpinCtrl->Disconnect( wxEVT_COMMAND_TEXT_ENTER, wxCommandEventHandler( BaseTexturePackPanel::OnFrameSpinCtrlEnterPressed ), NULL, this );
m_colourPicker->Disconnect( wxEVT_COMMAND_COLOURPICKER_CHANGED, wxColourPickerEventHandler( BaseTexturePackPanel::OnColourPickerChanged ), NULL, this );
m_exportGIFButton->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( BaseTexturePackPanel::OnExportGIFClicked ), NULL, this );
m_zoomSpinCtrl->Disconnect( wxEVT_COMMAND_SPINCTRL_UPDATED, wxSpinEventHandler( BaseTexturePackPanel::OnZoomSpinCtrlChanged ), NULL, this );

}
Expand Down
6 changes: 6 additions & 0 deletions HLMWADFrames.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <wx/spinctrl.h>
#include <wx/clrpicker.h>
#include <wx/statbmp.h>
#include <wx/button.h>
#include <wx/statbox.h>

///////////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -112,6 +114,9 @@ class BaseTexturePackPanel : public wxPanel
wxColourPickerCtrl* m_colourPicker;
wxPanel* m_framePanel;
wxStaticBitmap* m_frameBitmap;
wxStaticText* m_staticText8;
wxSpinCtrl* m_frameDelaySpinCtrl;
wxButton* m_exportGIFButton;
wxBoxSizer* m_infoSizer;
wxStaticText* m_staticText3;
wxSpinCtrl* m_zoomSpinCtrl;
Expand All @@ -125,6 +130,7 @@ class BaseTexturePackPanel : public wxPanel
virtual void OnFrameSpinCtrlChanged( wxSpinEvent& event ) { event.Skip(); }
virtual void OnFrameSpinCtrlEnterPressed( wxCommandEvent& event ) { event.Skip(); }
virtual void OnColourPickerChanged( wxColourPickerEvent& event ) { event.Skip(); }
virtual void OnExportGIFClicked( wxCommandEvent& event ) { event.Skip(); }
virtual void OnZoomSpinCtrlChanged( wxSpinEvent& event ) { event.Skip(); }


Expand Down
61 changes: 61 additions & 0 deletions TexturePackPanel.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#include "TexturePackPanel.h"

#include <wx/imaggif.h>
#include <wx/filedlg.h>
#include <wx/stdpaths.h>
#include <wx/anidecod.h>
#include <wx/wfstream.h>
#include <wx/quantize.h>

TexturePackPanel::TexturePackPanel( wxWindow* parent )
:
BaseTexturePackPanel( parent )
Expand All @@ -14,6 +21,9 @@ void TexturePackPanel::LoadTexture(wxInputStream& iStr, wxBitmap bitmap)
if (m_bitmap.IsOk())
m_drawBitmap = dstGC->CreateBitmap(bitmap);

m_exportGIFButton->Disable();
m_frameDelaySpinCtrl->Disable();

m_texturePack = new TexturePack(iStr);

m_textureListBox->Freeze();
Expand All @@ -39,6 +49,8 @@ void TexturePackPanel::OnTextureListBoxSelected( wxCommandEvent& event )

m_frameSpinCtrl->SetMax(tex.size() - 1);
m_frameSpinCtrl->SetValue(0);
m_exportGIFButton->Enable(tex.size() > 1);
m_frameDelaySpinCtrl->Enable(tex.size() > 1);

wxSpinEvent evt;
OnFrameSpinCtrlChanged(evt);
Expand Down Expand Up @@ -104,3 +116,52 @@ void TexturePackPanel::OnZoomSpinCtrlChanged(wxSpinEvent& event)
{
UpdateFrameImage();
}

void TexturePackPanel::OnExportGIFClicked(wxCommandEvent& event)
{
wxFileName exFileName(m_textureListBox->GetString(m_textureListBox->GetSelection()), wxPATH_UNIX);
exFileName.SetExt("gif");
wxString defaultFileName = exFileName.GetFullName();

wxFileDialog fileDlg(this, _("Select target gif"),
wxStandardPaths::Get().GetDocumentsDir(),
defaultFileName, "*.gif", wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
if (fileDlg.ShowModal() == wxID_OK)
{
wxBusyCursor busyCursor;

wxImageArray images;
const Texture& tex = m_texturePack->at(m_textureListBox->GetSelection());
for (auto frame = tex.begin(); frame != tex.end(); ++frame)
{
wxSize imgSize = frame->GetSize();

wxBitmap frameBmp(imgSize, m_bitmap.GetDepth());
wxMemoryDC dstDC;
dstDC.SelectObject(frameBmp);
dstDC.SetBrush(wxBrush(m_colourPicker->GetColour()));
dstDC.DrawRectangle(wxPoint(0, 0), imgSize);

wxSharedPtr<wxGraphicsContext> dstGC(wxGraphicsContext::Create(dstDC));
dstGC->SetAntialiasMode(wxANTIALIAS_NONE);
wxGraphicsBitmap srcSubBmp = dstGC->CreateSubBitmap(m_drawBitmap, frame->GetOffset().x, frame->GetOffset().y, frame->GetSize().GetWidth(), frame->GetSize().GetHeight());
dstGC->DrawBitmap(srcSubBmp, 0, 0, imgSize.GetWidth(), imgSize.GetHeight());
dstGC.reset();

dstDC.SelectObject(wxNullBitmap);

wxImage frameImg(frameBmp.ConvertToImage());

wxQuantize::Quantize(frameImg, frameImg, 256);

images.Add(frameImg);
}

wxGIFHandler* gifHandler = (wxGIFHandler*) wxImage::FindHandler(wxBITMAP_TYPE_GIF);

wxTempFileOutputStream oStr(fileDlg.GetPath());

gifHandler->SaveAnimation(images, &oStr, true, m_frameDelaySpinCtrl->GetValue());
oStr.Commit();
}
}
1 change: 1 addition & 0 deletions TexturePackPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class TexturePackPanel : public BaseTexturePackPanel
void OnFrameSpinCtrlEnterPressed( wxCommandEvent& event );
void OnColourPickerChanged(wxColourPickerEvent& event);
void OnZoomSpinCtrlChanged(wxSpinEvent& event);
void OnExportGIFClicked(wxCommandEvent& event);
public:
/** Constructor */
TexturePackPanel( wxWindow* parent );
Expand Down

0 comments on commit a833bb4

Please sign in to comment.