<?xml version="1.0" encoding="UTF-8"?>

<display window-flags="sticky, below" anchor="center">

  <meta author="Robert Pastierovic, Bjoern Koch"
        version="0.6"
        category="Fun/Amusements"
        name="15pieces"
        description="15 pieces puzzle game."
        dependency="0.36.1"
        preview="Numbers/15.png"/>

  <control id="randomizer" interface="IRandom:bss2bsoo8o0dl3kumdpegtrc2-2" />

  <image id="bg" uri="gfx/dummy.png" width="5cm" height="5cm"/>
  <group id="main" width="80%" height="80%" relative-to="bg, y" x="10%" y="-90%">
    <image id="p1" uri="gfx/dummy.png" width="25%" height="25%" x="0%" y="0%" on-click="click_piece(self)"/>
    <image id="p2" uri="gfx/dummy.png" width="25%" height="25%" x="25%" y="0%" on-click="click_piece(self)"/>
    <image id="p3" uri="gfx/dummy.png" width="25%" height="25%" x="50%" y="0%" on-click="click_piece(self)"/>
    <image id="p4" uri="gfx/dummy.png" width="25%" height="25%" x="75%" y="0%" on-click="click_piece(self)"/>
    <image id="p5" uri="gfx/dummy.png" width="25%" height="25%" x="0%" y="25%" on-click="click_piece(self)"/>
    <image id="p6" uri="gfx/dummy.png" width="25%" height="25%" x="25%" y="25%" on-click="click_piece(self)"/>
    <image id="p7" uri="gfx/dummy.png" width="25%" height="25%" x="50%" y="25%" on-click="click_piece(self)"/>
    <image id="p8" uri="gfx/dummy.png" width="25%" height="25%" x="75%" y="25%" on-click="click_piece(self)"/>
    <image id="p9" uri="gfx/dummy.png" width="25%" height="25%" x="0%" y="50%" on-click="click_piece(self)"/>
    <image id="p10" uri="gfx/dummy.png" width="25%" height="25%" x="25%" y="50%" on-click="click_piece(self)"/>
    <image id="p11" uri="gfx/dummy.png" width="25%" height="25%" x="50%" y="50%" on-click="click_piece(self)"/>
    <image id="p12" uri="gfx/dummy.png" width="25%" height="25%" x="75%" y="50%" on-click="click_piece(self)"/>
    <image id="p13" uri="gfx/dummy.png" width="25%" height="25%" x="0%" y="75%" on-click="click_piece(self)"/>
    <image id="p14" uri="gfx/dummy.png" width="25%" height="25%" x="25%" y="75%" on-click="click_piece(self)"/>
    <image id="p15" uri="gfx/dummy.png" width="25%" height="25%" x="50%" y="75%" on-click="click_piece(self)"/>
    <image id="p16" uri="gfx/dummy.png" width="25%" height="25%" x="75%" y="75%" on-click="click_piece(self)"/>
    <image id="restart_img" uri="gfx/dummy.png" width="75%" height="25%" x="12.5%" y="37.5%" visible="True" on-click="restart()"/>
  </group>

  <prefs callback="prefs_cb">
    <page label="Appearance">
     <float label="Size (cm):" bind="size" min="3" max="10" help="The size of the desklet."/>
     <enum label="Puzzle to Solve:" bind="pieces_image" help="What puzzle you want to solve.">
       <item label="gDesklets" value="gDesklets/"/>
       <item label="Globe" value="Globe/"/>
       <item label="Gnome" value="Gnome/"/>
       <item label="GNU" value="GNU/"/>
       <item label="Numbers" value="Numbers/"/>
       <item label="Tux" value="Tux/"/>
     </enum>
    </page>
  </prefs>

<script><![CDATA[

pieces_image = "gDesklets/"
size = 6

def prefs_cb(key, value):
  if (key == "size"):
    Dsp.bg.width = Unit(value, CM)
    Dsp.bg.height = Unit(value, CM)
  if (key == "pieces_image"):
    Dsp.restart_img.visible = True
    restart()
    Dsp.restart_img.visible = False

def click_piece(piece):
  if (piece.uri != pieces_image + "free.png"):
    free_piece = find_free_piece(piece)
    if (free_piece != piece):
      free_piece.uri = piece.uri
      piece.uri = pieces_image + "free.png"
  check_layout()

def find_free_piece(piece):
  #find free piece (range doesn't work somehow)
  if (Dsp.p1.uri == pieces_image + "free.png"): free_p = Dsp.p1
  if (Dsp.p2.uri == pieces_image + "free.png"): free_p = Dsp.p2
  if (Dsp.p3.uri == pieces_image + "free.png"): free_p = Dsp.p3
  if (Dsp.p4.uri == pieces_image + "free.png"): free_p = Dsp.p4
  if (Dsp.p5.uri == pieces_image + "free.png"): free_p = Dsp.p5
  if (Dsp.p6.uri == pieces_image + "free.png"): free_p = Dsp.p6
  if (Dsp.p7.uri == pieces_image + "free.png"): free_p = Dsp.p7
  if (Dsp.p8.uri == pieces_image + "free.png"): free_p = Dsp.p8
  if (Dsp.p9.uri == pieces_image + "free.png"): free_p = Dsp.p9
  if (Dsp.p10.uri == pieces_image + "free.png"): free_p = Dsp.p10
  if (Dsp.p11.uri == pieces_image + "free.png"): free_p = Dsp.p11
  if (Dsp.p12.uri == pieces_image + "free.png"): free_p = Dsp.p12
  if (Dsp.p13.uri == pieces_image + "free.png"): free_p = Dsp.p13
  if (Dsp.p14.uri == pieces_image + "free.png"): free_p = Dsp.p14
  if (Dsp.p15.uri == pieces_image + "free.png"): free_p = Dsp.p15
  if (Dsp.p16.uri == pieces_image + "free.png"): free_p = Dsp.p16

  #check if clicking on nearby piece
  if (abs(int(piece.id[1:]) - int(free_p.id[1:])) == 1) or (abs(int(piece.id[1:]) - int(free_p.id[1:])) == 4):
    return free_p
  else:
    return piece

def check_layout():
  if (Dsp.p1.uri == pieces_image + "1.png") and (Dsp.p2.uri == pieces_image + "2.png") and (Dsp.p3.uri == pieces_image + "3.png") and (Dsp.p4.uri == pieces_image + "4.png") \
  and (Dsp.p5.uri == pieces_image + "5.png") and (Dsp.p6.uri == pieces_image + "6.png") and (Dsp.p7.uri == pieces_image + "7.png") and (Dsp.p8.uri == pieces_image + "8.png") \
  and (Dsp.p9.uri == pieces_image + "9.png") and (Dsp.p10.uri == pieces_image + "10.png") and (Dsp.p11.uri == pieces_image + "11.png") and (Dsp.p12.uri == pieces_image + "12.png") \
  and (Dsp.p13.uri == pieces_image + "13.png") and (Dsp.p14.uri == pieces_image + "14.png") and (Dsp.p15.uri == pieces_image + "15.png") and (Dsp.p16.uri == pieces_image + "free.png"):
    Dsp.restart_img.visible = True
  else:
    Dsp.restart_img.visible = False

def restart():
  if (Dsp.restart_img.visible == True):
    layout = randomizer.sample
    Dsp.bg.uri = pieces_image + "bg.png"
    Dsp.p1.uri = pieces_image + str(layout[0]) +".png"
    Dsp.p2.uri = pieces_image + str(layout[1]) +".png"
    Dsp.p3.uri = pieces_image + str(layout[2]) +".png"
    Dsp.p4.uri = pieces_image + str(layout[3]) +".png"
    Dsp.p5.uri = pieces_image + str(layout[4]) +".png"
    Dsp.p6.uri = pieces_image + str(layout[5]) +".png"
    Dsp.p7.uri = pieces_image + str(layout[6]) +".png"
    Dsp.p8.uri = pieces_image + str(layout[7]) +".png"
    Dsp.p9.uri = pieces_image + str(layout[8]) +".png"
    Dsp.p10.uri = pieces_image + str(layout[9]) +".png"
    Dsp.p11.uri = pieces_image + str(layout[10]) +".png"
    Dsp.p12.uri = pieces_image + str(layout[11]) +".png"
    Dsp.p13.uri = pieces_image + str(layout[12]) +".png"
    Dsp.p14.uri = pieces_image + str(layout[13]) +".png"
    Dsp.p15.uri = pieces_image + str(layout[14]) +".png"
    Dsp.p16.uri = pieces_image + "free.png"
    Dsp.restart_img.uri = pieces_image + "restart.png"
    Dsp.restart_img.visible = False

randomizer.sequence = ((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), 15)
restart()

]]></script>

</display>
