It is currently Wed May 22, 2013 2:10 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Detecting dynamically created imagegadget click?
PostPosted: Mon May 28, 2012 8:48 am 
Offline
PureBasic Protozoa
PureBasic Protozoa
User avatar

Joined: Fri Apr 25, 2003 3:08 pm
Posts: 3011
With the image strip code below, I am dynamically creating an imagegadget each time.

Am I right in thinking that I cannot somehow detect imagegadget clicks on each image somehow now in the event loop??

I know I can put each handle into a list as they are created and use physical image area detection (look up handle in list, find out what area it occupies, check bounds and return 'found') but is that the best way?

Code:
;----------------------------------------------------------
; Load our image decoders
;----------------------------------------------------------

UseJPEGImageDecoder()
UsePNGImageDecoder()

;----------------------------------------------------------
; Window constant
;----------------------------------------------------------

Enumeration 1
  #Window_Scrolla
EndEnumeration

#WindowIndex = #PB_Compiler_EnumerationValue

;----------------------------------------------------------
; Gadget constants
;----------------------------------------------------------

Enumeration 1
  ; Window_Scrolla
  #Gadget_Scrolla_sPreview
  #Gadget_Scrolla_Area18
  #Gadget_Scrolla_cControl
  #Gadget_Scrolla_Area22
  #Gadget_Scrolla_Load
  #Gadget_Scrolla_Exit
EndEnumeration

#GadgetIndex = #PB_Compiler_EnumerationValue

;----------------------------------------------------------
; Image constants
;----------------------------------------------------------

Enumeration 1
  #Image_Scrolla_Temp      ; Temporary image variable for full scan
EndEnumeration

#ImageIndex = #PB_Compiler_EnumerationValue

;----------------------------------------------------------
; Main window
;----------------------------------------------------------

Procedure.l Window_Scrolla()
  If OpenWindow(#Window_Scrolla,84,60,157,655,"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_Invisible)
    SetWindowColor(#Window_Scrolla,$A2A2A2)
      ScrollAreaGadget(#Gadget_Scrolla_sPreview,5,5,145,575,126,575,5,#PB_ScrollArea_Flat|#PB_ScrollArea_BorderLess)
        SetGadgetColor(#Gadget_Scrolla_sPreview,#PB_Gadget_BackColor,$BFBFBF)
      CloseGadgetList()
      ContainerGadget(#Gadget_Scrolla_cControl,5,585,145,65,#PB_Container_Flat|#PB_Container_BorderLess)
        SetGadgetColor(#Gadget_Scrolla_cControl,#PB_Gadget_BackColor,$BFBFBF)
      ButtonGadget(#Gadget_Scrolla_Load,10,10,60,45,"Load image",#PB_Button_MultiLine)
        SetGadgetFont(#Gadget_Scrolla_Load,LoadFont(#Gadget_Scrolla_Load,"Comic Sans MS",10,0))
      ButtonGadget(#Gadget_Scrolla_Exit,70,10,60,45,"Exit program",#PB_Button_MultiLine)
        SetGadgetFont(#Gadget_Scrolla_Exit,LoadFont(#Gadget_Scrolla_Exit,"Comic Sans MS",10,0))
      CloseGadgetList()
      HideWindow(#Window_Scrolla,0)
      ProcedureReturn WindowID(#Window_Scrolla)
  EndIf
EndProcedure

;----------------------------------------------------------
; Program variables
;----------------------------------------------------------

Structure ProgramData
  QuitFlag.i          ; Program quit semaphore
  CurrentDir.s        ; The current directory we are working in
  ProgramVersion.s    ; Keep track of program version and compile version
 
  PreviewTop.i        ; Keep track of the horizontal preview position per image
  PreviewLeft.i       ; Keep track of the Preview left position
  AvailableScroll.i   ; Available scroll length to add gadgets to
  ImageCounter.i      ; Number of preview images in the scroll area before having to expand area
 
  PreviewWidth.i      ; Width of the preview image
  PreviewHeight.i     ; Height of the preview image
 
  LabelHeight.i       ; Height of the image label
EndStructure

;----------------------------------------------------------
; Declare your global structures
;----------------------------------------------------------

Global Program.ProgramData

;----------------------------------------------------------
; Setup directory variables
;----------------------------------------------------------

Program\CurrentDir      = GetCurrentDirectory()

;----------------------------------------------------------
; Initial scrollgadget positions
;----------------------------------------------------------

Program\PreviewTop      =  10     ; Initial position from top
Program\PreviewLeft     =  10     ; Preview left position
Program\AvailableScroll = 575     ; Total initial scroll space

Program\PreviewWidth    = 105     ; Preview image width
Program\PreviewHeight   = 105     ; Preview image height

Program\LabelHeight     =  20     ; Height of the image label

;----------------------------------------------------------
; Load a picture into the scrollareagadget dynamically
;----------------------------------------------------------

Procedure LoadAnotherImage()
 
  ; Ask to load a new picture into the image strip
 
  NewPicture.s = OpenFileRequester("Select picture to load", "", "Jpeg (*.jpg *.jpeg *.jfif)|*.jpg;*.jpeg;*.jfif|Bitmap (*.bmp)|*.bmp|Png (*.png)|*.png|", 0)
 
  ; Only proceed if we got a new picture
 
  If NewPicture.s
   
    ; Load a new picture into the image strip from any directory
   
    NewImageNumber.i = LoadImage(#PB_Any, NewPicture.s)
   
    ; Only proceed if we got a new image handle
   
    If NewImageNumber
       
      ; Resize the image to the needed preview size
     
      ResizeImage(NewImageNumber, Program\PreviewWidth, Program\PreviewHeight)
     
      ; Now open the gadget list to add a new image preview to it
     
      OpenGadgetList(#Gadget_Scrolla_sPreview)
     
        ; Create a new imagegadget in the scrollarea list 5 across, Program\PreviewWidth down
       
        NewImageGadget.i = ImageGadget(#PB_Any, Program\PreviewLeft, Program\PreviewTop, Program\PreviewWidth, Program\PreviewHeight, ImageID(NewImageNumber))
       
        ; Reset the next picture position to picture height + 5
       
        Program\PreviewTop + Program\PreviewHeight + 5
       
        ; Create a new label gadget at the bottom of the picture
       
        NewLabelGadget.i =  TextGadget(#PB_Any, Program\PreviewLeft, Program\PreviewTop, Program\PreviewWidth, Program\LabelHeight, "", #PB_Text_Center)
       
        ; Add the scroll gadget colour as the label background
 
        SetGadgetColor(NewLabelGadget.i, #PB_Gadget_BackColor, $BFBFBF)
       
        ;SetGadgetFont(NewLabelGadget.i, LoadFont(#PB_Any, "Comic Sans MS", 8, 0))
       
        SetGadgetText(NewLabelGadget.i, "Scan Image #" + Str(Program\ImageCounter + 1))
       
        ; Increment the position counter for the next preview    ; Height of image plus space plus label plus space
       
        Program\PreviewTop + 25
       
        ; Keep track of number of preview images in the scroll area. Add size of image after page 4 + space
       
        Program\ImageCounter + 1
       
        ; Let user know how many images are currently in the list
       
        SetWindowTitle(#Window_Scrolla, "Image: " + Str(Program\ImageCounter))
       
        ; If there are 5 previews in the list, time to start expanding the view area
       
        If Program\ImageCounter > = 5
         
          ; Height of the last image plus space plus label plus space
         
          Program\AvailableScroll + Program\PreviewHeight + 5 + Program\LabelHeight + 5
         
          ; Expand the scrolled area
         
          SetGadgetAttribute(#Gadget_Scrolla_sPreview, #PB_ScrollArea_InnerHeight, Program\AvailableScroll)
         
          ; Always keep the last image visible added
         
          SetGadgetAttribute(#Gadget_Scrolla_sPreview, #PB_ScrollArea_Y, Program\AvailableScroll)
         
        EndIf
       
        ; Close the scroll area gadget list
     
      CloseGadgetList()
       
    Else
 
      Debug "Cannot get new image number"
 
    EndIf
   
  Else
   
    Debug "Cannot load a picture or user cancelled"
   
  EndIf
 
EndProcedure

;----------------------------------------------------------
; Main Loop
;----------------------------------------------------------

If Window_Scrolla()

  Program\QuitFlag = 0
 
  Repeat
    EventID  = WaitWindowEvent()
    MenuID   = EventMenu()
    GadgetID = EventGadget()
    WindowID = EventWindow()
    Select EventID
      Case #PB_Event_CloseWindow
        Select WindowID
          Case  #Window_Scrolla         : Program\QuitFlag = 1
        EndSelect
      Case #PB_Event_Gadget
        Select GadgetID
          Case #Gadget_Scrolla_Load     : LoadAnotherImage()
          Case #Gadget_Scrolla_Exit     : Program\QuitFlag = 1
        EndSelect
    EndSelect
  Until Program\QuitFlag
  CloseWindow(#Window_Scrolla)
EndIf
End

_________________
Resist FaceBorg or have your ass laminated!


Top
 Profile  
 
 Post subject: Re: Detecting dynamically created imagegadget click?
PostPosted: Mon May 28, 2012 10:07 am 
Offline
Addict
Addict
User avatar

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 2399
Location: Utah, USA
Fangbeast wrote:
Am I right in thinking that I cannot somehow detect imagegadget clicks on each image somehow now in the event loop??

No.

Fangbeast wrote:
I know I can put each handle into a list as they are created and use physical image area detection (look up handle in list, find out what area it occupies, check bounds and return 'found') but is that the best way?

No.


Put the handle in a list and check for it in the event loop. This only deals with the event and not the area of the image.

Here's your example modified to implement it:
Code:
;----------------------------------------------------------
; Load our image decoders
;----------------------------------------------------------

UseJPEGImageDecoder()
UsePNGImageDecoder()

;----------------------------------------------------------
; Window constant
;----------------------------------------------------------

Enumeration 1
  #Window_Scrolla
EndEnumeration

#WindowIndex = #PB_Compiler_EnumerationValue

;----------------------------------------------------------
; Gadget constants
;----------------------------------------------------------

Enumeration 1
  ; Window_Scrolla
  #Gadget_Scrolla_sPreview
  #Gadget_Scrolla_Area18
  #Gadget_Scrolla_cControl
  #Gadget_Scrolla_Area22
  #Gadget_Scrolla_Load
  #Gadget_Scrolla_Exit
EndEnumeration

#GadgetIndex = #PB_Compiler_EnumerationValue

;----------------------------------------------------------
; Image constants
;----------------------------------------------------------

Enumeration 1
  #Image_Scrolla_Temp      ; Temporary image variable for full scan
EndEnumeration

#ImageIndex = #PB_Compiler_EnumerationValue

;----------------------------------------------------------
; Main window
;----------------------------------------------------------

Procedure.l Window_Scrolla()
  If OpenWindow(#Window_Scrolla,84,60,157,655,"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_Invisible)
    SetWindowColor(#Window_Scrolla,$A2A2A2)
    ScrollAreaGadget(#Gadget_Scrolla_sPreview,5,5,145,575,126,575,5,#PB_ScrollArea_Flat|#PB_ScrollArea_BorderLess)
      SetGadgetColor(#Gadget_Scrolla_sPreview,#PB_Gadget_BackColor,$BFBFBF)
    CloseGadgetList()
    ContainerGadget(#Gadget_Scrolla_cControl,5,585,145,65,#PB_Container_Flat|#PB_Container_BorderLess)
      SetGadgetColor(#Gadget_Scrolla_cControl,#PB_Gadget_BackColor,$BFBFBF)
      ButtonGadget(#Gadget_Scrolla_Load,10,10,60,45,"Load image",#PB_Button_MultiLine)
      SetGadgetFont(#Gadget_Scrolla_Load,LoadFont(#Gadget_Scrolla_Load,"Comic Sans MS",10,0))
      ButtonGadget(#Gadget_Scrolla_Exit,70,10,60,45,"Exit program",#PB_Button_MultiLine)
      SetGadgetFont(#Gadget_Scrolla_Exit,LoadFont(#Gadget_Scrolla_Exit,"Comic Sans MS",10,0))
    CloseGadgetList()
    HideWindow(#Window_Scrolla,0)
    ProcedureReturn WindowID(#Window_Scrolla)
  EndIf
EndProcedure

;----------------------------------------------------------
; Program variables
;----------------------------------------------------------
Structure imageData     
  gID.i       ;image gadget ID
  tid.i       ;text gadget ID
  number.i    ;image number
  label.s     ;label text
  filename.s  ;filename of source image
EndStructure

Structure ProgramData
  QuitFlag.i          ; Program quit semaphore
  CurrentDir.s        ; The current directory we are working in
  ProgramVersion.s    ; Keep track of program version and compile version
 
  PreviewTop.i        ; Keep track of the horizontal preview position per image
  PreviewLeft.i       ; Keep track of the Preview left position
  AvailableScroll.i   ; Available scroll length to add gadgets to
  List images.imageData()
  ImageCounter.i      ; Number of preview images in the scroll area before having to expand area
 
  PreviewWidth.i      ; Width of the preview image
  PreviewHeight.i     ; Height of the preview image
 
  LabelHeight.i       ; Height of the image label
EndStructure

;----------------------------------------------------------
; Declare your global structures
;----------------------------------------------------------

Global Program.ProgramData

;----------------------------------------------------------
; Setup directory variables
;----------------------------------------------------------

Program\CurrentDir      = GetCurrentDirectory()

;----------------------------------------------------------
; Initial scrollgadget positions
;----------------------------------------------------------

Program\PreviewTop      =  10     ; Initial position from top
Program\PreviewLeft     =  10     ; Preview left position
Program\AvailableScroll = 575     ; Total initial scroll space

Program\PreviewWidth    = 105     ; Preview image width
Program\PreviewHeight   = 105     ; Preview image height

Program\LabelHeight     =  20     ; Height of the image label

;----------------------------------------------------------
; Load a picture into the scrollareagadget dynamically
;----------------------------------------------------------

Procedure LoadAnotherImage()
 
  ; Ask to load a new picture into the image strip
 
  NewPicture.s = OpenFileRequester("Select picture to load", "", "Jpeg (*.jpg *.jpeg *.jfif)|*.jpg;*.jpeg;*.jfif|Bitmap (*.bmp)|*.bmp|Png (*.png)|*.png|", 0)
 
  ; Only proceed if we got a new picture
 
  If NewPicture.s
   
    ; Load a new picture into the image strip from any directory
   
    NewImageNumber.i = LoadImage(#PB_Any, NewPicture.s)
   
    ; Only proceed if we got a new image handle
   
    If NewImageNumber
       
      ; Resize the image to the needed preview size
     
      ResizeImage(NewImageNumber, Program\PreviewWidth, Program\PreviewHeight)
     
      ; Now open the gadget list to add a new image preview to it
     
      OpenGadgetList(#Gadget_Scrolla_sPreview)
       
        ; Create a new imagegadget in the scrollarea list 5 across, Program\PreviewWidth down
       
        NewImageGadget.i = ImageGadget(#PB_Any, Program\PreviewLeft, Program\PreviewTop, Program\PreviewWidth, Program\PreviewHeight, ImageID(NewImageNumber))
       
        ; Reset the next picture position to picture height + 5
       
        Program\PreviewTop + Program\PreviewHeight + 5
       
        ; Create a new label gadget at the bottom of the picture
       
        NewLabelGadget.i =  TextGadget(#PB_Any, Program\PreviewLeft, Program\PreviewTop, Program\PreviewWidth, Program\LabelHeight, "", #PB_Text_Center)
       
        ; Add the scroll gadget colour as the label background
       
        SetGadgetColor(NewLabelGadget.i, #PB_Gadget_BackColor, $BFBFBF)
       
        ;SetGadgetFont(NewLabelGadget.i, LoadFont(#PB_Any, "Comic Sans MS", 8, 0))
       
        SetGadgetText(NewLabelGadget.i, "Scan Image #" + Str(Program\ImageCounter + 1))
       
        ;Record image information for use later (i.e. events, replacing, freeing, etc.)

        AddElement(Program\images()): Program\images()\gID = NewImageGadget
        Program\images()\tid = NewLabelGadget
        Program\images()\label = GetGadgetText(NewLabelGadget)
        Program\images()\filename = NewPicture
        Program\images()\number = ImageCounter
       
        ; Increment the position counter for the next preview    ; Height of image plus space plus label plus space
       
        Program\PreviewTop + 25
       
        ; Keep track of number of preview images in the scroll area. Add size of image after page 4 + space
       
        Program\ImageCounter + 1
       
        ; Let user know how many images are currently in the list
       
        SetWindowTitle(#Window_Scrolla, "Image: " + Str(Program\ImageCounter))
       
        ; If there are 5 previews in the list, time to start expanding the view area
       
        If Program\ImageCounter > = 5
         
          ; Height of the last image plus space plus label plus space
         
          Program\AvailableScroll + Program\PreviewHeight + 5 + Program\LabelHeight + 5
         
          ; Expand the scrolled area
         
          SetGadgetAttribute(#Gadget_Scrolla_sPreview, #PB_ScrollArea_InnerHeight, Program\AvailableScroll)
         
          ; Always keep the last image visible added
         
          SetGadgetAttribute(#Gadget_Scrolla_sPreview, #PB_ScrollArea_Y, Program\AvailableScroll)
         
        EndIf
       
        ; Close the scroll area gadget list
       
      CloseGadgetList()
     
    Else
     
      Debug "Cannot get new image number"
     
    EndIf
   
  Else
   
    Debug "Cannot load a picture or user cancelled"
   
  EndIf
 
EndProcedure

;----------------------------------------------------------
; Main Loop
;----------------------------------------------------------

If Window_Scrolla()
 
  Program\QuitFlag = 0
 
  Repeat
    EventID  = WaitWindowEvent()
    MenuID   = EventMenu()
    gadgetID = EventGadget()
    WindowID = EventWindow()
    Select EventID
      Case #PB_Event_CloseWindow
        Select WindowID
          Case  #Window_Scrolla         : Program\QuitFlag = 1
        EndSelect
      Case #PB_Event_Gadget
        Select gadgetID
          Case #Gadget_Scrolla_Load     : LoadAnotherImage()
          Case #Gadget_Scrolla_Exit     : Program\QuitFlag = 1
          Default
            If EventType() = #PB_EventType_LeftClick
              ForEach Program\images()
                If Program\images()\gID = gadgetID
                  MessageRequester("Detected", Program\images()\label +  " was clicked")
                  Break
                EndIf
              Next
            EndIf
        EndSelect
    EndSelect
  Until Program\QuitFlag
  CloseWindow(#Window_Scrolla)
EndIf
End


You definitely will need the gadget handles for the image and text gadgets so that you can free or change their contents while the program is still running.

_________________
Image


Top
 Profile  
 
 Post subject: Re: Detecting dynamically created imagegadget click?
PostPosted: Mon May 28, 2012 11:54 am 
Offline
PureBasic Protozoa
PureBasic Protozoa
User avatar

Joined: Fri Apr 25, 2003 3:08 pm
Posts: 3011
Demivec, I would never have thought of that example you did, thank you.

I'm thinking of using the image strip code to provide a scrolling list of previews for my library manager, home inventory and other programs soon, once I know what I am doing of course:):)

_________________
Resist FaceBorg or have your ass laminated!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye