Is it safe to call TWinControl.Update from TGraphicControl.Paint? - delphi

I have a TGraphicControl descendant which paints rather slowly. There's also a TWinControl descendant, this one paint fast. Both controls are on the same TPanel.
While scrolling, the TGraphicControl is updated more often than the TWinControl, so they look out of sync. This updating is done by Windows and/or the VCL, not by my code.
I can solve this by calling TWinControl.Update from the TGraphicControl.Paint method, but I'm a bit worried this might cause problems one way or another.
Is it safe to do this?

As long as there isn't a call back to your TWinControl that should be fine. You might get the TWinControl updating more often than strictly necessary but that's all.

It's safe, as long as they're unrelated. But it doesn't look like a very efficient approach.
The TGraphic depends on Painting by the parent Control, is that also the Parent of the TWinControl?
You might experiment a little with:
a) change the TGraphic based control to be a TWinControl (TGraphic isn't that useful or efficient anyway). That is if you own the source.
b) Embed the Graphic Control in a special (borderless etc) TPanel

Related

DirectX10/ShapDX Custom control

I am trying to create custom control with directx10/direct2D output (panel, not a form). I do all rendering in the overriding OnPaint method, however I have read somewhere that it is wrong and RenderLoop should be used instead. But where should I insert RenderLoop.Run if I can write code only inside of the control? Thank you.
You would create a thread, and have RenderLoop.Run inside the thread. When you do this you have to make sure that events sent back and forth between the components are invoked in a safe manner.

how to detect interface changes on iOS application

Is there any possible way to detect every change on User Interface during runtime??
I'm trying to find all objects in the current app interface.
I'm trying to to get all nodes inspecting recursively the main Window, but, for example, how to know if the top viewcontroller changes or if it's added a uiview dynamically, or is presented a modalview??
The main objective is to have a library to do this..
Any idea, help?
Thanks!
You could write your own library based on this, using advanced Objective-C techniques. I do not recommend you to do this, since it mostly breaks MVC patterns on iOS. Depends on what do you want to use it for, maybe analytics?
So these are the options I believe, if you want to actively inspect UIView hierarchy. All options are pretty complicated though.
Swizzle methods such as addSubview and removeFromSuperview of UIView, so you could know when changes like that happens. Including the getters of frame and bounds, if you wish to know the position.
You could use KVO to watch properties such as: subviews, frame, bounds, superview to notice any changes. But at one point you would have to add the same object as the observer (could be singleton).
Decide for an interval that is fired by a NSTimer and go through the hierarchy recursively beginning at keyWindow on UIApplication. This would have a big performance impact though.
There may be other options, but these are the ones I believe to be the best choices.

Singleton-Like UIView Access?

I have a UIView I need to access the properties of from all around my app. I know you can't create a Singleton around a UIView object, so what might be a good way of doing similar?
E.g. The view has a label. From any view controller in my app I want to be able to change the text of this view (sitting in a parent view controller).
Thanks.
EDIT:
Success! Using KVO to track changes in my Singleton object worked a charm, and a very simple solution.
I think what you’re trying to do violates the separation of concerns of the MVC pattern: The only thing that should interact with a view is its controller. In your case, you should probably be creating a model that is watched by your view controller (maybe through key–value observing), and then the controller can propagate the necessary changes to your view.
If you know (read: you really know for now and forever!) that there will be at most one instance of that view alive at one point in time, you can just use a global variable to store that. Or use a class property on that view - which is as close as being a singleton as possible.
Or you might just fix your design, which has proven to be the better choice in every case I can remember. :) Just add some forward and backward references in your classes (and stick to MVC principle). It takes much less time to implement that worrying about those awkward workaround, and it will pay of rather sooner than later.

Custom TControl doesn't paint at design time until cut and pasted

I have an odd issue with TChromeTabs. I know it must be something simple, but I can't figure out what needs to be done to fix it.
When I initially drop the TChromeTabs control on a form it is completely transparent. If I cut the control then paste it back to the form the contents are displayed correctly. The contents also appear if I close, then re-open the form.
As I have no idea why this is happening I can't really give you any code samples. However, you can download the source code here: http://code.google.com/p/delphi-chrome-tabs/downloads/list.
Your control doesn't paint itself because you disable painting. You call BeginUpdate in the constructor, and you don't call EndUpdate until the Loaded method is called. But Loaded is only called when loading a control from a persisted state. Usually, we think of that as being when the control is loaded from a DFM file, but the IDE uses the same technique to allow putting controls on the clipboard.
You haven't noticed this before because, apparently, you only test your control by opening a pre-made demo project. The demo project has a control in its DFM file, so the only code path you exercise is the DFM case. You don't exercise the path where the constructor is called directly — when the control is first dropped on a form, or when the control is created "dynamically" in code.
To fix this, begin by getting rid of the BeginUpdate call in your constructor. Instead, to check whether your control is still in the process of being constructed, check csCreating in ControlState.
You can also get rid of your stsLoading state. Delphi already gives you that with the csLoading bit of ComponentState. Besides, your use of stsLoading is wrong since you set it in the constructor, just like you do with BeginUpdate.
Instead of relying on Loaded being called, you might wish to move certain code into the AfterConstruction method. Put code there that needs to run after your component is created but that has nothing to do with loading properties from a DFM (or other persistence source). I'm not sure I see anything in your Loaded method that really belongs there. Nearly all of it should be able to occur in the constructor.
You should also be aware of the CreateWnd method. It's called when your control's window handle gets allocated. That's where you should start allowing paint operations to occur. When you don't have a window handle, you have nothing to paint to.

Dealing with hierarchy-breaking effects in iOS games and apps

I started working as a iOS developer about a year and a half ago, and I'm having some trouble with software architecture and organization. I use Apple's recommended Model-View-Controller paradigm, and my code is generally very hierarchical: if a screen has (for example) a HUD, a control panel, and a display area, I have a main controller for the screen and sub-controllers for the HUD, control panel, and display area. The sub-controllers generally have no knowledge of their neighboring controllers and use methods in the main controller to interact with them.
However, especially in games, I often run into hierarchy-breaking problems that just can't be elegantly solved with this model. For instance, let's say I have a coin in the control panel area that I want to animate flying to the HUD. I can either animate the original coin to the new position, which would require a method like animateCoinToPosition: in the control panel sub-controller and a method like getPositionForFinalCoinPositionInHUD in the main controller; or, I can hide the original coin and create a duplicate coin either in the main controller or the HUD controller, which would require a delegate method like animateCoinToHUDFromStartingPosition:. I don't like having such oddly-specific methods in my controllers, since they only really exist to solve one problem, and additionally expose the hierarchy. My ideal solution would be to have a single method called animateCoinToHUD, but this would require flattening the entire hierarchy and merging the three controllers into one, which is obviously not worth it. (Or giving the sub-controllers access to their siblings — but that would essentially have the same effect. The sub-controllers would then have dependencies with each other, creating a single messy spiderweb controller instead of a main controller and three mostly independent sub-controllers.)
And it often gets worse. What if I want to display a full-screen animation or particle effect when moving the coin? What if my coin is a lot more complicated than a simple sprite, with many subviews and details, to the point where creating a duplicate coin using animateCoinToHUDFromStartingPosition: is inefficient? What if the coin flies to the HUD but then returns to the control panel? Do I "lend" the coin view to the main controller and then take it back when the animation completes, preserving the original position/z-order/etc. in temporary variables so that they can be restored? And another thing: logically, code that concerns multiple sub-controllers belongs in the main controller, but if these interactions are common, the main controller grows to be many thousands of lines long — which I've seen happen in many projects, not just my own.
Is there a consistent way to handle these hierarchy-breaking effects and actions that don't require duplicate code or assets, don't bloat my controllers, and elegantly allow me to share objects between sub-controllers? Or am I using the wrong approach entirely?
So, I think you may be thinking about the "never go up" the hierarchy a little too literally.
I think the idea is that you don't know specifically what the parent is, but you can define a protocol and know that whatever your parent object is it responds to said protocol. You ideally test in code to confirm that it responds to that protocol. Then use the protocol to send the message in a generic way wherein you pass the coin object to the parent object and let the parent object animate it off the screen and into the HUD.
The sub-controllers then have a id<parent_protocol> parent; instance variable and their initialization method takes one of those as a parameter. Given your description you already have something like this in place, or at least enough to implement "sub-controllers generally have no knowledge of their neighboring controllers and use methods in the main controller to interact with them" as you say.
So the idea, from a design perspective is that the coin pickup happens in the Display panel and all it knows is that it's parent object has a pickupCoin: method that will do whatever is appropriate with a picked up coin. The Display panel doesn't know it goes to the HUD, or whatever, just that picked up coins get handled by the parent controller's pickupCoin: method.
The OOP design philosophy here is that all knowledge of the parent is encapsulated in the protocol definition. This makes the child & parent more loosely coupled so that you could swap in any parent that implemented that protocol and the children would still work fine.
There are looser couplings you could use (globally posted notifications say), but in the cases you describe I think something like what I've outlined is probably more appropriate & likely more performant.
does that help?

Resources