kemarofangs
Topic Author
Posts: 32
Joined: 13 Jun 2014, 21:30

Cast As Interface

29 Jul 2016, 08:55

What is the proper way to cast interfaces in the code behind?
Is there a simple trick to make this work?

// Attempt 1) uiElement is valid and implements I_Equipment
I_Equipment iEquipment = uiElement as I_Equipment;
// iEquipment is NULL

// Attempt 2) uiElement is valid and implements I_Equipment
I_Equipment iEquipment = uiElement.As<I_Equipment>();
// cannot compile because interface cannot implement Noesis.BaseComponent and remain an interface.
 
User avatar
sfernandez
Site Admin
Posts: 1915
Joined: 22 Dec 2011, 19:20

Re: Cast As Interface

29 Jul 2016, 10:03

Hi,

Which version of NoesisGUI are you using?

Since NoesisGUI 1.2 you can use the 'as' operator to cast to any implementation class or interface.
For example, if I have the following class definitions:
public interface IHello
{
    void SayHello();
}

public class HelloElement : UIElement, IHello
{
    void IHello.SayHello()
    {
        Debug.Log("Hello!");
    }
}
And the xaml below:
<Grid
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Button x:Name="btn" Width="200" Height="100">
        <HelloElement/>
    </Button>
</Grid>
I can write a Unity behavior that gets the interface and uses it:
public class CastBehavior : MonoBehaviour
{
    void Start()
    {
        var gui = GetComponent<NoesisGUIPanel>();
        var content = gui.GetContent();

        var btn = (Button)content.FindName("btn");
        var hello = btn.Content as IHello;
        if (hello != null)
        {
            hello.SayHello();
        }
        else
        {
            Debug.Log("Element does not implement IHello");
        }
    }
}
 
kemarofangs
Topic Author
Posts: 32
Joined: 13 Jun 2014, 21:30

Re: Cast As Interface

29 Jul 2016, 12:32

Hey, mine's version 1.1 from 2014. I'm downloading the new one since I'm registered now. thanks!
Just got to muck through importing the latest version without errors. Seems DependencyData.cs in Proxies is an empty file...
 
kemarofangs
Topic Author
Posts: 32
Joined: 13 Jun 2014, 21:30

Re: Cast As Interface

29 Jul 2016, 13:42

So, is there a proper way to overwrite / upgrade the noesis unity asset? I backed up before upgrading but have since only ran into problems. It seems that the DependencyData cs file is empty no matter what I do. It's preventing UIElementData from compiling. Even tried reimporting the old asset then the newest one.
 
User avatar
jsantos
Site Admin
Posts: 2905
Joined: 20 Jan 2012, 17:18
Contact:

Re: Cast As Interface

29 Jul 2016, 13:54

Although it should update properly I recommend manually cleaning the old version and fresh installing the new one. I will investigate the problems you are having to fix them because the 1.2 is going to be uploaded to the Asset Store soon or later.
 
kemarofangs
Topic Author
Posts: 32
Joined: 13 Jun 2014, 21:30

Re: Cast As Interface

29 Jul 2016, 23:48

Ok, I've almost got it working. Had to comment alot of the #ifndef / #endif sections since I'm using the latest full version of unity 4 and it doesn't understand the unity 5 references. Can you advise on how to get the root element of the noesis panel now?

NoesisGUIPanel noesisGUIPanel = GetComponent<NoesisGUIPanel>();
Noesis.FrameworkElement frameworkElement = noesisGUIPanel.GetContent (); // Used to be GetRoot();
// frameworkElement is NULL
// can't use frameworkElement.FindName("nameOfElement") now...
 
User avatar
jsantos
Site Admin
Posts: 2905
Joined: 20 Jan 2012, 17:18
Contact:

Re: Cast As Interface

30 Jul 2016, 15:05

Yes, your code seems to be right, you must use GetContent().
    /// <summary>
    /// Gets the root of the loaded Xaml.
    /// </summary>
    /// <returns>Root element.</returns>
    public FrameworkElement GetContent()
Make sure you call this function when the XAML is loaded in Play mode.
 
kemarofangs
Topic Author
Posts: 32
Joined: 13 Jun 2014, 21:30

Re: Cast As Interface

31 Jul 2016, 01:23

So I've got a fresh copy of Unity 5.4.0f3 installed with Noesis 1.2.6f5. Loading the sample lion project produces the following error... The file is there so I'm trying to figure out why the Noesis GUI panel can't locate it. Any ideas or should I just wait for the new unity store noesis package to come out?

NoesisException: Loading Assets/NoesisGUI/Samples/Lion/Lion.xaml
Resource Assets/NoesisGUI/Samples/Lion/Lion.xaml not found
Noesis.Error.Check () (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisError.cs:26)
NoesisGUISystem.Noesis_LoadXAML (System.String xamlFile) (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisGUISystem.cs:409)
NoesisGUISystem.Load (System.String xamlFile) (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisGUISystem.cs:282)
NoesisGUISystem.LoadXaml (System.String xamlFile) (at Assets/Plugins/NoesisGUI/Scripts/Core/NoesisGUISystem.cs:47)
NoesisGUIPanel.LoadXaml () (at Assets/Plugins/NoesisGUI/Scripts/NoesisGUIPanel.cs:513)
NoesisGUIPanel.OnEnable () (at Assets/Plugins/NoesisGUI/Scripts/NoesisGUIPanel.cs:400)
 
kemarofangs
Topic Author
Posts: 32
Joined: 13 Jun 2014, 21:30

Re: Cast As Interface

31 Jul 2016, 06:35

I seem to have issues trying to make unity realize that changes have been made. It's like the editor won't auto compile. Frustrating. I did windows update and rebooted then the Lion example project magically started working. Had to open my XAML file and change the background color on my personal project for it to not have the "I can't find the XAML" compiler error. I wish there there was a "force recompile all" button in unity.

Next problem is getting my custom font to work again. Any ideas? I'm trying not to be annoying and really appreciate all the help. I followed the example in the documentation to double check my work but the compiler is again complaining (http://www.noesisengine.com/docs/Gui.Co ... orial.html).

Error 1)
[dx11] Assets/Darren/Code/Title.xaml
Parsing TextBlock.FontFamily (@15,2).
FontFamily 'D:/Work/Source Code/Unity/Marauders/Assets/Darren/Font/KaushanScript-Regular' not found

UnityEngine.Debug:LogError(Object)
Noesis.BuildToolKernel:OnLog(Int32, String) (at Assets/Editor/NoesisGUI/NoesisBuildToolKernel.cs:267)
System.Object:wrapper_native_4238D6E0(String)
Noesis.BuildToolKernel:BuildIncremental() (at Assets/Editor/NoesisGUI/NoesisBuildToolKernel.cs:116)
NoesisPostProcessor:Build(String) (at Assets/Editor/NoesisGUI/NoesisPostProcessor.cs:251)

Warning 1)
Unable to open Assets/Darren/Font/KaushanScript-Regular.otf: Check external application preferences.

XAML) Assets\Darren\Code\Title.xaml
<Canvas
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  mc:Ignorable="d"
  xmlns:noesis="clr-namespace:NoesisGUIExtensions">
  <Canvas.Background>
    <ImageBrush ImageSource="../Graphics/Raster/Background/MistyMountain.jpg" />
  </Canvas.Background>
  <!-- FontFamily="../Font/#KaushanScript-Regular" -->
  
  <!-- Title -->
  <Rectangle Fill="Crimson" Canvas.Top="250" Canvas.Left="40" Width="700" Height="100" Opacity="0.5" />
  <TextBlock Canvas.Top="75" Canvas.Left="250" Text="Marauders"
    FontSize="300" FontFamily="../Font/#KaushanScript-Regular" noesis:Text.Stroke="Crimson" noesis:Text.StrokeThickness="10" />
</Canvas>
Here's the physical location.
Assets\Darren\Font\KaushanScript-Regular.otf
Font is installed on the OS. Download here https://www.fontsquirrel.com/fonts/kaushan-script.
 
User avatar
jsantos
Site Admin
Posts: 2905
Joined: 20 Jan 2012, 17:18
Contact:

Re: Cast As Interface

01 Aug 2016, 15:11

First, make sure you have selected the desire platform under Tools->NoesisGUI->Settings. This comes with sane default values, but maybe the upgrading process broke something. Also, each time you hit the Build button all the assets are preprocessed.

After having all the assets compiled, each time you touch a xaml, font or texture it must be automatically rebuilt. You can check this by inspecting under Assets\StreamingAssets\NoesisGUI the corresponding .nsb file.

Regarding the font, I am opening it with windows and it tells me that the font name is Kaushan Script. Changing the xaml to the following is working fine here:
<Canvas
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  mc:Ignorable="d"
  xmlns:noesis="clr-namespace:NoesisGUIExtensions">
 
  <Rectangle Fill="Crimson" Canvas.Top="250" Canvas.Left="40" Width="700" Height="100" Opacity="0.5" />
  <TextBlock Canvas.Top="75" Canvas.Left="250" Text="Marauders" FontSize="300" 
    FontFamily="../Font/#Kaushan Script" noesis:Text.Stroke="Crimson" noesis:Text.StrokeThickness="10" />
</Canvas>
The support of font attributes is quite limited in 1.2 (just bold and italic). This has been improved in 1.3 (yet not public) to support all weights, styles and stretches.

Please, let us know if you solve the preprocessing issues.

Who is online

Users browsing this forum: No registered users and 2 guests