mouse over hotspot -display name of hotspot over coursor

Started by julius, Tue 29/06/2010 06:30:22

Previous topic - Next topic

Khris


Snarky

Quote from: Dualnames on Wed 21/09/2011 06:19:14
if you give us more details about that "bunch of code", perhaps we could come up with something.

I guess that was directed at me? I'll just post the code, if you're interested. I'm sure this can be cleaned up and condensed, but at least it seems to work:

Code: ags

#define TOOLTIPWIDTH 50

bool _oldhighlight = false;
int _olddir = 0; // for exit pointers in various directions. Numbered 1-4 in the order up, right, down, left; 0 is regular pointer
bool _clickable = true;

String[] Split(this String*, String divider)
{
  String heads[] = new String[this.Length];
  int i;

  String tail = this;
  int nextDivider = this.IndexOf(divider);
  while(nextDivider != -1)
  {
    heads[i] = tail.Substring(0, nextDivider);
    tail = tail.Substring(nextDivider+divider.Length, tail.Length - nextDivider-divider.Length);
    nextDivider = tail.IndexOf(divider);
    i++;
  }
  if(tail.Length>0)
    heads[i] = tail;
  
  return heads;
}

void DimToolTip(String text)
{
  int width = GetTextWidth(text, eFontMiniFontOutline);
  int height = GetTextHeight(text, eFontMiniFontOutline, TOOLTIPWIDTH+1);
  int lines = height / GetTextHeight(text, eFontMiniFontOutline, GetTextWidth(text, eFontMiniFontOutline)+1);
  if(width > TOOLTIPWIDTH)
  {
    // Set the width to the minimum width that doesn't wrap over more lines than TOOLTIPWIDTH does
    width = TOOLTIPWIDTH;
    while(GetTextHeight(text, eFontMiniFontOutline, width) == height)
      width--;
    width++;
    
    // If any individual words are longer, set the width to that
    String tooltipwords[] = text.Split(" ");
    int i = 0;
    while(tooltipwords[i] != null)
    {
      int wordwidth = GetTextWidth(tooltipwords[i], eFontMiniFontOutline);
      if(width < wordwidth)
        width = wordwidth;
      i++;
    }
  }
  
  // Set the dimensions of the tooltip
  gHotspot.Width = width+1;
  gHotspot.Height = height+lines;
  lblHotspot.Width = width+1;
  lblHotspot.Height = height+lines;
}

function SetHighlight(bool forceupdate)
{
  if(_uimode == eUiTwoButton)
  {
    bool highlight = false;
    int dir = 0;
    String tooltip = "";
    
    // Check if the cursor is over an interactive GUI control 
    GUIControl* gc =  GUIControl.GetAtScreenXY(mouse.x, mouse.y);
    if(gc != null)
    {
      Button* b = gc.AsButton;  // if gc is a button, b points to it, otherwise b is null
      ListBox* lb = gc.AsListBox;
      Slider*  s = gc.AsSlider;
      TextBox* tb = gc.AsTextBox;
      if (b != null || lb != null || s != null || tb != null)
      {
        highlight = true;
      }
      else
      {
        // If we're over an inventory window, check if we're actually over an inventory item
        InventoryItem* ii = InventoryItem.GetAtScreenXY(mouse.x, mouse.y);
        if(ii != null)
        {
          highlight = true;
          tooltip = ii.Name;
        }
        /*
        InvWindow* iw = gc.AsInvWindow;
        if(iw != null)
        {
          GUI* g = GUI.GetAtScreenXY(mouse.x, mouse.y);
          if(mouse.x < g.X + iw.X + iw.ItemWidth*(iw.ItemCount-iw.TopItem))
            highlight = true;
        }
        */
        
      }
      
      // This bit should be replaced by putting the tooltip as the button label in invisible font, and reading that
      if(gc == gmSaveButton)
        tooltip = "Save";
      else if(gc == gmLoadButton)
        tooltip = "Load";
      else if(gc == gmQuitButton)
        tooltip = "Quit";
    }
    
    
    else if (_clickable)  // If game is clickable, check if the cursor is over anything else interactive
    {
      //InventoryItem* ii = InventoryItem.GetAtScreenXY(mouse.x, mouse.y);
      LocationType ltype = GetLocationType(mouse.x, mouse.y);
      if (ltype != eLocationNothing)
      {
        if(ltype == eLocationHotspot)
        {
          Hotspot* hs = Hotspot.GetAtScreenXY(mouse.x, mouse.y);
          if(hs != null)
          {
            dir = hs.GetProperty("ExitDirection");
            tooltip = hs.Name;
          }
        }
        else if (ltype == eLocationObject)
        {
          Object* o = Object.GetAtScreenXY(mouse.x, mouse.y);
          if(o != null)
          {
            dir = o.GetProperty("ExitDirection");
            tooltip = o.Name;
          }
        }
        else if(ltype == eLocationCharacter)
        {
          Character* c = Character.GetAtScreenXY(mouse.x, mouse.y);
          if(c != null)
          {
            tooltip = c.Name;
          }
        }
        highlight = true;
      }
    }
    
    // Highlight cursor if moved over something active (or force update), or direction has changed
    if(highlight && (!_oldhighlight || forceupdate || dir != _olddir))
    {
      SetCursorMode(dir);
    }
    else if (!highlight && (_oldhighlight || forceupdate)) // Un-highlight cursor if moved away from something active (or force update)
    {
      SetCursorMode(-1);
    }

    // When game isn't clickable, show the tooltip (huh?)
    gHotspot.Visible = (mouse.Mode != eModeWait || !IsGameClickable());
    
    if(tooltip != null && tooltip.Length>0 && tooltip != lblHotspot.Text)
      DimToolTip(tooltip);

    int gHotspotX;
    int gHotspotY;
    // Show a tooltip by the cursor if over a hotspot/object/character
    if(dir == 1)  // up
    {
      gHotspotX = mouse.x - gHotspot.Width/2;
      gHotspotY = mouse.y + 11;
    }
    else if(dir == 2)  // right
    {
      gHotspotX = mouse.x - gHotspot.Width - 9;
      gHotspotY = mouse.y - gHotspot.Height/2;
    }
    else if (dir == 3) // down
    {
      gHotspotX = mouse.x - gHotspot.Width/2;
      gHotspotY = mouse.y - gHotspot.Height - 9;
    }
    else if (dir == 4) // left
    {
      gHotspotX = mouse.x + 11;
      gHotspotY = mouse.y - gHotspot.Height/2;
    }
    else // pointer
    {
      gHotspotX = mouse.x - gHotspot.Width/2;
      gHotspotY = mouse.y - gHotspot.Height;
    }
    if(gHotspotX < 1)
      gHotspotX = 1;
    else if(gHotspotX + gHotspot.Width >= System.ViewportWidth)
      gHotspotX = System.ViewportWidth - gHotspot.Width -1;
    if(gHotspotY < 1)
      gHotspotY = 1;
    
    gHotspot.X = gHotspotX;
    gHotspot.Y = gHotspotY;
    
    lblHotspot.Text = tooltip;

    _oldhighlight = highlight;
    _olddir = dir;
  }
}

SMF spam blocked by CleanTalk