"""   QuArK  -  Quake Army Knife
"""
#
# Copyright (C) 2003 Harry Kalogirou
#

Info = {
   "plug-in":       "Sylphis entities line support",
   "desc":          "Displays varius lines assosiated with Sylphis.",
   "date":          "23 Feb 2003",
   "author":        "Harry Kalogirou",
   "author e-mail": "harkal@gmx.net",
   "quark":         "Version 6.4" }


import math, string
from quarkpy.maputils import *
import quarkpy.mapentities
import quarkpy.qeditor
import quarkpy.qhandles

DefaultDrawEntityLines = quarkpy.mapentities.DefaultDrawEntityLines
ObjectOrigin = quarkpy.mapentities.ObjectOrigin

import plugins.deckerutils
FindOriginFlagPolyPos = plugins.deckerutils.FindOriginFlagPolyPos

class SylphisDrawEntityLines(DefaultDrawEntityLines):
    def drawentitylines(self, entity, org, view, entities, processentities):
        # Draw the default target/targetname/killtarget/light/_light arrows/ellipse
        DefaultDrawEntityLines.drawentitylines(self, entity, org, view, entities, processentities)
        rad = entity['radius']
        
        if rad:
            o = string.split(entity['radius'])
            if len(o) == 1: o = (o[0], o[0], o[0])
            elif len(o) == 2: o = (o[0], o[1], (o[0] + o[1]) / 2)
            try:
                radius = quarkx.vect(float(o[0]), float(o[1]), float(o[2]))
            except:
                radius = quarkx.vect(10.0, 10.0, 10.0)
            cx, cy = [], []
            up = view.proj(quarkx.vect(org.x, org.y, org.z + radius.z))
            down = view.proj(quarkx.vect(org.x, org.y, org.z - radius.z))
            right = view.proj(quarkx.vect(org.x, org.y + radius.y, org.z))
            left = view.proj(quarkx.vect(org.x, org.y - radius.y, org.z))
            front = view.proj(quarkx.vect(org.x + radius.x, org.y, org.z))
            back = view.proj(quarkx.vect(org.x - radius.x, org.y, org.z))

            cx.append(up.x); cx.append(down.x); cx.append(left.x); cx.append(right.x)
            cx.append(front.x); cx.append(back.x);
            maxX = max(cx)
            minX = min(cx)

            cy.append(up.y); cy.append(down.y); cy.append(left.y); cy.append(right.y)
            cy.append(front.y); cy.append(back.y);
            maxY = max(cy)
            minY = min(cy)

            lightfactor, = quarkx.setupsubset()['LightFactor']
            radius = 100
            try:
                color = quakecolor(quarkx.vect(entity["color"]))
            except:
                color = makeRGBcolor(255,255,255)
            cv = view.canvas()
            cv.pencolor = color
            cv.penwidth = 2
            cv.brushstyle = BS_CLEAR
            # This is not so "correct" but works most of the times
            cv.ellipse(minX, minY, maxX, maxY)
            cv.penwidth = 1
            cv.line(down.x, down.y, up.x, up.y)
            cv.line(left.x, left.y, right.x, right.y)
            cv.line(back.x, back.y, front.x, front.y)

        parent = entity['parent']
        if parent:
            o = string.split(parent, '.')
            parent = o[0]
            cv = view.canvas()
            for i in entities:
                if i['name'] == parent:
                    cv.pencolor = 0x0080ff
                    cv.penwidth = 2
                    cv.brushstyle = BS_CLEAR
                    o = ObjectOrigin(i)
                    if o is None:return
                    Arrow(cv, view, o, org)
            

#
# Register this class with its gamename
#
quarkpy.mapentities.EntityLinesMapping.update({
  "Sylphis": SylphisDrawEntityLines()
})

