I have added rectangle tool which contain an Image. The feature I want to develop is on right Click I want to change the image in it.
I tried using OnRectangleToolClick but not able to get the rectangle tool details which has been clicked.
Can someone help me with this.
Thanks
Akshay
I've made a simple example with VB6:
Private Sub Form_Load()
TChart1.AddSeries scBar
TChart1.Series(0).FillSampleValues 8
TChart1.Tools.Add tcRectangle
TChart1.Tools.Items(0).asRectangle.Shape.Picture.LoadImage "C:\tmp\MyImage.jpg"
End Sub
Private Sub TChart1_OnRectangleToolClick(ByVal Button As TeeChart.EMouseButton, ByVal Shift As TeeChart.EShiftState, ByVal X As Long, ByVal Y As Long)
If Button = mbRight Then
MsgBox "Rectangle clicked with the right mouse button"
End If
End Sub
The above seems not to work fine, but after debugging I've seen when I click on the rectangle tool with the right mouse button and TChart1_OnRectangleToolClick is fired, the Button variable has the value 1, while mbRight is 2. So changing it for this makes it work:
Private Sub TChart1_OnRectangleToolClick(ByVal Button As TeeChart.EMouseButton, ByVal Shift As TeeChart.EShiftState, ByVal X As Long, ByVal Y As Long)
If Button = 1 Then
MsgBox "Rectangle clicked with the right mouse button"
End If
End Sub
EDIT:
If you want to check when the user clicks the Rectangle tool with the left mouse button, the OnRectangleToolClick event may conflict with the dragging feature.
In that case, you could still play with OnMouseMove, OnClick and OnMouseDown events as in the following example:
Dim mouseXPos, mouseYPos, mouseXDown, mouseYDown As Long
Private Sub Form_Load()
TChart1.AddSeries scBar
TChart1.Series(0).FillSampleValues 8
TChart1.Tools.Add tcRectangle
TChart1.Tools.Items(0).asRectangle.Shape.Picture.LoadImage "C:\tmp\MyImage.jpg"
mouseXPos = -1
mouseYPos = -1
mouseXDown = -1
mouseYDown = -1
End Sub
Private Sub TChart1_OnMouseMove(ByVal Shift As TeeChart.EShiftState, ByVal X As Long, ByVal Y As Long)
mouseXPos = X
mouseYPos = Y
End Sub
Private Sub TChart1_OnClick()
If mouseXDown = mouseXPos And mouseYDown = mouseYPos Then
If TChart1.Tools.Items(0).asRectangle.Clicked(mouseXPos, mouseYPos) Then
MsgBox "Rectangle clicked with the left mouse button"
End If
End If
End Sub
Private Sub TChart1_OnMouseDown(ByVal Button As TeeChart.EMouseButton, ByVal Shift As TeeChart.EShiftState, ByVal X As Long, ByVal Y As Long)
mouseXDown = X
mouseYDown = Y
End Sub
Related
Hello I want to get the table that a function is attached to, I can't really find a nice way to explain it but I think i've explained it good enough within the code for. Basically I need to get the table that the function is attached to from another function, without passing in the table.
function DrawRect()
print(debug.getinfo(1).name) -- this gets the name of the function that is invoking DrawRect ('Paint')..
-- I want to be able to get the table that is attached to this function
-- So I can do table.x inside this function, and have it print 123
end
local r = math.random(1, 100)
_G["abc" .. r] = {
x = 123,
Paint = function(self)
DrawRect()
end
}
_G["abc" .. r]:Paint()
Example of the problem i'm trying to solve
This is my current code right now
function DrawRect(x,y,w,h)
draw.DrawRect(x,y,w,h)
end
local Button = {
Init = function(self)
self.label = gui.Label("Button")
self.label:SetPos(10, 5) -- see the position is relative to the Button's position
self.label:SetColor(255,255,255)
end,
Paint = function(self,x,y,w,h)
Color(40,40,40)
DrawRect(x,y,w,h) -- Draws dark background
end
}
As you can see paint has 4 args, x,y,w,h. I want to do away with x,y and only have w,h. I want to achieve this like this.
function DrawRect(x,y,w,h)
local relative_x = parent_table_of_paint.INTERNAL.draw_x
local relative_y = parent_table_of_paint.INTERNAL.draw_y
draw.DrawRect(relative_x + x, relative_y + y,w,h)
end
local Button = {
Init = function(self)
self.label = gui.Label("Button")
self.label:SetPos(10, 5) -- see the position is relative to the Button's position
self.label:SetColor(255,255,255)
end,
Paint = function(self,w,h)
Color(40,40,40)
DrawRect(0,0,w,h) -- Draws dark background
end
}
I know you can't see some of the properties in my example, but they are there.
Edit 2:
I am recreating the a frame, "VGUI".
https://wiki.facepunch.com/gmod/draw.RoundedBox
draw.RoundedBox( number cornerRadius, number x, number y, number width, number height, table color )
and as you can see this has the functionality I want
https://wiki.facepunch.com/gmod/PANEL:Paint
local panel = vgui.Create( "DPanel" )
panel:SetSize( 100, 100 )
panel:SetPos( ScrW() / 2 - 50, ScrH() / 2 - 50 )
function panel:Paint( w, h )
draw.RoundedBox( 8, 0, 0, w, h, Color( 0, 0, 0 ) )
end
A function is a first class value, so there may be many tables and variables that have a reference to the same function. There is no way for that function to know what those tables and variables are.
That's not how you would implement something like that. You don't implement a function that through some magic gets its caller's container so it can access its other values.
DrawRect should just draw a rect. If you want to draw a rect somewhere else you should provide that offset through the parameters of DrawRect.
I modified your button so it will simply put the buttons x and y coordinates (if they exist) into DrawRect
local Button = {
Init = function(self)
self.label = gui.Label("Button")
self.label:SetPos(10, 5) -- see the position is relative to the Button's position
self.label:SetColor(255,255,255)
end,
Paint = function(self,w,h)
Color(40,40,40)
local x = self.x or 0
local y = self.y or 0
DrawRect(x,y,w,h) -- Draws dark background
end
}
That way you can call Button:Paint(20,30) to draw a 20 by 30 rectangle at the buttons coordinates. If you want to add an offset, do that outside of DrawRect
# Edit 2:
This is something different. The host program will call the Paint function.
vgui.Create( "DPanel" ) will return a new panel instance and the game will add a reference to it to a list. Everytime the gui is updated it will call the Paint functions of all the panels in that list. That's how the function knows width and height of the panel.
If I am at the bottom of the page how can I check if some element is visible if that element is for example at the top of the page.
I need to write a test where I scroll down to the bottom of the page and when I click on div it moves me back to the top of the page. Next step is to check if I am at that position.
My idea was that I just need to check if that element is visible but it turns out that it is not that easy to do. Any suggestions?
.isDisplayed() and .isFocused() is not what I need.
Edit:
I had an idea and I need two methods for it.
First one is to get y coordinate of the element (we need only y), but the problem is that getY() returns number relative to your current position, so I need something that will move me to that element and then get y coordinate. So my method looked something like this:
int getYCoordinat() {
// first move to the element
interact {
moveToElement(element)
}
// get y coord
return element.getY()
}
second method looked something like this:
boolean isScrolledToElement(int yCoordinateOfElement) {
int y = element.getY()
return y == yCoordinateOfElement
}
}
So first I would call getYCoordinat(), store integer into yCoordinateOfElement.
Then I would click on div that moves me to the element and call isScrolledToElement(yCoordinateOfElement).
But it doesnt work because moveToElement moves you to that element only if the element is not visible. So if we stay where we are because moveToElement() didn't move us, because element is already visible, we get the wrong Y coordinate that won't be same as the one which we get when we click on div that moves us to the element.
Here is the only solution I could find. Basically you need to see if the pixel coordinate of the bottom-right corner of your element is within the window.
Taken from the link:
private boolean isWebElementVisible(WebElement w) {
Dimension weD = w.getSize();
Point weP = w.getLocation();
Dimension d = driver.manage().window().getSize();
int x = d.getWidth();
int y = d.getHeight();
int x2 = weD.getWidth() + weP.getX();
int y2 = weD.getHeight() + weP.getY();
return x2 <= x && y2 <= y;
}
In this particular code, I have 3 boxes that currently show up on the screen. The boxes in the center of the screen are the ones I want to have treated as the characters. The box on the top of the screen randomly generates an image of one of the characters, in this case the red box or the blue box. For example, if at the top the red box was selected as the randomly generated image, I would want the player to tap the red box that would be treated as a character. If the person tapped the correct box as a result of the box on the top, I would want the box at the top to change again, therefore creating a cycle between the characters and the box at the top. If the player taps the wrong box, I would want the game to end. What am I doing wrong, and more importantly how can I fix it? Here is my code:
local imageFiles = {"bluebox.png", "redbox.png"}
local imageFile = imageFiles[math.random(2)]
local randomImage = display.newImage(imageFile, 40, 40)
local button1 = display.newImage("redbox.png")
button1.x = centerX
button1.y = centerY
group:insert(button1)
local button2 = display.newImage("bluebox.png")
button2.x = centerX
button2.y = centerY - 100
group:insert(button2)
local function randomize(event)
if button2 == randomImage then
(insert code here)
end
end
local function generate(event)
if button2 ~= imageFile then
storyboard.gotoScene ("restartEasy")
elseif button2 == imageFile then
(insert code here)
end
end
button1:addEventListener("tap", randomize)
button2:addEventListener("tap", generate)
if button2 ~= imageFile then
elseif button2 == imageFile then
You're comparing images with strings. This will never work. But let's ignore that for now.
While I'm not familiar with Corona, I believe randomImage will never equal either button1 or button2 because it's a separate image (with independent coordinates and/or dimensions).
I'd replace your random selection code (the first three lines) with the following lines right after the second call to group:insert():
local one_or_two = math.random(2)
local endGameButton, shuffleButton
if one_or_two == 1 the
endGameButton, shuffleButton = button1, button2
else
endGameButton, shuffleButton = button2, button1
end
At the end I'd replace your two addEventListener lines with:
shuffleButton:addEventListener("tap", randomize)
endGameButton:addEventListener("tap", generate)
And then I'd remove all the conditions (if buttonX == whatever) from your functions because they are no longer needed.
In other words: instead of testing which button is the random one every time something is clicked (as you're doing now), we assign each button a meaning.
Ok so in order to have the top button correspond with the characters, I needed to use the png files of the images as opposed to using a variable defined by those png images. in this case instead of using
if button2 = randomimage
use
if imageFile = "nameofimagefile.png"
To simplify my problem:
I have the following loop:
local arguments =
{
{ text="foo", x=0, y=0, font=native.systemFont, size=32 },
{ text="bar", x=0, y=0, font=native.systemFont, size=32 }
}
for _,item in ipairs( arguments ) do
local text = display.newText( item.text, item.x, item.y, item.font, item.size )
text:setFillColor( 1 ) -- white
text.x = 50 + 50 * i
text.y = 100
i = i + 1
text:addEventListener( "touch", onTouch )
end
The function onTouch is defined previously and it responds by allowing the user to drag the object around the screen.
The function works fine. However, I would like to be able to access the text of the object the user clicks in from within the onTouch function. For example, if a user clicks on text that contains the string "foo", I would be able to access this string and work with it. Is this possible? I am using Corona Starter (the free one) in case that is relevant.
Thanks in advance.
In short:
Yes, it is possible you should simply declare a variable with a string on the object as such:
text.string = "foo"
And then in your onTouch function you can reach it through
event.target.string
I want to draw a circle on LbuttonDown on Series. What I am doing is I am using DrawLine tool and adding a line on OnMouseDownTchart Event. But the Issue is as soon as i do Scroll Axis the line disappears.
Thanks
Akshay
I'm trying to reproduce the problem with the code below but it seems to work fine for me here. It adds a line in a random position each time I click on the chart. And I can still scroll the chart dragging the same with the right mouse button.
Private Sub Form_Load()
TChart1.Aspect.View3D = False
TChart1.AddSeries scPoint
Dim i As Integer
For i = 0 To 20
TChart1.Series(0).Add i, "", clTeeColor
Next i
TChart1.Tools.Add tcDrawLine
TChart1.Tools.Items(0).asDrawLine.EnableDraw = False
End Sub
Private Sub TChart1_OnMouseDown(ByVal Button As TeeChart.EMouseButton, ByVal Shift As TeeChart.EShiftState, ByVal X As Long, ByVal Y As Long)
With TChart1.Tools.Items(0).asDrawLine
.AddLine Rnd * 20, Rnd * 20, Rnd * 20, Rnd * 20
End With
End Sub
I'm using TeeChart ActiveX v2013.0.1.0.
Maybe you are doing something at OnMouseDown that enters in conflict with the scroll action.
If you still find problems with it, please, improve the question being more specific. Some code would be helpful. Also I'm not sure to understand what "LbuttonDown on Series" means.