Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
mathdoku
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
pm3g19
mathdoku
Commits
50bb00ce
Commit
50bb00ce
authored
3 years ago
by
pm3g19
Browse files
Options
Downloads
Patches
Plain Diff
Added missing file
parent
aaa3f0b9
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/com/patryk/mathdoku/gui/InputHandler.java
+119
-0
119 additions, 0 deletions
src/com/patryk/mathdoku/gui/InputHandler.java
with
119 additions
and
0 deletions
src/com/patryk/mathdoku/gui/InputHandler.java
0 → 100644
+
119
−
0
View file @
50bb00ce
package
com.patryk.mathdoku.gui
;
import
com.patryk.mathdoku.util.Direction
;
import
com.patryk.mathdoku.util.Util
;
import
com.patryk.mathdoku.util.BoardPosVec
;
import
javafx.event.EventHandler
;
import
javafx.scene.input.KeyEvent
;
import
javafx.scene.input.MouseEvent
;
public
class
InputHandler
{
//the class must store view in order to be able to call its methods
GameGridView
view
;
//constructor
public
InputHandler
(
GameGridView
view
){
this
.
view
=
view
;
}
//mouse callback
EventHandler
<
MouseEvent
>
mouseHandler
=
new
EventHandler
<
MouseEvent
>()
{
@Override
public
void
handle
(
MouseEvent
mouseEvent
)
{
view
.
getNode
().
requestFocus
();
BoardPosVec
selectedCellPos
=
new
BoardPosVec
((
int
)
mouseEvent
.
getY
(),
(
int
)
mouseEvent
.
getX
());
selectedCellPos
=
selectedCellPos
.
fromPixelToBoardSpace
();
view
.
setSelectedCell
(
selectedCellPos
);
}
};
//key callback
EventHandler
<
KeyEvent
>
keyEventHandler
=
new
EventHandler
<
KeyEvent
>()
{
@Override
//can either be user entering a value, or navigating
public
void
handle
(
KeyEvent
keyEvent
)
{
if
(!
handleDirectionKeyPress
(
keyEvent
))
handleValueEntered
(
keyEvent
);
}
};
public
boolean
handleDirectionKeyPress
(
KeyEvent
event
)
{
Direction
direction
;
// = Util.Direction.NORTH;
switch
(
event
.
getCode
())
{
case
W:
case
UP:
direction
=
Direction
.
NORTH
;
break
;
case
D:
case
RIGHT:
direction
=
Direction
.
EAST
;
break
;
case
S:
case
DOWN:
direction
=
Direction
.
SOUTH
;
break
;
case
A:
case
LEFT:
direction
=
Direction
.
WEST
;
break
;
default
:
return
false
;
}
//consume if arrow key to prevent loss of focus
switch
(
event
.
getCode
())
{
case
UP:
case
RIGHT:
case
DOWN:
case
LEFT:
event
.
consume
();
}
view
.
setSelectedCell
(
view
.
getSelectedCell
().
add
(
direction
.
vector
));
return
true
;
}
public
boolean
handleValueEntered
(
KeyEvent
keyEvent
)
{
switch
(
keyEvent
.
getCode
())
{
case
BACK_SPACE:
view
.
getGameContext
().
setValueAtCell
(
view
.
getSelectedCell
(),
0
,
true
);
break
;
default
:
char
keyCharacter
=
keyEvent
.
getText
().
charAt
(
0
);
char
maxChar
=
Character
.
forDigit
(
view
.
getGameContext
().
getBoardWidth
(),
10
);
//if digit entered:
if
(
keyCharacter
>=
'1'
&&
keyCharacter
<=
maxChar
)
{
injectNumberKey
(
Util
.
charToInt
(
keyCharacter
));
return
true
;
}
return
false
;
}
return
true
;
}
public
void
injectNumberKey
(
int
digit
)
{
view
.
getGameContext
().
setValueAtCell
(
view
.
getSelectedCell
(),
digit
,
true
);
}
//returns handlers so that the view can register them
public
EventHandler
<
MouseEvent
>
getMouseHandler
()
{
return
mouseHandler
;
}
public
EventHandler
<
KeyEvent
>
getKeyEventHandler
()
{
return
keyEventHandler
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment