nizesh
Topic Author
Posts: 10
Joined: 31 Jul 2019, 22:32

Support for Custom Rendering in Unity C#

13 Aug 2019, 17:43

I am in a need for a custom rendering to create something similar to this https://ibb.co/JqyvCsy. I'm writing the logic for plotting each ellipse, and lines, the distance between them and the positioning of the UIElements. I'm overriding the Canvas and that's where I'm plotting these UIElements. But I see that the custom rendering is not supported by default in Noesis GUI for Unity C# yet. WPF has a Dispatcher.Invoke method to push the UI rendering logic in the UI thread pool which is not supported at least in NoesisGUI for Unity. The closest one I can find is UpdateRender but I couldn't find enough documentation about how to use the UpdateRender method from https://www.noesisengine.com/docs/Gui.Core._Canvas.html either. Whenever I try to render the UI directly, it's giving me errors like these in red https://ibb.co/MnznCYN. With all these, I am in loss about how to get around this issue. How do you put the custom rendering into the UI render thread pool so that I won't get the issues like I'm getting right now?

Tags:
 
User avatar
jsantos
Site Admin
Posts: 2597
Joined: 20 Jan 2012, 17:18
Contact:

Re: Suuport for Custom Rendering in Unity C#

15 Aug 2019, 10:55

We have plans to expose UIElement.OnRender functionality to C#, would that be enough for you?

Why do you need Custom Rendering, can't you use a standard Visual Tree? Is it because you want to optimize memory usage?
 
nizesh
Topic Author
Posts: 10
Joined: 31 Jul 2019, 22:32

Re: Support for Custom Rendering in Unity C#

15 Aug 2019, 15:57

The UIElement.OnRender would definitely help for sure. The reason I'm needing a Custom Rendering is because the Visual Tree would be unique for each dataset that I'm using and the elements inside the VIsualTree need to be populated dynamically. Do you know by when the UIElement.OnRender functionality would be available for use?
 
User avatar
jsantos
Site Admin
Posts: 2597
Joined: 20 Jan 2012, 17:18
Contact:

Re: Support for Custom Rendering in Unity C#

17 Aug 2019, 00:29

Why can't you populate the VisualTree dynamically by code?
 
nizesh
Topic Author
Posts: 10
Joined: 31 Jul 2019, 22:32

Re: Support for Custom Rendering in Unity C#

19 Aug 2019, 20:51

After careful consideration of your suggestion and a thoughtful reprocessing of my problem, I've finally managed to solve this issue. The problem isn't with dynamically loading the TreeView but that the data I receive to populate the Canvas is in a different thread and I was trying to update the UI thread from a different thread. Since Noesis doesn't support Dispatcher.BeginInvoke() method from WPF, I was struggling with accessing the UI thread from a different thread. It's a replica of this issue viewtopic.php?t=1659. There seems to be a Unity Asset for dispatching to the UI thread from another thread at https://assetstore.unity.com/packages/t ... cher-41637. That literally resolved the whole issue for me. Thanks for the assistance!
 
User avatar
sfernandez
Site Admin
Posts: 1666
Joined: 22 Dec 2011, 19:20

Re: Support for Custom Rendering in Unity C#

19 Aug 2019, 21:02

From the reference image:
Image

I think you can use MVVM to automatically generate the visual tree as I mentioned in other thread: viewtopic.php?f=3&t=1769&p=10100#p10096

You will define an ObservableCollection that contains the information for all the connections (start position, end position, label...). This list will be bound to an ItemsControl with a Canvas as ItemsPanel, and each item will draw the corresponding arrow line and label (by using a DataTemplate for ItemsControl.ItemTemplate property).
<ItemsControl ItemsSource="{Binding Connections}">
  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
      <Canvas/>
    </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <Grid>
        <Line X1="{Binding Start.X}" Y1="{Binding Start.Y}" X2="{End.X}" Y2="{Binding End.Y}" Stroke="Gray"/>
        <TextBlock Text="{Binding Label}" HorizontalAlignment="Center" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5">
          <TextBlock.RenderTransform>
            <RotateTransform Angle="{Binding LabelRotation}"/>
          </TextBlock.RenderTransform>
        </TextBlock>
      </Grid>
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>
Another ObservableCollection will contain the information about each node (position, size, stroke and fill, ...). This list will also be bound to an ItemsControl with a Canvas as ItemsPanel, and each item will draw the corresponding node (by using another DataTemplate).
<ItemsControl ItemsSource="{Binding Nodes}">
  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
      <Canvas/>
    </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <Ellipse Canvas.Left="{Binding Position.X}" Canvas.Top="{Binding Position.Y}" Fill="{Binding Fill}" Stroke="{Binding Stroke}" StrokeThickness="2"/>
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>
 
nizesh
Topic Author
Posts: 10
Joined: 31 Jul 2019, 22:32

Re: Support for Custom Rendering in Unity C#

20 Aug 2019, 22:00

Thanks for the help. I really appreciate it. I have already managed to get the graphs rendered as seen in the image https://ibb.co/YRtyx2G. However, I didn't do it through the MVVM pattern as I am fairly new to this pattern and I'm not so used to thinking that way (This is the first C# project I'm working on). I just created the UIElements programmatically. I will try to implement your way as a means of learning though. One hurdle that I'm trying to overcome is to make each node click and draggable with the mouse and with the drag movement of the mouse, the connected lines should also automatically update itself according to the new position of the node.

Who is online

Users browsing this forum: No registered users and 1 guest