Post by Adam on Jun 20, 2011 2:23:58 GMT -5
I had an idea for some decent gui controls last night and so i made a paint program to showcase it, but I decided i would go ahead and post it now before it starts getting complicated and while it is still independent of libraries or other files.
Middle Button is an airbrush tool and right click is the color picker tool, other than those ui-less features you can figure the rest out by playing with the circle in the top right of the program (just haven't figured out how i want to design the ui for those functions yet)
Middle Button is an airbrush tool and right click is the color picker tool, other than those ui-less features you can figure the rest out by playing with the circle in the top right of the program (just haven't figured out how i want to design the ui for those functions yet)
Struc SMouse
pos(1) as single
ClickPos(2)(1) as single
Down(2)
Click(2)
released(2)
end struc
Dim Texture = GLGenTexture()
Dim &Image(), X, Y
Dim Offset
Dim SMouse Mouse
Dim Size as single = 7
Dim Color#(2) = vec3(.05,.05,.05)
Dim i
Const SizeX = 560
Const SizeY = 480
Alloc Image, SizeX*SizeY*4-1
Declare Sub UpdateImage()
Declare Function MouseOffset(X#, Y#)
glClearColor(.3,.3,.3,1)
GLDisable(GL_DEPTH_TEST)
glEnable (GL_TEXTURE_2D)
glMatrixMode (GL_PROJECTION)
glLoadIdentity ()
glOrtho(0, sizeX+80, SizeY, 0, 1, -1)
glMatrixMode (GL_MODELVIEW)
glpointsize(2)
sub drawpie(x,y,size,arc,arc2)
size = size/2
glBegin(GL_POLYGON)
glvertex2f(x,y)
GLVertex2f(x,y+size)
for i = 0 to arc step 3
glvertex2f(-cosd(i-90)*size+x,-sind(i-90)*size+y)
next
glvertex2f(x,y)
glEnd()
glBegin(GL_LINE_STRIP)
glvertex2f(x,y)
GLVertex2f(x,y+size)
for i = 0 to arc2 step 7
glvertex2f(-cosd(i-90)*size+x,-sind(i-90)*size+y)
next
glvertex2f(x,y)
glEnd()
end sub
sub DrawQuad(x,y,w,h,r)
glPushMatrix()
glTranslatef(x,y,0)
glScalef(w,h,0)
glRotatef(r,0,0,1)
GLBegin(GL_LINE_LOOP)
GLTexCoord2F(0,0):GLVertex2F(-.5,.5)
GLTexCoord2F(0,1):GLVertex2F(-.5,-.5)
GLTexCoord2F(1,1):GLVertex2F(.5,-.5)
GLTexCoord2F(1,0):GLVertex2F(.5,.5)
GLEnd()
GLPopMatrix()
end sub
sub DrawSpray(x,y,w,h,r)
glPushMatrix()
glTranslatef(x,y,0)
glScalef(w,h,0)
glRotatef(r,0,0,1)
GLBegin(GL_Points)
for i = 0 to size
glVertex2f(rnd()%25/25.0-.5,rnd()%25/25.0-.5)
next
GLEnd()
GLPopMatrix()
end sub
for i = 0 to arraymax(image)
image(i) = 255
next
UpdateImage()
Function Angle2#(x1#,y1#,x2#,y2#)
if x1#-x2# = 0 and y1#-y2# = 0 then return 0 endif
Return 180-ATN2D(x2#-x1#, y2#-y1#)
EndFunction
function circlecollision(x2,y2,x1,y1,distance)
return sqrt(pow(x1-x2,2)+pow(y1-y2,2)) <= distance
end function
sub UpdateMouse()
mouse.pos = Vec2(Mouse_X()*(sizex+80),Mouse_Y()*sizey)
For i = 0 to 2
if mouse_button(i) then
if not mouse.down(i) then
mouse.click(i) = true
mouse.down(i) = true
Mouse.ClickPos(i) = mouse.pos
else
mouse.click(i) = false
endif
else
mouse.released(i) = false
if mouse.down(i) then
mouse.released(i) = true
endif
mouse.click(i) = false
mouse.down(i) = false
endif
next
end sub
Sub ToolBar()
glcolor3f(.7,.7,.7)
drawpie(sizex+40,45,75,size*5,color#(0)*360)
glcolor3f(1,0,0)
drawpie(sizex+40,45,60,color#(0)*360,color#(1)*360)
glcolor3f(0,1,0)
drawpie(sizex+40,45,45,color#(1)*360,color#(2)*360)
glcolor3f(0,0,1)
drawpie(sizex+40,45,30,color#(2)*360,360)
glcolor3fv(color#)
drawpie(sizex+40,45,15,360,0)
glcolor3f(.2,.2,.2)
DrawQuad(sizex+40,45,size,size,performancecounter()/25)
glColor3f(1,1,1)
End Sub
Sub ToolbarCollision()
if Mouse.down(0) then
if circleCollision(mouse.Clickpos(0)(0),mouse.Clickpos(0)(1),sizex+40,45,7.5) then
elseif circleCollision(mouse.Clickpos(0)(0),mouse.Clickpos(0)(1),sizex+40,45, 15) then
color#(2) = angle2#(mouse.pos(0),mouse.pos(1),sizex+40,45)/360
elseif circleCollision(mouse.Clickpos(0)(0),mouse.Clickpos(0)(1),sizex+40,45, 22.5) then
color#(1) = angle2#(mouse.pos(0),mouse.pos(1),sizex+40,45)/360
elseif circleCollision(mouse.Clickpos(0)(0),mouse.Clickpos(0)(1),sizex+40,45, 30) then
color#(0) = angle2#(mouse.pos(0),mouse.pos(1),sizex+40,45)/360
elseif circleCollision(mouse.Clickpos(0)(0),mouse.Clickpos(0)(1),sizex+40,45, 45) then
size = angle2#(mouse.pos(0),mouse.pos(1),sizex+40,45)/5
endif
endif
end sub
Do
GLClear(GL_COLOR_BUFFER_BIT)
GLLoadIdentity()
UpdateMouse()
if mouse.pos(0) > Sizex then ToolBarCollision()
else
IF Mouse.down(0) and mouse.clickpos(0)(0) < 560 Then
For X = -Size/2 To Size/2
For Y = -Size/2 To Size/2
Offset = MouseOffset(mouse.pos(0)+X,mouse.pos(1)+Y)
IF OffSet+2 < ArrayMax(Image) And OffSet > 0 Then
Image(Offset ) = Color#(0)*255
Image(Offset+1) = Color#(1)*255
Image(Offset+2) = Color#(2)*255
Endif
Next
Next
UpdateImage()
elseIF Mouse.down(2) and mouse.clickpos(2)(0) < 560 Then
For X = -Size/2 To Size/2
For Y = -Size/2 To Size/2
Offset = MouseOffset(mouse.pos(0)+X,mouse.pos(1)+Y)
IF OffSet+2 < ArrayMax(Image) And OffSet > 0 and rnd()%25=1 Then
Image(Offset ) = Color#(0)*255
Image(Offset+1) = Color#(1)*255
Image(Offset+2) = Color#(2)*255
Endif
Next
Next
UpdateImage()
elseif Mouse.down(1) then
Offset = MouseOffset(mouse.pos(0),mouse.pos(1))
Color#(0) = Image(Offset )/255.0
Color#(1) = Image(Offset+1)/255.0
Color#(2) = Image(Offset+2)/255.0
Endif
endif
Size = Size + Mouse_Wheel()
GLBegin(GL_Quads)
GLTExCoord2F(0,0):GLVertex2F( 0 , 0)
GLTExCoord2F(0,1):GLVertex2F( 0 , sizey)
GLTExCoord2F(1,1):GLVertex2F( sizex, sizey)
GLTExCoord2F(1,0):GLVertex2F( sizex, 0)
GLEnd()
glDisable(GL_TEXTURE_2D)
ToolBar()
glEnable(GL_TEXTURE_2D)
SwapBuffers()
Loop
Function MouseOffset(X#, Y#)
Return Int(Y#)*SizeX*4+Int(X#)*4
End Function
Sub UpdateImage()
glBindTexture(GL_TEXTURE_2D, Texture)
glTexImage2D(GL_TEXTURE_2D, 0, 4, SizeX, SizeY, 0, GL_RGBA, GL_UNSIGNED_BYTE, Image)
glTexParameterF(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameterF(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
End Sub