Skip to content

Commit

Permalink
saner mouse interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffomatic committed Dec 21, 2020
1 parent d191e28 commit 866894f
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 33 deletions.
7 changes: 4 additions & 3 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import {
import { EntityId } from '~/entities/EntityId'
import { EntityManager } from '~/entities/EntityManager'
import { GameState, gameProgression, initMap } from '~/Game'
import { IKeyboard } from '~/input/interfaces'
import { Mouse } from '~/input/Mouse'
import { IKeyboard, IMouse } from '~/input/interfaces'
import { Map } from '~/map/interfaces'
import { getModel, loadGrid } from '~/models'
import { ClientMessage, ClientMessageType } from '~/network/ClientMessage'
Expand Down Expand Up @@ -66,7 +65,7 @@ export class Client {
serverInputsPerFrame: RunningAverage

keyboard: IKeyboard
mouse?: Mouse
mouse: IMouse

// Common game state
state: GameState
Expand All @@ -81,6 +80,7 @@ export class Client {
canvas3d: HTMLCanvasElement
canvas2d: HTMLCanvasElement
keyboard: IKeyboard
mouse: IMouse
}) {
this.entityManager = new EntityManager([
[0, 0],
Expand Down Expand Up @@ -108,6 +108,7 @@ export class Client {
this.renderer2d = new Renderer2d(config.canvas2d)

this.keyboard = config.keyboard
this.mouse = config.mouse

this.lastUpdateAt = time.current()
this.lastTickAt = time.current()
Expand Down
6 changes: 2 additions & 4 deletions src/clientMain.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { vec2 } from 'gl-matrix'
import { Client } from '~/Client'
import * as clientHotReload from '~/clientHotReload'
import { DocumentEventKeyboard } from '~/input/DocumentEventKeyboard'
import { Mouse } from '~/input/Mouse'
import { DocumentEventMouse } from '~/input/Mouse'
import { createServerConnectionWs } from '~/network/ServerConnection'

declare global {
Expand Down Expand Up @@ -37,11 +37,9 @@ const client = new Client({
canvas3d,
canvas2d,
keyboard: new DocumentEventKeyboard(document),
mouse: new DocumentEventMouse(document),
})

// Set up local input
client.mouse = new Mouse(document.body)

// Development-related globals
window.client = client // expose game to console
clientHotReload.init({ enabled: true })
Expand Down
25 changes: 7 additions & 18 deletions src/input/Mouse.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,29 @@
import { vec2 } from 'gl-matrix'

export enum MouseButton {
LEFT = 0,
MIDDLE = 1,
RIGHT = 2,
}

function mouseButtonFromRaw(raw: number): MouseButton | undefined {
if (MouseButton[raw] === undefined) {
return undefined
}
return <MouseButton>raw
}
import { IMouse, MouseButton, mouseButtonFromRaw } from './interfaces'

export class Mouse {
export class DocumentEventMouse implements IMouse {
private pos: vec2 | null
private down: Set<MouseButton>
private up: Set<MouseButton>

constructor(element: HTMLElement) {
constructor(document: Document) {
this.pos = null
this.down = new Set()
this.up = new Set()

element.addEventListener('mousemove', (event) => {
document.addEventListener('mousemove', (event) => {
this.pos = vec2.fromValues(event.offsetX, event.offsetY)
})

element.addEventListener('mousedown', (event) => {
document.addEventListener('mousedown', (event) => {
const b = mouseButtonFromRaw(event.button)
if (b !== undefined) {
this.down.add(b)
}
})

element.addEventListener('mouseup', (event) => {
document.addEventListener('mouseup', (event) => {
const b = mouseButtonFromRaw(event.button)
if (b !== undefined) {
if (this.down.has(b)) {
Expand All @@ -46,7 +35,7 @@ export class Mouse {

// clear state if the mouse leaves the root element, or if the window loses
// focus
element.addEventListener('mouseout', (_event) => {
document.addEventListener('mouseout', (_event) => {
this.pos = null
this.down = new Set()
})
Expand Down
22 changes: 22 additions & 0 deletions src/input/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { vec2 } from 'gl-matrix'

export enum DirectionMove {
N = 0,
NE = -Math.PI / 4,
Expand All @@ -14,3 +16,23 @@ export interface IKeyboard {
upKeys: Set<number>
update: () => void
}

export enum MouseButton {
LEFT = 0,
MIDDLE = 1,
RIGHT = 2,
}

export function mouseButtonFromRaw(raw: number): MouseButton | undefined {
if (MouseButton[raw] === undefined) {
return undefined
}
return <MouseButton>raw
}

export interface IMouse {
getPos: () => vec2 | null
isDown: (b: MouseButton) => boolean
isUp: (b: MouseButton) => boolean
update: () => void
}
6 changes: 1 addition & 5 deletions src/systems/client/playerInput.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Client } from '~/Client'
import { DirectionMove } from '~/input/interfaces'
import { MouseButton } from '~/input/Mouse'
import { MouseButton } from '~/input/interfaces'
import { ClientMessageType } from '~/network/ClientMessage'

export const keyMap = {
Expand Down Expand Up @@ -78,10 +78,6 @@ const handleMoveInput = (client: Client, frame: number): void => {
}

const handleAttackInput = (client: Client, frame: number): void => {
if (!client.mouse) {
return
}

const mousePos = client.mouse.getPos()
if (!mousePos) {
return
Expand Down
7 changes: 4 additions & 3 deletions src/tools/map/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Camera } from '~/Camera'
import { TILE_SIZE } from '~/constants'
import * as entities from '~/entities'
import { IKeyboard } from '~/input/interfaces'
import { Mouse, MouseButton } from '~/input/Mouse'
import { IMouse, MouseButton } from '~/input/interfaces'
import { Map } from '~/map/interfaces'
import { Primitive2d, Renderer2d } from '~/renderer/Renderer2d'
import * as terrain from '~/terrain'
Expand Down Expand Up @@ -52,7 +52,7 @@ export class Editor {

camera: Camera
keyboard: IKeyboard
mouse: Mouse
mouse: IMouse

cursorTilePos: vec2 | null
brush: {
Expand All @@ -69,6 +69,7 @@ export class Editor {
canvas: HTMLCanvasElement
map: Map
keyboard: IKeyboard
mouse: IMouse
}) {
this.renderer = new Renderer2d(params.canvas)
this.events = new EventEmitter()
Expand All @@ -90,7 +91,7 @@ export class Editor {
vec2.scale(vec2.create(), this.map.dimensions, TILE_SIZE),
)
this.keyboard = params.keyboard
this.mouse = new Mouse(params.canvas)
this.mouse = params.mouse

this.cursorTilePos = null
this.brush = {
Expand Down
2 changes: 2 additions & 0 deletions src/tools/map/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as ReactDOM from 'react-dom'
import { loadMap } from './storage'

import { DocumentEventKeyboard } from '~/input/DocumentEventKeyboard'
import { DocumentEventMouse } from '~/input/Mouse'
import { Map } from '~/map/interfaces'
import { Controls } from '~/tools/map/Controls'
import { Editor } from '~/tools/map/Editor'
Expand All @@ -27,6 +28,7 @@ const editor = new Editor({
canvas,
map,
keyboard: new DocumentEventKeyboard(document),
mouse: new DocumentEventMouse(document),
})

let prevFrameTime = time.current()
Expand Down

0 comments on commit 866894f

Please sign in to comment.