<world>
  <textures>
    <texture name="menu_outside">
      <file>menu_textures/outside.png</file>
      <keepimage />
    </texture>
    <texture name="menu_bg">
      <file>menu_textures/menu_bg.png</file>
      <keepimage />
    </texture>
    <texture name="menu_container">
      <file>menu_textures/menu_container.png</file>
      <keepimage />
    </texture>
    <texture name="menu_item_def">
      <file>menu_textures/menu_item_def.png</file>
      <keepimage />
    </texture>
    <texture name="menu_item_hit">
      <file>menu_textures/menu_item_hit.png</file>
      <keepimage />
    </texture>
  </textures>

  <materials>
    <material name="menu_outside"><texture>menu_outside</texture></material>
    <material name="menu_bg"><texture>menu_bg</texture></material>
    <material name="menu_container"><texture>menu_container</texture></material>
    <material name="menu_item_def"><texture>menu_item_def</texture></material>
    <material name="menu_item_hit"><texture>menu_item_hit</texture></material>
  </materials>

  <sector name="room">
    <addon plugin="cel.addons.xmlscripts">
      <pcfactory>cel.pcfactory.billboard</pcfactory>
      <pcfactory>cel.pcfactory.properties</pcfactory>
      <pcfactory>cel.pcfactory.inventory</pcfactory>
      <layer>blxml</layer>

      <script name="menu">
        <event name="init">
          <createentity name="background" layer="blxml" behaviour="menu_bg" />
	  <createentity name="start_menu" layer="blxml"
                        behaviour="start_menu" />
        </event>
      </script>

      <script name="menu_item">
        <event name="init">
          <createpropclass name="pcbillboard" />
          <default propclass="pc(pcbillboard)" />
          <property id="propid(name)" value="item" />
          <property id="propid(materialname)" value="menu_item_def" />
          <property id="propid(movable)" value="false" />
          <property id="propid(clickable)" value="true" />
          <property id="propid(restack)" value="false" />
          <property id="propid(width)" value="67000" />
          <property id="propid(height)" value="20000" />
        </event>

        <!--
           Setup this menu item:
           @y is the y location of the menu item.
           @text is the text to use on the menu item.
           @event is the CC event to use when the item is selected.
	  -->
        <event name="setup">
          <bb_move x="-75000" y="@y" delta="0" />
          <bb_move x="0" y="@y" delta="500" />
          <default propclass="pc(pcbillboard)" />
          <property id="propid(text)" value="@text" />
          <property id="propid(text_offset)" value="[5000,3700]" />
          <var name="event" value="@event" />
	</event>
        <event name="pcbillboard_move">
          <expr eval="(?container)::selectmenu(item=ent())" />
        </event>
        <event name="pcbillboard_select">
          <expr eval="(?container)::closemenu(event=?event)" />
        </event>
      </script>

      <!--
        The main parent script for a menu.
        -->
      <script name="menu_container">
        <event name="init">
          <createpropclass name="pctimer" />
          <createpropclass name="pcproperties" />
          <createpropclass name="pcinventory" />
          <createpropclass name="pcbillboard" />

          <default propclass="pc(pcbillboard)" />
          <property id="propid(name)" value="cont" />
          <property id="propid(materialname)" value="menu_container" />
          <property id="propid(movable)" value="false" />
          <property id="propid(clickable)" value="false" />
          <property id="propid(restack)" value="false" />
          <property id="propid(width)" value="75000" />
          <property id="propid(height)" value="185000" />
          <property id="propid(x)" value="-75000" />
          <property id="propid(y)" value="90000" />
          <bb_move x="0" y="90000" delta="500" />

          <!-- Current selected menu item -->
          <var name="curmenu" value="''" />

          <!-- If this becomes filled then we fire this event in the timer -->
          <var name="exit_event" value="''" />

          <!-- Every few milliseconds we check if we're still on top of the
               current menu. If not we switch that menu back to old material -->
          <action propclass="pc(pctimer)" id="actid(WakeUp)">
            <par id="parid(time)" value="100" />
            <par id="parid(repeat)" value="true" />
          </action>

          <expr eval="resetmenu()" />
        </event>

        <!--
          Destroy this menu.
          -->
        <event name="destroymenu">
          <inventory propclass="pc(pcinventory)" />
          <while eval="inventory_count()>0" >
            <lvar name="c" value="inventory_get(0)" />
            <inventory_rem child="#c" />
            <destroyentity name="#c" />
          </while>
        </event>

        <!--
          Reset the menu.
          -->
        <event name="resetmenu">
          <var name="menu_y" value="117000" />
          <var name="menu_ent" value="1" />
        </event>

        <!--
          Create a menu item.
          @text is the text for the item
          @event is the event to fire
          -->
        <event name="createitem">
          <lvar name="ent" value="'m'+?menu_ent" />
          <createentity name="#ent" layer="blxml" behaviour="menu_item" />
          <expr eval="(#ent)::setup(text=@text,y=?menu_y,event=@event)" />
          <var name="menu_y" value="?menu_y+20100" />
          <var name="menu_ent" value="?menu_ent+1" />
          <inventory propclass="pc(ent(),pcinventory)" />
          <inventory_add child="#ent" />
          <var entity="#ent" name="container" value="ent()" />
        </event>

        <!--
          Select a menu item.
          @item is the entity for the item.
          -->
        <event name="selectmenu">
          <default propclass="pc(@item,pcbillboard)" />
          <property id="propid(materialnamefast)" value="menu_item_hit" />
          <if eval="?curmenu && ?curmenu != @item">
            <default propclass="pc(?curmenu,pcbillboard)" />
            <property id="propid(materialnamefast)" value="menu_item_def" />
          </if>
          <var name="curmenu" value="@item" />
        </event>

        <!--
          Close the menu and perform the specific event (in @event) on
          the game manager afterwards.
          -->
        <event name="closemenu">
          <inventory propclass="pc(pcinventory)" />
          <lvar name="c" value="inventory_count()" />
          <while eval="#c>0" >
            <lvar name="c" value="#c-1" />
            <lvar name="child" value="inventory_get(#c)" />
            <lvar name="pcbb" value="pc(#child,pcbillboard)" />
            <lvar name="y" value="property(#pcbb,propid(y))" />
            <bb_move pc="#pcbb" x="-75000" y="#y" delta="500" />
          </while>
        <!-- If we have a MENU_ event then we also slide away the background -->
	  <if eval="strsub(@event,0,5)=='MENU_'">
            <lvar name="pcbb" value="pc(pcbillboard)" />
            <lvar name="y" value="property(#pcbb,propid(y))" />
            <bb_move pc="#pcbb" x="-75000" y="#y" delta="500" />
          </if>
          <!-- The timer in this entity will catch the exit_event and then
               handle this -->
	  <var name="exit_event" value="@event" />
	  <var name="exit_event_counter" value="5" />
	</event>

        <!--
          Check if we're still on top of the menu item.
          -->
        <event name="pctimer_wakeup">
          <if eval="?exit_event">
            <var name="exit_event_counter" value="?exit_event_counter-1" />
            <if eval="?exit_event_counter<=0">
              <action propclass="pc(pctimer)" id="actid(Clear)" />
              <if eval="strsub(?exit_event,0,5)=='MENU_'">
                <lvar name="menu" value="strsub(?exit_event,5,1000)" />
                <expr eval="destroymenu()" />
                <destroyentity name="ent()" />
		<createentity name="#menu" layer="blxml" behaviour="#menu" />
		<stop />
              </if>
              <expr eval="destroymenu()" />
              <destroyentity name="ent()" />
              <call entity="menu" event="?exit_event" />
              <stop />
            </if>
          </if>
          <if eval="?curmenu">
            <lvar name="pcbb" value="pc(?curmenu,pcbillboard)" />
            <lvar name="mx" value="bb_mousex()" />
            <lvar name="my" value="bb_mousey()" />
            <lvar name="cx" value="property(#pcbb,propid(x))" />
            <lvar name="cy" value="property(#pcbb,propid(y))" />
            <lvar name="cw" value="property(#pcbb,propid(width))" />
            <lvar name="ch" value="property(#pcbb,propid(height))" />
            <if eval="!(#mx>#cx && #mx<(#cx+#cw) && #my>#cy && #my<(#cy+#ch))">
              <default propclass="#pcbb" />
              <property id="propid(materialnamefast)" value="menu_item_def" />
              <var name="curmenu" value="''" />
            </if>
          </if>
        </event>
      </script>

      <script name="start_menu">
        <superscript name="menu_container" />
	<event name="init">
          <createentity name="outside" layer="blxml" behaviour="menu_outside" />
          <super />
           <expr eval="createitem(text='',event=enter_game)" />
          <expr eval="createitem(text='',event=exit_menu)" />
        </event>

        <event name="destroymenu">
          <super />
	  <destroyentity name="outside" />
	</event>
      </script>

     
       <script name="menu_outside">
        <event name="init">
          <createpropclass name="pcbillboard" />
          <default propclass="pc(pcbillboard)" />
          <property id="propid(name)" value="bg_outside" />
          <property id="propid(materialname)" value="menu_outside" />
          <property id="propid(movable)" value="false" />
          <property id="propid(clickable)" value="false" />
          <property id="propid(restack)" value="false" />
          <property id="propid(width)" value="275000" />
          <property id="propid(height)" value="275000" />
          <property id="propid(x)" value="90000" />
          <property id="propid(y)" value="115000" />
        </event>
      </script>

 	<script name="menu_bg">
        <event name="init">
          <createpropclass name="pcbillboard" />
	    <default propclass="pc(pcbillboard)" />
          <property id="propid(name)" value="bg" />
          <property id="propid(materialname)" value="menu_bg" />
          <property id="propid(movable)" value="false" />
          <property id="propid(clickable)" value="false" />
          <property id="propid(restack)" value="false" />
          <property id="propid(width)" value="307200" />
          <property id="propid(height)" value="307200" />
          <property id="propid(x)" value="0" />
          <property id="propid(y)" value="0" />
        </event>
      </script>
    </addon>

    <addon plugin="cel.addons.celentity" entityname="menu" >
      <behaviour layer="blxml" name="menu" />
    </addon>
  </sector>
  <sequences/>
  <start name='Camera'>
    <sector>room</sector>
  </start>
</world>
