' 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$" 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 valdest 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