Skip to content
Snippets Groups Projects
map.py 39 KiB
Newer Older
  • Learn to ignore specific revisions
  • # -*- coding: utf-8 -*-
    
    import bpy
    import bgl
    import blf
    import sys
    import os
    import datetime
    import math
    
    from . sun_calc import degToRad, format_hms
    from . properties import Display, Sun
    
    # ---------------------------------------------------------------------------
    
    
    class MapObject:
    
        class Origin:
            x = 0
            y = 0
    
        def __init__(self, t, w, h):
            self.type = t
            self.width = w
            self.height = h
            self.heightFactor = .50
            self.opacity = 1.0
            self.focused = False
            self.view3d_area = None
            self.colorStyle = 'N'
            self.origin = self.Origin()
    
        def set_dimensions(self, width):
            self.width = width
            self.height = int(width * self.heightFactor)
    
        def check_focus(self, context, event):
            self.focused = self.is_focused(context, event)
            return self.focused
    
        def is_focused(self, context, event):
            if context.area != self.view3d_area:
                return False
    
            x = event.mouse_region_x
            y = event.mouse_region_y
    
            for reg in self.view3d_area.regions:
                if reg.type == 'WINDOW':
                    if x < 0 or x > reg.width:
                        return False
                    else:
                        break
    
            if x < self.origin.x or x > (self.origin.x + self.width) or \
                    y < self.origin.y or y > (self.origin.y + self.height) or \
                    y < 0 or y > reg.height:
                return False
            return True
    
        def near_border(self, context, event):
            if context.area != self.view3d_area:
                return False
    
            x = event.mouse_region_x
            y = event.mouse_region_y
    
            for reg in self.view3d_area.regions:
                if reg.type == 'WINDOW':
                    if x < 20 or x > (reg.width - 20) or \
                            y < 20 or y > (reg.height - 20):
                        return True
                    else:
                        break
            return False
    
    # ---------------------------------------------------------------------------
    
    
    class MapClass:
    
        class mouse:
            pass
    
        class grab:
    
            class spot:
                pass
    
            class offset:
                pass
    
        class zoom:
            pass
    
        class image:
            pass
    
        def __init__(self):
            self.handler1 = None
            self.handler2 = None
            self.view3d_area = None
            self.draw_region = None
            self.glImage = None
            self.mapLocation = 'VIEWPORT'
            self.init_zoom_preference = True
            self.reset(self.mapLocation)
    
        def init(self, location):
            self.object = [MapObject('MAP', 0, 0), MapObject('TEXT', 100, 160)]
            self.object[0].set_dimensions(200)
            self.object[1].origin.x = 10
            self.object[1].origin.y = 80
            self.mapLocation = location
    
        def zoom_preferences(self, invert_zoom_wheel, invert_mouse_zoom):
            self.init_zoom_preference = False
            if invert_zoom_wheel:
                self.zoom.wheel_up = 'OUT'
                self.zoom.wheel_down = 'IN'
            else:
                self.zoom.wheel_up = 'IN'
                self.zoom.wheel_down = 'OUT'
            if invert_mouse_zoom:
                self.zoom.mouse_up = 'IN'
                self.zoom.mouse_down = 'OUT'
            else:
                self.zoom.mouse_up = 'OUT'
                self.zoom.mouse_down = 'IN'
    
        def reset(self, location):
            self.init(location)
            self.action = None
            self.isActive = False
            self.start = False
            self.stop = False
            self.lockCrosshair = True
            self.showInfo = False
            self.latitude = 0.0
            self.longitude = 0.0
            self.ctrlPress = False
            self.altPress = False
            self.lineWidth = 1.0
            self.textureless = False
            self.mouse.x = 0
            self.mouse.y = 0
            self.grab.spot.x = 0
            self.grab.spot.y = 0
            self.grab.offset.x = 0
            self.grab.offset.y = 0
            self.zoom.width = 0
            self.zoom.x = 0
            self.zoom.y = 0
            self.image.name = None
            self.image.bindcode = 0
            self.image.loaded = False
            self.image.free_it = False
    
        def clear_callbacks(self):
            if self.mapLocation == 'PANEL':
                pl = bpy.types.SpaceProperties
            else:
                pl = bpy.types.SpaceView3D
            if self.handler2 is not None:
                pl.draw_handler_remove(self.handler2, 'WINDOW')
                self.handler2 = None
            if self.handler1 is not None:
                pl.draw_handler_remove(self.handler1, 'WINDOW')
                self.handler1 = None
    
        def set_view3d_area(self, area):
            for obj in self.object:
                obj.view3d_area = area
    
        def activate(self, context):
            if context.area.type == 'PROPERTIES':
                self.reset(self.mapLocation)
    
                def fw(self, context):
                    if self.mapLocation == 'PANEL':
                        self.draw_region = context.region
                    areas = bpy.context.screen.areas
                    for area in areas:
                        if area.type == 'VIEW_3D':
                            self.view3d_area = context.area
                            if self.mapLocation == 'PANEL':
                                return True
                            for reg in area.regions:
                                if reg.type == 'WINDOW':
                                    self.draw_region = reg
                                    Display.refresh()
                                    return True
                    return False
                if not fw(self, context):
                    self.draw_region = context.region
    
                self.set_view3d_area(self.view3d_area)
                self.start = True
                if self.mapLocation == 'PANEL':
                    self.handler1 = bpy.types.SpaceProperties.draw_handler_add(
                        Map_load_callback,
                        (self, context), 'WINDOW', 'POST_PIXEL')
                else:
                    self.handler1 = bpy.types.SpaceView3D.draw_handler_add(
                        Map_load_callback,
                        (self, context), 'WINDOW', 'POST_PIXEL')
                self.isActive = True
                return True
            else:
                return False
    
        def activateBGLcallback(self, context):
            if self.mapLocation == 'PANEL':
                self.handler2 = bpy.types.SpaceProperties.draw_handler_add(
                    Draw_map_callback,
                    (self, context), 'WINDOW', 'POST_PIXEL')
            else:
                self.handler2 = bpy.types.SpaceView3D.draw_handler_add(
                    Draw_map_callback,
                    (self, context), 'WINDOW', 'POST_PIXEL')
            self.view3d_area = context.area
            self.set_view3d_area(self.view3d_area)
            bpy.ops.sunpos.map('INVOKE_DEFAULT')
    
        def deactivate(self):
            self.clear_callbacks()
            self.stop = False
            self.action = None
            self.image.loaded = False
            if self.glImage is not None:
                try:
                    if self.glImage.bindcode == self.image.bindcode:
                        self.glImage.gl_free()
                        bpy.data.images.remove(self.glImage)
                except:
                    pass
            self.image.free_it = False
            self.glImage = None
            self.image.bindcode = 0
            self.isActive = False
    
            if Sun.SP:
                Sun.SP.ShowMap = False
    
    
        def load_blender_image(self, file_name):
            if file_name == "None":
                self.textureless = True
                self.object[0].heightFactor = .5
                return True
            else:
                self.textureless = False
    
    253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
            # S.L. fix to use any relative path
            dir_path = os.path.dirname(os.path.realpath(__file__))
            self.image.name = dir_path + os.path.sep + file_name
            if os.path.exists(self.image.name):
                try:
                    self.glImage = bpy.data.images.load(self.image.name)
                    if self.glImage is not None:
                        self.image.loaded = True
                        self.glImage.user_clear()
                        self.object[0].heightFactor = \
                            self.glImage.size[1] / self.glImage.size[0]
                        return True
                    else:
                        return False
                except:
                    pass
            return False
    
        def load_gl_image(self):
            for i in range(1, 6):  # Make up to 6 tries to load image
                self.glImage.gl_load(bgl.GL_NEAREST, bgl.GL_NEAREST)
                if self.glImage.bindcode != 0:
                    self.image.bindcode = self.glImage.bindcode
                    return True
            return False
    
        def show_text_in_viewport(self, text):
    
            def text_line(fx, fy, reduce, c, str):
                bgl.glColor4f(c[0], c[1], c[2], c[3])
                blf.position(0, fx, fy, 0)
                blf.draw(0, str)
                if reduce:
                    fy -= 20
                return fy
    
            if text.colorStyle == 'N':
                tColor = (0.8, 0.8, 0.8, 1.0)
                vColor = (1.0, 1.0, 1.0, 1.0)
            else:
                tColor = (0.0, 0.0, 0.0, 1.0)
                vColor = (0.23, 0.09, 0.18, 1.0)
    
            blf.size(0, 14, 72)
            fx = text.origin.x
            fy = text.origin.y + 140
            fy = text_line(fx + 10, fy, True, tColor, str(Sun.SP.Month) +
                           " / " + str(Sun.SP.Day) +
                           " / " + str(Sun.SP.Year))
    
            fy = text_line(fx, fy, False, tColor, "  Day: ")
            fy = text_line(fx + 40, fy, True, vColor, str(Sun.SP.Day_of_year))
            fy = text_line(fx, fy, False, tColor, "Time: ")
            fy = text_line(fx + 40, fy, True, vColor, format_hms(Sun.SP.Time))
    
            if Sun.ShowRiseSet:
                fy -= 10
                fy = text_line(fx, fy, True, tColor, "Solar Noon:")
                fy = text_line(fx + 14, fy, True, vColor,
                               format_hms(Sun.SolarNoon.time))
                fy -= 10
                fy = text_line(fx, fy, False, tColor, "Rise: ")
                if Sun.RiseSetOK:
                    fy = text_line(fx + 40, fy, True, vColor,
                                   format_hms(Sun.Sunrise.time))
                else:
                    fy = text_line(fx + 40, fy, True, vColor, "--------")
                fy = text_line(fx, fy, False, tColor, " Set: ")
                if Sun.RiseSetOK:
                    fy = text_line(fx + 40, fy, True, vColor,
                                   format_hms(Sun.Sunset.time))
                else:
                    fy = text_line(fx + 40, fy, True, vColor, "--------")
    
        def locked_crosshair_event(self, action, event):
            self.mouse.x = event.mouse_region_x
            self.mouse.y = event.mouse_region_y
            self.grab.offset.x = event.mouse_region_x
            self.grab.offset.y = event.mouse_region_y
            self.lockCrosshair = True
            Display.refresh()
            return Map_function[action](event)
    
        # -----------------------------------------------------------------------
    
        def event_controller(self, context, event):
    
            if not Sun.SP.ShowMap or event.type == 'TIMER':
                return {'PASS_THROUGH'}
    
            mapInFocus = self.object[0].check_focus(context, event)
    
            if event.type == 'MOUSEMOVE':
                if self.action == None:
                    if mapInFocus:
                        if self.object[0].near_border(context, event):
                            return {'PASS_THROUGH'}
                        else:
                            return {'RUNNING_MODAL'}
                    else:
                        return {'PASS_THROUGH'}
                if self.action in ('PAN', 'ZOOM', 'TIME', 'DAY', 'G'):
                    return self.locked_crosshair_event(self.action, event)
    
            if event.type in ('LEFT_CTRL', 'LEFT_ALT', 'RIGHT_CTRL', 'RIGHT_ALT'):
                Key_function[event.type](event)
                return {'RUNNING_MODAL'}
    
            self.object[1].check_focus(context, event)
    
            if event.type in ('MIDDLEMOUSE', 'LEFTMOUSE', 'G'):
                val = Key_function[event.type](context, event)
                if val:
                    return val
            elif event.type in ('RIGHTMOUSE', 'ESC',
                                'T', 'D', 'X', 'Y', 'R', 'S', 'E', 'A', 'O', 'C', 'H', 'F1'):
                Display.refresh()
                return Key_function[event.type](event)
    
            if self.action in ('PAN', 'ZOOM', 'TIME', 'DAY', 'G', 'O'):
                return self.locked_crosshair_event(self.action, event)
    
            if not mapInFocus:
                return {'PASS_THROUGH'}
    
            if self.action in ('X', 'Y'):
                return Map_function[self.action](event)
            else:
                self.mouse.x = event.mouse_region_x
                self.mouse.y = event.mouse_region_y
    
            if event.type == 'WHEELUPMOUSE':
                wheel_zoom(self.zoom.wheel_up)
            elif event.type == 'WHEELDOWNMOUSE':
                wheel_zoom(self.zoom.wheel_down)
            elif self.action == 'CROSS':
                self.lockCrosshair = False
                Display.refresh()
    
            return {'RUNNING_MODAL'}
    
    # ---------------------------------------------------------------------------
    
    
    Map = MapClass()
    
    
    # ---------------------------------------------------------------------------
    
    
    def key_Ctrl(event):
        if event.value == 'PRESS':
            Map.ctrlPress = True
        elif event.value == 'RELEASE':
            Map.ctrlPress = False
    
    
    def key_Alt(event):
        if event.value == 'PRESS':
            Map.altPress = True
        elif event.value == 'RELEASE':
            Map.altPress = False
    
    
    def key_Esc(event):
        if Map.object[1].focused and Map.showInfo:
            if event.value == 'RELEASE':
                Map.action = None
                Map.showInfo = False
            return {'RUNNING_MODAL'}
        if Map.object[0].focused:
            if Map.action is None:
                Map.stop = True
                return {'FINISHED'}
        if Map.action is not None:
            if event.value == 'RELEASE':
                Map.action = None
            return {'RUNNING_MODAL'}
        return {'PASS_THROUGH'}
    
    
    def key_LeftMouse(context, event):
        if event.value == 'PRESS':
            if Map.action is not None:
                Map.action = None
                Display.refresh()
                return {'RUNNING_MODAL'}
            elif Map.object[0].focused:
                if Map.object[0].near_border(context, event):
                    Map.action = 'BORDER'
                    Map.lockCrosshair = True
                    return {'PASS_THROUGH'}
                Map.action = 'CROSS'
                Map.lockCrosshair = False
                Display.refresh()
            else:
                return {'PASS_THROUGH'}
        elif event.value == 'RELEASE':
            Map.lockCrosshair = True
            Map.action = None
            Display.refresh()
            return {'PASS_THROUGH'}
    
        return False
    
    
    def key_MiddleMouse(context, event):
        if event.value == 'PRESS':
            if Map.object[1].focused and Map.showInfo:
                Map.action = 'G' if Map.action != 'G' else None
                Map.grab.spot.y = event.mouse_region_y
                Map.grab.spot.x = event.mouse_region_x
            elif Map.object[0].focused:
                if Map.ctrlPress:
                    map = Map.object[0]
                    Map.action = 'ZOOM'
                    Map.zoom.width = map.width
                    Map.zoom.x = map.origin.x
                    Map.zoom.y = map.origin.y
                else:
                    Map.action = 'PAN'
                Map.grab.spot.x = event.mouse_region_x
                Map.grab.spot.y = event.mouse_region_y
            else:
                return {'PASS_THROUGH'}
        elif event.value == 'RELEASE':
            Map.action = None
            Map.object[0].focused = False
            Map.object[1].focused = False
            Display.refresh()
        return {'RUNNING_MODAL'}
    
    # ---------------------------------------------------------------------------
    
    
    def key_A(event):
        if event.value == 'PRESS':
            if Map.object[0].focused:
                Sun.SP.ObjectGroup = 'ANALEMMA'
            else:
                return {'PASS_THROUGH'}
        return {'RUNNING_MODAL'}
    
    
    def key_C(event):
        if event.value == 'PRESS':
            if Map.object[0].focused or Map.object[1].focused:
                if Map.object[1].colorStyle == 'N':
                    Map.object[1].colorStyle = 'R'
                else:
                    Map.object[1].colorStyle = 'N'
            else:
                return {'PASS_THROUGH'}
        return {'RUNNING_MODAL'}
    
    
    def key_E(event):
        if event.value == 'PRESS':
            if Map.object[0].focused:
                Sun.SP.ObjectGroup = 'ECLIPTIC'
            else:
                return {'PASS_THROUGH'}
        return {'RUNNING_MODAL'}
    
    
    def key_G(context, event):
        if event.value == 'PRESS':
            if Map.object[1].focused and Map.showInfo:
                Map.action = 'G' if Map.action != 'G' else None
                Map.grab.spot.y = event.mouse_region_y
                Map.grab.spot.x = event.mouse_region_x
                Display.refresh()
            elif Map.object[0].focused:
                Map.action = 'PAN' if Map.action != 'PAN' else None
                Map.grab.spot.x = event.mouse_region_x
                Map.grab.spot.y = event.mouse_region_y
                return False
            else:
                return {'PASS_THROUGH'}
        return {'RUNNING_MODAL'}
    
    
    def key_H(event):
        if event.value == 'PRESS':
            if Map.object[0].focused:
                Map.action = None
                bpy.ops.object.help_operator('INVOKE_DEFAULT')
        return {'RUNNING_MODAL'}
    
    
    def key_R(event):
        if event.value == 'PRESS':
            if Map.object[0].focused:
                Map.lineWidth += 1.0
                if Map.lineWidth == 4.0:
                    Map.lineWidth = 0.0
            else:
                Map.action = None
                return {'PASS_THROUGH'}
        return {'RUNNING_MODAL'}
    
    
    def key_S(event):
        if event.value == 'PRESS':
            if Map.object[0].focused:
                Map.showInfo = True if not Map.showInfo else False
                Sun.PP.ShowRiseSet = True
                Sun.ShowRiseSet = True
            else:
                return {'PASS_THROUGH'}
        return {'RUNNING_MODAL'}
    
    
    def key_TDOXY(event):
        if event.value == 'PRESS':
            if Map.object[0].focused:
                Map.grab.spot.x = event.mouse_region_x
                Map.grab.spot.y = event.mouse_region_y
                if event.type == 'T':
                    if Map.action == 'TIME':
                        Map.action = None
                        Map.showInfo = False
                    else:
                        Map.action = 'TIME'
                        Map.showInfo = True
                elif event.type == 'D':
                    if Map.action == 'DAY':
                        Map.action = None
                        Map.showInfo = False
                    else:
                        Map.action = 'DAY'
                        Map.showInfo = True
                else:
                    Map.action = event.type
            else:
                Map.action = None
                return {'PASS_THROUGH'}
        return {'RUNNING_MODAL'}
    
    
    # ---------------------------------------------------------------------------
    
    def map_Pan(event):
        return {'RUNNING_MODAL'}
    
    
    def map_Zoom(event):
        mouse_zoom()
        return {'RUNNING_MODAL'}
    
    
    def map_Time(event):
        if event.type == 'WHEELUPMOUSE':
            time_change_wheel(Map.zoom.wheel_up)
        elif event.type == 'WHEELDOWNMOUSE':
            time_change_wheel(Map.zoom.wheel_down)
        else:
            time_change()
        return {'RUNNING_MODAL'}
    
    
    def map_Day(event):
        if event.type == 'WHEELUPMOUSE':
            day_change_wheel(Map.zoom.wheel_up)
        elif event.type == 'WHEELDOWNMOUSE':
            day_change_wheel(Map.zoom.wheel_down)
        else:
            day_change()
        return {'RUNNING_MODAL'}
    
    
    def map_G(event):
        if Map.grab.offset.x < Map.grab.spot.x:
            off = Map.grab.spot.x - Map.grab.offset.x
            Map.object[1].origin.x -= off
        else:
            off = Map.grab.offset.x - Map.grab.spot.x
            Map.object[1].origin.x += off
        if Map.grab.offset.y < Map.grab.spot.y:
            off = Map.grab.spot.y - Map.grab.offset.y
            Map.object[1].origin.y -= off
        else:
            off = Map.grab.offset.y - Map.grab.spot.y
            Map.object[1].origin.y += off
    
        Map.grab.spot.x = Map.mouse.x
        Map.grab.spot.y = Map.mouse.y
    
        return {'RUNNING_MODAL'}
    
    
    def map_O(event):
        if event.type == 'WHEELUPMOUSE':
            opacity_change_wheel(Map.zoom.wheel_up)
        elif event.type == 'WHEELDOWNMOUSE':
            opacity_change_wheel(Map.zoom.wheel_down)
        else:
            opacity_change()
        Display.refresh()
        return {'RUNNING_MODAL'}
    
    
    def map_X(event):
        if event.type == 'WHEELUPMOUSE':
            X_change_wheel(Map.zoom.wheel_up)
        elif event.type == 'WHEELDOWNMOUSE':
            X_change_wheel(Map.zoom.wheel_down)
        else:
            Map.mouse.y = Map.grab.spot.y
            Map.mouse.x = event.mouse_region_x
            Map.lockCrosshair = False
        Display.refresh()
        return {'RUNNING_MODAL'}
    
    
    def map_Y(event):
        if event.type == 'WHEELUPMOUSE':
            Y_change_wheel(Map.zoom.wheel_up)
        elif event.type == 'WHEELDOWNMOUSE':
            Y_change_wheel(Map.zoom.wheel_down)
        else:
            Map.mouse.x = Map.grab.spot.x
            Map.mouse.y = event.mouse_region_y
            Map.lockCrosshair = False
        Display.refresh()
        return {'RUNNING_MODAL'}
    
    ############################################################################
    
    Key_function = dict([('LEFT_CTRL', key_Ctrl), ('LEFT_ALT', key_Alt),
                         ('RIGHT_CTRL', key_Ctrl), ('RIGHT_ALT', key_Alt),
                         ('MIDDLEMOUSE', key_MiddleMouse),
                         ('LEFTMOUSE', key_LeftMouse),
                         ('RIGHTMOUSE', key_Esc), ('ESC', key_Esc),
                         ('A', key_A), ('E', key_E), ('G', key_G), ('C', key_C),
                         ('H', key_H), ('F1', key_H),
                         ('R', key_R), ('S', key_S),
                         ('T', key_TDOXY), ('D', key_TDOXY), ('O', key_TDOXY),
                         ('X', key_TDOXY), ('Y', key_TDOXY)])
    
    # ---------------------------------------------------------------------------
    
    Map_function = dict([('PAN', map_Pan), ('ZOOM', map_Zoom),
                         ('TIME', map_Time), ('DAY', map_Day),
                         ('G', map_G), ('O', map_O),
                         ('X', map_X), ('Y', map_Y)])
    
    ############################################################################
    
    
    def wheel_zoom(action):
        mf = 0.2 if Map.ctrlPress else 0.0
        af = 0.07 if Map.altPress else 0.0
        if action == 'IN':
            scale = 1.10 + mf - af
        else:
            scale = .90 - mf + af
        if Map.object[0].width * scale < 50:
            return
        else:
            Map.object[0].set_dimensions(int(int(Map.object[0].width * scale)))
        x = Map.mouse.x - Map.object[0].origin.x
        y = Map.mouse.y - Map.object[0].origin.y
        Map.object[0].origin.x += x - int(x * scale)
        Map.object[0].origin.y += y - int(y * scale)
        Map.lockCrosshair = True
        Display.refresh()
    
    
    def mouse_zoom():
        if Map.mouse.y > Map.grab.spot.y:
            s = Map.mouse.y - Map.grab.spot.y
            action = Map.zoom.mouse_up
        elif Map.mouse.y < Map.grab.spot.y:
            s = Map.grab.spot.y - Map.mouse.y
            action = Map.zoom.mouse_down
        else:
            s = 0
            action = Map.zoom.mouse_down
    
        if action == 'IN':
            scale = 1 + s * .01
        else:
            scale = 1 - s * .006
    
        w = int(Map.zoom.width * scale)
        if w < 50:
            return
        Map.object[0].set_dimensions(w)
        x = Map.grab.spot.x - Map.zoom.x
        y = Map.grab.spot.y - Map.zoom.y
        Map.object[0].origin.x = x - int(x * scale) + Map.zoom.x
        Map.object[0].origin.y = y - int(y * scale) + Map.zoom.y
    
    
    # ---------------------------------------------------------------------------
    
    
    def check_time_boundary():
        if Sun.SP.Time > 24.0:
            Sun.SP.Time += -24.0
        elif Sun.SP.Time < 0.0:
            Sun.SP.Time += 24.0
        Display.refresh()
    
    
    def time_change_wheel(action):
        if action == 'IN':
            val = 1.0 if not Map.altPress else 0.0166
        else:
            val = -1.0 if not Map.altPress else -0.0166
        mf = 1.5 if Map.ctrlPress else 1.0
        Sun.SP.Time += mf * val
        check_time_boundary()
    
    
    def time_change():
        if Map.mouse.x == Map.grab.spot.x:
            return
        mf = 50 if Map.ctrlPress else 1000
        if Map.altPress:
            sx = 0.0001 if Map.mouse.x > Map.grab.spot.x else -0.0001
        else:
            sx = (Map.mouse.x - Map.grab.spot.x) / mf
        Sun.SP.Time += sx
        Map.grab.spot.x = Map.mouse.x
        check_time_boundary()
    
    # ---------------------------------------------------------------------------
    
    
    def day_change_wheel(action):
        wf = wheel_factor(action)
        day_change_bounds(wf)
    
    
    def day_change():
        if Map.mouse.x == Map.grab.spot.x:
            return
        cf = 1 if Map.ctrlPress else 3
        if Map.altPress:
            mf = 1 if Map.mouse.x > Map.grab.spot.x else -1
        else:
            mf = (Map.mouse.x - Map.grab.spot.x) / cf
        day_change_bounds(mf)
        Map.grab.spot.x = Map.mouse.x
    
    
    def day_change_bounds(wf):
        if Sun.SP.Day_of_year + wf > 366:
            Sun.SP.Day_of_year = 1
            Sun.SP.Year += 1
        elif Sun.SP.Day_of_year + wf < 1:
            Sun.SP.Day_of_year = 366
            Sun.SP.Year -= 1
        else:
            Sun.SP.Day_of_year += wf
        dt = (datetime.date(Sun.SP.Year, 1, 1) +
              datetime.timedelta(Sun.SP.Day_of_year - 1))
        Sun.SP.Day = dt.day
        Sun.SP.Month = dt.month
        Display.refresh()
    
    # ---------------------------------------------------------------------------
    
    
    def opacity_change_wheel(action):
        if action == 'IN':
            val = 0.05 if not Map.altPress else 0.01
        else:
            val = -0.05 if not Map.altPress else -0.01
        mf = 1.5 if Map.ctrlPress else 1.0
        obj = Map.object[0]
        obj.opacity += mf * val
        opacity_change_bounds(obj)
    
    
    def opacity_change():
        if Map.mouse.x > Map.grab.spot.x:
            s = Map.mouse.x - Map.grab.spot.x
            action = '+'
        elif Map.mouse.x < Map.grab.spot.x:
            s = Map.grab.spot.x - Map.mouse.x
            action = '-'
        else:
            action = '-'
            s = 0
        s /= 10
        obj = Map.object[0]
    
        if action == '+':
            scale = obj.opacity + s * .002
        else:
            scale = obj.opacity - s * .002
        obj.opacity = scale
        opacity_change_bounds(obj)
    
    
    def opacity_change_bounds(obj):
        if obj.opacity > 1.0:
            obj.opacity = 1.0
            Map.grab.spot.x = Map.mouse.x
        elif obj.opacity < 0.0:
            obj.opacity = 0.0
            Map.grab.spot.x = Map.mouse.x
    
    # ---------------------------------------------------------------------------
    
    
    def X_change_wheel(action):
        wf = wheel_factor(action)
        if Sun.SP.Longitude + wf > 180.0:
            Sun.SP.Longitude = -180.0
        elif Sun.SP.Longitude + wf < -180.0:
            Sun.SP.Longitude = 180.0
        else:
            Sun.SP.Longitude += wf
    
    
    def Y_change_wheel(action):
        wf = wheel_factor(action)
        if Sun.SP.Latitude + wf > 90.0:
            Sun.SP.Latitude = -90.0
        elif Sun.SP.Latitude + wf < -90.0:
            Sun.SP.Latitude = 90.0
        else:
            Sun.SP.Latitude += wf
    
    
    def wheel_factor(action):
        if action == 'IN':
            val = 5.0 if not Map.altPress else 1.0
        else:
            val = -5.0 if not Map.altPress else -1.0
        wf = 3 * val if Map.ctrlPress else 1.0 * val
        return wf
    
    ############################################################################
    
    
    def Map_load_callback(self, context):
    
        if Sun.SP.ShowMap and not Map.image.loaded:
            Map.glImage = None
            if not Map.load_blender_image(Sun.MapName):
                print("Could not load image file: ", Map.image.name)
                Sun.SP.ShowMap = False
    
        if Map.start:
            def set_region_data():
                Map.activateBGLcallback(context)
                bgl.glTexParameteri(bgl.GL_TEXTURE_2D,
                                    bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR)
                bgl.glTexParameteri(bgl.GL_TEXTURE_2D,
                                    bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR)
                Map.object[0].set_dimensions(0)
                Map.toolProps = None
                Map.toolPropsWidth = 0
                for reg in Map.view3d_area.regions:
                    if reg.type == 'TOOL_PROPS':
                        Map.toolProps = reg
                        Map.toolProps_width = reg.width
                    elif reg.type == 'WINDOW':
                        Map.region = reg
                        Map.saved_region_width = reg.width
                        Map.object[0].set_dimensions(int(reg.width * .8))
                        Map.object[0].origin.x = \
                            int((Map.region.width - Map.object[0].width) / 2)
                        Map.object[0].origin.y = 2
                return
    
            Map.start = False
    
            if Map.image.loaded:
                if not Map.load_gl_image():
                    print("Could not load image file: ", Map.image.name)
                elif Map.glImage.bindcode != 0:
                    Map.image.free_it = True
                    set_region_data()
                    return
                print("Could not get texture in gl_load()")
                Map.glImage = None
                Map.image.bindcode = 0
                Sun.SP.ShowMap = False
    
            elif Map.textureless:
                set_region_data()
                return
            else:
                Sun.SP.ShowMap = False
            return
    
        if Map.stop:
            Map.deactivate()
            return
    
        return
    
    
    ############################################################################
    
    
    def Draw_map_callback(self, context):
    
        if context.area != Map.view3d_area:
            return
        elif context.area.type == 'PROPERTIES' and \
                context.space_data.context != 'WORLD':
            return
    
        # Check if window area has changed for sticky zoom
        theMap = Map.object[0]
        if Map.region.width < Map.saved_region_width:
            diff = Map.saved_region_width - Map.region.width
            if theMap.origin.x + theMap.width > Map.saved_region_width:
                if theMap.origin.x > 0:
                    theMap.origin.x -= diff
                else:
                    theMap.width -= diff
            else:
                if Map.toolProps is not None:
                    if Map.toolProps.width > Map.toolProps_width:
                        theMap.origin.x -= diff
                    Map.toolProps_width = Map.toolProps.width
                if theMap.origin.x < 0:
                    theMap.origin.x += diff
        else:
            diff = Map.region.width - Map.saved_region_width
            if theMap.width > Map.saved_region_width:
                theMap.width += diff
            else:
                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 = theMap.width / 2
        longFac = zLong / 180
        zLat = theMap.height / 2
        latFac = zLat / 90
        crossChange = True