<?xml version="1.0" encoding="utf-8"?>
<Resource>
  <Folder name="GacGenConfig">
    <Folder name="Cpp">
      <Text name="SourceFolder">Source</Text>
      <Text name="Resource">..\..\UIRes\CalculatorAndStateMachine.bin</Text>
      <Text name="NormalInclude">GacUI.h</Text>
      <Text name="ReflectionInclude">GacUIReflection.h</Text>
      <Text name="Name">Demo</Text>
    </Folder>
  </Folder>
  
  <Script name="ViewModelResource">
    <Workflow>
      <![CDATA[
        module viewmodel;
        
        using system::*;
        using presentation::*;
        
        namespace demo
        {
          class Calculator : StateMachine
          {
            var valueFirst : string = "";
            var op : string = "";
            prop Value : string = "0" {}
  
            func Update(value : string) : void
            {
              SetValue(value);
              valueFirst = value;
            }
  
            func Calculate() : void
            {
              if (valueFirst == "")
              {
                valueFirst = Value;
              }
              else if (op == "+")
              {
                Update((cast double valueFirst) + (cast double Value));
              }
              else if (op == "*")
              {
                Update((cast double valueFirst) * (cast double Value));
              }
              else
              {
                raise $"Unrecognized operator: $(op)";
              }
            }
  
            $state_machine
            {
              $state_input Digit(i : int);
              $state_input Dot();
              $state_input Add();
              $state_input Mul();
              $state_input Equal();
              $state_input Clear();
  
              $state Digits()
              {
                $switch (continue)
                {
                  case Digit(i):
                  {
                    Value = Value & i;
                    $goto_state Digits();
                  }
                }
              }

              $state Integer(newNumber: bool)
              {
                $switch (continue)
                {
                  case Digit(i):
                  {
                    if (newNumber)
                    {
                      Value = i;
                    }
                    else
                    {
                      Value = Value & i;
                    }
                    $goto_state Digits();
                  }
                }
              }

              $state Number()
              {
                $push_state Integer(true);
                $switch (continue, return)
                {
                  case Dot():
                  {
                    Value = Value & ".";
                  }
                }
                $push_state Integer(false);
              }

              $state Operator()
              {
                $push_state Number();
                $switch (raise)
                {
                  case Add():    {Calculate(); op = "+";}
                  case Mul():    {Calculate(); op = "*";}
                  case Equal():  {Calculate();}
                  case Clear():
                  {
                    valueFirst = "";
                    op = "";
                    Value = "0";
                  }
                }
                $goto_state Operator();
              }
    
              $state default()
              {
                $goto_state Operator();
              }
            }
          }
        }
      ]]>
    </Workflow>
  </Script>

  <InstanceStyle name="Style">
    <Styles>
      <Style ref.Path="//Cell/Button" Font="fontFamily:'Segoe UI' size: 24 antialias:true">
        <att.BoundsComposition-set AlignmentToParent="left:0 top:0 right:0 bottom:0"/>
      </Style>
    </Styles>
  </InstanceStyle>
  
  <Folder name="MainWindow">
    <Instance name="MainWindowResource">
      <Instance ref.CodeBehind="false" ref.Class="demo::MainWindow" ref.Styles="res://Style">
        <ref.Parameter Name="calculator" Class="demo::Calculator"/>
        <Window Text="CalculatorAndStateMachine" ClientSize="x:480 y:320">
          <att.BoundsComposition-set PreferredMinSize="x:480 y:320"/>

          <Table AlignmentToParent="left:0 top:0 right:0 bottom:0" CellPadding="5">
            <att.Rows>
              <CellOption>composeType:Percentage percentage:1</CellOption>
              <CellOption>composeType:Percentage percentage:1</CellOption>
              <CellOption>composeType:Percentage percentage:1</CellOption>
              <CellOption>composeType:Percentage percentage:1</CellOption>
              <CellOption>composeType:Percentage percentage:1</CellOption>
            </att.Rows>
            <att.Columns>
              <CellOption>composeType:Percentage percentage:1</CellOption>
              <CellOption>composeType:Percentage percentage:1</CellOption>
              <CellOption>composeType:Percentage percentage:1</CellOption>
              <CellOption>composeType:Percentage percentage:1</CellOption>
            </att.Columns>

            <Cell Site="row:0 column:0 columnSpan:4">
              <SolidLabel Font="fontFamily:'Segoe UI' size: 24 antialias:true" HorizontalAlignment="Right" VerticalAlignment="Center" Color="#FFFFFF" Text-bind="calculator.Value"/>
            </Cell>

            <Cell Site="row:1 column:0">
              <Button Text="1" ev.Clicked-eval="calculator.Digit(1);"/>
            </Cell>
            <Cell Site="row:1 column:1">
              <Button Text="2" ev.Clicked-eval="calculator.Digit(2);"/>
            </Cell>
            <Cell Site="row:1 column:2">
              <Button Text="3" ev.Clicked-eval="calculator.Digit(3);"/>
            </Cell>
            <Cell Site="row:2 column:0">
              <Button Text="4" ev.Clicked-eval="calculator.Digit(4);"/>
            </Cell>
            <Cell Site="row:2 column:1">
              <Button Text="5" ev.Clicked-eval="calculator.Digit(5);"/>
            </Cell>
            <Cell Site="row:2 column:2">
              <Button Text="6" ev.Clicked-eval="calculator.Digit(6);"/>
            </Cell>
            <Cell Site="row:3 column:0">
              <Button Text="7" ev.Clicked-eval="calculator.Digit(7);"/>
            </Cell>
            <Cell Site="row:3 column:1">
              <Button Text="8" ev.Clicked-eval="calculator.Digit(8);"/>
            </Cell>
            <Cell Site="row:3 column:2">
              <Button Text="9" ev.Clicked-eval="calculator.Digit(9);"/>
            </Cell>
            <Cell Site="row:4 column:1">
              <Button Text="0" ev.Clicked-eval="calculator.Digit(0);"/>
            </Cell>

            <Cell Site="row:4 column:0">
              <Button Text="." ev.Clicked-eval="calculator.Dot();"/>
            </Cell>
            <Cell Site="row:4 column:2">
              <Button Text="=" ev.Clicked-eval="calculator.Equal();"/>
            </Cell>
            <Cell Site="row:1 column:3">
              <Button Text="CE" ev.Clicked-eval="calculator.Clear();"/>
            </Cell>
            <Cell Site="row:2 column:3">
              <Button Text="*" ev.Clicked-eval="calculator.Mul();"/>
            </Cell>
            <Cell Site="row:3 column:3 rowSpan:2">
              <Button Text="+" ev.Clicked-eval="calculator.Add();"/>
            </Cell>
          </Table>
        </Window>
      </Instance>
    </Instance>
  </Folder>
</Resource>