Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help on creating visual component 1

Status
Not open for further replies.

Griffyn

Programmer
Jul 11, 2002
1,077
AU
Hi all,

I'm after some advice on how to do some things.
I've made plenty of classes in my time, but never a visual one. I'm making an RPG type game, and I want to make a visual object that contains many other visual components, sorta like a a TPanel - this will be for a Character Display for stats, items, etc. When I say Character, I'm referring to a game Hero, if you like, rather than 1 byte of a string.

In addition to this, the visual component will be linked to the non-visual object that actually contains all of the fields, records and lists for that Character. I thought this was best rather than storing everything in the visual object, because then I can have 6 or 10 characters but only one or two displayed on the screen, and changing which character is displayed is as easy as changing the reference to the non-visual object in the visual object. Make sense?

The problem I've got with this approach is synchronising the visual Character object with it's non-visual object. I'm thinking of keeping an Integer field in each object, and I'll increment it in the non-visual object when anything get's changed, and the visual object will see this and update everything.

Can anyone offer me some advice or thoughts on how they would go about doing something like this?
 
I don't understand why you need to have a non-visual object as well as the visual component. It seems to involve extra work for no benefit.

Each visual component should have a Visible property which would have the same meaning as the Visible property of a VCL component.

The fields, records and lists for the Character should be private fields of the Character and be exposed by properties. When a property of a Character is changed it should check if the Character is visible and if it is then it should invalidate the Character so that it gets painted again.





Andrew
Hampshire, UK
 
Thanks Andrew,

Haven't used Invalidate before - still not quite sure how it will work though. Let's say I have a TEdit that shows a character's Strength. It's displayed value will be in the TEdit.Text property. How does the private field FSTR: Integer get updated via Invalidate into the TEdit control?

I was thinking I'd have to write a new method that goes through all the controls, and updates the the TEdit.Text properties for all the displayed stats from the private fields. And once that was done, it seemed to be no extra work to have all the data in a non-visual object.

The benefit I saw with the non-visual object attached was that I could have a form with a TTabControl, one TVisualCharacter control, and then update it's attached non-visual object when the user clicked through the available character tabs. And then I could easily spawn new forms with a single TVisualCharacter control if they wanted to see more than one character on the screen at once.

With your approach of having all the data in with the one visual control, I would instead have to make a character Visible, and the other's not Visible. If I wanted to break out of the TTabControl, I don't know how I could do it.
 
A TEdit control might be just one way that you would display the character strength. Another way might be to use something like a progress bar. So it is helpful to isolate the character attributes like strength from the way they are displayed. I suggest you descend your character from TGraphicControl.
Code:
[COLOR=blue]
TVisualCharacter = class(TGraphicControl)
private
  fStrenth: integer;
  ...
protected
  procedure Paint; override;
  ...
public
  procedure SetStrength ( value: integer );
  ...
published
  property Strength: integer read fStrength write SetStrength;
  ...
end;
[/color]
Then your SetStrength procedure would be coded something like:
Code:
[COLOR=blue]
procedure TVisualCharacter.SetStrength(value: integer);
begin
  if value <> fStrength then 
  begin
    fStrength := value;
    Invalidate;
  end;
end;
[/color]
The Invalidate procedure will call your Paint procedure to redraw the component. There may be typos in the above but it should give you an idea.

You could have a pointer in your visual component to a non-visual object as you suggest. I suppose it depends on the requirements of your program. It might be analogous to having something like a TDBEdit linked to a TTable (or TQuery). You might need something similar to a TDatasource as well. It will give you more flexibility but I think it will be more complex to implement.

Andrew
Hampshire, UK
 
Thanks again Andrew.

I'd missed that step in my head - the rest of my program will update the property and not the field, thus automatically repainting the control as necessary.

I've used a TGraphicControl before to make my Map control to draw the 2 dimensional map with rooms and doors. That works quite well.

Something else that I would like advice on though is with the complexity of the TVisualCharacter component. I definitely want to have a TPageControl control on it to separate details like the Spell list, Inventory, Skill list, etc. Can I add a TPageControl and possibly other components onto my TGraphicControl and have all the resizing, moving and so forth happen automatically? Much like when you add controls to a TPanel?
 
The more I begin to understand your TVisualCharacter the more I think you are right in having a separate (non Visual) class for the actual Character.

I don't know if you can add a TPageControl to your TGraphicControl component as TGraphicControl does not have a Windows handle. I haven't tried it.

I'm going to be away from work for a few days but I haven't lost interest in this thread. However, it might be a case of the blind leading the blind. Anyone else got experience in this area?

Andrew
Hampshire, UK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top