Skip to content

Commit

Permalink
finished implementing drag and drop
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanrauh committed Sep 8, 2015
1 parent b126fe3 commit 61eb535
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,31 @@ public String getOpacity(int row, int column) {
return "opacity:1.0";
return "opacity:1.0";
}

public String getLegalTargets() {
String targets="";
for (int row = 0; row<8; row++)
for (int column=0; column<8;column++)
if (isLegalTarget(row, column))
targets+="("+row+","+column+")";
return targets;
}

public boolean isLegalTarget(int row, int column) {
if (!blackIsTop) {
row = 7 - row;
column = 7 - column;
}
if (startOpponentsMove)
return false;
if (!isPieceSelected)
return false;
if (selectedPieceColumn == column && selectedPieceRow == row)
return false;
if (chessboard.isMovePossible(selectedPieceRow, selectedPieceColumn, row, column))
return true;
return false;
}

public List<Row> getRows() {
return rows;
Expand Down
7 changes: 4 additions & 3 deletions BootsFacesChess/src/main/webapp/index.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<h:form id="form" style="margin-bottom:-10px">
<b:row>
<b:column span="8">
<script>targets='#{board.legalTargets}';</script>
<b:panel title="#{board.title}" id="board" look="info"
contentStyle="height:580px">
<ui:repeat id="rows" var="row" value="#{board.rows}"
Expand All @@ -57,9 +58,9 @@
style="position: absolute; top: 0px; left: 0px;width: 68px; height: 68px;#{board.getOpacity(rowStatus.index,columnStatus.index)}"
onclick="ajax:board.onclick(rowStatus.index,columnStatus.index)"
oncomplete="document.getElementById('form:opponent').click()"
ondragstart="console.log('drag start'+#{rowStatus.index}+'/'+#{columnStatus.index});fromRow=#{rowStatus.index};fromColumn=#{columnStatus.index};ajax:board.ondragstart(rowStatus.index,columnStatus.index)"
ondragover="console.log('drag over');if ((fromRow!=#{rowStatus.index}) || (fromColumn!=#{columnStatus.index})) event.preventDefault();"
ondrop="console.log('drop'+ fromRow + '//' + fromColumn + ' --> '+#{rowStatus.index}+'/'+#{columnStatus.index});ajax:board.ondragdrop(rowStatus.index,columnStatus.index)"
ondragstart="ajax:board.ondragstart(rowStatus.index,columnStatus.index)"
ondragover="if (targets.indexOf('(#{rowStatus.index},#{columnStatus.index})')>=0) event.preventDefault();"
ondrop="ajax:board.ondragdrop(rowStatus.index,columnStatus.index)"
update="@form :messages" />
</div>

Expand Down

0 comments on commit 61eb535

Please sign in to comment.