Add Licensing and Contributing
[clnl] / src / main / clnl-gltk / base.lisp
1 ; Copyright 2022 Frank Duncan (frank@consxy.com) under AGPL3.  See distributed LICENSE.txt.
2 (in-package #:clnl-gltk)
3
4 (defgeneric render (item)
5  (:documentation
6   "RENDER ITEM => RESULT
7
8 ARGUMENTS AND VALUES:
9
10   ITEM: item to be rendered
11   RESULT: undefined
12
13 DESCRIPTION:
14
15   RENDER is the entry point for rendering different items in the
16   CLNL-GLTK package.
17
18   RENDER will return the opengl world to the way it found it after
19   finishing, usually via just popping the matrix."))
20
21 (defgeneric resize (item width height)
22  (:documentation
23   "RESIZE ITEM WIDTH HEIGHT => RESULT
24
25 ARGUMENTS AND VALUES:
26
27   ITEM: item to be rendered
28   WIDTH: an integer
29   HEIGHT: an integer
30   RESULT: undefined
31
32 DESCRIPTION:
33
34   RESIZE is the general purpose resizing entry point for all widgets.
35
36   WIDTH and HEIGHT are contextual to the actual item being resized, and
37   may be even be ignored."))
38
39 (defgeneric reposition (item x y)
40  (:documentation
41   "REPOSITION ITEM X Y => RESULT
42
43 ARGUMENTS AND VALUES:
44
45   ITEM: item to be rendered
46   X: an integer
47   Y: an integer
48   RESULT: undefined
49
50 DESCRIPTION:
51
52   REPOSITION is the general purpose resizing entry point for all widgets.
53
54   X and Y are contextual to the actual item being repositioned, and
55   may be even be ignored."))
56
57 (defgeneric mousemove (item x y)
58  (:documentation
59   "MOUSEMOVE ITEM X Y => RESULT
60
61 ARGUMENTS AND VALUES:
62
63   ITEM: item handling event
64   X: an integer
65   Y: an integer
66   RESULT: undefined
67
68 DESCRIPTION:
69
70   MOUSEMOVE is the general purpose mousemove entry point for all widgets.  It
71   is used to alert widgets to movements of the mouse, regardless of button state.
72
73   X and Y are absolute coordinates, and assumed to be opengl coordinates,
74   not window coordinates (meaning they match the render and setup functions
75   of widgets).
76
77   A catchall method that does nothing is also defined so that mouse functions
78   can loop over all available widgets and let them decide what they want to do."))
79
80 (defmethod mousemove (item x y))
81
82 (defgeneric mousedown (item x y)
83  (:documentation
84   "MOUSEDOWN ITEM X Y => RESULT
85
86 ARGUMENTS AND VALUES:
87
88   ITEM: item handling event
89   X: an integer
90   Y: an integer
91   RESULT: undefined
92
93 DESCRIPTION:
94
95   MOUSEDOWN is the general purpose mousedown entry point for all widgets.  It
96   is used to alert widgets that a mouse button has been pressed, and where.
97   There's no information on which button has been pressed.
98
99   X and Y are absolute coordinates, and assumed to be opengl coordinates,
100   not window coordinates (meaning they match the render and setup functions
101   of widgets).
102
103   A catchall method that does nothing is also defined so that mouse functions
104   can loop over all available widgets and let them decide what they want to do."))
105
106 (defmethod mousedown (item x y))
107
108 (defgeneric mouseup (item x y)
109  (:documentation
110   "MOUSEUP ITEM X Y => RESULT
111
112 ARGUMENTS AND VALUES:
113
114   ITEM: item handling event
115   X: an integer
116   Y: an integer
117   RESULT: undefined
118
119 DESCRIPTION:
120
121   MOUSEUP is the general purpose mouseup entry point for all widgets.  It
122   is used to alert widgets to that a mouse button has been released, and where.
123   There's no information on which button has been released, and it is
124   up to the widget to decide if a click was triggered.
125
126   X and Y are absolute coordinates, and assumed to be opengl coordinates,
127   not window coordinates (meaning they match the render and setup functions
128   of widgets).
129
130   A catchall method that does nothing is also defined so that mouse functions
131   can loop over all available widgets and let them decide what they want to do."))
132
133 (defmethod mouseup (item x y))
134
135 (defgeneric toggle (item &optional state)
136  (:documentation
137   "TOGGLE ITEM &optional STATE => NEW-STATE
138
139 ARGUMENTS AND VALUES:
140
141   ITEM: an item
142   STATE: a boolean, the state to set to
143   NEW-STATE: a boolean, the state after being set
144
145 DESCRIPTION:
146
147   TOGGLE toggles an ITEM.
148
149   Inverts the current toggle status of the ITEM if no STATE
150   passed in, otherwise sets to STATE."))
151
152 ; Stick utilities here for now
153 (defun draw-border (x1 y1 x2 y2 &optional (line-width 1f0))
154  (let
155   ((offset (/ (1- line-width) 2)))
156   (gl:line-width line-width)
157   (gl:begin :lines)
158   (gl:vertex x1 y1)
159   (gl:vertex (+ x2 offset) y1)
160   (gl:vertex x2 y1)
161   (gl:vertex x2 (+ y2 offset))
162   (gl:vertex x2 y2)
163   (gl:vertex (- x1 offset) y2)
164   (gl:vertex x1 y2)
165   (gl:vertex x1 (- y1 offset))
166   (gl:end)))