2
0
mirror of https://github.com/Alia5/GlosSI.git synced 2024-11-01 09:20:17 +00:00
GlosSI/GlosSITarget/TargetWindow.cpp

105 lines
2.4 KiB
C++
Raw Normal View History

2021-10-15 15:10:57 +00:00
/*
Copyright 2021 Peter Repukat - FlatspotSoftware
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2021-10-15 13:02:36 +00:00
#include "TargetWindow.h"
2021-10-16 20:36:30 +00:00
#include <iostream>
2021-10-15 13:02:36 +00:00
#include <utility>
#include <SFML/Window/Event.hpp>
#ifdef _WIN32
#include <Windows.h>
#include <dwmapi.h>
#endif
2021-10-17 01:46:18 +00:00
static const bool DEV_MODE = false;
2021-10-16 20:36:30 +00:00
TargetWindow::TargetWindow(std::function<void()> on_close)
: on_close_(std::move(on_close))
2021-10-15 13:02:36 +00:00
{
2021-10-16 20:36:30 +00:00
if (DEV_MODE) {
window_.create(sf::VideoMode{1920, 1080}, "GlosSITarget", sf::Style::Default);
}
else {
window_.create(sf::VideoMode::getDesktopMode(), "GlosSITarget", sf::Style::None);
}
2021-10-15 13:02:36 +00:00
window_.setActive(true);
2021-10-15 15:10:57 +00:00
#ifdef _WIN32
HWND hwnd = window_.getSystemHandle();
MARGINS margins;
margins.cxLeftWidth = -1;
DwmExtendFrameIntoClientArea(hwnd, &margins);
2021-10-16 20:36:30 +00:00
if (!DEV_MODE) {
// always on top
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
}
2021-10-15 13:02:36 +00:00
#endif
2021-10-15 15:10:57 +00:00
2021-10-16 20:36:30 +00:00
if (!DEV_MODE) {
setClickThrough(true);
}
2021-10-15 13:02:36 +00:00
}
void TargetWindow::setFpsLimit(unsigned int fps_limit)
{
2021-10-15 15:10:57 +00:00
window_.setFramerateLimit(fps_limit);
2021-10-15 13:02:36 +00:00
}
void TargetWindow::setClickThrough(bool click_through)
{
2021-10-15 15:10:57 +00:00
#ifdef _WIN32
HWND hwnd = window_.getSystemHandle();
if (click_through) {
SetWindowLong(hwnd, GWL_EXSTYLE, WS_EX_LAYERED | WS_EX_TRANSPARENT | WS_EX_TOPMOST);
}
else {
SetWindowLong(hwnd, GWL_EXSTYLE, WS_EX_LAYERED);
}
#endif
}
2021-10-15 13:02:36 +00:00
void TargetWindow::update()
{
2021-10-15 15:10:57 +00:00
sf::Event event{};
while (window_.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window_.close();
on_close_();
}
}
2021-10-16 20:36:30 +00:00
if (DEV_MODE) {
window_.clear(sf::Color(0, 0, 0, 128));
}
else {
window_.clear(sf::Color::Transparent);
}
2021-10-15 15:10:57 +00:00
window_.display();
2021-10-15 13:02:36 +00:00
}
void TargetWindow::close()
{
2021-10-15 15:10:57 +00:00
window_.close();
on_close_();
2021-10-15 13:02:36 +00:00
}
2021-10-17 01:46:18 +00:00
WindowHandle TargetWindow::getSystemHandle() const
{
return window_.getSystemHandle();
}