Bläddra i källkod

Merge branch 'feature/menu_module' into develop

Iñigo Valentin 2 dagar sedan
förälder
incheckning
44da2f151b

+ 1 - 0
data/data/screens/_screens.xml

@@ -23,6 +23,7 @@
     <screen file_name="screens/name_menu/name_menu.xml" />
     <screen file_name="screens/save_menu/save_menu.xml" />
     <screen file_name="screens/shop_menu/shop_menu.xml" />
+    <screen file_name="screens/exchange_menu/exchange_menu.xml" />
     <prototype file_name="screens/save_menu/prototypes.xml" />
     <!-- Battle UI -->
     <prototype file_name="screens/battle/prototypes.xml" />

+ 258 - 0
data/data/screens/exchange_menu/exchange_menu.xml

@@ -0,0 +1,258 @@
+<screen name="ExchangeMenu" script="scripts/menu/exchange_menu.lua" visible="false">
+    <sprite name="Background" x="0" y="0" width="100%" height="100%" colour="0 0 0" visible="true"/>
+    <widget name="Container" width="40%" height="40%" x="0" y="0" align="left" valign="top" scale="2.5 2.5" visible="true">
+        <widget name="Info" x="0" y="0" width="100%" height="28" visible="true">
+            <prototype name="Window" />
+            <sprite name="Icon" image="icons/materia_1.png" x="10" y="6" width="16" height="16" visible="false"/>
+            <text_area name="Name" x="32" y="8" font="FFVIIFont" visible="false" />
+            <widget name="Stars" x="100%-190" y="7" width="90" height="16" visible="true">
+                <sprite name="Star1" image="icons/materia_star_1.png" x="0" y="0" width="15" height="15" visible="false"/>
+                <sprite name="Star2" image="icons/materia_star_1.png" x="16" y="0" width="15" height="15" visible="false"/>
+                <sprite name="Star3" image="icons/materia_star_1.png" x="32" y="0" width="15" height="15" visible="false"/>
+                <sprite name="Star4" image="icons/materia_star_1.png" x="48" y="0" width="15" height="15" visible="false"/>
+                <sprite name="Star5" image="icons/materia_star_1.png" x="64" y="0" width="15" height="15" visible="false"/>
+            </widget>
+        </widget>
+        <widget name="Help" x="0" y="28" width="100%" height="24" visible="true">
+            <prototype name="Window" />
+            <text_area name="Help" x="16" y="6" font="FFVIIFont" visible="true" />
+        </widget>
+        <widget name="Title" x="100%-86" y="0" width="86" height="24" visible="true">
+            <prototype name="Window" />
+            <text_area name="Title" x="16" y="6" text_name="MateriaMenuTitle" font="FFVIIFont" visible="true" />
+        </widget>
+        <widget name="Characters" x="0" y="52" width="66%" height="100%-52" visible="true">
+            <sprite name="ScrollBg" x="100%-10" y="2" width="8" height="100%-6" colour="0.2 0.2 0.2" alpha="0.8" visible="true"/>
+            <sprite name="Scroll" x="100%-9" y="3" width="6" height="18" colour="0.7 0.7 0.7" visible="true"/>
+            <widget name="Block1" x="0" y="0" width="100%-12" height="58" visible="false">
+                <prototype name="Window" />
+                <sprite name="Portrait" image="other/choco.png" x="8" y="12" width="26" height="29" visible="true"/>
+                <text_area name="Name" x="40" y="6" font="FFVIIFont" visible="true" />
+                <sprite name="WIcon" image="icons/item_weapon_0.png" x="40" y="22" width="14" height="14" visible="true"/>
+                <sprite name="AIcon" image="icons/item_armor.png" x="40" y="39" width="14" height="14" visible="true"/>
+                <sprite name="WSlot1" image="icons/materia_slot.png" x="70" y="22" width="14" height="14" visible="false"/>
+                <sprite name="WSlot2" image="icons/materia_slot.png" x="86" y="22" width="14" height="14" visible="false"/>
+                <sprite name="WSlot3" image="icons/materia_slot.png" x="102" y="22" width="14" height="14" visible="false"/>
+                <sprite name="WSlot4" image="icons/materia_slot.png" x="118" y="22" width="14" height="14" visible="false"/>
+                <sprite name="WSlot5" image="icons/materia_slot.png" x="134" y="22" width="14" height="14" visible="false"/>
+                <sprite name="WSlot6" image="icons/materia_slot.png" x="150" y="22" width="14" height="14" visible="false"/>
+                <sprite name="WSlot7" image="icons/materia_slot.png" x="166" y="22" width="14" height="14" visible="false"/>
+                <sprite name="WSlot8" image="icons/materia_slot.png" x="182" y="22" width="14" height="14" visible="false"/>
+                <sprite name="WLink1" image="icons/materia_slot_link.png" x="81" y="22" width="8" height="14" visible="false"/>
+                <sprite name="WLink2" image="icons/materia_slot_link.png" x="113" y="22" width="8" height="14" visible="false"/>
+                <sprite name="WLink3" image="icons/materia_slot_link.png" x="145" y="22" width="8" height="14" visible="false"/>
+                <sprite name="WLink4" image="icons/materia_slot_link.png" x="177" y="22" width="8" height="14" visible="false"/>
+                <sprite name="WMat1" image="icons/materia_1.png" x="72" y="24" width="10" height="10" visible="false"/>
+                <sprite name="WMat2" image="icons/materia_1.png" x="88" y="24" width="10" height="10" visible="false"/>
+                <sprite name="WMat3" image="icons/materia_1.png" x="104" y="24" width="10" height="10" visible="false"/>
+                <sprite name="WMat4" image="icons/materia_1.png" x="120" y="24" width="10" height="10" visible="false"/>
+                <sprite name="WMat5" image="icons/materia_1.png" x="136" y="24" width="10" height="10" visible="false"/>
+                <sprite name="WMat6" image="icons/materia_1.png" x="152" y="24" width="10" height="10" visible="false"/>
+                <sprite name="WMat7" image="icons/materia_1.png" x="168" y="24" width="10" height="10" visible="false"/>
+                <sprite name="WMat8" image="icons/materia_1.png" x="184" y="24" width="10" height="10" visible="false"/>
+                <sprite name="ASlot1" image="icons/materia_slot.png" x="70" y="39" width="14" height="14" visible="false"/>
+                <sprite name="ASlot2" image="icons/materia_slot.png" x="86" y="39" width="14" height="14" visible="false"/>
+                <sprite name="ASlot3" image="icons/materia_slot.png" x="102" y="39" width="14" height="14" visible="false"/>
+                <sprite name="ASlot4" image="icons/materia_slot.png" x="118" y="39" width="14" height="14" visible="false"/>
+                <sprite name="ASlot5" image="icons/materia_slot.png" x="134" y="39" width="14" height="14" visible="false"/>
+                <sprite name="ASlot6" image="icons/materia_slot.png" x="150" y="39" width="14" height="14" visible="false"/>
+                <sprite name="ASlot7" image="icons/materia_slot.png" x="166" y="39" width="14" height="14" visible="false"/>
+                <sprite name="ASlot8" image="icons/materia_slot.png" x="182" y="39" width="14" height="14" visible="false"/>
+                <sprite name="ALink1" image="icons/materia_slot_link.png" x="81" y="39" width="8" height="14" visible="false"/>
+                <sprite name="ALink2" image="icons/materia_slot_link.png" x="113" y="39" width="8" height="14" visible="false"/>
+                <sprite name="ALink3" image="icons/materia_slot_link.png" x="145" y="39" width="8" height="14" visible="false"/>
+                <sprite name="ALink4" image="icons/materia_slot_link.png" x="177" y="39" width="8" height="14" visible="false"/>
+                <sprite name="AMat1" image="icons/materia_1.png" x="72" y="41" width="10" height="10" visible="false"/>
+                <sprite name="AMat2" image="icons/materia_1.png" x="88" y="41" width="10" height="10" visible="false"/>
+                <sprite name="AMat3" image="icons/materia_1.png" x="104" y="41" width="10" height="10" visible="false"/>
+                <sprite name="AMat4" image="icons/materia_1.png" x="120" y="41" width="10" height="10" visible="false"/>
+                <sprite name="AMat5" image="icons/materia_1.png" x="136" y="41" width="10" height="10" visible="false"/>
+                <sprite name="AMat6" image="icons/materia_1.png" x="152" y="41" width="10" height="10" visible="false"/>
+                <sprite name="AMat7" image="icons/materia_1.png" x="168" y="41" width="10" height="10" visible="false"/>
+                <sprite name="AMat8" image="icons/materia_1.png" x="184" y="41" width="10" height="10" visible="false"/>
+            </widget>
+            <widget name="Block2" x="0" y="58" width="100%-12" height="58" visible="false">
+                <prototype name="Window" />
+                <sprite name="Portrait" image="other/choco.png" x="8" y="12" width="26" height="29" visible="true"/>
+                <text_area name="Name" x="40" y="6" font="FFVIIFont" visible="true" />
+                <sprite name="WIcon" image="icons/item_weapon_0.png" x="40" y="22" width="14" height="14" visible="true"/>
+                <sprite name="AIcon" image="icons/item_armor.png" x="40" y="39" width="14" height="14" visible="true"/>
+                <sprite name="WSlot1" image="icons/materia_slot.png" x="70" y="22" width="14" height="14" visible="false"/>
+                <sprite name="WSlot2" image="icons/materia_slot.png" x="86" y="22" width="14" height="14" visible="false"/>
+                <sprite name="WSlot3" image="icons/materia_slot.png" x="102" y="22" width="14" height="14" visible="false"/>
+                <sprite name="WSlot4" image="icons/materia_slot.png" x="118" y="22" width="14" height="14" visible="false"/>
+                <sprite name="WSlot5" image="icons/materia_slot.png" x="134" y="22" width="14" height="14" visible="false"/>
+                <sprite name="WSlot6" image="icons/materia_slot.png" x="150" y="22" width="14" height="14" visible="false"/>
+                <sprite name="WSlot7" image="icons/materia_slot.png" x="166" y="22" width="14" height="14" visible="false"/>
+                <sprite name="WSlot8" image="icons/materia_slot.png" x="182" y="22" width="14" height="14" visible="false"/>
+                <sprite name="WLink1" image="icons/materia_slot_link.png" x="81" y="22" width="8" height="14" visible="false"/>
+                <sprite name="WLink2" image="icons/materia_slot_link.png" x="113" y="22" width="8" height="14" visible="false"/>
+                <sprite name="WLink3" image="icons/materia_slot_link.png" x="145" y="22" width="8" height="14" visible="false"/>
+                <sprite name="WLink4" image="icons/materia_slot_link.png" x="177" y="22" width="8" height="14" visible="false"/>
+                <sprite name="WMat1" image="icons/materia_1.png" x="72" y="24" width="10" height="10" visible="false"/>
+                <sprite name="WMat2" image="icons/materia_1.png" x="88" y="24" width="10" height="10" visible="false"/>
+                <sprite name="WMat3" image="icons/materia_1.png" x="104" y="24" width="10" height="10" visible="false"/>
+                <sprite name="WMat4" image="icons/materia_1.png" x="120" y="24" width="10" height="10" visible="false"/>
+                <sprite name="WMat5" image="icons/materia_1.png" x="136" y="24" width="10" height="10" visible="false"/>
+                <sprite name="WMat6" image="icons/materia_1.png" x="152" y="24" width="10" height="10" visible="false"/>
+                <sprite name="WMat7" image="icons/materia_1.png" x="168" y="24" width="10" height="10" visible="false"/>
+                <sprite name="WMat8" image="icons/materia_1.png" x="184" y="24" width="10" height="10" visible="false"/>
+                <sprite name="ASlot1" image="icons/materia_slot.png" x="70" y="39" width="14" height="14" visible="false"/>
+                <sprite name="ASlot2" image="icons/materia_slot.png" x="86" y="39" width="14" height="14" visible="false"/>
+                <sprite name="ASlot3" image="icons/materia_slot.png" x="102" y="39" width="14" height="14" visible="false"/>
+                <sprite name="ASlot4" image="icons/materia_slot.png" x="118" y="39" width="14" height="14" visible="false"/>
+                <sprite name="ASlot5" image="icons/materia_slot.png" x="134" y="39" width="14" height="14" visible="false"/>
+                <sprite name="ASlot6" image="icons/materia_slot.png" x="150" y="39" width="14" height="14" visible="false"/>
+                <sprite name="ASlot7" image="icons/materia_slot.png" x="166" y="39" width="14" height="14" visible="false"/>
+                <sprite name="ASlot8" image="icons/materia_slot.png" x="182" y="39" width="14" height="14" visible="false"/>
+                <sprite name="ALink1" image="icons/materia_slot_link.png" x="81" y="39" width="8" height="14" visible="false"/>
+                <sprite name="ALink2" image="icons/materia_slot_link.png" x="113" y="39" width="8" height="14" visible="false"/>
+                <sprite name="ALink3" image="icons/materia_slot_link.png" x="145" y="39" width="8" height="14" visible="false"/>
+                <sprite name="ALink4" image="icons/materia_slot_link.png" x="177" y="39" width="8" height="14" visible="false"/>
+                <sprite name="AMat1" image="icons/materia_1.png" x="72" y="41" width="10" height="10" visible="false"/>
+                <sprite name="AMat2" image="icons/materia_1.png" x="88" y="41" width="10" height="10" visible="false"/>
+                <sprite name="AMat3" image="icons/materia_1.png" x="104" y="41" width="10" height="10" visible="false"/>
+                <sprite name="AMat4" image="icons/materia_1.png" x="120" y="41" width="10" height="10" visible="false"/>
+                <sprite name="AMat5" image="icons/materia_1.png" x="136" y="41" width="10" height="10" visible="false"/>
+                <sprite name="AMat6" image="icons/materia_1.png" x="152" y="41" width="10" height="10" visible="false"/>
+                <sprite name="AMat7" image="icons/materia_1.png" x="168" y="41" width="10" height="10" visible="false"/>
+                <sprite name="AMat8" image="icons/materia_1.png" x="184" y="41" width="10" height="10" visible="false"/>
+            </widget>
+            <widget name="Block3" x="0" y="116" width="100%-12" height="58" visible="false">
+                <prototype name="Window" />
+                <sprite name="Portrait" image="other/choco.png" x="8" y="12" width="26" height="29" visible="true"/>
+                <text_area name="Name" x="40" y="6" font="FFVIIFont" visible="true" />
+                <sprite name="WIcon" image="icons/item_weapon_0.png" x="40" y="22" width="14" height="14" visible="true"/>
+                <sprite name="AIcon" image="icons/item_armor.png" x="40" y="39" width="14" height="14" visible="true"/>
+                <sprite name="WSlot1" image="icons/materia_slot.png" x="70" y="22" width="14" height="14" visible="false"/>
+                <sprite name="WSlot2" image="icons/materia_slot.png" x="86" y="22" width="14" height="14" visible="false"/>
+                <sprite name="WSlot3" image="icons/materia_slot.png" x="102" y="22" width="14" height="14" visible="false"/>
+                <sprite name="WSlot4" image="icons/materia_slot.png" x="118" y="22" width="14" height="14" visible="false"/>
+                <sprite name="WSlot5" image="icons/materia_slot.png" x="134" y="22" width="14" height="14" visible="false"/>
+                <sprite name="WSlot6" image="icons/materia_slot.png" x="150" y="22" width="14" height="14" visible="false"/>
+                <sprite name="WSlot7" image="icons/materia_slot.png" x="166" y="22" width="14" height="14" visible="false"/>
+                <sprite name="WSlot8" image="icons/materia_slot.png" x="182" y="22" width="14" height="14" visible="false"/>
+                <sprite name="WLink1" image="icons/materia_slot_link.png" x="81" y="22" width="8" height="14" visible="false"/>
+                <sprite name="WLink2" image="icons/materia_slot_link.png" x="113" y="22" width="8" height="14" visible="false"/>
+                <sprite name="WLink3" image="icons/materia_slot_link.png" x="145" y="22" width="8" height="14" visible="false"/>
+                <sprite name="WLink4" image="icons/materia_slot_link.png" x="177" y="22" width="8" height="14" visible="false"/>
+                <sprite name="WMat1" image="icons/materia_1.png" x="72" y="24" width="10" height="10" visible="false"/>
+                <sprite name="WMat2" image="icons/materia_1.png" x="88" y="24" width="10" height="10" visible="false"/>
+                <sprite name="WMat3" image="icons/materia_1.png" x="104" y="24" width="10" height="10" visible="false"/>
+                <sprite name="WMat4" image="icons/materia_1.png" x="120" y="24" width="10" height="10" visible="false"/>
+                <sprite name="WMat5" image="icons/materia_1.png" x="136" y="24" width="10" height="10" visible="false"/>
+                <sprite name="WMat6" image="icons/materia_1.png" x="152" y="24" width="10" height="10" visible="false"/>
+                <sprite name="WMat7" image="icons/materia_1.png" x="168" y="24" width="10" height="10" visible="false"/>
+                <sprite name="WMat8" image="icons/materia_1.png" x="184" y="24" width="10" height="10" visible="false"/>
+                <sprite name="ASlot1" image="icons/materia_slot.png" x="70" y="39" width="14" height="14" visible="false"/>
+                <sprite name="ASlot2" image="icons/materia_slot.png" x="86" y="39" width="14" height="14" visible="false"/>
+                <sprite name="ASlot3" image="icons/materia_slot.png" x="102" y="39" width="14" height="14" visible="false"/>
+                <sprite name="ASlot4" image="icons/materia_slot.png" x="118" y="39" width="14" height="14" visible="false"/>
+                <sprite name="ASlot5" image="icons/materia_slot.png" x="134" y="39" width="14" height="14" visible="false"/>
+                <sprite name="ASlot6" image="icons/materia_slot.png" x="150" y="39" width="14" height="14" visible="false"/>
+                <sprite name="ASlot7" image="icons/materia_slot.png" x="166" y="39" width="14" height="14" visible="false"/>
+                <sprite name="ASlot8" image="icons/materia_slot.png" x="182" y="39" width="14" height="14" visible="false"/>
+                <sprite name="ALink1" image="icons/materia_slot_link.png" x="81" y="39" width="8" height="14" visible="false"/>
+                <sprite name="ALink2" image="icons/materia_slot_link.png" x="113" y="39" width="8" height="14" visible="false"/>
+                <sprite name="ALink3" image="icons/materia_slot_link.png" x="145" y="39" width="8" height="14" visible="false"/>
+                <sprite name="ALink4" image="icons/materia_slot_link.png" x="177" y="39" width="8" height="14" visible="false"/>
+                <sprite name="AMat1" image="icons/materia_1.png" x="72" y="41" width="10" height="10" visible="false"/>
+                <sprite name="AMat2" image="icons/materia_1.png" x="88" y="41" width="10" height="10" visible="false"/>
+                <sprite name="AMat3" image="icons/materia_1.png" x="104" y="41" width="10" height="10" visible="false"/>
+                <sprite name="AMat4" image="icons/materia_1.png" x="120" y="41" width="10" height="10" visible="false"/>
+                <sprite name="AMat5" image="icons/materia_1.png" x="136" y="41" width="10" height="10" visible="false"/>
+                <sprite name="AMat6" image="icons/materia_1.png" x="152" y="41" width="10" height="10" visible="false"/>
+                <sprite name="AMat7" image="icons/materia_1.png" x="168" y="41" width="10" height="10" visible="false"/>
+                <sprite name="AMat8" image="icons/materia_1.png" x="184" y="41" width="10" height="10" visible="false"/>
+            </widget>
+            <widget name="Block4" x="0" y="174" width="100%-12" height="58" visible="false">
+                <prototype name="Window" />
+                <sprite name="Portrait" image="other/choco.png" x="8" y="12" width="26" height="29" visible="true"/>
+                <text_area name="Name" x="40" y="6" font="FFVIIFont" visible="true" />
+                <sprite name="WIcon" image="icons/item_weapon_0.png" x="40" y="22" width="14" height="14" visible="true"/>
+                <sprite name="AIcon" image="icons/item_armor.png" x="40" y="39" width="14" height="14" visible="true"/>
+                <sprite name="WSlot1" image="icons/materia_slot.png" x="70" y="22" width="14" height="14" visible="false"/>
+                <sprite name="WSlot2" image="icons/materia_slot.png" x="86" y="22" width="14" height="14" visible="false"/>
+                <sprite name="WSlot3" image="icons/materia_slot.png" x="102" y="22" width="14" height="14" visible="false"/>
+                <sprite name="WSlot4" image="icons/materia_slot.png" x="118" y="22" width="14" height="14" visible="false"/>
+                <sprite name="WSlot5" image="icons/materia_slot.png" x="134" y="22" width="14" height="14" visible="false"/>
+                <sprite name="WSlot6" image="icons/materia_slot.png" x="150" y="22" width="14" height="14" visible="false"/>
+                <sprite name="WSlot7" image="icons/materia_slot.png" x="166" y="22" width="14" height="14" visible="false"/>
+                <sprite name="WSlot8" image="icons/materia_slot.png" x="182" y="22" width="14" height="14" visible="false"/>
+                <sprite name="WLink1" image="icons/materia_slot_link.png" x="81" y="22" width="8" height="14" visible="false"/>
+                <sprite name="WLink2" image="icons/materia_slot_link.png" x="113" y="22" width="8" height="14" visible="false"/>
+                <sprite name="WLink3" image="icons/materia_slot_link.png" x="145" y="22" width="8" height="14" visible="false"/>
+                <sprite name="WLink4" image="icons/materia_slot_link.png" x="177" y="22" width="8" height="14" visible="false"/>
+                <sprite name="WMat1" image="icons/materia_1.png" x="72" y="24" width="10" height="10" visible="false"/>
+                <sprite name="WMat2" image="icons/materia_1.png" x="88" y="24" width="10" height="10" visible="false"/>
+                <sprite name="WMat3" image="icons/materia_1.png" x="104" y="24" width="10" height="10" visible="false"/>
+                <sprite name="WMat4" image="icons/materia_1.png" x="120" y="24" width="10" height="10" visible="false"/>
+                <sprite name="WMat5" image="icons/materia_1.png" x="136" y="24" width="10" height="10" visible="false"/>
+                <sprite name="WMat6" image="icons/materia_1.png" x="152" y="24" width="10" height="10" visible="false"/>
+                <sprite name="WMat7" image="icons/materia_1.png" x="168" y="24" width="10" height="10" visible="false"/>
+                <sprite name="WMat8" image="icons/materia_1.png" x="184" y="24" width="10" height="10" visible="false"/>
+                <sprite name="ASlot1" image="icons/materia_slot.png" x="70" y="39" width="14" height="14" visible="false"/>
+                <sprite name="ASlot2" image="icons/materia_slot.png" x="86" y="39" width="14" height="14" visible="false"/>
+                <sprite name="ASlot3" image="icons/materia_slot.png" x="102" y="39" width="14" height="14" visible="false"/>
+                <sprite name="ASlot4" image="icons/materia_slot.png" x="118" y="39" width="14" height="14" visible="false"/>
+                <sprite name="ASlot5" image="icons/materia_slot.png" x="134" y="39" width="14" height="14" visible="false"/>
+                <sprite name="ASlot6" image="icons/materia_slot.png" x="150" y="39" width="14" height="14" visible="false"/>
+                <sprite name="ASlot7" image="icons/materia_slot.png" x="166" y="39" width="14" height="14" visible="false"/>
+                <sprite name="ASlot8" image="icons/materia_slot.png" x="182" y="39" width="14" height="14" visible="false"/>
+                <sprite name="ALink1" image="icons/materia_slot_link.png" x="81" y="39" width="8" height="14" visible="false"/>
+                <sprite name="ALink2" image="icons/materia_slot_link.png" x="113" y="39" width="8" height="14" visible="false"/>
+                <sprite name="ALink3" image="icons/materia_slot_link.png" x="145" y="39" width="8" height="14" visible="false"/>
+                <sprite name="ALink4" image="icons/materia_slot_link.png" x="177" y="39" width="8" height="14" visible="false"/>
+                <sprite name="AMat1" image="icons/materia_1.png" x="72" y="41" width="10" height="10" visible="false"/>
+                <sprite name="AMat2" image="icons/materia_1.png" x="88" y="41" width="10" height="10" visible="false"/>
+                <sprite name="AMat3" image="icons/materia_1.png" x="104" y="41" width="10" height="10" visible="false"/>
+                <sprite name="AMat4" image="icons/materia_1.png" x="120" y="41" width="10" height="10" visible="false"/>
+                <sprite name="AMat5" image="icons/materia_1.png" x="136" y="41" width="10" height="10" visible="false"/>
+                <sprite name="AMat6" image="icons/materia_1.png" x="152" y="41" width="10" height="10" visible="false"/>
+                <sprite name="AMat7" image="icons/materia_1.png" x="168" y="41" width="10" height="10" visible="false"/>
+                <sprite name="AMat8" image="icons/materia_1.png" x="184" y="41" width="10" height="10" visible="false"/>
+            </widget>
+            <sprite name="Cursor" image="icons/cursor.png" x="48" y="18" width="24" height="17" visible="false"/>
+            <sprite name="Held" image="icons/cursor.png" x="48" y="18" width="24" height="17" visible="false">
+                <animation name="Blink" length="0.4" alpha="0:1, 0.2:1, 0.21:0, 0.39:0, 0.4:1" />
+            </sprite>
+        </widget>
+        <widget name="Inventory" x="66%" y="52" width="34%" height="100%-52" visible="true">
+            <prototype name="Window" />
+            <sprite name="ScrollBg" x="100%-12" y="2" width="10" height="100%-6" colour="0.2 0.2 0.2" alpha="0.8" visible="true"/>
+            <sprite name="Scroll" x="100%-11" y="3" width="8" height="18" colour="0.7 0.7 0.7" visible="true"/>
+            <sprite name="Icon1" image="icons/materia_1.png" x="8" y="7" width="12" height="12" visible="false"/>
+            <text_area name="Name1" x="24" y="6" font="FFVIIFont" visible="false" />
+            <sprite name="Icon2" image="icons/materia_1.png" x="8" y="23" width="12" height="12" visible="false"/>
+            <text_area name="Name2" x="24" y="22" font="FFVIIFont" visible="false" />
+            <sprite name="Icon3" image="icons/materia_1.png" x="8" y="39" width="12" height="12" visible="false"/>
+            <text_area name="Name3" x="24" y="38" font="FFVIIFont" visible="false" />
+            <sprite name="Icon4" image="icons/materia_1.png" x="8" y="55" width="12" height="12" visible="false"/>
+            <text_area name="Name4" x="24" y="54" font="FFVIIFont" visible="false" />
+            <sprite name="Icon5" image="icons/materia_1.png" x="8" y="71" width="12" height="12" visible="false"/>
+            <text_area name="Name5" x="24" y="70" font="FFVIIFont" visible="false" />
+            <sprite name="Icon6" image="icons/materia_1.png" x="8" y="87" width="12" height="12" visible="false"/>
+            <text_area name="Name6" x="24" y="86" font="FFVIIFont" visible="false" />
+            <sprite name="Icon7" image="icons/materia_1.png" x="8" y="103" width="12" height="12" visible="false"/>
+            <text_area name="Name7" x="24" y="102" font="FFVIIFont" visible="false" />
+            <sprite name="Icon8" image="icons/materia_1.png" x="8" y="119" width="12" height="12" visible="false"/>
+            <text_area name="Name8" x="24" y="118" font="FFVIIFont" visible="false" />
+            <sprite name="Icon9" image="icons/materia_1.png" x="8" y="135" width="12" height="12" visible="false"/>
+            <text_area name="Name9" x="24" y="134" font="FFVIIFont" visible="false" />
+            <sprite name="Icon10" image="icons/materia_1.png" x="8" y="151" width="12" height="12" visible="false"/>
+            <text_area name="Name10" x="24" y="150" font="FFVIIFont" visible="false" />
+            <sprite name="Icon11" image="icons/materia_1.png" x="8" y="167" width="12" height="12" visible="false"/>
+            <text_area name="Name11" x="24" y="166" font="FFVIIFont" visible="false" />
+            <sprite name="Icon12" image="icons/materia_1.png" x="8" y="183" width="12" height="12" visible="false"/>
+            <text_area name="Name12" x="24" y="182" font="FFVIIFont" visible="false" />
+            <sprite name="Icon13" image="icons/materia_1.png" x="8" y="199" width="12" height="12" visible="false"/>
+            <text_area name="Name13" x="24" y="198" font="FFVIIFont" visible="false" />
+            <sprite name="Icon14" image="icons/materia_1.png" x="8" y="215" width="12" height="12" visible="false"/>
+            <text_area name="Name14" x="24" y="214" font="FFVIIFont" visible="false" />
+            <sprite name="Cursor" image="icons/cursor.png" x="-8" y="5" width="24" height="17" visible="false"/>
+            <sprite name="Held" image="icons/cursor.png" x="-8" y="5" width="24" height="17" visible="false">
+                <animation name="Blink" length="0.4" alpha="0:1, 0.2:1, 0.21:0, 0.39:0, 0.4:1" />
+            </sprite>
+        </widget>
+    </widget>
+</screen>

+ 15 - 0
data/data/screens/item_menu/item_menu.xml

@@ -136,6 +136,21 @@
                 <animation name="Position12" length="0" y="0:82%+2" />
                 <animation name="Position13" length="0" y="0:89%+2" />
             </sprite>
+            <sprite name="Held" image="icons/cursor.png" x="-11" y="5%+2" width="24" height="17" visible="false">
+                <animation name="Blink1" length="0.4" y="0:5%+2" alpha="0:1, 0.2:1, 0.21:0, 0.39:0, 0.4:1" />
+                <animation name="Blink2" length="0.4" y="0:12%+2" alpha="0:1, 0.2:1, 0.21:0, 0.39:0, 0.4:1" />
+                <animation name="Blink3" length="0.4" y="0:19%+2" alpha="0:1, 0.2:1, 0.21:0, 0.39:0, 0.4:1" />
+                <animation name="Blink4" length="0.4" y="0:26%+2" alpha="0:1, 0.2:1, 0.21:0, 0.39:0, 0.4:1" />
+                <animation name="Blink5" length="0.4" y="0:33%+2" alpha="0:1, 0.2:1, 0.21:0, 0.39:0, 0.4:1" />
+                <animation name="Blink6" length="0.4" y="0:40%+2" alpha="0:1, 0.2:1, 0.21:0, 0.39:0, 0.4:1" />
+                <animation name="Blink7" length="0.4" y="0:47%+2" alpha="0:1, 0.2:1, 0.21:0, 0.39:0, 0.4:1" />
+                <animation name="Blink8" length="0.4" y="0:54%+2" alpha="0:1, 0.2:1, 0.21:0, 0.39:0, 0.4:1" />
+                <animation name="Blink9" length="0.4" y="0:61%+2" alpha="0:1, 0.2:1, 0.21:0, 0.39:0, 0.4:1" />
+                <animation name="Blink10" length="0.4" y="0:68%+2" alpha="0:1, 0.2:1, 0.21:0, 0.39:0, 0.4:1" />
+                <animation name="Blink11" length="0.4" y="0:75%+2" alpha="0:1, 0.2:1, 0.21:0, 0.39:0, 0.4:1" />
+                <animation name="Blink12" length="0.4" y="0:82%+2" alpha="0:1, 0.2:1, 0.21:0, 0.39:0, 0.4:1" />
+                <animation name="Blink13" length="0.4" y="0:89%+2" alpha="0:1, 0.2:1, 0.21:0, 0.39:0, 0.4:1" />
+            </sprite>
 
 
         </widget>

+ 5 - 0
data/data/screens/main_menu/main_menu.xml

@@ -63,6 +63,11 @@
                 <animation name="Position2" length="0" y="0:32%+40" />
                 <animation name="Position3" length="0" y="0:62%+40" />
             </sprite>
+            <sprite name="Held" image="icons/cursor.png" x="0" y="2%+40" width="24" height="16" visible="false">
+                <animation name="Blink1" length="0.4" y="0:2%+40" alpha="0:1, 0.2:1, 0.21:0, 0.39:0, 0.4:1" />
+                <animation name="Blink2" length="0.4" y="0:32%+40" alpha="0:1, 0.2:1, 0.21:0, 0.39:0, 0.4:1" />
+                <animation name="Blink3" length="0.4" y="0:62%+40" alpha="0:1, 0.2:1, 0.21:0, 0.39:0, 0.4:1" />
+            </sprite>
         </widget>
         <widget name="Menu" x="100%-105" y="2%" width="90" height="144" visible="true">
             <prototype name="Window" />

+ 87 - 2
data/data/screens/materia_menu/materia_menu.xml

@@ -90,9 +90,34 @@
             <widget name="Empty" x="0" y ="0" width="100%" height="100%" visible="false">
             </widget>
             <widget name="Commands" x="0" y ="0" width="100%" height="100%" visible="false">
-                <widget name="Box" x="20" y ="20" width="100%-40" height="60" visible="true">
+                <widget name="Box" x="4" y="12" width="100%-8" height="88" visible="true">
                     <prototype name="Window" />
-                    <!-- TODO -->
+                    <text_area name="Command1" x="20" y="8" font="FFVIIFont" visible="false" />
+                    <text_area name="Command2" x="20" y="26" font="FFVIIFont" visible="false" />
+                    <text_area name="Command3" x="20" y="44" font="FFVIIFont" visible="false" />
+                    <text_area name="Command4" x="20" y="62" font="FFVIIFont" visible="false" />
+                    <text_area name="Command5" x="88" y="8" font="FFVIIFont" visible="false" />
+                    <text_area name="Command6" x="88" y="26" font="FFVIIFont" visible="false" />
+                    <text_area name="Command7" x="88" y="44" font="FFVIIFont" visible="false" />
+                    <text_area name="Command8" x="88" y="62" font="FFVIIFont" visible="false" />
+                    <text_area name="Command9" x="156" y="8" font="FFVIIFont" visible="false" />
+                    <text_area name="Command10" x="156" y="26" font="FFVIIFont" visible="false" />
+                    <text_area name="Command11" x="156" y="44" font="FFVIIFont" visible="false" />
+                    <text_area name="Command12" x="156" y="62" font="FFVIIFont" visible="false" />
+                    <sprite name="Cursor" image="icons/cursor.png" x="3" y="10" width="24" height="17" visible="false">
+                        <animation name="Position1" length="0" x="0:3" y="0:10" />
+                        <animation name="Position2" length="0" x="0:3" y="0:28" />
+                        <animation name="Position3" length="0" x="0:3" y="0:46" />
+                        <animation name="Position4" length="0" x="0:3" y="0:64" />
+                        <animation name="Position5" length="0" x="0:71" y="0:10" />
+                        <animation name="Position6" length="0" x="0:71" y="0:28" />
+                        <animation name="Position7" length="0" x="0:71" y="0:46" />
+                        <animation name="Position8" length="0" x="0:71" y="0:64" />
+                        <animation name="Position9" length="0" x="0:139" y="0:10" />
+                        <animation name="Position10" length="0" x="0:139" y="0:28" />
+                        <animation name="Position11" length="0" x="0:139" y="0:46" />
+                        <animation name="Position12" length="0" x="0:139" y="0:64" />
+                    </sprite>
                 </widget>
             </widget>
             <widget name="EnemySkill" x="0" y ="0" width="100%" height="100%" visible="false">
@@ -229,5 +254,65 @@
                 <animation name="Position10" length="0" y="0:85%+3" />
             </sprite>
         </widget>
+        <widget name="Abilities" x="230" y="99" width="100%-230" height="100%-99" visible="false">
+            <prototype name="Window" />
+            <text_area name="Name1" x="20" y="4%" font="FFVIIFont" visible="true" />
+            <text_area name="Mp1" x="100%-48" y="4%" font="FFVIIMenuDigits" visible="true" />
+            <text_area name="Name2" x="20" y="13%" font="FFVIIFont" visible="true" />
+            <text_area name="Mp2" x="100%-48" y="13%" font="FFVIIMenuDigits" visible="true" />
+            <text_area name="Name3" x="20" y="22%" font="FFVIIFont" visible="true" />
+            <text_area name="Mp3" x="100%-48" y="22%" font="FFVIIMenuDigits" visible="true" />
+            <text_area name="Name4" x="20" y="31%" font="FFVIIFont" visible="true" />
+            <text_area name="Mp4" x="100%-48" y="31%" font="FFVIIMenuDigits" visible="true" />
+            <text_area name="Name5" x="20" y="40%" font="FFVIIFont" visible="true" />
+            <text_area name="Mp5" x="100%-48" y="40%" font="FFVIIMenuDigits" visible="true" />
+            <text_area name="Name6" x="20" y="49%" font="FFVIIFont" visible="true" />
+            <text_area name="Mp6" x="100%-48" y="49%" font="FFVIIMenuDigits" visible="true" />
+            <text_area name="Name7" x="20" y="58%" font="FFVIIFont" visible="true" />
+            <text_area name="Mp7" x="100%-48" y="58%" font="FFVIIMenuDigits" visible="true" />
+            <text_area name="Name8" x="20" y="67%" font="FFVIIFont" visible="true" />
+            <text_area name="Mp8" x="100%-48" y="67%" font="FFVIIMenuDigits" visible="true" />
+            <text_area name="Name9" x="20" y="76%" font="FFVIIFont" visible="true" />
+            <text_area name="Mp9" x="100%-48" y="76%" font="FFVIIMenuDigits" visible="true" />
+            <text_area name="Name10" x="20" y="85%" font="FFVIIFont" visible="true" />
+            <text_area name="Mp10" x="100%-48" y="85%" font="FFVIIMenuDigits" visible="true" />
+            <sprite name="Cursor" image="icons/cursor.png" x="-15" y="8" width="24" height="17" visible="true">
+                <animation name="Position1" length="0" y="0:4%+3" />
+                <animation name="Position2" length="0" y="0:13%+3" />
+                <animation name="Position3" length="0" y="0:22%+3" />
+                <animation name="Position4" length="0" y="0:31%+3" />
+                <animation name="Position5" length="0" y="0:40%+3" />
+                <animation name="Position6" length="0" y="0:49%+3" />
+                <animation name="Position7" length="0" y="0:58%+3" />
+                <animation name="Position8" length="0" y="0:67%+3" />
+                <animation name="Position9" length="0" y="0:76%+3" />
+                <animation name="Position10" length="0" y="0:85%+3" />
+            </sprite>
+        </widget>
+        <widget name="OrderMenu" x="170" y="2" width="108" height="84" visible="false">
+            <prototype name="Window" />
+            <text_area name="Item1" text_name="MateriaMenuArrange" x="20" y="6" font="FFVIIFont" visible="true" />
+            <text_area name="Item2" text_name="MateriaMenuExchange" x="20" y="24" font="FFVIIFont" visible="true" />
+            <text_area name="Item3" text_name="MateriaMenuRemoveAll" x="20" y="42" font="FFVIIFont" visible="true" />
+            <text_area name="Item4" text_name="MateriaMenuTrash" x="20" y="60" font="FFVIIFont" visible="true" />
+            <sprite name="Cursor" image="icons/cursor.png" x="-8" y="8" width="24" height="17" visible="true">
+                <animation name="Position1" length="0" y="0:8" />
+                <animation name="Position2" length="0" y="0:26" />
+                <animation name="Position3" length="0" y="0:44" />
+                <animation name="Position4" length="0" y="0:62" />
+            </sprite>
+        </widget>
+        <widget name="TrashWindow" x="50%-110" y="50%-31" width="220" height="64" visible="false">
+            <prototype name="Window" />
+            <sprite name="Icon" image="icons/materia_0.png" x="12" y="6" width="18" height="18" visible="true"/>
+            <text_area name="Name" x="36" y="9" font="FFVIIFont" visible="true" />
+            <sprite name="Star1" image="icons/materia_star_0.png" x="12" y="28" width="15" height="15" visible="true"/>
+            <sprite name="Star2" image="icons/materia_star_0.png" x="28" y="28" width="15" height="15" visible="true"/>
+            <sprite name="Star3" image="icons/materia_star_0.png" x="44" y="28" width="15" height="15" visible="true"/>
+            <sprite name="Star4" image="icons/materia_star_0.png" x="60" y="28" width="15" height="15" visible="true"/>
+            <sprite name="Star5" image="icons/materia_star_0.png" x="76" y="28" width="15" height="15" visible="true"/>
+            <text_area name="LblAP" text_name="MateriaMenuAP" x="12" y="47" font="FFVIIFont" visible="true" />
+            <text_area name="AP" x="100%-90" y="47" font="FFVIIMenuDigits" visible="true" />
+        </widget>
     </widget>
 </screen>

+ 25 - 0
data/data/screens/save_menu/save_menu.xml

@@ -25,5 +25,30 @@
             <animation name="Position2" length="0" y="0:48%+12" />
             <animation name="Position3" length="0" y="0:81%+12" />
         </sprite>
+        <widget name="ConfirmSame" x="50%-100" y="50%-33" width="200" height="66" visible="false">
+            <prototype name="Window" />
+            <text_area name="Question" text_name="SaveMenuOverride" x="16" y="8" font="FFVIIFont" visible="true" />
+            <text_area name="Yes" text_name="Yes" x="44" y="28" font="FFVIIFont" visible="true" />
+            <text_area name="No" text_name="No" x="44" y="44" font="FFVIIFont" visible="true" />
+            <sprite name="Cursor" image="icons/cursor.png" x="20" y="29" width="24" height="17" visible="true">
+                <animation name="Position1" length="0" y="0:29" />
+                <animation name="Position2" length="0" y="0:45" />
+            </sprite>
+        </widget>
+        <widget name="ConfirmOther" x="50%-150" y="50%-41" width="300" height="82" visible="false">
+            <prototype name="Window" />
+            <text_area name="Question" text_name="SaveMenuOverride" x="16" y="8" font="FFVIIFont" visible="true" />
+            <text_area name="Warning" text_name="SaveMenuOtherPlaythrough" x="16" y="24" font="FFVIIFont" visible="true" />
+            <text_area name="Yes" text_name="Yes" x="44" y="44" font="FFVIIFont" visible="true" />
+            <text_area name="No" text_name="No" x="44" y="60" font="FFVIIFont" visible="true" />
+            <sprite name="Cursor" image="icons/cursor.png" x="20" y="45" width="24" height="17" visible="true">
+                <animation name="Position1" length="0" y="0:45" />
+                <animation name="Position2" length="0" y="0:61" />
+            </sprite>
+        </widget>
+        <widget name="Saved" x="50%-60" y="50%-17" width="120" height="34" visible="false">
+            <prototype name="Window" />
+            <text_area name="Text" text_name="SaveMenuSaved" x="16" y="9" font="FFVIIFont" visible="true" />
+        </widget>
     </widget>
 </screen>

+ 27 - 1
data/data/screens/shop_menu/shop_menu.xml

@@ -173,12 +173,38 @@
                 <text_area name="DefNew" x="78" y="15" font="FFVIIMenuDigits" visible="true" />
             </widget>
         </widget>
-        <widget name="Quantity" x="12" y="82" width="62%-24" height="44" visible="false">
+        <widget name="Quantity" x="50%-110" y="50%-31" width="220" height="62" visible="false">
             <prototype name="Window" />
             <text_area name="Name" x="12" y="7" font="FFVIIFont" visible="true" />
             <text_area name="Times" text_name="ShopMenuTimes" x="12" y="23" font="FFVIIFont" visible="true" />
             <text_area name="Count" x="24" y="24" font="FFVIIMenuDigits" visible="true" />
             <text_area name="Total" x="100%-90" y="24" font="FFVIIMenuDigits" visible="true" />
+            <sprite name="Slot1" image="icons/materia_slot.png" x="12" y="42" width="16" height="16" visible="false"/>
+            <sprite name="Slot2" image="icons/materia_slot.png" x="29" y="42" width="16" height="16" visible="false"/>
+            <sprite name="Link1" image="icons/materia_slot_link.png" x="24" y="42" width="9" height="16" visible="false"/>
+            <sprite name="Slot3" image="icons/materia_slot.png" x="46" y="42" width="16" height="16" visible="false"/>
+            <sprite name="Slot4" image="icons/materia_slot.png" x="63" y="42" width="16" height="16" visible="false"/>
+            <sprite name="Link2" image="icons/materia_slot_link.png" x="58" y="42" width="9" height="16" visible="false"/>
+            <sprite name="Slot5" image="icons/materia_slot.png" x="80" y="42" width="16" height="16" visible="false"/>
+            <sprite name="Slot6" image="icons/materia_slot.png" x="97" y="42" width="16" height="16" visible="false"/>
+            <sprite name="Link3" image="icons/materia_slot_link.png" x="92" y="42" width="9" height="16" visible="false"/>
+            <sprite name="Slot7" image="icons/materia_slot.png" x="114" y="42" width="16" height="16" visible="false"/>
+            <sprite name="Slot8" image="icons/materia_slot.png" x="131" y="42" width="16" height="16" visible="false"/>
+            <sprite name="Link4" image="icons/materia_slot_link.png" x="126" y="42" width="9" height="16" visible="false"/>
+        </widget>
+        <widget name="MateriaSale" x="50%-110" y="50%-41" width="220" height="82" visible="false">
+            <prototype name="Window" />
+            <sprite name="Icon" image="icons/materia_0.png" x="12" y="6" width="18" height="18" visible="true"/>
+            <text_area name="Name" x="36" y="9" font="FFVIIFont" visible="true" />
+            <sprite name="Star1" image="icons/materia_star_0.png" x="12" y="28" width="15" height="15" visible="true"/>
+            <sprite name="Star2" image="icons/materia_star_0.png" x="28" y="28" width="15" height="15" visible="true"/>
+            <sprite name="Star3" image="icons/materia_star_0.png" x="44" y="28" width="15" height="15" visible="true"/>
+            <sprite name="Star4" image="icons/materia_star_0.png" x="60" y="28" width="15" height="15" visible="true"/>
+            <sprite name="Star5" image="icons/materia_star_0.png" x="76" y="28" width="15" height="15" visible="true"/>
+            <text_area name="ApLbl" text_name="ShopMenuAP" x="12" y="47" font="FFVIIFont" visible="true" />
+            <text_area name="Ap" x="100%-90" y="47" font="FFVIIMenuDigits" visible="true" />
+            <text_area name="PriceLbl" text_name="ShopMenuGil" x="12" y="63" font="FFVIIFont" visible="true" />
+            <text_area name="Price" x="100%-90" y="63" font="FFVIIMenuDigits" visible="true" />
         </widget>
     </widget>
 </screen>

+ 50 - 0
data/data/scripts/character_abilities.lua

@@ -91,6 +91,56 @@ Characters.get_abilities = function(char_id)
     return result
 end
 
+--- Gets the commands a character has in battle, in the order the battle menu shows them.
+--
+-- The Attack command (or the materia that replace it, like Slash-All or Flash) goes first. Then
+-- come the commands of the materia, Item (unless W-Item is equipped), Magic and Summon (if the
+-- character has spells or summons, and no W-Magic or W-Summon is equipped). The Limit command is
+-- not included, as it only appears in battle when the limit bar is full.
+--
+-- @param char_id ID of the character.
+-- @return List of IDs of commands (see Game.Commands).
+Characters.get_commands = function(char_id)
+    local commands = { 1 }
+    local character = Characters[char_id]
+    if character == nil then return commands end
+    local w_magic, w_summon, w_item = false, false, false
+    for _, part in ipairs({ "weapon", "armor" }) do
+        local equipment = character[part]
+        for slot = 0, 7 do
+            local equipped = equipment.materia[slot]
+            local materia = (equipped ~= nil) and Game.Materia[equipped.id] or nil
+            if materia ~= nil and materia.type == Materia.TYPE.COMMAND and materia.command ~= nil then
+                local level = get_materia_level(materia, equipped.ap or 0)
+                local command
+                for l, id in ipairs(materia.command) do
+                    if level >= l then command = id end
+                end
+                if command ~= nil then
+                    if command == 1 or command == 20 or (command >= 24 and command <= 27) then
+                        commands[1] = command
+                    else
+                        commands[#commands + 1] = command
+                    end
+                    if command == 21 then w_magic = true end
+                    if command == 22 then w_summon = true end
+                    if command == 23 then w_item = true end
+                end
+            end
+        end
+    end
+    local abilities = Characters.get_abilities(char_id)
+    if not w_item then commands[#commands + 1] = 4 end
+    if #abilities.magic > 0 and not w_magic then commands[#commands + 1] = 2 end
+    if #abilities.summon > 0 and not w_summon then commands[#commands + 1] = 3 end
+    local function substitute(id) return id == 1 or id == 20 or (id >= 24 and id <= 27) end
+    table.sort(commands, function(l, r)
+        if substitute(l) ~= substitute(r) then return substitute(l) end
+        return l < r
+    end)
+    return commands
+end
+
 --- Gets what the independent materia (Long Range, Mega All, Counter Attack, Cover, Pre-Emptive, EXP
 -- Plus and Gil Plus) of a character do in battle.
 --

+ 2 - 0
data/data/scripts/data.lua

@@ -247,6 +247,8 @@ MenuSettings = {
 --- Current party.
 Party = {
     MAX = 3,
+    --- Leader of the party in the field, the character the player controls. nil: the first member.
+    leader = nil,
     [1] = nil,
     [2] = nil,
     [3] = nil

+ 4 - 4
data/data/scripts/field.lua

@@ -110,17 +110,17 @@ end
 -- @param entity_name The entity name.
 -- @param character_id Id of the character to assign.
 set_entity_to_character = function(entity_name, character_id)
-    if character_id == Party[1] and entity_name ~= "" then
+    if character_id == Party.get_leader() and entity_name ~= "" then
         place_player_at_map_entry(entity_name)
     end
     entity_manager:set_entity_to_character(entity_name, character_id)
-    -- The model of a playable character is shown only if it is the leader of the party. The other
-    -- members are not drawn following the leader (join_party hides them too), and the scenes show
+    -- The model of a playable character is shown only if it is the leader of the party (the one the
+    -- scripts set, see Party.get_leader). The other members are not drawn following the leader (join_party hides them too), and the scenes show
     -- their models when they need them. Models are visible by default, so the ones of the
     -- characters that are not the leader, such as Cid in mds6_1 or Aerith in mrkt2 after the
     -- first scene, have to be hidden here. With no party at all (a new game that has not set it
     -- yet) nothing is hidden.
-    if character_id ~= Party[1] and entity_name ~= "" and Party[1] ~= nil then
+    if character_id ~= Party.get_leader() and entity_name ~= "" and Party.get_leader() ~= nil then
         local entity = entity_manager:get_entity(entity_name)
         if entity ~= nil then
             entity:set_visible(false)

+ 463 - 0
data/data/scripts/menu/exchange_menu.lua

@@ -0,0 +1,463 @@
+if UiContainer == nil then UiContainer = {} end
+
+--- The materia exchange menu (Materia > Order > Exchange).
+--
+-- The left window lists every character enabled in the PHS, with the slots of their weapon and
+-- armor. The right window is the materia of the inventory. The cursor goes through all the slots
+-- and all the positions of the inventory.
+--
+-- Confirm on a position with a materia picks it up: its cursor stays blinking while the real one
+-- moves. Confirm on another position swaps the two (the destination can be empty). Confirm on the
+-- picked position, or cancel, drops it. Cancel with nothing picked closes the menu.
+--
+-- A position is a table: {kind = "equip", char = index in `chars`, part = "weapon" or "armor",
+-- slot = 0-7} or {kind = "inv", index = slot of Materia}.
+UiContainer.ExchangeMenu = UiContainer.Menu.new{
+
+    id = "exchange",
+
+    --- Menu that opened this one.
+    parent = "materia",
+
+    --- Character blocks shown at once in the left window.
+    BLOCKS = 4,
+
+    --- Rows shown at once in the inventory window.
+    INV_ROWS = 14,
+
+    --- Position and size of the elements of the left window.
+    BLOCK_Y = 0,
+    BLOCK_HEIGHT = 58,
+    ROW_Y = {weapon = 22, armor = 39},
+    SLOT_X = 70,
+    SLOT_PITCH = 16,
+
+    --- Position of the rows of the inventory window.
+    INV_Y = 6,
+    INV_PITCH = 16,
+
+    --- IDs of the characters in the list (enabled in the PHS).
+    chars = {},
+
+    --- First character shown in the left window.
+    first = 1,
+
+    --- First inventory slot shown in the right window.
+    inv_first = 1,
+
+    --- Position of the cursor.
+    pos = nil,
+
+    --- Position of the picked materia, or nil.
+    held = nil,
+
+    --- Last position of the left window and of the inventory the cursor was on, to come back to.
+    last_equip = nil,
+    last_inv = 1,
+
+    states = {
+        [""] = {
+            confirm_sound = false,
+            cancel_sound = false,
+            confirm = function(self) return self:confirm() end,
+            cancel = function(self) return self:cancel() end,
+            buttons = {},
+        },
+    },
+
+    -----------------------------------------------------------------------------------------------
+    -- Opening and closing
+    -----------------------------------------------------------------------------------------------
+
+    --- Opens the menu.
+    show = function(self)
+        self.chars = {}
+        for char_id = 0, 8 do
+            local character = Characters[char_id]
+            if type(character) == "table" and character.enabled == true then
+                self.chars[#self.chars + 1] = char_id
+            end
+        end
+        self.first = 1
+        self.inv_first = 1
+        self.held = nil
+        self.last_inv = 1
+        self.last_equip = nil
+        local rows = self:rows()
+        if #rows > 0 then
+            self.pos = {kind = "equip", char = rows[1].char, part = rows[1].part, slot = 0}
+        else
+            self.pos = {kind = "inv", index = 1}
+        end
+        self:activate()
+        ui_manager:get_widget("MateriaMenu"):set_visible(false)
+        ui_manager:get_widget("ExchangeMenu"):set_visible(true)
+        self:refresh()
+        return 0
+    end,
+
+    --- Closes the menu and goes back to the materia menu.
+    hide = function(self)
+        ui_manager:get_widget("ExchangeMenu"):set_visible(false)
+        ui_manager:get_widget("MateriaMenu"):set_visible(true)
+        UiContainer.current_menu = "materia"
+        UiContainer.current_submenu = "order"
+        -- The materia menu shows the equipment, so it has to know what changed.
+        UiContainer.MateriaMenu:on_exchange_closed()
+        return 0
+    end,
+
+    -----------------------------------------------------------------------------------------------
+    -- Positions
+    -----------------------------------------------------------------------------------------------
+
+    --- Number of materia slots of the weapon or armor of a character of the list.
+    slot_count = function(self, char, part)
+        local equipment = Characters[self.chars[char]][part]
+        return #(Game.Items[equipment.id].slots)
+    end,
+
+    --- Rows of the left window that have slots, in order.
+    --
+    -- @return List of {char = index in `chars`, part = "weapon" or "armor", slots = count}.
+    rows = function(self)
+        local rows = {}
+        for char = 1, #self.chars do
+            for _, part in ipairs({"weapon", "armor"}) do
+                local count = self:slot_count(char, part)
+                if count > 0 then rows[#rows + 1] = {char = char, part = part, slots = count} end
+            end
+        end
+        return rows
+    end,
+
+    --- Gets the materia in a position (nil if empty).
+    get = function(self, pos)
+        if pos.kind == "inv" then return Materia[pos.index] end
+        return Characters[self.chars[pos.char]][pos.part].materia[pos.slot]
+    end,
+
+    --- Puts a materia (or nil) in a position.
+    set = function(self, pos, materia)
+        if pos.kind == "inv" then
+            Materia[pos.index] = materia
+        else
+            Characters[self.chars[pos.char]][pos.part].materia[pos.slot] = materia
+        end
+    end,
+
+    --- Are two positions the same?
+    same = function(self, a, b)
+        if a == nil or b == nil or a.kind ~= b.kind then return false end
+        if a.kind == "inv" then return a.index == b.index end
+        return a.char == b.char and a.part == b.part and a.slot == b.slot
+    end,
+
+    --- Moves the cursor up or down.
+    --
+    -- In the left window it goes through the rows that have slots (wrapping around), keeping the
+    -- column or going to the last slot of a shorter row. In the inventory it goes through all the
+    -- slots, without wrapping.
+    move_vertical = function(self, delta)
+        local pos = self.pos
+        if pos.kind == "inv" then
+            local index = pos.index + delta
+            if index < 1 or index > MAX_MATERIA_SLOTS then
+                UiContainer.play_sound("Error")
+                return
+            end
+            pos.index = index
+        else
+            local rows = self:rows()
+            local current = 1
+            for i, row in ipairs(rows) do
+                if row.char == pos.char and row.part == pos.part then current = i end
+            end
+            local target = rows[(current - 1 + delta) % #rows + 1]
+            pos.char, pos.part = target.char, target.part
+            pos.slot = math.min(pos.slot, target.slots - 1)
+        end
+        UiContainer.play_sound("Cursor")
+        self:refresh()
+    end,
+
+    --- Moves the cursor left or right.
+    --
+    -- Right from the last slot of a row goes to the inventory, and left from the inventory goes
+    -- back to the slot it came from.
+    move_horizontal = function(self, delta)
+        local pos = self.pos
+        if pos.kind == "inv" then
+            if delta > 0 then
+                UiContainer.play_sound("Error")
+                return
+            end
+            local rows = self:rows()
+            if #rows == 0 then
+                UiContainer.play_sound("Error")
+                return
+            end
+            self.last_inv = pos.index
+            local back = self.last_equip or {char = rows[1].char, part = rows[1].part, slot = 0}
+            self.pos = {kind = "equip", char = back.char, part = back.part, slot = back.slot}
+        else
+            local slot = pos.slot + delta
+            if slot < 0 then
+                UiContainer.play_sound("Error")
+                return
+            elseif slot >= self:slot_count(pos.char, pos.part) then
+                self.last_equip = {char = pos.char, part = pos.part, slot = pos.slot}
+                self.pos = {kind = "inv", index = self.last_inv}
+            else
+                pos.slot = slot
+            end
+        end
+        UiContainer.play_sound("Cursor")
+        self:refresh()
+    end,
+
+    --- Moves the cursor in the inventory by a page.
+    move_page = function(self, delta)
+        if self.pos.kind ~= "inv" then return end
+        local index = math.max(1, math.min(MAX_MATERIA_SLOTS, self.pos.index + delta * self.INV_ROWS))
+        if index == self.pos.index then
+            UiContainer.play_sound("Error")
+            return
+        end
+        UiContainer.play_sound("Cursor")
+        self.pos.index = index
+        self:refresh()
+    end,
+
+    -----------------------------------------------------------------------------------------------
+    -- Actions
+    -----------------------------------------------------------------------------------------------
+
+    --- Confirm: picks up the materia of the cursor, or swaps it with the picked one.
+    confirm = function(self)
+        if self.held == nil then
+            if self:get(self.pos) == nil then return false end
+            UiContainer.play_sound("Cursor")
+            self.held = {}
+            for key, value in pairs(self.pos) do self.held[key] = value end
+            self:refresh()
+            return
+        end
+        if self:same(self.held, self.pos) then
+            UiContainer.play_sound("Back")
+            self.held = nil
+            self:refresh()
+            return
+        end
+        local from, to = self.held, self.pos
+        local moved, replaced = self:get(from), self:get(to)
+        self:set(from, replaced)
+        self:set(to, moved)
+        self.held = nil
+        -- The materia change the stats of the characters involved, and the current HP and MP can't
+        -- be over the new maximums.
+        local recalculated = {}
+        for _, pos in ipairs({from, to}) do
+            if pos.kind == "equip" and not recalculated[pos.char] then
+                recalculated[pos.char] = true
+                Characters.recalculate_stats(self.chars[pos.char])
+            end
+        end
+        UiContainer.play_sound("Cursor")
+        self:refresh()
+    end,
+
+    --- Cancel: drops the picked materia, or closes the menu.
+    cancel = function(self)
+        if self.held ~= nil then
+            self.held = nil
+            self:refresh()
+            UiContainer.play_sound("Back")
+            return
+        end
+        UiContainer.play_sound("Back")
+        script:request_end_sync(Script.UI, "ExchangeMenu", "hide", 0)
+    end,
+
+    -----------------------------------------------------------------------------------------------
+    -- Widgets
+    -----------------------------------------------------------------------------------------------
+
+    --- Scrolls the windows so that the cursor is in them, and redraws everything.
+    refresh = function(self)
+        local pos = self.pos
+        if pos.kind == "equip" then
+            if pos.char < self.first then
+                self.first = pos.char
+            elseif pos.char > self.first + self.BLOCKS - 1 then
+                self.first = pos.char - self.BLOCKS + 1
+            end
+        else
+            if pos.index < self.inv_first then
+                self.inv_first = pos.index
+            elseif pos.index > self.inv_first + self.INV_ROWS - 1 then
+                self.inv_first = pos.index - self.INV_ROWS + 1
+            end
+        end
+        self:populate_equip()
+        self:populate_inventory()
+        self:place_cursors()
+        local materia = self:get(pos)
+        self:populate_info(materia)
+        ui_manager:get_widget("ExchangeMenu.Container.Help.Help"):set_text(
+            materia ~= nil and Game.Materia[materia.id].description or "")
+    end,
+
+    --- Fills the top bar with the icon, name and stars of a materia (nothing if it is nil).
+    populate_info = function(self, materia)
+        local bar = "ExchangeMenu.Container.Info."
+        ui_manager:get_widget(bar .. "Icon"):set_visible(materia ~= nil)
+        ui_manager:get_widget(bar .. "Name"):set_visible(materia ~= nil)
+        local data = materia ~= nil and Game.Materia[materia.id] or nil
+        local levels = data ~= nil and data.levels_ap or {}
+        local level = 0
+        for _, level_ap in ipairs(levels) do
+            if level_ap > (materia.ap or 0) then break end
+            level = level + 1
+        end
+        if data ~= nil then
+            ui_manager:get_widget(bar .. "Icon"):set_image("icons/materia_" .. tostring(data.type) .. ".png")
+            ui_manager:get_widget(bar .. "Name"):set_text(data.name)
+        end
+        for star = 1, 5 do
+            local widget = ui_manager:get_widget(bar .. "Stars.Star" .. star)
+            if data == nil or star > #levels then
+                widget:set_visible(false)
+            else
+                widget:set_image((star <= level and "icons/materia_star_" or "icons/materia_star_empty_")
+                    .. tostring(data.type) .. ".png")
+                widget:set_visible(true)
+            end
+        end
+    end,
+
+    --- Fills the blocks of the left window.
+    populate_equip = function(self)
+        local window = "ExchangeMenu.Container.Characters."
+        for b = 1, self.BLOCKS do
+            local block = window .. "Block" .. b
+            local char_id = self.chars[self.first + b - 1]
+            ui_manager:get_widget(block):set_visible(char_id ~= nil)
+            if char_id ~= nil then
+                local character = Characters[char_id]
+                ui_manager:get_widget(block .. ".Portrait"):set_image("characters/" .. tostring(char_id) .. ".png")
+                ui_manager:get_widget(block .. ".Name"):set_text(character.name)
+                ui_manager:get_widget(block .. ".WIcon"):set_image("icons/item_weapon_" .. tostring(char_id) .. ".png")
+                self:populate_slots(block, "W", character.weapon)
+                self:populate_slots(block, "A", character.armor)
+            end
+        end
+        -- Scroll bar.
+        local scroll = ui_manager:get_widget(window .. "Scroll")
+        local hidden = math.max(0, #self.chars - self.BLOCKS)
+        local fraction = hidden > 0 and (self.first - 1) / hidden or 0
+        scroll:set_y(fraction * 100, 3 - fraction * 24)
+        scroll:set_visible(hidden > 0)
+        ui_manager:get_widget(window .. "ScrollBg"):set_visible(hidden > 0)
+    end,
+
+    --- Fills the slots of the weapon or armor of a block.
+    --
+    -- @param block Path of the block.
+    -- @param prefix "W" or "A".
+    -- @param equipment Weapon or armor of the character (an entry of Characters).
+    populate_slots = function(self, block, prefix, equipment)
+        local slots = Game.Items[equipment.id].slots
+        for s = 1, 8 do
+            local slot = ui_manager:get_widget(block .. "." .. prefix .. "Slot" .. s)
+            local mat = ui_manager:get_widget(block .. "." .. prefix .. "Mat" .. s)
+            slot:set_visible(#slots >= s)
+            local materia = equipment.materia[s - 1]
+            if #slots >= s and materia ~= nil then
+                mat:set_image("icons/materia_" .. tostring(Game.Materia[materia.id].type) .. ".png")
+                mat:set_visible(true)
+            else
+                mat:set_visible(false)
+            end
+            -- A link joins an odd slot with the next one.
+            if s % 2 ~= 0 then
+                ui_manager:get_widget(block .. "." .. prefix .. "Link" .. (math.floor(s / 2) + 1)):set_visible(
+                    #slots > s and slots[s] == 1)
+            end
+        end
+    end,
+
+    --- Fills the rows of the inventory window.
+    populate_inventory = function(self)
+        local window = "ExchangeMenu.Container.Inventory."
+        for r = 1, self.INV_ROWS do
+            local materia = Materia[self.inv_first + r - 1]
+            local icon = ui_manager:get_widget(window .. "Icon" .. r)
+            local name = ui_manager:get_widget(window .. "Name" .. r)
+            if materia ~= nil then
+                local data = Game.Materia[materia.id]
+                icon:set_image("icons/materia_" .. tostring(data.type) .. ".png")
+                icon:set_visible(true)
+                name:set_text(data.name)
+                name:set_visible(true)
+            else
+                icon:set_visible(false)
+                name:set_visible(false)
+            end
+        end
+        local scroll = ui_manager:get_widget(window .. "Scroll")
+        local fraction = (self.inv_first - 1) / (MAX_MATERIA_SLOTS - self.INV_ROWS)
+        scroll:set_y(fraction * 100, 3 - fraction * 24)
+    end,
+
+    --- Coordinates of a position inside its window, and the window.
+    --
+    -- @return Window ("Characters" or "Inventory"), x, y, or nil if the position is not visible.
+    cursor_place = function(self, pos)
+        if pos.kind == "inv" then
+            local row = pos.index - self.inv_first
+            if row < 0 or row >= self.INV_ROWS then return nil end
+            return "Inventory", -8, self.INV_Y + self.INV_PITCH * row - 1
+        end
+        local block = pos.char - self.first
+        if block < 0 or block >= self.BLOCKS then return nil end
+        return "Characters", self.SLOT_X + self.SLOT_PITCH * pos.slot - 22,
+            self.BLOCK_Y + self.BLOCK_HEIGHT * block + self.ROW_Y[pos.part] - 1
+    end,
+
+    --- Places the cursor and the blinking cursor of the picked materia.
+    place_cursors = function(self)
+        local base = "ExchangeMenu.Container."
+        for _, window in ipairs({"Characters", "Inventory"}) do
+            ui_manager:get_widget(base .. window .. ".Cursor"):set_visible(false)
+            ui_manager:get_widget(base .. window .. ".Held"):set_visible(false)
+        end
+        local function place(pos, name)
+            if pos == nil then return end
+            local window, x, y = self:cursor_place(pos)
+            if window == nil then return end
+            local cursor = ui_manager:get_widget(base .. window .. "." .. name)
+            cursor:set_x(0, x)
+            cursor:set_y(0, y)
+            cursor:set_visible(true)
+            if name == "Held" then cursor:set_default_animation("Blink") end
+        end
+        place(self.pos, "Cursor")
+        place(self.held, "Held")
+    end,
+}
+
+--- Directional buttons and pages.
+local function direction(handler)
+    return function(self, event)
+        if event == "Press" or event == "Repeat" then handler(self) end
+    end
+end
+
+local buttons = UiContainer.ExchangeMenu.states[""].buttons
+buttons[Config.CONTROLS.UP] = direction(function(self) self:move_vertical(-1) end)
+buttons[Config.CONTROLS.DOWN] = direction(function(self) self:move_vertical(1) end)
+buttons[Config.CONTROLS.LEFT] = direction(function(self) self:move_horizontal(-1) end)
+buttons[Config.CONTROLS.RIGHT] = direction(function(self) self:move_horizontal(1) end)
+buttons[Config.ACTIONS.L1] = direction(function(self) self:move_page(-1) end)
+buttons[Config.ACTIONS.R1] = direction(function(self) self:move_page(1) end)

+ 82 - 4
data/data/scripts/menu/item_menu.lua

@@ -71,6 +71,12 @@ UiContainer.ItemMenu = {
     -- Slots in the party inventory
     inventory_size = 320,
 
+    --- True while the items are being ordered by hand (Order > Custom).
+    custom_mode = false,
+
+    --- Inventory position of the item picked in the custom order, or nil.
+    custom_held = nil,
+
     --- Run when the menu is creaded.
     --
     -- It does nothing.
@@ -151,7 +157,9 @@ UiContainer.ItemMenu = {
                 elseif button == Config.ACTIONS.CONFIRM then
                     audio_manager:play_sound("Cursor")
                     if self.order_cursor_position == 1 then
-                        Inventory.sort(Inventory.ORDER.CUSTOM)
+                        -- Custom: the items are moved by hand in the list.
+                        self.submenu_custom(self)
+                        return 0
                     elseif self.order_cursor_position == 2 then
                         Inventory.sort(Inventory.ORDER.FIELD)
                     elseif self.order_cursor_position == 3 then
@@ -233,9 +241,21 @@ UiContainer.ItemMenu = {
                     end
                 elseif button == Config.ACTIONS.CANCEL and event == "Press" then
                     audio_manager:play_sound("Back")
-                    self.submenu_bar(self)
-                    UiContainer.current_submenu = "bar_selection"
-                    ui_manager:get_widget("ItemMenu.Container.ItemList.Cursor"):set_visible(false)
+                    if self.custom_mode then
+                        if self.custom_held ~= nil then
+                            -- Drop the picked item.
+                            self.custom_held = nil
+                        else
+                            -- Back to the order options.
+                            self.submenu_order(self)
+                        end
+                    else
+                        self.submenu_bar(self)
+                        UiContainer.current_submenu = "bar_selection"
+                        ui_manager:get_widget("ItemMenu.Container.ItemList.Cursor"):set_visible(false)
+                    end
+                elseif button == Config.ACTIONS.CONFIRM and event == "Press" and self.custom_mode then
+                    self.custom_confirm(self)
                 elseif button == Config.ACTIONS.CONFIRM then
                     local selected = Inventory[self.item_cursor_item_selected]
                     if selected ~= nil and Game.Items[selected.item].menu == 1 then
@@ -436,6 +456,8 @@ UiContainer.ItemMenu = {
                 end
 
             end
+            -- The blinking cursor of the custom order follows the list when it scrolls.
+            if self.custom_mode then self.update_held(self) end
         end
         return 0
     end,
@@ -465,6 +487,9 @@ UiContainer.ItemMenu = {
     --- Initializes the selection of the menu section in the top bar.
     submenu_bar = function(self)
         UiContainer.current_submenu = "bar_selection"
+        self.custom_mode = false
+        self.custom_held = nil
+        self.update_held(self)
         ui_manager:get_widget("ItemMenu.Container.ItemList.Cursor"):set_visible(false)
         ui_manager:get_widget("ItemMenu.Container.ItemMenuCharacters.Cursor"):set_visible(false)
         ui_manager:get_widget("ItemMenu.Container.ItemMenuCharacters.CursorMultiple"):set_visible(false)
@@ -479,6 +504,9 @@ UiContainer.ItemMenu = {
     --- Initializes the sorting mode window.
     submenu_order = function(self)
         UiContainer.current_submenu = "order_selection"
+        self.custom_mode = false
+        self.custom_held = nil
+        self.update_held(self)
         self.order_cursor_position = 1
         ui_manager:get_widget("ItemMenu.Container.ItemList.Cursor"):set_visible(false)
         ui_manager:get_widget("ItemMenu.Container.ItemMenuCharacters.Cursor"):set_visible(false)
@@ -491,6 +519,56 @@ UiContainer.ItemMenu = {
         ui_manager:get_widget("ItemMenu.Container.ItemOrder.Cursor"):set_default_animation("Position" .. self.order_cursor_position)
     end,
 
+    --- Starts the custom order: the cursor goes over the list of items, and they can be swapped.
+    submenu_custom = function(self)
+        self.custom_mode = true
+        self.custom_held = nil
+        ui_manager:get_widget("ItemMenu.Container.ItemOrder"):set_visible(false)
+        ui_manager:get_widget("ItemMenu.Container.ItemOrder.Cursor"):set_visible(false)
+        self.submenu_items(self)
+    end,
+
+    --- Confirm in the custom order: picks the highlighted item, or swaps it with the picked one.
+    --
+    -- Items can only be swapped with other items, so the list never gets empty places in the
+    -- middle.
+    custom_confirm = function(self)
+        local index = self.item_cursor_item_selected
+        if self.custom_held == nil then
+            if Inventory[index] == nil then
+                audio_manager:play_sound("Error")
+                return
+            end
+            audio_manager:play_sound("Cursor")
+            self.custom_held = index
+        elseif self.custom_held == index then
+            audio_manager:play_sound("Back")
+            self.custom_held = nil
+        elseif Inventory[index] == nil then
+            audio_manager:play_sound("Error")
+        else
+            audio_manager:play_sound("Cursor")
+            Inventory[self.custom_held], Inventory[index] = Inventory[index], Inventory[self.custom_held]
+            self.custom_held = nil
+            self.populate_items(self)
+            self.show_item_description(self)
+        end
+    end,
+
+    --- Places the blinking cursor of the item picked in the custom order, or hides it.
+    update_held = function(self)
+        local held = ui_manager:get_widget("ItemMenu.Container.ItemList.Held")
+        local row = nil
+        if self.custom_held ~= nil then row = self.custom_held - self.first_item_in_window end
+        if row == nil or row < 1 or row > self.item_cursor_position_total then
+            held:set_visible(false)
+            return
+        end
+        held:set_visible(true)
+        held:set_default_animation("Blink" .. tostring(row))
+        held:play_animation("Blink" .. tostring(row))
+    end,
+
     --- Shows the description of the highlighted inventory item, or clears it if the slot is empty.
     show_item_description = function(self)
         local label = ui_manager:get_widget("ItemMenu.Container.ItemMenuSecondBar.ItemText")

+ 53 - 4
data/data/scripts/menu/main_menu.lua

@@ -13,6 +13,7 @@ local BAR_WIDTH = 62
 local STAY = "stay"
 
 local CHARACTERS_CURSOR = "MainMenu.Container.Characters.Cursor"
+local CHARACTERS_HELD = "MainMenu.Container.Characters.Held"
 
 --- The main menu.
 UiContainer.MainMenu = UiContainer.Menu.new{
@@ -78,11 +79,12 @@ UiContainer.MainMenu = UiContainer.Menu.new{
             UiContainer.current_character_id = Party[slot]
             script:request_end_sync(Script.UI, "StatusMenu", "show", 0)
         end,
-        --- Toggles the row (front/back) of the character. Stays in the character selection.
+        --- Picks the character to change its row or swap it with another party slot.
+        -- A blinking cursor stays on it while the cursor moves (see the order_swap state).
         [ENTRY.ORDER] = function(self, slot)
-            local character = Characters[Party[slot]]
-            character.back_row = not character.back_row
-            self:update_portrait(slot)
+            self.order_held = slot
+            self:set_order_held(slot)
+            self:set_state("order_swap")
             return STAY
         end,
         [ENTRY.LIMIT] = function(self, slot)
@@ -91,6 +93,9 @@ UiContainer.MainMenu = UiContainer.Menu.new{
         end,
     },
 
+    --- Party slot of the character picked with Order.
+    order_held = 0,
+
     states = {
         --- Choosing an entry of the menu.
         [""] = {
@@ -137,8 +142,52 @@ UiContainer.MainMenu = UiContainer.Menu.new{
                 ui_manager:get_widget(CHARACTERS_CURSOR):set_visible(false)
             end,
         },
+
+        --- Order: a character is picked (blinking cursor) and the cursor moves over the three
+        -- party slots, empty ones included. Confirm on the picked character toggles its row;
+        -- confirm on another slot swaps the two.
+        order_swap = {
+            cursor = function(self) return self.character_cursor end,
+            axis = "vertical",
+            confirm_sound = false,
+            cancel = function(self)
+                self:set_order_held(nil)
+                self:set_state("main_character")
+            end,
+            confirm = function(self, cursor)
+                local held, slot = self.order_held, cursor:index()
+                if slot == held then
+                    local character = Characters[Party[held]]
+                    character.back_row = not character.back_row
+                    self:update_portrait(held)
+                    UiContainer.play_sound("Cursor")
+                else
+                    -- The first slot can't be left empty.
+                    if Party[slot] == nil and held == 1 then return false end
+                    Party[held], Party[slot] = Party[slot], Party[held]
+                    self:populate_character(held)
+                    self:populate_character(slot)
+                    UiContainer.play_sound("Cursor")
+                end
+                self:set_order_held(nil)
+                self:set_state("main_character")
+            end,
+        },
     },
 
+    --- Shows (or hides, with nil) the blinking cursor that marks the character picked with Order.
+    set_order_held = function(self, slot)
+        local held = ui_manager:get_widget(CHARACTERS_HELD)
+        held:set_visible(slot ~= nil)
+        if slot ~= nil then
+            local animation = "Blink" .. tostring(slot)
+            held:set_default_animation(animation)
+            held:play_animation(animation)
+        else
+            held:set_alpha(1.0)
+        end
+    end,
+
     --- Handles the menu button while the main menu is closed.
     on_inactive_button = function(self, button, event)
         if ui_manager:get_widget("MainMenu"):is_visible() == false

+ 408 - 7
data/data/scripts/menu/materia_menu.lua

@@ -30,6 +30,49 @@ UiContainer.MateriaMenu = {
     --- Max materias in the list
     list_size = 200,
 
+    --- Commands of the character, shown in the Check window.
+    commands = {},
+
+    --- Cursor over the commands of the Check window (columns of four, filled top to bottom).
+    command_cursor = UiContainer.Cursor.new{
+        widget = "MateriaMenu.Container.Details.Commands.Box.Cursor",
+        total = 1,
+        on_move = function() UiContainer.MateriaMenu:populate_command_help() end,
+    },
+
+    --- Cursor over the options of the Order menu.
+    order_cursor = UiContainer.Cursor.new{
+        widget = "MateriaMenu.Container.OrderMenu.Cursor",
+        total = 4,
+        wrap = true,
+        on_move = function() UiContainer.MateriaMenu:populate_order_help() end,
+    },
+
+    --- Descriptions of the options of the Order menu.
+    ORDER_HELP = {
+        "Sort the materia in the inventory",
+        "Exchange materia between the characters and the inventory",
+        "Remove all the materia equipped on the character",
+        "Trash materia",
+    },
+
+    --- Options of the Order menu that are not available yet.
+    ORDER_DISABLED = {},
+
+    --- True while the materia list is used to choose a materia to trash (Order > Trash).
+    trash_mode = false,
+
+    --- Entries of the list of spells, summons or enemy skills of a command.
+    abilities = {},
+
+    --- Cursor over the abilities list.
+    ability_cursor = UiContainer.ListCursor.new{
+        widget = "MateriaMenu.Container.Abilities.Cursor",
+        rows = 10,
+        on_scroll = function() UiContainer.MateriaMenu:populate_abilities() end,
+        on_move = function() UiContainer.MateriaMenu:populate_ability_help() end,
+    },
+
     --- Run when the menu is creaded.
     --
     -- It does nothing.
@@ -85,9 +128,15 @@ UiContainer.MateriaMenu = {
                     self.populate_details(self, false)
                 elseif button == Config.ACTIONS.CONFIRM then
                     if self.header_position_row == 1 and self.header_position == 1 then
-                        -- TODO: Command menu
+                        if #self.commands == 0 then
+                            audio_manager:play_sound("Error")
+                        else
+                            audio_manager:play_sound("Cursor")
+                            self.submenu_commands(self)
+                        end
                     elseif self.header_position_row == 2 and self.header_position == 1 then
-                        -- TODO: Order menu
+                        audio_manager:play_sound("Cursor")
+                        self.submenu_order(self)
                     else
                         -- TODO: Check for  avalid slot before entering list
                         if self.header_position_row == 1 and #(Game.Items[Characters[self.char_id].weapon.id].slots) > self.header_position - 2 then
@@ -104,6 +153,31 @@ UiContainer.MateriaMenu = {
                 else
                     return 0
                 end
+            elseif UiContainer.current_submenu == "order" then
+                if button == Config.ACTIONS.CANCEL and event == "Press" then
+                    audio_manager:play_sound("Back")
+                    self.submenu_none(self)
+                elseif button == Config.ACTIONS.CONFIRM and event == "Press" then
+                    self.run_order(self)
+                elseif event == "Press" or event == "Repeat" then
+                    self.order_cursor:handle(button, "vertical")
+                end
+            elseif UiContainer.current_submenu == "commands" then
+                if button == Config.ACTIONS.CANCEL and event == "Press" then
+                    audio_manager:play_sound("Back")
+                    self.submenu_none(self)
+                elseif button == Config.ACTIONS.CONFIRM and event == "Press" then
+                    self.open_command(self)
+                elseif event == "Press" or event == "Repeat" then
+                    self:move_command_cursor(button)
+                end
+            elseif UiContainer.current_submenu == "abilities" then
+                if button == Config.ACTIONS.CANCEL and event == "Press" then
+                    audio_manager:play_sound("Back")
+                    self.submenu_commands(self)
+                elseif event == "Press" or event == "Repeat" then
+                    self.ability_cursor:handle(button, "vertical")
+                end
             elseif UiContainer.current_submenu == "list" then
                 if button == Config.CONTROLS.DOWN then
                     if self.list_first_visible + self.list_position > self.list_size then
@@ -161,11 +235,27 @@ UiContainer.MateriaMenu = {
                     end
                 elseif button == Config.ACTIONS.CANCEL and event == "Press" then
                     audio_manager:play_sound("Back")
-                    self.submenu_none(self)
+                    if self.trash_mode then
+                        self.submenu_order(self)
+                        self.order_cursor:set(4)
+                    else
+                        self.submenu_none(self)
+                    end
                 elseif button == Config.ACTIONS.CONFIRM then
-                    -- TODO: Replace for "Equip materia" sound.
-                    audio_manager:play_sound("Cursor")
-                    self.equip_materia(self)
+                    if self.trash_mode then
+                        self.submenu_trash_confirm(self)
+                    else
+                        -- TODO: Replace for "Equip materia" sound.
+                        audio_manager:play_sound("Cursor")
+                        self.equip_materia(self)
+                    end
+                end
+            elseif UiContainer.current_submenu == "trash_confirm" then
+                if button == Config.ACTIONS.CANCEL and event == "Press" then
+                    audio_manager:play_sound("Back")
+                    self.submenu_trash(self)
+                elseif button == Config.ACTIONS.CONFIRM and event == "Press" then
+                    self.trash_materia(self)
                 end
             end
         end
@@ -193,17 +283,309 @@ UiContainer.MateriaMenu = {
     --- Enters or returns to the main equip menu.
     submenu_none = function(self)
         UiContainer.current_submenu = ""
+        self.trash_mode = false
+        ui_manager:get_widget("MateriaMenu.Container.TrashWindow"):set_visible(false)
         UiContainer.populate_character_data("MateriaMenu.Container.Character", Characters[self.char_id])
         ui_manager:get_widget("MateriaMenu.Container.Character.Portrait"):set_image("characters/" .. tostring(self.char_id) .. ".png")
         self.populate_equip(self)
         -- TODO: Do something for chars 9 and 10
         ui_manager:get_widget("MateriaMenu.Container.Character.WpnIcon"):set_image("icons/item_weapon_" .. tostring(self.char_id) .. ".png")
         ui_manager:get_widget("MateriaMenu.Container.List.Cursor"):set_visible(false)
+        ui_manager:get_widget("MateriaMenu.Container.Abilities"):set_visible(false)
+        ui_manager:get_widget("MateriaMenu.Container.OrderMenu"):set_visible(false)
+        ui_manager:get_widget("MateriaMenu.Container.Details.Commands.Box.Cursor"):set_visible(false)
         --TODO: Uncomment when implemented: ui_manager:get_widget("MateriaMenu.Container.Order.Cursor"):set_visible(false)
         ui_manager:get_widget("MateriaMenu.Container.Character.Cursor"):set_visible(true)
         self.populate_details(self, false)
     end,
 
+    --- Refreshes the menu after the exchange menu closed (the equipment and stats may have changed).
+    on_exchange_closed = function(self)
+        UiContainer.populate_character_data("MateriaMenu.Container.Character", Characters[self.char_id])
+        self.populate_equip(self)
+        self.populate_list(self)
+        self.populate_order_help(self)
+    end,
+
+    --- Opens the Order menu (Arrange, Exchange, Remove all, Trash).
+    submenu_order = function(self)
+        UiContainer.current_submenu = "order"
+        self.trash_mode = false
+        ui_manager:get_widget("MateriaMenu.Container.TrashWindow"):set_visible(false)
+        ui_manager:get_widget("MateriaMenu.Container.List.Cursor"):set_visible(false)
+        ui_manager:get_widget("MateriaMenu.Container.OrderMenu"):set_visible(true)
+        for option = 1, 4 do
+            local item = ui_manager:get_widget("MateriaMenu.Container.OrderMenu.Item" .. option)
+            if self.ORDER_DISABLED[option] then item:set_colour(0.6, 0.6, 0.6) else item:set_colour(1, 1, 1) end
+        end
+        self.order_cursor:set(1)
+    end,
+
+    --- Enters (or returns to) the materia list to choose the materia to trash.
+    submenu_trash = function(self)
+        self.trash_mode = true
+        -- The Order popup stays open (with Trash highlighted) while choosing.
+        ui_manager:get_widget("MateriaMenu.Container.TrashWindow"):set_visible(false)
+        self.submenu_list(self)
+    end,
+
+    --- Opens the window that shows the highlighted materia before it is trashed.
+    submenu_trash_confirm = function(self)
+        local materia = Materia[self.list_first_visible + self.list_position - 1]
+        if materia == nil then
+            audio_manager:play_sound("Error")
+            return
+        end
+        audio_manager:play_sound("Cursor")
+        UiContainer.current_submenu = "trash_confirm"
+        local data = Game.Materia[materia.id]
+        local window = "MateriaMenu.Container.TrashWindow."
+        local levels = data.levels_ap or {}
+        local level = 0
+        for _, level_ap in ipairs(levels) do
+            if level_ap > (materia.ap or 0) then break end
+            level = level + 1
+        end
+        ui_manager:get_widget(window .. "Icon"):set_image("icons/materia_" .. tostring(data.type) .. ".png")
+        ui_manager:get_widget(window .. "Name"):set_text(data.name)
+        for star = 1, 5 do
+            local widget = ui_manager:get_widget(window .. "Star" .. star)
+            if star > #levels then
+                widget:set_visible(false)
+            else
+                widget:set_image((star <= level and "icons/materia_star_" or "icons/materia_star_empty_")
+                    .. tostring(data.type) .. ".png")
+                widget:set_visible(true)
+            end
+        end
+        ui_manager:get_widget(window .. "AP"):set_text(UiContainer.pad_value(materia.ap or 0, 8))
+        ui_manager:get_widget("MateriaMenu.Container.TrashWindow"):set_visible(true)
+    end,
+
+    --- Discards the materia shown in the trash window and goes back to the list.
+    trash_materia = function(self)
+        Materia[self.list_first_visible + self.list_position - 1] = nil
+        audio_manager:play_sound("Cursor")
+        self.populate_list(self)
+        self.submenu_trash(self)
+    end,
+
+    --- Shows the description of the highlighted option of the Order menu.
+    populate_order_help = function(self)
+        ui_manager:get_widget("MateriaMenu.Container.Help.Help"):set_text(self.ORDER_HELP[self.order_cursor:index()] or "")
+    end,
+
+    --- Runs the highlighted option of the Order menu.
+    run_order = function(self)
+        local option = self.order_cursor:index()
+        if self.ORDER_DISABLED[option] then
+            audio_manager:play_sound("Error")
+        elseif option == 1 then
+            audio_manager:play_sound("Cursor")
+            self.arrange_materia(self)
+        elseif option == 2 then
+            audio_manager:play_sound("Cursor")
+            script:request_end_sync(Script.UI, "ExchangeMenu", "show", 0)
+        elseif option == 4 then
+            audio_manager:play_sound("Cursor")
+            self.submenu_trash(self)
+        elseif option == 3 then
+            if self.remove_all_materia(self) then
+                audio_manager:play_sound("Cursor")
+            else
+                audio_manager:play_sound("Error")
+            end
+        end
+    end,
+
+    --- IDs of the master materia (Master Command, Master Magic and Master Summon).
+    MASTER_MATERIA = {[48] = true, [73] = true, [90] = true},
+
+    --- Sorts the materia of the inventory, as the original game does.
+    --
+    -- By type (the order of Materia.TYPE: magic, support, command, independent, summon); in each
+    -- type the master materia go first, then by ID ascending and by AP descending. The empty slots
+    -- go at the end.
+    arrange_materia = function(self)
+        local sorted = {}
+        for slot = 1, MAX_MATERIA_SLOTS do
+            if Materia[slot] ~= nil then sorted[#sorted + 1] = {materia = Materia[slot], order = #sorted} end
+        end
+        local master = self.MASTER_MATERIA
+        table.sort(sorted, function(l, r)
+            local l_id, r_id = l.materia.id, r.materia.id
+            local l_type, r_type = Game.Materia[l_id].type, Game.Materia[r_id].type
+            if l_type ~= r_type then return l_type < r_type end
+            if (master[l_id] == true) ~= (master[r_id] == true) then return master[l_id] == true end
+            if l_id ~= r_id then return l_id < r_id end
+            if (l.materia.ap or 0) ~= (r.materia.ap or 0) then return (l.materia.ap or 0) > (r.materia.ap or 0) end
+            return l.order < r.order
+        end)
+        for slot = 1, MAX_MATERIA_SLOTS do
+            Materia[slot] = sorted[slot] ~= nil and sorted[slot].materia or nil
+        end
+        self.list_first_visible = 1
+        self.list_position = 1
+        ui_manager:get_widget("MateriaMenu.Container.List.Cursor"):set_default_animation("Position1")
+        self.populate_list(self)
+    end,
+
+    --- Moves all the materia equipped on the character to the inventory.
+    --
+    -- If the inventory has not room for all of it, as many as fit are removed (the ones of the weapon
+    -- first, in slot order).
+    -- @return False if nothing was removed.
+    remove_all_materia = function(self)
+        local equipped = {}
+        local character = Characters[self.char_id]
+        for _, equipment in ipairs({character.weapon, character.armor}) do
+            for slot = 0, 7 do
+                if equipment.materia[slot] ~= nil then equipped[#equipped + 1] = {equipment, slot} end
+            end
+        end
+        local free = {}
+        for slot = 1, MAX_MATERIA_SLOTS do
+            if Materia[slot] == nil then free[#free + 1] = slot end
+        end
+        local count = math.min(#equipped, #free)
+        if count == 0 then return false end
+        for i = 1, count do
+            local place = equipped[i]
+            Materia[free[i]] = place[1].materia[place[2]]
+            place[1].materia[place[2]] = nil
+        end
+        -- The materia change the stats, and the current HP and MP can't be over the new maximums.
+        Characters.recalculate_stats(self.char_id)
+        self.submenu_none(self)
+        self.populate_list(self)
+        return true
+    end,
+
+    --- Enters (or returns to) the navigable list of commands of the Check window.
+    submenu_commands = function(self)
+        UiContainer.current_submenu = "commands"
+        ui_manager:get_widget("MateriaMenu.Container.Abilities"):set_visible(false)
+        ui_manager:get_widget("MateriaMenu.Container.Details.Commands.Box.Cursor"):set_visible(true)
+        self.command_cursor.total = #self.commands
+        if self.command_cursor.position > #self.commands then self.command_cursor.position = 1 end
+        self.command_cursor:refresh()
+    end,
+
+    --- Number of commands in each column of the Check window.
+    COMMAND_ROWS = 4,
+
+    --- Moves the command cursor. The commands fill the columns from top to bottom.
+    --
+    -- Up and down wrap inside the column, left and right wrap around the columns keeping the row
+    -- (or going to the last command of a shorter column).
+    move_command_cursor = function(self, button)
+        local rows = self.COMMAND_ROWS
+        local index = self.command_cursor:index() - 1
+        local column, row = math.floor(index / rows), index % rows
+        local columns = math.ceil(#self.commands / rows)
+        local function length(c) return math.min(rows, #self.commands - c * rows) end
+        if button == Config.CONTROLS.UP then
+            row = (row - 1) % length(column)
+        elseif button == Config.CONTROLS.DOWN then
+            row = (row + 1) % length(column)
+        elseif button == Config.CONTROLS.LEFT then
+            column = (column - 1) % columns
+            row = math.min(row, length(column) - 1)
+        elseif button == Config.CONTROLS.RIGHT then
+            column = (column + 1) % columns
+            row = math.min(row, length(column) - 1)
+        else
+            return
+        end
+        audio_manager:play_sound("Cursor")
+        self.command_cursor:set(column * rows + row + 1)
+    end,
+
+    --- Shows the description of the highlighted command.
+    populate_command_help = function(self)
+        local command = Game.Commands[self.commands[self.command_cursor:index()]]
+        ui_manager:get_widget("MateriaMenu.Container.Help.Help"):set_text(command ~= nil and command.description or "")
+    end,
+
+    --- Gets the kind of list a command opens.
+    --
+    -- @param command Definition of the command (an entry of Game.Commands).
+    -- @return "magic", "summon" or "eskill", or nil if the command has no list.
+    command_list_kind = function(self, command)
+        local menu = command ~= nil and command.menu or nil
+        if menu == Battle.Menu.MAGIC or menu == Battle.Menu.W_MAGIC then return "magic"
+        elseif menu == Battle.Menu.SUMMON or menu == Battle.Menu.W_SUMMON then return "summon"
+        elseif menu == Battle.Menu.E_SKILL then return "eskill"
+        end
+        return nil
+    end,
+
+    --- Opens the list of a command (spells, summons or enemy skills), if it has one.
+    open_command = function(self)
+        local command = Game.Commands[self.commands[self.command_cursor:index()]]
+        local kind = self:command_list_kind(command)
+        local entries = kind ~= nil and self:build_abilities(kind) or {}
+        if #entries == 0 then
+            audio_manager:play_sound("Error")
+            return
+        end
+        audio_manager:play_sound("Cursor")
+        self.abilities = entries
+        UiContainer.current_submenu = "abilities"
+        ui_manager:get_widget("MateriaMenu.Container.Abilities"):set_visible(true)
+        self.ability_cursor.count = #entries
+        self.ability_cursor:reset()
+        self:populate_abilities()
+    end,
+
+    --- Gets the spells, summons or enemy skills the character can use in battle.
+    --
+    -- @param kind "magic", "summon" or "eskill".
+    -- @return List of {attack = ID of the attack, times = uses per battle of a summon}.
+    build_abilities = function(self, kind)
+        local abilities = Characters.get_abilities(self.char_id)
+        local entries = {}
+        if kind == "magic" then
+            -- The spells of a materia go together.
+            for _, group in ipairs(abilities.magic_groups) do
+                for _, spell in ipairs(group) do entries[#entries + 1] = {attack = spell} end
+            end
+        elseif kind == "summon" then
+            for _, summon in ipairs(abilities.summon) do
+                entries[#entries + 1] = {attack = summon.id, times = summon.times}
+            end
+        else
+            for _, skill in ipairs(abilities.e_skill) do entries[#entries + 1] = {attack = skill} end
+        end
+        return entries
+    end,
+
+    --- Fills the rows of the abilities list.
+    populate_abilities = function(self)
+        local window = "MateriaMenu.Container.Abilities."
+        for row = 1, 10 do
+            local entry = self.abilities[self.ability_cursor.first + row - 1]
+            local data = entry ~= nil and Game.Attacks[entry.attack] or nil
+            if data == nil then
+                ui_manager:get_widget(window .. "Name" .. row):set_text("")
+                ui_manager:get_widget(window .. "Mp" .. row):set_text("")
+            else
+                local name = data.name
+                if entry.times ~= nil and entry.times < 255 then name = name .. " x" .. tostring(entry.times) end
+                ui_manager:get_widget(window .. "Name" .. row):set_text(name)
+                ui_manager:get_widget(window .. "Mp" .. row):set_text(UiContainer.pad_value(data.mp or 0, 3))
+            end
+        end
+        self:populate_ability_help()
+    end,
+
+    --- Shows the description of the highlighted ability.
+    populate_ability_help = function(self)
+        local entry = self.abilities[self.ability_cursor:index()]
+        local data = entry ~= nil and Game.Attacks[entry.attack] or nil
+        ui_manager:get_widget("MateriaMenu.Container.Help.Help"):set_text(data ~= nil and data.description or "")
+    end,
+
     submenu_list = function(self)
         UiContainer.current_submenu = "list"
         ui_manager:get_widget("MateriaMenu.Container.List.Cursor"):set_visible(true)
@@ -314,7 +696,26 @@ UiContainer.MateriaMenu = {
         ui_manager:get_widget("MateriaMenu.Container.Details.EnemySkill"):set_visible(false)
         ui_manager:get_widget("MateriaMenu.Container.Details.Materia"):set_visible(false)
         ui_manager:get_widget("MateriaMenu.Container.Help.Help"):set_text("")
-        -- TODO
+        -- The commands the character has in battle.
+        local commands = Characters.get_commands(self.char_id)
+        self.commands = commands
+        for i = 1, 12 do
+            local widget = ui_manager:get_widget("MateriaMenu.Container.Details.Commands.Box.Command" .. i)
+            local command = Game.Commands[commands[i]]
+            if command ~= nil then
+                widget:set_text(command.name)
+                widget:set_visible(true)
+                -- Commands with a list but nothing in it (no enemy skill learned...) are dimmed.
+                local kind = self:command_list_kind(command)
+                if kind ~= nil and #self:build_abilities(kind) == 0 then
+                    widget:set_colour(0.6, 0.6, 0.6)
+                else
+                    widget:set_colour(1, 1, 1)
+                end
+            else
+                widget:set_visible(false)
+            end
+        end
     end,
 
     --- Populated the details of the selected header item (check, materia, or none).

+ 17 - 9
data/data/scripts/menu/phs_menu.lua

@@ -55,7 +55,8 @@ local function neighbour(cell, button)
     return nil
 end
 
---- Is a character locked in its place? Locked characters can't be moved or replaced.
+--- Is a character locked in the party? Locked characters can't leave the party or join it, but they
+-- can change slots inside it.
 local function is_locked(char_id)
     return char_id ~= nil and Characters[char_id].locked == true
 end
@@ -93,8 +94,12 @@ end
 --  * party <-> party: changes the order (or moves the character to an empty slot).
 --  * party <-> reserve: the character leaves the party and the other one (if any) takes its place.
 --  * reserve <-> reserve: not allowed.
--- Locked characters can't be moved or replaced. The party can only be left once it has three
--- characters, or all the available ones if there are fewer.
+-- Locked characters can't leave the party or join it, but their slots can be swapped.
+-- The party can only be left once it has three characters, or all the available ones if there are
+-- fewer.
+--
+-- This menu never changes the leader of the field (see Party.get_leader). If the leader leaves the
+-- party, the character of the first slot becomes the leader.
 UiContainer.PhsMenu = UiContainer.Menu.new{
 
     id = "phs",
@@ -205,7 +210,8 @@ UiContainer.PhsMenu = UiContainer.Menu.new{
         local character = self:get_character(cell)
         if self.held == nil then
             -- Pick a character.
-            if character == nil or is_locked(character) then return false end
+            -- A locked character can only be moved inside the party.
+            if character == nil or (is_locked(character) and not is_party_cell(cell)) then return false end
             self:set_held(cell)
             return
         end
@@ -227,13 +233,15 @@ UiContainer.PhsMenu = UiContainer.Menu.new{
         if not is_party_cell(from) and not is_party_cell(to) then return false end
         local a = self:get_character(from)
         local b = self:get_character(to)
-        if is_locked(a) or is_locked(b) then return false end
+        -- Locked characters can swap slots in the party, but can't leave it or join it.
+        if (is_locked(a) or is_locked(b)) and not (is_party_cell(from) and is_party_cell(to)) then
+            return false
+        end
 
         -- Both cells exchange their contents, so nobody in the reserve changes place.
         self:set_character(from, b)
         self:set_character(to, a)
-        set_party(Party[1], Party[2], Party[3])
-        -- TODO: If the leader (first slot) changed, change the player entity of the field too.
+        set_party(Party[1], Party[2], Party[3], true)
         return true
     end,
 
@@ -243,7 +251,7 @@ UiContainer.PhsMenu = UiContainer.Menu.new{
         for slot = 1, PARTY_SLOTS do
             if Party[slot] ~= nil then members[#members + 1] = Party[slot] end
         end
-        set_party(members[1], members[2], members[3])
+        set_party(members[1], members[2], members[3], true)
     end,
 
     --- Shows the characters of the party and of the reserve.
@@ -299,7 +307,7 @@ UiContainer.PhsMenu = UiContainer.Menu.new{
         for slot = 1, PARTY_SLOTS do
             if Party[slot] ~= nil and Characters[Party[slot]] == nil then Party[slot] = nil end
         end
-        set_party(Party[1], Party[2], Party[3])
+        set_party(Party[1], Party[2], Party[3], true)
         -- Whoever is in the party is available, even if no script enabled them.
         for slot = 1, PARTY_SLOTS do
             if Party[slot] ~= nil then Characters[Party[slot]].enabled = true end

+ 84 - 5
data/data/scripts/menu/save_menu.lua

@@ -19,7 +19,48 @@ UiContainer.SaveMenu = UiContainer.Menu.new{
         on_scroll = function() UiContainer.SaveMenu:populate_slots() end,
     },
 
+    --- Cursors over Yes and No of the confirmation windows.
+    confirm_cursor = {
+        same = UiContainer.Cursor.new{widget = "SaveMenu.Container.ConfirmSame.Cursor", total = 2},
+        other = UiContainer.Cursor.new{widget = "SaveMenu.Container.ConfirmOther.Cursor", total = 2},
+    },
+
+    --- Slot (1-based) waiting for the confirmation, and which window is open ("same" or "other").
+    pending_slot = nil,
+    confirm_kind = "same",
+
     states = {
+        --- Confirmation to override a slot. Yes is the default unless the slot belongs to another
+        -- playthrough.
+        confirm_save = {
+            cursor = function(self) return self.confirm_cursor[self.confirm_kind] end,
+            axis = "vertical",
+            confirm_sound = false,
+            confirm = function(self, cursor)
+                if cursor:index() == 1 then
+                    self:save_slot(self.pending_slot)
+                else
+                    self:close_confirm()
+                    UiContainer.play_sound("Back")
+                end
+            end,
+            cancel = function(self) self:close_confirm() end,
+        },
+
+        --- "Game saved". Confirm or cancel close it.
+        saved = {
+            confirm_sound = false,
+            cancel_sound = false,
+            confirm = function(self)
+                self:close_saved()
+                UiContainer.play_sound("Back")
+            end,
+            cancel = function(self)
+                self:close_saved()
+                UiContainer.play_sound("Back")
+            end,
+        },
+
         [""] = {
             cursor = function(self) return self.cursor end,
             axis = "vertical",
@@ -63,6 +104,9 @@ UiContainer.SaveMenu = UiContainer.Menu.new{
         ui_manager:get_widget(container .. "TopBar.DescLoad"):set_visible(loading)
         ui_manager:get_widget(container .. "Title.TitleSave"):set_visible(not loading)
         ui_manager:get_widget(container .. "Title.TitleLoad"):set_visible(loading)
+        ui_manager:get_widget("SaveMenu.Container.ConfirmSame"):set_visible(false)
+        ui_manager:get_widget("SaveMenu.Container.ConfirmOther"):set_visible(false)
+        ui_manager:get_widget("SaveMenu.Container.Saved"):set_visible(false)
         ui_manager:get_widget("SaveMenu"):set_visible(true)
         self.cursor.count = self.max_slots
         self.cursor:reset()
@@ -106,13 +150,20 @@ UiContainer.SaveMenu = UiContainer.Menu.new{
 
     action = function(self, slot)
         if self.operation == "save" then
-            if savemap_manager:is_slot_empty(slot - 1) == false and ControlKey ~= savemap_manager:get_slot_control_key(slot - 1) then
-                -- TODO: Confirmation dialog
-                print("OVERRIDE ANOTHER SAVE")
+            if savemap_manager:is_slot_empty(slot - 1) == true then
+                -- Nothing to override.
+                self:save_slot(slot)
+                return
             end
-            save_game(slot - 1, true)
+            -- Ask first. A slot of another playthrough is not overridden unless the player says so.
+            self.pending_slot = slot
+            local other = (ControlKey ~= savemap_manager:get_slot_control_key(slot - 1))
+            self.confirm_kind = other and "other" or "same"
+            ui_manager:get_widget("SaveMenu.Container.ConfirmSame"):set_visible(not other)
+            ui_manager:get_widget("SaveMenu.Container.ConfirmOther"):set_visible(other)
+            self.confirm_cursor[self.confirm_kind]:set(other and 2 or 1)
+            self:set_state("confirm_save")
             audio_manager:play_sound("Cursor")
-            self:populate_slots()
         elseif self.operation == "load" then
             audio_manager:play_sound("Window")
             script:request_end_sync(Script.UI, "SaveMenu", "hide", 0)
@@ -121,6 +172,34 @@ UiContainer.SaveMenu = UiContainer.Menu.new{
         end
     end,
 
+    --- Closes the confirmation window without saving.
+    close_confirm = function(self)
+        ui_manager:get_widget("SaveMenu.Container.ConfirmSame"):set_visible(false)
+        ui_manager:get_widget("SaveMenu.Container.ConfirmOther"):set_visible(false)
+        self.pending_slot = nil
+        self:set_state("")
+    end,
+
+    --- Saves the game in a slot and shows the "Game saved" window.
+    --
+    -- @param slot Slot to save in (1-based).
+    save_slot = function(self, slot)
+        ui_manager:get_widget("SaveMenu.Container.ConfirmSame"):set_visible(false)
+        ui_manager:get_widget("SaveMenu.Container.ConfirmOther"):set_visible(false)
+        self.pending_slot = nil
+        save_game(slot - 1, true)
+        audio_manager:play_sound("Cursor")
+        self:populate_slots()
+        ui_manager:get_widget("SaveMenu.Container.Saved"):set_visible(true)
+        self:set_state("saved")
+    end,
+
+    --- Closes the "Game saved" window.
+    close_saved = function(self)
+        ui_manager:get_widget("SaveMenu.Container.Saved"):set_visible(false)
+        self:set_state("")
+    end,
+
     --- Hides the item menu and goes back to the main menu.
     hide = function(self)
         ui_manager:get_widget("SaveMenu"):set_visible(false)

+ 76 - 10
data/data/scripts/menu/shop_menu.lua

@@ -12,6 +12,7 @@ if UiContainer == nil then UiContainer = {} end
 --  * "buy_quantity": how many of the highlighted item to buy.
 --  * "sell": list of the things of the party that can be sold.
 --  * "sell_quantity": how many of the highlighted item to sell.
+--  * "sell_materia": stars, AP and price of the highlighted materia, to confirm the sale.
 UiContainer.ShopMenu = UiContainer.Menu.new{
 
     id = "shop",
@@ -113,6 +114,13 @@ UiContainer.ShopMenu = UiContainer.Menu.new{
             cancel = function(self) self:enter_command() end,
         },
 
+        -- Details of a materia before selling it: confirm sells it, cancel goes back to the list.
+        sell_materia = {
+            confirm_sound = false,
+            confirm = function(self) return self:sell(self.entries[self.list_cursor:index()]) end,
+            cancel = function(self) self:set_state("sell") self:update_prompt() end,
+        },
+
         sell_quantity = {
             confirm_sound = false,
             confirm = function(self) return self:sell(self.entries[self.list_cursor:index()]) end,
@@ -206,6 +214,7 @@ UiContainer.ShopMenu = UiContainer.Menu.new{
         ui_manager:get_widget(bar .. "PromptSell"):set_visible(state == "sell")
         ui_manager:get_widget(bar .. "PromptQuantity"):set_visible(
             state == "buy_quantity" or state == "sell_quantity")
+        ui_manager:get_widget("ShopMenu.Container.MateriaSale"):set_visible(state == "sell_materia")
         ui_manager:get_widget("ShopMenu.Container.Quantity"):set_visible(
             state == "buy_quantity" or state == "sell_quantity")
         ui_manager:get_widget("ShopMenu.Container.List.Cursor"):set_visible(state ~= "")
@@ -245,7 +254,7 @@ UiContainer.ShopMenu = UiContainer.Menu.new{
                 local owned = Materia[slot]
                 if owned ~= nil and Game.Materia[owned.id] ~= nil then
                     table.insert(entries, {
-                        materia = true, id = owned.id, slot = slot,
+                        materia = true, id = owned.id, slot = slot, ap = owned.ap or 0,
                         price = self:materia_sell_price(owned.id, owned.ap or 0),
                         sellable = true,
                     })
@@ -411,7 +420,11 @@ UiContainer.ShopMenu = UiContainer.Menu.new{
         if entry == nil or not entry.sellable then return false end
         if not entry.materia and entry.price <= 0 then return false end
         if entry.materia then
-            return self:sell(entry)
+            self:set_state("sell_materia")
+            self:update_prompt()
+            self:populate_materia_sale(entry)
+            UiContainer.play_sound("Cursor")
+            return
         end
         self.max_quantity = entry.quantity
         self:enter_quantity("sell_quantity")
@@ -420,7 +433,7 @@ UiContainer.ShopMenu = UiContainer.Menu.new{
 
     --- Sells the chosen quantity of an entry.
     sell = function(self, entry)
-        if entry == nil then return false end
+        if entry == nil or not entry.sellable then return false end
         if entry.materia then
             Materia[entry.slot] = nil
             Inventory.add_money(entry.price)
@@ -486,6 +499,60 @@ UiContainer.ShopMenu = UiContainer.Menu.new{
         ui_manager:get_widget(window .. "Count"):set_text(UiContainer.pad_value(self.quantity, 2))
         ui_manager:get_widget(window .. "Total"):set_text(
             UiContainer.pad_value(entry.price * self.quantity, 8))
+        self:populate_slots(entry)
+    end,
+
+    --- Shows the materia slots and links of a weapon or armor in the quantity window, as the
+    -- equipment menu does. Other things have no slots.
+    populate_slots = function(self, entry)
+        local data = (not entry.materia) and Game.Items[entry.id] or nil
+        local slots = {}
+        local growth = 1
+        if data ~= nil and (data.type == Inventory.ITEM_TYPE.WEAPON or data.type == Inventory.ITEM_TYPE.ARMOR) then
+            slots = data.slots or {}
+            growth = data.growth
+        end
+        local window = "ShopMenu.Container.Quantity."
+        for s = 1, 8 do
+            local slot = ui_manager:get_widget(window .. "Slot" .. s)
+            slot:set_visible(#slots >= s)
+            if growth == 0 then
+                slot:set_image("icons/materia_slot_no_growth.png")
+            else
+                slot:set_image("icons/materia_slot.png")
+            end
+            if s % 2 ~= 0 then
+                -- A link joins an odd slot with the next one.
+                ui_manager:get_widget(window .. "Link" .. (math.floor(s / 2) + 1)):set_visible(
+                    #slots > s and slots[s] == 1)
+            end
+        end
+    end,
+
+    --- Fills the window that shows a materia before it is sold.
+    populate_materia_sale = function(self, entry)
+        local data = Game.Materia[entry.id]
+        local window = "ShopMenu.Container.MateriaSale."
+        local levels = data.levels_ap or {}
+        local level = 0
+        for _, level_ap in ipairs(levels) do
+            if level_ap > entry.ap then break end
+            level = level + 1
+        end
+        ui_manager:get_widget(window .. "Icon"):set_image("icons/materia_" .. tostring(data.type) .. ".png")
+        ui_manager:get_widget(window .. "Name"):set_text(data.name)
+        for star = 1, 5 do
+            local widget = ui_manager:get_widget(window .. "Star" .. star)
+            if star > #levels then
+                widget:set_visible(false)
+            else
+                widget:set_image((star <= level and "icons/materia_star_" or "icons/materia_star_empty_")
+                    .. tostring(data.type) .. ".png")
+                widget:set_visible(true)
+            end
+        end
+        ui_manager:get_widget(window .. "Ap"):set_text(UiContainer.pad_value(entry.ap, 8))
+        ui_manager:get_widget(window .. "Price"):set_text(UiContainer.pad_value(entry.price, 8))
     end,
 
     --- Fills the rows of the list.
@@ -542,11 +609,13 @@ UiContainer.ShopMenu = UiContainer.Menu.new{
         local data = (entry ~= nil and not entry.materia) and Game.Items[entry.id] or nil
         local is_weapon = data ~= nil and data.type == Inventory.ITEM_TYPE.WEAPON
         local is_armor = data ~= nil and data.type == Inventory.ITEM_TYPE.ARMOR
-        local slot = 0
-        for char_id = 0, 8 do
+        -- Each character has the spot of its ID, even if that leaves empty spots between them.
+        for char_id = 0, self.MAX_CHARACTERS - 1 do
+            local slot = char_id + 1
             local character = Characters[char_id]
-            if type(character) == "table" and character.enabled == true and slot < self.MAX_CHARACTERS then
-                slot = slot + 1
+            if not (type(character) == "table" and character.enabled == true) then
+                ui_manager:get_widget("ShopMenu.Container.Party.Slot" .. slot):set_visible(false)
+            else
                 local widget = "ShopMenu.Container.Party.Slot" .. slot
                 ui_manager:get_widget(widget):set_visible(true)
                 ui_manager:get_widget(widget .. ".Portrait"):set_image("characters/" .. tostring(char_id) .. ".png")
@@ -569,9 +638,6 @@ UiContainer.ShopMenu = UiContainer.Menu.new{
                 self:populate_stat(widget .. ".Def", can_use, defense, new_defense)
             end
         end
-        for hidden = slot + 1, self.MAX_CHARACTERS do
-            ui_manager:get_widget("ShopMenu.Container.Party.Slot" .. hidden):set_visible(false)
-        end
     end,
 
     --- Fills a stat row of a party member.

+ 11 - 0
data/data/scripts/save.lua

@@ -25,6 +25,8 @@ save_game = function(slot, force)
        end
     end
     savemap_manager:set_party(Party[1] or -1, Party[2] or -1, Party[3] or -1)
+    -- The leader of the field is not always the first member of the party.
+    savemap_manager:set_setting("field_leader", Party.get_leader() or -1)
     for i = 1, MAX_INVENTORY_SLOTS do
         if Inventory[i] ~= nil then
             savemap_manager:set_item(i - 1, Inventory[i].item, Inventory[i].quantity)
@@ -187,6 +189,9 @@ end
 --
 -- @param slot The slot to load (0-15).
 load_game = function(slot)
+    -- The game that was loaded keeps the control key of its slot, so it can be saved there again
+    -- without being taken for another playthrough.
+    ControlKey = savemap_manager:get_slot_control_key(slot)
     -- Populate banks
     for b = 0, Banks.COUNT do
         for a = 0, Banks.SIZE do
@@ -204,6 +209,12 @@ load_game = function(slot)
             Party[p] = nil
         end
     end
+    -- Games saved before the leader was saved have the first member as leader.
+    Party.leader = nil
+    if savemap_manager:is_slot_setting_set(slot, "field_leader") then
+        local leader = savemap_manager:get_slot_setting(slot, "field_leader")
+        if leader >= 0 then Party.leader = leader end
+    end
     -- The battles and dialogs read the party from the text manager.
     export_party_members()
     -- The inventory and the materia are 1-based in the game, and 0-based in the savemap.

+ 26 - 1
data/data/scripts/system.lua

@@ -132,16 +132,40 @@ end
 
 --- Sets the current party.
 --
+-- This is what the scripts do (PRTYE and the party selection), so the first character also becomes
+-- the leader of the field: the character the player controls (see Party.get_leader). Changing the
+-- order of the party in the menus does not change the leader, so they pass `keep_leader`.
+--
 -- @param character1 The first member character ID, the party leader.
 -- @param character2 The second member character ID, can be nil.
 -- @param character3 The third member character ID, can be nil.
-set_party = function(character1, character2, character3)
+-- @param keep_leader If true, the leader of the field is not changed.
+set_party = function(character1, character2, character3, keep_leader)
     Party[1] = character1
     Party[2] = character2
     Party[3] = character3
+    if not keep_leader then Party.leader = character1 end
     export_party_members()
 end
 
+--- Gets the leader of the party in the field: the character whose entity the player controls.
+--
+-- It is set by the scripts when they set the party, and it isn't changed by moving characters
+-- around in the Order menu. If there is no leader, or the leader isn't in the party (a party made
+-- by adding characters, or a game saved before the leader was saved), the first member of the party
+-- becomes the leader, and stays so until a script changes it.
+--
+-- @return ID of the character, or nil if there is no party.
+Party.get_leader = function()
+    if Party.leader ~= nil then
+        for slot = 1, 3 do
+            if Party[slot] == Party.leader then return Party.leader end
+        end
+    end
+    Party.leader = Party[1]
+    return Party.leader
+end
+
 --- Sets the party from three values (SPTYE), the ones that GTPYE stored: 254 and 255 are an empty
 -- place.
 --
@@ -211,6 +235,7 @@ remove_party_character = function(character)
     for i = 1, 3 do
         if Party[i] == character then
             Party[i] = nil
+            if Party.leader == character then Party.leader = nil end
             export_party_members()
             return
         end

+ 8 - 0
data/data/texts/english/menu.xml

@@ -91,6 +91,10 @@
 
     <text name="MateriaMenuTitle">Materia</text>
     <text name="MateriaMenuCheck"><colour value="0.7 0.7 0.7">Check</colour></text>
+    <text name="MateriaMenuArrange">Arrange</text>
+    <text name="MateriaMenuExchange">Exchange</text>
+    <text name="MateriaMenuRemoveAll">Remove all</text>
+    <text name="MateriaMenuTrash">Trash</text>
     <text name="MateriaMenuOrder"><colour value="0.7 0.7 0.7">Order</colour></text>
     <text name="MateriaMenuAP"><colour value="0 0.8 1">A.P.:</colour></text>
     <text name="MateriaMenuAPNext"><colour value="0 0.8 1">Next:</colour></text>
@@ -109,6 +113,9 @@
     <text name="SaveMenuTime">Time</text>
     <text name="SaveMenuMoney">Gil</text>
     <text name="SaveMenuEmptySlot"><colour value="0.6 0.6 0.6">Empty slot</colour></text>
+    <text name="SaveMenuOverride">Override this save slot?</text>
+    <text name="SaveMenuOtherPlaythrough"><colour value="0.75 0.4 1">This slot belongs to another playthrough</colour></text>
+    <text name="SaveMenuSaved"><colour value="0.75 0.4 1">Game saved</colour></text>
     <text name="SaveMenuTitleSave">Save</text>
     <text name="SaveMenuTitleLoad">Load</text>
     <text name="SaveMenuDescSave">Select a slot to save</text>
@@ -261,6 +268,7 @@
     <text name="ShopMenuGil"><colour value="0 0.8 1">Gil</colour></text>
     <text name="ShopMenuOwned"><colour value="0 0.8 1">Owned</colour></text>
     <text name="ShopMenuEquipped"><colour value="0 0.8 1">Equipped</colour></text>
+    <text name="ShopMenuAP"><colour value="0 0.8 1">AP</colour></text>
     <text name="ShopMenuAtk"><colour value="0 0.8 1">A</colour></text>
     <text name="ShopMenuDef"><colour value="0 0.8 1">D</colour></text>
 </texts>

+ 29 - 0
src/core/ConfigCmdHandlerCommands.h

@@ -316,6 +316,34 @@ void CmdShop(const Ogre::StringVector& params){
     );
 }
 
+/**
+ * Enables or disables characters in the PHS.
+ *
+ * @param[in] params Command parameters: the command name, the ID of the character (0-8) or "all", and
+ * optionally "on" (the default) or "off".
+ */
+void CmdPhs(const Ogre::StringVector& params){
+    if (params.size() < 2 || params.size() > 3){
+        Console::getSingleton().AddTextToOutput("Usage: /phs <character_id|all> [on|off]");
+        return;
+    }
+    const bool enable = params.size() < 3 || (params[2] != "off" && params[2] != "0");
+    const std::string value = enable ? "true" : "false";
+    std::string first = "0";
+    std::string last = "8";
+    if (params[1] != "all"){
+        first = params[1];
+        last = params[1];
+    }
+    ScriptManager::getSingleton().RunString(
+      "for id = (tonumber(\"" + first + "\") or -1), (tonumber(\"" + last + "\") or -2) do "
+      "if type(Characters[id]) == \"table\" then Characters[id].enabled = " + value + " end end"
+    );
+    Console::getSingleton().AddTextToOutput(
+      std::string(enable ? "Enabled " : "Disabled ") + (params[1] == "all" ? "all characters" : "character " + params[1]) + " in the PHS"
+    );
+}
+
 /**
  * Loads a list of map names.
  *
@@ -504,6 +532,7 @@ void ConfigCmdHandler::InitCmd(){
     AddCommand("set_log_level", "Set log messages level", "", CmdSetLogLevel, NULL);
     AddCommand("map", "Run game module", "", CmdMap, CmdMapCompletion);
     AddCommand("shop", "Open a shop", "<shop_id>", CmdShop, NULL);
+    AddCommand("phs", "Enable or disable characters in the PHS", "<character_id|all> [on|off]", CmdPhs, NULL);
     AddCommand("resolution", "Change resolution", "", CmdResolution, CmdResolutionCompletition);
     AddCommand("screenshot", "Capture current screen content", "", CmdScreenshot, NULL);
     AddCommand("fade", "Start a screen fade", "<type> <r> <g> <b> <speed>", CmdFade, NULL);