How to track QML particle death - qml

I want to do something with JavaScript when the QML particle dies.
I use the following to check particle death but it's not stable:
onAffectParticles: {
for(const particle of particles) {
if(particle.lifeLeft() < 0.02) {
// do something
}
}
}

Related

How to update the map properly using canvas and a button when the players relocate to a new location?

My game snapshot Attachment> Blockquote
My canvas constructs
public class DrawingView4 extends View{
DrawingView4(Context context4)
{
super(context4);
}
#Override protected void onDraw(Canvas canvas4)
{
int tile4=0;
if (DoneDrawFacilities && doneloading) {
}
else {
for (int i4=0; i4< 17 && notfixingorientation; i4++){
if (i4< 16) {
for (int ii4=0; ii4 < 16; ii4++){
try {
//Checking the data of all spots of the game map from package directory.
//Then Draw in canvas if the data of the spot occupied by type of human and core facilities,
FacilityList = new Gson().fromJson(FileUtil.readFile(FileUtil.getPackageDataDir(getApplicationContext()).concat("/GameResource/Tile".concat(String.valueOf((long)(tile4 + 1)).concat(".data")))), new TypeToken<ArrayList<HashMap<String, Object>>>(){}.getType());
if (FacilityList.get((int)0).get("Type").toString().equals("Human")) {
canvas4.drawBitmap(BitmapFactory.decodeFile(AllObjects.getString(String.valueOf((long)(tile4)), "")),null,new Rect(ii4*120, i4*120, 120*(ii4+1),120*(i4+1)), null);
}
if (FacilityList.get((int)0).get("Type").toString().equals("Core")) {
if (FacilityList.get((int)0).get("Name").toString().equals("Arena")) {
canvas4.drawBitmap(BitmapFactory.decodeFile(AllObjects.getString(String.valueOf((long)(tile4)), "")),null,new Rect(7*120, 7*120, 120*(8+1),120*(8+1)), null);
}
else {
}
}
} catch (Exception e) {
}
FacilityList.clear();
tile4++;
}
}
else {
DoneDrawFacilities = true;
}
}
}
}}
Blockquote
My Relocate button
//I use sharepreference called AllObjects rather than a list and I Only update the path of objects in specific tile in sharedpreference such as in variable 1, 2, etc. and then re-draw using this code below, next time I update the objects path and get this object path from the same json file in the package directory. Too decode to Bitmap and then Draw in canvas.
//Some other code are removed that just updating some data in specific file directory.
AA_structures_facilities.removeAllViews();
AA_structures_facilities.addView(new DrawingView4(GameActivity.this));
// But It freezes the screen or stop me from touching the touch event in a second everytime I update new canvas.
//WHILE MY touchevent is hundled in the parent LinearLayout where the canvas is placed.

How to access camera frames in flutter quickly

I would like to implement near real-time OCR on the camera feed of my flutter app. To do this I would like to access the camera data in a speedy manner.
As far as I can tell I have two options, and have hit roadblocks with both:
Take a screenshot of the CameraPreview by putting a RepaintBoundary around it and creating a RenderRepaintBoundary, and calling boundary.toImage(). The problem with this method is that the .toImage method only seems to capture the painted widgets in the boundary and not the data from the camera preview. Simmilar to the issue described here: https://github.com/flutter/flutter/issues/17687
Capture an image with controller.takePicture(filePath) from Camera 0.2.1, similar to the example docs. The problem here is that it takes super long before the image becomes available (2-3 seconds). I guess that this is because the file is saved to the disc on capture and then needs to be read from the file again.
Is there any way that one can directly access the picture information after capture, to do things like pre-process and OCR?
For "near real-time OCR", you need CameraController#startImageStream
example code
import 'package:camera/camera.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(home: _MyHomePage()));
class _MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<_MyHomePage> {
dynamic _scanResults;
CameraController _camera;
bool _isDetecting = false;
CameraLensDirection _direction = CameraLensDirection.back;
#override
void initState() {
super.initState();
_initializeCamera();
}
Future<CameraDescription> _getCamera(CameraLensDirection dir) async {
return await availableCameras().then(
(List<CameraDescription> cameras) => cameras.firstWhere(
(CameraDescription camera) => camera.lensDirection == dir,
),
);
}
void _initializeCamera() async {
_camera = CameraController(
await _getCamera(_direction),
defaultTargetPlatform == TargetPlatform.iOS
? ResolutionPreset.low
: ResolutionPreset.medium,
);
await _camera.initialize();
_camera.startImageStream((CameraImage image) {
if (_isDetecting) return;
_isDetecting = true;
try {
// await doSomethingWith(image)
} catch (e) {
// await handleExepction(e)
} finally {
_isDetecting = false;
}
});
}
Widget build(BuildContext context) {
return null;
}
}
This functionality was merged to https://github.com/flutter/plugins but it was not well documented.
Ref:
https://github.com/flutter/flutter/issues/26348
https://github.com/flutter/plugins/pull/965
https://github.com/bparrishMines/mlkit_demo/blob/master/lib/main.dart#L43
https://youtu.be/OAEWySye0BQ?t=1460
A better solution today (2022) for real-time OCR is to use the camera in a loop with a frequency of 500ms and process the image using google ML Kit's Text recognition.

QML iterating through model

I am trying to draw the content of a ListModel to a canvas in QML. The content of this model is displayed in a ListView elsewhere in the application, so I know the model is correctly filled with content.
No I am trying to update the canvas every time the model data changes:
import QtQuick 2.7
import QtQml.Models 2.2
Item {
Canvas {
anchors.fill: parent
id: canvas
onPaint: {
console.log("onPaint()")
var ctx = getContext("2d")
ctx.fillStyle = Qt.rgba(0, 0, 0, 1)
ctx.fillRect(0, 0, width, height)
console.log(particleListModel.count)
for(var i = 0; i < particleListModel.count; i++) {
console.log(i)
}
}
}
Connections {
target: particleListModel
onDataChanged: {
console.log("data changed")
canvas.requestPaint()
}
}
}
Once I change the data (in C++) I receive the dataChanged() signal and the onPaint() of the canvas gets called. However the debug output of
console.log(particleListModel.count)
is "undefined".
How can this be, while the regular ListView is able to display the content correctly?
When working with the Model you need to call the rowCount function instead of count since the latter is a property of the ListView and not of the model.
The following should work:
console.log(particleListModel.rowCount())

Pause application in QML when app is in background Symbian

I want to know of any pure QML way to find out whether the application is in the background or not and then accordingly stop or play music. In meego the alternate way to do is through the PlatformWindow Element but it does not exist in Symbian QML. Help needed please
Finally I got it working :) and i did it though Qt way... here are the steps
1) Create a class MyEventFilter
class myEventFilter : public QObject
{
bool eventFilter(QObject *obj, QEvent *event) {
switch(event->type()) {
case QEvent::WindowActivate:
emit qmlvisiblechange(true);
qDebug() << "Window activated";
bis_foreground=true;
return true;
case QEvent::WindowDeactivate:
emit qmlvisiblechange(false);
qDebug() << "Window deactivated";
bis_foreground=false;
return true;
default:
return false;
}
}
void dosomething();
private:
int something;
public:
bool bis_foreground;
Q_OBJECT
public slots:
Q_INVOKABLE QString checkvisibility() {
if (bis_foreground==true) return "true";
else return "false";
}
signals:
void qmlvisiblechange(bool is_foreground);
};
2) Then in main.cpp include this file include the class and add setContext propery like this
context->setContextProperty("myqmlobject", &ef);
3) in qml file call it like this:
Item {
id: name
Connections
{
target:myqmlobject
onQmlvisiblechange:
{
if(is_foreground)
{
//dont do anything...
}
else
{
playSound.stop()
}
}
}
}
Enjoy :)
Why do you need a pure QML way?
You can detect if an application has been sent to the background by installing an event filter.
Check: http://www.developer.nokia.com/Community/Wiki/Detecting_when_a_Qt_application_has_been_switched_to_the_background_and_when_resumed
For a "pure" QML way, there is the Symbian QML element:
http://doc.qt.nokia.com/qt-components-symbian/qml-symbian.html
It has a foreground property that indicates whether the app is in the foreground or in the background. You can try connecting to onForegroundChanged.
From the documentation, the Symbian element is not "creatable". It exists as a context property named symbian. So a sample usage would be:
import QtQuick 1.1
import com.nokia.symbian 1.1
PageStackWindow {
id: window
initialPage: MainPage {tools: toolBarLayout}
showStatusBar: true
showToolBar: true
function appForegroundChanged() {
console.log("Foreground: " + symbian.foreground)
}
function appCurrentTimeChanged() {
console.log("Current time: " + symbian.currentTime)
}
Component.onCompleted: {
symbian.currentTimeChanged.connect(appCurrentTimeChanged)
symbian.foregroundChanged.connect(appForegroundChanged)
}
ToolBarLayout {
id: toolBarLayout
ToolButton {
flat: true
iconSource: "toolbar-back"
onClicked: window.pageStack.depth <= 1 ? Qt.quit() : window.pageStack.pop()
}
}
}

Making custom widget on Gtk with Transparency

I'm using Gtk# and mono on Linux to make a program where I am creating a slider control over a timeline graph. I have it basically working, but there is one nagging problem that I can not figure out--- how to use transparency when using the Gtk.Style drawing routines such as "PaintBox" AND "PaintHLine". Because I could not figure out how to use these and still maintain transparency, I am currently using Cairo drawing routines, but this does not allow me to use the theme consistent look provided by the Style routines.
Currently, I have a custom widget class that has a Gtk.DrawingArea. I have a Cairo.ImageSurface kept in memory for the underlying Graph, and a Separate Cairo.ImageSurface for the "slider" which is supposed to be painted onto the DrawingArea to set a location in the timeline.
Problem is, I cannot for the life of me determine how to get my Gtk.Style.Paint... functions to draw on my Cairo.ImageSurface and still maintain transparency. Here is some sample code that is basically what I currently have:
private Cairo.ImageSurface graphImage;
private Cairo.ImageSurface gripImage;
private int grip_width;
private int grip_height;
public void MainClass() {
*...(initialization code here)...*
}
private override void OnExposeEvent() {
if (graphImage == null) {
graphImage = new Cairo.ImageSurface(Format.Rgba,graphDrawingArea.Allocation.Width,graphDrawingArea.Allocation.Height);
}
if (gripImage == null) {
gripImage = new Cairo.ImageSurface(Format.Rgba,grip_width,grip_height);
}
DrawGraph();
DrawGrip();
}
private override void OnResizeEvent() {
MakeGraph();
MakeGrip();
}
private void MakeGraph() {
using (Cairo.Context context = Gdk.CairoHelper(graphImage)) {
*...(use cairo drawing routines to draw the graph)...*
}
}
private void MakeGrip() {
using (Cairo.Context context = Gdk.CairoHelper(gripImage)) {
*...(use cairo drawing routines to draw the graph)...*
}
}
private void DrawGraph() {
using (Cairo.Context context = Gdk.CairoHelper(graphDrawingArea.GdkWindow)) {
*...(use cairo to "paint" the graphImage onto our drawing area at the proper location)...*
}
}