iw5-mod/src/launcher/launcher.cpp

121 lines
3.2 KiB
C++
Raw Normal View History

2018-12-23 07:17:08 -05:00
#include <std_include.hpp>
#include "launcher.hpp"
2018-12-23 13:19:37 -05:00
launcher::launcher() : window_("Open-IW5", 615, 300), image_sp_(IMAGE_SP), image_mp_(IMAGE_MP)
2018-12-23 07:17:08 -05:00
{
2018-12-23 13:19:37 -05:00
this->image_sp_.set_position({100, 90});
this->image_mp_.set_position({401, 90});
2018-12-23 07:17:08 -05:00
2018-12-23 12:25:22 -05:00
this->image_sp_.set_size({100, 100});
this->image_mp_.set_size({100, 100});
this->image_sp_.set_click_listener(std::bind(&launcher::select_mode, this, mode::SINGLEPLAYER));
this->image_mp_.set_click_listener(std::bind(&launcher::select_mode, this, mode::MULTIPLAYER));
this->window_.set_callback(std::bind(&launcher::handler, this, std::placeholders::_1, std::placeholders::_2,
std::placeholders::_3));
2018-12-23 07:17:08 -05:00
}
2018-12-23 12:25:22 -05:00
launcher::mode launcher::run() const
2018-12-23 07:17:08 -05:00
{
this->window_.run();
2018-12-23 12:25:22 -05:00
return this->mode_;
}
LRESULT launcher::handler(const UINT message, const WPARAM w_param, const LPARAM l_param)
{
if (message == WM_PAINT)
{
this->paint();
}
else if (message == WM_MOUSEMOVE)
{
this->mouse_move(l_param);
}
else if (message == WM_LBUTTONDOWN)
{
this->image_sp_.click(this->mouse_, true);
this->image_mp_.click(this->mouse_, true);
}
else if (message == WM_LBUTTONUP)
{
this->image_sp_.click(this->mouse_, false);
this->image_mp_.click(this->mouse_, false);
}
else if (message == WM_ERASEBKGND)
{
return TRUE;
}
return DefWindowProc(this->window_, message, w_param, l_param);
}
void launcher::select_mode(const mode mode)
{
this->mode_ = mode;
this->window_.close();
}
void launcher::draw_text(const HDC hdc)
{
Gdiplus::Graphics graphics(hdc);
Gdiplus::SolidBrush color(Gdiplus::Color(255, 150, 150, 150));
Gdiplus::FontFamily font_family(L"Segoe UI");
2018-12-23 13:19:37 -05:00
Gdiplus::Font font(&font_family, 20, Gdiplus::FontStyleRegular, Gdiplus::UnitPixel);
const auto stringformat = Gdiplus::StringFormat::GenericTypographic();
2018-12-23 12:25:22 -05:00
2018-12-23 13:19:37 -05:00
std::wstring sp(L"Singleplayer");
std::wstring mp(L"Multiplayer");
2018-12-23 12:25:22 -05:00
2018-12-23 13:19:37 -05:00
Gdiplus::RectF rect{};
graphics.MeasureString(sp.data(), -1, &font, rect, stringformat, &rect);
Gdiplus::PointF pos{150 - (rect.Width / 2 + 2), 45};
graphics.DrawString(sp.data(), -1, &font, pos, &color);
rect = {};
graphics.MeasureString(mp.data(), -1, &font, rect, stringformat, &rect);
graphics.DrawString(mp.data(), -1, &font, {451 - (rect.Width / 2 + 4), 45}, &color);
Gdiplus::Pen pen(Gdiplus::Color(50, 255, 255, 255), 1);
graphics.DrawLine(&pen, 300, 0, 300, 600);
2018-12-23 12:25:22 -05:00
}
void launcher::paint() const
{
RECT rect;
GetClientRect(this->window_, &rect);
const int width = rect.right - rect.left;
const int height = rect.bottom + rect.left;
PAINTSTRUCT ps;
const auto hdc = BeginPaint(this->window_, &ps);
const auto hdc_copy = CreateCompatibleDC(hdc);
const auto bitmap = CreateCompatibleBitmap(hdc, width, height);
SelectObject(hdc_copy, bitmap);
this->window_.clear(hdc_copy);
this->draw_text(hdc_copy);
this->image_sp_.paint(hdc_copy, this->mouse_);
this->image_mp_.paint(hdc_copy, this->mouse_);
BitBlt(hdc, 0, 0, width, height, hdc_copy, 0, 0, SRCCOPY);
DeleteObject(bitmap);
DeleteDC(hdc_copy);
EndPaint(this->window_, &ps);
}
2018-12-23 16:15:32 -05:00
void launcher::mouse_move(const LPARAM l_param)
2018-12-23 12:25:22 -05:00
{
this->mouse_.x = GET_X_LPARAM(l_param);
this->mouse_.y = GET_Y_LPARAM(l_param);
InvalidateRect(this->window_, nullptr, FALSE);
2018-12-23 07:17:08 -05:00
}