PDA

View Full Version : Unlimited loops



M109uk
26-06-2003, 07:00 PM
Hi all,

im not sure how i can explain this but i will try..

i have a structure which basicly looks like this:

TStructureList = class;

TStructure = class
StructureType {either stGroup, stPolyhedron, stFace or stEntity}
children: TStructureList;
.. Other properties ..
end;

Now in my editor, i have a Treeview, which i want to display all these structure objects
eg.
http://www.biocoders.org/image1.gif

Groups can contain: Groups, Polyhedrons, and Entities
Polyhedrons can only contain faces
Faces and Entites do not contain anything.

The only way i can think of doing this is with lots of If and For Loops?

Please can anyone give their ideas of how i can do this??

Thanks for any help,
M109uk

WILL
26-06-2003, 10:15 PM
I'm a bit rusty on TreeView objects, but as far as I know and recall; You would need one main for loop and a set of if statements each with a secondary. It would be best focus on keeping track of the object's parent rather than it's child since naturally all objects can have only one parent, by many children.

Something like:
for i := lower(List.Items) to upper(List.Items) do
begin
if (List.Items[i].StructureType = stGroup) then
begin
for j := lower(List.Items) to upper(List.Items) do
{find parent in List, if any, and stick it inside that one}
end;
if (List.Items[i].StructureType = stPolyhedron) then
begin
for j := lower(List.Items) to upper(List.Items) do
{find parent in List and stick it inside that one}
end;
if (List.Items[i].StructureType = stFace) then
begin
for j := lower(List.Items) to upper(List.Items) do
{find parent in List and stick it inside that one}
end;
{etc...}
end;

I believe you get the point right? This is the most straitforward way to do it, I think. There may be a better way to do it, but if someone can correct me, I'd be most interested. This is the way I've always done this type of structure listing.

M109uk
26-06-2003, 10:26 PM
Hi, Thanks for your reply..

in the end i tried something very similar, and i got curious to see if something would work so i tried the following code::


procedure Update_Structure;
var
i: Integer;

procedure AddFace(aParent: TElTreeItem; Face: TcxWorldData);
begin
With StructureET.Items.AddItem(aParent) Do
Begin
Text := Face.Text;
ImageIndex := iiFace;
StateImageIndex := siFace;
Data := Face;
End;
end;

procedure AddPolyhedron(aParent: TElTreeItem; Polyhedron: TcxWorldData);
var
anItem: TElTreeItem;
i: Integer;
begin
anItem := StructureET.Items.AddItem(aParent);
With anItem Do
Begin
Text := Polyhedron.Text;
ImageIndex := iiPolyhedron;
StateImageIndex := siPolyhedron;
Data := Polyhedron;
End;
For i := 0 To Polyhedron.Children.Count-1 Do
AddFace(anItem, Polyhedron.Children[i]);
end;

procedure AddGroup(aParent: TElTreeItem; Group: TcxWorldData);
var
anItem: TElTreeItem;
i: Integer;
begin
anItem := StructureET.Items.AddItem(aParent);
With anItem Do
Begin
Text := Group.Text;
ImageIndex := iiGroup;
StateImageIndex := siGroup;
Data := Group;
End;

For i := 0 To Group.Children.Count-1 Do
Case Group.Children[i].DataType Of
dtGroup: AddGroup(anItem,Group.Children[i]);
dtPolyhedron: AddPolyhedron(anItem,Group.Children[i]);
dtFace: AddFace(anItem,Group.Children[i]);
dtEntity: ;//AddEntity(Nil, World.Data[i]);
End;
end;

begin
StructureET.Items.Clear;

With StructureET.Items.AddItem(Nil) Do
Begin
Text := 'worldspawn';
ImageIndex := iiWorldspawn;
StateImageIndex := siWorldspawn;
End;

With StructureET.Items.AddItem(Nil) Do
Begin
Text := 'environment';
ImageIndex := iiEnvironment;
StateImageIndex := siEnvironment;
End;

ShowMessage('World Data Count :: '+InttoStr(World.Data.Count));
For i := 0 To World.Data.Count-1 Do
Begin
Case World.Data[i].DataType Of
dtGroup: AddGroup(Nil, World.Data[i]);
dtPolyhedron: AddPolyhedron(Nil, World.Data[i]);
dtEntity: ;//AddEntity(Nil, World.Data[i]);
End;
End;


I have tested, and so far it seems to be working great, but the only problem i have with doing it like this is where i clear the list out so when ever i want to add a new group or something the whole list is recreated and it just looks crap :( do you have any ideas around this? i tried to do the obvious just adding it to the list when they add the new object, but it causes problems when using a selected object :s

also would you know a function that can format a float to a float, im using FormatFloat and it works great but when i convert it back to a float using StrtoFloat i get something like 0.50000003456 but all i want is something like 0.5!

M109uk
26-06-2003, 11:29 PM
ah no worries, i think i found away around it, just a few dozen extra lines of code and it works great :lol:

But im still having problems with the FloatFormat :x

WILL
27-06-2003, 12:46 AM
Are you saying that you want to round off the number of digits to the right of the decimal point? If so then try this:
Val := 10.009;
roundedValue := StrToFloat(FormatFloat('#.##', Val));
roundedValue will return 10.01. Just use the proper number of #s you want after the decimal point. There is faster ways of rounding off floating point values though. I wouldn't reccomend using this in a game where code speed is a priority.

M109uk
27-06-2003, 02:28 AM
at the moment i have:


Str1,Str2: String;
V2F: Array[0..1] of Single;

Str1 := FormatFloat('0.00', cxwV2FEditorF.JvSpinEdit1.Value);
Str2 := FormatFloat('0.00', cxwV2FEditorF.JvSpinEdit2.Value);

V2F[0] := StrtoFloat(Str1);
V2F[1] := StrtoFloat(Str2);


but when i check the values you a treeview item i get:
-0.2000000002980232 instead of -0.20

if tried using FormatFloat('#.##', ....); but thats the same but it also causes a problem when the value is 0.00.

at the moment it doesnt really mater for my editor, but im concerned that it could cause problems when im rendering it in my engine??

WILL
27-06-2003, 04:25 AM
Ok well in that case, you can easily check to see if FormatFloat('#.##', Value) returns a string length of 0.

Str1 := FormatFloat('#.##', cxwV2FEditorF.JvSpinEdit1.Value);
if (Length(Str1) = 0) then
V2F[0] := 0
else
V2F[0] := StrtoFloat(Str1);

That should work flawlessly.

WILL
27-06-2003, 05:19 AM
Ok try using '0.##' instead. Then you'll at least get a 0 returned. With only #s it doesn't bother to hold the value and you end up with a 0 length string.

You know what the difference between using the 0 and # characters are right? # doesn't care if there are values that remain there. 0 makes sure that a a value of 0 is there in the string.

So...
StrToFloat(FormatFloat('0.##', 0.00));
will return 0. No errors, one line.