Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
execute a bunch of code somehow extending imgui button? — Gideros Forum

execute a bunch of code somehow extending imgui button?

Hi, I guess there might not be an easy way to do this but many people are smarter than me, so maybe there is:

I have a thousand imgui buttons in my app, and I'd like to execute a small piece of code for each one of them to give some acoustic feedback when they are pressed.

The basic (not smart) idea is to wrap my sound:play() code inside a single playfeedback() function and call it inside each "button actions".

However, since they are many already, and adding the same piece of code is time consuming and prone to errors I was wondering if it might be possible to extend the imgui button class through lua to do it in one simple passage.

It would be easy if imgui:buttons were Core.class object but with imgui I am not sure if this is feasible and how, without touching imgui itself..

Thank you :smile:

Comments

  • keszeghkeszegh Member
    edited October 2023 Accepted Answer
    modifying ImGui functions works as you expect and i do it all the time, see an example:
    function ImGui:textHoverable(text,align)
      local backupX,backupY = self:getCursorPos()
      local w, h = self:calcTextSize(text)
      self:invisibleButton("##dummy",w,h)
      self:setCursorPos(backupX,backupY)		
      if align then self:alignTextToFramePadding()	end
      self:text(text)
    end
    overriding works also similarly, just give the original function a dummy name like _button=ImGui.button so you can call it inside your override function.

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • The way I do it when I have a bunch of buttons is to put them in a table then iterate through them to execute some code. Example:
    	self.tiled_mixamoactor_btns = {}
    	self.tiled_mixamoactor_btns[0] = self.tiled_ui.actorunload
    	self.tiled_mixamoactor_btns[1] = self.tiled_ui.actorstandard
    	self.tiled_mixamoactor_btns[2] = self.tiled_ui.actorswat
    	self.tiled_mixamoactor_btns[3] = self.tiled_ui.actorlightsword
    	self.tiled_mixamoactor_btns[4] = self.tiled_ui.actorzombie
     
    	for i = 0, #self.tiled_mixamoactor_btns do
    		self.tiled_mixamoactor_btns[i]:setVisible(true)
    	end
    Hopefully it is possible to put imgui:buttons in a table?!
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • piepie Member
    edited October 2023
    Thank you @keszegh I hoped there was a smart way!!

    for the records, this is how I did it:
     
    local ui = ImGui.new(nil, false, true, false) 
     
    ui._button = ImGui.button
     
    function ui:button(text, w, h, sound)
    	if not w then w = 0 end
    	if not h then h = 0 end
     
     
    	if	ui:_button(text,w,h) then
    		playSound(sound)
    		return self
    	end
     
     
    end
    MoKaLux said:

    The way I do it when I have a bunch of buttons is to put them in a table then iterate through them to execute some code. Example:

    	self.tiled_mixamoactor_btns = {}
    	self.tiled_mixamoactor_btns[0] = self.tiled_ui.actorunload
    	self.tiled_mixamoactor_btns[1] = self.tiled_ui.actorstandard
    	self.tiled_mixamoactor_btns[2] = self.tiled_ui.actorswat
    	self.tiled_mixamoactor_btns[3] = self.tiled_ui.actorlightsword
    	self.tiled_mixamoactor_btns[4] = self.tiled_ui.actorzombie
     
    	for i = 0, #self.tiled_mixamoactor_btns do
    		self.tiled_mixamoactor_btns[i]:setVisible(true)
    	end
    Hopefully it is possible to put imgui:buttons in a table?!
    yes you can do it: I do it when I have a list of consecutive buttons, but the overraiding trick works across the whole app even for single sparse buttons

    Likes: keszegh, MoKaLux

    +1 -1 (+2 / -0 )Share on Facebook
  • keszeghkeszegh Member
    edited October 2023
    @pie , i don't understand the part "return self", probably it would be better to return the flag, to stay compatible with old button function:
    local flag=ui:_button(text,w,h) 
                    if flag then
    		playSound(sound)
    		return flag
    end
    but i did not try the code, so perhaps i'm wrong.
  • keszeghkeszegh Member
    edited October 2023
    also it is practical to just pass args this way (this does the same as original button function, you can modify this):
      function ImGui:button(...)
      local arg={...}	
     self:_button(unpack(arg))   
    end

    Likes: pie, MoKaLux

    +1 -1 (+2 / -0 )Share on Facebook
  • piepie Member
    keszegh said:

    @pie , i don't understand the part "return self", probably it would be better to return the flag, to stay compatible with old button function:

    local flag=ui:_button(text,w,h) 
                    if flag then
    		playSound(sound)
    		return flag
    end
    but i did not check the code, so perhaps i'm wrong.
    You are right, I like your approach better because it specifically returns a flag, but I guess that in the end it is the same thing since flag = button? Maybe someone else knows better?

    Both approaches work in my tests though, thank you also for the heads up of the arg unpack
  • i think the flag should be a boolean, but you can check it out.

    Likes: pie

    +1 -1 (+1 / -0 )Share on Facebook
  • piepie Member
    keszegh said:

    i think the flag should be a boolean, but you can check it out.

    yep, self is a table, returning flag is much better.

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.