How to know if a component is selected in the editor?

I am working on a new component. I need to know if it is selected in the editor. Now I do this:

........
TMyComponent= class (TCastleScene)
.........
procedure TMyComponent.Update(const SecondsPassed: Single; var RemoveMe: TRemoveType);
var i,tb:integer;
begin
 inherited;
 tb:=0;
 for i:=0 to Self.Count-1 do begin
  if Self.Items[i].ClassName= 'TDebugTransformBox.TInternalScene' then tb:=tb+1;
 end;
 if tb>=1 then begin
  // Component selected
 end;
end;

Maybe there is a more correct way?

There is no “official” way to check this for now.

We (me and Andrzej Kilijański) have been playing with a mechanism for this in physics_j branch (which should hopefully be merged very soon). We got it working, although right now it is commented on this branch (as we didn’t need it in the end – we experimented with adding some tools for selected components, but in the end it was better UX to make these tools shown/hidden by explicit menu items by user). But I could uncomment it :slight_smile:

The way it works is that

  • all TCastleBehavior(s) attached to a TCastleTransform receive a call to (virtual) TCastleBehavior.DesigningBegin (when the transformation starts being selected) or TCastleBehavior.DesigningEnd (when it stops).
  • It would be ~zero effort to also have virtual TCastleTransform.DesigningBegin and TCastleTransform.DesigningEnd.

Question: Can you describe your use-case for this?

I’m open to uncommenting this mechanism, and then the answer for you would be "please wait for physics_j branch merge (should be this week) and then use virtual DesigningBegin / DesigningEnd " :slight_smile:

Yes, I think this is what I need.

And can you describe your use-case? Why do you need this?

To be clear, I’m happy to make TCastleTransform.DesigningBegin and TCastleTransform.DesigningEnd available, and I can imagine some potential use-cases. But still it would be good to know exactly why and how you in particular want to use it because then I am safe with knowledge that it’s exactly a solution you need.

I want to create an editable polyline.

This is very unfinished code.
CastleLines.zip (186.8 KB)
To edit switch to “Select component (Alt+2)”

I tested it – very cool, thank you for sharing this! This is honestly one of the reasons why I incredibly enjoy creating a game engine. It’s great to see how developers take it as a tool to build new things upon. Editing polygons straight in CGE editor opens a lot of possibilities.

Thank you!

Notes:

  1. As for UX, I think you are in a similar position that me + Andrzej were when making physics joint editing tools: we started thinking “let’s make the tool available automatically upon selection” but in the end this was too confusing for user. It wasn’t obvious when the editing started (and how should it be ended). In the end, we opted instead for explicitly allowing user to start/stop the editing.

    I would advise to test and consider to add method to start/stop polygon editing using “verbs” (menu options available when you right-click the component in the hierarchy tree), instead of relying on when the selection starts/stops. See Custom Components | Manual | Castle Game Engine . CGE has example showing it, in examples/advanced_editor/custom_component/code/gamecontrols.pas .

    Let me know what you think. I am still open to expose methods to allow components to “know” when they are selected (using DesigningBegin/End) methods. But maybe exposing menu items to explicitly “Start Polygon Editing”, “Stop Polygon Editing” would be easier for user in this case. E.g. right now it’s not obvious how to stop editing the polygon for user.

  2. Unrelated notes:

  • I see you use TVector2DinamicArray = array of TVector2. While it’s perfectly OK, you may consider using instead our list TVector2List. It’s an object (so you need to create/destroy it) but in exchange it has a lot of ready methods to operate/iterate on items.

  • I only briefly looked at CastleLineMath, possibly you can use some helpers from CastleVectors to make them easier.

    • Lines2DIntersection combined with Line2DFrom2Points could maybe be used to implement your CrossingSegments.

    • AngleRadPointToPoint looks similar to your AngleOX.

  1. I was thinking about “verb”, maybe this should be done.

2 .Thanks. It looks like I didn’t learn the API well.

Don’t worry, the CastleVectors API is huge and full of various utilities, nobody ever has learned it :slight_smile: