How to change QLabel Geometry periodically without blocking loops - user-interface

I do like to extend this answer especially the blink function to change the geometry of the label setGeometry, in particular, the starting coordinates of the label.
So according to duration, the label will change not only the color but also the position(Geometry) periodically.
Can you please tell me if that is possible (without blocking loops) and how can I do it please? thanks in advance.

Thanks to the suggestion of #musicamante here is the solution:
self.pos_anim = QPropertyAnimation(self, b"geometry")
def alarm_alert(self, pos1, pos2, duration=3000):
"""
Function: alarm_alert, to alarm the motion of the Animated QLabel.
---
Parameters:
#param: pos1, QRect, geometry of first position.
#param: pos2, QRect, geometry of second position.
#param:duration, int, motion duration in ms.
---
#return: None
"""
self.pos_anim.stop()
self.pos_anim.setDuration(duration)
self.pos_anim.setStartValue(pos2)
self.color_anim.setKeyValueAt(0.2, pos2)
self.color_anim.setKeyValueAt(0.6, pos1)
self.color_anim.setKeyValueAt(0.2, pos2)
self.pos_anim.setEndValue(pos1)
self.pos_anim.setLoopCount(-1)
self.pos_anim.start()

Related

gsap Flip.fit svg element not moving to expected coordinates because of { duration: x }

I am confused with gsap's Flip.fit moving to coordinates.
I have a game board with 182 tiles and 182 playing tiles.
The goal
When the user clicks the bag, a random playing tile is selected and is "supposed" to move over the tile on the board.
If you change
Flip.fit(PTILE[tileArray], TILE[tileArray], {duration: 1 , scale: true});
when changing { duration: 0, ... } the move works as expected, however no animation. When duration is above zero, the final location is very random.
codepen
I'm not sure how the duration affects the final position, however, I found a way to get the positions right. That is reset the transform of your PTILE before telling GSAP to do the Flip animation.
// reset transform value
gsap.set(PTILE[tileArray], { transform: "" });
// animate with new transform value
Flip.fit(PTILE[tileArray], TILE[tileArray], {
duration: 1,
scale: true
});
My reason is that PTITLE and TITLE are placed in different <g> tags which means their transform systems are inconsistent. Plus, Flip.fit() will act like gsap.to() with new TITLE position is the to object, GSAP will try to calculate the from object from your original transforms which are already set in the SVG as transform:matrix(). This process, somehow, is messing up. So what I did is give GSAP an exact transform value for the from object, which is empty.
Ok, I found out that Inkscape stores the SVG with inline transforms that threw the animation off. I tried saving in plain or optimised, but still had no luck.
So there are two solutions.
Use SVGOMG an online SVG cleaner.
Use Affinity Designer application which can export and flatten transforms.
The key to rule out other factors is to use relative coordinates and flatten transforms.
I have included a screenshot of Affinity exporting options.
Affinity Export screenshot

How to block possibility to edit/drag impoly in matlab?

I create a polygon on image_area in matlab.
I used impoly.
But after creation polygon.
I need to block possibility to move and drag impoly (ROI is already created).
I don't know how I should do it ?
I would appreciate for any help please.
You can set the makeConstrainToRectFcn such that it is a rectangle encompassing your ROI, then whenever you try to move the latter it won't work. You can also, after creating the ROI, set the setVerticesDraggable method to false in order to prevent vertices from being dragged.
Sample code (adapted from example by the Mathworks):
clc
clear
figure
imshow('gantrycrane.png');
h = impoly(gca, [188,30; 189,142; 93,141; 13,41; 14,29]);
%// Get currentposition
Pos = getPosition(h);
%// Prevent draggable vertices
setVerticesDraggable(h,0);
%// Set up rectangle to prvent movement of ROI
fcn = makeConstrainToRectFcn('impoly', [min(Pos(:,1)) max(Pos(:,1))], [min(Pos(:,2)) max(Pos(:,2))]);
%// Apply function
h.setPositionConstraintFcn(fcn);
which results in this kind of situation (with red rectangle for illustration):

Snap SVG animating existing matrix

I use Snap.svg to create a simple card game. I loaded drawed cards from file and moved them to specific location using matrix translate.
It's svg element now looks kinda like this:
<g id="card11" inkscape:label="#g3908" transform="matrix(1.5621,0,0,1.5621,625.1085,529.3716)" cardposition="4" style="visibility: visible;" class="card inhand hand-4 ofplayer1">...</g>
However, now I'm trying to animate them to a specific position (same for all cards) using this:
function animateTo(object, x, y, scaleX, scaleY, time) {
var matrix = object.transform().localMatrix;
var added = new Snap.Matrix();
added.translate(x, y);
added.scale(scaleX, scaleY);
added.add(matrix);
object.animate({transform: added}, time);
}
or something like this:
function animateTo(object, x, y, scaleX, scaleY, time) {
object.animate({transform: "t100,100"}, time);//this one I tried to use to understand how snap animations works
}
And here is my problem - when it animates, it allways first deletes the animation matrix of object and start animate from it's original location with blank matrix (where the object would be without transform attribute).
For example, when I tried:
var matrix = object.transform().localMatrix;
object.animate({transform: matrix}, time);
I expected it will do nothing, but my object blinks to the top left corner (blank matrix) and animates to position where it should stay.
What am I doing wrong? I need to animate that object from some matrix state to another (ideally the same one for every object). Is it somehow possible? Like I can specify start transform attribute somehow?
Thanks.
According to Ian's advice, I've used toTransformString:
object.animate({transform: matrix.toTransformString()}, time);
but of course, I had to use it in previous transformations too using
object.attr({transform: added.toTransformString()});//this
//object.transform(added);//instead of this
However, getting local matrix still works as expected. Animation now works and I can use matrix.translate() - to relative move the object or object.animate({transform: "t100,100"}, time).
I also can modify a,b,c,d,e,f attributes of the matrix directly. (or use transform: "T100,100")
It works!
Thanks!

pygame rotation around center point

I've read several other posts about rotating an image around the center point, but I've yet to figure it out. I even copy pasted one of the solutions posted on another SO question and it didn't work.
This is my code
def rotate(self, angle):
self.old_center = self.surface.get_rect().center
self.surface = pygame.transform.rotate(self.surface, angle)
self.surface.get_rect(center = self.old_center)
it's inside a class which contains the surface.
When I call this method the image rotates but also translates, and gets distorted.
You are not assigning the new rect, you should do something like:
self.rect = self.surface.get_rect(center = self.old_center)
And you should always keep the original surface and rotate from the original, that way distortion doesn't accumulate when rotating multiple times.
Update
If you don't want to keep track of the rect object, you can do it every time you blit. If you keep the center coordinates.
Example:
rect = object.surface.get_rect()
rect.center = object.center
display.blit(object.surface, rect.topleft)

XNA game studio simple sprite animation

Ok so im writing 2d game like bubble-bobble i have sprites i have done the physics (moving,jumping,falling so far :D) i have done some kind of shooting but now i want to make my player sprite(rectangle image) animated,but i dont want some kind of very complicated animation. I want something like this : i have 2 player images Right faced creature and Left faced creature the rectangle size of the images is exactly the same ,i also have :
Vector2 playerPosition declared
player = Content.Load<Texture2D>("PlayerRight"); in my LoadContent
spriteBatch.Draw(player, playerPosition, Color.White); in my draw function
all i want is when i press Right/Left - arrow buttons my player image switch between
Right/Left faced images
i dont know what kind of code and where i have to write for that task.
if you need more information about the task please let me know
this is not best solution, but it could give you hint.
playerRight = Content.Load<Texture2D>("PlayerRight");
playerLeft = Content.Load<Texture2D>("PlayerLeft");
playerCameraFacing = Content.Load<Texture2D>("playerCameraFacing");
declare PlayerSprite as Texture2D in player class
update funciton pseudo:
PlayerSprite = playerCameraFacing ;
if key.left then PlayerSprite = playerLeft;
if key.right then PlayerSprite = playerRight;
draw function pseudo:
draw(PlayerSprite, Position, Color)
i put all my animation frames into single image, and then show them using "sourceRectangle".
what is source rectangle in spritebatch.draw in xna
http://msdn.microsoft.com/en-us/library/ff433988.aspx

Resources