FANDOM


local p = {}
function p.layermap( f )
  local args = f:getParent().args
  -- set default values (borderCoords are for top, right, bottom, left)
  local mapFile, borderCoords, mapsize, markersize, markercolor, markericon, opacity, text, float, linking = 'Map The Island.jpg', {t=7.2,r=92.8,b=92.8,l=7.2}, 300, 10, '#f40','', 1, '', '', false
 
  local uniqueSeed = 0
 
  ---- get values from parameters
 
  -- default values for main maps
  if args.map ~= nil then
    if args.map == 'The Center' then
      mapFile = 'The Center Map.jpg'
      borderCoords = {t=-2.0,r=100.0,b=100.0,l=0.0}
    elseif      args.map == 'Scorched Earth' then
      mapFile = 'Scorched Earth Map.jpg'
      borderCoords = {t=0,r=100,b=100,l=0}
    end
  end
  if args.mapsize ~= nil then
    mapsize = args.mapsize
  end
  if args.markersize ~= nil then
    markersize = args.markersize
  end
  if args.markercolor ~= nil then
    markercolor = args.markercolor
  end
  if args.markericon ~= nil then
    markericon = args.markericon
  end
  if args.opacity ~= nil then
    opacity = args.opacity
  end
  if args.text ~= nil then
    text = args.text
  end
 
  if args.float ~= nil then
    float = args.float
  end
  if #float > 0 then
    float = 'float:'..float
  end
 
  if args.linking ~= nil and string.lower(args.linking) == 'true' then
    linking = true
  end
  if args.borderCoordT ~= nil then
    borderCoords.t = args.borderCoordT
  end
  if args.borderCoordR ~= nil then
    borderCoords.r = args.borderCoordR
  end
  if args.borderCoordB ~= nil then
    borderCoords.b = args.borderCoordB
  end
  if args.borderCoordL ~= nil then
    borderCoords.l = args.borderCoordL
  end
 
  -- variables for a single marker:
  -- 1: lat, 2: lon, 3: mSize (markersize), 4: mcolor (markercolor), 5: mtext (markertext/title/tooltip/link if linking==true), 6: layerName, 7+: micons (markericons), mlinkicon (markerlink for icons), mlink (markerlink for non-icons)
  local layers, layerNames, currentLayer, locations, lat, lon, mSize, mcolor, mtext, micons, mlinkicon, mlink = {}, {}, '', {}, 0, 0, 0, '','', {}, '', ''
  for _,l in ipairs(args) do
    mSize = markersize
    mcolor = markercolor
    micons = {markericon}
 
    -- extract parts from entry (csv)
    local parts,i = {},0
    for part in string.gmatch(l..',', "([^,]*),") do
      table.insert(parts,part:match "^%s*(.-)%s*$") -- trim
    end
 
    if #parts > 1 then
      lat = parts[1]
      uniqueSeed = uniqueSeed + lat
      lon = parts[2]
      mtext = 'lat '..lat..', lon '..lon
      if #parts > 2 and string.len(parts[3])>0 then
        mSize = parts[3]
      end
      if #parts > 3 and string.len(parts[4])>0 then
        mcolor = parts[4]
      end
      if #parts > 4 and string.len(parts[5])>0 then
        mtext = parts[5]..'
'..mtext
        if linking then
          mlink = '<a href="/'..parts[5]..'" styles="width:100%;height:100%" class="image" title="'..mtext..'"></a>'
          mlinkicon = '|link=' --remove linking from image so other link in the line above is used
          mtext = ''
        end
      end
      -- if new layername is given, save locations so far in layer and start with a fresh locations-table
      if #parts > 5 and string.len(parts[6])>0 and currentLayer ~= parts[6] then
        if #locations > 0 then
          table.insert(layers,locations)
          table.insert(layerNames,currentLayer)
        end
        currentLayer = parts[6]
        locations = {}
      end
      i=6
      while #parts > i do
        micons[i-5] = parts[i+1]
        i = i + 1
      end
 
      if #micons > 0 and string.len(micons[1]) > 0 then
        table.insert(locations,'<div style="position:absolute;line-height:0;left:'.. 100*((lon-borderCoords.l)/(borderCoords.r-borderCoords.l) - mSize/(2*mapsize)) ..'%;top:'.. 100*((lat-borderCoords.t)/(borderCoords.b-borderCoords.t) - mSize/(2*mapsize)) ..'%;padding:0;opacity:'..opacity..'" title="'..mtext..'"><div style="position:absolute">[[File:'..table.concat(micons,'|'..mSize..'px'..mlinkicon..']]</div><div style="position:absolute">[[File:')..'|'..mSize..'px'..mlinkicon..']]</div>'..mlink..'</div>')
      else
        table.insert(locations,'<div style="position:absolute;line-height:0;left:'.. 100*((lon-borderCoords.l)/(borderCoords.r-borderCoords.l) - mSize/(2*mapsize)) ..'%;top:'.. 100*((lat-borderCoords.t)/(borderCoords.b-borderCoords.t) - mSize/(2*mapsize)) ..'%;padding:0;width:'..mSize..'px;height:'..mSize..'px;border-radius:50%;background-color:'..mcolor..';border:1px solid black;opacity:'..opacity..'" title="'..mtext..'">'..mlink..'</div>')
      end
    end
  end
 
  -- last layer
  if #locations > 0 then
    table.insert(layers,locations)
    table.insert(layerNames,currentLayer)
  end
 
  -- random number for making ids unique TODO find better way to generate unique number
  math.randomseed(uniqueSeed + #layerNames)
  local unique = math.random(1,1000)
 
  local subtitle, layerControls, layerContents = '', '', {}
  if text ~= '' then
    subtitle = '<tr><td align="middle">'..text..'</td></tr>'
  end
  for _,l in ipairs(layerNames) do
    layerControls = layerControls..'<span class="pointer layerMapToggleButton" data-forid="layerMap-'..unique..'-'..l:gsub('%s','-')..'">'..l..'</span> '
  end
  if layerControls ~= '' then
    layerControls = '<tr><td align="middle">'..layerControls..'</td></tr>'
  end
  for i,l in ipairs(layers) do
    table.insert(layerContents,'<div id="layerMap-'..unique..'-'..layerNames[i]:gsub('%s','-')..'">'..table.concat(l)..'</div>')
  end
  return '<table class="wikitable layerMap" style="'..float..'"><tr><td><div class="noviewer" style="position: relative;width:'..mapsize..'px;height:'..mapsize..'px">'..table.concat(layerContents)..'[[File:'..mapFile..'|'..mapsize..'px]]</div></td></tr>'..subtitle..layerControls..'</table>'
end
return p