# How to in-code copying design of UI element?

**URL:** <https://forum.castle-engine.io/t/how-to-in-code-copying-design-of-ui-element/1425>\
**Category:** 3D\
**Created:** [November 12, 2024, 2:43pm UTC](https://forum.castle-engine.io/t/how-to-in-code-copying-design-of-ui-element/1425 "2024-11-12T14:43:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Recongalina](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/recongalina/32/1530_2.png) [@Recongalina](https://forum.castle-engine.io/u/Recongalina)\
**Post date:** [November 12, 2024, 2:43pm UTC](https://forum.castle-engine.io/t/how-to-in-code-copying-design-of-ui-element/1425/1 "2024-11-12T14:43:13Z")

</div>

Hello coders!  
I made beautiful button in Castle Editor:  
 ![image](https://forum.castle-engine.io/uploads/default/original/2X/c/c901038f6da375d94c450fca0fc80ddb9aff29d6.png)  
Is there an easy way to copy parameters for a newly created in code button from an existing one?  
Or I need to make a special function for coping needed for me parameters?

---

<div class="post-metadata">

**Author:** ![michalis](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/michalis/32/3_2.png) [@michalis](https://forum.castle-engine.io/u/michalis)\
**Post date:** [November 12, 2024, 4:11pm UTC](https://forum.castle-engine.io/t/how-to-in-code-copying-design-of-ui-element/1425/2 "2024-11-12T16:11:42Z")

</div>

If you have an existing button, and want to copy it, you can create copies of it in code. You can use `ComponentClone` method from `CastleComponentSerialize`. Or, if you want to spawn a lot of such buttons, you can use `TCastleComponentFactory`:

In code:

```auto
var
  MyButtonFactory: TCastleComponentFactory;
begin
  MyButtonFactory := TCastleComponentFactory.Create(SomeOwner);
  MyButtonFactory.LoadFromComponent(MyButton);

  MyButton1 := MyButtonFactory.ComponentLoad(SomeOwner) as TCastleButton;
  MyButton1.Caption := 'Button 1 caption customized';
  SomeParent.InsertFront(MyButton1);

  MyButton2 := MyButtonFactory.ComponentLoad(SomeOwner) as TCastleButton;
  MyButton2.Caption := 'Button 2 caption customized';
  SomeParent.InsertFront(MyButton2);

```

You could also use RTTI (enumerate source properties, copy them using utilities from CastleInternalRttiUtils).

---

<div class="post-metadata">

**Author:** ![Recongalina](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/recongalina/32/1530_2.png) [@Recongalina](https://forum.castle-engine.io/u/Recongalina)\
**Post date:** [November 13, 2024, 2:34pm UTC](https://forum.castle-engine.io/t/how-to-in-code-copying-design-of-ui-element/1425/3 "2024-11-13T14:34:07Z")

</div>

Thanks! It Working 🙂  
 ![image](https://forum.castle-engine.io/uploads/default/original/2X/0/095d672ca6363cc9e1983f0d248dff03943c47bf.png)  
The only thing is that the TCastleButton.CustomFont parameter is not copied automated, I have to do it manually in code.

---

<div class="post-metadata">

**Author:** ![michalis](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/michalis/32/3_2.png) [@michalis](https://forum.castle-engine.io/u/michalis)\
**Post date:** [November 13, 2024, 5:49pm UTC](https://forum.castle-engine.io/t/how-to-in-code-copying-design-of-ui-element/1425/4 "2024-11-13T17:49:07Z")

</div>

> [@Recongalina](#):
>
> The only thing is that the TCastleButton.CustomFont parameter is not copied automated, I have to do it manually in code.

We do try to copy references to other components, like `TCastleButton.CustomFont`. But for this, at the moment of instantiation, (`MyButtonFactory.ComponentLoad(SomeOwner)` in example above) we must have the owner that knows how to resolve it. I.e. the `SomeOwner` must be also the owner of the font instance referred to by the `TCastleButton.CustomFont`.

Which indeed is not possible if you have loaded the design in TCastleView, using `TCastleView.Url`. In this case we hide the owner, deliberately, to have more reliable API (the `FDesignLoadedOwner` is private in TCastleView, and this means that published fields of view are reliably updated) and simpler implementation (`TCastleView.TDesignOwner...` make some assumptions and updates published fields of the view always). So we don’t give you possibility to pass proper `SomeOwner`.

**Long story short:** yes, this is a limitation of this approach for now. Sorry for long and useless explanation above 🙂 You cannot escape it, and workarounding it by manually assigning `MyNewButton.CustomFont` after clone is what you should do, for now. I’ll sleep on this and think can we offer something better, without breaking anything else.
