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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
' Copyright (c) 2006 Ian Cowburn
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of
' this software and associated documentation files (the "Software"), to deal in
' the Software without restriction, including without limitation the rights to
' use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
' of the Software, and to permit persons to whom the Software is furnished to do
' so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all
' copies or substantial portions of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
' SOFTWARE.
'
' $Id$
'
Rem
bbdoc: noddybox.gfxmenu
EndRem
Module noddybox.gfxmenu
ModuleInfo "Framework: Simple Graphical Menu"
ModuleInfo "Copyright: Ian Cowburn -- released under the MIT License"
ModuleInfo "Author: Ian Cowburn"
ModuleInfo "Version: $Revision$"
Strict
Import brl.linkedlist
Import brl.max2d
Rem
bbdoc: Allows a menu backdrop to be automatically processed. Derive a class from this to use.
EndRem
Type TGfxMenuBackdrop Abstract
Rem
bbdoc: Create a menu backdrop element.
returns: The created elemnt.
about: Notice that this is a method, rather than the usual function creator.
EndRem
Method Create:TGfxMenuBackdrop() Abstract
Rem
bbdoc: Called by the menu to update the backdrop.
EndRem
Method Update() Abstract
End Type
Rem
bbdoc: Defines a graphical menu.
EndRem
Type TGfxMenu
Field list:TList
Field bdrop:TList
Field mbdown:Int
Field r:Int
Field g:Int
Field b:Int
Field over_r:Int
Field over_g:Int
Field over_b:Int
Field fade:Int
Rem
bbdoc: Create a menu.
returns: The created menu.
about: @backdrop is the backdrop to create (null for no backdrop). @num is the number of backdrop items to create.
SetColor() is called with @r, @g and @b when the mouse is not over an item, overwise SetColor() is
called with @over_r, @over_g and @over_b when the item is active. @fade is the amount colours will alter as the
mouse hovers over buttons. Set this to 255 to make colour changes instant.
EndRem
Function Create:TGfxMenu(r:Int=164, g:Int=164, b:Int=164, over_r:Int=255, over_g:Int=255, over_b:Int=255, fade:Int=5, backdrop:TGfxMenuBackdrop=Null, num:Int=0)
Local menu:TGfxMenu=New TGfxMenu
menu.list=New TList
menu.bdrop=New TList
menu.mbdown=False
menu.r=r
menu.g=g
menu.b=b
menu.over_r=over_r
menu.over_g=over_g
menu.over_b=over_b
menu.fade=fade
If backdrop
For Local f:Int=0 Until num
menu.bdrop.AddLast(backdrop.Create())
Next
EndIf
Return menu
End Function
Rem
bbdoc: Adds a menu item.
returns: The created menu.
about: @x and @y are the position to draw the image at. If @x is -1 then the image is centred. @i is the image.
@id is the value returned from @Render() for this menu item. Don't use -1 for this!
EndRem
Method Add(x:Int, y:Int, i:TImage, id:Int)
list.AddLast(TGfxMenuOpt.Create(x,y,i,id,r,g,b))
End Method
Rem
bbdoc: Renders and updates the menu.
returns: The selected item, or -1 if nothing clicked.
about: Set @hide to True to hide the menu (the backdrop is still updated). SetColor() may have changed when this routine exits.
EndRem
Method Render:Int(hide:Int)
For Local p:TGfxMenuBackdrop=EachIn bdrop
p.Update()
Next
If hide
Return -1
EndIf
Local in:Int=-1
Local mx:Int=MouseX()
Local my:Int=MouseY()
Local any:Int=False
For Local opt:TGfxMenuOpt=EachIn list
If opt.InBox(mx,my)
opt.r=Towards(over_r,opt.r)
opt.g=Towards(over_g,opt.g)
opt.b=Towards(over_b,opt.b)
SetColor(opt.r,opt.g,opt.b)
If MouseDown(1)
mbdown=True
DrawImage(opt.i,opt.x,opt.y+2)
any=True
Else
DrawImage(opt.i,opt.x,opt.y)
If mbdown
in=opt.id
EndIf
mbdown=False
EndIf
Else
opt.r=Towards(r,opt.r)
opt.g=Towards(g,opt.g)
opt.b=Towards(b,opt.b)
SetColor(opt.r,opt.g,opt.b)
DrawImage(opt.i,opt.x,opt.y)
EndIf
Next
If Not any
mbdown=False
EndIf
Return in
End Method
Method Towards:Int(dest:Int, val:Int)
If val<dest
Return Min(dest,val+fade)
ElseIf val>dest
Return Max(dest,val-fade)
EndIf
Return val
End Method
End Type
Private
Type TGfxMenuOpt
Field x:Int
Field y:Int
Field i:TImage
Field id:Int
Field r:Int
Field g:Int
Field b:Int
Function Create:TGfxMenuOpt(x:Int, y:Int, i:TImage, id:Int, r:Int, g:Int, b:Int)
Local o:TGfxMenuOpt=New TGfxMenuOpt
If x=-1
x=(GraphicsWidth()-ImageWidth(i))/2
EndIf
o.x=x
o.y=y
o.i=i
o.id=id
o.r=r
o.g=g
o.b=b
Return o
End Function
Method InBox:Int(mx:Int, my:Int)
Return mx>=x And my>=y And mx<=x+ImageWidth(i) And my<=y+ImageHeight(i)
End Method
End Type
|