Crafter.Graphics/examples/HelloUI/main.cpp

45 lines
2.3 KiB
C++
Raw Normal View History

2025-06-13 23:59:36 +02:00
import Crafter.Event;
import Crafter.Graphics;
2025-11-16 18:52:52 +01:00
import std;
2025-06-13 23:59:36 +02:00
using namespace Crafter;
int main() {
2025-11-23 04:04:53 +01:00
WindowWayland window(1280, 720, "Hello Input!");
2025-06-13 23:59:36 +02:00
2025-11-26 02:56:38 +01:00
RenderingElement element(
2025-11-27 00:17:06 +01:00
OpaqueType::FullyOpaque,
2025-11-26 02:56:38 +01:00
2,
1,
2025-11-23 04:04:53 +01:00
FractionalToMapped(0.5), //anchorX: relative position where this elements x anchor (top-left) is placed to its parent x anchor
FractionalToMapped(0.5), //anchorY: relative position where this elements y anchor (top-left) is placed to its parent y anchor
FractionalToMapped(0.5), //relativeSizeX: the relative x 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
FractionalToMapped(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), //anchorOffsetY: the amount this element's anchor should be offset from the top left corner (0.5 to place it in the middle)
2025-06-14 01:45:33 +02:00
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
);
2025-11-25 20:30:54 +01:00
MouseElement mouse(window);
element.children.push_back(&mouse);
window.elements.push_back(&element);
2025-11-26 02:56:38 +01:00
element.buffer = {{255, 0, 0 ,255}, {0, 255, 0 ,255}};
2025-11-25 19:43:40 +01:00
element.UpdatePosition(window);
2025-11-23 04:04:53 +01:00
2025-11-25 20:30:54 +01:00
EventListener<MousePoint> clickListener(&mouse.onMouseLeftClick, [&mouse, &window](MousePoint point){
2025-11-23 04:04:53 +01:00
// Print the coordinates where the user clicked relative to the element's top left corner.
2025-11-24 01:47:49 +01:00
//Mapped space
std::cout << std::format("Clicked on Mapped X:{} Y:{}!", point.x, point.y) << std::endl;
// Fraction space
std::cout << std::format("Clicked on Fraction X:{} Y:{}!", MappedToFractionalBoundless(point.x), MappedToFractionalBoundless(point.y)) << std::endl;
// Screen space
2025-11-25 20:30:54 +01:00
std::cout << std::format("Clicked on Screen X:{} Y:{}!\n", MappedToPixelBoundless(point.x, MappedToPixelBoundless(mouse.scaled.width, window.width)), MappedToPixelBoundless(point.y, MappedToPixelBoundless(mouse.scaled.width, window.height))) << std::endl;
2025-11-23 04:04:53 +01:00
});
window.Render();
2025-06-13 23:59:36 +02:00
window.StartSync();
}