3D Image Button gadget

Everything related to 3D programming
Didaktik
User
User
Posts: 79
Joined: Fri Mar 14, 2014 2:12 pm

3D Image Button gadget

Post by Didaktik »

To build a more or less serious GUI is very necessary Image Button Gadget. In CEGUI does not. So as CEGUI does not track clicks on a lot of gadgets, such as ImageGadget.

There are some ways to fix it?
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: 3D Image Button gadget

Post by applePi »

Hi Didaktik
if you can't find a 3D Image Button gadget, then i suggest to use the 3D objects as buttons (spheres, cubes, ...). we only picked the body of the object, then we will know its name, then we add code .

Code: Select all

If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
        InputEvent3D(MouseX(), MouseY(),0)
        
        ent = BodyPick(CameraID(0), MouseButton(#PB_MouseButton_Left), MouseX(), MouseY(), 1)
        If ent = 3
          Sleep_(200)
          x.f+1:y.f=0: ScrollMaterial(3,x,y,#PB_Material_Animated)
          x=0
          rotate ! 1
          
         ElseIf ent = 2: Debug "ok"
                
        EndIf
        
 EndIf
we must use sleep_(200) or what is suitable for the system.
but i prefer if there is MouseUp function , since MouseButton can have many moments while pressing the Button.

suitable for static scenes, but for a movable scenes we must reposition these Buttons, haven't tried it.

needs PB 5.50 (for the BodyPick() function
press the Sphere Button to toggle ON/OFF rotation of the graphics shape,

Code: Select all

;
; ------------------------------------------------------------
;
;   PureBasic - CompoundBody
;
;    (c) Fantaisie Software
;
; ------------------------------------------------------------
Declare DrawCurve()

IncludeFile #PB_Compiler_Home + "examples/3d/Screen3DRequester.pb"

#CameraSpeed = 1

Define.f KeyX, KeyY, MouseX, MouseY


If InitEngine3D()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/"              , #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures"  , #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Models"    , #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts"    , #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/GUI"        , #PB_3DArchive_FileSystem)
    Parse3DScripts()
    
    ;WorldDebug(#PB_World_DebugBody)
    
     ;-------------------------------
    ; create  material
    CreateMaterial(1, LoadTexture(1, "White.jpg"))
    SetMaterialColor(1, #PB_Material_AmbientColor, #PB_Material_AmbientColors)
    ; 
    CreateMaterial(2, LoadTexture(2, "dirt.jpg"))
    SetMaterialColor(2, #PB_Material_AmbientColor, #PB_Material_AmbientColors)
    
    CreateMaterial(3, LoadTexture(3, "ground_diffuse.png"))
    SetMaterialColor(3, #PB_Material_AmbientColor, #PB_Material_AmbientColors)
    
    CreateMaterial(4, LoadTexture(4, "snow_1024.jpg"))
    CreateMaterial(5, LoadTexture(5, "Geebee2.bmp"))
    
    ;-------------------------------
    CreateCube(1, 1.0)
    CreateSphere(3, 2)
           
    DrawCurve()
    ;Buttons Ground
    ;
    Ground = CreateEntity(#PB_Any, MeshID(1), MaterialID(4), -11,11 , 0)
    ScaleEntity(Ground, 12, 0.4, 6)
    RotateEntity(Ground, 90,0,0)
    CreateEntityBody(Ground, #PB_Entity_StaticBody)
    
    ;buttons:
    CreateCube(2, 3)
    CreateEntity(2, MeshID(2), MaterialID(5),  -8,  11, 0)
    CreateEntityBody(2, #PB_Entity_BoxBody, 0, 0.5, 0.5)
    CreateEntity(3, MeshID(3), MaterialID(3),  -14,  11, 0)
    CreateEntityBody(3, #PB_Entity_SphereBody, 0, 0.5, 0.5)
    
   
    ; camera
    ;
    CreateCamera(0, 0, 0, 100, 100, #True)
    MoveCamera(0,0,2,30, #PB_Absolute)
    CameraBackColor(0, RGB(200,200,100))
    
    ; GUI
    OpenWindow3D(0, 0, 0, 50 , 10 , "")
    HideWindow3D(0,1)
    ShowGUI(128, 1) ; Display the GUI, semi-transparent and display the mouse cursor
        
    Repeat
      Screen3DEvents()
      
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
        InputEvent3D(MouseX(), MouseY(),0)
        
        ent = BodyPick(CameraID(0), MouseButton(#PB_MouseButton_Left), MouseX(), MouseY(), 1)
        If ent = 3
          Sleep_(200)
          x.f+1:y.f=0: ScrollMaterial(3,x,y,#PB_Material_Animated)
          x=0
          rotate ! 1
          
         ElseIf ent = 2: Debug "ok"
          
                    
        EndIf
        
      EndIf
    
      If ExamineKeyboard()
        
          
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed
        Else
          KeyY = 0
        EndIf
        
      EndIf
      
  
      
      MoveCamera  (0, KeyX, 0, KeyY)
      ;RotateCamera(0,  MouseY, MouseX, 0, #PB_Relative)
      If rotate = 0 
        ScrollMaterial(3,0,0,#PB_Material_Animated)
      EndIf
      
      RotateEntity(0, 0,rotate, 0, #PB_Relative)
      
      RenderWorld()
      
      FlipBuffers()
      
      
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
    
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf

End

Procedure DrawCurve()
  Protected.f a, b, x, y, z, t
  Protected n.l
a.f = 0.2 :b.f=0.8 : n=20: t.f

CreateMesh(0, #PB_Mesh_LineStrip, #PB_Mesh_Static) ; #PB_Mesh_LineStrip to joint the points with lines

While t <= 2*#PI
          x = Cos(t) - Cos(6* t)/2 + Sin(14* t)/3
          y = Cos(14* t)/3 + Sin(t)- Sin(6* t)/2 
          z = 0
                          
        MeshVertexPosition(x, y, z)
        MeshVertexColor(RGB(0,0,255))
      
    t + 0.01
  Wend
  ;************************************************************
 FinishMesh(#True)
  SetMeshMaterial(0, MaterialID(1))
  CreateEntity(0, MeshID(0), #PB_Material_None ,0,0,0)
  
  ScaleEntity(0,4,4,4) 

EndProcedure  

User avatar
DK_PETER
Addict
Addict
Posts: 898
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: 3D Image Button gadget

Post by DK_PETER »

Code: Select all

;I very seldom use the CEGUI and normally stick to sprites only.
;Anyway, here's a very simple approach, which uses the ImageGadget3D()
;as an ImageButton.
;You need to use LoadTexture() for each image. You can't do a CreateImage()
;and then a CreateTexture() -  and then assign it to the gadget.
;If you create your images with code, you must save them and then reload using LoadTexture().
;Remember to use Add3DArchive(Path,Type) to point to the location of the images you wish to use.

InitEngine3D()
InitSprite()
InitKeyboard()
InitMouse()

Structure _iButton
  id.i
  tx.i[2]
  x.i
  y.i
EndStructure

Structure _Base
  ca.i
  sc.i
  wi.i
  wD3.i
  w.i
  h.i
  ev.i
  ibut._iButton
  pressed.i
EndStructure

#ButtonSizeWidth  = 200
#ButtonSizeHeight = 100

Declare.i GetImages(Text.s = "Button", Fnt.i = -1) 
Declare.i Window(Width.i = 1024, Height.i = 768)
Declare.i Main()
Declare.i CheckArea()

Global b._Base

Procedure.i Window(Width.i = 1024, Height.i = 768)
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/GUI", #PB_3DArchive_FileSystem)
  Add3DArchive(".", #PB_3DArchive_FileSystem)

  ExamineDesktops()
  If Width = #PB_Any : b\w = DesktopWidth(0) : Else : b\w = Width : EndIf
  If Height = #PB_Any : b\h = DesktopHeight(0) : Else : b\h = Height : EndIf
  
  b\wi = OpenWindow(#PB_Any, 0, 0, b\w, b\h, "Test", #PB_Window_ScreenCentered)
  b\sc = OpenWindowedScreen(WindowID(b\wi), 0, 0, b\w, b\h)
  b\ca = CreateCamera(#PB_Any, 0, 0, 100, 100)
  
  b\pressed = #False
EndProcedure


Procedure.i Main()
  Protected ret.i = getimages()
  
  b\wD3 = OpenWindow3D(#PB_Any, 0, 0, b\w/2 , b\h/2 , "Image button test")
  b\ibut\id = ImageGadget3D(#PB_Any, 10, 10, #ButtonSizeWidth, #ButtonSizeHeight, TextureID(b\ibut\tx[0]))
  b\ibut\x = 10 : b\ibut\y = 10
  
  Repeat
    
    Repeat
      b\ev = WindowEvent()
    Until b\ev = 0
    
    ExamineMouse()
  
    If MouseButton(#PB_MouseButton_Left) = 1 And b\pressed = #False
      If CheckArea() = #True
        InputEvent3D(MouseX(), MouseY(), 1)
        SetGadgetState3D(b\ibut\id, TextureID(b\ibut\tx[1]))
        b\pressed = #True
      Else
        InputEvent3D(MouseX(), MouseY(), 0)
      EndIf
    ElseIf MouseButton(#PB_MouseButton_Left) = 0 And b\pressed = #True
      InputEvent3D(MouseX(), MouseY(), 0)
      SetGadgetState3D(b\ibut\id, TextureID(b\ibut\tx[0]))
      b\pressed = #False
    Else
      InputEvent3D(MouseX(), MouseY(), 0)
    EndIf      

    RenderWorld()
    
    FlipBuffers()
    
    ExamineKeyboard()

  Until KeyboardPushed(#PB_Key_Escape)
EndProcedure

Procedure.i CheckArea() 
  Protected Value.i = #False
  If MouseX() > b\ibut\x And MouseX() < b\ibut\x + #ButtonSizeWidth
    If MouseY()> b\ibut\y And MouseY() < b\ibut\y + #ButtonSizeHeight
      Value = #True
    EndIf
  EndIf
  ProcedureReturn Value
EndProcedure

Procedure.i GetImages(Text.s = "Button", Fnt.i = -1) ;Make a simple button
  Protected file.s = ""
  Protected im.i, tx.i
    im = CreateImage(#PB_Any, #ButtonSizeWidth, #ButtonSizeHeight, 32)
    StartDrawing(ImageOutput(im))
    DrawingMode(#PB_2DDrawing_Default)
    RoundBox(0, 0, #ButtonSizeWidth, #ButtonSizeHeight, 4, 4, $DCD8D4)
    DrawingMode(#PB_2DDrawing_Outlined)
    RoundBox(0, 0, #ButtonSizeWidth, #ButtonSizeHeight, 4, 4, $969999)
    DrawingMode(#PB_2DDrawing_Transparent)
    If Fnt > -1  : DrawingFont(FontID(Fnt)) : EndIf ;Default font or...
    DrawText((#ButtonSizeWidth / 2) - (TextWidth(Text)/2) , (#ButtonSizeHeight / 2) - (TextHeight(Text)/2), Text, $2A2A2A)
    StopDrawing()
    SaveImage(im, "Down.bmp", #PB_ImagePlugin_BMP)
    FreeImage(im)
    
    im = CreateImage(#PB_Any, #ButtonSizeWidth, #ButtonSizeHeight, 32)
    StartDrawing(ImageOutput(im))
    DrawingMode(#PB_2DDrawing_Default)
    RoundBox(0, 0, #ButtonSizeWidth, #ButtonSizeHeight, 4, 4, $D5D5D5)
    DrawingMode(#PB_2DDrawing_Outlined)
    RoundBox(0, 0, #ButtonSizeWidth, #ButtonSizeHeight, 4, 4, $5B2C46)
    DrawingMode(#PB_2DDrawing_Transparent)
    If Fnt > -1  : DrawingFont(FontID(Fnt)) : EndIf ;Default font or...
    DrawText((#ButtonSizeWidth / 2) - (TextWidth(Text)/2)+2 , (#ButtonSizeHeight / 2) - (TextHeight(Text)/2)+2, Text, $8A8D8A)
    StopDrawing()
    SaveImage(im, "Up.bmp", #PB_ImagePlugin_BMP)
    FreeImage(im)
   
    b\ibut\tx[0] = LoadTexture(#PB_Any, "Down.bmp")
    b\ibut\tx[1] = LoadTexture(#PB_Any, "Up.bmp")
    ProcedureReturn #True
  EndProcedure
  

  Window()
  Main()
Bye
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
Didaktik
User
User
Posts: 79
Joined: Fri Mar 14, 2014 2:12 pm

Re: 3D Image Button gadget

Post by Didaktik »

DK_PETER, thx!

But this code not work in ScrollAread3D. And it will be very difficult to alter it for ScrollArea.
siesit
User
User
Posts: 12
Joined: Fri Aug 21, 2009 8:40 am
Location: rus
Contact:

Re: 3D Image Button gadget

Post by siesit »

Faced the same problem. Requires either "ImageButtonGadget3d" or event tracking (click) on ImageGadget3d
site created by purebasic work-flow-Initiative
Post Reply