stoneharry

Untitled

Aug 24th, 2012
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.56 KB | None | 0 0
  1. --[[
  2. Status list:
  3. - CURRENT
  4. - REACHABLE
  5. - DISTANT
  6. - NONE
  7. ]]
  8. -- X, Y, Name, NumRoutes, Cost (100 = 1 silver), nodes accessable, npc name, status
  9. local coords_m = {
  10.     {0.4, 0.2, "Westfall", 1, 910, {2,3,4}, "[PH] Vanguard Landing Flight Master", "NONE"},
  11.     {0.6, 0.4, "Testing", 2, 100, {1, 3}, "stuff", "NONE"},
  12.     {0.5, 0.8, "Test2", 3, 1000, {1, 2, 4}, "stuff2", "DISTANT"},
  13.     {0.6, 0.8, "Test3", 1, 5033, {3}, "stuff3", "NONE"}
  14. }
  15. local current = 0
  16.  
  17. TAXI_MAP_WIDTH = 316;
  18. TAXI_MAP_HEIGHT = 352;
  19. NUM_TAXI_BUTTONS = 0;
  20. NUM_TAXI_ROUTES = 0;
  21.  
  22. TaxiButtonTypes = { };
  23. TaxiButtonTypes["CURRENT"] = {
  24.     file = "Interface\\TaxiFrame\\UI-Taxi-Icon-Green"
  25. }
  26. TaxiButtonTypes["REACHABLE"] = {
  27.     file = "Interface\\TaxiFrame\\UI-Taxi-Icon-White"
  28. }
  29. TaxiButtonTypes["DISTANT"] = {
  30.     file = "Interface\\TaxiFrame\\UI-Taxi-Icon-Yellow"
  31. }
  32.  
  33. TAXI_BUTTON_HALF_WIDTH = 8;
  34. TAXI_BUTTON_HALF_HEIGHT = 8;
  35.  
  36.  
  37. function TaxiFrame_OnLoad(self)
  38.     self:RegisterEvent("TAXIMAP_OPENED");
  39.     self:RegisterEvent("TAXIMAP_CLOSED");
  40. end
  41.  
  42. function TaxiFrame_OnEvent(self, event, ...)
  43.     if ( event == "TAXIMAP_OPENED" ) then
  44.         -- Show the merchant we're dealing with
  45.         local name = UnitName("npc")
  46.         TaxiMerchant:SetText(name);
  47.         SetPortraitTexture(TaxiPortrait, "npc");
  48.  
  49.         current = 0
  50.         for i=1, #coords_m do
  51.             if name == coords_m[i][7] then
  52.                 current = i
  53.                 coords_m[current][8] = "CURRENT";
  54.             end
  55.         end
  56.         if current == 0 then
  57.             UIErrorsFrame:AddMessage("ERROR: No path find for this creature.", 1.0, 0.1, 0.1, 1.0);
  58.             HideUIPanel(TaxiFrame);
  59.             return;
  60.         end
  61.         for k,v in pairs(coords_m[current][6]) do
  62.             coords_m[v][8] = "REACHABLE";
  63.         end
  64.  
  65.         -- Set the texture coords on the map
  66.         TaxiMap:SetTexCoord(0,1,0,1);
  67.         SetTaxiMap(TaxiMap);
  68.  
  69.         -- Show the taxi node map and buttons
  70.         local num_nodes = #coords_m
  71.         if ( num_nodes > NUM_TAXI_BUTTONS ) then
  72.             local button;
  73.             for i = NUM_TAXI_BUTTONS+1, num_nodes do
  74.                 button = CreateFrame("Button", "TaxiButton"..i, TaxiRouteMap, "TaxiButtonTemplate");
  75.                 button:SetID(i);
  76.             end
  77.         end
  78.        
  79.         -- Draw nodes
  80.         local taxiNodePositions = {};
  81.         local numValidFlightNodes = 0;
  82.         for index = 1, #coords_m do
  83.             local type = coords_m[index][8]
  84.             local button = _G["TaxiButton"..index];
  85.             taxiNodePositions[index] = {};
  86.             if ( type ~= "NONE" ) then
  87.                 numValidFlightNodes = numValidFlightNodes + 1;
  88.                 local x, y = coords_m[index][1], coords_m[index][2]
  89.                 local currX = x*TAXI_MAP_WIDTH;
  90.                 local currY = y*TAXI_MAP_HEIGHT;
  91.                 taxiNodePositions[index].x = currX;
  92.                 taxiNodePositions[index].y = currY;
  93.                 -- check if we are obscuring a previous placement (eg: Ebon Hold and Light's Hope Chapel)
  94.                 --[[if ( numValidFlightNodes > 1 ) then
  95.                     for checkNode = 1, index do
  96.                         local checkX = taxiNodePositions[checkNode].x;
  97.                         local checkY = taxiNodePositions[checkNode].y;
  98.                         if ( taxiNodePositions[checkNode].x ) then
  99.                             if ( (currX > checkX - TAXI_BUTTON_HALF_WIDTH) and (currX < checkX + TAXI_BUTTON_HALF_WIDTH) ) then
  100.                                 if ( (currY > checkY - TAXI_BUTTON_HALF_HEIGHT) and (currY < checkY + TAXI_BUTTON_HALF_HEIGHT) ) then
  101.                                     taxiNodePositions[index].x = currX + (currX - checkX) * 0.5;
  102.                                     taxiNodePositions[index].y = currY + (currY - checkY) * 0.5;
  103.                                     taxiNodePositions[checkNode].x = checkX + (checkX - currX) * 0.5;
  104.                                     taxiNodePositions[checkNode].y = checkY + (checkY - currY) * 0.5;
  105.                                 end
  106.                             end
  107.                         end
  108.                     end
  109.                 end]]
  110.                 -- set the button position
  111.                 button:ClearAllPoints();
  112.                 button:SetPoint("CENTER", "TaxiMap", "BOTTOMLEFT", taxiNodePositions[index].x, taxiNodePositions[index].y);
  113.                 button:SetNormalTexture(TaxiButtonTypes[type].file);
  114.                 button:Show();
  115.             else
  116.                 button:Hide();
  117.             end
  118.         end
  119.  
  120.         if ( num_nodes > NUM_TAXI_BUTTONS ) then
  121.             NUM_TAXI_BUTTONS = num_nodes
  122.         end
  123.  
  124.         -- All set...
  125.         ShowUIPanel(self);
  126.         if ( not self:IsShown() ) then
  127.             CloseTaxiMap();
  128.         end
  129.         return;
  130.     end
  131.     if ( event == "TAXIMAP_CLOSED" ) then
  132.         HideUIPanel(self);
  133.         return;
  134.     end
  135. end
  136.  
  137. function TaxiNodeOnButtonEnter(button)
  138.     local index = button:GetID();
  139.     GameTooltip:SetOwner(button, "ANCHOR_RIGHT");
  140.     GameTooltip:AddLine(coords_m[index][3], "", 1.0, 1.0, 1.0);
  141.    
  142.     -- Setup variables
  143.     local numRoutes = coords_m[current][4];
  144.     local line;
  145.     local sX, sY, dX, dY;
  146.     local w = TaxiRouteMap:GetWidth();
  147.     local h = TaxiRouteMap:GetHeight();
  148.    
  149.     local type = coords_m[index][8]
  150.     if ( type == "REACHABLE" ) then
  151.         SetTooltipMoney(GameTooltip, coords_m[index][5]);
  152.         TaxiNodeSetCurrent(index);
  153.        
  154.         if ( numRoutes > NUM_TAXI_ROUTES ) then
  155.             for i = NUM_TAXI_ROUTES+1, numRoutes do
  156.                 line = TaxiRouteMap:CreateTexture("TaxiRoute"..i, "BACKGROUND");
  157.                 line:SetTexture("Interface\\TaxiFrame\\UI-Taxi-Line");
  158.             end
  159.             NUM_TAXI_ROUTES = numRoutes;
  160.         end
  161.  
  162.         for i=1, NUM_TAXI_ROUTES do
  163.             line = _G["TaxiRoute"..i];
  164.             if ( i <= numRoutes ) then
  165.                 sX = coords_m[current][1]*w;
  166.                 sY = coords_m[current][2]*h;
  167.                 dX = coords_m[index][1]*w;
  168.                 dY = coords_m[index][2]*h;
  169.                 DrawRouteLine(line, "TaxiRouteMap", sX, sY, dX, dY, 32);
  170.                 line:Show();
  171.             else
  172.                 line:Hide();
  173.             end
  174.         end
  175.     elseif ( type == "CURRENT" ) then
  176.         GameTooltip:AddLine(TAXINODEYOUAREHERE, "", 0.5, 1.0, 0.5);
  177.         DrawOneHopLines();
  178.     end
  179.  
  180.     GameTooltip:Show();
  181. end
  182.  
  183. -- Draw all flightpaths within one hop of current location
  184. function DrawOneHopLines()
  185.     local line;
  186.     local sX, sY, dX, dY;
  187.     local w = TaxiRouteMap:GetWidth();
  188.     local h = TaxiRouteMap:GetHeight();
  189.     local numNodes = #coords_m
  190.     local numLines = 0;
  191.     local numSingleHops = 0;
  192.     for i=1, numNodes  do
  193.         if ( GetNumRoutes(i) == 1 ) then
  194.             numSingleHops = numSingleHops + 1;
  195.             numLines = numLines + 1;
  196.             if ( numLines > NUM_TAXI_ROUTES ) then
  197.                 line = TaxiRouteMap:CreateTexture("TaxiRoute"..numLines, "BACKGROUND");
  198.                 line:SetTexture("Interface\\TaxiFrame\\UI-Taxi-Line");
  199.                 NUM_TAXI_ROUTES = numLines;
  200.             else
  201.                 line = _G["TaxiRoute"..numLines];
  202.             end
  203.             if ( line ) then
  204.                 sX = coords_m[current][1]*w;
  205.                 sY = coords_m[current][2]*h;
  206.                 dX = coords_m[current][1]*w;
  207.                 dY = coords_m[current][2]*h;
  208.                 DrawRouteLine(line, "TaxiRouteMap", sX, sY, dX, dY, 32);
  209.                 line:Show();
  210.             end
  211.         end
  212.     end
  213.     for i=numLines+1, NUM_TAXI_ROUTES do
  214.         _G["TaxiRoute"..i]:Hide();
  215.     end
  216.     if ( numSingleHops == 0 ) then
  217.         UIErrorsFrame:AddMessage(ERR_TAXINOPATHS, 1.0, 0.1, 0.1, 1.0);
  218.         HideUIPanel(TaxiFrame);
  219.     end
  220. end
  221.  
  222.  
  223. -- The following function is used with permission from Daniel Stephens <[email protected]>
  224. TAXIROUTE_LINEFACTOR = 32/30; -- Multiplying factor for texture coordinates
  225. TAXIROUTE_LINEFACTOR_2 = TAXIROUTE_LINEFACTOR / 2; -- Half o that
  226.  
  227. -- T        - Texture
  228. -- C        - Canvas Frame (for anchoring)
  229. -- sx,sy    - Coordinate of start of line
  230. -- ex,ey    - Coordinate of end of line
  231. -- w        - Width of line
  232. -- relPoint - Relative point on canvas to interpret coords (Default BOTTOMLEFT)
  233. function DrawRouteLine(T, C, sx, sy, ex, ey, w, relPoint)
  234.    if (not relPoint) then relPoint = "BOTTOMLEFT"; end
  235.  
  236.    -- Determine dimensions and center point of line
  237.    local dx,dy = ex - sx, ey - sy;
  238.    local cx,cy = (sx + ex) / 2, (sy + ey) / 2;
  239.  
  240.    -- Normalize direction if necessary
  241.    if (dx < 0) then
  242.       dx,dy = -dx,-dy;
  243.    end
  244.  
  245.    -- Calculate actual length of line
  246.    local l = sqrt((dx * dx) + (dy * dy));
  247.  
  248.    -- Quick escape if it's zero length
  249.    if (l == 0) then
  250.       T:SetTexCoord(0,0,0,0,0,0,0,0);
  251.       T:SetPoint("BOTTOMLEFT", C, relPoint, cx,cy);
  252.       T:SetPoint("TOPRIGHT",   C, relPoint, cx,cy);
  253.       return;
  254.    end
  255.  
  256.    -- Sin and Cosine of rotation, and combination (for later)
  257.    local s,c = -dy / l, dx / l;
  258.    local sc = s * c;
  259.  
  260.    -- Calculate bounding box size and texture coordinates
  261.    local Bwid, Bhgt, BLx, BLy, TLx, TLy, TRx, TRy, BRx, BRy;
  262.    if (dy >= 0) then
  263.       Bwid = ((l * c) - (w * s)) * TAXIROUTE_LINEFACTOR_2;
  264.       Bhgt = ((w * c) - (l * s)) * TAXIROUTE_LINEFACTOR_2;
  265.       BLx, BLy, BRy = (w / l) * sc, s * s, (l / w) * sc;
  266.       BRx, TLx, TLy, TRx = 1 - BLy, BLy, 1 - BRy, 1 - BLx;
  267.       TRy = BRx;
  268.    else
  269.       Bwid = ((l * c) + (w * s)) * TAXIROUTE_LINEFACTOR_2;
  270.       Bhgt = ((w * c) + (l * s)) * TAXIROUTE_LINEFACTOR_2;
  271.       BLx, BLy, BRx = s * s, -(l / w) * sc, 1 + (w / l) * sc;
  272.       BRy, TLx, TLy, TRy = BLx, 1 - BRx, 1 - BLx, 1 - BLy;
  273.       TRx = TLy;
  274.    end
  275.  
  276.    -- Set texture coordinates and anchors
  277.    T:ClearAllPoints();
  278.    T:SetTexCoord(TLx, TLy, BLx, BLy, TRx, TRy, BRx, BRy);
  279.    T:SetPoint("BOTTOMLEFT", C, relPoint, cx - Bwid, cy - Bhgt);
  280.    T:SetPoint("TOPRIGHT",   C, relPoint, cx + Bwid, cy + Bhgt);
  281. end
Advertisement
Add Comment
Please, Sign In to add comment