/
Splash.cpp
135 lines (105 loc) · 3.06 KB
/
Splash.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
#include "Splash.h"
#include <wx/panel.h>
#include <wx/dcbuffer.h>
#include <wx/splash.h>
#include <wx/sizer.h>
#include <wx/app.h>
#include <sigc++/retype_return.h>
#include <sigc++/functors/mem_fun.h>
#include "modulesystem/ModuleRegistry.h"
namespace ui
{
namespace
{
const char* const SPLASH_FILENAME = "darksplash.png";
}
class wxImagePanel :
public wxPanel
{
private:
wxBitmap _image;
wxString _text;
public:
wxImagePanel(wxFrame* parent, const wxString& file, wxBitmapType format);
void paintEvent(wxPaintEvent & evt);
void setText(const wxString& text);
void render(wxDC& dc);
};
void wxImagePanel::setText(const wxString& text)
{
_text = text;
}
wxImagePanel::wxImagePanel(wxFrame* parent, const wxString& file, wxBitmapType format) :
wxPanel(parent)
{
// load the file... ideally add a check to see if loading was successful
_image.LoadFile(file, format);
SetMinClientSize(wxSize(_image.GetWidth(), _image.GetHeight()));
SetBackgroundStyle(wxBG_STYLE_PAINT);
Bind(wxEVT_PAINT, [this](wxPaintEvent& ev) { paintEvent(ev); });
}
void wxImagePanel::paintEvent(wxPaintEvent & evt)
{
// depending on your system you may need to look at double-buffered dcs
wxAutoBufferedPaintDC dc(this);
render(dc);
}
void wxImagePanel::render(wxDC& dc)
{
dc.DrawBitmap(_image, 0, 0, false);
dc.SetTextForeground(wxColour(240, 240, 240));
dc.DrawText(_text, wxPoint(15, _image.GetHeight() - wxNORMAL_FONT->GetPixelSize().GetHeight() - 15));
}
Splash::Splash() :
wxFrame(nullptr, wxID_ANY, wxT("DarkRadiant"), wxDefaultPosition, wxDefaultSize, wxCENTRE),
_progressBar(nullptr)
{
const ApplicationContext& ctx = module::ModuleRegistry::Instance().getApplicationContext();
std::string fullFileName(ctx.getBitmapsPath() + SPLASH_FILENAME);
wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
_imagePanel = new wxImagePanel(this, fullFileName, wxBITMAP_TYPE_ANY);
sizer->Add(_imagePanel, 1, wxEXPAND);
_progressBar = new wxGauge(this, wxID_ANY, 100);
sizer->Add(_progressBar, 0, wxEXPAND);
SetSizer(sizer);
Fit();
Centre();
Show();
// Subscribe to the post-module init event to destroy ourselves
module::ModuleRegistry::Instance().signal_allModulesInitialised().connect(
sigc::hide_return(sigc::mem_fun(this, &Splash::Destroy)));
}
void Splash::queueDraw()
{
// Trigger a (re)draw, just to make sure that it gets displayed
Refresh(false);
Update();
wxTheApp->Yield(true);
}
void Splash::setText(const std::string& text)
{
_imagePanel->setText(text);
queueDraw();
}
void Splash::setProgress(float fraction)
{
_progressBar->SetValue(static_cast<int>(fraction*100));
queueDraw();
}
void Splash::setProgressAndText(const std::string& text, float fraction)
{
setText(text);
setProgress(fraction);
}
Splash& Splash::Instance()
{
static Splash* instance = new Splash;
return *instance;
}
void Splash::OnAppStartup()
{
// Connect the module progress callback
module::ModuleRegistry::Instance().signal_moduleInitialisationProgress().connect(
sigc::mem_fun(Instance(), &Splash::setProgressAndText));
}
} // namespace ui