[Solved] Animate move/repaint not working lazarus / 33-macOS - Mac OS X ---------------------------------------------------------------------- Trenatos · 2016-04-06 19:06 I'm trying to get some simple animation for an application, sliding panels in and out by following this tutorial: http://lazplanet.blogspot.com/2013/05/animate-move-of-object.html But all that's happening is that the button just appears at the target location with no animation. Slowing it down, I see that the button/panel/whatever simply doesn't seem to repaint on each iteration, only after the loop finishes, making it jump to the location. I'm using Lazarus 1.6 on OSX El Capitan, not sure if this is a bug, old non-working example, or if there's just a different way to do it on Mac. ---------------------------------------------------------------------- skalogryz · 2016-04-06 20:34 Omg! The example is the worst possible. 1) it's using SLEEP for animation! That's #1 not-to-do in the events driven apps 2) it's painting ad-hoc (outside of paint event). Infact the application should have shown glitches in Windows 8, because it will unlikely work in OSX. The only valid way to do that, is as shown with the TTimer. ---------------------------------------------------------------------- Josh · 2016-04-06 20:35 Hi Try placing application.processmessages; in the loop. Code: [Select]  while i <= 100 do begin ....... .......         application.processmessages;   end; ---------------------------------------------------------------------- skalogryz · 2016-04-06 20:39 Quote from: josh on April 06, 2016, 10:35:23 pm Try placing application.processmessages; in the loop. Please, have mercy on your project and DO NOT do that. Otherwise you might end up with pretty nice recursion of Application.ProcessMessages. ---------------------------------------------------------------------- howardpc · 2016-04-06 20:43 Yes, the example is pretty dreadful code, including casting a TPanel as a TButton! Try changing the signature of the private MoveAnimation method to the following: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   procedure TForm1.MoveAnimation(aPanel: TPanel; leftFrom, leftTo: Integer);  Then implement it using the much simpler code (hopefully simple enough to demonstrate the basic idea - not that this is how animations are best done at all): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.MoveAnimation(aPanel: TPanel; leftFrom, leftTo: Integer);var  i: Integer;  moveLeftRange: integer;begin  moveLeftRange:=leftTo - leftFrom;  for i:=1 to 1000 do begin    aPanel.Left:=leftFrom + trunc(moveLeftRange * i / 1000);    aPanel.Repaint; // this may not work on a Mac  end;end;  You'll then have to call the modified method like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);begin  MoveAnimation(Panel1, -Panel1.Width, 0);end; ---------------------------------------------------------------------- skalogryz · 2016-04-06 20:50 Quote from: howardpc on April 06, 2016, 10:43:01 pmYes, the example is pretty dreadful code, including casting a TPanel as a TButton! And they could easily avoid that by declaring TControl as the parameter type rather than TComponent. This article was not approved by Freepascal Foundation and should not be used as an example ... Seriously! ---------------------------------------------------------------------- Trenatos · 2016-04-07 12:49 I just tried howardpc's example and it acts the same way. Press the button, it stays depressed for the duration, and the panel moves instantly when the loop is over. ---------------------------------------------------------------------- skalogryz · 2016-04-07 13:46 try this sample instead. comments on the code: It should be understood that placing "animation code" into the form isn't good either. Instead animation handling code should be moved to a separate component. ---------------------------------------------------------------------- Trenatos · 2016-04-07 17:14 Thanks skalogryz, your example code works and animates. Is there any styleguide for Object Pascal/Lazarus I can look through, or a best practices tutorial? ---------------------------------------------------------------------- skalogryz · 2016-04-07 17:17 Quote from: Trenatos on April 07, 2016, 07:14:36 pmIs there any styleguide for Object Pascal/Lazarus I can look through, or a best practices tutorial? You might find some in the wiki ... i know what not to do, though: * sleeps with user interface. (Sleeps are ok, if used in threads and/or hardware communications. In all other cases are evil) * gotos (if the code is new, just rewrite the code) * Application.ProcessMessages (use threads!)