Skip to content
Snippets Groups Projects
map.py 40 KiB
Newer Older
  • Learn to ignore specific revisions
  •             if Map.toolProps is not None:
                    if Map.toolProps.width < Map.toolProps_width:
                        theMap.origin.x += diff
                    Map.toolProps_width = Map.toolProps.width
        theMap.set_dimensions(theMap.width)
        Map.saved_region_width = Map.region.width
    
        # Latitude and longitude are set to an equidistant
        # cylindrical projection with lat/long 0/0 exactly
        # in the middle of the image.
    
    
        zLong = the_map.width / 2
        longFac = z_long / 180
        zLat = the_map.height / 2
        latFac = z_lat / 90
    
        crossChange = True
    
        if not Map.action == 'PAN':
            x = Map.mouse.x
            y = Map.mouse.y
            if x < theMap.origin.x or x > theMap.origin.x + theMap.width:
                crossChange = False
                x = 0
            else:
                testBoundary = theMap.origin.x + theMap.width
                if testBoundary < Map.region.width:
                    rightBoundary = testBoundary
                else:
                    rightBoundary = Map.region.width
                if x > rightBoundary:
                    crossChange = False
                    x = rightBoundary
            cX = x - zLong - theMap.origin.x
    
            if longFac:
                newLongitude = cX / longFac
            else:
                newLongitude = 0.0
    
            if y < theMap.origin.y or y < 0:
                crossChange = False
                y = 0
            elif y > theMap.origin.y + theMap.height:
                crossChange = False
                y = theMap.origin.y + theMap.height
            cY = y - zLat - theMap.origin.y
    
            if latFac:
                newLatitude = cY / latFac
            else:
                newLatitude = 0.0
    
            if newLatitude == Map.latitude and newLongitude == Map.longitude:
                crossChange = False
            else:
                Map.latitude = newLatitude
                Map.longitude = newLongitude
        else:
            if Map.grab.offset.x < Map.grab.spot.x:
                off = Map.grab.spot.x - Map.grab.offset.x
                theMap.origin.x -= off
            else:
                off = Map.grab.offset.x - Map.grab.spot.x
                theMap.origin.x += off
            if Map.grab.offset.y < Map.grab.spot.y:
                off = Map.grab.spot.y - Map.grab.offset.y
                theMap.origin.y -= off
            else:
                off = Map.grab.offset.y - Map.grab.spot.y
                theMap.origin.y += off
            Map.grab.spot.x = Map.mouse.x
            Map.grab.spot.y = Map.mouse.y
    
        Lx = theMap.origin.x
        Ly = theMap.origin.y
    
        # ---------------------
        # Draw a textured quad
        # ---------------------
    
        if not Map.textureless:
            bgl.glEnable(bgl.GL_BLEND)
            if Map.glImage.bindcode == 0:
                Map.load_gl_image()
            bgl.glBindTexture(bgl.GL_TEXTURE_2D, Map.glImage.bindcode[0])
            bgl.glEnable(bgl.GL_TEXTURE_2D)
            bgl.glColor4f(1.0, 1.0, 1.0, Map.object[0].opacity)
            bgl.glBegin(bgl.GL_QUADS)
            bgl.glTexCoord2f(0.0, 0.0)
            bgl.glVertex2f(Lx, Ly)
            bgl.glTexCoord2f(1.0, 0.0)
            bgl.glVertex2f(Lx + theMap.width, Ly)
            bgl.glTexCoord2f(1.0, 1.0)
            bgl.glVertex2f(Lx + theMap.width, Ly + theMap.height)
            bgl.glTexCoord2f(0.0, 1.0)
            bgl.glVertex2f(Lx, theMap.height + Ly)
            bgl.glEnd()
            bgl.glDisable(bgl.GL_TEXTURE_2D)
    
        # -----------------------
        # Output text for stats
        # -----------------------
        if Map.action == 'TIME' or Map.action == 'DAY' or Map.showInfo:
            Map.show_text_in_viewport(Map.object[1])
    
        # ---------------------
        # draw the crosshair
        # ---------------------
        x = theMap.width / 2.0
        if crossChange and not Map.lockCrosshair:
            if Map.action != 'Y':
    
                sun.sp.longitude = newLongitude
        longitude = (sun.sp.longitude * x / 180.0) + x
    
    
        bgl.glEnable(bgl.GL_BLEND)
        bgl.glEnable(bgl.GL_LINES)
        bgl.glLineWidth(1.0)
        alpha = 1.0 if Map.action == 'Y' else 0.5
        color = (0.894, 0.741, .510, alpha)
        bgl.glColor4f(color[0], color[1], color[2], color[3])
        bgl.glBegin(bgl.GL_LINES)
        bgl.glVertex2f(Lx + longitude, Ly)
        bgl.glVertex2f(Lx + longitude, Ly + theMap.height)
        bgl.glEnd()
    
        y = theMap.height / 2.0
        if crossChange and not Map.lockCrosshair:
            if Map.action != 'X':
    
                sun.sp.latitude = newLatitude
        latitude = (sun.sp.latitude * y / 90.0) + y
    
    
        alpha = 1.0 if Map.action == 'X' else 0.5
        color = (0.894, 0.741, .510, alpha)
        bgl.glColor4f(color[0], color[1], color[2], color[3])
        bgl.glBegin(bgl.GL_LINES)
        bgl.glVertex2f(Lx, Ly + latitude)
        bgl.glVertex2f(Lx + theMap.width, Ly + latitude)
        bgl.glEnd()
    
        # ---------------------
        # draw the border
        # ---------------------
        bgl.glDisable(bgl.GL_BLEND)
        color = (0.6, 0.6, .6, 1.0)
        bgl.glColor4f(color[0], color[1], color[2], color[3])
        bgl.glBegin(bgl.GL_LINE_LOOP)
        bgl.glVertex2f(Lx, Ly)
        bgl.glVertex2f(Lx + theMap.width, Ly)
        bgl.glVertex2f(Lx + theMap.width, Ly + theMap.height)
        bgl.glVertex2f(Lx, theMap.height + Ly)
        bgl.glVertex2f(Lx, Ly)
        bgl.glEnd()
    
    
        if not sun.ShowRiseSet or not Map.lineWidth:
    
            bgl.glDisable(bgl.GL_LINES)
            bgl.glFlush()
            return
    
        if Map.action == 'G':
            draw_text_region()
    
        # ------------------------
        # draw the sunrise, sunset
        # ------------------------
    
        def draw_angled_line(color, angle, bx, by):
            x = math.cos(angle) * radius
            y = math.sin(angle) * radius
            bgl.glColor4f(color[0], color[1], color[2], color[3])
            bgl.glBegin(bgl.GL_LINES)
            bgl.glVertex2f(bx, by)
            bgl.glVertex2f(bx + x, by + y)
            bgl.glEnd()
    
        px = Lx + longitude
        py = Ly + latitude
    
        radius = 30 + Map.lineWidth * 10
    
        if sun.RiseSetOK and Map.lineWidth:
    
            angle = -(radians(sun.sunrise.azimuth) - math.pi / 2)
    
            bgl.glLineWidth(Map.lineWidth)
            draw_angled_line(color, angle, px, py)
    
            color = (0.86, 0.18, 0.18, 0.9)
    
            angle = -(radians(sun.sunset.azimuth) - math.pi / 2)
    
            draw_angled_line(color, angle, px, py)
    
        # ------------------------
        # draw current time line
        # ------------------------
    
        if Map.textureless:
    
            phi = radians(sun.AzNorth) * -1
    
            phi = radians(sun.Azimuth) * -1
        x = math.sin(phi) * math.sin(-sun.Theta) * (radius + 10)
        y = math.sin(sun.Theta) * math.cos(phi) * (radius + 10)
    
        night = (0.24, 0.29, 0.94, 0.9)
        day = (0.85, 0.77, 0.60, 0.9)
    
        if sun.SolarNoon.elevation < 0.0:
    
        elif sun.Elevation >= sun.Sunrise.elevation:
            if sun.Time >= sun.Sunset.time and \
                    sun.Elevation <= sun.Sunset.elevation:
    
                color = night
            else:
                color = day
        else:
            color = night
        bgl.glLineWidth(Map.lineWidth + 1.0)
    
        bgl.glColor4f(color[0], color[1], color[2], color[3])
        bgl.glBegin(bgl.GL_LINES)
        bgl.glVertex2f(px, py)
        bgl.glVertex2f(px + x, py + y)
        bgl.glEnd()
        bgl.glLineWidth(1.0)
        bgl.glDisable(bgl.GL_LINES)
        bgl.glFlush()
    
    # ---------------------------------------------------------------------------
    
    
    def draw_text_region():
        x = Map.object[1].origin.x
        y = Map.object[1].origin.y - 5
    
        bgl.glEnable(bgl.GL_BLEND)
        color = (0.894, 0.741, .510, 0.2)
        bgl.glColor4f(color[0], color[1], color[2], color[3])
        bgl.glBegin(bgl.GL_QUADS)
        bgl.glVertex2f(x, y)
        bgl.glVertex2f(x + Map.object[1].width, y)
        bgl.glVertex2f(x + Map.object[1].width, y + Map.object[1].height)
        bgl.glVertex2f(x, Map.object[1].height + y)
        bgl.glEnd()
        bgl.glDisable(bgl.GL_BLEND)
    
    
    # ---------------------------------------------------------------------------
    
    
    class SunPos_Help(bpy.types.Operator):
        bl_idname = "object.help_operator"
        bl_label = "Map help"
    
        def execute(self, context):
            self.report({'INFO'}, self.message)
            return {'FINISHED'}
    
        def invoke(self, context, event):
            wm = context.window_manager
            return wm.invoke_popup(self, width=400, height=200)
    
        def draw(self, context):
    
            self.layout.label(text="Available map commands:")
    
    NBurn's avatar
    NBurn committed
            split = row.split(factor=.27)
    
            colL = split.column()
            colR = split.column()
    
            colL.label(text="Esc or Right Mouse ")
    
            colR.label("Close map or text.")
    
            colL.label(text="Left Mouse")
            colR.label(text="Move crosshair.")
            colL.label(text="G or MiddleMouse")
    
            colR.label(text="Pan mode. Grab and move map or text.")
    
            colL.label(text="Ctrl Middlemouse")
            colR.label(text="Mouse zoom to point.")
            colL.label(text="C")
    
            colR.label(text="Invert text color.")
    
            colL.label(text="R")
            colR.label(text="Toggle through thickness of the radiating rise/set lines.")
            colL.label(text="S")
            colR.label(text="Show statistics data. Move with pan command.")
    
            self.layout.label(text="----- The following are changed by moving " +
    
                              "the mouse or using the scroll wheel.")
    
            self.layout.label(text="----- Use Ctrl for coarse increments or Alt for fine.")
    
    NBurn's avatar
    NBurn committed
            split = row.split(factor=.25)
    
            colL = split.column()
            colR = split.column()
    
            colL.label(text="Scroll wheel")
            colR.label(text="Zoom to point.")
            colL.label(text="T")
            colR.label(text="Time mode.")
            colL.label(text="D")
            colR.label(text="Day mode.")
            colL.label(text="O")
            colR.label(text="Change map opacity.")
            colL.label(text="X")
            colR.label(text="Lock latitude and change longitude.")
            colL.label(text="Y")
            colR.label(text="Lock longitude and change latitude.")
            colL.label(text="A")
            colR.label(text="Show analemma if object group is set.")
            colL.label(text="E")
            colR.label(text="Show ecliptic if object group is set.")