View Single Post
Old 05-14-2016, 06:05 AM   #32
Sexan
Human being with feelings
 
Sexan's Avatar
 
Join Date: Jun 2009
Location: Croatia
Posts: 2,452
Default

There are no errors,just the button does not change after clicking it
Code:
local function IsInside(x, y, w, h)
  
  local mouse_x, mouse_y = gfx.mouse_x, gfx.mouse_y
  
  local inside = 
    mouse_x >= x and mouse_x < (x + w) and 
    mouse_y >= y and mouse_y < (y + h)
    
  return inside
  
end

local function roundrect(x, y, w, h, r, antialias, fill)
  
  local aa = antialias or 1
  fill = fill or 0
  
  if fill == 0 or false then
    gfx.roundrect(x, y, w, h, r, aa)
  elseif h >= 2 * r then
    
    -- Corners
    gfx.circle(x + r, y + r, r, 1, aa)    -- top-left
    gfx.circle(x + w - r, y + r, r, 1, aa)    -- top-right
    gfx.circle(x + w - r, y + h - r, r , 1, aa)  -- bottom-right
    gfx.circle(x + r, y + h - r, r, 1, aa)    -- bottom-left
    
    -- Ends
    gfx.rect(x, y + r, r, h - r * 2)
    gfx.rect(x + w - r, y + r, r + 1, h - r * 2)
      
    -- Body + sides
    gfx.rect(x + r, y, w - r * 2, h + 1)
    
  else
  
    r = h / 2 - 1
  
    -- Ends
    gfx.circle(x + r, y + r, r, 1, aa)
    gfx.circle(x + w - r, y + r, r, 1, aa)
    
    -- Body
    gfx.rect(x + r, y, w - r * 2, h)
    
  end  
  
end

local function rgb2num(red, green, blue)  
  green = green * 256
  blue = blue * 256 * 256  
  return red + green + blue
end
state = 1
local function Main()
    
  local char = gfx.getchar()
  if char ~= 27 and char ~= -1 then
    reaper.defer(Main)
  end 
  
  local my_str = "This is a string"
  
  gfx.set(1, 0.5, 0.5, 1)
  
  local x, y = 100, 100
  local w, h = 200, 80
  local r = 10
  
  gfx.setfont(1, "Arial", 28)
  
  local str_w, str_h = gfx.measurestr(my_str)
  local txt_x = x + ((w - str_w) / 2)
  local txt_y = y + ((h - str_h) / 2) 
 
  -- Unclicked
  if state == 0 then 
  
    roundrect(x, y, w, h, r, 1, 0)
  
    gfx.x = txt_x
    gfx.y = txt_y
  
    gfx.drawstr(my_str)
    
  -- Clicked  
  else  
    
    roundrect(x, y, w, h, r, 1, 1)
    
    gfx.x = txt_x
    gfx.y = txt_y
    
    -- Store the current foreground color
    local r, g, b = gfx.r, gfx.g, gfx.b
    
    -- Set our text to the background color instead
    -- Remember the different color conventions; this is the equivalent of RGB 64, 64, 64
    gfx.set(0.25, 0.25, 0.25, 1)
    
    gfx.drawstr(my_str)
    
    -- Set the color back to what it was, before we forget
    gfx.set(r, g, b)
  end
  
  if gfx.mouse_cap & 1 == 1 then
      
        -- If the cursor is inside the rectangle AND the button wasn't down before
        if IsInside(x, y, w, h) and not mouse_btn_down then
          
          mouse_btn_down = true
          state = not state
          
        end
    
      -- If the left button is up
      else
    
        mouse_btn_down = false
      
      end 
  
  gfx.update()
  
end


-- A nice, respectable shade of grey.
local r, g, b = 64, 64, 64
gfx.clear = rgb2num(r, g, b) 

gfx.init("My Window", 400, 300, 0, 200, 200)
Main()
edit:after changing this line
Code:
if state == 0 then
to
Code:
if state == false then
everything works,are we supposed to change this or it should work as it is?

Last edited by Sexan; 05-14-2016 at 06:16 AM.
Sexan is offline   Reply With Quote