Crafter.Graphics/examples/HelloUI/main.cpp

44 lines
2.4 KiB
C++

import Crafter.Event;
import Crafter.Graphics;
import std;
using namespace Crafter;
int main() {
WindowWayland window(1280, 720, "Hello Input!");
UiElement* element = new UiElement(
2, //bufferWidth: the width of this elements pixel buffer
1, //bufferHeight: the height of this elements pixel buffer
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)
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
);
window.elements.push_back(element);
window.mouseElements.push_back(element);
element->buffer = {{255, 0, 0 ,255}, {0, 255, 0 ,255}};
element->UpdatePosition(window);
window.ScaleMouse(element->mouseTransform, element->transform);
EventListener<MousePoint> clickListener(&element->onMouseLeftClick, [element, &window](MousePoint point){
// Print the coordinates where the user clicked relative to the element's top left corner.
//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
std::cout << std::format("Clicked on Screen X:{} Y:{}!\n", MappedToPixelBoundless(point.x, MappedToPixelBoundless(element->mouseTransform.scaled.width, window.width)), MappedToPixelBoundless(point.y, window.height)) << std::endl;
});
window.Render();
window.StartSync();
}