Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Move view by xy coordinate position as opposed to offset  (Read 4209 times)

0 Members and 2 Guests are viewing this topic.

Anteara

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Move view by xy coordinate position as opposed to offset
« on: July 24, 2013, 07:05:52 am »
Hi, I'm using Winforms with the c# binding (not posting this in the c# binding forum as it's relevant to all of the c++ and others too) and have a question about moving the view.

I know that I can move it by offset, like 16, 0 to move it 16 on the X and 0 on the Y.

I'm doing that here, for my scrollbar:

        private void mapYScrollbar_Scroll(object sender, ScrollEventArgs e)
        {
            Console.WriteLine("Scrolling {0}, Old value {1}, New value {2}", mapHScrollbar.Value, e.OldValue, e.NewValue);
            if (e.OldValue < e.NewValue) //going up
            {
                Console.WriteLine("Going up!");
                gridWindow.DefaultView.Move(new Vector2f(0f, Constants.TileSize));
            }
            else if (e.OldValue > e.NewValue) //going down
            {
                Console.WriteLine("Going down!");
                gridWindow.DefaultView.Move(new Vector2f(0f, Constants.TileSize * -1)); //multiply by -1 to get a negative
            }
        }

However, It works most of the time, but I noticed that if I "jump" the scrollbar, i.e. change the value from 30 to 35, it still only moves 16 pixels, so its actually 16 * 4 pixels off of where the map should be, which bugs it out.

A solution could be to set the relative position to the scrollbar. i.e. if the value is 33 on the scrollbar, then the position is TileSize (16) * 33.

So basically if I move the Y scrollbar to the 33rd value, it would be at 528 on the Y axis. However, I'm not sure how I can specify exactly where I want it to be, as Move uses offset, and not a relative position.

Do you know how I could implement this? Thanks! :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Move view by xy coordinate position as opposed to offset
« Reply #1 on: July 24, 2013, 08:47:11 am »
Please read the documentation and tutorial carefully. Move is just a shortcut that uses the SetCenter function (which takes an absolute position).
Laurent Gomila - SFML developer

Anteara

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: Move view by xy coordinate position as opposed to offset
« Reply #2 on: July 24, 2013, 09:00:46 am »
Ah okay, sorry I missed that - Thanks, that will solve it :)

Edit: Okay cool, I got it working as per your suggestion, and I think the implementation is quite good (to my novice eyes at least  :-X) :D

private void mapXScrollbar_Scroll(object sender, ScrollEventArgs e)
{
    gridWindow.DefaultView.Center = new Vector2f(Constants.ViewXCenter + Constants.TileSize * e.NewValue, gridWindow.DefaultView.Center.Y);
}

private void mapYScrollbar_Scroll(object sender, ScrollEventArgs e)
{
    gridWindow.DefaultView.Center = new Vector2f(gridWindow.DefaultView.Center.X, Constants.ViewYCenter + Constants.TileSize * e.NewValue);
}

If you don't mind, I have one more question. Essentially, I've got a panel for my container, another panel for my sfml window, and two scrollbars for the scrolling, I did this so I can scale the window at runtime. It all works good and scales well.

One struggle I'm having however is that mouse clicks are registered even when I'm outside of the sfml window, but still in the WinForms window (Note that this happened in my c++ version when I used one window and multiple views, as opposed to one winforms window).

Basically, I think I need a way to stop events from being registered when the mouse is out of context, i.e. If i'm scrolling on the scrollbar, and the mouse is raised on to the map, since I'm holding down left click, It will go ahead and place tiles, even though I'm scrolling. This is not what I want, I want it to essentially "ignore" the map, while I'm scrolling.

A solution could be while scrolling implement a boolean... if not scrolling then allow tile placement, however I don't want to add a conditional check unless it's vital.

What i'm kind of looking for is something like:
*Scroll function called
*Disable view input
*Scroll function returns
*Enable view input

Or something similar. I also need the input to not register if the click is not within the view, at the moment it does.

        public void ChangeCurrentTileTemp(ref RenderWindow window, ref int tile, ref int mouseX, ref int mouseY)
        {
            Vector2f mouse = window.MapPixelToCoords(Mouse.GetPosition(window));

That's the start of the function, so I could add a conditional if not scrolling && if mouse within viewport x and y, but I just want to see if there's a more elegant solution first.

Do you know anything that I could do instead of implementing a conditional (which might reduce speed by a tiny bit)?

Edit, as per http://www.sfml-dev.org/tutorials/2.0/window-events.php I may be able to use LostFocus, and GainedFocus for the scrollbar scenario.
« Last Edit: July 24, 2013, 10:31:40 am by Anteara »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Move view by xy coordinate position as opposed to offset
« Reply #3 on: July 24, 2013, 11:14:32 am »
Quote
gridWindow.DefaultView.Center =
This is not supposed to work:
- you can't modify the default view, it's supposed to be read-only
- you must call SetView everytime your view changes, so that the window gets the updated view

Quote
implementing a conditional (which might reduce speed by a tiny bit)?
Are you serious? :P
Laurent Gomila - SFML developer

Anteara

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: Move view by xy coordinate position as opposed to offset
« Reply #4 on: July 24, 2013, 11:44:32 am »
Quote
gridWindow.DefaultView.Center =
This is not supposed to work:
- you can't modify the default view, it's supposed to be read-only
- you must call SetView everytime your view changes, so that the window gets the updated view

That's strange, because it works perfectly; however, i don't know about the first point you made, but to the second one I'm only using one view, as I have another SFML window attached to a seperate panel, because I have winforms controls wrapped around them.

Quote
Quote
implementing a conditional (which might reduce speed by a tiny bit)?
Are you serious? :P

Are you laughing at me because any performance hit wouldn't be noticeable? I'm still new to writing code to make it fast lol.
« Last Edit: July 24, 2013, 11:50:27 am by Anteara »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Move view by xy coordinate position as opposed to offset
« Reply #5 on: July 24, 2013, 11:52:59 am »
Quote
Are you laughing at me because any performance hit wouldn't be noticeable?
Yes. Testing a boolean is unlikely to have a noticeable effect on performances, compared to the huge amount of CPU cycles required by other common operations such as checking events, drawing stuff, etc.
Laurent Gomila - SFML developer