<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- .bmp and .png files in assets\images -->
  <ItemGroup>
    <ImageAssets Include="$(ApplicationRoot)\assets\images\**\*.bmp"/>
    <ImageAssets Include="$(ApplicationRoot)\assets\images\**\*.png"/>
  </ItemGroup>
  <!-- .ttf/.otf/.bdf files in assets\fonts -->
  <ItemGroup>
    <FontsAssets Include="$(ApplicationRoot)\assets\fonts\*.ttf"/>
    <FontsAssets Include="$(ApplicationRoot)\assets\fonts\*.otf"/>
    <FontsAssets Include="$(ApplicationRoot)\assets\fonts\*.bdf"/>
  </ItemGroup>
  <!-- Texts files in assets\texts -->
  <ItemGroup>
    <TextsAssets Include="$(ApplicationRoot)\assets\texts\texts.xlsx"/>
  </ItemGroup>
  <!-- Asset files in assets\fonts and assets\texts -->
  <ItemGroup>
    <FontsAndTextsAssets Include="@(FontsAssets);@(TextsAssets)"/>
  </ItemGroup>
  <!-- .cpp files in generated\fonts and generated\texts -->
  <ItemGroup>
    <AllGeneratedFontsAndTextsFiles Include="$(ApplicationRoot)\generated\fonts\**\*.*;$(ApplicationRoot)\generated\texts\**\*.*"/>
  </ItemGroup>
  <!-- .All generated files in generated\-->
  <ItemGroup>
    <AllGeneratedFiles Include="$(ApplicationRoot)\generated\**\*.*"
		       Exclude="$(ApplicationRoot)\generated\gui_generated\**\*.*"/>
  </ItemGroup>

  <PropertyGroup>
    <UseBPPOption>/DUSE_BPP=$(UseBPP)</UseBPPOption>
  </PropertyGroup>
  
  <!-- Rotate90 option -->
  <PropertyGroup Condition="'$(RotateImage90)'=='true'">
    <RotateOption>-rotate90</RotateOption>
  </PropertyGroup>
  <PropertyGroup Condition="'$(RotateImage90)'!='true'">
    <RotateOption></RotateOption>
  </PropertyGroup>

  <!-- DitherAlgorithm, default is "2" -->
  <PropertyGroup Condition="'$(DitherAlgorithm)'!=''">
    <DitherAlgorithmOption>$(DitherAlgorithm)</DitherAlgorithmOption>
  </PropertyGroup>
  <PropertyGroup Condition="'$(DitherAlgorithm)'==''">
    <DitherAlgorithmOption>2</DitherAlgorithmOption>
  </PropertyGroup>

  <!-- Should alpha channel be dithered? -->
  <PropertyGroup Condition="'$(AlphaDitherImage)'=='yes'">
    <AlphaDitherOption>yes</AlphaDitherOption>
  </PropertyGroup>
  <PropertyGroup Condition="'$(AlphaDitherImage)'!='yes'">
    <AlphaDitherOption>no</AlphaDitherOption>
  </PropertyGroup>

  <!-- opaque image output format -->
  <PropertyGroup>
    <OpaqueImageOutputOption>$(OpaqueImageOutputFormat)</OpaqueImageOutputOption>
  </PropertyGroup>

  <!-- non opaque image output format -->
  <PropertyGroup>
    <NonOpaqueImageOutputOption>$(NonOpaqueImageOutputFormat)</NonOpaqueImageOutputOption>
  </PropertyGroup>
  
  <!-- Remap identical texts -->
  <PropertyGroup Condition="'$(RemapIdenticalTexts)'=='yes'">
    <RemapIdenticalTexts>yes</RemapIdenticalTexts>
  </PropertyGroup>
  <PropertyGroup Condition="'$(RemapIdenticalTexts)'!='yes'">
    <RemapIdenticalTexts>no</RemapIdenticalTexts>
  </PropertyGroup>
  
  <!-- Text Data format option -->
  <PropertyGroup Condition="'$(TextDataFormat)'=='A4'">
    <TextDataFormat>A4</TextDataFormat>
  </PropertyGroup>
  <PropertyGroup Condition="'$(TextDataFormat)'!='A4'">
    <TextDataFormat></TextDataFormat>
  </PropertyGroup>
  
  <!-- Generate BitmapDatabase files using ImageConvert -->
  <Target Name="GenerateBitmapDatabaseFiles"
	  BeforeTargets="ClCompile">
    <Message Text="Using ImageConvert to generate BitmapDatabase files"/>
    <Exec Command="echo Using ImageConvert to generate BitmapDatabase files"/>
    <!-- Run ImageConvert to generate BitMapDatabase.hpp/.cpp files -->
    <Exec Command="&quot;$(TouchGFXReleasePath)\framework\tools\imageconvert\build\msvs\ImageConvert.exe&quot; -dither $(DitherAlgorithmOption) -alpha_dither $(AlphaDitherOption) -non_opaque_image_format $(NonOpaqueImageOutputOption) -opaque_image_format $(OpaqueImageOutputOption) $(RotateOption) -r &quot;$(ApplicationRoot)\assets\images&quot; -w &quot;$(ApplicationRoot)\generated\images&quot;"/>
  </Target>

  <PropertyGroup>
    <touchgfx_env Condition="'$(TouchGFXEnvPath)'==''">$(TouchGFXReleasePath)\..\env</touchgfx_env>
    <touchgfx_env Condition="'$(TouchGFXEnvPath)'!=''">$(TouchGFXEnvPath)</touchgfx_env>
  </PropertyGroup>
  
  <!-- Handle texts files in assets\texts -->
  <Target Name="GenerateFontsAndTextsFiles"
          BeforeTargets="ClCompile"
          Inputs="@(TextsAssets);@(FontsAssets)"
          Outputs="@(TextsAssets->'$(ApplicationRoot)\generated\texts\src\%(Filename).cpp')"
          >
    <Message Text="Compiling: %(TextsAssets.Identity) using textconvert" />
    <Exec Command="echo Using textconvert for %(TextsAssets.Identity)"/>
    <!-- Delete previous generated font and text files -->
    <Delete Files="@(AllGeneratedFontsAndTextsFiles)"/>
    <!-- Generate header and source files for fonts and texts -->
    <Exec Command="&quot;$(touchgfx_env)\MinGW\msys\1.0\ruby193\bin\ruby.exe&quot; &quot;$(TouchGFXReleasePath)\framework\tools\textconvert\main.rb&quot; &quot;%(TextsAssets.FullPath)&quot; &quot;$(TouchGFXReleasePath)\framework\tools\fontconvert\build\win\fontconvert.out&quot; &quot;$(ApplicationRoot)\generated\fonts&quot; &quot;$(ApplicationRoot)\generated\texts&quot; &quot;$(ApplicationRoot)\assets\fonts&quot; &quot;$(ApplicationRoot)&quot; $(RemapIdenticalTexts) $(TextDataFormat)"/>
  </Target>

  <!-- Compile all generated source files -->
  <Target Name="CompileAllGeneratedFiles"
          BeforeTargets="ClCompile"
          >
    <ItemGroup>
      <AllGeneratedCompileFiles Include="$(ApplicationRoot)\generated\**\*.cpp"
				Exclude="$(ApplicationRoot)\generated\gui_generated\**\*.cpp"/>
    </ItemGroup>
    <Message Text="Creating compile items for all generated source files: @(AllGeneratedCompileFiles)"/>
    <Exec Command="echo Creating compile items for all generated source files: @(AllGeneratedCompileFiles)"/>
    <!-- Create compile items for all generated .cpp files -->
    <CreateItem Include="@(AllGeneratedCompileFiles)">
      <Output TaskParameter="Include" ItemName="ClCompile"/>
    </CreateItem>
  </Target>

  <!-- Clean-up generated files -->
  <Target Name="CleanupGeneratedFiles"
          AfterTargets="Clean"
          >
    <Message Text="Clean-up generated files: @(AllGeneratedFiles)"/>
    <Exec Command="echo Clean-up generated files: @(AllGeneratedFiles)"/>

    <!-- Delete all generated files -->
    <Delete Files="@(AllGeneratedFiles)"/>
    <!--RemoveDir Directories="generated"/-->
  </Target>
</Project>
