integer math
This commit is contained in:
parent
4428cfe12c
commit
5ff43e240c
27 changed files with 922 additions and 1011 deletions
|
|
@ -6,7 +6,7 @@ int main() {
|
||||||
constexpr std::uint_fast32_t width = 1280;
|
constexpr std::uint_fast32_t width = 1280;
|
||||||
constexpr std::uint_fast32_t height = 720;
|
constexpr std::uint_fast32_t height = 720;
|
||||||
|
|
||||||
Window<true, false, false, false> window(width, height);
|
WindowWayland window(width, height, "Hello Drawing!");
|
||||||
|
|
||||||
for(std::uint_fast32_t x = 0; x < width; x++) {
|
for(std::uint_fast32_t x = 0; x < width; x++) {
|
||||||
for(std::uint_fast32_t y = 0; y < height; y++) {
|
for(std::uint_fast32_t y = 0; y < height; y++) {
|
||||||
|
|
|
||||||
|
|
@ -24,8 +24,8 @@ Make sure that the window has focus.
|
||||||
## Highlighted Code Snippet
|
## Highlighted Code Snippet
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
EventListener<MousePoint> clickListener(&window.onMouseLeftClick, [](MousePoint point){
|
EventListener<MousePoint> clickListener(&window.onMouseLeftClick, [&window](MousePoint point){
|
||||||
std::cout << std::format("Clicked on X:{} Y:{}!", point.x, point.y);
|
std::cout << std::format("Clicked on X:{} Y:{}!", MappedToPixel(point.x, window.width), MappedToPixel(point.y, window.height));
|
||||||
});
|
});
|
||||||
|
|
||||||
EventListener<void> keyAListener(&window.onKeyDown['a'], [](){
|
EventListener<void> keyAListener(&window.onKeyDown['a'], [](){
|
||||||
|
|
|
||||||
|
|
@ -4,15 +4,13 @@ import std;
|
||||||
using namespace Crafter;
|
using namespace Crafter;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
// Create a Wayland window named "HelloWindow" with resolution 1280x720
|
WindowWayland window(1280, 720, "Hello Input!");
|
||||||
// (window creation explained in HelloWindow example)
|
|
||||||
Window window("HelloWindow", 1280, 720);
|
|
||||||
|
|
||||||
// Listen for left mouse click events on the window
|
// Listen for left mouse click events on the window
|
||||||
// The callback receives the MousePoint struct containing the click coordinates in float pixels from the top left corner
|
// The callback receives the MousePoint struct containing the click coordinates in float pixels from the top left corner
|
||||||
EventListener<MousePoint> clickListener(&window.onMouseLeftClick, [](MousePoint point){
|
EventListener<MousePoint> clickListener(&window.onMouseLeftClick, [&window](MousePoint point){
|
||||||
// Print the coordinates where the user clicked
|
// Print the coordinates where the user clicked
|
||||||
std::cout << std::format("Clicked on X:{} Y:{}!", point.x, point.y) << std::endl;
|
std::cout << std::format("Clicked on X:{} Y:{}!", MappedToPixel(point.x, window.width), MappedToPixel(point.y, window.height)) << std::endl;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Listen specifically for the 'a' key being pressed down
|
// Listen specifically for the 'a' key being pressed down
|
||||||
|
|
@ -29,6 +27,6 @@ int main() {
|
||||||
std::cout << std::format("Pressed the {} key!", key) << std::endl;
|
std::cout << std::format("Pressed the {} key!", key) << std::endl;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Start the window event loop, unless the window is started events will not trigger.
|
//Start the window event loop, unless the window is started events will not trigger.
|
||||||
window.StartSync();
|
window.StartSync();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,27 +4,31 @@ import std;
|
||||||
using namespace Crafter;
|
using namespace Crafter;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
Window<true, false, false, false> window(1280, 720);
|
WindowWayland window(1280, 720, "Hello Input!");
|
||||||
|
|
||||||
UiElement<true, true>& element = window.CreateElement<true, true>(
|
UiElementBufferMouseBuffer* element = new UiElementBufferMouseBuffer(
|
||||||
2, //bufferWidth: the width of this elements pixel buffer
|
2, //bufferWidth: the width of this elements pixel buffer
|
||||||
1, //bufferHeight: the height of this elements pixel buffer
|
1, //bufferHeight: the height of this elements pixel buffer
|
||||||
0.5, //anchorX: relative position where this elements x anchor (top-left) is placed to its parent x anchor
|
FractionalToMapped(0.5), //anchorX: relative position where this elements x anchor (top-left) is placed to its parent x anchor
|
||||||
0.5, //anchorY: relative position where this elements y anchor (top-left) is placed to its parent y anchor
|
FractionalToMapped(0.5), //anchorY: relative position where this elements y anchor (top-left) is placed to its parent y anchor
|
||||||
0.5f, //relativeSizeX: the relative x size this element should be scaled to compared to its parent
|
FractionalToMapped(0.5), //relativeSizeX: the relative x size this element should be scaled to compared to its parent
|
||||||
0.5f, //relativeSizeY: the relative y size this element should be scaled to compared to its parent
|
FractionalToMapped(0.5), //relativeSizeY: the relative y size this element should be scaled to compared to its parent
|
||||||
0.5, //anchorOffsetX: the amount this element's anchor should be offset from the top left corner (0.5 to in the middle)
|
FractionalToMapped(0.5), //anchorOffsetX: the amount this element's anchor should be offset from the top left corner (0.5 to in the middle)
|
||||||
0.5, //anchorOffsetY: the amount this element's anchor should be offset from the top left corner (0.5 to place it in the middle)
|
FractionalToMapped(0.5), //anchorOffsetY: the amount this element's anchor should be offset from the top left corner (0.5 to place it in the middle)
|
||||||
0, //z: this elements Z position
|
0, //z: this elements Z position
|
||||||
false //ignoreScaling: wether this element ignores the scaling of the window, if true its size will be scaled according to the window scale
|
false //ignoreScaling: wether this element ignores the scaling of the window, if true its size will be scaled according to the window scale
|
||||||
);
|
);
|
||||||
|
|
||||||
// EventListener<MousePoint> clickListener(&element.onMouseLeftClick, [](MousePoint point){
|
window.elements.push_back(element);
|
||||||
// // Print the coordinates where the user clicked relative to the element's top left corner.
|
|
||||||
// std::cout << std::format("Clicked on X:{} Y:{}!", point.x, point.y) << std::endl;
|
|
||||||
// });
|
|
||||||
|
|
||||||
element.buffer.buffer = {{255, 0, 0 ,255}, {0, 255, 0 ,255}};
|
element->UpdatePosition(window);
|
||||||
window.framebuffer.Render();
|
|
||||||
|
EventListener<MousePoint> clickListener(&element.onMouseLeftClick, [](MousePoint point){
|
||||||
|
// Print the coordinates where the user clicked relative to the element's top left corner.
|
||||||
|
std::cout << std::format("Clicked on X:{} Y:{}!", point.x, point.y) << std::endl;
|
||||||
|
});
|
||||||
|
|
||||||
|
element->buffer = {{255, 0, 0 ,255}, {0, 255, 0 ,255}};
|
||||||
|
window.Render();
|
||||||
window.StartSync();
|
window.StartSync();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,9 +11,7 @@ A empty window with the title "HelloWindow" shows onscreen.
|
||||||
## Highlighted Code Snippet
|
## Highlighted Code Snippet
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
Window<true, true, false, false> window;
|
WindowWayland window(1280, 720, "Hello Window!");
|
||||||
window.framebuffer.Create(1280, 720);
|
|
||||||
window.title.Set("HelloWindow");
|
|
||||||
window.StartSync();
|
window.StartSync();
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,6 @@ import std;
|
||||||
using namespace Crafter;
|
using namespace Crafter;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
Window<true, true, false, false> window(1280, 720, "HelloWindow");
|
WindowWayland window(1280, 720, "Hello Window!");
|
||||||
window.StartSync();
|
window.StartSync();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,10 +20,36 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
|
||||||
module Crafter.Graphics:UiElement_impl;
|
module Crafter.Graphics:UiElement_impl;
|
||||||
import :UiElement;
|
import :UiElement;
|
||||||
|
import :Window;
|
||||||
|
import :Types;
|
||||||
import std;
|
import std;
|
||||||
|
|
||||||
using namespace Crafter;
|
using namespace Crafter;
|
||||||
|
|
||||||
UiElementBase::UiElementBase(float anchorX, float anchorY, float relativeWidth, float relativeHeight, float anchorOffsetX, float anchorOffsetY, float z, bool ignoreScaling) : anchorX(anchorX), anchorY(anchorY), relativeWidth(relativeWidth), relativeHeight(relativeHeight), anchorOffsetX(anchorOffsetX), anchorOffsetY(anchorOffsetY), z(z), ignoreScaling(ignoreScaling) {
|
UiElement::UiElement(std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX, std::int_fast32_t anchorOffsetY, std::int_fast32_t z, bool ignoreScaling) : anchorX(anchorX), anchorY(anchorY), relativeWidth(relativeWidth), relativeHeight(relativeHeight), anchorOffsetX(anchorOffsetX), anchorOffsetY(anchorOffsetY), z(z), ignoreScaling(ignoreScaling) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UiElementBuffer::UiElementBuffer(std::uint_fast32_t width, std::uint_fast32_t height) : width(width), height(height) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// void UiElement::UpdatePosition(Window& window) {
|
||||||
|
// // scaled.width = relativeWidth;
|
||||||
|
// // scaled.height = relativeHeight;
|
||||||
|
// // scaled.x = anchorX - (anchorOffsetX * scaled.width);
|
||||||
|
// // scaled.y = anchorY - (anchorOffsetY * scaled.height);
|
||||||
|
// // for(UiElement* child : children) {
|
||||||
|
// // UpdatePosition(*child);
|
||||||
|
// // }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// void UiElement::UpdatePosition(UiElement& parent) {
|
||||||
|
// // scaled.width = parent.scaled.width / relativeWidth;
|
||||||
|
// // scaled.height = parent.scaled.height / relativeHeight;
|
||||||
|
// // scaled.x = (parent.scaled.width / anchorX) - (anchorOffsetX * scaled.width);
|
||||||
|
// // scaled.y = (parent.scaled.height / anchorY) - (anchorOffsetY * scaled.height);
|
||||||
|
// // for(UiElement* child : children) {
|
||||||
|
// // UpdatePosition(*child);
|
||||||
|
// // }
|
||||||
|
// }
|
||||||
|
|
@ -1,54 +0,0 @@
|
||||||
/*
|
|
||||||
Crafter®.Graphics
|
|
||||||
Copyright (C) 2025 Catcrafts®
|
|
||||||
catcrafts.net
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License version 3.0 as published by the Free Software Foundation;
|
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
module Crafter.Graphics:UiElementBuffer_impl;
|
|
||||||
import :UiElementBuffer;
|
|
||||||
import :UiElement;
|
|
||||||
import :WindowFramebuffer;
|
|
||||||
import std;
|
|
||||||
|
|
||||||
namespace Crafter {
|
|
||||||
UiElementBuffer::UiElementBuffer(UiElementBase& parent, std::uint_fast32_t width, std::uint_fast32_t height) : parent(parent), width(width), height(height) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void UiElementBuffer::DrawNearestNeighbour(Pixel_BU8_GU8_RU8_AU8* dst, std::uint_fast32_t dstWidth, std::uint_fast32_t dstHeight) const {
|
|
||||||
for (std::uint_fast32_t y = 0; y < dstHeight; y++) {
|
|
||||||
std::uint_fast32_t srcY = y * height / dstHeight;
|
|
||||||
for (std::uint_fast32_t x = 0; x < dstWidth; x++) {
|
|
||||||
std::uint_fast32_t srcX = x * width / dstWidth;
|
|
||||||
dst[y * dstWidth + x] = buffer[srcY * width + srcX];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ScaleData UiElementBuffer::ScaleElement(const UiElementBase& element, ScaleData own, WindowFramebuffer& window) const {
|
|
||||||
ScaleData data;
|
|
||||||
if(element.ignoreScaling) {
|
|
||||||
data.width = element.relativeWidth*own.width;
|
|
||||||
data.height = element.relativeHeight*own.height;
|
|
||||||
} else {
|
|
||||||
data.width = element.relativeWidth*own.width*window.scale;
|
|
||||||
data.height = element.relativeHeight*own.height*window.scale;
|
|
||||||
}
|
|
||||||
data.x = ((element.anchorX*own.width)-(element.anchorOffsetX*data.width))+own.x;
|
|
||||||
data.y = ((element.anchorY*own.height)-(element.anchorOffsetY*data.height))+own.y;
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
33
implementations/Crafter.Graphics-UiElementBufferBuffer.cpp
Normal file
33
implementations/Crafter.Graphics-UiElementBufferBuffer.cpp
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
/*
|
||||||
|
Crafter®.Graphics
|
||||||
|
Copyright (C) 2025 Catcrafts®
|
||||||
|
Catcrafts.net
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 3.0 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
|
||||||
|
module Crafter.Graphics:UiElementBufferBuffer_impl;
|
||||||
|
import :UiElement;
|
||||||
|
import std;
|
||||||
|
|
||||||
|
using namespace Crafter;
|
||||||
|
|
||||||
|
UiElementBufferBuffer::UiElementBufferBuffer(std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX, std::int_fast32_t anchorOffsetY, std::int_fast32_t z, bool ignoreScaling) : UiElement(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
UiElementBufferBuffer::UiElementBufferBuffer(std::uint_fast32_t width, std::uint_fast32_t height, std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX, std::int_fast32_t anchorOffsetY, std::int_fast32_t z, bool ignoreScaling) : UiElement(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling), UiElementBufferBufferBase(width, height){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,85 @@
|
||||||
|
/*
|
||||||
|
Crafter®.Graphics
|
||||||
|
Copyright (C) 2025 Catcrafts®
|
||||||
|
Catcrafts.net
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 3.0 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
|
||||||
|
module Crafter.Graphics:UiElementBufferBufferBase_impl;
|
||||||
|
import :UiElement;
|
||||||
|
import std;
|
||||||
|
|
||||||
|
using namespace Crafter;
|
||||||
|
|
||||||
|
UiElementBufferBufferBase::UiElementBufferBufferBase(std::uint_fast32_t width, std::uint_fast32_t height) : UiElementBuffer(width, height), buffer(width*height) {
|
||||||
|
|
||||||
|
}
|
||||||
|
void UiElementBufferBufferBase::Create(std::uint_fast32_t width, std::uint_fast32_t height) {
|
||||||
|
buffer.resize(width*height);
|
||||||
|
}
|
||||||
|
void UiElementBufferBufferBase::Resize(std::uint_fast32_t width, std::uint_fast32_t height) {
|
||||||
|
buffer.resize(width*height);
|
||||||
|
}
|
||||||
|
void UiElementBufferBufferBase::Resize(std::uint_fast32_t width, std::uint_fast32_t height, std::uint_fast32_t offsetX, std::uint_fast32_t offsetY) {
|
||||||
|
|
||||||
|
}
|
||||||
|
void UiElementBufferBufferBase::ResizeNearestNeighbour(std::uint_fast32_t width, std::uint_fast32_t height) {
|
||||||
|
|
||||||
|
}
|
||||||
|
void UiElementBufferBufferBase::ResizeBicubic(std::uint_fast32_t width, std::uint_fast32_t height) {
|
||||||
|
|
||||||
|
}
|
||||||
|
void UiElementBufferBufferBase::Destroy() {
|
||||||
|
buffer.clear();
|
||||||
|
width = 0;
|
||||||
|
height = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void UiElementBufferBufferBase::Copy(Pixel_BU8_GU8_RU8_AU8* dst) const {
|
||||||
|
std::memcpy(dst, buffer.data(), width*height*sizeof(Pixel_BU8_GU8_RU8_AU8));
|
||||||
|
}
|
||||||
|
|
||||||
|
void UiElementBufferBufferBase::CopyNearestNeighbour(Pixel_BU8_GU8_RU8_AU8* dst, std::uint_fast32_t dstWidth, std::uint_fast32_t dstHeight) const {
|
||||||
|
for (std::uint_fast32_t y = 0; y < dstHeight; y++) {
|
||||||
|
std::uint_fast32_t srcY = y * height / dstHeight;
|
||||||
|
for (std::uint_fast32_t x = 0; x < dstWidth; x++) {
|
||||||
|
std::uint_fast32_t srcX = x * width / dstWidth;
|
||||||
|
dst[y * dstWidth + x] = buffer[srcY * width + srcX];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void UiElementBufferBufferBase::CopyBicubic(Pixel_BU8_GU8_RU8_AU8* dst, std::uint_fast32_t dstWidth, std::uint_fast32_t dstHeight) const {
|
||||||
|
|
||||||
|
}
|
||||||
|
void UiElementBufferBufferBase::Write(Pixel_BU8_GU8_RU8_AU8* pixels) {
|
||||||
|
std::memcpy(buffer.data(), pixels, width*height*sizeof(Pixel_BU8_GU8_RU8_AU8));
|
||||||
|
}
|
||||||
|
void UiElementBufferBufferBase::Write(std::uint_fast32_t x, std::uint_fast32_t y, Pixel_BU8_GU8_RU8_AU8 pixel) {
|
||||||
|
buffer[y * width + x] = pixel;
|
||||||
|
}
|
||||||
|
Pixel_BU8_GU8_RU8_AU8 UiElementBufferBufferBase::Read(std::uint_fast32_t x, std::uint_fast32_t y) const {
|
||||||
|
return buffer[y * width + x];
|
||||||
|
}
|
||||||
|
const Pixel_BU8_GU8_RU8_AU8* UiElementBufferBufferBase::Read() const {
|
||||||
|
return buffer.data();
|
||||||
|
}
|
||||||
|
Pixel_BU8_GU8_RU8_AU8* UiElementBufferBufferBase::Get() {
|
||||||
|
return buffer.data();
|
||||||
|
}
|
||||||
|
void UiElementBufferBufferBase::Store() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,33 @@
|
||||||
|
/*
|
||||||
|
Crafter®.Graphics
|
||||||
|
Copyright (C) 2025 Catcrafts®
|
||||||
|
Catcrafts.net
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 3.0 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
|
||||||
|
module Crafter.Graphics:UiElementBufferMouseBuffer_impl;
|
||||||
|
import :UiElement;
|
||||||
|
import std;
|
||||||
|
|
||||||
|
using namespace Crafter;
|
||||||
|
|
||||||
|
UiElementBufferMouseBuffer::UiElementBufferMouseBuffer(std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX, std::int_fast32_t anchorOffsetY, std::int_fast32_t z, bool ignoreScaling) : UiElementBufferBuffer(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
UiElementBufferMouseBuffer::UiElementBufferMouseBuffer(std::uint_fast32_t width, std::uint_fast32_t height, std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX, std::int_fast32_t anchorOffsetY, std::int_fast32_t z, bool ignoreScaling) : UiElementBufferBuffer(width, height, anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,55 +0,0 @@
|
||||||
/*
|
|
||||||
Crafter®.Graphics
|
|
||||||
Copyright (C) 2025 Catcrafts®
|
|
||||||
catcrafts.net
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License version 3.0 as published by the Free Software Foundation;
|
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
module Crafter.Graphics:WindowFramebuffer_impl;
|
|
||||||
import :WindowFramebuffer;
|
|
||||||
import :Window;
|
|
||||||
import std;
|
|
||||||
import :Types;
|
|
||||||
import :UiElement;
|
|
||||||
import Crafter.Event;
|
|
||||||
|
|
||||||
namespace Crafter {
|
|
||||||
WindowFramebuffer::WindowFramebuffer(WindowBase& window) : window(window) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
WindowFramebuffer::WindowFramebuffer(WindowBase& window, std::uint_fast32_t width, std::uint_fast32_t height) : window(window) {
|
|
||||||
Create(width, height);
|
|
||||||
}
|
|
||||||
|
|
||||||
WindowFramebuffer::~WindowFramebuffer() {
|
|
||||||
Destroy();
|
|
||||||
}
|
|
||||||
|
|
||||||
ScaleData WindowFramebuffer::ScaleElement(const UiElementBase& element) {
|
|
||||||
ScaleData data;
|
|
||||||
if(element.ignoreScaling) {
|
|
||||||
data.width = element.relativeWidth*width;
|
|
||||||
data.height = element.relativeHeight*height;
|
|
||||||
} else {
|
|
||||||
data.width = element.relativeWidth*width*scale;
|
|
||||||
data.height = element.relativeHeight*height*scale;
|
|
||||||
}
|
|
||||||
data.x = ((element.anchorX*width)-(element.anchorOffsetX*data.width));
|
|
||||||
data.y = ((element.anchorY*height)-(element.anchorOffsetY*data.height));
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -1,107 +0,0 @@
|
||||||
/*
|
|
||||||
Crafter®.Graphics
|
|
||||||
Copyright (C) 2025 Catcrafts®
|
|
||||||
catcrafts.net
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License version 3.0 as published by the Free Software Foundation;
|
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
module;
|
|
||||||
|
|
||||||
#include <wayland-client.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <sys/mman.h>
|
|
||||||
|
|
||||||
module Crafter.Graphics:WindowFramebuffer_wayland_impl;
|
|
||||||
import :WindowFramebuffer;
|
|
||||||
import :Window;
|
|
||||||
import :UiElement;
|
|
||||||
import std;
|
|
||||||
import :Types;
|
|
||||||
import :Shm;
|
|
||||||
import Crafter.Event;
|
|
||||||
|
|
||||||
namespace Crafter {
|
|
||||||
void WindowFramebuffer::Create(std::uint_fast32_t width, std::uint_fast32_t height) {
|
|
||||||
this->width = width;
|
|
||||||
this->height = height;
|
|
||||||
int stride = width * 4;
|
|
||||||
int size = stride * height;
|
|
||||||
|
|
||||||
int fd = create_shm_file(size);
|
|
||||||
if (fd < 0) {
|
|
||||||
fprintf(stderr, "creating a buffer file for %d B failed: %m\n", size);
|
|
||||||
}
|
|
||||||
|
|
||||||
framebuffer = reinterpret_cast<Pixel_BU8_GU8_RU8_AU8*>(mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0));
|
|
||||||
if (framebuffer == MAP_FAILED) {
|
|
||||||
fprintf(stderr, "mmap failed: %m\n");
|
|
||||||
close(fd);
|
|
||||||
}
|
|
||||||
|
|
||||||
wl_shm_pool *pool = wl_shm_create_pool(window.shm, fd, size);
|
|
||||||
buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, WL_SHM_FORMAT_ARGB8888);
|
|
||||||
wl_shm_pool_destroy(pool);
|
|
||||||
close(fd);
|
|
||||||
|
|
||||||
if (buffer == NULL) {
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
wl_surface_attach(window.surface, buffer, 0, 0);
|
|
||||||
wl_surface_commit(window.surface);
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowFramebuffer::Resize(std::uint_fast32_t width, std::uint_fast32_t height) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowFramebuffer::Destroy() {
|
|
||||||
wl_buffer_destroy(buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowFramebuffer::Render() {
|
|
||||||
std::vector<const UiElementBuffer*> drawOrder;
|
|
||||||
drawOrder.reserve(elements.size());
|
|
||||||
for (const UiElementBuffer* e : elements) drawOrder.push_back(e);
|
|
||||||
std::sort(drawOrder.begin(), drawOrder.end(), [](const UiElementBuffer* a, const UiElementBuffer* b){ return a->parent.z < b->parent.z; });
|
|
||||||
|
|
||||||
for(const UiElementBuffer* element : drawOrder) {
|
|
||||||
ScaleData data = ScaleElement(element->parent);
|
|
||||||
std::vector<Pixel_BU8_GU8_RU8_AU8> scaled(data.width*data.height);
|
|
||||||
element->DrawNearestNeighbour(scaled.data(), data.width, data.height);
|
|
||||||
for (std::int32_t x = data.x; x - data.x < data.width; x++) {
|
|
||||||
for (std::int32_t y = data.y; y - data.y < data.height; y++) {
|
|
||||||
if (x >= 0 && x < width && y >= 0 && y < height) {
|
|
||||||
Pixel_BU8_GU8_RU8_AU8& dst = framebuffer[y * width + x];
|
|
||||||
const Pixel_BU8_GU8_RU8_AU8& src = scaled[(y - data.y) * data.width + (x - data.x)];
|
|
||||||
|
|
||||||
float srcA = src.a / 255.0f;
|
|
||||||
float dstA = dst.a / 255.0f;
|
|
||||||
|
|
||||||
float outA = srcA + dstA * (1.0f - srcA);
|
|
||||||
if (outA > 0.0f) {
|
|
||||||
dst.r = static_cast<uint8_t>((src.r * srcA + dst.r * dstA * (1.0f - srcA)) / outA);
|
|
||||||
dst.g = static_cast<uint8_t>((src.g * srcA + dst.g * dstA * (1.0f - srcA)) / outA);
|
|
||||||
dst.b = static_cast<uint8_t>((src.b * srcA + dst.b * dstA * (1.0f - srcA)) / outA);
|
|
||||||
dst.a = static_cast<uint8_t>(outA * 255);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//RenderElements(window, *element, data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,42 +0,0 @@
|
||||||
/*
|
|
||||||
Crafter®.Graphics
|
|
||||||
Copyright (C) 2025 Catcrafts®
|
|
||||||
catcrafts.net
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License version 3.0 as published by the Free Software Foundation;
|
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
module;
|
|
||||||
|
|
||||||
#include <wayland-client.h>
|
|
||||||
#include "../lib/xdg-shell-client-protocol.h"
|
|
||||||
|
|
||||||
module Crafter.Graphics:WindowTitle_impl;
|
|
||||||
import :WindowTitle;
|
|
||||||
import :Window;
|
|
||||||
import std;
|
|
||||||
import :Types;
|
|
||||||
import :Shm;
|
|
||||||
import Crafter.Event;
|
|
||||||
|
|
||||||
namespace Crafter {
|
|
||||||
WindowTitle::WindowTitle(WindowBase& window) : window(window) {
|
|
||||||
|
|
||||||
}
|
|
||||||
WindowTitle::WindowTitle(WindowBase& window, const std::string_view title) : window(window) {
|
|
||||||
Set(title);
|
|
||||||
}
|
|
||||||
void WindowTitle::Set(const std::string_view title) {
|
|
||||||
xdg_toplevel_set_title(window.xdgToplevel, title.data());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -19,7 +19,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
|
||||||
module;
|
module;
|
||||||
|
|
||||||
#ifdef CRAFTER_GRAPHICS_WAYLAND
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
@ -37,30 +36,432 @@ module;
|
||||||
#include <print>
|
#include <print>
|
||||||
#include <wayland-client.h>
|
#include <wayland-client.h>
|
||||||
#include <wayland-client-protocol.h>
|
#include <wayland-client-protocol.h>
|
||||||
#endif
|
|
||||||
|
|
||||||
export module Crafter.Graphics:Window_impl;
|
module Crafter.Graphics:Window_wayland_impl;
|
||||||
import :Window;
|
import :Window;
|
||||||
|
import :UiElement;
|
||||||
import std;
|
import std;
|
||||||
import :WindowKeyboard;
|
|
||||||
import :WindowFramebuffer;
|
|
||||||
import :Types;
|
import :Types;
|
||||||
import :Shm;
|
import :Shm;
|
||||||
import Crafter.Event;
|
import Crafter.Event;
|
||||||
|
|
||||||
using namespace Crafter;
|
using namespace Crafter;
|
||||||
|
|
||||||
void WindowBase::StartSync() {
|
WindowFramebuffer::WindowFramebuffer(std::uint_fast32_t width, std::uint_fast32_t height) : width(width), height(height) {
|
||||||
while (open && wl_display_dispatch(display) != -1) {
|
|
||||||
std::cout << "bruh3" << std::endl;
|
}
|
||||||
|
|
||||||
|
WindowWayland::WindowWayland(std::uint_fast32_t width, std::uint_fast32_t height) : WindowFramebuffer(width, height) {
|
||||||
|
display = wl_display_connect(NULL);
|
||||||
|
if (display == NULL) {
|
||||||
|
std::cerr << "failed to create display" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
wl_registry* registry = wl_display_get_registry(display);
|
||||||
|
wl_registry_add_listener(registry, ®istry_listener, this);
|
||||||
|
if (wl_display_roundtrip(display) == -1) {
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
if (shm == NULL || compositor == NULL || xdgWmBase == NULL) {
|
||||||
|
std::cerr << "no wl_shm, wl_compositor or xdg_wm_base support" << std::endl;
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
surface = wl_compositor_create_surface(compositor);
|
||||||
|
xdgSurface = xdg_wm_base_get_xdg_surface(xdgWmBase, surface);
|
||||||
|
xdgToplevel = xdg_surface_get_toplevel(xdgSurface);
|
||||||
|
|
||||||
|
xdg_surface_add_listener(xdgSurface, &xdg_surface_listener, this);
|
||||||
|
xdg_toplevel_add_listener(xdgToplevel, &xdg_toplevel_listener, this);
|
||||||
|
wl_surface_commit(surface);
|
||||||
|
|
||||||
|
while (wl_display_dispatch(display) != -1 && !configured) {}
|
||||||
|
|
||||||
|
wl_surface_commit(surface);
|
||||||
|
|
||||||
|
zxdg_toplevel_decoration_v1* decoration = zxdg_decoration_manager_v1_get_toplevel_decoration(manager, xdgToplevel);
|
||||||
|
zxdg_toplevel_decoration_v1_set_mode(decoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
|
||||||
|
|
||||||
|
// Create a wl_buffer, attach it to the surface and commit the surface
|
||||||
|
int stride = width * 4;
|
||||||
|
int size = stride * height;
|
||||||
|
|
||||||
|
// Allocate a shared memory file with the right size
|
||||||
|
int fd = create_shm_file(size);
|
||||||
|
if (fd < 0) {
|
||||||
|
fprintf(stderr, "creating a buffer file for %d B failed: %m\n", size);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Map the shared memory file
|
||||||
|
framebuffer = reinterpret_cast<Pixel_BU8_GU8_RU8_AU8*>(mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0));
|
||||||
|
if (framebuffer == MAP_FAILED) {
|
||||||
|
fprintf(stderr, "mmap failed: %m\n");
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a wl_buffer from our shared memory file descriptor
|
||||||
|
wl_shm_pool *pool = wl_shm_create_pool(shm, fd, size);
|
||||||
|
buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, WL_SHM_FORMAT_ARGB8888);
|
||||||
|
wl_shm_pool_destroy(pool);
|
||||||
|
|
||||||
|
// Now that we've mapped the file and created the wl_buffer, we no longer
|
||||||
|
// need to keep file descriptor opened
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
if (buffer == NULL) {
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
wl_surface_attach(surface, buffer, 0, 0);
|
||||||
|
wl_surface_commit(surface);
|
||||||
|
}
|
||||||
|
|
||||||
|
WindowWayland::WindowWayland(std::uint_fast32_t width, std::uint_fast32_t height, const std::string_view title) : WindowWayland(width, height) {
|
||||||
|
xdg_toplevel_set_title(xdgToplevel, title.data());
|
||||||
|
}
|
||||||
|
|
||||||
|
WindowWayland::~WindowWayland() {
|
||||||
|
xdg_toplevel_destroy(xdgToplevel);
|
||||||
|
xdg_surface_destroy(xdgSurface);
|
||||||
|
wl_surface_destroy(surface);
|
||||||
|
wl_buffer_destroy(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void WindowWayland::StartSync() {
|
||||||
|
while (open && wl_display_dispatch(display) != -1) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void RenderElement(UiElementBufferBuffer* element, ScaleData data , WindowWayland* window) {
|
||||||
|
std::vector<Pixel_BU8_GU8_RU8_AU8> scaled(data.width*data.height);
|
||||||
|
element->CopyNearestNeighbour(scaled.data(), data.width, data.height);
|
||||||
|
|
||||||
|
for (std::int32_t x = data.x; x - data.x < data.width; x++) {
|
||||||
|
for (std::int32_t y = data.y; y - data.y < data.height; y++) {
|
||||||
|
if (x >= 0 && x < window->width && y >= 0 && y < window->height) {
|
||||||
|
Pixel_BU8_GU8_RU8_AU8& dst = window->framebuffer[y * window->width + x];
|
||||||
|
const Pixel_BU8_GU8_RU8_AU8& src = scaled[(y - data.y) * data.width + (x - data.x)];
|
||||||
|
|
||||||
|
float srcA = src.a / 255.0f;
|
||||||
|
float dstA = dst.a / 255.0f;
|
||||||
|
|
||||||
|
float outA = srcA + dstA * (1.0f - srcA);
|
||||||
|
if (outA > 0.0f) {
|
||||||
|
dst.r = static_cast<uint8_t>((src.r * srcA + dst.r * dstA * (1.0f - srcA)) / outA);
|
||||||
|
dst.g = static_cast<uint8_t>((src.g * srcA + dst.g * dstA * (1.0f - srcA)) / outA);
|
||||||
|
dst.b = static_cast<uint8_t>((src.b * srcA + dst.b * dstA * (1.0f - srcA)) / outA);
|
||||||
|
dst.a = static_cast<uint8_t>(outA * 255);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::sort(element->children.begin(), element->children.end(), [](UiElement* a, UiElement* b){ return a->z < b->z; });
|
||||||
|
for(UiElement* child : element->children) {
|
||||||
|
std::int32_t scaledWidth = MappedToPixel(element->relativeWidth, data.width);
|
||||||
|
std::int32_t scaledHeight = MappedToPixel(element->relativeHeight, data.height);
|
||||||
|
std::int32_t scaledX = MappedToPixel(element->anchorX, data.width) - MappedToPixel(element->anchorOffsetX, scaledWidth) - data.x;
|
||||||
|
std::int32_t scaledY = MappedToPixel(element->anchorY, data.height) - MappedToPixel(element->anchorOffsetY, scaledHeight) - data.y;
|
||||||
|
RenderElement(element, {
|
||||||
|
scaledX,
|
||||||
|
scaledY,
|
||||||
|
scaledWidth,
|
||||||
|
scaledHeight
|
||||||
|
}, window);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void WindowWayland::Render() {
|
||||||
|
std::sort(elements.begin(), elements.end(), [](UiElementBufferBuffer* a, UiElementBufferBuffer* b){ return a->z < b->z; });
|
||||||
|
|
||||||
|
for(UiElementBufferBuffer* element : elements) {
|
||||||
|
std::int32_t scaledWidth = MappedToPixel(element->relativeWidth, width);
|
||||||
|
std::int32_t scaledHeight = MappedToPixel(element->relativeHeight, height);
|
||||||
|
std::int32_t scaledX = MappedToPixel(element->anchorX, width) - MappedToPixel(element->anchorOffsetX, scaledWidth);
|
||||||
|
std::int32_t scaledY = MappedToPixel(element->anchorY, height) - MappedToPixel(element->anchorOffsetY, scaledHeight);
|
||||||
|
RenderElement(element, {
|
||||||
|
scaledX,
|
||||||
|
scaledY,
|
||||||
|
scaledWidth,
|
||||||
|
scaledHeight
|
||||||
|
}, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
wl_surface_attach(surface, buffer, 0, 0);
|
||||||
|
wl_surface_damage(surface, 0, 0, width, height);
|
||||||
|
wl_surface_commit(surface);
|
||||||
|
}
|
||||||
|
|
||||||
|
void WindowWayland::StartUpdate() {
|
||||||
|
cb = wl_surface_frame(surface);
|
||||||
|
wl_callback_add_listener(cb, &wl_callback_listener, this);
|
||||||
|
updating = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void WindowWayland::StopUpdate() {
|
||||||
|
updating = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void WindowWayland::SetTitle(const std::string_view title) {
|
||||||
|
xdg_toplevel_set_title(xdgToplevel, title.data());
|
||||||
|
}
|
||||||
|
|
||||||
|
void WindowWayland::Resize(std::uint_fast32_t width, std::uint_fast32_t height) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void WindowWayland::Write(Pixel_BU8_GU8_RU8_AU8* pixels) {
|
||||||
|
std::memcpy(framebuffer, pixels, width*height*sizeof(Pixel_BU8_GU8_RU8_AU8));
|
||||||
|
}
|
||||||
|
|
||||||
|
void WindowWayland::Write(std::uint_fast32_t x, std::uint_fast32_t y, Pixel_BU8_GU8_RU8_AU8 pixel) {
|
||||||
|
framebuffer[y * width + x] = pixel;
|
||||||
|
}
|
||||||
|
|
||||||
|
Pixel_BU8_GU8_RU8_AU8 WindowWayland::Read(std::uint_fast32_t x, std::uint_fast32_t y) const{
|
||||||
|
return framebuffer[y * width + x];
|
||||||
|
}
|
||||||
|
|
||||||
|
const Pixel_BU8_GU8_RU8_AU8* WindowWayland::Read() const{
|
||||||
|
return framebuffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
Pixel_BU8_GU8_RU8_AU8* WindowWayland::Get() {
|
||||||
|
return framebuffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
void WindowWayland::Store() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void WindowWayland::xdg_wm_base_handle_ping(void* data, xdg_wm_base* xdg_wm_base, std::uint32_t serial) {
|
||||||
|
xdg_wm_base_pong(xdg_wm_base, serial);
|
||||||
|
}
|
||||||
|
|
||||||
|
void WindowWayland::wl_surface_frame_done(void* data, struct wl_callback *cb, uint32_t time)
|
||||||
|
{
|
||||||
|
wl_callback_destroy(cb);
|
||||||
|
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
|
||||||
|
|
||||||
|
if(window->updating) {
|
||||||
|
cb = wl_surface_frame(window->surface);
|
||||||
|
wl_callback_add_listener(cb, &WindowWayland::wl_callback_listener, window);
|
||||||
|
}
|
||||||
|
|
||||||
|
window->Render();
|
||||||
|
}
|
||||||
|
|
||||||
|
void WindowWayland::pointer_handle_button(void* data, wl_pointer* pointer, std::uint32_t serial, std::uint32_t time, std::uint32_t button, std::uint32_t state) {
|
||||||
|
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
|
||||||
|
if (button == BTN_LEFT) {
|
||||||
|
if(state == WL_POINTER_BUTTON_STATE_PRESSED) {
|
||||||
|
window->mouseLeftHeld = true;
|
||||||
|
window->onMouseLeftClick.Invoke(window->currentMousePos);
|
||||||
|
for(UiElementMouse* element : window->mouseElements) {
|
||||||
|
if(window->currentMousePos.x >= element->clickArea.x && window->currentMousePos.x <= element->clickArea.x+element->clickArea.width && window->currentMousePos.y > element->clickArea.y && window->currentMousePos.y < element->clickArea.y+element->clickArea.height) {
|
||||||
|
element->onMouseLeftClick.Invoke({window->currentMousePos.x-element->clickArea.x, window->currentMousePos.y-element->clickArea.y});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
window->mouseLeftHeld = false;
|
||||||
|
window->onMouseLeftRelease.Invoke(window->currentMousePos);
|
||||||
|
for(UiElementMouse* element : window->mouseElements) {
|
||||||
|
if(window->currentMousePos.x >= element->clickArea.x && window->currentMousePos.x <= element->clickArea.x+element->clickArea.width && window->currentMousePos.y > element->clickArea.y && window->currentMousePos.y < element->clickArea.y+element->clickArea.height) {
|
||||||
|
element->onMouseLeftRelease.Invoke({window->currentMousePos.x-element->clickArea.x, window->currentMousePos.y-element->clickArea.y});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if(button == BTN_RIGHT){
|
||||||
|
if(state == WL_POINTER_BUTTON_STATE_PRESSED) {
|
||||||
|
window->mouseRightHeld = true;
|
||||||
|
window->onMouseRightClick.Invoke(window->currentMousePos);
|
||||||
|
for(UiElementMouse* element : window->mouseElements) {
|
||||||
|
if(window->currentMousePos.x >= element->clickArea.x && window->currentMousePos.x <= element->clickArea.x+element->clickArea.width && window->currentMousePos.y > element->clickArea.y && window->currentMousePos.y < element->clickArea.y+element->clickArea.height) {
|
||||||
|
element->onMouseRightClick.Invoke({window->currentMousePos.x-element->clickArea.x, window->currentMousePos.y-element->clickArea.y});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
window->mouseRightHeld = true;
|
||||||
|
window->onMouseRightRelease.Invoke(window->currentMousePos);
|
||||||
|
for(UiElementMouse* element : window->mouseElements) {
|
||||||
|
if(window->currentMousePos.x >= element->clickArea.x && window->currentMousePos.x <= element->clickArea.x+element->clickArea.width && window->currentMousePos.y > element->clickArea.y && window->currentMousePos.y < element->clickArea.y+element->clickArea.height) {
|
||||||
|
element->onMouseRightRelease.Invoke({window->currentMousePos.x-element->clickArea.x, window->currentMousePos.y-element->clickArea.y});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void WindowWayland::PointerListenerHandleMotion(void* data, wl_pointer* wl_pointer, uint time, wl_fixed_t surface_x, wl_fixed_t surface_y) {
|
||||||
|
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
|
||||||
|
MousePoint pos = {PixelToMapped(wl_fixed_to_double(surface_x), window->width), PixelToMapped(wl_fixed_to_double(surface_y), window->height)};
|
||||||
|
window->lastMousePos = window->currentMousePos;
|
||||||
|
window->currentMousePos = pos;
|
||||||
|
window->mouseDelta = {window->currentMousePos.x-window->lastMousePos.x, window->currentMousePos.y-window->lastMousePos.y};
|
||||||
|
window->onMouseMove.Invoke({window->lastMousePos, window->currentMousePos, window->mouseDelta});
|
||||||
|
for(UiElementMouse* element : window->mouseElements) {
|
||||||
|
if(window->currentMousePos.x >= element->clickArea.x && window->currentMousePos.x <= element->clickArea.x+element->clickArea.width && window->currentMousePos.y > element->clickArea.y && window->currentMousePos.y < element->clickArea.y+element->clickArea.height) {
|
||||||
|
element->onMouseMove.Invoke({window->currentMousePos.x-element->clickArea.x, window->currentMousePos.y-element->clickArea.y});
|
||||||
|
if(!(window->lastMousePos.x >= element->clickArea.x && window->lastMousePos.x <= element->clickArea.x+element->clickArea.width && window->lastMousePos.y > element->clickArea.y && window->lastMousePos.y < element->clickArea.y+element->clickArea.height)) {
|
||||||
|
element->onMouseEnter.Invoke({window->currentMousePos.x-element->clickArea.x, window->currentMousePos.y-element->clickArea.y});
|
||||||
|
}
|
||||||
|
} else if(window->lastMousePos.x >= element->clickArea.x && window->lastMousePos.x <= element->clickArea.x+element->clickArea.width && window->lastMousePos.y > element->clickArea.y && window->lastMousePos.y < element->clickArea.y+element->clickArea.height) {
|
||||||
|
element->onMouseLeave.Invoke({window->currentMousePos.x-element->clickArea.x, window->currentMousePos.y-element->clickArea.y});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void WindowWayland::PointerListenerHandleEnter(void* data, wl_pointer* wl_pointer, uint serial, wl_surface* surface, wl_fixed_t surface_x, wl_fixed_t surface_y) {
|
||||||
|
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
|
||||||
|
window->onMouseEnter.Invoke({window->lastMousePos, window->currentMousePos, window->mouseDelta});
|
||||||
|
}
|
||||||
|
|
||||||
|
void WindowWayland::PointerListenerHandleLeave(void* data, wl_pointer*, std::uint32_t, wl_surface*) {
|
||||||
|
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
|
||||||
|
window->onMouseEnter.Invoke({window->lastMousePos, window->currentMousePos, window->mouseDelta});
|
||||||
|
}
|
||||||
|
|
||||||
|
void WindowWayland::PointerListenerHandleAxis(void*, wl_pointer*, std::uint32_t, std::uint32_t, wl_fixed_t value) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void WindowWayland::keyboard_keymap(void *data, wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size) {
|
||||||
|
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
|
||||||
|
|
||||||
|
if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
|
||||||
|
close(fd);
|
||||||
|
fprintf(stderr, "Unsupported keymap format\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *map = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
|
||||||
|
if (map == MAP_FAILED) {
|
||||||
|
close(fd);
|
||||||
|
perror("mmap");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
window->xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
|
||||||
|
window->xkb_keymap = xkb_keymap_new_from_string(window->xkb_context, (const char *)map, XKB_KEYMAP_FORMAT_TEXT_V1,XKB_KEYMAP_COMPILE_NO_FLAGS);
|
||||||
|
munmap(map, size);
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
window->xkb_state = xkb_state_new(window->xkb_keymap);
|
||||||
|
}
|
||||||
|
|
||||||
|
void WindowWayland::keyboard_enter(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface, wl_array *keys) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void WindowWayland::keyboard_leave(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void WindowWayland::keyboard_key(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state) {
|
||||||
|
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
|
||||||
|
|
||||||
|
if (!window->xkb_state) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
xkb_keycode_t keycode = key + 8;
|
||||||
|
xkb_keysym_t keysym = xkb_state_key_get_one_sym(window->xkb_state, keycode);
|
||||||
|
|
||||||
|
char utf8[8] = {0};
|
||||||
|
int len = xkb_keysym_to_utf8(keysym, utf8, sizeof(utf8));
|
||||||
|
if (len != 0) {
|
||||||
|
char keypress = utf8[0];
|
||||||
|
if(state == WL_KEYBOARD_KEY_STATE_PRESSED) {
|
||||||
|
if(window->heldkeys[keypress]) {
|
||||||
|
window->onKeyHold[keypress].Invoke();
|
||||||
|
window->onAnyKeyHold.Invoke(keypress);
|
||||||
|
} else{
|
||||||
|
window->heldkeys[keypress] = true;
|
||||||
|
window->onKeyDown[keypress].Invoke();
|
||||||
|
window->onAnyKeyDown.Invoke(keypress);
|
||||||
|
}
|
||||||
|
} else{
|
||||||
|
window->heldkeys[keypress] = false;
|
||||||
|
window->onKeyUp[keypress].Invoke();
|
||||||
|
window->onAnyKeyUp.Invoke(keypress);
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// // fallback for keys like Return, Escape, etc.
|
||||||
|
// char name[64];
|
||||||
|
// if (xkb_keysym_get_name(keysym, name, sizeof(name)) > 0) {
|
||||||
|
// printf("Key %s pressed (non-printable or multi-char)\n", name);
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void WindowWayland::keyboard_modifiers(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void WindowWayland::keyboard_repeat_info(void *data, wl_keyboard *keyboard, int32_t rate, int32_t delay) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void WindowWayland::seat_handle_capabilities(void* data, wl_seat* seat, uint32_t capabilities) {
|
||||||
|
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
|
||||||
|
window->seat = seat;
|
||||||
|
if (capabilities & WL_SEAT_CAPABILITY_POINTER) {
|
||||||
|
wl_pointer* pointer = wl_seat_get_pointer(seat);
|
||||||
|
wl_pointer_add_listener(pointer, &pointer_listener, window);
|
||||||
|
}
|
||||||
|
if (capabilities & WL_SEAT_CAPABILITY_KEYBOARD) {
|
||||||
|
wl_keyboard* keyboard = wl_seat_get_keyboard(seat);
|
||||||
|
wl_keyboard_add_listener(keyboard, &keyboard_listener, window);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WindowBase::StartUpdate(void* window) {
|
void WindowWayland::handle_global(void *data, wl_registry *registry, std::uint32_t name, const char *interface, std::uint32_t version) {
|
||||||
cb = wl_surface_frame(surface);
|
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
|
||||||
wl_callback_add_listener(cb, &surface_frame_listener, window);
|
if (strcmp(interface, wl_shm_interface.name) == 0) {
|
||||||
|
window->shm = reinterpret_cast<wl_shm*>(wl_registry_bind(registry, name, &wl_shm_interface, 1));
|
||||||
|
} else if (strcmp(interface, wl_seat_interface.name) == 0) {
|
||||||
|
wl_seat* seat = reinterpret_cast<wl_seat*>(wl_registry_bind(registry, name, &wl_seat_interface, 1));
|
||||||
|
wl_seat_add_listener(seat, &seat_listener, window);
|
||||||
|
} else if (compositor == NULL && strcmp(interface, wl_compositor_interface.name) == 0) {
|
||||||
|
compositor = reinterpret_cast<wl_compositor*>(wl_registry_bind(registry, name, &wl_compositor_interface, 1));
|
||||||
|
} else if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
|
||||||
|
window->xdgWmBase = reinterpret_cast<xdg_wm_base*>(wl_registry_bind(registry, name, &xdg_wm_base_interface, 1));
|
||||||
|
xdg_wm_base_add_listener(window->xdgWmBase, &xdgWmBaseListener, NULL);
|
||||||
|
} else if (strcmp(interface, zxdg_decoration_manager_v1_interface.name) == 0) {
|
||||||
|
window->manager = reinterpret_cast<zxdg_decoration_manager_v1*>(wl_registry_bind(registry, name, &zxdg_decoration_manager_v1_interface, 1));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WindowBase::StopUpdate() {
|
void WindowWayland::handle_global_remove(void* data, wl_registry* registry, uint32_t name) {
|
||||||
onUpdate.listeners.clear();
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void WindowWayland::xdg_toplevel_configure(void*, xdg_toplevel*, std::int32_t, std::int32_t, wl_array*){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void WindowWayland::xdg_toplevel_handle_close(void* data, xdg_toplevel*) {
|
||||||
|
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
|
||||||
|
window->onClose.Invoke();
|
||||||
|
window->open = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void WindowWayland::xdg_surface_handle_configure(void* data, xdg_surface* xdg_surface, std::uint32_t serial) {
|
||||||
|
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
|
||||||
|
// The compositor configures our surface, acknowledge the configure event
|
||||||
|
xdg_surface_ack_configure(xdg_surface, serial);
|
||||||
|
|
||||||
|
if (window->configured) {
|
||||||
|
// If this isn't the first configure event we've received, we already
|
||||||
|
// have a buffer attached, so no need to do anything. Commit the
|
||||||
|
// surface to apply the configure acknowledgement.
|
||||||
|
wl_surface_commit(window->surface);
|
||||||
|
}
|
||||||
|
|
||||||
|
window->configured = true;
|
||||||
}
|
}
|
||||||
|
|
@ -23,8 +23,8 @@ import std;
|
||||||
|
|
||||||
namespace Crafter {
|
namespace Crafter {
|
||||||
export struct MousePoint {
|
export struct MousePoint {
|
||||||
double x;
|
std::uint_fast32_t x;
|
||||||
double y;
|
std::uint_fast32_t y;
|
||||||
};
|
};
|
||||||
|
|
||||||
export struct MouseMoveEvent {
|
export struct MouseMoveEvent {
|
||||||
|
|
@ -94,4 +94,80 @@ namespace Crafter {
|
||||||
float b;
|
float b;
|
||||||
float a;
|
float a;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
export constexpr std::int_fast32_t BOUND = 9;
|
||||||
|
export constexpr std::int_fast32_t SCALE = std::numeric_limits<std::int_fast32_t>::max() / BOUND;
|
||||||
|
export constexpr double SCALEDOUBLE = static_cast<double>(std::numeric_limits<std::int_fast32_t>::max()) / BOUND;
|
||||||
|
|
||||||
|
export constexpr std::int_fast32_t FractionalToMapped(double f) {
|
||||||
|
return std::int_fast32_t(f * SCALEDOUBLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
export constexpr std::int_fast32_t MappedToPixel(std::int_fast32_t mapped, std::int_fast32_t width) {
|
||||||
|
return mapped / (SCALE / width);
|
||||||
|
}
|
||||||
|
|
||||||
|
export constexpr double MappedToFractional(std::int_fast32_t mapped) {
|
||||||
|
return static_cast<double>(mapped) / SCALEDOUBLE;
|
||||||
|
}
|
||||||
|
|
||||||
|
export constexpr std::int_fast32_t PixelToMapped(std::int_fast32_t pixel, std::int_fast32_t width) {
|
||||||
|
return pixel * (SCALE / width);
|
||||||
|
}
|
||||||
|
|
||||||
|
// export constexpr double bound = 10;
|
||||||
|
|
||||||
|
// export constexpr std::uint_fast32_t FractionalToMapped(double fractional) {
|
||||||
|
// constexpr double min_x = -bound;
|
||||||
|
// constexpr double max_x = bound + 1.0;
|
||||||
|
// constexpr double range_x = max_x - min_x;
|
||||||
|
|
||||||
|
// constexpr double MAXD = static_cast<double>(
|
||||||
|
// std::numeric_limits<std::uint_fast32_t>::max()
|
||||||
|
// );
|
||||||
|
|
||||||
|
// // Normalize to [0,1]
|
||||||
|
// double t = (fractional - min_x) / range_x;
|
||||||
|
|
||||||
|
// // Clamp (important for constexpr safety and edge behavior)
|
||||||
|
// if (t < 0.0) t = 0.0;
|
||||||
|
// if (t > 1.0) t = 1.0;
|
||||||
|
|
||||||
|
// return static_cast<std::uint_fast32_t>(t * MAXD);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// export constexpr double MappedToFractional(std::uint_fast32_t mapped) {
|
||||||
|
// constexpr double min_x = -bound;
|
||||||
|
// constexpr double max_x = bound + 1.0;
|
||||||
|
// constexpr double range_x = max_x - min_x;
|
||||||
|
|
||||||
|
// constexpr double MAXD = static_cast<double>(
|
||||||
|
// std::numeric_limits<std::uint_fast32_t>::max()
|
||||||
|
// );
|
||||||
|
|
||||||
|
// double t = static_cast<double>(mapped) / MAXD;
|
||||||
|
|
||||||
|
// return min_x + t * range_x;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// export constexpr std::int_fast32_t MappedToPixel(std::uint_fast32_t mapped, std::uint_fast32_t size) {
|
||||||
|
// constexpr double MAXD = static_cast<double>(
|
||||||
|
// std::numeric_limits<std::uint_fast32_t>::max()
|
||||||
|
// );
|
||||||
|
|
||||||
|
// // scale mapped ∈ [0, MAX] to pixel ∈ [0, size]
|
||||||
|
// double t = static_cast<double>(mapped) / MAXD;
|
||||||
|
|
||||||
|
// // Clamp (shouldn't be necessary, but protects constexpr eval)
|
||||||
|
// if (t < 0.0) t = 0.0;
|
||||||
|
// if (t > 1.0) t = 1.0;
|
||||||
|
|
||||||
|
// return static_cast<std::int_fast32_t>(t * static_cast<double>(size));
|
||||||
|
// }
|
||||||
|
|
||||||
|
export constexpr std::uint_fast32_t PixelToMapped(double pixel, std::uint_fast32_t size) {
|
||||||
|
return static_cast<std::uint_fast32_t>((pixel / size) * static_cast<double>((std::numeric_limits<std::uint_fast32_t>::max() / 10)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,38 +18,100 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export module Crafter.Graphics:UiElement;
|
export module Crafter.Graphics:UiElement;
|
||||||
import :UiElementBuffer;
|
|
||||||
import :UiElementMouse;
|
|
||||||
import std;
|
import std;
|
||||||
import Crafter.Event;
|
import Crafter.Event;
|
||||||
import :Types;
|
import :Types;
|
||||||
|
|
||||||
export namespace Crafter {
|
export namespace Crafter {
|
||||||
class Font;
|
class Window;
|
||||||
class UiElementBase {
|
class UiElement {
|
||||||
public:
|
public:
|
||||||
float z;
|
std::int_fast32_t z;
|
||||||
float anchorX;
|
std::int_fast32_t anchorX;
|
||||||
float anchorY;
|
std::int_fast32_t anchorY;
|
||||||
|
std::int_fast32_t anchorOffsetX;
|
||||||
|
std::int_fast32_t anchorOffsetY;
|
||||||
|
std::uint_fast32_t relativeWidth;
|
||||||
|
std::uint_fast32_t relativeHeight;
|
||||||
bool ignoreScaling;
|
bool ignoreScaling;
|
||||||
float relativeWidth;
|
std::vector<UiElement*> children;
|
||||||
float relativeHeight;
|
ScaleData scaled;
|
||||||
float anchorOffsetX;
|
|
||||||
float anchorOffsetY;
|
|
||||||
std::vector<std::unique_ptr<UiElementBase>> children;
|
|
||||||
|
|
||||||
UiElementBase(float anchorX, float anchorY, float relativeWidth, float relativeHeight, float anchorOffsetX = 0.5, float anchorOffsetY = 0.5, float z = 0, bool ignoreScaling = false);
|
UiElement(std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0, bool ignoreScaling = false);
|
||||||
UiElementBase(UiElementBase&&) noexcept = default;
|
UiElement(UiElement&) = delete;
|
||||||
UiElementBase& operator=(UiElementBase&&) noexcept = default;
|
UiElement& operator=(UiElement&) = delete;
|
||||||
|
// void UpdatePosition(Window& window);
|
||||||
|
// void UpdatePosition(UiElement& parent);
|
||||||
};
|
};
|
||||||
|
|
||||||
template<bool Buffer, bool Mouse>
|
class UiElementMouse {
|
||||||
class UiElement : public UiElementBase{
|
|
||||||
public:
|
public:
|
||||||
std::conditional_t<Buffer, UiElementBuffer, std::monostate> buffer;
|
ScaleData clickArea;
|
||||||
std::conditional_t<Buffer, UiElementMouse, std::monostate> mouse;
|
Event<MouseMoveEvent> onMouseMove;
|
||||||
UiElement(float anchorX, float anchorY, float relativeWidth, float relativeHeight, float anchorOffsetX = 0.5, float anchorOffsetY = 0.5, float z = 0, bool ignoreScaling = false) : UiElementBase(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling = false) {}
|
Event<MouseMoveEvent> onMouseEnter;
|
||||||
UiElement(float anchorX, float anchorY, float relativeWidth, float relativeHeight, float anchorOffsetX = 0.5, float anchorOffsetY = 0.5, float z = 0, bool ignoreScaling = false) requires (Buffer) : UiElementBase(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling), buffer(*this) {}
|
Event<MouseMoveEvent> onMouseLeave;
|
||||||
UiElement(std::uint_fast32_t width, std::uint_fast32_t height, float anchorX, float anchorY, float relativeWidth, float relativeHeight, float anchorOffsetX = 0.5, float anchorOffsetY = 0.5, float z = 0, bool ignoreScaling = false) requires (Buffer) : UiElementBase(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling), buffer(*this, width, height) {}
|
Event<MousePoint> onMouseRightClick;
|
||||||
|
Event<MousePoint> onMouseLeftClick;
|
||||||
|
Event<MousePoint> onMouseRightHold;
|
||||||
|
Event<MousePoint> onMouseLeftHold;
|
||||||
|
Event<MousePoint> onMouseRightRelease;
|
||||||
|
Event<MousePoint> onMouseLeftRelease;
|
||||||
|
};
|
||||||
|
|
||||||
|
class UiElementBuffer {
|
||||||
|
public:
|
||||||
|
std::uint_fast32_t width;
|
||||||
|
std::uint_fast32_t height;
|
||||||
|
UiElementBuffer() = default;
|
||||||
|
UiElementBuffer(std::uint_fast32_t width, std::uint_fast32_t height);
|
||||||
|
virtual void Create(std::uint_fast32_t width, std::uint_fast32_t height) = 0;
|
||||||
|
virtual void Resize(std::uint_fast32_t width, std::uint_fast32_t height) = 0;
|
||||||
|
virtual void Resize(std::uint_fast32_t width, std::uint_fast32_t height, std::uint_fast32_t offsetX, std::uint_fast32_t offsetY) = 0;
|
||||||
|
virtual void ResizeNearestNeighbour(std::uint_fast32_t width, std::uint_fast32_t height) = 0;
|
||||||
|
virtual void ResizeBicubic(std::uint_fast32_t width, std::uint_fast32_t height) = 0;
|
||||||
|
virtual void Destroy() = 0;
|
||||||
|
virtual void Copy(Pixel_BU8_GU8_RU8_AU8* dst) const = 0;
|
||||||
|
virtual void CopyNearestNeighbour(Pixel_BU8_GU8_RU8_AU8* dst, std::uint_fast32_t dstWidth, std::uint_fast32_t dstHeight) const = 0;
|
||||||
|
virtual void CopyBicubic(Pixel_BU8_GU8_RU8_AU8* dst, std::uint_fast32_t dstWidth, std::uint_fast32_t dstHeight) const = 0;
|
||||||
|
virtual void Write(Pixel_BU8_GU8_RU8_AU8* pixels) = 0;
|
||||||
|
virtual void Write(std::uint_fast32_t x, std::uint_fast32_t y, Pixel_BU8_GU8_RU8_AU8 pixel) = 0;
|
||||||
|
virtual Pixel_BU8_GU8_RU8_AU8 Read(std::uint_fast32_t x, std::uint_fast32_t y) const = 0;
|
||||||
|
virtual const Pixel_BU8_GU8_RU8_AU8* Read() const = 0;
|
||||||
|
virtual Pixel_BU8_GU8_RU8_AU8* Get() = 0;
|
||||||
|
virtual void Store() = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
class UiElementBufferBufferBase : public UiElementBuffer {
|
||||||
|
public:
|
||||||
|
std::vector<Pixel_BU8_GU8_RU8_AU8> buffer;
|
||||||
|
UiElementBufferBufferBase() = default;
|
||||||
|
UiElementBufferBufferBase(std::uint_fast32_t width, std::uint_fast32_t height);
|
||||||
|
void Create(std::uint_fast32_t width, std::uint_fast32_t height) override;
|
||||||
|
void Resize(std::uint_fast32_t width, std::uint_fast32_t height) override;
|
||||||
|
void Resize(std::uint_fast32_t width, std::uint_fast32_t height, std::uint_fast32_t offsetX, std::uint_fast32_t offsetY) override;
|
||||||
|
void ResizeNearestNeighbour(std::uint_fast32_t width, std::uint_fast32_t height) override;
|
||||||
|
void ResizeBicubic(std::uint_fast32_t width, std::uint_fast32_t height) override;
|
||||||
|
void Destroy() override;
|
||||||
|
void Copy(Pixel_BU8_GU8_RU8_AU8* dst) const override;
|
||||||
|
void CopyNearestNeighbour(Pixel_BU8_GU8_RU8_AU8* dst, std::uint_fast32_t dstWidth, std::uint_fast32_t dstHeight) const override;
|
||||||
|
void CopyBicubic(Pixel_BU8_GU8_RU8_AU8* dst, std::uint_fast32_t dstWidth, std::uint_fast32_t dstHeight) const override;
|
||||||
|
void Write(Pixel_BU8_GU8_RU8_AU8* pixels) override;
|
||||||
|
void Write(std::uint_fast32_t x, std::uint_fast32_t y, Pixel_BU8_GU8_RU8_AU8 pixel) override;
|
||||||
|
Pixel_BU8_GU8_RU8_AU8 Read(std::uint_fast32_t x, std::uint_fast32_t y) const override;
|
||||||
|
const Pixel_BU8_GU8_RU8_AU8* Read() const override;
|
||||||
|
Pixel_BU8_GU8_RU8_AU8* Get() override;
|
||||||
|
void Store() override;
|
||||||
|
};
|
||||||
|
|
||||||
|
class UiElementBufferBuffer : public UiElement, public UiElementBufferBufferBase {
|
||||||
|
public:
|
||||||
|
UiElementBufferBuffer(std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0, bool ignoreScaling = false);
|
||||||
|
UiElementBufferBuffer(std::uint_fast32_t width, std::uint_fast32_t height, std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0, bool ignoreScaling = false);
|
||||||
|
};
|
||||||
|
|
||||||
|
class UiElementBufferMouseBuffer : public UiElementBufferBuffer, public UiElementMouse {
|
||||||
|
public:
|
||||||
|
UiElementBufferMouseBuffer(std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0, bool ignoreScaling = false);
|
||||||
|
UiElementBufferMouseBuffer(std::uint_fast32_t width, std::uint_fast32_t height, std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0, bool ignoreScaling = false);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
@ -1,51 +0,0 @@
|
||||||
/*
|
|
||||||
Crafter®.Graphics
|
|
||||||
Copyright (C) 2025 Catcrafts®
|
|
||||||
catcrafts.net
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License version 3.0 as published by the Free Software Foundation;
|
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
export module Crafter.Graphics:UiElementBuffer;
|
|
||||||
import std;
|
|
||||||
import :Types;
|
|
||||||
|
|
||||||
export namespace Crafter {
|
|
||||||
class UiElementBase;
|
|
||||||
class WindowFramebuffer;
|
|
||||||
class UiElementBuffer {
|
|
||||||
public:
|
|
||||||
UiElementBase& parent;
|
|
||||||
std::uint_fast32_t width;
|
|
||||||
std::uint_fast32_t height;
|
|
||||||
std::vector<Pixel_BU8_GU8_RU8_AU8> buffer;
|
|
||||||
UiElementBuffer(UiElementBase& parent);
|
|
||||||
UiElementBuffer(UiElementBase& parent, std::uint_fast32_t width, std::uint_fast32_t height);
|
|
||||||
|
|
||||||
void DrawNearestNeighbour(Pixel_BU8_GU8_RU8_AU8* dst, std::uint_fast32_t dstWidth, std::uint_fast32_t dstHeight) const;
|
|
||||||
ScaleData ScaleElement(const UiElementBase& element, ScaleData own, WindowFramebuffer& window) const;
|
|
||||||
|
|
||||||
void Create(std::uint_fast32_t width, std::uint_fast32_t height);
|
|
||||||
void Destroy();
|
|
||||||
|
|
||||||
void Write(Pixel_BU8_GU8_RU8_AU8* pixels);
|
|
||||||
void Write(std::uint_fast32_t x, std::uint_fast32_t y, Pixel_BU8_GU8_RU8_AU8 pixel);
|
|
||||||
|
|
||||||
Pixel_BU8_GU8_RU8_AU8 Read(std::uint_fast32_t x, std::uint_fast32_t y);
|
|
||||||
Pixel_BU8_GU8_RU8_AU8* Read();
|
|
||||||
|
|
||||||
Pixel_BU8_GU8_RU8_AU8* Get();
|
|
||||||
void Store();
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
@ -1,37 +0,0 @@
|
||||||
/*
|
|
||||||
Crafter®.Graphics
|
|
||||||
Copyright (C) 2025 Catcrafts®
|
|
||||||
catcrafts.net
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License version 3.0 as published by the Free Software Foundation;
|
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
export module Crafter.Graphics:UiElementMouse;
|
|
||||||
import std;
|
|
||||||
import :Types;
|
|
||||||
import Crafter.Event;
|
|
||||||
|
|
||||||
export namespace Crafter {
|
|
||||||
class UiElementMouse {
|
|
||||||
public:
|
|
||||||
Event<MouseMoveEvent> onMouseMove;
|
|
||||||
Event<MouseMoveEvent> onMouseEnter;
|
|
||||||
Event<MouseMoveEvent> onMouseLeave;
|
|
||||||
Event<MousePoint> onMouseRightClick;
|
|
||||||
Event<MousePoint> onMouseLeftClick;
|
|
||||||
Event<MousePoint> onMouseRightHold;
|
|
||||||
Event<MousePoint> onMouseLeftHold;
|
|
||||||
Event<MousePoint> onMouseRightRelease;
|
|
||||||
Event<MousePoint> onMouseLeftRelease;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
@ -16,7 +16,6 @@ You should have received a copy of the GNU Lesser General Public
|
||||||
License along with this library; if not, write to the Free Software
|
License along with this library; if not, write to the Free Software
|
||||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
module;
|
module;
|
||||||
|
|
||||||
#ifdef CRAFTER_GRAPHICS_WAYLAND
|
#ifdef CRAFTER_GRAPHICS_WAYLAND
|
||||||
|
|
@ -41,207 +40,139 @@ module;
|
||||||
|
|
||||||
export module Crafter.Graphics:Window;
|
export module Crafter.Graphics:Window;
|
||||||
import std;
|
import std;
|
||||||
import :WindowBase;
|
import :Types;
|
||||||
import :WindowKeyboard;
|
import Crafter.Event;
|
||||||
import :WindowMouse;
|
|
||||||
import :WindowTitle;
|
|
||||||
import :WindowFramebuffer;
|
|
||||||
|
|
||||||
export namespace Crafter {
|
export namespace Crafter {
|
||||||
template<bool Framebuffer, bool Title, bool Keyboard, bool Mouse>
|
class UiElement;
|
||||||
class Window : public WindowBase {
|
class Window {
|
||||||
public:
|
public:
|
||||||
std::conditional_t<Framebuffer, WindowFramebuffer, std::monostate> framebuffer;
|
Event<void> onClose;
|
||||||
std::conditional_t<Title, WindowTitle, std::monostate> title;
|
Event<std::chrono::time_point<std::chrono::high_resolution_clock>> onUpdate;
|
||||||
std::conditional_t<Keyboard, WindowKeyboard, std::monostate> keyboard;
|
bool open = true;
|
||||||
std::conditional_t<Mouse, WindowMouse, std::monostate> mouse;
|
bool updating = false;
|
||||||
Window() requires (Framebuffer && !Title && !Keyboard && !Mouse) : framebuffer(*this) {}
|
Window() = default;
|
||||||
Window(std::uint_fast32_t width, std::uint_fast32_t height) requires (Framebuffer && !Title && !Keyboard && !Mouse) : framebuffer(*this, width, height) {}
|
Window(Window&) = delete;
|
||||||
|
Window(Window&&) = delete;
|
||||||
|
virtual ~Window() = default;
|
||||||
|
Window& operator=(const Window&) = delete;
|
||||||
|
virtual void StartSync() = 0;
|
||||||
|
virtual void StartUpdate() = 0;
|
||||||
|
virtual void StopUpdate() = 0;
|
||||||
|
ScaleData ScaleElement(const UiElement& element);
|
||||||
|
};
|
||||||
|
|
||||||
Window() requires (!Framebuffer &&!Title && !Keyboard && !Mouse) : title(*this) {}
|
class WindowKeyboard {
|
||||||
Window(const std::string_view title) requires (!Framebuffer &&!Title && !Keyboard && !Mouse) : title(*this, title) {}
|
public:
|
||||||
|
bool heldkeys[255] = {};
|
||||||
|
Event<void> onKeyDown[255];
|
||||||
|
Event<void> onKeyHold[255];
|
||||||
|
Event<void> onKeyUp[255];
|
||||||
|
Event<char> onAnyKeyDown;
|
||||||
|
Event<char> onAnyKeyHold;
|
||||||
|
Event<char> onAnyKeyUp;
|
||||||
|
};
|
||||||
|
|
||||||
Window(std::uint_fast32_t width, std::uint_fast32_t height, const std::string_view title) requires (Framebuffer && Title && !Keyboard && !Mouse) : framebuffer(*this, width, height), title(*this, title) {}
|
class UiElementMouse;
|
||||||
|
class WindowMouse {
|
||||||
~Window() {
|
public:
|
||||||
std::cout << "destr" << std::endl;
|
Event<MousePoint> onMouseRightClick;
|
||||||
#ifdef CRAFTER_GRAPHICS_WAYLAND
|
Event<MousePoint> onMouseLeftClick;
|
||||||
xdg_toplevel_destroy(xdgToplevel);
|
Event<MousePoint> onMouseRightHold;
|
||||||
xdg_surface_destroy(xdgSurface);
|
Event<MousePoint> onMouseLeftHold;
|
||||||
wl_surface_destroy(surface);
|
Event<MousePoint> onMouseRightRelease;
|
||||||
#endif
|
Event<MousePoint> onMouseLeftRelease;
|
||||||
}
|
Event<MouseMoveEvent> onMouseMove;
|
||||||
|
Event<MouseMoveEvent> onMouseEnter;
|
||||||
|
Event<MouseMoveEvent> onMouseLeave;
|
||||||
|
Event<std::uint_fast32_t> onMouseScroll;
|
||||||
|
MousePoint currentMousePos;
|
||||||
|
MousePoint lastMousePos;
|
||||||
|
MousePoint mouseDelta;
|
||||||
|
bool mouseLeftHeld = false;
|
||||||
|
bool mouseRightHeld = false;
|
||||||
|
std::vector<UiElementMouse*> mouseElements;
|
||||||
|
};
|
||||||
|
|
||||||
// template<bool Buffer, bool Mouse>
|
class WindowTitle {
|
||||||
// UiElement<Buffer, Mouse>& AddElement(float anchorX, float anchorY, float relativeWidth, float relativeHeight, float anchorOffsetX = 0.5, float anchorOffsetY = 0.5, float z = 0, bool ignoreScaling = false) requires(Buffer == true) {
|
public:
|
||||||
|
virtual void SetTitle(const std::string_view title) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
// }
|
class WindowFramebuffer {
|
||||||
template<bool UiBuffer, bool UiMouse>
|
public:
|
||||||
UiElement<UiBuffer, UiMouse>& CreateElement(std::uint_fast32_t width, std::uint_fast32_t height, float anchorX, float anchorY, float relativeWidth, float relativeHeight, float anchorOffsetX = 0.5, float anchorOffsetY = 0.5, float z = 0, bool ignoreScaling = false) requires(UiBuffer && Framebuffer) {
|
std::uint_fast32_t width;
|
||||||
std::unique_ptr<UiElement<UiBuffer, UiMouse>> element = std::make_unique<UiElement<UiBuffer, UiMouse>>(width, height, anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling);
|
std::uint_fast32_t height;
|
||||||
UiElement<UiBuffer, UiMouse>* ptr = element.get();
|
WindowFramebuffer() = default;
|
||||||
framebuffer.elements.push_back(&element->buffer);
|
WindowFramebuffer(std::uint_fast32_t width, std::uint_fast32_t height);
|
||||||
if constexpr(UiMouse) {
|
virtual void Resize(std::uint_fast32_t width, std::uint_fast32_t height) = 0;
|
||||||
mouse.elements.push_back(&element->mouse);
|
virtual void Write(Pixel_BU8_GU8_RU8_AU8* pixels) = 0;
|
||||||
}
|
virtual void Write(std::uint_fast32_t x, std::uint_fast32_t y, Pixel_BU8_GU8_RU8_AU8 pixel) = 0;
|
||||||
elements.emplace_back(std::move(element));
|
virtual Pixel_BU8_GU8_RU8_AU8 Read(std::uint_fast32_t x, std::uint_fast32_t y) const = 0;
|
||||||
return *ptr;
|
virtual const Pixel_BU8_GU8_RU8_AU8* Read() const = 0;
|
||||||
}
|
virtual Pixel_BU8_GU8_RU8_AU8* Get() = 0;
|
||||||
template<bool UiBuffer, bool UiMouse>
|
virtual void Store() = 0;
|
||||||
void RemoveElement(UiElement<UiBuffer, UiMouse>& element) {
|
virtual void Render() = 0;
|
||||||
if constexpr(UiBuffer) {
|
ScaleData ScaleElementAbsolute(const UiElement& element);
|
||||||
framebuffer.elements.erase(std::find(framebuffer.elements.begin(), framebuffer.elements.end(), &element.buffer));
|
};
|
||||||
}
|
|
||||||
for(std::uint_fast32_t i = 0; i < elements.size(); i++) {
|
|
||||||
if(elements[i].get() == &element) {
|
|
||||||
elements.erase(elements.begin() + i);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
#ifdef CRAFTER_GRAPHICS_WAYLAND
|
||||||
#ifdef CRAFTER_GRAPHICS_WAYLAND
|
class UiElementBufferBuffer;
|
||||||
void InitWayland() {
|
class WindowWayland final: public Window, public WindowKeyboard, public WindowMouse, public WindowFramebuffer, public WindowTitle {
|
||||||
std::cout << "bruh" << std::endl;
|
public:
|
||||||
display = wl_display_connect(NULL);
|
Pixel_BU8_GU8_RU8_AU8* framebuffer = nullptr;
|
||||||
if (display == NULL) {
|
std::vector<UiElementBufferBuffer*> elements;
|
||||||
std::cerr << "failed to create display" << std::endl;
|
WindowWayland(std::uint_fast32_t width, std::uint_fast32_t height);
|
||||||
}
|
WindowWayland(std::uint_fast32_t width, std::uint_fast32_t height, const std::string_view title);
|
||||||
|
~WindowWayland();
|
||||||
wl_registry* registry = wl_display_get_registry(display);
|
bool configured = false;
|
||||||
wl_registry_add_listener(registry, ®istry_listener, this);
|
wl_shm* shm = nullptr;
|
||||||
if (wl_display_roundtrip(display) == -1) {
|
wl_seat* seat = nullptr;
|
||||||
exit(EXIT_FAILURE);
|
xdg_toplevel* xdgToplevel = nullptr;
|
||||||
}
|
xdg_wm_base* xdgWmBase = nullptr;
|
||||||
if (shm == NULL || compositor == NULL || xdgWmBase == NULL) {
|
zxdg_decoration_manager_v1* manager = nullptr;
|
||||||
std::cerr << "no wl_shm, wl_compositor or xdg_wm_base support" << std::endl;
|
wl_surface* surface = nullptr;
|
||||||
exit(EXIT_FAILURE);
|
wl_buffer* buffer = nullptr;
|
||||||
}
|
wl_buffer* backBuffer = nullptr;
|
||||||
|
xdg_surface* xdgSurface = nullptr;
|
||||||
surface = wl_compositor_create_surface(compositor);
|
wl_display* display = nullptr;
|
||||||
xdgSurface = xdg_wm_base_get_xdg_surface(xdgWmBase, surface);
|
wl_callback* cb = nullptr;
|
||||||
xdgToplevel = xdg_surface_get_toplevel(xdgSurface);
|
xkb_keymap* xkb_keymap;
|
||||||
cb = wl_surface_frame(surface);
|
xkb_context* xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
|
||||||
|
xkb_state* xkb_state;
|
||||||
//wl_callback_add_listener(cb, &surface_frame_listener, this);
|
void Render() override;
|
||||||
xdg_surface_add_listener(xdgSurface, &xdg_surface_listener, this);
|
void StartSync() override;
|
||||||
xdg_toplevel_add_listener(xdgToplevel, &xdg_toplevel_listener, this);
|
void StartUpdate() override;
|
||||||
wl_surface_commit(surface);
|
void StopUpdate() override;
|
||||||
|
void SetTitle(const std::string_view title) override;
|
||||||
while (wl_display_dispatch(display) != -1 && !configured) {}
|
void Resize(std::uint_fast32_t width, std::uint_fast32_t height) override;
|
||||||
|
void Write(Pixel_BU8_GU8_RU8_AU8* pixels) override;
|
||||||
wl_surface_commit(surface);
|
void Write(std::uint_fast32_t x, std::uint_fast32_t y, Pixel_BU8_GU8_RU8_AU8 pixel) override;
|
||||||
|
Pixel_BU8_GU8_RU8_AU8 Read(std::uint_fast32_t x, std::uint_fast32_t y) const override;
|
||||||
zxdg_toplevel_decoration_v1* decoration = zxdg_decoration_manager_v1_get_toplevel_decoration(manager, xdgToplevel);
|
const Pixel_BU8_GU8_RU8_AU8* Read() const override;
|
||||||
zxdg_toplevel_decoration_v1_set_mode(decoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
|
Pixel_BU8_GU8_RU8_AU8* Get() override;
|
||||||
|
void Store() override;
|
||||||
wl_surface_commit(surface);
|
inline static wl_compositor* compositor = nullptr;
|
||||||
std::cout << "bruh" << std::endl;
|
static void wl_surface_frame_done(void *data, wl_callback *cb, uint32_t time);
|
||||||
open = true;
|
static void PointerListenerHandleMotion(void* data, wl_pointer* wl_pointer, uint time, wl_fixed_t surface_x, wl_fixed_t surface_y);
|
||||||
}
|
static void PointerListenerHandleAxis(void*, wl_pointer*, std::uint32_t, std::uint32_t, wl_fixed_t value);
|
||||||
|
static void PointerListenerHandleEnter(void* data, wl_pointer* wl_pointer, uint serial, wl_surface* surface, wl_fixed_t surface_x, wl_fixed_t surface_y);
|
||||||
static void wl_surface_frame_done(void* data, struct wl_callback *cb, uint32_t time) {
|
static void PointerListenerHandleLeave(void*, wl_pointer*, std::uint32_t, wl_surface*);
|
||||||
wl_callback_destroy(cb);
|
static void xdg_toplevel_handle_close(void* data, xdg_toplevel*);
|
||||||
Window<Framebuffer, Title, Keyboard, Mouse>* window = reinterpret_cast<Window<Framebuffer, Title, Keyboard, Mouse>*>(data);
|
static void handle_global(void* data, wl_registry* registry, std::uint32_t name, const char* interface, std::uint32_t version);
|
||||||
if(window->onUpdate.listeners.size() > 0) {
|
static void pointer_handle_button(void* data, wl_pointer* pointer, std::uint32_t serial, std::uint32_t time, std::uint32_t button, std::uint32_t state);
|
||||||
cb = wl_surface_frame(window->surface);
|
static void seat_handle_capabilities(void* data, wl_seat* seat, uint32_t capabilities);
|
||||||
wl_callback_add_listener(cb, &WindowBase::surface_frame_listener, window);
|
static void xdg_surface_handle_configure(void* data, xdg_surface* xdg_surface, std::uint32_t serial);
|
||||||
}
|
static void xdg_wm_base_handle_ping(void* data, xdg_wm_base* xdg_wm_base, std::uint32_t serial);
|
||||||
}
|
static void keyboard_keymap(void* data, wl_keyboard* keyboard, uint32_t format, int fd, uint32_t size);
|
||||||
constexpr static wl_callback_listener surface_frame_listener = {
|
static void keyboard_enter(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface, wl_array *keys);
|
||||||
.done = wl_surface_frame_done,
|
static void keyboard_leave(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface);
|
||||||
};
|
static void keyboard_key(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state);
|
||||||
|
static void keyboard_modifiers(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group);
|
||||||
static void xdg_wm_base_handle_ping(void* data, xdg_wm_base* xdg_wm_base, std::uint32_t serial) {
|
static void keyboard_repeat_info(void *data, wl_keyboard *keyboard, int32_t rate, int32_t delay);
|
||||||
xdg_wm_base_pong(xdg_wm_base, serial);
|
static void handle_global_remove(void* data, wl_registry* registry, uint32_t name);
|
||||||
}
|
static void xdg_toplevel_configure(void*, xdg_toplevel*, std::int32_t, std::int32_t, wl_array*);
|
||||||
|
|
||||||
constexpr static xdg_wm_base_listener xdgWmBaseListener = {
|
|
||||||
.ping = xdg_wm_base_handle_ping,
|
|
||||||
};
|
|
||||||
|
|
||||||
static void pointer_handle_button(void* data, wl_pointer* pointer, std::uint32_t serial, std::uint32_t time, std::uint32_t button, std::uint32_t state) {
|
|
||||||
Window<Framebuffer, Title, Keyboard, Mouse>* window = reinterpret_cast<<Window<Framebuffer, Title, Keyboard, Mouse>*>(data);
|
|
||||||
if (button == BTN_LEFT) {
|
|
||||||
if(state == WL_POINTER_BUTTON_STATE_PRESSED) {
|
|
||||||
window->mouseLeftHeld = true;
|
|
||||||
window->mouse.onMouseLeftClick.Invoke(window->mouse.currentMousePos);
|
|
||||||
if constexpr (Framebuffer) {
|
|
||||||
for(UiElementMouse& element : window->mouse.elements) {
|
|
||||||
ScaleData data = window->framebuffer.ScaleElement(element);
|
|
||||||
if(window->mouse.currentMousePos.x >= data.x && window->mouse.currentMousePos.x <= data.x+data.width && window->mouse.currentMousePos.y > data.y && window->mouse.currentMousePos.y < data.y+data.height) {
|
|
||||||
element.onMouseLeftClick.Invoke({window->mouse.currentMousePos.x-data.x, window->mouse.currentMousePos.y-data.y});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
window->mouse.mouseLeftHeld = false;
|
|
||||||
window->mouse.onMouseLeftRelease.Invoke(window->mouse.currentMousePos);
|
|
||||||
for(UiElement& element : window->mouse.elements) {
|
|
||||||
ScaleData data = window->mouse.ScaleElement(element);
|
|
||||||
if(window->mouse.currentMousePos.x >= data.x && window->mouse.currentMousePos.x <= data.x+data.width && window->mouse.currentMousePos.y > data.y && window->mouse.currentMousePos.y < data.y+data.height) {
|
|
||||||
element.onMouseLeftRelease.Invoke({window->mouse.currentMousePos.x-data.x, window->mouse.currentMousePos.y-data.y});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if(button == BTN_RIGHT){
|
|
||||||
if(state == WL_POINTER_BUTTON_STATE_PRESSED) {
|
|
||||||
window->mouse.mouseRightHeld = true;
|
|
||||||
window->mouse.onMouseRightClick.Invoke(window->mouse.currentMousePos);
|
|
||||||
for(UiElement& element : window->mouse.elements) {
|
|
||||||
ScaleData data = window->mouse.ScaleElement(element);
|
|
||||||
if(window->mouse.currentMousePos.x >= data.x && window->mouse.currentMousePos.x <= data.x+data.width && window->mouse.currentMousePos.y > data.y && window->mouse.currentMousePos.y < data.y+data.height) {
|
|
||||||
element.onMouseRightClick.Invoke({window->mouse.currentMousePos.x-data.x, window->mouse.currentMousePos.y-data.y});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
window->mouse.mouseRightHeld = true;
|
|
||||||
window->mouse.onMouseRightRelease.Invoke(window->mouse.currentMousePos);
|
|
||||||
for(UiElement& element : window->mouse.elements) {
|
|
||||||
ScaleData data = window->mouse.ScaleElement(element);
|
|
||||||
if(window->mouse.currentMousePos.x >= data.x && window->mouse.currentMousePos.x <= data.x+data.width && window->mouse.currentMousePos.y > data.y && window->mouse.currentMousePos.y < data.y+data.height) {
|
|
||||||
element.onMouseRightRelease.Invoke({window->mouse.currentMousePos.x-data.x, window->mouse.currentMousePos.y-data.y});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void PointerListenerHandleMotion(void* data, wl_pointer* wl_pointer, uint time, wl_fixed_t surface_x, wl_fixed_t surface_y) {
|
|
||||||
<Window<Framebuffer, Title, Keyboard, Mouse>* window = reinterpret_cast<<Window<Framebuffer, Title, Keyboard, Mouse>*>(data);
|
|
||||||
MousePoint pos = {wl_fixed_to_double(surface_x), wl_fixed_to_double(surface_y)};
|
|
||||||
window->mouse.lastMousePos = window->currentMousePos;
|
|
||||||
window->currentMousePos = pos;
|
|
||||||
window->mouseDelta = {window->currentMousePos.x-window->lastMousePos.x, window->currentMousePos.y-window->lastMousePos.y};
|
|
||||||
window->onMouseMove.Invoke({window->lastMousePos, window->currentMousePos, window->mouseDelta});
|
|
||||||
for(UiElement& element : window->elements) {
|
|
||||||
ScaleData data = window->ScaleElement(element);
|
|
||||||
if(window->currentMousePos.x >= data.x && window->currentMousePos.x <= data.x+data.width && window->currentMousePos.y > data.y && window->currentMousePos.y < data.y+data.height) {
|
|
||||||
element.onMouseMove.Invoke({window->currentMousePos.x-data.x, window->currentMousePos.y-data.y});
|
|
||||||
if(!(window->lastMousePos.x >= data.x && window->lastMousePos.x <= data.x+data.width && window->lastMousePos.y > data.y && window->lastMousePos.y < data.y+data.height)) {
|
|
||||||
element.onMouseEnter.Invoke({window->currentMousePos.x-data.x, window->currentMousePos.y-data.y});
|
|
||||||
}
|
|
||||||
} else if(window->lastMousePos.x >= data.x && window->lastMousePos.x <= data.x+data.width && window->lastMousePos.y > data.y && window->lastMousePos.y < data.y+data.height) {
|
|
||||||
element.onMouseLeave.Invoke({window->currentMousePos.x-data.x, window->currentMousePos.y-data.y});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void PointerListenerHandleEnter(void* data, wl_pointer* wl_pointer, uint serial, wl_surface* surface, wl_fixed_t surface_x, wl_fixed_t surface_y) {
|
|
||||||
<Window<Framebuffer, Title, Keyboard, Mouse>* window = reinterpret_cast<<Window<Framebuffer, Title, Keyboard, Mouse>*>(data);
|
|
||||||
window->onMouseEnter.Invoke({window->lastMousePos, window->currentMousePos, window->mouseDelta});
|
|
||||||
}
|
|
||||||
|
|
||||||
static void PointerListenerHandleLeave(void* data, wl_pointer*, std::uint32_t, wl_surface*) {
|
|
||||||
<Window<Framebuffer, Title, Keyboard, Mouse>* window = reinterpret_cast<<Window<Framebuffer, Title, Keyboard, Mouse>*>(data);
|
|
||||||
window->onMouseEnter.Invoke({window->lastMousePos, window->currentMousePos, window->mouseDelta});
|
|
||||||
}
|
|
||||||
|
|
||||||
static void PointerListenerHandleAxis(void*, wl_pointer*, std::uint32_t, std::uint32_t, wl_fixed_t value) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr static wl_pointer_listener pointer_listener = {
|
constexpr static wl_pointer_listener pointer_listener = {
|
||||||
.enter = PointerListenerHandleEnter,
|
.enter = PointerListenerHandleEnter,
|
||||||
.leave = PointerListenerHandleLeave,
|
.leave = PointerListenerHandleLeave,
|
||||||
|
|
@ -249,88 +180,6 @@ export namespace Crafter {
|
||||||
.button = pointer_handle_button,
|
.button = pointer_handle_button,
|
||||||
.axis = PointerListenerHandleAxis,
|
.axis = PointerListenerHandleAxis,
|
||||||
};
|
};
|
||||||
|
|
||||||
xkb_keymap* xkb_keymap;
|
|
||||||
xkb_context* xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
|
|
||||||
xkb_state* xkb_state;
|
|
||||||
|
|
||||||
|
|
||||||
static void keyboard_keymap(void *data, wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size) {
|
|
||||||
if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
|
|
||||||
close(fd);
|
|
||||||
fprintf(stderr, "Unsupported keymap format\n");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
void *map = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
|
|
||||||
if (map == MAP_FAILED) {
|
|
||||||
close(fd);
|
|
||||||
perror("mmap");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
|
|
||||||
xkb_keymap = xkb_keymap_new_from_string(xkb_context, (const char *)map, XKB_KEYMAP_FORMAT_TEXT_V1,XKB_KEYMAP_COMPILE_NO_FLAGS);
|
|
||||||
munmap(map, size);
|
|
||||||
close(fd);
|
|
||||||
|
|
||||||
xkb_state = xkb_state_new(xkb_keymap);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void keyboard_enter(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface, wl_array *keys) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
static void keyboard_leave(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
static void keyboard_key(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state) {
|
|
||||||
if (!xkb_state) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Window<Framebuffer, Title, Keyboard, Mouse>* window = reinterpret_cast<Window<Framebuffer, Title, Keyboard, Mouse>*>(data);
|
|
||||||
|
|
||||||
xkb_keycode_t keycode = key + 8;
|
|
||||||
xkb_keysym_t keysym = xkb_state_key_get_one_sym(xkb_state, keycode);
|
|
||||||
|
|
||||||
char utf8[8] = {0};
|
|
||||||
int len = xkb_keysym_to_utf8(keysym, utf8, sizeof(utf8));
|
|
||||||
if (len != 0) {
|
|
||||||
char keypress = utf8[0];
|
|
||||||
if(state == WL_KEYBOARD_KEY_STATE_PRESSED) {
|
|
||||||
if(window->heldkeys[keypress]) {
|
|
||||||
window->onKeyHold[keypress].Invoke();
|
|
||||||
window->onAnyKeyHold.Invoke(keypress);
|
|
||||||
} else{
|
|
||||||
window->heldkeys[keypress] = true;
|
|
||||||
window->onKeyDown[keypress].Invoke();
|
|
||||||
window->onAnyKeyDown.Invoke(keypress);
|
|
||||||
}
|
|
||||||
} else{
|
|
||||||
window->heldkeys[keypress] = false;
|
|
||||||
window->onKeyUp[keypress].Invoke();
|
|
||||||
window->onAnyKeyUp.Invoke(keypress);
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
// // fallback for keys like Return, Escape, etc.
|
|
||||||
// char name[64];
|
|
||||||
// if (xkb_keysym_get_name(keysym, name, sizeof(name)) > 0) {
|
|
||||||
// printf("Key %s pressed (non-printable or multi-char)\n", name);
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void keyboard_modifiers(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
static void keyboard_repeat_info(void *data, wl_keyboard *keyboard, int32_t rate, int32_t delay) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr static wl_keyboard_listener keyboard_listener = {
|
constexpr static wl_keyboard_listener keyboard_listener = {
|
||||||
.keymap = keyboard_keymap,
|
.keymap = keyboard_keymap,
|
||||||
.enter = keyboard_enter,
|
.enter = keyboard_enter,
|
||||||
|
|
@ -339,76 +188,26 @@ export namespace Crafter {
|
||||||
.modifiers = keyboard_modifiers,
|
.modifiers = keyboard_modifiers,
|
||||||
.repeat_info = keyboard_repeat_info,
|
.repeat_info = keyboard_repeat_info,
|
||||||
};
|
};
|
||||||
|
|
||||||
void seat_handle_capabilities(void* data, wl_seat* seat, uint32_t capabilities) {
|
|
||||||
Window<Framebuffer, Title, Keyboard, Mouse>* window = reinterpret_cast<Window<Framebuffer, Title, Keyboard, Mouse>*>(data);
|
|
||||||
window->seat = seat;
|
|
||||||
if constexpr(Mouse) {
|
|
||||||
wl_pointer* pointer = wl_seat_get_pointer(seat);
|
|
||||||
wl_pointer_add_listener(pointer, &pointer_listener, window);
|
|
||||||
}
|
|
||||||
if constexpr(Keyboard) {
|
|
||||||
wl_keyboard* keyboard = wl_seat_get_keyboard(seat);
|
|
||||||
wl_keyboard_add_listener(keyboard, &keyboard_listener, window);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
constexpr static wl_seat_listener seat_listener = {
|
constexpr static wl_seat_listener seat_listener = {
|
||||||
.capabilities = seat_handle_capabilities,
|
.capabilities = seat_handle_capabilities,
|
||||||
};
|
};
|
||||||
|
constexpr static wl_registry_listener registry_listener = {
|
||||||
|
|
||||||
static void handle_global(void *data, wl_registry *registry, std::uint32_t name, const char *interface, std::uint32_t version) {
|
|
||||||
Window<Framebuffer, Title, Keyboard, Mouse>* window = reinterpret_cast<Window<Framebuffer, Title, Keyboard, Mouse>*>(data);
|
|
||||||
if (strcmp(interface, wl_shm_interface.name) == 0) {
|
|
||||||
window->shm = reinterpret_cast<wl_shm*>(wl_registry_bind(registry, name, &wl_shm_interface, 1));
|
|
||||||
} else if (strcmp(interface, wl_seat_interface.name) == 0) {
|
|
||||||
if constexpr (Keyboard || Mouse) {
|
|
||||||
wl_seat* seat = reinterpret_cast<wl_seat*>(wl_registry_bind(registry, name, &wl_seat_interface, 1));
|
|
||||||
wl_seat_add_listener(seat, &seat_listener, window);
|
|
||||||
}
|
|
||||||
} else if (compositor == NULL && strcmp(interface, wl_compositor_interface.name) == 0) {
|
|
||||||
compositor = reinterpret_cast<wl_compositor*>(wl_registry_bind(registry, name, &wl_compositor_interface, 1));
|
|
||||||
} else if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
|
|
||||||
window->xdgWmBase = reinterpret_cast<xdg_wm_base*>(wl_registry_bind(registry, name, &xdg_wm_base_interface, 1));
|
|
||||||
xdg_wm_base_add_listener(window->xdgWmBase, &xdgWmBaseListener, NULL);
|
|
||||||
} else if (strcmp(interface, zxdg_decoration_manager_v1_interface.name) == 0) {
|
|
||||||
window->manager = reinterpret_cast<zxdg_decoration_manager_v1*>(wl_registry_bind(registry, name, &zxdg_decoration_manager_v1_interface, 1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
void handle_global_remove(void* data, wl_registry* registry, uint32_t name) {}
|
|
||||||
constexpr wl_registry_listener registry_listener = {
|
|
||||||
.global = handle_global,
|
.global = handle_global,
|
||||||
.global_remove = handle_global_remove,
|
.global_remove = handle_global_remove,
|
||||||
};
|
};
|
||||||
|
constexpr static xdg_toplevel_listener xdg_toplevel_listener = {
|
||||||
static void xdg_surface_handle_configure(void* data, xdg_surface* xdg_surface, std::uint32_t serial) {
|
.configure = xdg_toplevel_configure,
|
||||||
Window<Framebuffer, Title, Keyboard, Mouse>* window = reinterpret_cast<Window<Framebuffer, Title, Keyboard, Mouse>*>(data);
|
.close = xdg_toplevel_handle_close,
|
||||||
// The compositor configures our surface, acknowledge the configure event
|
};
|
||||||
xdg_surface_ack_configure(xdg_surface, serial);
|
constexpr static wl_callback_listener wl_callback_listener = {
|
||||||
|
.done = wl_surface_frame_done,
|
||||||
if (window->configured) {
|
};
|
||||||
// If this isn't the first configure event we've received, we already
|
constexpr static xdg_wm_base_listener xdgWmBaseListener = {
|
||||||
// have a buffer attached, so no need to do anything. Commit the
|
.ping = xdg_wm_base_handle_ping,
|
||||||
// surface to apply the configure acknowledgement.
|
};
|
||||||
wl_surface_commit(window->surface);
|
constexpr static xdg_surface_listener xdg_surface_listener = {
|
||||||
}
|
|
||||||
|
|
||||||
window->configured = true;
|
|
||||||
}
|
|
||||||
constexpr xdg_surface_listener xdg_surface_listener = {
|
|
||||||
.configure = xdg_surface_handle_configure,
|
.configure = xdg_surface_handle_configure,
|
||||||
};
|
};
|
||||||
|
|
||||||
void noop5(void*, xdg_toplevel*, std::int32_t, std::int32_t, wl_array*){}
|
|
||||||
static void xdg_toplevel_handle_close(void* data, xdg_toplevel*) {
|
|
||||||
WindowBase* window = reinterpret_cast<WindowBase*>(data);
|
|
||||||
window->onClose.Invoke();
|
|
||||||
window->open = false;
|
|
||||||
}
|
|
||||||
constexpr xdg_toplevel_listener xdg_toplevel_listener = {
|
|
||||||
.configure = noop5,
|
|
||||||
.close = WindowBase::xdg_toplevel_handle_close,
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
@ -1,72 +0,0 @@
|
||||||
/*
|
|
||||||
Crafter®.Graphics
|
|
||||||
Copyright (C) 2025 Catcrafts®
|
|
||||||
catcrafts.net
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License version 3.0 as published by the Free Software Foundation;
|
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
module;
|
|
||||||
|
|
||||||
#ifdef CRAFTER_GRAPHICS_WAYLAND
|
|
||||||
#include <xkbcommon/xkbcommon.h>
|
|
||||||
#include "../lib/xdg-shell-client-protocol.h"
|
|
||||||
#include "../lib/wayland-xdg-decoration-unstable-v1-client-protocol.h"
|
|
||||||
#include <wayland-cursor.h>
|
|
||||||
#include <xkbcommon/xkbcommon.h>
|
|
||||||
#include <wayland-client.h>
|
|
||||||
#include <wayland-client-protocol.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
export module Crafter.Graphics:WindowBase;
|
|
||||||
import std;
|
|
||||||
import :Types;
|
|
||||||
import Crafter.Event;
|
|
||||||
|
|
||||||
export namespace Crafter {
|
|
||||||
enum WindowCapabilities {
|
|
||||||
CRAFTER_WINDOWCAPTABILITIES_FRAMEBUFFER = 0b1,
|
|
||||||
CRAFTER_WINDOWCAPTABILITIES_KEYBOARD = 0b10,
|
|
||||||
CRAFTER_WINDOWCAPTABILITIES_MOUSE = 0b100,
|
|
||||||
};
|
|
||||||
|
|
||||||
class UiElementBase;
|
|
||||||
class WindowBase {
|
|
||||||
public:
|
|
||||||
Event<void> onClose;
|
|
||||||
Event<void> onUpdate;
|
|
||||||
bool open;
|
|
||||||
std::vector<std::unique_ptr<UiElementBase>> elements;
|
|
||||||
WindowBase(WindowBase&) = delete;
|
|
||||||
WindowBase(WindowBase&&) = delete;
|
|
||||||
WindowBase& operator=(const WindowBase&) = delete;
|
|
||||||
void StartSync();
|
|
||||||
void StartUpdate(void* window);
|
|
||||||
void StopUpdate();
|
|
||||||
#ifdef CRAFTER_GRAPHICS_WAYLAND
|
|
||||||
WindowBase();
|
|
||||||
~WindowBase();
|
|
||||||
#endif
|
|
||||||
protected:
|
|
||||||
friend class WindowKeyboard;
|
|
||||||
friend class WindowFramebuffer;
|
|
||||||
friend class WindowTitle;
|
|
||||||
#ifdef CRAFTER_GRAPHICS_WAYLAND
|
|
||||||
wl_surface* surface = nullptr;
|
|
||||||
wl_callback* cb = nullptr;
|
|
||||||
static void wl_surface_frame_done(void* data, struct wl_callback *cb, uint32_t time);
|
|
||||||
static wl_callback_listener surface_frame_listener;
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
@ -1,61 +0,0 @@
|
||||||
/*
|
|
||||||
Crafter®.Graphics
|
|
||||||
Copyright (C) 2025 Catcrafts®
|
|
||||||
catcrafts.net
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License version 3.0 as published by the Free Software Foundation;
|
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
module;
|
|
||||||
|
|
||||||
#ifdef CRAFTER_GRAPHICS_WAYLAND
|
|
||||||
#include <wayland-client.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
export module Crafter.Graphics:WindowFramebuffer;
|
|
||||||
import std;
|
|
||||||
import :Types;
|
|
||||||
import :UiElement;
|
|
||||||
import :WindowBase;
|
|
||||||
import Crafter.Event;
|
|
||||||
|
|
||||||
export namespace Crafter {
|
|
||||||
class WindowBase;
|
|
||||||
class UiElementBase;
|
|
||||||
class UiElementBuffer;
|
|
||||||
class WindowFramebuffer {
|
|
||||||
public:
|
|
||||||
std::vector<UiElementBuffer*> elements;
|
|
||||||
Pixel_BU8_GU8_RU8_AU8* framebuffer;
|
|
||||||
std::uint_fast32_t width;
|
|
||||||
std::uint_fast32_t height;
|
|
||||||
float scale = 1;
|
|
||||||
WindowFramebuffer(WindowBase& window);
|
|
||||||
WindowFramebuffer(WindowBase& window, std::uint_fast32_t width, std::uint_fast32_t height);
|
|
||||||
WindowFramebuffer(WindowFramebuffer&) = delete;
|
|
||||||
WindowFramebuffer(WindowFramebuffer&&) = delete;
|
|
||||||
WindowFramebuffer& operator=(const WindowFramebuffer&) = delete;
|
|
||||||
ScaleData ScaleElement(const UiElementBase& element);
|
|
||||||
|
|
||||||
void Render();
|
|
||||||
void Create(std::uint_fast32_t width, std::uint_fast32_t height);
|
|
||||||
void Resize(std::uint_fast32_t width, std::uint_fast32_t height);
|
|
||||||
void Destroy();
|
|
||||||
~WindowFramebuffer();
|
|
||||||
private:
|
|
||||||
WindowBase& window;
|
|
||||||
#ifdef CRAFTER_GRAPHICS_WAYLAND
|
|
||||||
wl_buffer* buffer;
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
@ -1,35 +0,0 @@
|
||||||
/*
|
|
||||||
Crafter®.Graphics
|
|
||||||
Copyright (C) 2025 Catcrafts®
|
|
||||||
catcrafts.net
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License version 3.0 as published by the Free Software Foundation;
|
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
export module Crafter.Graphics:WindowKeyboard;
|
|
||||||
import std;
|
|
||||||
import Crafter.Event;
|
|
||||||
|
|
||||||
export namespace Crafter {
|
|
||||||
class WindowKeyboard {
|
|
||||||
public:
|
|
||||||
bool heldkeys[255] = {};
|
|
||||||
Event<void> onKeyDown[255];
|
|
||||||
Event<void> onKeyHold[255];
|
|
||||||
Event<void> onKeyUp[255];
|
|
||||||
Event<char> onAnyKeyDown;
|
|
||||||
Event<char> onAnyKeyHold;
|
|
||||||
Event<char> onAnyKeyUp;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
@ -1,46 +0,0 @@
|
||||||
/*
|
|
||||||
Crafter®.Graphics
|
|
||||||
Copyright (C) 2025 Catcrafts®
|
|
||||||
catcrafts.net
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License version 3.0 as published by the Free Software Foundation;
|
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
|
|
||||||
export module Crafter.Graphics:WindowKeyboard;
|
|
||||||
import std;
|
|
||||||
import Crafter.Event;
|
|
||||||
|
|
||||||
export namespace Crafter {
|
|
||||||
class UiElementMouse;
|
|
||||||
class WindowKeyboard {
|
|
||||||
public:
|
|
||||||
std::vector<UiElementMouse*> elements;
|
|
||||||
Event<MousePoint> onMouseRightClick;
|
|
||||||
Event<MousePoint> onMouseLeftClick;
|
|
||||||
Event<MousePoint> onMouseRightHold;
|
|
||||||
Event<MousePoint> onMouseLeftHold;
|
|
||||||
Event<MousePoint> onMouseRightRelease;
|
|
||||||
Event<MousePoint> onMouseLeftRelease;
|
|
||||||
Event<MouseMoveEvent> onMouseMove;
|
|
||||||
Event<MouseMoveEvent> onMouseEnter;
|
|
||||||
Event<MouseMoveEvent> onMouseLeave;
|
|
||||||
Event<double> onMouseScroll;
|
|
||||||
Event<void> onClose;
|
|
||||||
MousePoint currentMousePos;
|
|
||||||
MousePoint lastMousePos;
|
|
||||||
MousePoint mouseDelta;
|
|
||||||
bool mouseLeftHeld = false;
|
|
||||||
bool mouseRightHeld = false;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
@ -1,40 +0,0 @@
|
||||||
/*
|
|
||||||
Crafter®.Graphics
|
|
||||||
Copyright (C) 2025 Catcrafts®
|
|
||||||
catcrafts.net
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU Lesser General Public
|
|
||||||
License version 3.0 as published by the Free Software Foundation;
|
|
||||||
|
|
||||||
This library is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
Lesser General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Lesser General Public
|
|
||||||
License along with this library; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*/
|
|
||||||
module;
|
|
||||||
|
|
||||||
#ifdef CRAFTER_GRAPHICS_WAYLAND
|
|
||||||
#include <wayland-client.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
export module Crafter.Graphics:WindowTitle;
|
|
||||||
import std;
|
|
||||||
import :Types;
|
|
||||||
import Crafter.Event;
|
|
||||||
|
|
||||||
export namespace Crafter {
|
|
||||||
class WindowBase;
|
|
||||||
class WindowTitle {
|
|
||||||
public:
|
|
||||||
WindowTitle(WindowBase& window);
|
|
||||||
WindowTitle(WindowBase& window, const std::string_view title);
|
|
||||||
void Set(const std::string_view title);
|
|
||||||
private:
|
|
||||||
WindowBase& window;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
@ -22,12 +22,8 @@ export module Crafter.Graphics;
|
||||||
|
|
||||||
export import :Window;
|
export import :Window;
|
||||||
export import :UiElement;
|
export import :UiElement;
|
||||||
export import :UiElementBuffer;
|
|
||||||
export import :UiElementMouse;
|
|
||||||
export import :Types;
|
export import :Types;
|
||||||
export import :Font;
|
export import :Font;
|
||||||
export import :WindowKeyboard;
|
|
||||||
export import :WindowFramebuffer;
|
|
||||||
export import :Shm;
|
export import :Shm;
|
||||||
|
|
||||||
// export import :WindowWaylandVulkan;
|
// export import :WindowWaylandVulkan;
|
||||||
|
|
|
||||||
|
|
@ -3,13 +3,13 @@
|
||||||
"configurations": [
|
"configurations": [
|
||||||
{
|
{
|
||||||
"name": "base",
|
"name": "base",
|
||||||
"implementations": ["implementations/Crafter.Graphics-Font", "implementations/Crafter.Graphics-Shm", "implementations/Crafter.Graphics-WindowFramebuffer", "implementations/Crafter.Graphics-WindowFramebuffer", "implementations/Crafter.Graphics-UiElement"],
|
"implementations": ["implementations/Crafter.Graphics-Font", "implementations/Crafter.Graphics-Shm", "implementations/Crafter.Graphics-UiElement", "implementations/Crafter.Graphics-UiElementBufferBuffer", "implementations/Crafter.Graphics-UiElementBufferBufferBase", "implementations/Crafter.Graphics-UiElementBufferMouseBuffer"],
|
||||||
"interfaces": ["interfaces/Crafter.Graphics-Window", "interfaces/Crafter.Graphics-WindowBase", "interfaces/Crafter.Graphics", "interfaces/Crafter.Graphics-Types", "interfaces/Crafter.Graphics-Font", "interfaces/Crafter.Graphics-WindowKeyboard", "interfaces/Crafter.Graphics-WindowFramebuffer", "interfaces/Crafter.Graphics-WindowTitle", "interfaces/Crafter.Graphics-Shm", "interfaces/Crafter.Graphics-UiElement", "interfaces/Crafter.Graphics-UiElementBuffer", "interfaces/Crafter.Graphics-UiElementMouse"],
|
"interfaces": ["interfaces/Crafter.Graphics-Window", "interfaces/Crafter.Graphics", "interfaces/Crafter.Graphics-Types", "interfaces/Crafter.Graphics-Font", "interfaces/Crafter.Graphics-Shm", "interfaces/Crafter.Graphics-UiElement"],
|
||||||
"type": "library"
|
"type": "library"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "wayland",
|
"name": "wayland",
|
||||||
"implementations": ["implementations/Crafter.Graphics-Window_wayland", "implementations/Crafter.Graphics-WindowFramebuffer_wayland", "implementations/Crafter.Graphics-WindowTitle_wayland", "implementations/Crafter.Graphics-UiElementBuffer"],
|
"implementations": ["implementations/Crafter.Graphics-Window_wayland"],
|
||||||
"interfaces": [],
|
"interfaces": [],
|
||||||
"libs": ["wayland-client", "xkbcommon"],
|
"libs": ["wayland-client", "xkbcommon"],
|
||||||
"c_files": ["lib/xdg-shell-protocol", "lib/wayland-xdg-decoration-unstable-v1-client-protocol"],
|
"c_files": ["lib/xdg-shell-protocol", "lib/wayland-xdg-decoration-unstable-v1-client-protocol"],
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue