Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Handle mouse hovering in the screen.  (Read 2702 times)
0 Members and 1 Guest are viewing this topic.
Offline zengoku

Senior Newbie




Trying hard to program well >.<


« Posted 2012-03-29 21:31:30 »

Hi!

I've recently implement the inventory for my game wich shows in the screen a simple rect partitioned in 32 subrects showing all the items in characters inv. http://puu.sh/mZny

I'd like to  know how can i handle the mouse hovering of every item in the inventory to make the mouse listener work.

I mean, when hovering an item, right clicking will use/equip it and left clicking will able it to drag/drop it.

Thanks for help!

PS: I don't need code, just knowledge about how to do it Smiley
Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-03-29 22:51:47 »

What exactly don't you know how to do? This sounds like a basic mouse click/motion event problem.

Offline theagentd

« JGO Bitwise Duke »


Medals: 361
Projects: 2
Exp: 8 years



« Reply #2 - Posted 2012-03-29 23:51:54 »

Quote
Zengoku's avatar
Ka... Kawaii... OMOCHIKAERIII!!!

Hi!

I've recently implement the inventory for my game wich shows in the screen a simple rect partitioned in 32 subrects showing all the items in characters inv. http://puu.sh/mZny

I'd like to  know how can i handle the mouse hovering of every item in the inventory to make the mouse listener work.

I mean, when hovering an item, right clicking will use/equip it and left clicking will able it to drag/drop it.

Thanks for help!

PS: I don't need code, just knowledge about how to do it Smiley
I think what you're missing is a MouseMotionListener.

Myomyomyo.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline GabrielBailey74
« Reply #3 - Posted 2012-03-30 01:57:35 »

If you let a class 'implement' a 'MouseMotionListener', you than have access to methods like:
1  
2  
3  
4  
5  
6  
7  
8  
   // Anytime the mouse is moved we assign its X,Y.
   public void mouseMoved(MouseEvent e) { }

   // When the mouse leaves the screen.
   public void mouseExited(MouseEvent e) { }

   // When the mouse is inside of the screen.
   public void mouseEntered(MouseEvent e) { }


Here's a simple way of assigning a int to a rectangle, than creating a boolean to detect if the mouse's inside of the given rectangle's coordinates.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
public int mouseX, mouseY = 0;

      // So if the mouse is inside of this 40x40 rectangle at 40X, 40Y, return = true;
   public boolean mouseInSlot(int slot) {
      int x = mouseX;
      int y = mouseY;
      if (x > 40 && x < 80 && y > 40 && y < 80) {
         return true;
      } else {
         return false;
      }
   }

   // Anytime the mouse is moved we assign its X,Y.
   public void mouseMoved(MouseEvent e) {
      mouseX = e.getX();
      mouseY = e.getY();
   }


Sorry for posting code Angry hope it helps.

Offline theagentd

« JGO Bitwise Duke »


Medals: 361
Projects: 2
Exp: 8 years



« Reply #4 - Posted 2012-03-30 03:14:05 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
public int mouseX, mouseY = 0;

      // So if the mouse is inside of this 40x40 rectangle at 40X, 40Y, return = true;
   public boolean mouseInSlot(int slot) {
      int x = mouseX;
      int y = mouseY;
      if (x > 40 && x < 80 && y > 40 && y < 80) {
         return true;
      } else {
         return false;
      }
   }

   // Anytime the mouse is moved we assign its X,Y.
   public void mouseMoved(MouseEvent e) {
      mouseX = e.getX();
      mouseY = e.getY();
   }

Don't do this. Calculate which slot the mouse is over instead.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public int getHoveredSlot(int x, int y){
    if(!isInsideInventoryWindow(x, y)){
        return -1; //Outside window, no slot
    }
    return ((y - inventoryWindowY) / SLOT_HEIGHT) * INVENTORY_COLUMNS + (x - inventoryWindowX) / SLOT_WIDTH;
    //Example: window location = (0, 0), slot size = (10, 10), columns = 10, mouse position = (15, 15) = center of second row, second column = slot coordinates (1, 1) (starts at 0) = index 11.
    //((15 - 0) / 10) * 10 = ((15)/10)*10 = 1*10 = 10 (note the rounding)
    // +
    // (15 - 0) / 10 = 15/10 = 1
    // =
    // 11
}

Myomyomyo.
Offline Rorkien
« Reply #5 - Posted 2012-03-30 14:54:31 »

Don't do this. Calculate which slot the mouse is over instead.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public int getHoveredSlot(int x, int y){
    if(!isInsideInventoryWindow(x, y)){
        return -1; //Outside window, no slot
    }
    return ((y - inventoryWindowY) / SLOT_HEIGHT) * INVENTORY_COLUMNS + (x - inventoryWindowX) / SLOT_WIDTH;
    //Example: window location = (0, 0), slot size = (10, 10), columns = 10, mouse position = (15, 15) = center of second row, second column = slot coordinates (1, 1) (starts at 0) = index 11.
    //((15 - 0) / 10) * 10 = ((15)/10)*10 = 1*10 = 10 (note the rounding)
    // +
    // (15 - 0) / 10 = 15/10 = 1
    // =
    // 11
}


Its efficient, but OP has gaps between the inventory slots
Offline theagentd

« JGO Bitwise Duke »


Medals: 361
Projects: 2
Exp: 8 years



« Reply #6 - Posted 2012-03-30 15:41:10 »

Don't do this. Calculate which slot the mouse is over instead.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public int getHoveredSlot(int x, int y){
    if(!isInsideInventoryWindow(x, y)){
        return -1; //Outside window, no slot
    }
    return ((y - inventoryWindowY) / SLOT_HEIGHT) * INVENTORY_COLUMNS + (x - inventoryWindowX) / SLOT_WIDTH;
    //Example: window location = (0, 0), slot size = (10, 10), columns = 10, mouse position = (15, 15) = center of second row, second column = slot coordinates (1, 1) (starts at 0) = index 11.
    //((15 - 0) / 10) * 10 = ((15)/10)*10 = 1*10 = 10 (note the rounding)
    // +
    // (15 - 0) / 10 = 15/10 = 1
    // =
    // 11
}


Its efficient, but OP has gaps between the inventory slots
Aw, damn. I guess we'll just have to give up then. It's not like we're programmers who can just add an additional check to ensure we didn't hit the space between slots after using the method I posted...  Wink

Myomyomyo.
Offline Fokusas

Senior Devvie


Medals: 3
Projects: 1



« Reply #7 - Posted 2012-03-30 15:44:28 »

Unless space is really big you can ignore it  Grin
Offline Rorkien
« Reply #8 - Posted 2012-03-30 16:09:45 »

Aw, damn. I guess we'll just have to give up then. It's not like we're programmers who can just add an additional check to ensure we didn't hit the space between slots after using the method I posted...  Wink

Hah, sorry, i tend to get obvious sometimes  Tongue
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

trollwarrior1 (33 views)
2014-11-22 12:13:56

xFryIx (73 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (40 views)
2014-11-12 21:09:33

kovacsa (65 views)
2014-11-07 19:57:14

TehJavaDev (70 views)
2014-11-03 22:04:50

BurntPizza (68 views)
2014-11-03 18:54:52

moogie (83 views)
2014-11-03 06:22:04

CopyableCougar4 (82 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!