Jump to content

[Question]Using onClientClick


kieran

Recommended Posts

So I have two problems on this gui browser I am making, but the main one is getting the position when I click and loading the url, code (and other problem) is below.

Spoiler

local screenWidth, screenHeight = guiGetScreenSize()
--resolution
local px,py = 800, 600
--divide the x and y resolution you just got by the screen size
local x,y =  (screenWidth/px), (screenHeight/py) 
--use the new x and y resolution and times them by a number to keep position	

loading = false

function createBrowser()
	if ( window ) then --Toggling the window
    		if ( guiGetVisible( window )) then    	
    			showCursor( false )
				toggleAllControls(true)
                guiSetVisible( window, false )
				removeEventHandler ( "onClientClick", root, clientClicking ) --remove the handler for the image (the problem I have)
        	else
        		guiSetInputEnabled(false)
    			showCursor(true)
				toggleAllControls(false)
        		guiSetVisible( window, true )
				addEventHandler ( "onClientClick", root, clientClicking ) --add it back when my window shows again
        	end
        else
		toggleAllControls(false)
		window = guiCreateWindow(x+50, y+50, screenWidth-100, screenHeight-100, "Please wait while your page loads", false)
		edit_site = guiCreateEdit(0+30, y+25, screenWidth-180, 25, "", false, window)
		goBrowser = guiCreateButton(screenWidth-150, y+25, 20, 25, ">>", false, window)
		Home = guiCreateStaticImage(0, y+25, 20, 25, "home.png", false, window) 
		--Home = guiCreateStaticImage(x+58, y+73, x+20, y+25, "home.png", false) --This shows the exact x and y position of the image
		webBrowser = guiCreateBrowser(0, 55, screenWidth, screenHeight-100, false, false, false, window)
		theBrowser = guiGetBrowser( webBrowser )
		bindKey ("enter", "down", nextBrowser)
		addEventHandler("onClientGUIClick",goBrowser,nextBrowser)
		addEventHandler ( "onClientClick", root, clientClicking )--This is the handler for clicking the image
	end
end

bindKey ("F3", "down", createBrowser)


function SetURL(url)
	guiSetText(window, url)
	if (guiGetEnabled(edit_site) == false) then
		guiSetEnabled(edit_site, true)
		guiSetEnabled(goBrowser, true)
    	loading = false
		guiSetText(edit_site, ""..url)
	end
end


function loadBrowser()
	loadBrowserURL(source, "http://www.youtube.com")
	showCursor(true)
	addEventHandler("onClientBrowserDocumentReady", root, SetURL)
	site = guiGetText(edit_site)
	guiSetText(edit_site, ""..site)
	if (guiGetEnabled(edit_site) and guiGetEnabled(goBrowser) == true) then
		guiSetEnabled(edit_site, false)
		guiSetEnabled(goBrowser, false)
		loading = true
		guiSetText(edit_site, "Please wait while your page loads.")
	end
end	

addEventHandler("onClientBrowserCreated", resourceRoot, loadBrowser)


function nextBrowser()
	if ( guiGetVisible( window )) then
		
		if (loading) == true then
			return
		end
		
		site = guiGetText(edit_site)
		
		if site:sub(0, 22)  == "http://www.http://www." then
			guiSetText(edit_site, "http://www.http://www."..site:sub(0, 22))
			return
		elseif site:sub(0, 18)  == "http://www.http://" then
			guiSetText(edit_site, "http://www."..site:sub(0, 18))
			return
		end
		
		if ( isBrowserDomainBlocked ( ""..site ) ) then
			requestBrowserDomains({""..site})
		end
		
		if isBrowserLoading (theBrowser) then
			if (guiGetEnabled(edit_site) == true) then
			guiSetEnabled(edit_site, false)
			guiSetEnabled(goBrowser, false)
			guiSetText(edit_site, "Please wait while your page loads.")
		end
		end
		
			loadBrowserURL(theBrowser, site)
		
		if site == "" then
			guiSetText(edit_site, "http://www.google.com")
			loadBrowserURL(theBrowser, site)
		end
		
		if not isBrowserLoading (theBrowser) then
			if site:sub(0, 7)  ~= "http://"  then
				guiSetText(edit_site, "http://"..site)
				return
			elseif site:sub(0, 11)  ~= "http://www." then
				guiSetText(edit_site, "http://www."..site)
				loadBrowserURL(theBrowser, site)
				return
			elseif (isBrowserLoading) and (getBrowserURL(theBrowser) == ""..site) then
				return
			end
		end
	end
end

function clientClicking ( btn, state, cx, cy ) --The function I am using to see if my player is clicking the image or not
	if ( btn == 'left' and guiGetVisible ( window ) and state == 'down' ) then
		if ( cy >= y+73 and cy <= y+25 and cx >= x+58 and cx <= x+25 ) then
			site = guiGetText(edit_site)
			guiSetText(edit_site, "http://www.google.com")
			loadBrowserURL(theBrowser, site)
		end
	end
end

 

Second problem:  I can not use :sub(int, int) to set the url on my gui edit, it spams "http://www." even if it's in my URL edit box.

 

Thanks for any help, been really struggling with both of these problems for about a week. :/

Edited by kieran
Link to comment

1. If image is GUI element, why not using onClientGUIClick event?

Home = guiCreateStaticImage(0, y+25, 20, 25, "home.png", false, window)
addEventHandler("onClientGUIClick", Home, clientClicking, false)

2. Im not sure what are you trying to do with this code

if site:sub(0, 22)  == "http://www.http://www." then
  guiSetText(edit_site, "http://www.http://www."..site:sub(0, 22)) --> this, you keep adding that spam
  return
elseif site:sub(0, 18)  == "http://www.http://" then
  guiSetText(edit_site, "http://www."..site:sub(0, 18)) --> also this
  return
end

That code, of course will spam that http. String will repeat because you keep adding it. You may use gsub, to remove that spam.

site:gsub("http://www.http://www.", "") -- remove spam

-- or

site:gsub("http://www.http://www.", "http://www.") -- replace it with http://www.

 

  • Thanks 1
Link to comment

@idarrr The point of it is to create an auto complete for URLs if the player enters, for example, "google.com" and forgets "http://www."

I only looked at dX images, this made me a little confused as I have only added gui images, but first time doing a click event for them, found out I could just shove a button after the image and set it's alpha to 0, works well enough but your way is better. 

I also found one more issue....  When I do a google search, I can not click the hyperlinks. :/  How would I check if a client clicked a URL?  All I'd need to do after that is check if the domain is blocked and request it. :) 

I appreciate the help, hopefully you'll see a new browser on forums that works just like the one on your PC soon! :P  But yeah, would help if google search actually worked...

Link to comment

@idarrr worked for one URL, not any others :/ also it keeps sending a warning basically telling me it got the URL when I click, not the button...  I can't find a way to get the button, the explanation on the wiki isn't that clear...

Spoiler

loading = false --This will check whether the page is loading later in code.

function createBrowser()
	if ( window ) then
    		if ( guiGetVisible( window )) then    	
    			showCursor( false )
				toggleAllControls(true)
                guiSetVisible( window, false )
        	else
        		guiSetInputEnabled(false)
    			showCursor(true)
				toggleAllControls(false)
        		guiSetVisible( window, true )
        	end
        else
		toggleAllControls(false)
		window = guiCreateWindow(x+50, y+50, screenWidth-100, screenHeight-100, "Please wait while your page loads", false)
		edit_site = guiCreateEdit(0+30, y+25, screenWidth-180, 25, "", false, window)
		goBrowser = guiCreateButton(screenWidth-150, y+25, 20, 25, ">>", false, window)
		Home = guiCreateStaticImage(0, y+25, 20, 25, "home.png", false, window) 
		goHome = guiCreateButton(0, y+25, 20, 25, "", false, window)
		guiSetAlpha(goHome, 0)
		webBrowser = guiCreateBrowser(0, 55, screenWidth, screenHeight-100, false, false, false, window)
		theBrowser = guiGetBrowser( webBrowser )
		bindKey ("enter", "down", nextBrowser)
		addEventHandler("onClientGUIClick",goBrowser,nextBrowser)
		addEventHandler("onClientGUIClick",goHome,clickHome)
		addEventHandler("onClientRender", root, IsLoading)
		addEventHandler("onClientClick", root, onPageClick)--When client clicks it will go to the function.
		addEventHandler("onClientBrowserNavigate", theBrowser, onPageClick)--How can I get the button?  It keeps saying it returns URL.
	end
end


function IsLoading()
	site = guiGetText(edit_site)
	if isBrowserLoading(theBrowser) then
		if ( isBrowserDomainBlocked ( ""..site ) ) then
			requestBrowserDomains({""..site})
		end
		guiSetEnabled(edit_site, false)
		guiSetEnabled(goBrowser, false)
		loading = true	
		guiSetText(edit_site, "Please wait while your page loads.")
	else
		guiSetEnabled(edit_site, true)
		guiSetEnabled(goBrowser, true)
		loading = false
		guiSetText(edit_site, ""..site)
	end
end


function onPageClick(button, state)
	if (loading) == true then
		return
	end
	if state == "up" then
		injectBrowserMouseUp(theBrowser, button)
		theURL = getBrowserURL(theBrowser)
			if ( isBrowserDomainBlocked ( ""..theURL ) ) then
				requestBrowserDomains({""..theURL})
				outputChatBox(""..theURL)
			end
	else
		injectBrowserMouseDown(theBrowser, button)
	end
end

Don't know what I am doing wrong...

Link to comment
8 minutes ago, MIKI785 said:

I don't understand what do you mean by 'get the button'. Try to explain a bit more.

Also, there is no need to inject the mouse clicks as you are using a GUI browser. You have to inject clicks when you use the regular DX browser (createBrowser).

It's to make links work on google search, I have the code to request blocked domains, and it works when I type the URL's in manually to my edit box, but I am trying to get the blocked URL when I am accessing a website with google search as it will not load when I click the link, but all elements in the browser, tabs, text boxes, even going to the next page of the search works, it's just accessing the pages I have  a problem with, for example, you  go to google, type mtasa.com in the search box, you can access this forum that way, I can not access it unless I type the exact URL.

Edited by kieran
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...