summaryrefslogtreecommitdiff
path: root/catbird/mode.scm
blob: d0d45ac0cc31d57c85b9e3cd144acae2ec5ffc38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
;;; Catbird Game Engine
;;; Copyright © 2022 David Thompson <davet@gnu.org>
;;;
;;; Catbird is free software: you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; Catbird is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with Catbird.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; Game state encapsulation.
;;
;;; Code:
(define-module (catbird mode)
  #:use-module (catbird config)
  #:use-module (catbird input-map)
  #:use-module (catbird mixins)
  #:use-module (ice-9 match)
  #:use-module (oop goops)
  #:use-module (srfi srfi-1)
  #:export (<major-mode>
            <minor-mode>
            bind-input
            unbind-input
            name-mode
            clear-inputs
            input-map
            on-key-press
            on-key-release
            on-text-input
            on-mouse-press
            on-mouse-release
            on-mouse-move
            on-mouse-wheel
            on-controller-press
            on-controller-release
            on-controller-move
            <nothing-mode>)
  #:re-export (controller-move
               controller-press
               controller-release
               key-press
               key-release
               text-input
               mouse-move
               mouse-press
               mouse-release
               mouse-wheel
               name
               on-enter
               on-exit
               on-pause
               on-resume
               update))

(define-root-class <mode> (<scriptable> <containable> <nameable>)
  (input-map #:accessor input-map #:allocation #:each-subclass
             #:init-thunk make-input-map))

(define-method (input-map (mode-class <class>))
  (class-slot-ref mode-class 'input-map))

(define-method (bind-input mode-class spec handler)
  (class-slot-set! mode-class 'input-map
                   (add-input (input-map mode-class) spec handler)))

(define-method (unbind-input mode-class spec)
  (class-slot-set! mode-class 'input-map
                   (remove-input (input-map mode-class) spec)))

(define (clear-inputs mode-class)
  (class-slot-set! mode-class 'input-map '()))

(define-method (on-key-press (mode <mode>) key modifiers)
  (let ((handler (key-press-handler (input-map mode) key modifiers)))
    (and handler (handler mode))))

(define-method (on-key-release (mode <mode>) key modifiers)
  (let ((handler (key-release-handler (input-map mode) key modifiers)))
    (and handler (handler mode))))

(define-method (on-text-input (mode <mode>) text)
  (let ((handler (text-input-handler (input-map mode))))
    (and handler (handler mode text))))

(define-method (on-mouse-press (mode <mode>) button x y)
  (let ((handler (mouse-press-handler (input-map mode) button)))
    (and handler (handler mode x y))))

(define-method (on-mouse-release (mode <mode>) button x y)
  (let ((handler (mouse-release-handler (input-map mode) button)))
    (and handler (handler mode x y))))

(define-method (on-mouse-move (mode <mode>) x y x-rel y-rel buttons)
  (let ((handler (mouse-move-handler (input-map mode) buttons)))
    (and handler (handler mode x y x-rel y-rel))))

(define-method (on-mouse-wheel (mode <mode>) x y)
  (let ((handler (mouse-wheel-handler (input-map mode))))
    (and handler (handler mode x y))))

(define-method (on-controller-press (mode <mode>) controller-id button)
  (let ((handler (controller-press-handler (input-map mode) controller-id button)))
    (and handler (handler mode))))

(define-method (on-controller-release (mode <mode>) controller-id button)
  (let ((handler (controller-release-handler (input-map mode) controller-id button)))
    (and handler (handler mode))))

(define-method (on-controller-move (mode <mode>) controller-id axis value)
  (let ((handler (controller-move-handler (input-map mode) controller-id axis)))
    (and handler (handler mode value))))

(define-class <major-mode> (<mode>))

(define-class <minor-mode> (<mode>))

(define-class <nothing-mode> (<major-mode>))