LCOV - code coverage report
Current view: top level - input - keyboard.go Coverage Total Hit
Test: coverage.lcov Lines: 53.1 % 49 26
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package input
       2              : 
       3              : import (
       4              :         "sync"
       5              :         "time"
       6              : 
       7              :         "github.com/hajimehoshi/ebiten/v2"
       8              :         "github.com/hajimehoshi/ebiten/v2/inpututil"
       9              : )
      10              : 
      11              : const (
      12              :         // KeyRepeatInitialDelay はキーリピート開始までの遅延
      13              :         KeyRepeatInitialDelay = 500 * time.Millisecond
      14              : 
      15              :         // KeyRepeatInterval はキーリピートの間隔
      16              :         KeyRepeatInterval = 50 * time.Millisecond
      17              : )
      18              : 
      19              : // keyRepeatState はキーリピート状態を管理する
      20              : type keyRepeatState struct {
      21              :         pressStartTime time.Time // キーが押され始めた時刻
      22              :         lastRepeatTime time.Time // 最後にリピートした時刻
      23              : }
      24              : 
      25              : // KeyboardInput はキーボード入力を抽象化するインターフェース
      26              : type KeyboardInput interface {
      27              :         IsKeyJustPressed(key ebiten.Key) bool
      28              :         IsKeyPressed(key ebiten.Key) bool
      29              :         IsEnterJustPressedOnce() bool               // Enterキーが押下-押上のワンセットで押されたかどうか
      30              :         IsKeyPressedWithRepeat(key ebiten.Key) bool // キーリピート機能付きの押下判定
      31              : }
      32              : 
      33              : // sharedKeyboardInput はシングルトンのキーボード入力実装。
      34              : // キー状態をインスタンスフィールドとして保持する
      35              : type sharedKeyboardInput struct {
      36              :         enterPressSession bool                           // Enterキーの押下セッション状態
      37              :         keyRepeatStates   map[ebiten.Key]*keyRepeatState // キーリピート状態
      38              :         mu                sync.Mutex
      39              : }
      40              : 
      41              : var (
      42              :         keyboardInstance KeyboardInput
      43              :         once             sync.Once
      44              : )
      45              : 
      46              : // GetSharedKeyboardInput は共有されるキーボード入力インスタンスを返す
      47              : func GetSharedKeyboardInput() KeyboardInput {
      48            1 :         once.Do(func() {
      49            1 :                 keyboardInstance = &sharedKeyboardInput{
      50            1 :                         keyRepeatStates: make(map[ebiten.Key]*keyRepeatState),
      51            1 :                 }
      52              :         })
      53            1 :         return keyboardInstance
      54              : }
      55              : 
      56              : func (s *sharedKeyboardInput) IsKeyJustPressed(key ebiten.Key) bool {
      57            1 :         return inpututil.IsKeyJustPressed(key)
      58            1 : }
      59              : 
      60              : func (s *sharedKeyboardInput) IsKeyPressed(key ebiten.Key) bool {
      61            1 :         return ebiten.IsKeyPressed(key)
      62            1 : }
      63              : 
      64              : // IsEnterJustPressedOnce はEnterキーが押下-押上のワンセットで押されたかどうかを返す
      65              : func (s *sharedKeyboardInput) IsEnterJustPressedOnce() bool {
      66            1 :         s.mu.Lock()
      67            1 :         defer s.mu.Unlock()
      68            1 : 
      69            1 :         currentlyPressed := ebiten.IsKeyPressed(ebiten.KeyEnter)
      70            1 :         wasInSession := s.enterPressSession
      71            1 : 
      72            1 :         s.enterPressSession = currentlyPressed
      73            1 : 
      74              :         // セッション終了時(押下から押上への遷移)のみtrueを返す
      75            1 :         return wasInSession && !currentlyPressed
      76            1 : }
      77              : 
      78              : // IsKeyPressedWithRepeat はキーリピート機能付きの押下判定を行う
      79              : func (s *sharedKeyboardInput) IsKeyPressedWithRepeat(key ebiten.Key) bool {
      80            1 :         s.mu.Lock()
      81            1 :         defer s.mu.Unlock()
      82            1 : 
      83              :         // 初回押下
      84            1 :         if inpututil.IsKeyJustPressed(key) {
      85            0 :                 now := time.Now()
      86            0 :                 s.keyRepeatStates[key] = &keyRepeatState{
      87            0 :                         pressStartTime: now,
      88            0 :                         lastRepeatTime: now,
      89            0 :                 }
      90            0 :                 return true
      91            0 :         }
      92              : 
      93              :         // キーが押され続けている場合
      94            1 :         if ebiten.IsKeyPressed(key) {
      95            0 :                 state, exists := s.keyRepeatStates[key]
      96            0 :                 if !exists {
      97            0 :                         return false
      98            0 :                 }
      99              : 
     100            0 :                 now := time.Now()
     101            0 :                 pressDuration := now.Sub(state.pressStartTime)
     102            0 : 
     103              :                 // 初回遅延未経過
     104            0 :                 if pressDuration < KeyRepeatInitialDelay {
     105            0 :                         return false
     106            0 :                 }
     107              : 
     108              :                 // リピート間隔チェック
     109            0 :                 timeSinceLastRepeat := now.Sub(state.lastRepeatTime)
     110            0 :                 if timeSinceLastRepeat >= KeyRepeatInterval {
     111            0 :                         state.lastRepeatTime = now
     112            0 :                         return true
     113            0 :                 }
     114              : 
     115            0 :                 return false
     116              :         }
     117              : 
     118              :         // キーが離された場合、状態をクリア
     119            1 :         delete(s.keyRepeatStates, key)
     120            1 :         return false
     121              : }
        

Generated by: LCOV version 2.0-1