Skip to content

Commit

Permalink
Reverted 91c1517 and reinserted the old mouse cursor code
Browse files Browse the repository at this point in the history
With SDL_RenderSetLogicalSize() gone the old mouse code works again
and it doesn't glitch the mouse pointer on magnification changes as
the new code does.
  • Loading branch information
Grumbel committed Aug 1, 2014
1 parent a432e4e commit 7421a76
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 84 deletions.
99 changes: 37 additions & 62 deletions src/gui/menu.cpp
Expand Up @@ -768,78 +768,53 @@ Menu::event(const SDL_Event& event)
return;

switch(event.type) {
case SDL_MOUSEBUTTONUP:
case SDL_MOUSEBUTTONDOWN:
if(event.button.button == SDL_BUTTON_LEFT)
{
Vector mouse_pos = Renderer::instance()->to_logical(event.motion.x, event.motion.y);
int x = int(mouse_pos.x);
int y = int(mouse_pos.y);

if(x > pos.x - get_width()/2 &&
x < pos.x + get_width()/2 &&
y > pos.y - get_height()/2 &&
y < pos.y + get_height()/2)
{
Vector mouse_pos = Renderer::instance()->to_logical(event.button.x, event.button.y);

if(MouseCursor::current())
{
MouseCursor::current()->set_pos(mouse_pos);
}
menuaction = MENU_ACTION_HIT;
}
break;
}
break;

case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEMOTION:
{
Vector mouse_pos = Renderer::instance()->to_logical(event.motion.x, event.motion.y);
float x = mouse_pos.x;
float y = mouse_pos.y;

if(x > pos.x - get_width()/2 &&
x < pos.x + get_width()/2 &&
y > pos.y - get_height()/2 &&
y < pos.y + get_height()/2)
{
Vector mouse_pos = Renderer::instance()->to_logical(event.button.x, event.button.y);
int new_active_item
= static_cast<int> ((y - (pos.y - get_height()/2)) / 24);

if(MouseCursor::current())
{
MouseCursor::current()->set_pos(mouse_pos);
}
/* only change the mouse focus to a selectable item */
if ((items[new_active_item]->kind != MN_HL)
&& (items[new_active_item]->kind != MN_LABEL)
&& (items[new_active_item]->kind != MN_INACTIVE))
active_item = new_active_item;

if(event.button.button == SDL_BUTTON_LEFT)
{
int x = int(mouse_pos.x);
int y = int(mouse_pos.y);

if(x > pos.x - get_width()/2 &&
x < pos.x + get_width()/2 &&
y > pos.y - get_height()/2 &&
y < pos.y + get_height()/2)
{
menuaction = MENU_ACTION_HIT;
}
}
if(MouseCursor::current())
MouseCursor::current()->set_state(MC_LINK);
}
break;

case SDL_MOUSEMOTION:
else
{
Vector mouse_pos = Renderer::instance()->to_logical(event.motion.x, event.motion.y);

if(MouseCursor::current())
{
MouseCursor::current()->set_pos(mouse_pos);
}

float x = mouse_pos.x;
float y = mouse_pos.y;

if(x > pos.x - get_width()/2 &&
x < pos.x + get_width()/2 &&
y > pos.y - get_height()/2 &&
y < pos.y + get_height()/2)
{
int new_active_item
= static_cast<int> ((y - (pos.y - get_height()/2)) / 24);

/* only change the mouse focus to a selectable item */
if ((items[new_active_item]->kind != MN_HL)
&& (items[new_active_item]->kind != MN_LABEL)
&& (items[new_active_item]->kind != MN_INACTIVE))
active_item = new_active_item;

if(MouseCursor::current())
MouseCursor::current()->set_state(MC_LINK);
}
else
{
if(MouseCursor::current())
MouseCursor::current()->set_state(MC_NORMAL);
}
MouseCursor::current()->set_state(MC_NORMAL);
}
break;
}
break;

default:
break;
Expand Down
25 changes: 9 additions & 16 deletions src/gui/mousecursor.cpp
Expand Up @@ -26,7 +26,6 @@
MouseCursor* MouseCursor::current_ = 0;

MouseCursor::MouseCursor(std::string cursor_file) :
mouse_pos(),
mid_x(0),
mid_y(0),
state_before_click(),
Expand All @@ -52,12 +51,6 @@ void MouseCursor::set_state(int nstate)
cur_state = nstate;
}

void
MouseCursor::set_pos(const Vector& pos)
{
mouse_pos = pos;
}

void MouseCursor::set_mid(int x, int y)
{
mid_x = x;
Expand All @@ -69,17 +62,17 @@ void MouseCursor::draw(DrawingContext& context)
if(cur_state == MC_HIDE)
return;

// Not using coordinates from mouse, as they are in the wrong
// coordinate system, see:
// https://bugzilla.libsdl.org/show_bug.cgi?id=2442
Uint8 ispressed = SDL_GetMouseState(NULL, NULL);
int x,y,w,h;
Uint8 ispressed = SDL_GetMouseState(&x,&y);

Vector mouse_pos = Renderer::instance()->to_logical(x, y);

int x = int(mouse_pos.x);
int y = int(mouse_pos.y);
x = int(mouse_pos.x);
y = int(mouse_pos.y);

int w = (int) cursor->get_width();
int h = (int) (cursor->get_height() / MC_STATES_NB);
if(ispressed & SDL_BUTTON(1) || ispressed & SDL_BUTTON(2)) {
w = (int) cursor->get_width();
h = (int) (cursor->get_height() / MC_STATES_NB);
if(ispressed &SDL_BUTTON(1) || ispressed &SDL_BUTTON(2)) {
if(cur_state != MC_CLICK) {
state_before_click = cur_state;
cur_state = MC_CLICK;
Expand Down
6 changes: 0 additions & 6 deletions src/gui/mousecursor.hpp
Expand Up @@ -19,7 +19,6 @@

#include <string>

#include "math/vector.hpp"
#include "video/surface_ptr.hpp"

#define MC_STATES_NB 3
Expand All @@ -32,7 +31,6 @@ enum {
};

class DrawingContext;
class Vector;

/// Mouse cursor.
/** Used to create mouse cursors.
Expand All @@ -56,9 +54,6 @@ class MouseCursor
/** Useful for cross mouse cursor images in example. */
void set_mid(int x, int y);

/** Set the position where the cursor should appear */
void set_pos(const Vector& pos);

/// Draw MouseCursor on screen.
void draw(DrawingContext& context);

Expand All @@ -72,7 +67,6 @@ class MouseCursor
friend class Resources;

private:
Vector mouse_pos;
int mid_x;
int mid_y;
int state_before_click;
Expand Down

0 comments on commit 7421a76

Please sign in to comment.