Hi Guys, Sasuke here with a quick small On Demand Colour Picker.
It uses an on demand approach to displaying and controlling the GUI and one main advantage of this is you don't have to store hardly anything, only what you need and you only use it when and where you need it. It currently uses Matrix1Utils and Adcanced2d plugins but I could make a version without and or improve upon the functionality of this one if request. The code is simple to expand upon so please use at your leisure. Enjoy
// core UDT for the On Demand Gui
type onDemandDirector
click as integer
oldClick as integer
mousePosX as integer
mousePosY as integer
dragX as integer
dragY as integer
heldCall as integer
currentCall as integer
// outputs
x as integer
y as integer
w as integer
h as integer
endtype
global _onDemandDirector as onDemandDirector
type colorPicker
windowPosX as integer
windowPosY as integer
currentColor as dword
R as float
G as float
B as float
A as float
endtype
global _colorPicker as colorPicker
// setup screen
sync on : sync rate 60
set text font "Tahoma" : set text size 13
color backdrop rgb(128,128,128)
// set initals for out color picker
_colorPicker.windowPosX = 128
_colorPicker.windowPosY = 128
_colorPicker.currentColor = 0
_colorPicker.R = 255
_colorPicker.G = 255
_colorPicker.B = 255
_colorPicker.A = 255
// create and object to apply color to
make object cube 1,1
set object ambient 1,0
do
// rotate it abit so it's not so boring
r# = wrapvalue(r#+0.25)
rotate object 1,r#,r#,0
// update its color
set object diffuse 1,_colorPicker.currentColor
set alpha mapping on 1,_colorPicker.A/2.5
// main update
updateOnDemand() // - this first if your gonna use a OD GUI
onDemandColorPicker()
sync
loop
// this function combines gui element for out color picker, so all the functionality and everything is all in here
function onDemandColorPicker()
// get some values
windowWidth = 146 : windowHeight = 256
_colorPicker.currentColor = set alpha( rgb(_colorPicker.R,_colorPicker.G,_colorPicker.B),_colorPicker.A)
// create draggable window
onDemandWindow( _colorPicker.windowPosX, _colorPicker.windowPosY, windowWidth, windowHeight, "Color Picker" )
_colorPicker.windowPosX = getOnDemandOutX() : _colorPicker.windowPosY = getOnDemandOutY()
// draw color boxes
x = _colorPicker.windowPosX+32 : y = _colorPicker.windowPosY+32
w = 84 : h = 84
a2fillbox x,y,x+w,y+h,rgb(0,0,0)
a2fillbox x+1,y+1,x+w/2-1,y+h-1,rgb(255,255,255)
a2fillbox x+1,y+1,x+w,y+h,_colorPicker.currentColor
// draw some progress bars
x = _colorPicker.windowPosX + 32 : y = _colorPicker.windowPosY + 136 : w = 84
_colorPicker.R = onDemandProgressBar( x, y, w, 255, 0, _colorPicker.R, "R" )
inc y, 28
_colorPicker.G = onDemandProgressBar( x, y, w, 255, 0, _colorPicker.G, "G" )
inc y, 28
_colorPicker.B = onDemandProgressBar( x, y, w, 255, 0, _colorPicker.B, "B" )
inc y, 28
_colorPicker.A = onDemandProgressBar( x, y, w, 255, 0, _colorPicker.A, "A" )
endfunction
// on demand functions
function updateOnDemand()
_onDemandDirector.oldClick = _onDemandDirector.click
_onDemandDirector.click = mouseclick()
_onDemandDirector.mousePosX = mousex()
_onDemandDirector.mousePosY = mousey()
_onDemandDirector.currentCall = 0
if _onDemandDirector.click = 0 then _onDemandDirector.heldCall = -1
_onDemandDirector.x = 0
_onDemandDirector.y = 0
_onDemandDirector.w = 0
_onDemandDirector.h = 0
endfunction
function onDemandWindow( x, y, w, h, caption as string )
// bounds check
if _onDemandDirector.mousePosX > x and _onDemandDirector.mousePosY > y
if _onDemandDirector.mousePosX < (x+w) and _onDemandDirector.mousePosY < (y+26)
if _onDemandDirector.click <> 0 and _onDemandDirector.click <> _onDemandDirector.oldClick
if _onDemandDirector.heldCall < 0
_onDemandDirector.heldCall = _onDemandDirector.currentCall
_onDemandDirector.dragX = _onDemandDirector.mousePosX - x
_onDemandDirector.dragY = _onDemandDirector.mousePosY - y
endif
endif
endif
endif
// window dragging
if _onDemandDirector.heldCall = _onDemandDirector.currentCall
x = _onDemandDirector.mousePosX - _onDemandDirector.dragX
y = _onDemandDirector.mousePosY - _onDemandDirector.dragY
endif
// keep window within client
x = clamp( x, 0, screen width()-w )
y = clamp( y, 0, screen height()-h )
// render window
a2box x,y,x+w,y+h,rgb(0,0,0)
a2fillbox x+1,y+1,x+w,y+h,rgb(51,51,51)
a2fillbox x+1,y+1,x+w,y+12,rgb(64, 64, 64),rgb(64, 64, 64),rgb(51,51,51),rgb(51,51,51)
ink rgb(255,255,255) : text x+11,y+6,caption
_onDemandDirector.x = x : _onDemandDirector.y = y
inc _onDemandDirector.currentCall
endfunction
// notes
// MaxV is the upperlimit from 0, haven't added negative numbers yet
// percision is how many decial places, so if you want a range from 0.0 to 1.0 you'd set percision to 1 and so on...
function onDemandProgressBar( x, y, w, maxV as float, percision as integer, currentV as float, caption as string )
// bounds check
if _onDemandDirector.mousePosX > x and _onDemandDirector.mousePosY > y
if _onDemandDirector.mousePosX < (x+w) and _onDemandDirector.mousePosY < (y+15)
if _onDemandDirector.click <> 0 and _onDemandDirector.click <> _onDemandDirector.oldClick
if _onDemandDirector.heldCall < 0
_onDemandDirector.heldCall = _onDemandDirector.currentCall
endif
endif
endif
endif
// progress bar dragging
if _onDemandDirector.heldCall = _onDemandDirector.currentCall
currentV = (clamp(_onDemandDirector.mousePosX-x,0,w)*maxV)/w
endif
// render progress bar
a2box x,y,x+w,y+15,rgb(0,0,0)
a2fillbox x+1,y+1,x+w,y+15,rgb(26,26,26),rgb(26,26,26),rgb(58,58,58),rgb(58,58,58)
if currentV > 0.0
a2fillbox x+1,y+1,x+(currentV*w)/maxV,y+15,rgb(145,109,13),rgb(145,109,13),rgb(108,78,14),rgb(108,78,14)
endif
center text x+(w/2),y+1,str$(currentV,percision)
ink rgb(255,255,255)
text x-6-text width(caption),y+1,caption
inc _onDemandDirector.currentCall
endfunction currentV
function getOnDemandOutX()
endfunction _onDemandDirector.x
function getOnDemandOutY()
endfunction _onDemandDirector.y
Notes: The window is drag-able and you can change the range of the progress bars via function creation. This is handy if you want a range between 0.00 and 1.00 because shaders don't use 0-255.
"Get in the Van!" - Van B