commit
This commit is contained in:
parent
dfe9b1abe9
commit
c82c8c0887
35 changed files with 245 additions and 10 deletions
49
examples/HelloInput/README.md
Normal file
49
examples/HelloInput/README.md
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
# HelloInput Example
|
||||
|
||||
## Description
|
||||
|
||||
This example demonstrates how to handle basic input events by:
|
||||
|
||||
- Create a window using `WindowWaylandWayland`
|
||||
- Register event listeners using `EventListener<T>` for:
|
||||
- Mouse click events
|
||||
- Specific key events
|
||||
- General keypress events
|
||||
- Print formatted feedback to the console when events are triggered
|
||||
|
||||
## Expected Result
|
||||
|
||||
When you interact with the window, you might see console output like:
|
||||
|
||||
Clicked on X:450 Y:320!
|
||||
Pressed specifically the a key!
|
||||
Pressed the b key!
|
||||
|
||||
Make sure that the window has focus.
|
||||
|
||||
## Highlighted Code Snippet
|
||||
|
||||
```cpp
|
||||
EventListener<MousePoint> clickListener(&window.onMouseLeftClick, [](MousePoint point){
|
||||
std::cout << std::format("Clicked on X:{} Y:{}!", point.x, point.y);
|
||||
});
|
||||
|
||||
EventListener<void> keyAListener(&window.onKeyDown['a'], [](){
|
||||
std::cout << std::format("Pressed specifically the a key!");
|
||||
});
|
||||
|
||||
EventListener<char> anyKeyListener(&window.onAnyKeyDown, [](char key){
|
||||
std::cout << std::format("Pressed the {} key!", key);
|
||||
});
|
||||
```
|
||||
|
||||
## How to Run
|
||||
|
||||
```bash
|
||||
crafter-build -c example -r
|
||||
```
|
||||
|
||||
## Relevant documentation
|
||||
|
||||
[Window](https://crafter-graphics.docs.catcrafts.net/classCrafter_1_1Window.html)
|
||||
|
||||
57
examples/HelloInput/main.cpp
Normal file
57
examples/HelloInput/main.cpp
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
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
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <format>
|
||||
|
||||
import Crafter.Graphics;
|
||||
//Needed for events
|
||||
import Crafter.Event;
|
||||
using namespace Crafter;
|
||||
|
||||
int main() {
|
||||
// Create a Wayland window named "HelloWindow" with resolution 1280x720
|
||||
// (window creation explained in HelloWindow example)
|
||||
WindowWaylandWayland window("HelloWindow", 1280, 720);
|
||||
|
||||
// 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
|
||||
EventListener<MousePoint> clickListener(&window.onMouseLeftClick, [](MousePoint point){
|
||||
// Print the coordinates where the user clicked
|
||||
std::cout << std::format("Clicked on X:{} Y:{}!", point.x, point.y) << std::endl;
|
||||
});
|
||||
|
||||
// Listen specifically for the 'a' key being pressed down
|
||||
// The callback takes no parameters since the key is fixed
|
||||
EventListener<void> keyAListener(&window.onKeyDown['a'], [](){
|
||||
// Print confirmation of 'a' key press
|
||||
std::cout << std::format("Pressed specifically the a key!") << std::endl;
|
||||
});
|
||||
|
||||
// Listen for any key press on the window
|
||||
// The callback receives the character of the key pressed
|
||||
EventListener<char> anyKeyListener(&window.onAnyKeyDown, [](char key){
|
||||
// Print which key was pressed
|
||||
std::cout << std::format("Pressed the {} key!", key) << std::endl;
|
||||
});
|
||||
|
||||
// Start the window event loop, unless the window is started events will not trigger.
|
||||
window.StartSync();
|
||||
}
|
||||
22
examples/HelloInput/project.json
Normal file
22
examples/HelloInput/project.json
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
{
|
||||
"name": "crafter-graphics",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "example",
|
||||
"standard": "c++26",
|
||||
"source_files": ["main"],
|
||||
"module_files": [],
|
||||
"build_dir": "build",
|
||||
"output_dir": "bin",
|
||||
"type":"executable",
|
||||
"libs": [],
|
||||
"flags": ["-Wno-uninitialized"],
|
||||
"dependencies": [
|
||||
{
|
||||
"path":"../../project.json",
|
||||
"configuration":"lib-debug"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue